blob: 1008dd5ab9a0b20d7686c5dfdc9f5cc892b10809 [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 */
54#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020055#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010056#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020058/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010059 * Quickfix list type.
60 */
61typedef enum
62{
63 QFLT_QUICKFIX, // Quickfix list - global list
64 QFLT_LOCATION, // Location list - per window list
65 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
66} qfltype_T;
67
68/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020069 * Quickfix/Location list definition
70 * Contains a list of entries (qfline_T). qf_start points to the first entry
71 * and qf_last points to the last entry. qf_count contains the list size.
72 *
73 * Usually the list contains one or more entries. But an empty list can be
74 * created using setqflist()/setloclist() with a title and/or user context
75 * information and entries can be added later using setqflist()/setloclist().
76 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000077typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000078{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020079 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010080 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020081 qfline_T *qf_start; // pointer to the first error
82 qfline_T *qf_last; // pointer to the last error
83 qfline_T *qf_ptr; // pointer to the current error
84 int qf_count; // number of errors (0 means empty list)
85 int qf_index; // current index in the error list
86 int qf_nonevalid; // TRUE if not a single valid entry found
Tom Praschanca6ac992023-08-11 23:26:12 +020087 int qf_has_user_data; // TRUE if at least one item has user_data attached
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020088 char_u *qf_title; // title derived from the command that created
89 // the error list or set by setqflist
90 typval_T *qf_ctx; // context set by setqflist/setloclist
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +010091 callback_T qf_qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020092
93 struct dir_stack_T *qf_dir_stack;
94 char_u *qf_directory;
95 struct dir_stack_T *qf_file_stack;
96 char_u *qf_currfile;
97 int qf_multiline;
98 int qf_multiignore;
99 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +0100100 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000101} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar6a8958d2017-06-22 21:33:20 +0200103/*
104 * Quickfix/Location list stack definition
105 * Contains a list of quickfix/location lists (qf_list_T)
106 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000107struct qf_info_S
108{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200109 // Count of references to this list. Used only for location lists.
110 // When a location list window reference this list, qf_refcount
111 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
112 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200114 int qf_listcount; // current number of lists
115 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000116 qf_list_T qf_lists[LISTCOUNT];
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
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200121static qf_info_T ql_info; // global quickfix list
122static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200124#define FMT_PATTERNS 14 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
126/*
127 * Structure used to hold the info of one part of 'errorformat'
128 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000129typedef struct efm_S efm_T;
130struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200132 regprog_T *prog; // pre-formatted part of 'errorformat'
133 efm_T *next; // pointer to next (NULL if last)
134 char_u addr[FMT_PATTERNS]; // indices of used % patterns
135 char_u prefix; // prefix of this format line:
136 // 'D' enter directory
137 // 'X' leave directory
138 // 'A' start of multi-line message
139 // 'E' error message
140 // 'W' warning message
141 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200142 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200143 // 'C' continuation line
144 // 'Z' end of multi-line message
145 // 'G' general, unspecific message
146 // 'P' push file (partial) message
147 // 'Q' pop/quit file (partial) message
148 // 'O' overread (partial) message
149 char_u flags; // additional flags given in prefix
150 // '-' do not include this line
151 // '+' include whole line in message
152 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153};
154
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200155// List of location lists to be deleted.
156// Used to delay the deletion of locations lists by autocmds.
157typedef struct qf_delq_S
158{
159 struct qf_delq_S *next;
160 qf_info_T *qi;
161} qf_delq_T;
162static qf_delq_T *qf_delq_head = NULL;
163
164// Counter to prevent autocmds from freeing up location lists when they are
165// still being used.
166static int quickfix_busy = 0;
167
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200168static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100169
Bram Moolenaard43906d2020-07-20 21:31:32 +0200170// callback function for 'quickfixtextfunc'
171static callback_T qftf_cb;
172
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Tom Praschanca6ac992023-08-11 23:26:12 +0200174static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, long end_lnum, int col, int end_col, int vis_col, char_u *pattern, int nr, int type, typval_T *user_data, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200175static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100177static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200178static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200180static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200181static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +0100182static void qf_fmt_text(garray_T *gap, char_u *text);
183static void qf_range_text(garray_T *gap, qfline_T *qfp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100184static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100185static win_T *qf_find_win(qf_info_T *qi);
186static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200187static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200188static 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 +0100189static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
190static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
191static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
192static qf_info_T *ll_get_or_alloc_list(win_T *);
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +0200193static 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 +0000194
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200195// Quickfix window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000196#define IS_QF_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200197// Location list window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000198#define IS_LL_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200199
200// Quickfix and location list stack check helper macros
kylo252ae6f1d82022-02-16 19:24:07 +0000201#define IS_QF_STACK(qi) ((qi)->qfl_type == QFLT_QUICKFIX)
202#define IS_LL_STACK(qi) ((qi)->qfl_type == QFLT_LOCATION)
203#define IS_QF_LIST(qfl) ((qfl)->qfl_type == QFLT_QUICKFIX)
204#define IS_LL_LIST(qfl) ((qfl)->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200205
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000206/*
207 * Return location list for window 'wp'
208 * For location list window, return the referenced location list
209 */
kylo252ae6f1d82022-02-16 19:24:07 +0000210#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? (wp)->w_llist_ref : (wp)->w_llist)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000211
Bram Moolenaar95946f12019-03-31 15:31:59 +0200212// Macro to loop through all the items in a quickfix list
213// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100214#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
kylo252ae6f1d82022-02-16 19:24:07 +0000215 for ((i) = 1, (qfp) = (qfl)->qf_start; \
216 !got_int && (i) <= (qfl)->qf_count && (qfp) != NULL; \
217 ++(i), (qfp) = (qfp)->qf_next)
Bram Moolenaara16123a2019-03-28 20:31:07 +0100218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200220 * Looking up a buffer can be slow if there are many. Remember the last one
221 * to make this a lot faster if there are multiple matches in the same file.
222 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200223static char_u *qf_last_bufname = NULL;
224static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200225
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +0100226static garray_T qfga;
227
228/*
229 * Get a growarray to buffer text in. Shared between various commands to avoid
230 * many alloc/free calls.
231 */
232 static garray_T *
233qfga_get(void)
234{
235 static int initialized = FALSE;
236
237 if (!initialized)
238 {
239 initialized = TRUE;
240 ga_init2(&qfga, 1, 256);
241 }
242
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +0100243 // Reset the length to zero. Retain ga_data from previous use to avoid
244 // many alloc/free calls.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +0100245 qfga.ga_len = 0;
246
247 return &qfga;
248}
249
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200250/*
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +0100251 * The "qfga" grow array buffer is reused across multiple quickfix commands as
252 * a temporary buffer to reduce the number of alloc/free calls. But if the
253 * buffer size is large, then to avoid holding on to that memory, clear the
254 * grow array. Otherwise just reset the grow array length.
255 */
256 static void
257qfga_clear(void)
258{
259 if (qfga.ga_maxlen > 1000)
260 ga_clear(&qfga);
261 else
262 qfga.ga_len = 0;
263}
264
265/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200266 * Maximum number of bytes allowed per line while reading a errorfile.
267 */
268#define LINE_MAXLEN 4096
269
haya14busae023d492022-02-08 18:09:29 +0000270/*
271 * Patterns used. Keep in sync with qf_parse_fmt[].
272 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200273static struct fmtpattern
274{
275 char_u convchar;
276 char *pattern;
277} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200278 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200279 {'f', ".\\+"}, // only used when at end
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200280 {'b', "\\d\\+"}, // 1
281 {'n', "\\d\\+"}, // 2
282 {'l', "\\d\\+"}, // 3
283 {'e', "\\d\\+"}, // 4
284 {'c', "\\d\\+"}, // 5
285 {'k', "\\d\\+"}, // 6
286 {'t', "."}, // 7
287#define FMT_PATTERN_M 8
288 {'m', ".\\+"}, // 8
289#define FMT_PATTERN_R 9
290 {'r', ".*"}, // 9
291 {'p', "[- .]*"}, // 10
292 {'v', "\\d\\+"}, // 11
293 {'s', ".\\+"}, // 12
294 {'o', ".\\+"} // 13
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200295 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200296
297/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200298 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200299 * See fmt_pat definition above for the list of supported patterns. The
300 * pattern specifier is supplied in "efmpat". The converted pattern is stored
301 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 */
303 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304efmpat_to_regpat(
305 char_u *efmpat,
306 char_u *regpat,
307 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100309 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200310{
311 char_u *srcptr;
312
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200313 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200315 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000316 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200317 return NULL;
318 }
haya14busae023d492022-02-08 18:09:29 +0000319 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000321 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200322 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200323 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000324 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325 return NULL;
326 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200327 efminfo->addr[idx] = (char_u)++round;
328 *regpat++ = '\\';
329 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200331 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200332 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200333 // Also match "c:" in the file name, even when
334 // checking for a colon next: "%f:".
335 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200336 STRCPY(regpat, "\\%(\\a:\\)\\=");
337 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338 }
339#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200340 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200341 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200342 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200344 // A file name may contain spaces, but this isn't
345 // in "\f". For "%f:%l:%m" there may be a ":" in
346 // the file name. Use ".\{-1,}x" instead (x is
347 // the next character), the requirement that :999:
348 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200349 STRCPY(regpat, ".\\{-1,}");
350 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 }
352 else
353 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200354 // File name followed by '\\' or '%': include as
355 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200356 STRCPY(regpat, "\\f\\+");
357 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200358 }
359 }
360 else
361 {
362 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200363 while ((*regpat = *srcptr++) != NUL)
364 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200365 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200366 *regpat++ = '\\';
367 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200369 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370}
371
372/*
373 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375 */
376 static char_u *
377scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200378 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200379 char_u *efm,
380 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100381 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382{
383 char_u *efmp = *pefmp;
384
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200385 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200386 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200387 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200388 {
389 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 if (efmp < efm + len)
392 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200393 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200395 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200396 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200397 if (efmp == efm + len)
398 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000399 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200400 return NULL;
401 }
402 }
403 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200404 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200405 *regpat++ = *++efmp;
406 *regpat++ = '\\';
407 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200408 }
409 else
410 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200411 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000412 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200413 return NULL;
414 }
415
416 *pefmp = efmp;
417
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200418 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200419}
420
421/*
422 * Analyze/parse an errorformat prefix.
423 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200424 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100425efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200426{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200427 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200429 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200430 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200431 else
432 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000433 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200434 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200435 }
436
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200437 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200438}
439
440/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200441 * Converts a 'errorformat' string part in 'efm' to a regular expression
442 * pattern. The resulting regex pattern is returned in "regpat". Additional
443 * information about the 'erroformat' pattern is returned in "fmt_ptr".
444 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200445 */
446 static int
447efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200448 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200449 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200450 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100451 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200452{
453 char_u *ptr;
454 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200455 int round;
456 int idx = 0;
457
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200458 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200459 ptr = regpat;
460 *ptr++ = '^';
461 round = 0;
462 for (efmp = efm; efmp < efm + len; ++efmp)
463 {
464 if (*efmp == '%')
465 {
466 ++efmp;
467 for (idx = 0; idx < FMT_PATTERNS; ++idx)
468 if (fmt_pat[idx].convchar == *efmp)
469 break;
470 if (idx < FMT_PATTERNS)
471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100472 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200473 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200474 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200475 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200476 }
477 else if (*efmp == '*')
478 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200479 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100480 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200481 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200482 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200483 }
484 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200485 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200486 else if (*efmp == '#')
487 *ptr++ = '*';
488 else if (*efmp == '>')
489 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200490 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200491 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200492 // prefix is allowed only at the beginning of the errorformat
493 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100494 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200495 if (efmp == NULL)
496 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200497 }
498 else
499 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000500 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200501 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200502 }
503 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200504 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200505 {
506 if (*efmp == '\\' && efmp + 1 < efm + len)
507 ++efmp;
508 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200509 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200510 if (*efmp)
511 *ptr++ = *efmp;
512 }
513 }
514 *ptr++ = '$';
515 *ptr = NUL;
516
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200517 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200518}
519
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200520/*
521 * Free the 'errorformat' information list
522 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200523 static void
524free_efm_list(efm_T **efm_first)
525{
526 efm_T *efm_ptr;
527
528 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
529 {
530 *efm_first = efm_ptr->next;
531 vim_regfree(efm_ptr->prog);
532 vim_free(efm_ptr);
533 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100534 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200535}
536
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200537/*
538 * Compute the size of the buffer used to convert a 'errorformat' pattern into
539 * a regular expression pattern.
540 */
541 static int
542efm_regpat_bufsz(char_u *efm)
543{
544 int sz;
545 int i;
546
547 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
548 for (i = FMT_PATTERNS; i > 0; )
549 sz += (int)STRLEN(fmt_pat[--i].pattern);
550#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200551 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200552#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200553 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200554#endif
555
556 return sz;
557}
558
559/*
560 * Return the length of a 'errorformat' option part (separated by ",").
561 */
562 static int
563efm_option_part_len(char_u *efm)
564{
565 int len;
566
567 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
568 if (efm[len] == '\\' && efm[len + 1] != NUL)
569 ++len;
570
571 return len;
572}
573
574/*
575 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
576 * are parsed and converted to regular expressions. Returns information about
577 * the parsed 'errorformat' option.
578 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200579 static efm_T *
580parse_efm_option(char_u *efm)
581{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200582 efm_T *fmt_ptr = NULL;
583 efm_T *fmt_first = NULL;
584 efm_T *fmt_last = NULL;
585 char_u *fmtstr = NULL;
586 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200587 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200588
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200589 // Each part of the format string is copied and modified from errorformat
590 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200591
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200592 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200593 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000594 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200595 goto parse_efm_error;
596
597 while (efm[0] != NUL)
598 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200599 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000600 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200601 if (fmt_ptr == NULL)
602 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200603 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200604 fmt_first = fmt_ptr;
605 else
606 fmt_last->next = fmt_ptr;
607 fmt_last = fmt_ptr;
608
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200609 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200610 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200611
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100612 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200613 goto parse_efm_error;
614 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
615 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200616 // Advance to next part
617 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200618 }
619
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200620 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000621 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200622
623 goto parse_efm_end;
624
625parse_efm_error:
626 free_efm_list(&fmt_first);
627
628parse_efm_end:
629 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200630
631 return fmt_first;
632}
633
Bram Moolenaare0d37972016-07-15 22:36:01 +0200634enum {
635 QF_FAIL = 0,
636 QF_OK = 1,
637 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200638 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200639 QF_IGNORE_LINE = 4,
640 QF_MULTISCAN = 5,
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +0100641 QF_ABORT = 6
Bram Moolenaare0d37972016-07-15 22:36:01 +0200642};
643
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200644/*
645 * State information used to parse lines and add entries to a quickfix/location
646 * list.
647 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200648typedef struct {
649 char_u *linebuf;
650 int linelen;
651 char_u *growbuf;
652 int growbufsiz;
653 FILE *fd;
654 typval_T *tv;
655 char_u *p_str;
656 listitem_T *p_li;
657 buf_T *buf;
658 linenr_T buflnum;
659 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100660 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200661} qfstate_T;
662
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200663/*
664 * Allocate more memory for the line buffer used for parsing lines.
665 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200666 static char_u *
667qf_grow_linebuf(qfstate_T *state, int newsz)
668{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200669 char_u *p;
670
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200671 // If the line exceeds LINE_MAXLEN exclude the last
672 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200673 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
674 if (state->growbuf == NULL)
675 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000676 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200677 if (state->growbuf == NULL)
678 return NULL;
679 state->growbufsiz = state->linelen;
680 }
681 else if (state->linelen > state->growbufsiz)
682 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200683 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200684 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200685 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200686 state->growbufsiz = state->linelen;
687 }
688 return state->growbuf;
689}
690
691/*
692 * Get the next string (separated by newline) from state->p_str.
693 */
694 static int
695qf_get_next_str_line(qfstate_T *state)
696{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200697 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200698 char_u *p_str = state->p_str;
699 char_u *p;
700 int len;
701
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200702 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200703 return QF_END_OF_INPUT;
704
705 p = vim_strchr(p_str, '\n');
706 if (p != NULL)
707 len = (int)(p - p_str) + 1;
708 else
709 len = (int)STRLEN(p_str);
710
711 if (len > IOSIZE - 2)
712 {
713 state->linebuf = qf_grow_linebuf(state, len);
714 if (state->linebuf == NULL)
715 return QF_NOMEM;
716 }
717 else
718 {
719 state->linebuf = IObuff;
720 state->linelen = len;
721 }
722 vim_strncpy(state->linebuf, p_str, state->linelen);
723
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200724 // Increment using len in order to discard the rest of the
725 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200726 p_str += len;
727 state->p_str = p_str;
728
729 return QF_OK;
730}
731
732/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000733 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200734 */
735 static int
736qf_get_next_list_line(qfstate_T *state)
737{
738 listitem_T *p_li = state->p_li;
739 int len;
740
741 while (p_li != NULL
742 && (p_li->li_tv.v_type != VAR_STRING
743 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200744 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200745
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200746 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200747 {
748 state->p_li = NULL;
749 return QF_END_OF_INPUT;
750 }
751
752 len = (int)STRLEN(p_li->li_tv.vval.v_string);
753 if (len > IOSIZE - 2)
754 {
755 state->linebuf = qf_grow_linebuf(state, len);
756 if (state->linebuf == NULL)
757 return QF_NOMEM;
758 }
759 else
760 {
761 state->linebuf = IObuff;
762 state->linelen = len;
763 }
764
765 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
766
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200767 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200768 return QF_OK;
769}
770
771/*
772 * Get the next string from state->buf.
773 */
774 static int
775qf_get_next_buf_line(qfstate_T *state)
776{
777 char_u *p_buf = NULL;
778 int len;
779
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200780 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200781 if (state->buflnum > state->lnumlast)
782 return QF_END_OF_INPUT;
783
784 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +0100785 len = ml_get_buf_len(state->buf, state->buflnum);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 state->buflnum += 1;
787
Bram Moolenaare0d37972016-07-15 22:36:01 +0200788 if (len > IOSIZE - 2)
789 {
790 state->linebuf = qf_grow_linebuf(state, len);
791 if (state->linebuf == NULL)
792 return QF_NOMEM;
793 }
794 else
795 {
796 state->linebuf = IObuff;
797 state->linelen = len;
798 }
799 vim_strncpy(state->linebuf, p_buf, state->linelen);
800
801 return QF_OK;
802}
803
804/*
805 * Get the next string from file state->fd.
806 */
807 static int
808qf_get_next_file_line(qfstate_T *state)
809{
810 int discard;
811 int growbuflen;
812
813 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
814 return QF_END_OF_INPUT;
815
816 discard = FALSE;
817 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200818 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200819 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200820 // The current line exceeds IObuff, continue reading using
821 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200822 if (state->growbuf == NULL)
823 {
824 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000825 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200826 if (state->growbuf == NULL)
827 return QF_NOMEM;
828 }
829
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200830 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200831 memcpy(state->growbuf, IObuff, IOSIZE - 1);
832 growbuflen = state->linelen;
833
834 for (;;)
835 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200836 char_u *p;
837
Bram Moolenaare0d37972016-07-15 22:36:01 +0200838 if (fgets((char *)state->growbuf + growbuflen,
839 state->growbufsiz - growbuflen, state->fd) == NULL)
840 break;
841 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
842 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200843 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200844 break;
845 if (state->growbufsiz == LINE_MAXLEN)
846 {
847 discard = TRUE;
848 break;
849 }
850
851 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
852 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200853 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200854 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200855 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200856 }
857
858 while (discard)
859 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200860 // The current line is longer than LINE_MAXLEN, continue
861 // reading but discard everything until EOL or EOF is
862 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200863 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
864 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200865 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200866 break;
867 }
868
869 state->linebuf = state->growbuf;
870 state->linelen = growbuflen;
871 }
872 else
873 state->linebuf = IObuff;
874
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200875 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200876 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
877 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100878 char_u *line;
879
880 line = string_convert(&state->vc, state->linebuf, &state->linelen);
881 if (line != NULL)
882 {
883 if (state->linelen < IOSIZE)
884 {
885 STRCPY(state->linebuf, line);
886 vim_free(line);
887 }
888 else
889 {
890 vim_free(state->growbuf);
891 state->linebuf = state->growbuf = line;
892 state->growbufsiz = state->linelen < LINE_MAXLEN
893 ? state->linelen : LINE_MAXLEN;
894 }
895 }
896 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100897
Bram Moolenaare0d37972016-07-15 22:36:01 +0200898 return QF_OK;
899}
900
901/*
902 * Get the next string from a file/buffer/list/string.
903 */
904 static int
905qf_get_nextline(qfstate_T *state)
906{
907 int status = QF_FAIL;
908
909 if (state->fd == NULL)
910 {
911 if (state->tv != NULL)
912 {
913 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200914 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200915 status = qf_get_next_str_line(state);
916 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200917 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200918 status = qf_get_next_list_line(state);
919 }
920 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200921 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200922 status = qf_get_next_buf_line(state);
923 }
924 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200925 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200926 status = qf_get_next_file_line(state);
927
928 if (status != QF_OK)
929 return status;
930
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200931 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200932 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200933 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200934 state->linebuf[state->linelen - 1] = NUL;
935#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200936 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
937 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200938#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200939 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200940
Bram Moolenaare0d37972016-07-15 22:36:01 +0200941 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200942
943 return QF_OK;
944}
945
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200946typedef struct {
947 char_u *namebuf;
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200948 int bnr;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200949 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200950 char_u *errmsg;
951 int errmsglen;
952 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200953 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200954 int col;
thinca6864efa2021-06-19 20:45:20 +0200955 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200956 char_u use_viscol;
957 char_u *pattern;
958 int enr;
959 int type;
Tom Praschanca6ac992023-08-11 23:26:12 +0200960 typval_T *user_data;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200961 int valid;
962} qffields_T;
963
964/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200965 * Parse the match for filename ('%f') pattern in regmatch.
966 * Return the matched value in "fields->namebuf".
967 */
968 static int
969qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
970{
971 int c;
972
973 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
974 return QF_FAIL;
975
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200976 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200977 c = *rmp->endp[midx];
978 *rmp->endp[midx] = NUL;
979 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
980 *rmp->endp[midx] = c;
981
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200982 // For separate filename patterns (%O, %P and %Q), the specified file
983 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200984 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
985 && mch_getperm(fields->namebuf) == -1)
986 return QF_FAIL;
987
988 return QF_OK;
989}
990
991/*
zeertzjqb0221812023-10-26 23:15:44 +0200992 * Parse the match for buffer number ('%b') pattern in regmatch.
993 * Return the matched value in "fields->bnr".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200994 */
995 static int
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200996qf_parse_fmt_b(regmatch_T *rmp, int midx, qffields_T *fields)
997{
998 if (rmp->startp[midx] == NULL)
999 return QF_FAIL;
1000 int bnr = (int)atol((char *)rmp->startp[midx]);
1001 if (buflist_findnr(bnr) == NULL)
1002 return QF_FAIL;
1003 fields->bnr = bnr;
1004 return QF_OK;
1005}
1006
1007/*
1008 * Parse the match for error number ('%n') pattern in regmatch.
1009 * Return the matched value in "fields->enr".
1010 */
1011 static int
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001012qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
1013{
1014 if (rmp->startp[midx] == NULL)
1015 return QF_FAIL;
1016 fields->enr = (int)atol((char *)rmp->startp[midx]);
1017 return QF_OK;
1018}
1019
1020/*
haya14busae023d492022-02-08 18:09:29 +00001021 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001022 * Return the matched value in "fields->lnum".
1023 */
1024 static int
1025qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
1026{
1027 if (rmp->startp[midx] == NULL)
1028 return QF_FAIL;
1029 fields->lnum = atol((char *)rmp->startp[midx]);
1030 return QF_OK;
1031}
1032
1033/*
haya14busae023d492022-02-08 18:09:29 +00001034 * Parse the match for end line number ('%e') pattern in regmatch.
1035 * Return the matched value in "fields->end_lnum".
1036 */
1037 static int
1038qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
1039{
1040 if (rmp->startp[midx] == NULL)
1041 return QF_FAIL;
1042 fields->end_lnum = atol((char *)rmp->startp[midx]);
1043 return QF_OK;
1044}
1045
1046/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001047 * Parse the match for column number ('%c') pattern in regmatch.
1048 * Return the matched value in "fields->col".
1049 */
1050 static int
1051qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
1052{
1053 if (rmp->startp[midx] == NULL)
1054 return QF_FAIL;
1055 fields->col = (int)atol((char *)rmp->startp[midx]);
1056 return QF_OK;
1057}
1058
1059/*
haya14busae023d492022-02-08 18:09:29 +00001060 * Parse the match for end column number ('%k') pattern in regmatch.
1061 * Return the matched value in "fields->end_col".
1062 */
1063 static int
1064qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1065{
1066 if (rmp->startp[midx] == NULL)
1067 return QF_FAIL;
1068 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1069 return QF_OK;
1070}
1071
1072/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001073 * Parse the match for error type ('%t') pattern in regmatch.
1074 * Return the matched value in "fields->type".
1075 */
1076 static int
1077qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1078{
1079 if (rmp->startp[midx] == NULL)
1080 return QF_FAIL;
1081 fields->type = *rmp->startp[midx];
1082 return QF_OK;
1083}
1084
1085/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001086 * Copy a non-error line into the error string. Return the matched line in
1087 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001088 */
1089 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001090copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001091{
1092 char_u *p;
1093
1094 if (linelen >= fields->errmsglen)
1095 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001096 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001097 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1098 return QF_NOMEM;
1099 fields->errmsg = p;
1100 fields->errmsglen = linelen + 1;
1101 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001102 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001103 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001104
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001105 return QF_OK;
1106}
1107
1108/*
1109 * Parse the match for error message ('%m') pattern in regmatch.
1110 * Return the matched value in "fields->errmsg".
1111 */
1112 static int
1113qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1114{
1115 char_u *p;
1116 int len;
1117
1118 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1119 return QF_FAIL;
1120 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1121 if (len >= fields->errmsglen)
1122 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001123 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001124 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1125 return QF_NOMEM;
1126 fields->errmsg = p;
1127 fields->errmsglen = len + 1;
1128 }
1129 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1130 return QF_OK;
1131}
1132
1133/*
1134 * Parse the match for rest of a single-line file message ('%r') pattern.
1135 * Return the matched value in "tail".
1136 */
1137 static int
1138qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1139{
1140 if (rmp->startp[midx] == NULL)
1141 return QF_FAIL;
1142 *tail = rmp->startp[midx];
1143 return QF_OK;
1144}
1145
1146/*
1147 * Parse the match for the pointer line ('%p') pattern in regmatch.
1148 * Return the matched value in "fields->col".
1149 */
1150 static int
1151qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1152{
1153 char_u *match_ptr;
1154
1155 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1156 return QF_FAIL;
1157 fields->col = 0;
1158 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1159 ++match_ptr)
1160 {
1161 ++fields->col;
1162 if (*match_ptr == TAB)
1163 {
1164 fields->col += 7;
1165 fields->col -= fields->col % 8;
1166 }
1167 }
1168 ++fields->col;
1169 fields->use_viscol = TRUE;
1170 return QF_OK;
1171}
1172
1173/*
1174 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1175 * Return the matched value in "fields->col".
1176 */
1177 static int
1178qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1179{
1180 if (rmp->startp[midx] == NULL)
1181 return QF_FAIL;
1182 fields->col = (int)atol((char *)rmp->startp[midx]);
1183 fields->use_viscol = TRUE;
1184 return QF_OK;
1185}
1186
1187/*
1188 * Parse the match for the search text ('%s') pattern in regmatch.
1189 * Return the matched value in "fields->pattern".
1190 */
1191 static int
1192qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1193{
1194 int len;
1195
1196 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1197 return QF_FAIL;
1198 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1199 if (len > CMDBUFFSIZE - 5)
1200 len = CMDBUFFSIZE - 5;
1201 STRCPY(fields->pattern, "^\\V");
1202 STRNCAT(fields->pattern, rmp->startp[midx], len);
1203 fields->pattern[len + 3] = '\\';
1204 fields->pattern[len + 4] = '$';
1205 fields->pattern[len + 5] = NUL;
1206 return QF_OK;
1207}
1208
1209/*
1210 * Parse the match for the module ('%o') pattern in regmatch.
1211 * Return the matched value in "fields->module".
1212 */
1213 static int
1214qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1215{
1216 int len;
1217
1218 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1219 return QF_FAIL;
1220 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1221 if (len > CMDBUFFSIZE)
1222 len = CMDBUFFSIZE;
1223 STRNCAT(fields->module, rmp->startp[midx], len);
1224 return QF_OK;
1225}
1226
1227/*
1228 * 'errorformat' format pattern parser functions.
1229 * The '%f' and '%r' formats are parsed differently from other formats.
1230 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001231 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001232 */
1233static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1234{
haya14busae023d492022-02-08 18:09:29 +00001235 NULL, // %f
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001236 qf_parse_fmt_b,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001237 qf_parse_fmt_n,
1238 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001239 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001240 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001241 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001242 qf_parse_fmt_t,
1243 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001244 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001245 qf_parse_fmt_p,
1246 qf_parse_fmt_v,
1247 qf_parse_fmt_s,
1248 qf_parse_fmt_o
1249};
1250
1251/*
1252 * Parse the error format pattern matches in "regmatch" and set the values in
1253 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1254 * match. Returns QF_OK if all the matches are successfully parsed. On
1255 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001256 */
1257 static int
1258qf_parse_match(
1259 char_u *linebuf,
1260 int linelen,
1261 efm_T *fmt_ptr,
1262 regmatch_T *regmatch,
1263 qffields_T *fields,
1264 int qf_multiline,
1265 int qf_multiscan,
1266 char_u **tail)
1267{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001268 int idx = fmt_ptr->prefix;
1269 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001270 int midx;
1271 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001272
1273 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1274 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001275 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001276 fields->type = idx;
1277 else
1278 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001279
1280 // Extract error message data from matched line.
1281 // We check for an actual submatch, because "\[" and "\]" in
1282 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001283 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001284 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001285 status = QF_OK;
1286 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001287 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001288 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001289 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001290 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001291 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001292 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001293 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001294 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001295 }
haya14busae023d492022-02-08 18:09:29 +00001296 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001297 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001298 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001299 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001300
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001301 if (status != QF_OK)
1302 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001303 }
1304
1305 return QF_OK;
1306}
1307
1308/*
1309 * Parse an error line in 'linebuf' using a single error format string in
1310 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1311 * Returns QF_OK if the efm format matches completely and the fields are
1312 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1313 */
1314 static int
1315qf_parse_get_fields(
1316 char_u *linebuf,
1317 int linelen,
1318 efm_T *fmt_ptr,
1319 qffields_T *fields,
1320 int qf_multiline,
1321 int qf_multiscan,
1322 char_u **tail)
1323{
1324 regmatch_T regmatch;
1325 int status = QF_FAIL;
1326 int r;
1327
1328 if (qf_multiscan &&
1329 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1330 return QF_FAIL;
1331
1332 fields->namebuf[0] = NUL;
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001333 fields->bnr = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001334 fields->module[0] = NUL;
1335 fields->pattern[0] = NUL;
1336 if (!qf_multiscan)
1337 fields->errmsg[0] = NUL;
1338 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001339 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001340 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001341 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001342 fields->use_viscol = FALSE;
1343 fields->enr = -1;
1344 fields->type = 0;
1345 *tail = NULL;
1346
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001347 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001348 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001349 regmatch.regprog = fmt_ptr->prog;
1350 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1351 fmt_ptr->prog = regmatch.regprog;
1352 if (r)
1353 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1354 fields, qf_multiline, qf_multiscan, tail);
1355
1356 return status;
1357}
1358
1359/*
1360 * Parse directory error format prefixes (%D and %X).
1361 * Push and pop directories from the directory stack when scanning directory
1362 * names.
1363 */
1364 static int
1365qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1366{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001367 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001368 {
1369 if (*fields->namebuf == NUL)
1370 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001371 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001372 return QF_FAIL;
1373 }
1374 qfl->qf_directory =
1375 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1376 if (qfl->qf_directory == NULL)
1377 return QF_FAIL;
1378 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001379 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001380 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1381
1382 return QF_OK;
1383}
1384
1385/*
1386 * Parse global file name error format prefixes (%O, %P and %Q).
1387 */
1388 static int
1389qf_parse_file_pfx(
1390 int idx,
1391 qffields_T *fields,
1392 qf_list_T *qfl,
1393 char_u *tail)
1394{
1395 fields->valid = FALSE;
1396 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1397 {
1398 if (*fields->namebuf && idx == 'P')
1399 qfl->qf_currfile =
1400 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1401 else if (idx == 'Q')
1402 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1403 *fields->namebuf = NUL;
1404 if (tail && *tail)
1405 {
1406 STRMOVE(IObuff, skipwhite(tail));
1407 qfl->qf_multiscan = TRUE;
1408 return QF_MULTISCAN;
1409 }
1410 }
1411
1412 return QF_OK;
1413}
1414
1415/*
1416 * Parse a non-error line (a line which doesn't match any of the error
1417 * format in 'efm').
1418 */
1419 static int
1420qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1421{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001422 fields->namebuf[0] = NUL; // no match found, remove file name
1423 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001424 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001425
Bram Moolenaarf4140482020-02-15 23:06:45 +01001426 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001427}
1428
1429/*
1430 * Parse multi-line error format prefixes (%C and %Z)
1431 */
1432 static int
1433qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001434 int idx,
1435 qf_list_T *qfl,
1436 qffields_T *fields)
1437{
1438 char_u *ptr;
1439 int len;
1440
1441 if (!qfl->qf_multiignore)
1442 {
1443 qfline_T *qfprev = qfl->qf_last;
1444
1445 if (qfprev == NULL)
1446 return QF_FAIL;
1447 if (*fields->errmsg && !qfl->qf_multiignore)
1448 {
1449 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001450 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1451 aid_qf_multiline_pfx);
1452 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001453 return QF_FAIL;
1454 STRCPY(ptr, qfprev->qf_text);
1455 vim_free(qfprev->qf_text);
1456 qfprev->qf_text = ptr;
1457 *(ptr += len) = '\n';
1458 STRCPY(++ptr, fields->errmsg);
1459 }
1460 if (qfprev->qf_nr == -1)
1461 qfprev->qf_nr = fields->enr;
1462 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001463 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001464 qfprev->qf_type = fields->type;
1465
1466 if (!qfprev->qf_lnum)
1467 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001468 if (!qfprev->qf_end_lnum)
1469 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001470 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001471 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001472 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001473 qfprev->qf_viscol = fields->use_viscol;
1474 }
haya14busae023d492022-02-08 18:09:29 +00001475 if (!qfprev->qf_end_col)
1476 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001477 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001478 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001479 qfl->qf_directory,
1480 *fields->namebuf || qfl->qf_directory != NULL
1481 ? fields->namebuf
1482 : qfl->qf_currfile != NULL && fields->valid
1483 ? qfl->qf_currfile : 0);
1484 }
1485 if (idx == 'Z')
1486 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1487 line_breakcheck();
1488
1489 return QF_IGNORE_LINE;
1490}
1491
1492/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001493 * Parse a line and get the quickfix fields.
1494 * Return the QF_ status.
1495 */
1496 static int
1497qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001498 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001499 char_u *linebuf,
1500 int linelen,
1501 efm_T *fmt_first,
1502 qffields_T *fields)
1503{
1504 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001505 int idx = 0;
1506 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001507 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001508
Bram Moolenaare333e792018-04-08 13:27:39 +02001509restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001510 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001511 if (fmt_start == NULL)
1512 fmt_ptr = fmt_first;
1513 else
1514 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001515 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001516 fmt_ptr = fmt_start;
1517 fmt_start = NULL;
1518 }
1519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001520 // Try to match each part of 'errorformat' until we find a complete
1521 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001522 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001523 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1524 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001525 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001526 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1527 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1528 if (status == QF_NOMEM)
1529 return status;
1530 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001531 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001532 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001533 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001534
1535 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1536 {
1537 if (fmt_ptr != NULL)
1538 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001539 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001540 status = qf_parse_dir_pfx(idx, fields, qfl);
1541 if (status != QF_OK)
1542 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001543 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001544
1545 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1546 if (status != QF_OK)
1547 return status;
1548
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001549 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001550 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001551 }
1552 else if (fmt_ptr != NULL)
1553 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001554 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001555 if (fmt_ptr->conthere)
1556 fmt_start = fmt_ptr;
1557
Bram Moolenaare9283662020-06-07 14:10:47 +02001558 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001559 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001560 qfl->qf_multiline = TRUE; // start of a multi-line message
1561 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001562 }
1563 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001564 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001565 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001566 if (status != QF_OK)
1567 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001568 }
1569 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001570 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001571 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1572 if (status == QF_MULTISCAN)
1573 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001574 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001575 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001576 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001577 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001578 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001579 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001580 return QF_IGNORE_LINE;
1581 }
1582 }
1583
1584 return QF_OK;
1585}
1586
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001587/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001588 * Returns TRUE if the specified quickfix/location stack is empty
1589 */
1590 static int
1591qf_stack_empty(qf_info_T *qi)
1592{
1593 return qi == NULL || qi->qf_listcount <= 0;
1594}
1595
1596/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001597 * Returns TRUE if the specified quickfix/location list is empty.
1598 */
1599 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001600qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001601{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001602 return qfl == NULL || qfl->qf_count <= 0;
1603}
1604
1605/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001606 * Returns TRUE if the specified quickfix/location list is not empty and
1607 * has valid entries.
1608 */
1609 static int
1610qf_list_has_valid_entries(qf_list_T *qfl)
1611{
1612 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1613}
1614
1615/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001616 * Return a pointer to a list in the specified quickfix stack
1617 */
1618 static qf_list_T *
1619qf_get_list(qf_info_T *qi, int idx)
1620{
1621 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001622}
1623
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001624/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001625 * Allocate the fields used for parsing lines and populating a quickfix list.
1626 */
1627 static int
1628qf_alloc_fields(qffields_T *pfields)
1629{
1630 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1631 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1632 pfields->errmsglen = CMDBUFFSIZE + 1;
1633 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1634 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1635 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1636 || pfields->pattern == NULL || pfields->module == NULL)
1637 return FAIL;
1638
1639 return OK;
1640}
1641
1642/*
1643 * Free the fields used for parsing lines and populating a quickfix list.
1644 */
1645 static void
1646qf_free_fields(qffields_T *pfields)
1647{
1648 vim_free(pfields->namebuf);
1649 vim_free(pfields->module);
1650 vim_free(pfields->errmsg);
1651 vim_free(pfields->pattern);
1652}
1653
1654/*
1655 * Setup the state information used for parsing lines and populating a
1656 * quickfix list.
1657 */
1658 static int
1659qf_setup_state(
1660 qfstate_T *pstate,
1661 char_u *enc,
1662 char_u *efile,
1663 typval_T *tv,
1664 buf_T *buf,
1665 linenr_T lnumfirst,
1666 linenr_T lnumlast)
1667{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001668 pstate->vc.vc_type = CONV_NONE;
1669 if (enc != NULL && *enc != NUL)
1670 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001671
1672 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1673 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001674 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001675 return FAIL;
1676 }
1677
1678 if (tv != NULL)
1679 {
1680 if (tv->v_type == VAR_STRING)
1681 pstate->p_str = tv->vval.v_string;
1682 else if (tv->v_type == VAR_LIST)
1683 pstate->p_li = tv->vval.v_list->lv_first;
1684 pstate->tv = tv;
1685 }
1686 pstate->buf = buf;
1687 pstate->buflnum = lnumfirst;
1688 pstate->lnumlast = lnumlast;
1689
1690 return OK;
1691}
1692
1693/*
1694 * Cleanup the state information used for parsing lines and populating a
1695 * quickfix list.
1696 */
1697 static void
1698qf_cleanup_state(qfstate_T *pstate)
1699{
1700 if (pstate->fd != NULL)
1701 fclose(pstate->fd);
1702
1703 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001704 if (pstate->vc.vc_type != CONV_NONE)
1705 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001706}
1707
1708/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001709 * Process the next line from a file/buffer/list/string and add it
1710 * to the quickfix list 'qfl'.
1711 */
1712 static int
1713qf_init_process_nextline(
1714 qf_list_T *qfl,
1715 efm_T *fmt_first,
1716 qfstate_T *state,
1717 qffields_T *fields)
1718{
1719 int status;
1720
1721 // Get the next line from a file/buffer/list/string
1722 status = qf_get_nextline(state);
1723 if (status != QF_OK)
1724 return status;
1725
1726 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1727 fmt_first, fields);
1728 if (status != QF_OK)
1729 return status;
1730
1731 return qf_add_entry(qfl,
1732 qfl->qf_directory,
1733 (*fields->namebuf || qfl->qf_directory != NULL)
1734 ? fields->namebuf
1735 : ((qfl->qf_currfile != NULL && fields->valid)
1736 ? qfl->qf_currfile : (char_u *)NULL),
1737 fields->module,
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001738 fields->bnr,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001739 fields->errmsg,
1740 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001741 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001742 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001743 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001744 fields->use_viscol,
1745 fields->pattern,
1746 fields->enr,
1747 fields->type,
Tom Praschanca6ac992023-08-11 23:26:12 +02001748 fields->user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001749 fields->valid);
1750}
1751
1752/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001753 * Read the errorfile "efile" into memory, line by line, building the error
1754 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001755 * Alternative: when "efile" is NULL read errors from buffer "buf".
1756 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001757 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001758 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1759 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001760 * Return -1 for error, number of errors for success.
1761 */
1762 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001763qf_init_ext(
1764 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001765 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001766 char_u *efile,
1767 buf_T *buf,
1768 typval_T *tv,
1769 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001770 int newlist, // TRUE: start a new error list
1771 linenr_T lnumfirst, // first line number to use
1772 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001773 char_u *qf_title,
1774 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001775{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001776 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001777 qfstate_T state;
1778 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001779 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001780 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001781 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001783 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001784 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001785 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001787 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001788 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001789
Bram Moolenaara80faa82020-04-12 19:37:17 +02001790 CLEAR_FIELD(state);
1791 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001792 if ((qf_alloc_fields(&fields) == FAIL) ||
1793 (qf_setup_state(&state, enc, efile, tv, buf,
1794 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 goto qf_init_end;
1796
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001797 if (newlist || qf_idx == qi->qf_listcount)
1798 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001799 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001800 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001801 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001802 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001803 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001804 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001805 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001806 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001807 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001808 qfl = qf_get_list(qi, qf_idx);
1809 if (!qf_list_empty(qfl))
1810 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001813 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001814 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001815 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 else
1817 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001819 // If the errorformat didn't change between calls, then reuse the
1820 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001821 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1822 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001823 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001824 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001825 free_efm_list(&fmt_first);
1826
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001827 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001828 fmt_first = parse_efm_option(efm);
1829 if (fmt_first != NULL)
1830 last_efm = vim_strsave(efm);
1831 }
1832
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001833 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001836 // got_int is reset here, because it was probably set when killing the
1837 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 got_int = FALSE;
1839
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001840 // Read the lines in the error file one by one.
1841 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001842 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001844 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001845 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001846 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001847 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001848 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001849 if (status == QF_FAIL)
1850 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 line_breakcheck();
1853 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001854 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001856 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001858 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001859 qfl->qf_ptr = qfl->qf_start;
1860 qfl->qf_index = 1;
1861 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
1863 else
1864 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001865 qfl->qf_nonevalid = FALSE;
1866 if (qfl->qf_ptr == NULL)
1867 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001869 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001870 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001871 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001873 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001875 if (!adding)
1876 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001877 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001878 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001879 qi->qf_listcount--;
1880 if (qi->qf_curlist > 0)
1881 --qi->qf_curlist;
1882 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001883qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001884 if (qf_idx == qi->qf_curlist)
1885 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001886 qf_cleanup_state(&state);
1887 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 return retval;
1890}
1891
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001892/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001893 * Read the errorfile "efile" into memory, line by line, building the error
1894 * list. Set the error list's title to qf_title.
1895 * Return -1 for error, number of errors for success.
1896 */
1897 int
1898qf_init(win_T *wp,
1899 char_u *efile,
1900 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001901 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001902 char_u *qf_title,
1903 char_u *enc)
1904{
1905 qf_info_T *qi = &ql_info;
1906
1907 if (wp != NULL)
1908 {
1909 qi = ll_get_or_alloc_list(wp);
1910 if (qi == NULL)
1911 return FAIL;
1912 }
1913
1914 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1915 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1916}
1917
1918/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001919 * Set the title of the specified quickfix list. Frees the previous title.
1920 * Prepends ':' to the title.
1921 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001922 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001923qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001924{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001925 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001926
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001927 if (title == NULL)
1928 return;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001929
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001930 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
1931
1932 qfl->qf_title = p;
1933 if (p != NULL)
1934 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001935}
1936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001938 * The title of a quickfix/location list is set, by default, to the command
1939 * that created the quickfix list with the ":" prefix.
1940 * Create a quickfix list title string by prepending ":" to a user command.
1941 * Returns a pointer to a static buffer with the title.
1942 */
1943 static char_u *
1944qf_cmdtitle(char_u *cmd)
1945{
1946 static char_u qftitle_str[IOSIZE];
1947
1948 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1949 return qftitle_str;
1950}
1951
1952/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001953 * Return a pointer to the current list in the specified quickfix stack
1954 */
1955 static qf_list_T *
1956qf_get_curlist(qf_info_T *qi)
1957{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001958 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001959}
1960
1961/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001962 * Prepare for adding a new quickfix list. If the current list is in the
1963 * middle of the stack, then all the following lists are freed and then
1964 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 */
1966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001967qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968{
1969 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001970 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001972 // If the current entry is not the last entry, delete entries beyond
1973 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001974 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001975 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001976 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001978 // When the stack is full, remove to oldest entry
1979 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001982 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001984 qi->qf_lists[i - 1] = qi->qf_lists[i];
1985 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001988 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001989 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001990 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001991 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001992 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001993 qfl->qf_id = ++last_qf_id;
Tom Praschanca6ac992023-08-11 23:26:12 +02001994 qfl->qf_has_user_data = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995}
1996
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001997/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001998 * Queue location list stack delete request.
1999 */
2000 static void
2001locstack_queue_delreq(qf_info_T *qi)
2002{
2003 qf_delq_T *q;
2004
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002005 q = ALLOC_ONE(qf_delq_T);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002006 if (q == NULL)
2007 return;
2008
2009 q->qi = qi;
2010 q->next = qf_delq_head;
2011 qf_delq_head = q;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002012}
2013
2014/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002015 * Return the global quickfix stack window buffer number.
2016 */
2017 int
2018qf_stack_get_bufnr(void)
2019{
2020 return ql_info.qf_bufnr;
2021}
2022
2023/*
2024 * Wipe the quickfix window buffer (if present) for the specified
2025 * quickfix/location list.
2026 */
2027 static void
2028wipe_qf_buffer(qf_info_T *qi)
2029{
2030 buf_T *qfbuf;
2031
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002032 if (qi->qf_bufnr == INVALID_QFBUFNR)
2033 return;
2034
2035 qfbuf = buflist_findnr(qi->qf_bufnr);
2036 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002037 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002038 // If the quickfix buffer is not loaded in any window, then
2039 // wipe the buffer.
2040 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
2041 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002042 }
2043}
2044
2045/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002046 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002047 */
2048 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002049ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002050{
2051 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002053
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002054 qi = *pqi;
2055 if (qi == NULL)
2056 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002057 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002059 // If the location list is still in use, then queue the delete request
2060 // to be processed later.
2061 if (quickfix_busy > 0)
2062 {
2063 locstack_queue_delreq(qi);
2064 return;
2065 }
2066
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002067 qi->qf_refcount--;
2068 if (qi->qf_refcount < 1)
2069 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002070 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002071 // If the quickfix window buffer is loaded, then wipe it
2072 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002073
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002074 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002075 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002076 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002078}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002079
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002080/*
2081 * Free all the quickfix/location lists in the stack.
2082 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002083 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002084qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002085{
2086 int i;
2087 qf_info_T *qi = &ql_info;
2088
2089 if (wp != NULL)
2090 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002091 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092 ll_free_all(&wp->w_llist);
2093 ll_free_all(&wp->w_llist_ref);
2094 }
2095 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002096 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002097 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002098 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002099}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002102 * Delay freeing of location list stacks when the quickfix code is running.
2103 * Used to avoid problems with autocmds freeing location list stacks when the
2104 * quickfix code is still referencing the stack.
2105 * Must always call decr_quickfix_busy() exactly once after this.
2106 */
2107 static void
2108incr_quickfix_busy(void)
2109{
2110 quickfix_busy++;
2111}
2112
2113/*
2114 * Safe to free location list stacks. Process any delayed delete requests.
2115 */
2116 static void
2117decr_quickfix_busy(void)
2118{
2119 if (--quickfix_busy == 0)
2120 {
2121 // No longer referencing the location lists. Process all the pending
2122 // delete requests.
2123 while (qf_delq_head != NULL)
2124 {
2125 qf_delq_T *q = qf_delq_head;
2126
2127 qf_delq_head = q->next;
2128 ll_free_all(&q->qi);
2129 vim_free(q);
2130 }
2131 }
2132#ifdef ABORT_ON_INTERNAL_ERROR
2133 if (quickfix_busy < 0)
2134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002135 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002136 abort();
2137 }
2138#endif
2139}
2140
2141#if defined(EXITFREE) || defined(PROTO)
2142 void
2143check_quickfix_busy(void)
2144{
2145 if (quickfix_busy != 0)
2146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002147 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002148# ifdef ABORT_ON_INTERNAL_ERROR
2149 abort();
2150# endif
2151 }
2152}
2153#endif
2154
2155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002157 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 */
2159 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002160qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002161 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002162 char_u *dir, // optional directory name
2163 char_u *fname, // file name or NULL
2164 char_u *module, // module name or NULL
2165 int bufnum, // buffer number or zero
2166 char_u *mesg, // message
2167 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002168 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002169 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002170 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002171 int vis_col, // using visual column
2172 char_u *pattern, // search pattern
2173 int nr, // error number
2174 int type, // type character
Tom Praschanca6ac992023-08-11 23:26:12 +02002175 typval_T *user_data, // custom user data or NULL
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002176 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
Austin Chang29822992024-10-03 10:50:05 +02002178 buf_T *buf;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002179 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002180 qfline_T **lastp; // pointer to qf_last or NULL
Austin Chang29822992024-10-03 10:50:05 +02002181 char_u *fullname = NULL;
2182 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002184 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002185 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002186 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002187 {
Austin Chang29822992024-10-03 10:50:05 +02002188 buf = buflist_findnr(bufnum);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002189
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002190 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002191 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002192 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002193 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002194 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002195 else
Austin Chang29822992024-10-03 10:50:05 +02002196 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002197 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Austin Chang29822992024-10-03 10:50:05 +02002198 buf = buflist_findnr(qfp->qf_fnum);
2199 }
2200 if (fname != NULL)
2201 fullname = fix_fname(fname);
2202 qfp->qf_fname = NULL;
2203 if (buf != NULL &&
2204 buf->b_ffname != NULL && fullname != NULL)
2205 {
2206 if (fnamecmp(fullname, buf->b_ffname) != 0)
2207 {
2208 p = shorten_fname1(fullname);
2209 if (p != NULL)
2210 qfp->qf_fname = vim_strsave(p);
2211 }
2212 }
2213 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2215 {
2216 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002217 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
2219 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002220 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002222 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002223 qfp->qf_viscol = vis_col;
Tom Praschanca6ac992023-08-11 23:26:12 +02002224 if (user_data == NULL || user_data->v_type == VAR_UNKNOWN)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002225 qfp->qf_user_data.v_type = VAR_UNKNOWN;
Tom Praschanca6ac992023-08-11 23:26:12 +02002226 else
2227 {
2228 copy_tv(user_data, &qfp->qf_user_data);
2229 qfl->qf_has_user_data = TRUE;
2230 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002231 if (pattern == NULL || *pattern == NUL)
2232 qfp->qf_pattern = NULL;
2233 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2234 {
2235 vim_free(qfp->qf_text);
2236 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002237 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002238 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002239 if (module == NULL || *module == NUL)
2240 qfp->qf_module = NULL;
2241 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2242 {
2243 vim_free(qfp->qf_text);
2244 vim_free(qfp->qf_pattern);
2245 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002246 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002249 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 type = 0;
2251 qfp->qf_type = type;
2252 qfp->qf_valid = valid;
2253
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002254 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002255 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002257 qfl->qf_start = qfp;
2258 qfl->qf_ptr = qfp;
2259 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002260 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262 else
2263 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002264 qfp->qf_prev = *lastp;
2265 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002267 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002269 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002270 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002271 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002273 qfl->qf_index = qfl->qf_count;
2274 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276
Bram Moolenaar95946f12019-03-31 15:31:59 +02002277 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278}
2279
2280/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002281 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002282 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002283 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002284qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002285{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002286 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002287
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002288 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002289 if (qi == NULL)
2290 return NULL;
2291
2292 qi->qf_refcount++;
2293 qi->qfl_type = qfltype;
2294 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002295 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002296}
2297
2298/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002299 * Return the location list stack for window 'wp'.
2300 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002301 */
2302 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002303ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002304{
2305 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002306 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307 return wp->w_llist_ref;
2308
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002309 // For a non-location list window, w_llist_ref should not point to a
2310 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002311 ll_free_all(&wp->w_llist_ref);
2312
2313 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002314 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002315 return wp->w_llist;
2316}
2317
2318/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002319 * Get the quickfix/location list stack to use for the specified Ex command.
2320 * For a location list command, returns the stack for the current window. If
2321 * the location list is not found, then returns NULL and prints an error
2322 * message if 'print_emsg' is TRUE.
2323 */
2324 static qf_info_T *
2325qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2326{
2327 qf_info_T *qi = &ql_info;
2328
2329 if (is_loclist_cmd(eap->cmdidx))
2330 {
2331 qi = GET_LOC_LIST(curwin);
2332 if (qi == NULL)
2333 {
2334 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002335 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002336 return NULL;
2337 }
2338 }
2339
2340 return qi;
2341}
2342
2343/*
2344 * Get the quickfix/location list stack to use for the specified Ex command.
2345 * For a location list command, returns the stack for the current window.
2346 * If the location list is not present, then allocates a new one.
2347 * Returns NULL if the allocation fails. For a location list command, sets
2348 * 'pwinp' to curwin.
2349 */
2350 static qf_info_T *
2351qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2352{
2353 qf_info_T *qi = &ql_info;
2354
2355 if (is_loclist_cmd(eap->cmdidx))
2356 {
2357 qi = ll_get_or_alloc_list(curwin);
2358 if (qi == NULL)
2359 return NULL;
2360 *pwinp = curwin;
2361 }
2362
2363 return qi;
2364}
2365
2366/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002367 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2368 */
2369 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002370copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002371{
2372 int i;
2373 qfline_T *from_qfp;
2374 qfline_T *prevp;
2375
2376 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002377 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002378 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002379 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002380 NULL,
2381 NULL,
2382 from_qfp->qf_module,
2383 0,
2384 from_qfp->qf_text,
2385 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002386 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002387 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002388 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002389 from_qfp->qf_viscol,
2390 from_qfp->qf_pattern,
2391 from_qfp->qf_nr,
2392 0,
Tom Praschanca6ac992023-08-11 23:26:12 +02002393 &from_qfp->qf_user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002394 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002395 return FAIL;
2396
2397 // qf_add_entry() will not set the qf_num field, as the
2398 // directory and file names are not supplied. So the qf_fnum
2399 // field is copied here.
2400 prevp = to_qfl->qf_last;
2401 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2402 prevp->qf_type = from_qfp->qf_type; // error type
2403 if (from_qfl->qf_ptr == from_qfp)
2404 to_qfl->qf_ptr = prevp; // current location
2405 }
2406
2407 return OK;
2408}
2409
2410/*
2411 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2412 */
2413 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002414copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002415{
2416 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002417 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002418 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
Tom Praschanca6ac992023-08-11 23:26:12 +02002419 to_qfl->qf_has_user_data = from_qfl->qf_has_user_data;
Bram Moolenaar09037502018-09-25 22:08:14 +02002420 to_qfl->qf_count = 0;
2421 to_qfl->qf_index = 0;
2422 to_qfl->qf_start = NULL;
2423 to_qfl->qf_last = NULL;
2424 to_qfl->qf_ptr = NULL;
2425 if (from_qfl->qf_title != NULL)
2426 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2427 else
2428 to_qfl->qf_title = NULL;
2429 if (from_qfl->qf_ctx != NULL)
2430 {
2431 to_qfl->qf_ctx = alloc_tv();
2432 if (to_qfl->qf_ctx != NULL)
2433 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2434 }
2435 else
2436 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002437 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2438 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002439 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002440 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002441
2442 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002443 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002444 return FAIL;
2445
2446 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2447
2448 // Assign a new ID for the location list
2449 to_qfl->qf_id = ++last_qf_id;
2450 to_qfl->qf_changedtick = 0L;
2451
2452 // When no valid entries are present in the list, qf_ptr points to
2453 // the first item in the list
2454 if (to_qfl->qf_nonevalid)
2455 {
2456 to_qfl->qf_ptr = to_qfl->qf_start;
2457 to_qfl->qf_index = 1;
2458 }
2459
2460 return OK;
2461}
2462
2463/*
2464 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002465 */
2466 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002467copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002468{
2469 qf_info_T *qi;
2470 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002471
Bram Moolenaar09037502018-09-25 22:08:14 +02002472 // When copying from a location list window, copy the referenced
2473 // location list. For other windows, copy the location list for
2474 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002475 if (IS_LL_WINDOW(from))
2476 qi = from->w_llist_ref;
2477 else
2478 qi = from->w_llist;
2479
Bram Moolenaar09037502018-09-25 22:08:14 +02002480 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002481 return;
2482
Bram Moolenaar09037502018-09-25 22:08:14 +02002483 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002484 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002485 return;
2486
2487 to->w_llist->qf_listcount = qi->qf_listcount;
2488
Bram Moolenaar09037502018-09-25 22:08:14 +02002489 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002490 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002491 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002492 to->w_llist->qf_curlist = idx;
2493
Bram Moolenaar0398e002019-03-21 21:12:49 +01002494 if (copy_loclist(qf_get_list(qi, idx),
2495 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002496 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002497 qf_free_all(to);
2498 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002499 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002500 }
2501
Bram Moolenaar09037502018-09-25 22:08:14 +02002502 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002503}
2504
2505/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002506 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002507 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
2509 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002510qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
Bram Moolenaar82404332016-07-10 17:00:38 +02002512 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002513 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002514 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002515
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002516 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
Bram Moolenaare60acc12011-05-10 16:41:25 +02002519#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002520 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002521#endif
2522#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002523 if (directory != NULL)
2524 slash_adjust(directory);
2525 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002526#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002527 if (directory != NULL && !vim_isAbsName(fname)
2528 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2529 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002530 // Here we check if the file really exists.
2531 // This should normally be true, but if make works without
2532 // "leaving directory"-messages we might have missed a
2533 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002534 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002537 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002538 if (directory)
2539 ptr = concat_fnames(directory, fname, TRUE);
2540 else
2541 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002543 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002544 bufname = ptr;
2545 }
2546 else
2547 bufname = fname;
2548
2549 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002550 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002551 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002552 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002553 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002555 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002556 {
2557 vim_free(qf_last_bufname);
2558 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2559 if (bufname == ptr)
2560 qf_last_bufname = bufname;
2561 else
2562 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002563 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002564 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002565 if (buf == NULL)
2566 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002567
Bram Moolenaarc1542742016-07-20 21:44:37 +02002568 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002569 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002570 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571}
2572
2573/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002574 * Push dirbuf onto the directory stack and return pointer to actual dir or
2575 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
2577 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002578qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580 struct dir_stack_T *ds_new;
2581 struct dir_stack_T *ds_ptr;
2582
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002583 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002584 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (ds_new == NULL)
2586 return NULL;
2587
2588 ds_new->next = *stackptr;
2589 *stackptr = ds_new;
2590
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002591 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (vim_isAbsName(dirbuf)
2593 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002594 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 (*stackptr)->dirname = vim_strsave(dirbuf);
2596 else
2597 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002598 // Okay we don't have an absolute path.
2599 // dirbuf must be a subdir of one of the directories on the stack.
2600 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 ds_new = (*stackptr)->next;
2602 (*stackptr)->dirname = NULL;
2603 while (ds_new)
2604 {
2605 vim_free((*stackptr)->dirname);
2606 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2607 TRUE);
2608 if (mch_isdir((*stackptr)->dirname) == TRUE)
2609 break;
2610
2611 ds_new = ds_new->next;
2612 }
2613
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002614 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 while ((*stackptr)->next != ds_new)
2616 {
2617 ds_ptr = (*stackptr)->next;
2618 (*stackptr)->next = (*stackptr)->next->next;
2619 vim_free(ds_ptr->dirname);
2620 vim_free(ds_ptr);
2621 }
2622
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002623 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (ds_new == NULL)
2625 {
2626 vim_free((*stackptr)->dirname);
2627 (*stackptr)->dirname = vim_strsave(dirbuf);
2628 }
2629 }
2630
2631 if ((*stackptr)->dirname != NULL)
2632 return (*stackptr)->dirname;
2633 else
2634 {
2635 ds_ptr = *stackptr;
2636 *stackptr = (*stackptr)->next;
2637 vim_free(ds_ptr);
2638 return NULL;
2639 }
2640}
2641
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642/*
2643 * pop dirbuf from the directory stack and return previous directory or NULL if
2644 * stack is empty
2645 */
2646 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002647qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 struct dir_stack_T *ds_ptr;
2650
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002651 // TODO: Should we check if dirbuf is the directory on top of the stack?
2652 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002654 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (*stackptr != NULL)
2656 {
2657 ds_ptr = *stackptr;
2658 *stackptr = (*stackptr)->next;
2659 vim_free(ds_ptr->dirname);
2660 vim_free(ds_ptr);
2661 }
2662
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002663 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 return *stackptr ? (*stackptr)->dirname : NULL;
2665}
2666
2667/*
2668 * clean up directory stack
2669 */
2670 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002671qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
2673 struct dir_stack_T *ds_ptr;
2674
2675 while ((ds_ptr = *stackptr) != NULL)
2676 {
2677 *stackptr = (*stackptr)->next;
2678 vim_free(ds_ptr->dirname);
2679 vim_free(ds_ptr);
2680 }
2681}
2682
2683/*
2684 * Check in which directory of the directory stack the given file can be
2685 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002686 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 * Cleans up intermediate directory entries.
2688 *
2689 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002690 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 * ./
2692 * ./aa
2693 * ./aa/bb
2694 * ./bb
2695 * ./bb/x.c
2696 * and make says:
2697 * making all in aa
2698 * making all in bb
2699 * x.c:9: Error
2700 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2701 * qf_guess_filepath will return NULL.
2702 */
2703 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002704qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
2706 struct dir_stack_T *ds_ptr;
2707 struct dir_stack_T *ds_tmp;
2708 char_u *fullname;
2709
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002710 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002711 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 return NULL;
2713
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002714 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 fullname = NULL;
2716 while (ds_ptr)
2717 {
2718 vim_free(fullname);
2719 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002721 // If concat_fnames failed, just go on. The worst thing that can happen
2722 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2724 break;
2725
2726 ds_ptr = ds_ptr->next;
2727 }
2728
2729 vim_free(fullname);
2730
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002731 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002732 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002734 ds_tmp = qfl->qf_dir_stack->next;
2735 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 vim_free(ds_tmp->dirname);
2737 vim_free(ds_tmp);
2738 }
2739
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002740 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741}
2742
2743/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002744 * Returns TRUE if a quickfix/location list with the given identifier exists.
2745 */
2746 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002747qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002748{
2749 qf_info_T *qi = &ql_info;
2750 int i;
2751
2752 if (wp != NULL)
2753 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002754 if (!win_valid(wp))
2755 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002756 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002757 if (qi == NULL)
2758 return FALSE;
2759 }
2760
2761 for (i = 0; i < qi->qf_listcount; ++i)
2762 if (qi->qf_lists[i].qf_id == qf_id)
2763 return TRUE;
2764
2765 return FALSE;
2766}
2767
2768/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002769 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002770 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002771 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002772 * Similar to location list.
2773 */
2774 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002775is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002776{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002777 qfline_T *qfp;
2778 int i;
2779
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002780 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002781 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2782 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002783 break;
2784
Bram Moolenaar95946f12019-03-31 15:31:59 +02002785 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002786 return FALSE;
2787
2788 return TRUE;
2789}
2790
2791/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002792 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002793 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002794 */
2795 static qfline_T *
2796get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002797 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002798 qfline_T *qf_ptr,
2799 int *qf_index,
2800 int dir)
2801{
2802 int idx;
2803 int old_qf_fnum;
2804
2805 idx = *qf_index;
2806 old_qf_fnum = qf_ptr->qf_fnum;
2807
2808 do
2809 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002810 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002811 return NULL;
2812 ++idx;
2813 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002814 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002815 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2816
2817 *qf_index = idx;
2818 return qf_ptr;
2819}
2820
2821/*
2822 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002823 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002824 */
2825 static qfline_T *
2826get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002827 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002828 qfline_T *qf_ptr,
2829 int *qf_index,
2830 int dir)
2831{
2832 int idx;
2833 int old_qf_fnum;
2834
2835 idx = *qf_index;
2836 old_qf_fnum = qf_ptr->qf_fnum;
2837
2838 do
2839 {
2840 if (idx == 1 || qf_ptr->qf_prev == NULL)
2841 return NULL;
2842 --idx;
2843 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002844 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002845 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2846
2847 *qf_index = idx;
2848 return qf_ptr;
2849}
2850
2851/*
2852 * Get the n'th (errornr) previous/next valid entry from the current entry in
2853 * the quickfix list.
2854 * dir == FORWARD or FORWARD_FILE: next valid entry
2855 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2856 */
2857 static qfline_T *
2858get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002859 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002860 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002861 int dir,
2862 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002863{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002864 qfline_T *qf_ptr = qfl->qf_ptr;
2865 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002866 qfline_T *prev_qf_ptr;
2867 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002868 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002869
2870 while (errornr--)
2871 {
2872 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002873 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002874
2875 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002876 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002877 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002878 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002879 if (qf_ptr == NULL)
2880 {
2881 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002882 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002883 if (err != NULL)
2884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002885 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002886 return NULL;
2887 }
2888 break;
2889 }
2890
2891 err = NULL;
2892 }
2893
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002894 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002895 return qf_ptr;
2896}
2897
2898/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002899 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2900 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002901 */
2902 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002903get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002904{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002905 qfline_T *qf_ptr = qfl->qf_ptr;
2906 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002907
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002908 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002909 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2910 {
2911 --qf_idx;
2912 qf_ptr = qf_ptr->qf_prev;
2913 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002914 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002915 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2916 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002917 {
2918 ++qf_idx;
2919 qf_ptr = qf_ptr->qf_next;
2920 }
2921
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002922 *new_qfidx = qf_idx;
2923 return qf_ptr;
2924}
2925
2926/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002927 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002928 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2929 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2930 * Returns a pointer to the entry and the index of the new entry is stored in
2931 * 'new_qfidx'.
2932 */
2933 static qfline_T *
2934qf_get_entry(
2935 qf_list_T *qfl,
2936 int errornr,
2937 int dir,
2938 int *new_qfidx)
2939{
2940 qfline_T *qf_ptr = qfl->qf_ptr;
2941 int qfidx = qfl->qf_index;
2942
2943 if (dir != 0) // next/prev valid entry
2944 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2945 else if (errornr != 0) // go to specified number
2946 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2947
2948 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002949 return qf_ptr;
2950}
2951
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002952/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002953 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002954 */
2955 static win_T *
2956qf_find_help_win(void)
2957{
2958 win_T *wp;
2959
2960 FOR_ALL_WINDOWS(wp)
2961 if (bt_help(wp->w_buffer))
2962 return wp;
2963
2964 return NULL;
2965}
2966
2967/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002968 * Set the location list for the specified window to 'qi'.
2969 */
2970 static void
2971win_set_loclist(win_T *wp, qf_info_T *qi)
2972{
2973 wp->w_llist = qi;
2974 qi->qf_refcount++;
2975}
2976
2977/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002978 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2979 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002980 */
2981 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002982jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002983{
2984 win_T *wp;
2985 int flags;
2986
Bram Moolenaare1004402020-10-24 20:49:43 +02002987 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002988 wp = NULL;
2989 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002990 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002991 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2992 win_enter(wp, TRUE);
2993 else
2994 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002995 // Split off help window; put it at far top if no position
2996 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002997 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002999 && curwin->w_width < 80)
3000 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003001 // If the user asks to open a new window, then copy the location list.
3002 // Otherwise, don't copy the location list.
3003 if (IS_LL_STACK(qi) && !newwin)
3004 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003005
3006 if (win_split(0, flags) == FAIL)
3007 return FAIL;
3008
3009 *opened_window = TRUE;
3010
3011 if (curwin->w_height < p_hh)
3012 win_setheight((int)p_hh);
3013
Bram Moolenaarb2443732018-11-11 22:50:27 +01003014 // When using location list, the new window should use the supplied
3015 // location list. If the user asks to open a new window, then the new
3016 // window will get a copy of the location list.
3017 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003018 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003019 }
3020
3021 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003022 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003023
3024 return OK;
3025}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003026
3027/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003028 * Find a non-quickfix window using the given location list stack in the
3029 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003030 * Returns NULL if a matching window is not found.
3031 */
3032 static win_T *
3033qf_find_win_with_loclist(qf_info_T *ll)
3034{
3035 win_T *wp;
3036
3037 FOR_ALL_WINDOWS(wp)
3038 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
3039 return wp;
3040
3041 return NULL;
3042}
3043
3044/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003045 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003046 */
3047 static win_T *
3048qf_find_win_with_normal_buf(void)
3049{
3050 win_T *wp;
3051
3052 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02003053 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003054 return wp;
3055
3056 return NULL;
3057}
3058
3059/*
3060 * Go to a window in any tabpage containing the specified file. Returns TRUE
3061 * if successfully jumped to the window. Otherwise returns FALSE.
3062 */
3063 static int
3064qf_goto_tabwin_with_file(int fnum)
3065{
3066 tabpage_T *tp;
3067 win_T *wp;
3068
3069 FOR_ALL_TAB_WINDOWS(tp, wp)
3070 if (wp->w_buffer->b_fnum == fnum)
3071 {
3072 goto_tabpage_win(tp, wp);
3073 return TRUE;
3074 }
3075
3076 return FALSE;
3077}
3078
3079/*
3080 * Create a new window to show a file above the quickfix window. Called when
3081 * only the quickfix window is present.
3082 */
3083 static int
3084qf_open_new_file_win(qf_info_T *ll_ref)
3085{
3086 int flags;
3087
3088 flags = WSP_ABOVE;
3089 if (ll_ref != NULL)
3090 flags |= WSP_NEWLOC;
3091 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003092 return FAIL; // not enough room for window
3093 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003094 swb_flags = 0;
3095 RESET_BINDING(curwin);
3096 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003097 // The new window should use the location list from the
3098 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003099 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003100 return OK;
3101}
3102
3103/*
3104 * Go to a window that shows the right buffer. If the window is not found, go
3105 * to the window just above the location list window. This is used for opening
3106 * a file from a location window and not from a quickfix window. If some usable
3107 * window is previously found, then it is supplied in 'use_win'.
3108 */
3109 static void
3110qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3111{
3112 win_T *win = use_win;
3113
3114 if (win == NULL)
3115 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003116 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003117 FOR_ALL_WINDOWS(win)
3118 if (win->w_buffer->b_fnum == qf_fnum)
3119 break;
3120 if (win == NULL)
3121 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003122 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003123 win = curwin;
3124 do
3125 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003126 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003127 break;
3128 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003129 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003130 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003131 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003132 } while (win != curwin);
3133 }
3134 }
3135 win_goto(win);
3136
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003137 // If the location list for the window is not set, then set it
3138 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003139 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003140 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003141}
3142
3143/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003144 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3145 * not found, then go to the window just above the quickfix window. This is
3146 * used for opening a file from a quickfix window and not from a location
3147 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003148 */
3149 static void
3150qf_goto_win_with_qfl_file(int qf_fnum)
3151{
3152 win_T *win;
3153 win_T *altwin;
3154
3155 win = curwin;
3156 altwin = NULL;
3157 for (;;)
3158 {
3159 if (win->w_buffer->b_fnum == qf_fnum)
3160 break;
3161 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003162 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003163 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003164 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003165
3166 if (IS_QF_WINDOW(win))
3167 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003168 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003169 // window, unless 'switchbuf' contains 'uselast': in this case we
3170 // try to jump to the previously used window first.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003171 if ((swb_flags & SWB_USELAST) && win_valid(prevwin) &&
3172 !prevwin->w_p_wfb)
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003173 win = prevwin;
3174 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003175 win = altwin;
3176 else if (curwin->w_prev != NULL)
3177 win = curwin->w_prev;
3178 else
3179 win = curwin->w_next;
3180 break;
3181 }
3182
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003183 // Remember a usable window.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003184 if (altwin == NULL && !win->w_p_pvw && !win->w_p_wfb &&
3185 bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003186 altwin = win;
3187 }
3188
3189 win_goto(win);
3190}
3191
3192/*
3193 * Find a suitable window for opening a file (qf_fnum) from the
3194 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003195 * window, jump to it. Otherwise open a new window to display the file. If
3196 * 'newwin' is TRUE, then always open a new window. This is called from either
3197 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003198 */
3199 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003200qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003201{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003202 win_T *usable_wp = NULL;
3203 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003204 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003205
Bram Moolenaarb2443732018-11-11 22:50:27 +01003206 // If opening a new window, then don't use the location list referred by
3207 // the current window. Otherwise two windows will refer to the same
3208 // location list.
3209 if (!newwin)
3210 ll_ref = curwin->w_llist_ref;
3211
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003212 if (ll_ref != NULL)
3213 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003214 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003215 usable_wp = qf_find_win_with_loclist(ll_ref);
3216 if (usable_wp != NULL)
3217 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003218 }
3219
3220 if (!usable_win)
3221 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003222 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003223 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003224 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003225 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003226 }
3227
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003228 // If no usable window is found and 'switchbuf' contains "usetab"
3229 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003230 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003231 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003232
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003233 // If there is only one window and it is the quickfix window, create a
3234 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003235 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003236 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003237 if (qf_open_new_file_win(ll_ref) != OK)
3238 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003239 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003240 }
3241 else
3242 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003243 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003244 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003245 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003246 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003247 }
3248
3249 return OK;
3250}
3251
3252/*
3253 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003254 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003255 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003256 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003257 */
3258 static int
3259qf_jump_edit_buffer(
3260 qf_info_T *qi,
3261 qfline_T *qf_ptr,
3262 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003263 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003264 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003265{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003266 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003267 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003268 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003269 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003270 int old_qf_curlist = qi->qf_curlist;
3271 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003272
3273 if (qf_ptr->qf_type == 1)
3274 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003275 // Open help file (do_ecmd() will set b_help flag, readfile() will
3276 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003277 if (!can_abandon(curbuf, forceit))
3278 {
3279 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003280 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003281 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003282
3283 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3284 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003285 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003286 }
3287 else
Colin Kennedy21570352024-03-03 16:16:47 +01003288 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003289 int fnum = qf_ptr->qf_fnum;
3290
3291 if (!forceit && curwin->w_p_wfb && curbuf->b_fnum != fnum)
Colin Kennedy21570352024-03-03 16:16:47 +01003292 {
3293 if (qi->qfl_type == QFLT_LOCATION)
3294 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003295 // Location lists cannot split or reassign their window
3296 // so 'winfixbuf' windows must fail
3297 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3298 return FAIL;
Colin Kennedy21570352024-03-03 16:16:47 +01003299 }
3300
Sean Dewar4bb505e2024-03-05 20:39:07 +01003301 if (win_valid(prevwin) && !prevwin->w_p_wfb &&
3302 !bt_quickfix(prevwin->w_buffer))
Colin Kennedy21570352024-03-03 16:16:47 +01003303 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003304 // 'winfixbuf' is set; attempt to change to a window without it
3305 // that isn't a quickfix/location list window.
3306 win_goto(prevwin);
3307 }
3308 if (curwin->w_p_wfb)
3309 {
3310 // Split the window, which will be 'nowinfixbuf', and set curwin
3311 // to that
3312 if (win_split(0, 0) == OK)
3313 *opened_window = TRUE;
3314
3315 if (curwin->w_p_wfb)
3316 {
3317 // Autocommands set 'winfixbuf' or sent us to another window
Sean Dewar769eb2d2024-03-07 21:37:50 +01003318 // with it set, or we failed to split the window. Give up,
3319 // but don't return immediately, as they may have messed
3320 // with the list.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003321 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3322 retval = FAIL;
3323 }
Colin Kennedy21570352024-03-03 16:16:47 +01003324 }
3325 }
3326
Sean Dewar4bb505e2024-03-05 20:39:07 +01003327 if (retval == OK)
3328 {
3329 retval = buflist_getfile(fnum,
3330 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
3331 }
Colin Kennedy21570352024-03-03 16:16:47 +01003332 }
Bram Moolenaar3c097222017-12-21 20:54:49 +01003333
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003334 // If a location list, check whether the associated window is still
3335 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003336 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003337 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003338 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003339
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003340 if (wp == NULL && curwin->w_llist != qi)
3341 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003342 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003343 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003344 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003345 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003346 }
3347
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003348 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003349 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003350 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003351 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003352 }
3353
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003354 // Check if the list was changed. The pointers may happen to be identical,
3355 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003356 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003357 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003358 || !is_qf_entry_present(qfl, qf_ptr))
3359 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003360 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003361 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003362 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003363 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003364 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003365 }
3366
3367 return retval;
3368}
3369
3370/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003371 * Go to the error line in the current file using either line/column number or
3372 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003373 */
3374 static void
3375qf_jump_goto_line(
3376 linenr_T qf_lnum,
3377 int qf_col,
3378 char_u qf_viscol,
3379 char_u *qf_pattern)
3380{
3381 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003382
3383 if (qf_pattern == NULL)
3384 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003385 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003386 i = qf_lnum;
3387 if (i > 0)
3388 {
3389 if (i > curbuf->b_ml.ml_line_count)
3390 i = curbuf->b_ml.ml_line_count;
3391 curwin->w_cursor.lnum = i;
3392 }
3393 if (qf_col > 0)
3394 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003395 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003396 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003397 coladvance(qf_col - 1);
3398 else
3399 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003400 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003401 check_cursor();
3402 }
3403 else
3404 beginline(BL_WHITE | BL_FIX);
3405 }
3406 else
3407 {
3408 pos_T save_cursor;
3409
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003410 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003411 save_cursor = curwin->w_cursor;
3412 curwin->w_cursor.lnum = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02003413 if (!do_search(NULL, '/', '/', qf_pattern, STRLEN(qf_pattern), (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003414 curwin->w_cursor = save_cursor;
3415 }
3416}
3417
3418/*
3419 * Display quickfix list index and size message
3420 */
3421 static void
3422qf_jump_print_msg(
3423 qf_info_T *qi,
3424 int qf_index,
3425 qfline_T *qf_ptr,
3426 buf_T *old_curbuf,
3427 linenr_T old_lnum)
3428{
3429 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003430 garray_T *gap;
3431
3432 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003433
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003434 // Update the screen before showing the message, unless the screen
3435 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003436 if (!msg_scrolled)
3437 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003438 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003439 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003440 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3441 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003442 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003443 ga_concat(gap, IObuff);
3444 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Shane Harper5bf04282023-06-07 19:09:57 +01003445 ga_append(gap, NUL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003446
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003447 // Output the message. Overwrite to avoid scrolling when the 'O'
3448 // flag is present in 'shortmess'; But when not jumping, print the
3449 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003450 i = msg_scroll;
3451 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3452 msg_scroll = TRUE;
3453 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3454 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003455 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003456 msg_scroll = i;
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003457
3458 qfga_clear();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003459}
3460
3461/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003462 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003463 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3464 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003465 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3466 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003467 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3468 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003469 */
3470 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003471qf_jump_open_window(
3472 qf_info_T *qi,
3473 qfline_T *qf_ptr,
3474 int newwin,
3475 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003476{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003477 qf_list_T *qfl = qf_get_curlist(qi);
3478 int old_changedtick = qfl->qf_changedtick;
3479 int old_qf_curlist = qi->qf_curlist;
3480 qfltype_T qfl_type = qfl->qfl_type;
3481
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003482 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003483 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3484 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003485 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003486 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003487 if (old_qf_curlist != qi->qf_curlist
3488 || old_changedtick != qfl->qf_changedtick
3489 || !is_qf_entry_present(qfl, qf_ptr))
3490 {
3491 if (qfl_type == QFLT_QUICKFIX)
3492 emsg(_(e_current_quickfix_list_was_changed));
3493 else
3494 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003495 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003496 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003497
3498 // If currently in the quickfix window, find another window to show the
3499 // file in.
3500 if (bt_quickfix(curbuf) && !*opened_window)
3501 {
3502 // If there is no file specified, we don't know where to go.
3503 // But do advance, otherwise ":cn" gets stuck.
3504 if (qf_ptr->qf_fnum == 0)
3505 return NOTDONE;
3506
Bram Moolenaarb2443732018-11-11 22:50:27 +01003507 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3508 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003509 return FAIL;
3510 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003511 if (old_qf_curlist != qi->qf_curlist
3512 || old_changedtick != qfl->qf_changedtick
3513 || !is_qf_entry_present(qfl, qf_ptr))
3514 {
3515 if (qfl_type == QFLT_QUICKFIX)
3516 emsg(_(e_current_quickfix_list_was_changed));
3517 else
3518 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003519 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003520 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003521
3522 return OK;
3523}
3524
3525/*
3526 * Edit a selected file from the quickfix/location list and jump to a
3527 * particular line/column, adjust the folds and display a message about the
3528 * jump.
3529 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003530 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003531 * the file.
3532 */
3533 static int
3534qf_jump_to_buffer(
3535 qf_info_T *qi,
3536 int qf_index,
3537 qfline_T *qf_ptr,
3538 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003539 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003540 int *opened_window,
3541 int openfold,
3542 int print_message)
3543{
3544 buf_T *old_curbuf;
3545 linenr_T old_lnum;
3546 int retval = OK;
3547
3548 // If there is a file name, read the wanted file if needed, and check
3549 // autowrite etc.
3550 old_curbuf = curbuf;
3551 old_lnum = curwin->w_cursor.lnum;
3552
3553 if (qf_ptr->qf_fnum != 0)
3554 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003555 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003556 opened_window);
3557 if (retval != OK)
3558 return retval;
3559 }
3560
3561 // When not switched to another buffer, still need to set pc mark
3562 if (curbuf == old_curbuf)
3563 setpcmark();
3564
3565 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3566 qf_ptr->qf_pattern);
3567
3568#ifdef FEAT_FOLDING
3569 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3570 foldOpenCursor();
3571#endif
3572 if (print_message)
3573 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3574
3575 return retval;
3576}
3577
3578/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003579 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 */
3581 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003582qf_jump(qf_info_T *qi,
3583 int dir,
3584 int errornr,
3585 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003587 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3588}
3589
3590/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003591 * Jump to a quickfix line.
3592 * If dir == 0 go to entry "errornr".
3593 * If dir == FORWARD go "errornr" valid entries forward.
3594 * If dir == BACKWARD go "errornr" valid entries backward.
3595 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3596 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3597 * else if "errornr" is zero, redisplay the same line
3598 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003599 * If 'newwin' is TRUE, then open the file in a new window.
3600 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003601 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003602qf_jump_newwin(qf_info_T *qi,
3603 int dir,
3604 int errornr,
3605 int forceit,
3606 int newwin)
3607{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003608 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003609 qfline_T *qf_ptr;
3610 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003613 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003614 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003615 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003618 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003619 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003621 if (qi == NULL)
3622 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003623
Bram Moolenaar0398e002019-03-21 21:12:49 +01003624 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003626 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return;
3628 }
3629
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003630 incr_quickfix_busy();
3631
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003632 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003633
3634 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003636 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003638
3639 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3640 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003642 qf_ptr = old_qf_ptr;
3643 qf_index = old_qf_index;
3644 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003647 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003648 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003649 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003650 // No need to print the error message if it's visible in the error
3651 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 print_message = FALSE;
3653
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003654 prev_winid = curwin->w_id;
3655
Bram Moolenaarb2443732018-11-11 22:50:27 +01003656 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003657 if (retval == FAIL)
3658 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003659 if (retval == QF_ABORT)
3660 {
3661 qi = NULL;
3662 qf_ptr = NULL;
3663 goto theend;
3664 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003665 if (retval == NOTDONE)
3666 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003667
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003668 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003669 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003670 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003672 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003673 qi = NULL;
3674 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003677 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003680 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003681 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003683 // Couldn't open file, so put index back where it was. This could
3684 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 qf_ptr = old_qf_ptr;
3687 qf_index = old_qf_index;
3688 }
3689 }
3690theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003691 if (qi != NULL)
3692 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003693 qfl->qf_ptr = qf_ptr;
3694 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003695 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003696 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003698 // Restore old 'switchbuf' value, but not when an autocommand or
3699 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003700 p_swb = old_swb;
3701 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003703 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704}
3705
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003706// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003707static int qfFileAttr;
3708static int qfSepAttr;
3709static int qfLineAttr;
3710
3711/*
3712 * Display information about a single entry from the quickfix/location list.
3713 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003714 * 'cursel' will be set to TRUE for the currently selected entry in the
3715 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003716 */
3717 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003718qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003719{
3720 char_u *fname;
3721 buf_T *buf;
3722 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003723 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003724
3725 fname = NULL;
3726 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3727 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3728 (char *)qfp->qf_module);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003729 else
3730 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003731 if (qfp->qf_fnum != 0
3732 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3733 {
Austin Chang29822992024-10-03 10:50:05 +02003734 if (qfp->qf_fname == NULL)
3735 fname = buf->b_fname;
3736 else
3737 fname = qfp->qf_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003738 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003739 fname = gettail(fname);
3740 }
3741 if (fname == NULL)
3742 sprintf((char *)IObuff, "%2d", qf_idx);
3743 else
3744 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3745 qf_idx, (char *)fname);
3746 }
3747
3748 // Support for filtering entries using :filter /pat/ clist
3749 // Match against the module name, file name, search pattern and
3750 // text of the entry.
3751 filter_entry = TRUE;
3752 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3753 filter_entry &= message_filtered(qfp->qf_module);
3754 if (filter_entry && fname != NULL)
3755 filter_entry &= message_filtered(fname);
3756 if (filter_entry && qfp->qf_pattern != NULL)
3757 filter_entry &= message_filtered(qfp->qf_pattern);
3758 if (filter_entry)
3759 filter_entry &= message_filtered(qfp->qf_text);
3760 if (filter_entry)
3761 return;
3762
3763 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003764 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003765
3766 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003767 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003768 gap = qfga_get();
Shane Harper5bf04282023-06-07 19:09:57 +01003769 if (qfp->qf_lnum != 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003770 qf_range_text(gap, qfp);
3771 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
3772 ga_append(gap, NUL);
3773 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003774 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003775 if (qfp->qf_pattern != NULL)
3776 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003777 gap = qfga_get();
3778 qf_fmt_text(gap, qfp->qf_pattern);
Shane Harper5bf04282023-06-07 19:09:57 +01003779 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003780 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003781 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003782 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003783 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003784
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003785 // Remove newlines and leading whitespace from the text. For an
3786 // unrecognized line keep the indent, the compiler may mark a word
3787 // with ^^^^.
3788 gap = qfga_get();
3789 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
Shane Harper5bf04282023-06-07 19:09:57 +01003790 ? skipwhite(qfp->qf_text) : qfp->qf_text);
3791 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003792 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003793 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003794}
3795
3796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003798 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 */
3800 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003801qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003803 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003804 qfline_T *qfp;
3805 int i;
3806 int idx1 = 1;
3807 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003808 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003809 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003810 int all = eap->forceit; // if not :cl!, only show
3811 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003812 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003814 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3815 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816
Bram Moolenaar0398e002019-03-21 21:12:49 +01003817 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003819 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 return;
3821 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003822 if (*arg == '+')
3823 {
3824 ++arg;
3825 plus = TRUE;
3826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3828 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003829 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 return;
3831 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003832 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003833 if (plus)
3834 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003835 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003836 idx2 = i + idx1;
3837 idx1 = i;
3838 }
3839 else
3840 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003841 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003842 if (idx1 < 0)
3843 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3844 if (idx2 < 0)
3845 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003848 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003849 shorten_fnames(FALSE);
3850
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003851 // Get the attributes for the different quickfix highlight items. Note
3852 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003853 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3854 if (qfFileAttr == 0)
3855 qfFileAttr = HL_ATTR(HLF_D);
3856 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3857 if (qfSepAttr == 0)
3858 qfSepAttr = HL_ATTR(HLF_D);
3859 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3860 if (qfLineAttr == 0)
3861 qfLineAttr = HL_ATTR(HLF_N);
3862
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003863 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003865 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003868 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 ui_breakcheck();
3871 }
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003872 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873}
3874
3875/*
3876 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003877 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 */
3879 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003880qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 char_u *p = text;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003883 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
3885 if (*p == '\n')
3886 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003887 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003889 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 break;
3891 }
3892 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003893 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895}
3896
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003897/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003898 * Add the range information from the lnum, col, end_lnum, and end_col values
3899 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02003900 */
3901 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003902qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02003903{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003904 char_u *buf = IObuff;
3905 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02003906 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003907
thinca6864efa2021-06-19 20:45:20 +02003908 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3909 len = (int)STRLEN(buf);
3910
3911 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3912 {
Shane Harper5bf04282023-06-07 19:09:57 +01003913 vim_snprintf((char *)buf + len, bufsize - len, "-%ld",
3914 qfp->qf_end_lnum);
thinca6864efa2021-06-19 20:45:20 +02003915 len += (int)STRLEN(buf + len);
3916 }
3917 if (qfp->qf_col > 0)
3918 {
3919 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3920 len += (int)STRLEN(buf + len);
3921 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3922 {
Shane Harper5bf04282023-06-07 19:09:57 +01003923 vim_snprintf((char *)buf + len, bufsize - len, "-%d",
3924 qfp->qf_end_col);
thinca6864efa2021-06-19 20:45:20 +02003925 len += (int)STRLEN(buf + len);
3926 }
3927 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003928
3929 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02003930}
3931
3932/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003933 * Display information (list number, list size and the title) about a
3934 * quickfix/location list.
3935 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003936 static void
3937qf_msg(qf_info_T *qi, int which, char *lead)
3938{
3939 char *title = (char *)qi->qf_lists[which].qf_title;
3940 int count = qi->qf_lists[which].qf_count;
3941 char_u buf[IOSIZE];
3942
3943 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3944 lead,
3945 which + 1,
3946 qi->qf_listcount,
3947 count);
3948
3949 if (title != NULL)
3950 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003951 size_t len = STRLEN(buf);
3952
3953 if (len < 34)
3954 {
3955 vim_memset(buf + len, ' ', 34 - len);
3956 buf[34] = NUL;
3957 }
3958 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003959 }
3960 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003961 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003962}
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964/*
3965 * ":colder [count]": Up in the quickfix stack.
3966 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003967 * ":lolder [count]": Up in the location list stack.
3968 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 */
3970 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003971qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003973 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 int count;
3975
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003976 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3977 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (eap->addr_count != 0)
3980 count = eap->line2;
3981 else
3982 count = 1;
3983 while (count--)
3984 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003985 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003987 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003989 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003992 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 else
3995 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003996 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003998 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003999 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004001 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004004 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004005 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006}
4007
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004008/*
4009 * Display the information about all the quickfix/location lists in the stack
4010 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004011 void
4012qf_history(exarg_T *eap)
4013{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004014 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004015 int i;
4016
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004017 if (eap->addr_count > 0)
4018 {
4019 if (qi == NULL)
4020 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004021 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004022 return;
4023 }
4024
4025 // Jump to the specified quickfix list
4026 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
4027 {
4028 qi->qf_curlist = eap->line2 - 1;
4029 qf_msg(qi, qi->qf_curlist, "");
4030 qf_update_buffer(qi, NULL);
4031 }
4032 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004033 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004034
4035 return;
4036 }
4037
Bram Moolenaar5b69c222019-01-11 14:50:06 +01004038 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004039 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004040 else
4041 for (i = 0; i < qi->qf_listcount; ++i)
4042 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
4043}
4044
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004046 * Free all the entries in the error list "idx". Note that other information
4047 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 */
4049 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004050qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004052 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004053 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01004054 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004056 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004058 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004059 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004060 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004061 {
Austin Chang29822992024-10-03 10:50:05 +02004062 vim_free(qfp->qf_fname);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004063 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004064 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004065 vim_free(qfp->qf_pattern);
Tom Praschanca6ac992023-08-11 23:26:12 +02004066 clear_tv(&qfp->qf_user_data);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004067 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004068 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01004069 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004070 // Somehow qf_count may have an incorrect value, set it to 1
4071 // to avoid crashing when it's wrong.
4072 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004073 qfl->qf_count = 1;
Christian Brabandt567cae22023-11-19 16:19:27 +01004074 else
4075 qfl->qf_start = qfpnext;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004076 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004077 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004079
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004080 qfl->qf_index = 0;
4081 qfl->qf_start = NULL;
4082 qfl->qf_last = NULL;
4083 qfl->qf_ptr = NULL;
4084 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02004085
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004086 qf_clean_dir_stack(&qfl->qf_dir_stack);
4087 qfl->qf_directory = NULL;
4088 qf_clean_dir_stack(&qfl->qf_file_stack);
4089 qfl->qf_currfile = NULL;
4090 qfl->qf_multiline = FALSE;
4091 qfl->qf_multiignore = FALSE;
4092 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093}
4094
4095/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004096 * Free error list "idx". Frees all the entries in the quickfix list,
4097 * associated context information and the title.
4098 */
4099 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004100qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004101{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004102 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004103
Bram Moolenaard23a8232018-02-10 18:45:26 +01004104 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004105 free_tv(qfl->qf_ctx);
4106 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004107 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004108 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004109 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004110}
4111
4112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * qf_mark_adjust: adjust marks
4114 */
4115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004116qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02004117 win_T *wp,
4118 linenr_T line1,
4119 linenr_T line2,
4120 long amount,
4121 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004123 int i;
4124 qfline_T *qfp;
4125 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004126 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004127 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004128 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
Bram Moolenaarc1542742016-07-20 21:44:37 +02004130 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004131 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004132 if (wp != NULL)
4133 {
4134 if (wp->w_llist == NULL)
4135 return;
4136 qi = wp->w_llist;
4137 }
4138
4139 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004140 {
4141 qf_list_T *qfl = qf_get_list(qi, idx);
4142
4143 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004144 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 if (qfp->qf_fnum == curbuf->b_fnum)
4146 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004147 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4149 {
4150 if (amount == MAXLNUM)
4151 qfp->qf_cleared = TRUE;
4152 else
4153 qfp->qf_lnum += amount;
4154 }
4155 else if (amount_after && qfp->qf_lnum > line2)
4156 qfp->qf_lnum += amount_after;
4157 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004158 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004159
4160 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004161 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
4163
4164/*
4165 * Make a nice message out of the error character and the error number:
4166 * char number message
4167 * e or E 0 " error"
4168 * w or W 0 " warning"
4169 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004170 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 * 0 0 ""
4172 * other 0 " c"
4173 * e or E n " error n"
4174 * w or W n " warning n"
4175 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004176 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 * 0 n " error n"
4178 * other n " c n"
4179 * 1 x "" :helpgrep
4180 */
4181 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004182qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
4184 static char_u buf[20];
4185 static char_u cc[3];
4186 char_u *p;
4187
4188 if (c == 'W' || c == 'w')
4189 p = (char_u *)" warning";
4190 else if (c == 'I' || c == 'i')
4191 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004192 else if (c == 'N' || c == 'n')
4193 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4195 p = (char_u *)" error";
4196 else if (c == 0 || c == 1)
4197 p = (char_u *)"";
4198 else
4199 {
4200 cc[0] = ' ';
4201 cc[1] = c;
4202 cc[2] = NUL;
4203 p = cc;
4204 }
4205
4206 if (nr <= 0)
4207 return p;
4208
4209 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4210 return buf;
4211}
4212
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004214 * When "split" is FALSE: Open the entry/result under the cursor.
4215 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4216 */
4217 void
4218qf_view_result(int split)
4219{
4220 qf_info_T *qi = &ql_info;
4221
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004222 if (IS_LL_WINDOW(curwin))
4223 qi = GET_LOC_LIST(curwin);
4224
Bram Moolenaar0398e002019-03-21 21:12:49 +01004225 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004226 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004227 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004228 return;
4229 }
4230
4231 if (split)
4232 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004233 // Open the selected entry in a new window
4234 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4235 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004236 return;
4237 }
4238
4239 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4240}
4241
4242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 * ":cwindow": open the quickfix window if we have errors to display,
4244 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004245 * ":lwindow": open the location list window if we have locations to display,
4246 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 */
4248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004249ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004251 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004252 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 win_T *win;
4254
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004255 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4256 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004257
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004258 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004259
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004260 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004261 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004263 // If a quickfix window is open but we have no errors to display,
4264 // close the window. If a quickfix window is not open, then open
4265 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004266 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004267 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004268 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 {
4270 if (win != NULL)
4271 ex_cclose(eap);
4272 }
4273 else if (win == NULL)
4274 ex_copen(eap);
4275}
4276
4277/*
4278 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004279 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004282ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004284 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004285 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004287 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4288 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004290 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004291 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (win != NULL)
4293 win_close(win, FALSE);
4294}
4295
4296/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004297 * Set "w:quickfix_title" if "qi" has a title.
4298 */
4299 static void
4300qf_set_title_var(qf_list_T *qfl)
4301{
4302 if (qfl->qf_title != NULL)
4303 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4304}
4305
4306/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004307 * Goto a quickfix or location list window (if present).
4308 * Returns OK if the window is found, FAIL otherwise.
4309 */
4310 static int
4311qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4312{
4313 win_T *win;
4314
4315 win = qf_find_win(qi);
4316 if (win == NULL)
4317 return FAIL;
4318
4319 win_goto(win);
4320 if (resize)
4321 {
4322 if (vertsplit)
4323 {
4324 if (sz != win->w_width)
4325 win_setwidth(sz);
4326 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004327 else if (sz != win->w_height && win->w_height
4328 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004329 win_setheight(sz);
4330 }
4331
4332 return OK;
4333}
4334
4335/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004336 * Set options for the buffer in the quickfix or location list window.
4337 */
4338 static void
4339qf_set_cwindow_options(void)
4340{
4341 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004342 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4343 set_option_value_give_err((char_u *)"bt",
4344 0L, (char_u *)"quickfix", OPT_LOCAL);
4345 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004346 RESET_BINDING(curwin);
4347#ifdef FEAT_DIFF
4348 curwin->w_p_diff = FALSE;
4349#endif
4350#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004351 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004352 OPT_LOCAL);
4353#endif
4354}
4355
4356/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004357 * Open a new quickfix or location list window, load the quickfix buffer and
4358 * set the appropriate options for the window.
4359 * Returns FAIL if the window could not be opened.
4360 */
4361 static int
4362qf_open_new_cwindow(qf_info_T *qi, int height)
4363{
4364 buf_T *qf_buf;
4365 win_T *oldwin = curwin;
4366 tabpage_T *prevtab = curtab;
4367 int flags = 0;
4368 win_T *win;
4369
4370 qf_buf = qf_find_buf(qi);
4371
4372 // The current window becomes the previous window afterwards.
4373 win = curwin;
4374
Bram Moolenaare1004402020-10-24 20:49:43 +02004375 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004376 // Create the new quickfix window at the very bottom, except when
4377 // :belowright or :aboveleft is used.
4378 win_goto(lastwin);
4379 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004380 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004381 flags = WSP_BELOW;
4382 flags |= WSP_NEWLOC;
4383 if (win_split(height, flags) == FAIL)
4384 return FAIL; // not enough room for window
4385 RESET_BINDING(curwin);
4386
4387 if (IS_LL_STACK(qi))
4388 {
4389 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004390 // location list stack from the window 'win'.
4391 curwin->w_llist_ref = qi;
4392 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004393 }
4394
4395 if (oldwin != curwin)
4396 oldwin = NULL; // don't store info when in another window
4397 if (qf_buf != NULL)
4398 {
4399 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004400 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004401 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004402 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004403 }
4404 else
4405 {
4406 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004407 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4408 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004409 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004410
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004411 // save the number of the new buffer
4412 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004413 }
4414
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004415 // Set the options for the quickfix buffer/window (if not already done)
4416 // Do this even if the quickfix buffer was already present, as an autocmd
4417 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004418 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004419 qf_set_cwindow_options();
4420
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004421 // Only set the height when still in the same tab page and there is no
4422 // window to the side.
4423 if (curtab == prevtab && curwin->w_width == Columns)
4424 win_setheight(height);
4425 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4426 if (win_valid(win))
4427 prevwin = win;
4428
4429 return OK;
4430}
4431
4432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004434 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 */
4436 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004437ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004439 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004440 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004442 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004443 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004445 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4446 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004447
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004448 incr_quickfix_busy();
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (eap->addr_count != 0)
4451 height = eap->line2;
4452 else
4453 height = QF_WINHEIGHT;
4454
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004455 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456#ifdef FEAT_GUI
4457 need_mouse_correct = TRUE;
4458#endif
4459
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004460 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004461 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004462 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004463 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004464 if (status == FAIL)
4465 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004466 {
4467 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004468 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004471 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004472 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004473 // Save the current index here, as updating the quickfix buffer may free
4474 // the quickfix list
4475 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004476
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004477 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004478 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004480 decr_quickfix_busy();
4481
4482 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 curwin->w_cursor.col = 0;
4484 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004485 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486}
4487
4488/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004489 * Move the cursor in the quickfix window to "lnum".
4490 */
4491 static void
4492qf_win_goto(win_T *win, linenr_T lnum)
4493{
4494 win_T *old_curwin = curwin;
4495
4496 curwin = win;
4497 curbuf = win->w_buffer;
4498 curwin->w_cursor.lnum = lnum;
4499 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004500 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004501 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004502 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004503 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004504 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004505 curwin = old_curwin;
4506 curbuf = curwin->w_buffer;
4507}
4508
4509/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004510 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004511 */
4512 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004513ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004514{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004515 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004516 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004517
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004518 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4519 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004520
4521 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004522 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4523 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4524}
4525
4526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 * Return the number of the current entry (line number in the quickfix
4528 * window).
4529 */
4530 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004531qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004533 qf_info_T *qi = &ql_info;
4534
4535 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004536 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004537 qi = wp->w_llist_ref;
4538
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004539 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540}
4541
4542/*
4543 * Update the cursor position in the quickfix window to the current error.
4544 * Return TRUE if there is a quickfix window.
4545 */
4546 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004547qf_win_pos_update(
4548 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004549 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
4551 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004552 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004554 // Put the cursor on the current error in the quickfix window, so that
4555 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004556 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (win != NULL
4558 && qf_index <= win->w_buffer->b_ml.ml_line_count
4559 && old_qf_index != qf_index)
4560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 if (qf_index > old_qf_index)
4562 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004563 win->w_redraw_top = old_qf_index;
4564 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566 else
4567 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004568 win->w_redraw_top = qf_index;
4569 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004571 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
4573 return win != NULL;
4574}
4575
4576/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004577 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004578 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004579 */
4580 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004581is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004582{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004583 // A window displaying the quickfix buffer will have the w_llist_ref field
4584 // set to NULL.
4585 // A window displaying a location list buffer will have the w_llist_ref
4586 // pointing to the location list.
Christian Brabandtfc682992023-09-03 20:20:52 +02004587 if (buf_valid(win->w_buffer) && bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004588 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4589 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004590 return TRUE;
4591
4592 return FALSE;
4593}
4594
4595/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004596 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4597 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004598 */
4599 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004600qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004601{
4602 win_T *win;
4603
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004604 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004605 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004606 return win;
4607 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004608}
4609
4610/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004611 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004612 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 */
4614 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004615qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004617 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004618 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004620 if (qi->qf_bufnr != INVALID_QFBUFNR)
4621 {
4622 buf_T *qfbuf;
4623 qfbuf = buflist_findnr(qi->qf_bufnr);
4624 if (qfbuf != NULL)
4625 return qfbuf;
4626 // buffer is no longer present
4627 qi->qf_bufnr = INVALID_QFBUFNR;
4628 }
4629
Bram Moolenaar9c102382006-05-03 21:26:49 +00004630 FOR_ALL_TAB_WINDOWS(tp, win)
4631 if (is_qf_win(win, qi))
4632 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004633
Bram Moolenaar9c102382006-05-03 21:26:49 +00004634 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635}
4636
4637/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004638 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004639 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004640 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004641 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004642did_set_quickfixtextfunc(optset_T *args UNUSED)
Bram Moolenaard43906d2020-07-20 21:31:32 +02004643{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004644 if (option_set_callback_func(p_qftf, &qftf_cb) == FAIL)
4645 return e_invalid_argument;
4646
4647 return NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004648}
4649
4650/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004651 * Update the w:quickfix_title variable in the quickfix/location list window in
4652 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004653 */
4654 static void
4655qf_update_win_titlevar(qf_info_T *qi)
4656{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004657 qf_list_T *qfl = qf_get_curlist(qi);
4658 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004659 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004660 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004661
Bram Moolenaar530bed92020-12-16 21:02:56 +01004662 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004663 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004664 if (is_qf_win(win, qi))
4665 {
4666 curwin = win;
4667 qf_set_title_var(qfl);
4668 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004669 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004670 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004671}
4672
4673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 * Find the quickfix buffer. If it exists, update the contents.
4675 */
4676 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004677qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004680 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004683 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004684 buf = qf_find_buf(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004685 if (buf == NULL)
4686 return;
4687
4688 linenr_T old_line_count = buf->b_ml.ml_line_count;
4689 int qf_winid = 0;
4690
4691 if (IS_LL_STACK(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004693 if (curwin->w_llist == qi)
4694 win = curwin;
4695 else
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004696 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004697 // Find the file window (non-quickfix) with this location list
4698 win = qf_find_win_with_loclist(qi);
4699 if (win == NULL)
4700 // File window is not found. Find the location list window.
4701 win = qf_find_win(qi);
4702 if (win == NULL)
4703 return;
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004704 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004705 qf_winid = win->w_id;
4706 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004707
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004708 // autocommands may cause trouble
4709 incr_quickfix_busy();
Bram Moolenaard0fab102022-10-20 16:03:33 +01004710
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004711 int do_fill = TRUE;
4712 if (old_last == NULL)
4713 {
4714 // set curwin/curbuf to buf and save a few things
4715 aucmd_prepbuf(&aco, buf);
4716 if (curbuf != buf)
4717 do_fill = FALSE; // failed to find a window for "buf"
4718 }
4719
4720 if (do_fill)
4721 {
4722 qf_update_win_titlevar(qi);
4723
4724 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
4725 ++CHANGEDTICK(buf);
4726
Bram Moolenaar864293a2016-06-02 13:40:04 +02004727 if (old_last == NULL)
4728 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004729 (void)qf_win_pos_update(qi, 0);
4730
4731 // restore curwin/curbuf and a few other things
4732 aucmd_restbuf(&aco);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004735
4736 // Only redraw when added lines are visible. This avoids flickering
4737 // when the added lines are not visible.
4738 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4739 redraw_buf_later(buf, UPD_NOT_VALID);
4740
4741 // always called after incr_quickfix_busy()
4742 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743}
4744
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004745/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004746 * Add an error line to the quickfix buffer.
4747 */
4748 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004749qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004750 buf_T *buf, // quickfix window buffer
4751 linenr_T lnum,
4752 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004753 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004754 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004755 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004756{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004757 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004758 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004759
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004760 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004761
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004762 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004763 // for this entry, then use it.
4764 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004765 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004766 ga_concat(gap, qftf_str);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004767 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004768 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004769 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004770 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004771 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004772 else if (qfp->qf_fnum != 0
4773 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4774 && errbuf->b_fname != NULL)
4775 {
4776 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004777 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02004778 else
4779 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004780 // Shorten the file name if not done already.
4781 // For optimization, do this only for the first entry in a
4782 // buffer.
4783 if (first_bufline && (errbuf->b_sfname == NULL
4784 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004785 {
4786 if (*dirname == NUL)
4787 mch_dirname(dirname, MAXPATHL);
4788 shorten_buf_fname(errbuf, dirname, FALSE);
4789 }
Austin Chang29822992024-10-03 10:50:05 +02004790 if (qfp->qf_fname == NULL)
4791 ga_concat(gap, errbuf->b_fname);
4792 else
4793 ga_concat(gap, qfp->qf_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004794 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004795 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004796
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004797 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02004798
4799 if (qfp->qf_lnum > 0)
4800 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004801 qf_range_text(gap, qfp);
4802 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004803 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004804 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004805 qf_fmt_text(gap, qfp->qf_pattern);
4806 ga_append(gap, '|');
4807 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004808
Bram Moolenaar858ba062020-05-31 23:11:59 +02004809 // Remove newlines and leading whitespace from the text.
4810 // For an unrecognized line keep the indent, the compiler may
4811 // mark a word with ^^^^.
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004812 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text)
4813 : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004814 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004815
Shane Harper5bf04282023-06-07 19:09:57 +01004816 ga_append(gap, NUL);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004817 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004818 return FAIL;
4819
4820 return OK;
4821}
4822
Bram Moolenaard43906d2020-07-20 21:31:32 +02004823/*
4824 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4825 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4826 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004827 static list_T *
4828call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4829{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004830 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004831 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004832 static int recursive = FALSE;
4833
4834 if (recursive)
4835 return NULL; // this doesn't work properly recursively
4836 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004837
4838 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4839 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4840 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004841 if (qfl->qf_qftf_cb.cb_name != NULL)
4842 cb = &qfl->qf_qftf_cb;
4843 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004844 {
4845 typval_T args[1];
4846 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004847 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004848
4849 // create the dict argument
4850 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01004851 {
4852 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004853 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004854 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004855 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4856 dict_add_number(d, "winid", (long)qf_winid);
4857 dict_add_number(d, "id", (long)qfl->qf_id);
4858 dict_add_number(d, "start_idx", start_idx);
4859 dict_add_number(d, "end_idx", end_idx);
4860 ++d->dv_refcount;
4861 args[0].v_type = VAR_DICT;
4862 args[0].vval.v_dict = d;
4863
Bram Moolenaard43906d2020-07-20 21:31:32 +02004864 qftf_list = NULL;
4865 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4866 {
4867 if (rettv.v_type == VAR_LIST)
4868 {
4869 qftf_list = rettv.vval.v_list;
4870 qftf_list->lv_refcount++;
4871 }
4872 clear_tv(&rettv);
4873 }
4874 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004875 }
4876
Bram Moolenaard6c67622022-08-24 20:07:22 +01004877 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004878 return qftf_list;
4879}
4880
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 * Fill current buffer with quickfix errors, replacing any previous contents.
4883 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004884 * If "old_last" is not NULL append the items after this one.
4885 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4886 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 */
4888 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004889qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004891 linenr_T lnum;
4892 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004893 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004894 list_T *qftf_list = NULL;
4895 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
Bram Moolenaar864293a2016-06-02 13:40:04 +02004897 if (old_last == NULL)
4898 {
zeertzjqc7a8eb52024-05-08 20:22:40 +02004899 win_T *wp;
4900 tabpage_T *tp;
4901
Bram Moolenaar864293a2016-06-02 13:40:04 +02004902 if (buf != curbuf)
4903 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004904 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004905 return;
4906 }
4907
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004908 // delete all existing lines
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004909 //
4910 // Note: we cannot store undo information, because
4911 // qf buffer is usually not allowed to be modified.
4912 //
4913 // So we need to clean up undo information
4914 // otherwise autocommands may invalidate the undo stack
Bram Moolenaar864293a2016-06-02 13:40:04 +02004915 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004916 (void)ml_delete((linenr_T)1);
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004917
zeertzjqc7a8eb52024-05-08 20:22:40 +02004918 FOR_ALL_TAB_WINDOWS(tp, wp)
4919 if (wp->w_buffer == curbuf)
4920 wp->w_skipcol = 0;
4921
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004922 // Remove all undo information
Christian Brabandt9071ed82024-02-15 20:17:37 +01004923 u_clearallandblockfree(curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004926 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004927 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004929 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004930 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004931 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004932
4933 *dirname = NUL;
4934
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004935 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004936 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004937 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004938 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004939 lnum = 0;
4940 }
4941 else
4942 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004943 if (old_last->qf_next != NULL)
4944 qfp = old_last->qf_next;
4945 else
4946 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004947 lnum = buf->b_ml.ml_line_count;
4948 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004949
4950 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4951 (long)qfl->qf_count);
4952 if (qftf_list != NULL)
4953 qftf_li = qftf_list->lv_first;
4954
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004955 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004957 char_u *qftf_str = NULL;
4958
Bram Moolenaard43906d2020-07-20 21:31:32 +02004959 // Use the text supplied by the user defined function (if any).
4960 // If the returned value is not string, then ignore the rest
4961 // of the returned values and use the default.
4962 if (qftf_li != NULL && !invalid_val)
4963 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004964 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004965 if (qftf_str == NULL)
4966 invalid_val = TRUE;
4967 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004968
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004969 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4970 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004972
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004973 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004974 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004976 if (qfp == NULL)
4977 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004978
4979 if (qftf_li != NULL)
4980 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004982
4983 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004984 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004985 (void)ml_delete(lnum + 1);
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01004986
4987 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004990 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 check_lnums(TRUE);
4992
Bram Moolenaar864293a2016-06-02 13:40:04 +02004993 if (old_last == NULL)
4994 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004995 // Set the 'filetype' to "qf" each time after filling the buffer.
4996 // This resembles reading a file into a buffer, it's more logical when
4997 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004998 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004999 set_option_value_give_err((char_u *)"ft",
5000 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005001 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
zeertzjq5bf6c212024-03-31 18:41:27 +02005003 curbuf->b_keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02005004 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005006 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 FALSE, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +02005008 curbuf->b_keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02005009 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005010
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005011 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005012 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005015 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 KeyTyped = old_KeyTyped;
5017}
5018
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005019/*
5020 * For every change made to the quickfix list, update the changed tick.
5021 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01005022 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005023qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01005024{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005025 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005026}
5027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005029 * Return the quickfix/location list number with the given identifier.
5030 * Returns -1 if list is not found.
5031 */
5032 static int
5033qf_id2nr(qf_info_T *qi, int_u qfid)
5034{
5035 int qf_idx;
5036
5037 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
5038 if (qi->qf_lists[qf_idx].qf_id == qfid)
5039 return qf_idx;
5040 return INVALID_QFIDX;
5041}
5042
5043/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005044 * If the current list is not "save_qfid" and we can find the list with that ID
5045 * then make it the current list.
5046 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005047 * Returns OK if successfully restored the list. Returns FAIL if the list with
5048 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005049 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005050 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005051qf_restore_list(qf_info_T *qi, int_u save_qfid)
5052{
5053 int curlist;
5054
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00005055 if (qf_get_curlist(qi)->qf_id == save_qfid)
5056 return OK;
5057
5058 curlist = qf_id2nr(qi, save_qfid);
5059 if (curlist < 0)
5060 // list is not present
5061 return FAIL;
5062 qi->qf_curlist = curlist;
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005063 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005064}
5065
5066/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005067 * Jump to the first entry if there is one.
5068 */
5069 static void
5070qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
5071{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005072 if (qf_restore_list(qi, save_qfid) == FAIL)
5073 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005074
Colin Kennedy21570352024-03-03 16:16:47 +01005075
5076 if (!check_can_set_curbuf_forceit(forceit))
5077 return;
5078
5079
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005080 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005081 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005082 qf_jump(qi, 0, 0, forceit);
5083}
5084
5085/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005086 * Return TRUE when using ":vimgrep" for ":grep".
5087 */
5088 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005089grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005090{
Bram Moolenaar754b5602006-02-09 23:53:20 +00005091 return ((cmdidx == CMD_grep
5092 || cmdidx == CMD_lgrep
5093 || cmdidx == CMD_grepadd
5094 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005095 && STRCMP("internal",
5096 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
5097}
5098
5099/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005100 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005102 static char_u *
5103make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005105 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005106 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005107 case CMD_make: return (char_u *)"make";
5108 case CMD_lmake: return (char_u *)"lmake";
5109 case CMD_grep: return (char_u *)"grep";
5110 case CMD_lgrep: return (char_u *)"lgrep";
5111 case CMD_grepadd: return (char_u *)"grepadd";
5112 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5113 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115}
5116
5117/*
5118 * Return the name for the errorfile, in allocated memory.
5119 * Find a new unique name when 'makeef' contains "##".
5120 * Returns NULL for error.
5121 */
5122 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01005123get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
5125 char_u *p;
5126 char_u *name;
5127 static int start = -1;
5128 static int off = 0;
5129#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02005130 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131#endif
5132
5133 if (*p_mef == NUL)
5134 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005135 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005137 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 return name;
5139 }
5140
5141 for (p = p_mef; *p; ++p)
5142 if (p[0] == '#' && p[1] == '#')
5143 break;
5144
5145 if (*p == NUL)
5146 return vim_strsave(p_mef);
5147
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005148 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 for (;;)
5150 {
5151 if (start == -1)
5152 start = mch_get_pid();
5153 else
5154 off += 19;
5155
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005156 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (name == NULL)
5158 break;
5159 STRCPY(name, p_mef);
5160 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
5161 STRCAT(name, p + 2);
5162 if (mch_getperm(name) < 0
5163#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005164 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 && mch_lstat((char *)name, &sb) < 0
5166#endif
5167 )
5168 break;
5169 vim_free(name);
5170 }
5171 return name;
5172}
5173
5174/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005175 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5176 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5177 */
5178 static char_u *
5179make_get_fullcmd(char_u *makecmd, char_u *fname)
5180{
5181 char_u *cmd;
5182 unsigned len;
5183
5184 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5185 if (*p_sp != NUL)
5186 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005187 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005188 if (cmd == NULL)
5189 return NULL;
5190 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5191 (char *)p_shq);
5192
5193 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5194 if (*p_sp != NUL)
5195 append_redir(cmd, len, p_sp, fname);
5196
5197 // Display the fully formed command. Output a newline if there's something
5198 // else than the :make command that was typed (in which case the cursor is
5199 // in column 0).
5200 if (msg_col == 0)
5201 msg_didout = FALSE;
5202 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005203 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005204 msg_outtrans(cmd); // show what we are doing
5205
5206 return cmd;
5207}
5208
5209/*
5210 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5211 */
5212 void
5213ex_make(exarg_T *eap)
5214{
5215 char_u *fname;
5216 char_u *cmd;
5217 char_u *enc = NULL;
5218 win_T *wp = NULL;
5219 qf_info_T *qi = &ql_info;
5220 int res;
5221 char_u *au_name = NULL;
5222 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005223 char_u *errorformat = p_efm;
5224 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005225
5226 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5227 if (grep_internal(eap->cmdidx))
5228 {
5229 ex_vimgrep(eap);
5230 return;
5231 }
5232
5233 au_name = make_get_auname(eap->cmdidx);
5234 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5235 curbuf->b_fname, TRUE, curbuf))
5236 {
5237#ifdef FEAT_EVAL
5238 if (aborting())
5239 return;
5240#endif
5241 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005242 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005243
5244 if (is_loclist_cmd(eap->cmdidx))
5245 wp = curwin;
5246
5247 autowrite_all();
5248 fname = get_mef_name();
5249 if (fname == NULL)
5250 return;
5251 mch_remove(fname); // in case it's not unique
5252
5253 cmd = make_get_fullcmd(eap->arg, fname);
5254 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005255 {
5256 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005257 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005258 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005259
5260 // let the shell know if we are redirecting output or not
5261 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5262
5263#ifdef AMIGA
5264 out_flush();
5265 // read window status report and redraw before message
5266 (void)char_avail();
5267#endif
5268
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005269 incr_quickfix_busy();
5270
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005271 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5272 errorformat = p_gefm;
5273 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5274 newlist = FALSE;
5275
5276 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5277 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005278 if (wp != NULL)
5279 {
5280 qi = GET_LOC_LIST(wp);
5281 if (qi == NULL)
5282 goto cleanup;
5283 }
5284 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005285 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005286
5287 // Remember the current quickfix list identifier, so that we can
5288 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005289 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005290 if (au_name != NULL)
5291 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5292 curbuf->b_fname, TRUE, curbuf);
5293 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5294 // display the first error
5295 qf_jump_first(qi, save_qfid, FALSE);
5296
5297cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005298 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005299 mch_remove(fname);
5300 vim_free(fname);
5301 vim_free(cmd);
5302}
5303
5304/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005305 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005306 */
5307 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005308qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005309{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005310 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005311
5312 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5313 return 0;
5314 return qf_get_curlist(qi)->qf_count;
5315}
5316
5317/*
5318 * Returns the number of valid entries in the current quickfix/location list.
5319 */
5320 int
5321qf_get_valid_size(exarg_T *eap)
5322{
5323 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005324 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005325 qfline_T *qfp;
5326 int i, sz = 0;
5327 int prev_fnum = 0;
5328
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005329 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5330 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005331
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005332 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005333 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005334 {
5335 if (qfp->qf_valid)
5336 {
5337 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005338 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005339 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5340 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005341 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005342 sz++;
5343 prev_fnum = qfp->qf_fnum;
5344 }
5345 }
5346 }
5347
5348 return sz;
5349}
5350
5351/*
5352 * Returns the current index of the quickfix/location list.
5353 * Returns 0 if there is an error.
5354 */
5355 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005356qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005357{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005358 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005359
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005360 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5361 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005362
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005363 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005364}
5365
5366/*
5367 * Returns the current index in the quickfix/location list (counting only valid
5368 * entries). If no valid entries are in the list, then returns 1.
5369 */
5370 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005371qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005372{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005373 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005374 qf_list_T *qfl;
5375 qfline_T *qfp;
5376 int i, eidx = 0;
5377 int prev_fnum = 0;
5378
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005379 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5380 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005381
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005382 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005383 qfp = qfl->qf_start;
5384
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005385 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005386 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005387 return 1;
5388
5389 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5390 {
5391 if (qfp->qf_valid)
5392 {
5393 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5394 {
5395 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5396 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005397 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005398 eidx++;
5399 prev_fnum = qfp->qf_fnum;
5400 }
5401 }
5402 else
5403 eidx++;
5404 }
5405 }
5406
5407 return eidx ? eidx : 1;
5408}
5409
5410/*
5411 * Get the 'n'th valid error entry in the quickfix or location list.
5412 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5413 * For :cdo and :ldo returns the 'n'th valid error entry.
5414 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5415 */
5416 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005417qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005418{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005419 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005420 int i, eidx;
5421 int prev_fnum = 0;
5422
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005423 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005424 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005425 return 1;
5426
Bram Moolenaar95946f12019-03-31 15:31:59 +02005427 eidx = 0;
5428 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005429 {
5430 if (qfp->qf_valid)
5431 {
5432 if (fdo)
5433 {
5434 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5435 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005436 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005437 eidx++;
5438 prev_fnum = qfp->qf_fnum;
5439 }
5440 }
5441 else
5442 eidx++;
5443 }
5444
5445 if (eidx == n)
5446 break;
5447 }
5448
5449 if (i <= qfl->qf_count)
5450 return i;
5451 else
5452 return 1;
5453}
5454
5455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005457 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005458 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 */
5460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005461ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005463 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005464 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005465
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005466 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5467 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005468
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005469 if (eap->addr_count > 0)
5470 errornr = (int)eap->line2;
5471 else
5472 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005473 switch (eap->cmdidx)
5474 {
5475 case CMD_cc: case CMD_ll:
5476 errornr = 0;
5477 break;
5478 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5479 case CMD_lfirst:
5480 errornr = 1;
5481 break;
5482 default:
5483 errornr = 32767;
5484 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005485 }
5486
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005487 // For cdo and ldo commands, jump to the nth valid error.
5488 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005489 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5490 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005491 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005492 eap->addr_count > 0 ? (int)eap->line1 : 1,
5493 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5494
5495 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496}
5497
5498/*
5499 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005500 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005501 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 */
5503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005504ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005506 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005507 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005508 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005509
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005510 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5511 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005512
Bram Moolenaar55b69262017-08-13 13:42:01 +02005513 if (eap->addr_count > 0
5514 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5515 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005516 errornr = (int)eap->line2;
5517 else
5518 errornr = 1;
5519
Bram Moolenaar39665952018-08-15 20:59:48 +02005520 // Depending on the command jump to either next or previous entry/file.
5521 switch (eap->cmdidx)
5522 {
5523 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5524 dir = FORWARD;
5525 break;
5526 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5527 case CMD_lNext:
5528 dir = BACKWARD;
5529 break;
5530 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5531 dir = FORWARD_FILE;
5532 break;
5533 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5534 dir = BACKWARD_FILE;
5535 break;
5536 default:
5537 dir = FORWARD;
5538 break;
5539 }
5540
5541 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542}
5543
5544/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005545 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5546 * The index of the entry is stored in 'errornr'.
5547 * Returns NULL if an entry is not found.
5548 */
5549 static qfline_T *
5550qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5551{
5552 qfline_T *qfp = NULL;
5553 int idx = 0;
5554
5555 // Find the first entry in this file
5556 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5557 if (qfp->qf_fnum == bnr)
5558 break;
5559
5560 *errornr = idx;
5561 return qfp;
5562}
5563
5564/*
5565 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5566 * with the error number for the first entry. Assumes the entries are sorted in
5567 * the quickfix list by line number.
5568 */
5569 static qfline_T *
5570qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5571{
5572 while (!got_int
5573 && entry->qf_prev != NULL
5574 && entry->qf_fnum == entry->qf_prev->qf_fnum
5575 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5576 {
5577 entry = entry->qf_prev;
5578 --*errornr;
5579 }
5580
5581 return entry;
5582}
5583
5584/*
5585 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5586 * with the error number for the last entry. Assumes the entries are sorted in
5587 * the quickfix list by line number.
5588 */
5589 static qfline_T *
5590qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5591{
5592 while (!got_int &&
5593 entry->qf_next != NULL
5594 && entry->qf_fnum == entry->qf_next->qf_fnum
5595 && entry->qf_lnum == entry->qf_next->qf_lnum)
5596 {
5597 entry = entry->qf_next;
5598 ++*errornr;
5599 }
5600
5601 return entry;
5602}
5603
5604/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005605 * Returns TRUE if the specified quickfix entry is
5606 * after the given line (linewise is TRUE)
5607 * or after the line and column.
5608 */
5609 static int
5610qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5611{
5612 if (linewise)
5613 return qfp->qf_lnum > pos->lnum;
5614 else
5615 return (qfp->qf_lnum > pos->lnum ||
5616 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5617}
5618
5619/*
5620 * Returns TRUE if the specified quickfix entry is
5621 * before the given line (linewise is TRUE)
5622 * or before the line and column.
5623 */
5624 static int
5625qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5626{
5627 if (linewise)
5628 return qfp->qf_lnum < pos->lnum;
5629 else
5630 return (qfp->qf_lnum < pos->lnum ||
5631 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5632}
5633
5634/*
5635 * Returns TRUE if the specified quickfix entry is
5636 * on or after the given line (linewise is TRUE)
5637 * or on or after the line and column.
5638 */
5639 static int
5640qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5641{
5642 if (linewise)
5643 return qfp->qf_lnum >= pos->lnum;
5644 else
5645 return (qfp->qf_lnum > pos->lnum ||
5646 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5647}
5648
5649/*
5650 * Returns TRUE if the specified quickfix entry is
5651 * on or before the given line (linewise is TRUE)
5652 * or on or before the line and column.
5653 */
5654 static int
5655qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5656{
5657 if (linewise)
5658 return qfp->qf_lnum <= pos->lnum;
5659 else
5660 return (qfp->qf_lnum < pos->lnum ||
5661 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5662}
5663
5664/*
5665 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5666 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5667 * multiple entries on a single line as one. Otherwise returns the entry after
5668 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005669 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5670 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005671 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005672 */
5673 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005674qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005675 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005676 pos_T *pos,
5677 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005678 qfline_T *qfp,
5679 int *errornr)
5680{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005681 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005682 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005683 return qfp;
5684
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005685 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005686 while (qfp->qf_next != NULL
5687 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005688 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005689 {
5690 qfp = qfp->qf_next;
5691 ++*errornr;
5692 }
5693
5694 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005695 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005696 return NULL;
5697
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005698 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005699 qfp = qfp->qf_next;
5700 ++*errornr;
5701
5702 return qfp;
5703}
5704
5705/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005706 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5707 * If 'linewise' is TRUE, returns the entry before the specified line and
5708 * treats multiple entries on a single line as one. Otherwise returns the entry
5709 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005710 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5711 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005712 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005713 */
5714 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005715qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005716 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005717 pos_T *pos,
5718 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005719 qfline_T *qfp,
5720 int *errornr)
5721{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005722 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005723 while (qfp->qf_next != NULL
5724 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005725 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005726 {
5727 qfp = qfp->qf_next;
5728 ++*errornr;
5729 }
5730
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005731 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005732 return NULL;
5733
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005734 if (linewise)
5735 // If multiple entries are on the same line, then use the first entry
5736 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005737
5738 return qfp;
5739}
5740
5741/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005742 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005743 * the direction 'dir'.
5744 */
5745 static qfline_T *
5746qf_find_closest_entry(
5747 qf_list_T *qfl,
5748 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005749 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005750 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005751 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005752 int *errornr)
5753{
5754 qfline_T *qfp;
5755
5756 *errornr = 0;
5757
5758 // Find the first entry in this file
5759 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5760 if (qfp == NULL)
5761 return NULL; // no entry in this file
5762
5763 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005764 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005765 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005766 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005767
5768 return qfp;
5769}
5770
5771/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005772 * Get the nth quickfix entry below the specified entry. Searches forward in
5773 * the list. If linewise is TRUE, then treat multiple entries on a single line
5774 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005775 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005776 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005777qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005778{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005779 qfline_T *entry = entry_arg;
5780
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005781 while (n-- > 0 && !got_int)
5782 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005783 int first_errornr = *errornr;
5784
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005785 if (linewise)
5786 // Treat all the entries on the same line in this file as one
5787 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005788
5789 if (entry->qf_next == NULL
5790 || entry->qf_next->qf_fnum != entry->qf_fnum)
5791 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005792 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005793 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005794 break;
5795 }
5796
5797 entry = entry->qf_next;
5798 ++*errornr;
5799 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005800}
5801
5802/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005803 * Get the nth quickfix entry above the specified entry. Searches backwards in
5804 * the list. If linewise is TRUE, then treat multiple entries on a single line
5805 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005806 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005807 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005808qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005809{
5810 while (n-- > 0 && !got_int)
5811 {
5812 if (entry->qf_prev == NULL
5813 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5814 break;
5815
5816 entry = entry->qf_prev;
5817 --*errornr;
5818
5819 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005820 if (linewise)
5821 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005822 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005823}
5824
5825/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005826 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5827 * the specified direction. Returns the error number in the quickfix list or 0
5828 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005829 */
5830 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005831qf_find_nth_adj_entry(
5832 qf_list_T *qfl,
5833 int bnr,
5834 pos_T *pos,
5835 int n,
5836 int dir,
5837 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005838{
5839 qfline_T *adj_entry;
5840 int errornr;
5841
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005842 // Find an entry closest to the specified position
5843 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005844 if (adj_entry == NULL)
5845 return 0;
5846
5847 if (--n > 0)
5848 {
5849 // Go to the n'th entry in the current buffer
5850 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005851 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005852 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005853 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005854 }
5855
5856 return errornr;
5857}
5858
5859/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005860 * Jump to a quickfix entry in the current file nearest to the current line or
5861 * current line/col.
5862 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5863 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005864 */
5865 void
5866ex_cbelow(exarg_T *eap)
5867{
5868 qf_info_T *qi;
5869 qf_list_T *qfl;
5870 int dir;
5871 int buf_has_flag;
5872 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005873 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005874
5875 if (eap->addr_count > 0 && eap->line2 <= 0)
5876 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005877 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005878 return;
5879 }
5880
5881 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005882 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5883 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005884 buf_has_flag = BUF_HAS_QF_ENTRY;
5885 else
5886 buf_has_flag = BUF_HAS_LL_ENTRY;
5887 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5888 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005889 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005890 return;
5891 }
5892
5893 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5894 return;
5895
5896 qfl = qf_get_curlist(qi);
5897 // check if the list has valid errors
5898 if (!qf_list_has_valid_entries(qfl))
5899 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005900 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005901 return;
5902 }
5903
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005904 if (eap->cmdidx == CMD_cbelow
5905 || eap->cmdidx == CMD_lbelow
5906 || eap->cmdidx == CMD_cafter
5907 || eap->cmdidx == CMD_lafter)
5908 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005909 dir = FORWARD;
5910 else
5911 dir = BACKWARD;
5912
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005913 pos = curwin->w_cursor;
5914 // A quickfix entry column number is 1 based whereas cursor column
5915 // number is 0 based. Adjust the column number.
5916 pos.col++;
5917 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5918 eap->addr_count > 0 ? eap->line2 : 0, dir,
5919 eap->cmdidx == CMD_cbelow
5920 || eap->cmdidx == CMD_lbelow
5921 || eap->cmdidx == CMD_cabove
5922 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005923
5924 if (errornr > 0)
5925 qf_jump(qi, 0, errornr, FALSE);
5926 else
5927 emsg(_(e_no_more_items));
5928}
5929
5930/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005931 * Return the autocmd name for the :cfile Ex commands
5932 */
5933 static char_u *
5934cfile_get_auname(cmdidx_T cmdidx)
5935{
5936 switch (cmdidx)
5937 {
5938 case CMD_cfile: return (char_u *)"cfile";
5939 case CMD_cgetfile: return (char_u *)"cgetfile";
5940 case CMD_caddfile: return (char_u *)"caddfile";
5941 case CMD_lfile: return (char_u *)"lfile";
5942 case CMD_lgetfile: return (char_u *)"lgetfile";
5943 case CMD_laddfile: return (char_u *)"laddfile";
5944 default: return NULL;
5945 }
5946}
5947
5948/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005949 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005950 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 */
5952 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005953ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005955 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005956 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005957 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005958 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005959 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005960 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005961
Bram Moolenaara16123a2019-03-28 20:31:07 +01005962 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005963 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5964 NULL, FALSE, curbuf))
5965 {
5966#ifdef FEAT_EVAL
5967 if (aborting())
5968 return;
5969#endif
5970 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005971
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005972 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005973#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005974 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005975 {
5976 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005977 NULL, NULL,
5978 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005979 if (browse_file == NULL)
5980 return;
5981 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5982 vim_free(browse_file);
5983 }
5984 else
5985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005987 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005988
Bram Moolenaar39665952018-08-15 20:59:48 +02005989 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005990 wp = curwin;
5991
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005992 incr_quickfix_busy();
5993
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005994 // This function is used by the :cfile, :cgetfile and :caddfile
5995 // commands.
Colin Kennedy21570352024-03-03 16:16:47 +01005996 // :cfile always creates a new quickfix list and may jump to the
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005997 // first error.
5998 // :cgetfile creates a new quickfix list but doesn't jump to the
5999 // first error.
6000 // :caddfile adds to an existing quickfix list. If there is no
6001 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006002 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006003 && eap->cmdidx != CMD_laddfile),
6004 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006005 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006006 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01006007 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006008 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006009 {
6010 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006011 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006012 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006013 }
6014 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006015 qf_list_changed(qf_get_curlist(qi));
6016 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006017 if (au_name != NULL)
6018 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01006019
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006020 // Jump to the first error for a new list and if autocmds didn't
6021 // free the list.
6022 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
6023 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006024 // display the first error
6025 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006026
6027 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006028}
6029
6030/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006031 * Return the vimgrep autocmd name.
6032 */
6033 static char_u *
6034vgr_get_auname(cmdidx_T cmdidx)
6035{
6036 switch (cmdidx)
6037 {
6038 case CMD_vimgrep: return (char_u *)"vimgrep";
6039 case CMD_lvimgrep: return (char_u *)"lvimgrep";
6040 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
6041 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
6042 case CMD_grep: return (char_u *)"grep";
6043 case CMD_lgrep: return (char_u *)"lgrep";
6044 case CMD_grepadd: return (char_u *)"grepadd";
6045 case CMD_lgrepadd: return (char_u *)"lgrepadd";
6046 default: return NULL;
6047 }
6048}
6049
6050/*
6051 * Initialize the regmatch used by vimgrep for pattern "s".
6052 */
6053 static void
6054vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
6055{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006056 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006057 regmatch->regprog = NULL;
6058
6059 if (s == NULL || *s == NUL)
6060 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006061 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006062 if (last_search_pat() == NULL)
6063 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006064 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006065 return;
6066 }
6067 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
6068 }
6069 else
6070 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
6071
6072 regmatch->rmm_ic = p_ic;
6073 regmatch->rmm_maxcol = 0;
6074}
6075
6076/*
6077 * Display a file name when vimgrep is running.
6078 */
6079 static void
6080vgr_display_fname(char_u *fname)
6081{
6082 char_u *p;
6083
6084 msg_start();
6085 p = msg_strtrunc(fname, TRUE);
6086 if (p == NULL)
6087 msg_outtrans(fname);
6088 else
6089 {
6090 msg_outtrans(p);
6091 vim_free(p);
6092 }
6093 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006094 msg_didout = FALSE; // overwrite this message
6095 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006096 msg_col = 0;
6097 out_flush();
6098}
6099
6100/*
6101 * Load a dummy buffer to search for a pattern using vimgrep.
6102 */
6103 static buf_T *
6104vgr_load_dummy_buf(
6105 char_u *fname,
6106 char_u *dirname_start,
6107 char_u *dirname_now)
6108{
6109 int save_mls;
6110#if defined(FEAT_SYN_HL)
6111 char_u *save_ei = NULL;
6112#endif
6113 buf_T *buf;
6114
6115#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006116 // Don't do Filetype autocommands to avoid loading syntax and
6117 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006118 save_ei = au_event_disable(",Filetype");
6119#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006120 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006121 save_mls = p_mls;
6122 p_mls = 0;
6123
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006124 // Load file into a buffer, so that 'fileencoding' is detected,
6125 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006126 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
6127
6128 p_mls = save_mls;
6129#if defined(FEAT_SYN_HL)
6130 au_event_restore(save_ei);
6131#endif
6132
6133 return buf;
6134}
6135
6136/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006137 * Check whether a quickfix/location list is valid. Autocmds may remove or
6138 * change a quickfix list when vimgrep is running. If the list is not found,
6139 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006140 */
6141 static int
6142vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006143 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006144 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006145 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006146 char_u *title)
6147{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006148 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006149 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006150 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006151 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006152 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006153 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02006154 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006155 return FALSE;
6156 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006157 else
6158 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006159 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006160 qf_new_list(qi, title);
6161 return TRUE;
6162 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006163 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006164
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02006165 if (qf_restore_list(qi, qfid) == FAIL)
6166 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006167
6168 return TRUE;
6169}
6170
6171/*
6172 * Search for a pattern in all the lines in a buffer and add the matching lines
6173 * to a quickfix list.
6174 */
6175 static int
6176vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006177 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006178 char_u *fname,
6179 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006180 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006181 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006182 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006183 int duplicate_name,
6184 int flags)
6185{
6186 int found_match = FALSE;
6187 long lnum;
6188 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006189 int pat_len = (int)STRLEN(spat);
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006190 if (pat_len > MAX_FUZZY_MATCHES)
6191 pat_len = MAX_FUZZY_MATCHES;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006192
Bram Moolenaar1c299432018-10-28 14:36:09 +01006193 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006194 {
6195 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006196 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006197 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006198 // Regular expression match
6199 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006200 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006201 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006202 // Pass the buffer number so that it gets used even for a
6203 // dummy buffer, unless duplicate_name is set, then the
6204 // buffer will be wiped out below.
6205 if (qf_add_entry(qfl,
6206 NULL, // dir
6207 fname,
6208 NULL,
6209 duplicate_name ? 0 : buf->b_fnum,
6210 ml_get_buf(buf,
6211 regmatch->startpos[0].lnum + lnum, FALSE),
6212 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006213 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006214 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006215 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006216 FALSE, // vis_col
6217 NULL, // search pattern
6218 0, // nr
6219 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006220 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006221 TRUE // valid
6222 ) == QF_FAIL)
6223 {
6224 got_int = TRUE;
6225 break;
6226 }
6227 found_match = TRUE;
6228 if (--*tomatch == 0)
6229 break;
6230 if ((flags & VGR_GLOBAL) == 0
6231 || regmatch->endpos[0].lnum > 0)
6232 break;
6233 col = regmatch->endpos[0].col
6234 + (col == regmatch->endpos[0].col);
zeertzjq94b7c322024-03-12 21:50:32 +01006235 if (col > ml_get_buf_len(buf, lnum))
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006236 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006237 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006238 }
6239 else
6240 {
6241 char_u *str = ml_get_buf(buf, lnum, FALSE);
zeertzjq8c55d602024-03-13 20:42:26 +01006242 colnr_T linelen = ml_get_buf_len(buf, lnum);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006243 int score;
6244 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006245 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006246
6247 // Fuzzy string match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006248 CLEAR_FIELD(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006249 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6250 {
6251 // Pass the buffer number so that it gets used even for a
6252 // dummy buffer, unless duplicate_name is set, then the
6253 // buffer will be wiped out below.
6254 if (qf_add_entry(qfl,
6255 NULL, // dir
6256 fname,
6257 NULL,
6258 duplicate_name ? 0 : buf->b_fnum,
6259 str,
6260 lnum,
thinca6864efa2021-06-19 20:45:20 +02006261 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006262 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006263 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006264 FALSE, // vis_col
6265 NULL, // search pattern
6266 0, // nr
6267 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006268 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006269 TRUE // valid
6270 ) == QF_FAIL)
6271 {
6272 got_int = TRUE;
6273 break;
6274 }
6275 found_match = TRUE;
6276 if (--*tomatch == 0)
6277 break;
6278 if ((flags & VGR_GLOBAL) == 0)
6279 break;
6280 col = matches[pat_len - 1] + col + 1;
zeertzjq8c55d602024-03-13 20:42:26 +01006281 if (col > linelen)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006282 break;
6283 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006284 }
6285 line_breakcheck();
6286 if (got_int)
6287 break;
6288 }
6289
6290 return found_match;
6291}
6292
6293/*
6294 * Jump to the first match and update the directory.
6295 */
6296 static void
6297vgr_jump_to_match(
6298 qf_info_T *qi,
6299 int forceit,
6300 int *redraw_for_dummy,
6301 buf_T *first_match_buf,
6302 char_u *target_dir)
6303{
6304 buf_T *buf;
6305
6306 buf = curbuf;
6307 qf_jump(qi, 0, 0, forceit);
6308 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006309 // If we jumped to another buffer redrawing will already be
6310 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006311 *redraw_for_dummy = FALSE;
6312
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006313 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006314 if (curbuf == first_match_buf && target_dir != NULL)
6315 {
6316 exarg_T ea;
6317
Bram Moolenaara80faa82020-04-12 19:37:17 +02006318 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006319 ea.arg = target_dir;
6320 ea.cmdidx = CMD_lcd;
6321 ex_cd(&ea);
6322 }
6323}
6324
6325/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006326 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006327 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006328typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006329{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006330 long tomatch; // maximum number of matches to find
6331 char_u *spat; // search pattern
6332 int flags; // search modifier
6333 char_u **fnames; // list of files to search
6334 int fcount; // number of files
6335 regmmatch_T regmatch; // compiled search pattern
6336 char_u *qf_title; // quickfix list title
6337} vgr_args_T;
6338
6339/*
6340 * Process :vimgrep command arguments. The command syntax is:
6341 *
6342 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6343 */
6344 static int
6345vgr_process_args(
6346 exarg_T *eap,
6347 vgr_args_T *args)
6348{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006349 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006350
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00006351 CLEAR_POINTER(args);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006352
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006353 args->regmatch.regprog = NULL;
6354 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006355
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006356 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006357 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006358 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006359 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006360
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006361 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006362 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006363 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006364 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006365 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006366 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006367 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006368
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006369 vgr_init_regmatch(&args->regmatch, args->spat);
6370 if (args->regmatch.regprog == NULL)
6371 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006372
6373 p = skipwhite(p);
6374 if (*p == NUL)
6375 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006376 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006377 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006378 }
6379
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006380 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006381 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6382 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006383 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006384 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006385 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006386 }
6387
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006388 return OK;
6389}
6390
6391/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006392 * Return TRUE if "buf" had an existing swap file, the current swap file does
6393 * not end in ".swp".
6394 */
6395 static int
6396existing_swapfile(buf_T *buf)
6397{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006398 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006399 {
6400 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6401 size_t len = STRLEN(fname);
6402
6403 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6404 }
6405 return FALSE;
6406}
6407
6408/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006409 * Search for a pattern in a list of files and populate the quickfix list with
6410 * the matches.
6411 */
6412 static int
6413vgr_process_files(
6414 win_T *wp,
6415 qf_info_T *qi,
6416 vgr_args_T *cmd_args,
6417 int *redraw_for_dummy,
6418 buf_T **first_match_buf,
6419 char_u **target_dir)
6420{
6421 int status = FAIL;
6422 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6423 time_t seconds = 0;
6424 char_u *fname;
6425 int fi;
6426 buf_T *buf;
6427 int duplicate_name = FALSE;
6428 int using_dummy;
6429 char_u *dirname_start = NULL;
6430 char_u *dirname_now = NULL;
6431 int found_match;
6432 aco_save_T aco;
6433
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006434 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6435 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006436 if (dirname_start == NULL || dirname_now == NULL)
6437 goto theend;
6438
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006439 // Remember the current directory, because a BufRead autocommand that does
6440 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006441 mch_dirname(dirname_start, MAXPATHL);
6442
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006443 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006444 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6445 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006446 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006447 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006448 if (time(NULL) > seconds)
6449 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006450 // Display the file name every second or so, show the user we are
6451 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006452 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006453 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006454 }
6455
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006456 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006457 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6458 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006459 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006460 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006461 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006462 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006463
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006464 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006465 }
6466 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006467 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006468 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006469
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006470 // Check whether the quickfix list is still valid. When loading a
6471 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006472 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006473 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006474
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006475 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006476
Bram Moolenaar81695252004-12-29 20:58:21 +00006477 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006478 {
6479 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006480 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006481 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006482 else
6483 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006484 // Try for a match in all lines of the buffer.
6485 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006486 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006487 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006488 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006489
Bram Moolenaar81695252004-12-29 20:58:21 +00006490 if (using_dummy)
6491 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006492 if (found_match && *first_match_buf == NULL)
6493 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006494 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006495 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006496 // Never keep a dummy buffer if there is another buffer
6497 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006498 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006499 buf = NULL;
6500 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006501 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006502 || buf->b_p_bh[0] == 'u' // "unload"
6503 || buf->b_p_bh[0] == 'w' // "wipe"
6504 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006505 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006506 // When no match was found we don't need to remember the
6507 // buffer, wipe it out. If there was a match and it
6508 // wasn't the first one or we won't jump there: only
6509 // unload the buffer.
6510 // Ignore 'hidden' here, because it may lead to having too
6511 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006512 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006513 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006514 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006515 buf = NULL;
6516 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006517 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006518 || (cmd_args->flags & VGR_NOJUMP)
6519 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006520 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006521 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006522 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006523 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006524 buf = NULL;
6525 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006526 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006527
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006528 if (buf != NULL)
6529 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006530 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006531 buf->b_flags &= ~BF_DUMMY;
6532
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006533 // If the buffer is still loaded we need to use the
6534 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006535 if (buf == *first_match_buf
6536 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006537 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006538 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006539
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006540 // The buffer is still loaded, the Filetype autocommands
6541 // need to be done now, in that buffer. And the modelines
6542 // need to be done (again). But not the window-local
6543 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006544 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006545 if (curbuf == buf)
6546 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006547#if defined(FEAT_SYN_HL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00006548 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006549 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006550#endif
Bram Moolenaare76062c2022-11-28 18:51:43 +00006551 do_modelines(OPT_NOWIN);
6552 aucmd_restbuf(&aco);
6553 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006554 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006555 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006556 }
6557 }
6558
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006559 status = OK;
6560
6561theend:
6562 vim_free(dirname_now);
6563 vim_free(dirname_start);
6564 return status;
6565}
6566
6567/*
6568 * ":vimgrep {pattern} file(s)"
6569 * ":vimgrepadd {pattern} file(s)"
6570 * ":lvimgrep {pattern} file(s)"
6571 * ":lvimgrepadd {pattern} file(s)"
6572 */
6573 void
6574ex_vimgrep(exarg_T *eap)
6575{
6576 vgr_args_T args;
6577 qf_info_T *qi;
6578 qf_list_T *qfl;
6579 int_u save_qfid;
6580 win_T *wp = NULL;
6581 int redraw_for_dummy = FALSE;
6582 buf_T *first_match_buf = NULL;
6583 char_u *target_dir = NULL;
6584 char_u *au_name = NULL;
6585 int status;
6586
Colin Kennedy21570352024-03-03 16:16:47 +01006587 if (!check_can_set_curbuf_forceit(eap->forceit))
6588 return;
6589
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006590 au_name = vgr_get_auname(eap->cmdidx);
6591 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6592 curbuf->b_fname, TRUE, curbuf))
6593 {
6594#ifdef FEAT_EVAL
6595 if (aborting())
6596 return;
6597#endif
6598 }
6599
6600 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6601 if (qi == NULL)
6602 return;
6603
6604 if (vgr_process_args(eap, &args) == FAIL)
6605 goto theend;
6606
6607 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6608 && eap->cmdidx != CMD_vimgrepadd
6609 && eap->cmdidx != CMD_lvimgrepadd)
6610 || qf_stack_empty(qi))
6611 // make place for a new list
6612 qf_new_list(qi, args.qf_title);
6613
6614 incr_quickfix_busy();
6615
6616 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6617 &first_match_buf, &target_dir);
6618 if (status != OK)
6619 {
6620 FreeWild(args.fcount, args.fnames);
6621 decr_quickfix_busy();
6622 goto theend;
6623 }
6624
6625 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006626
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006627 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006628 qfl->qf_nonevalid = FALSE;
6629 qfl->qf_ptr = qfl->qf_start;
6630 qfl->qf_index = 1;
6631 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006632
Bram Moolenaar864293a2016-06-02 13:40:04 +02006633 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006634
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006635 // Remember the current quickfix list identifier, so that we can check for
6636 // autocommands changing the current quickfix list.
6637 save_qfid = qf_get_curlist(qi)->qf_id;
6638
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006639 if (au_name != NULL)
6640 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6641 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006642 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6643 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006644 if (!qflist_valid(wp, save_qfid)
6645 || qf_restore_list(qi, save_qfid) == FAIL)
6646 {
6647 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006648 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006649 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006650
zeertzjq8c55d602024-03-13 20:42:26 +01006651 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006652 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006653 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006654 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006655 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6656 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006657 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006658 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006659 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006660
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006661 decr_quickfix_busy();
6662
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006663 // If we loaded a dummy buffer into the current window, the autocommands
6664 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006665 if (redraw_for_dummy)
6666 {
6667#ifdef FEAT_FOLDING
6668 foldUpdateAll(curwin);
6669#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006670 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006671#endif
6672 }
6673
Bram Moolenaar86b68352004-12-27 21:59:20 +00006674theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006675 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006676 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006677 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006678}
6679
6680/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006681 * Restore current working directory to "dirname_start" if they differ, taking
6682 * into account whether it is set locally or globally.
6683 */
6684 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006685restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006686{
6687 char_u *dirname_now = alloc(MAXPATHL);
6688
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006689 if (dirname_now == NULL)
6690 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006691
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006692 mch_dirname(dirname_now, MAXPATHL);
6693 if (STRCMP(dirname_start, dirname_now) != 0)
6694 {
6695 // If the directory has changed, change it back by building up an
6696 // appropriate ex command and executing it.
6697 exarg_T ea;
6698
6699 CLEAR_FIELD(ea);
6700 ea.arg = dirname_start;
6701 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6702 ex_cd(&ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006703 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006704 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006705}
6706
6707/*
6708 * Load file "fname" into a dummy buffer and return the buffer pointer,
6709 * placing the directory resulting from the buffer load into the
6710 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6711 * prior to calling this function. Restores directory to "dirname_start" prior
6712 * to returning, if autocmds or the 'autochdir' option have changed it.
6713 *
6714 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6715 * or wipe_dummy_buffer() later!
6716 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006717 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006718 */
6719 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006720load_dummy_buffer(
6721 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006722 char_u *dirname_start, // in: old directory
6723 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006724{
6725 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006726 bufref_T newbufref;
6727 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006728 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006729 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006730 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006731
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006732 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006733 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6734 if (newbuf == NULL)
6735 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006736 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006737
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006738 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006739 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6740
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006741 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006742 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006744 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006745 ++newbuf->b_locked;
6746
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006747 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006748 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006749 if (curbuf == newbuf)
Bram Moolenaar81695252004-12-29 20:58:21 +00006750 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006751 // Need to set the filename for autocommands.
6752 (void)setfname(curbuf, fname, NULL, FALSE);
6753
6754 // Create swap file now to avoid the ATTENTION message.
6755 check_need_swap(TRUE);
6756
6757 // Remove the "dummy" flag, otherwise autocommands may not
6758 // work.
6759 curbuf->b_flags &= ~BF_DUMMY;
6760
6761 newbuf_to_wipe.br_buf = NULL;
6762 readfile_result = readfile(fname, NULL,
6763 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6764 NULL, READ_NEW | READ_DUMMY);
6765 --newbuf->b_locked;
6766 if (readfile_result == OK
6767 && !got_int
6768 && !(curbuf->b_flags & BF_NEW))
Bram Moolenaar81695252004-12-29 20:58:21 +00006769 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006770 failed = FALSE;
6771 if (curbuf != newbuf)
6772 {
6773 // Bloody autocommands changed the buffer! Can happen when
6774 // using netrw and editing a remote file. Use the current
6775 // buffer instead, delete the dummy one after restoring the
6776 // window stuff.
6777 set_bufref(&newbuf_to_wipe, newbuf);
6778 newbuf = curbuf;
6779 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006780 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00006781
6782 // restore curwin/curbuf and a few other things
6783 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00006784
Bram Moolenaar84497cd2022-11-28 20:34:52 +00006785 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6786 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
6787 }
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006788
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006789 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6790 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006791 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006792 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006793
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006794 // When autocommands/'autochdir' option changed directory: go back.
6795 // Let the caller know what the resulting dir was first, in case it is
6796 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006797 mch_dirname(resulting_dir, MAXPATHL);
6798 restore_start_dir(dirname_start);
6799
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006800 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006801 return NULL;
6802 if (failed)
6803 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006804 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006805 return NULL;
6806 }
6807 return newbuf;
6808}
6809
6810/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006811 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6812 * directory to "dirname_start" prior to returning, if autocmds or the
6813 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006814 */
6815 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006816wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006817{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006818 // If any autocommand opened a window on the dummy buffer, close that
6819 // window. If we can't close them all then give up.
6820 while (buf->b_nwindows > 0)
6821 {
6822 int did_one = FALSE;
6823 win_T *wp;
6824
6825 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006826 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006827 if (wp->w_buffer == buf)
6828 {
6829 if (win_close(wp, FALSE) == OK)
6830 did_one = TRUE;
6831 break;
6832 }
6833 if (!did_one)
6834 return;
6835 }
6836
6837 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006838 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006839#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006840 cleanup_T cs;
6841
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006842 // Reset the error/interrupt/exception state here so that aborting()
6843 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6844 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006845 enter_cleanup(&cs);
6846#endif
6847
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006848 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006849
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006850#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006851 // Restore the error/interrupt/exception state if not discarded by a
6852 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006853 leave_cleanup(&cs);
6854#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006855 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006856 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006857 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006858}
6859
6860/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006861 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6862 * directory to "dirname_start" prior to returning, if autocmds or the
6863 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006864 */
6865 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006866unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006867{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006868 if (curbuf == buf) // safety check
6869 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006870
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006871 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
6872
6873 // When autocommands/'autochdir' option changed directory: go back.
6874 restore_start_dir(dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006875}
6876
Bram Moolenaar05159a02005-02-26 23:04:13 +00006877#if defined(FEAT_EVAL) || defined(PROTO)
6878/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006879 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006880 * to 'list'. Returns OK on success.
6881 */
6882 static int
6883get_qfline_items(qfline_T *qfp, list_T *list)
6884{
6885 int bufnum;
6886 dict_T *dict;
6887 char_u buf[2];
6888
6889 // Handle entries with a non-existing buffer number.
6890 bufnum = qfp->qf_fnum;
6891 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6892 bufnum = 0;
6893
6894 if ((dict = dict_alloc()) == NULL)
6895 return FAIL;
6896 if (list_append_dict(list, dict) == FAIL)
6897 return FAIL;
6898
6899 buf[0] = qfp->qf_type;
6900 buf[1] = NUL;
6901 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006902 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6903 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6904 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6905 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6906 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6907 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006908 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6909 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6910 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6911 || dict_add_string(dict, "type", buf) == FAIL
Tom Praschanca6ac992023-08-11 23:26:12 +02006912 || (qfp->qf_user_data.v_type != VAR_UNKNOWN
6913 && dict_add_tv(dict, "user_data", &qfp->qf_user_data) == FAIL )
Bram Moolenaara16123a2019-03-28 20:31:07 +01006914 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6915 return FAIL;
6916
6917 return OK;
6918}
6919
6920/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006921 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006922 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006923 * If eidx is not 0, then return only the specified entry. Otherwise return
6924 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006925 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006926 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006927get_errorlist(
6928 qf_info_T *qi_arg,
6929 win_T *wp,
6930 int qf_idx,
6931 int eidx,
6932 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006933{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006934 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006935 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006936 qfline_T *qfp;
6937 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006938
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006939 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006940 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006941 qi = &ql_info;
6942 if (wp != NULL)
6943 {
6944 qi = GET_LOC_LIST(wp);
6945 if (qi == NULL)
6946 return FAIL;
6947 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006948 }
6949
Bram Moolenaar858ba062020-05-31 23:11:59 +02006950 if (eidx < 0)
6951 return OK;
6952
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006953 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006954 qf_idx = qi->qf_curlist;
6955
Bram Moolenaar0398e002019-03-21 21:12:49 +01006956 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006957 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006958
Bram Moolenaar0398e002019-03-21 21:12:49 +01006959 qfl = qf_get_list(qi, qf_idx);
6960 if (qf_list_empty(qfl))
6961 return FAIL;
6962
Bram Moolenaara16123a2019-03-28 20:31:07 +01006963 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006964 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006965 if (eidx > 0)
6966 {
6967 if (eidx == i)
6968 return get_qfline_items(qfp, list);
6969 }
6970 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006971 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006972 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006973
Bram Moolenaar05159a02005-02-26 23:04:13 +00006974 return OK;
6975}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006976
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006977// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006978enum {
6979 QF_GETLIST_NONE = 0x0,
6980 QF_GETLIST_TITLE = 0x1,
6981 QF_GETLIST_ITEMS = 0x2,
6982 QF_GETLIST_NR = 0x4,
6983 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006984 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006985 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006986 QF_GETLIST_IDX = 0x40,
6987 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006988 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006989 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006990 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006991 QF_GETLIST_QFTF = 0x800,
6992 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006993};
6994
6995/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006996 * Parse text from 'di' and return the quickfix list items.
6997 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006998 */
6999 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02007000qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007001{
7002 int status = FAIL;
7003 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02007004 char_u *errorformat = p_efm;
7005 dictitem_T *efm_di;
7006 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007007
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007008 // Only a List value is supported
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007009 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7010 return FAIL;
7011
7012 // If errorformat is supplied then use it, otherwise use the 'efm'
7013 // option setting
7014 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007015 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007016 if (efm_di->di_tv.v_type != VAR_STRING ||
7017 efm_di->di_tv.vval.v_string == NULL)
Bram Moolenaarda732532017-08-31 20:58:02 +02007018 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007019 errorformat = efm_di->di_tv.vval.v_string;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007020 }
7021
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007022 l = list_alloc();
7023 if (l == NULL)
7024 return FAIL;
7025
7026 qi = qf_alloc_stack(QFLT_INTERNAL);
7027 if (qi != NULL)
7028 {
7029 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
7030 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7031 {
7032 (void)get_errorlist(qi, NULL, 0, 0, l);
7033 qf_free(&qi->qf_lists[0]);
7034 }
7035 free(qi);
7036 }
7037 dict_add_list(retdict, "items", l);
7038 status = OK;
7039
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007040 return status;
7041}
7042
7043/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007044 * Return the quickfix/location list window identifier in the current tabpage.
7045 */
7046 static int
7047qf_winid(qf_info_T *qi)
7048{
7049 win_T *win;
7050
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007051 // The quickfix window can be opened even if the quickfix list is not set
7052 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007053 if (qi == NULL)
7054 return 0;
7055 win = qf_find_win(qi);
7056 if (win != NULL)
7057 return win->w_id;
7058 return 0;
7059}
7060
7061/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007062 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007063 * window. If there is no buffer associated with the list or the buffer is
7064 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007065 */
7066 static int
7067qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
7068{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007069 int bufnum = 0;
7070
7071 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
7072 bufnum = qi->qf_bufnr;
7073
7074 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007075}
7076
7077/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007078 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007079 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007080 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007081qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007082{
Bram Moolenaard823fa92016-08-12 16:29:27 +02007083 int flags = QF_GETLIST_NONE;
7084
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007085 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007086 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007087 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007088 if (!loclist)
7089 // File window ID is applicable only to location list windows
7090 flags &= ~ QF_GETLIST_FILEWINID;
7091 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007092
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007093 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007094 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007095
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007096 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007097 flags |= QF_GETLIST_NR;
7098
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007099 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007100 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007101
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007102 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007103 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007104
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007105 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007106 flags |= QF_GETLIST_ID;
7107
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007108 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007109 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007110
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007111 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007112 flags |= QF_GETLIST_IDX;
7113
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007114 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007115 flags |= QF_GETLIST_SIZE;
7116
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007117 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01007118 flags |= QF_GETLIST_TICK;
7119
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007120 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007121 flags |= QF_GETLIST_FILEWINID;
7122
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007123 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007124 flags |= QF_GETLIST_QFBUFNR;
7125
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007126 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02007127 flags |= QF_GETLIST_QFTF;
7128
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007129 return flags;
7130}
Bram Moolenaara6d48492017-12-12 22:45:31 +01007131
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007132/*
7133 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
7134 * If 'nr' and 'id' are not present in 'what' then return the current
7135 * quickfix list index.
7136 * If 'nr' is zero then return the current quickfix list index.
7137 * If 'nr' is '$' then return the last quickfix list index.
7138 * If 'id' is present then return the index of the quickfix list with that id.
7139 * If 'id' is zero then return the quickfix list index specified by 'nr'.
7140 * Return -1, if quickfix list is not present or if the stack is empty.
7141 */
7142 static int
7143qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
7144{
7145 int qf_idx;
7146 dictitem_T *di;
7147
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007148 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007149 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7150 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007151 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007152 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007153 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007154 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007155 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007156 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007157 qf_idx = di->di_tv.vval.v_number - 1;
7158 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007159 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007160 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01007161 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007162 else if (di->di_tv.v_type == VAR_STRING
7163 && di->di_tv.vval.v_string != NULL
7164 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007165 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007166 qf_idx = qi->qf_listcount - 1;
7167 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007168 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007169 }
7170
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007171 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
7172 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007173 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007174 if (di->di_tv.v_type == VAR_NUMBER)
7175 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007176 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007177 if (di->di_tv.vval.v_number != 0)
7178 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
7179 }
7180 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007181 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007182 }
7183
7184 return qf_idx;
7185}
7186
7187/*
7188 * Return default values for quickfix list properties in retdict.
7189 */
7190 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007191qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007192{
7193 int status = OK;
7194
7195 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007196 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007197 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7198 {
7199 list_T *l = list_alloc();
7200 if (l != NULL)
7201 status = dict_add_list(retdict, "items", l);
7202 else
7203 status = FAIL;
7204 }
7205 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007206 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007207 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007208 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007209 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007210 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007211 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007212 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007213 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007214 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007215 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007216 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007217 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007218 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007219 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007220 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007221 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7222 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007223 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7224 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007225
7226 return status;
7227}
7228
7229/*
7230 * Return the quickfix list title as 'title' in retdict
7231 */
7232 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007233qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007234{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007235 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007236}
7237
7238/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007239 * Returns the identifier of the window used to display files from a location
7240 * list. If there is no associated window, then returns 0. Useful only when
7241 * called from a location list window.
7242 */
7243 static int
7244qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7245{
7246 int winid = 0;
7247
7248 if (wp != NULL && IS_LL_WINDOW(wp))
7249 {
7250 win_T *ll_wp = qf_find_win_with_loclist(qi);
7251 if (ll_wp != NULL)
7252 winid = ll_wp->w_id;
7253 }
7254
7255 return dict_add_number(retdict, "filewinid", winid);
7256}
7257
7258/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007259 * Return the quickfix list items/entries as 'items' in retdict.
7260 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007261 */
7262 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007263qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007264{
7265 int status = OK;
7266 list_T *l = list_alloc();
7267 if (l != NULL)
7268 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007269 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007270 dict_add_list(retdict, "items", l);
7271 }
7272 else
7273 status = FAIL;
7274
7275 return status;
7276}
7277
7278/*
7279 * Return the quickfix list context (if any) as 'context' in retdict.
7280 */
7281 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007282qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007283{
7284 int status;
7285 dictitem_T *di;
7286
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007287 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007288 {
7289 di = dictitem_alloc((char_u *)"context");
7290 if (di != NULL)
7291 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007292 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007293 status = dict_add(retdict, di);
7294 if (status == FAIL)
7295 dictitem_free(di);
7296 }
7297 else
7298 status = FAIL;
7299 }
7300 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007301 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007302
7303 return status;
7304}
7305
7306/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007307 * Return the current quickfix list index as 'idx' in retdict.
7308 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007309 */
7310 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007311qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007312{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007313 if (eidx == 0)
7314 {
7315 eidx = qfl->qf_index;
7316 if (qf_list_empty(qfl))
7317 // For empty lists, current index is set to 0
7318 eidx = 0;
7319 }
7320 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007321}
7322
7323/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007324 * Return the 'quickfixtextfunc' function of a quickfix/location list
7325 */
7326 static int
7327qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7328{
7329 int status;
7330
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007331 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007332 {
7333 typval_T tv;
7334
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007335 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007336 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7337 clear_tv(&tv);
7338 }
7339 else
7340 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7341
7342 return status;
7343}
7344
7345/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007346 * Return quickfix/location list details (title) as a
7347 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7348 * then current list is used. Otherwise the specified list is used.
7349 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007350 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007351qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7352{
7353 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007354 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007355 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007356 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007357 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007358 dictitem_T *di;
7359 int flags = QF_GETLIST_NONE;
7360
7361 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7362 return qf_get_list_from_lines(what, di, retdict);
7363
7364 if (wp != NULL)
7365 qi = GET_LOC_LIST(wp);
7366
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007367 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007368
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007369 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007370 qf_idx = qf_getprop_qfidx(qi, what);
7371
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007372 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007373 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007374 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007375
Bram Moolenaar0398e002019-03-21 21:12:49 +01007376 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007377
Bram Moolenaar858ba062020-05-31 23:11:59 +02007378 // If an entry index is specified, use that
7379 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7380 {
7381 if (di->di_tv.v_type != VAR_NUMBER)
7382 return FAIL;
7383 eidx = di->di_tv.vval.v_number;
7384 }
7385
Bram Moolenaard823fa92016-08-12 16:29:27 +02007386 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007387 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007388 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007389 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007390 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007391 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007392 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007393 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007394 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007395 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007396 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007397 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007398 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007399 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007400 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007401 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007402 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007403 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007404 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7405 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007406 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7407 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007408 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7409 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007410
Bram Moolenaard823fa92016-08-12 16:29:27 +02007411 return status;
7412}
7413
7414/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007415 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007416 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7417 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007418 */
7419 static int
7420qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007421 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007422 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007423 int first_entry,
7424 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007425{
7426 static int did_bufnr_emsg;
7427 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007428 int bufnum, valid, status, col, end_col, vcol, nr;
7429 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007430
7431 if (first_entry)
7432 did_bufnr_emsg = FALSE;
7433
Bram Moolenaard61efa52022-07-23 09:52:04 +01007434 filename = dict_get_string(d, "filename", TRUE);
7435 module = dict_get_string(d, "module", TRUE);
7436 bufnum = (int)dict_get_number(d, "bufnr");
7437 lnum = (int)dict_get_number(d, "lnum");
7438 end_lnum = (int)dict_get_number(d, "end_lnum");
7439 col = (int)dict_get_number(d, "col");
7440 end_col = (int)dict_get_number(d, "end_col");
7441 vcol = (int)dict_get_number(d, "vcol");
7442 nr = (int)dict_get_number(d, "nr");
7443 type = dict_get_string(d, "type", TRUE);
7444 pattern = dict_get_string(d, "pattern", TRUE);
7445 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007446 if (text == NULL)
7447 text = vim_strsave((char_u *)"");
Tom Praschanca6ac992023-08-11 23:26:12 +02007448 typval_T user_data;
7449 user_data.v_type = VAR_UNKNOWN;
7450 dict_get_tv(d, "user_data", &user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007451
7452 valid = TRUE;
7453 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7454 valid = FALSE;
7455
7456 // Mark entries with non-existing buffer number as not valid. Give the
7457 // error message only once.
7458 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7459 {
7460 if (!did_bufnr_emsg)
7461 {
7462 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007463 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007464 }
7465 valid = FALSE;
7466 bufnum = 0;
7467 }
7468
7469 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007470 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007471 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007472
Bram Moolenaar0398e002019-03-21 21:12:49 +01007473 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007474 NULL, // dir
7475 filename,
7476 module,
7477 bufnum,
7478 text,
7479 lnum,
thinca6864efa2021-06-19 20:45:20 +02007480 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007481 col,
thinca6864efa2021-06-19 20:45:20 +02007482 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007483 vcol, // vis_col
7484 pattern, // search pattern
7485 nr,
7486 type == NULL ? NUL : *type,
Tom Praschanca6ac992023-08-11 23:26:12 +02007487 &user_data,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007488 valid);
7489
7490 vim_free(filename);
7491 vim_free(module);
7492 vim_free(pattern);
7493 vim_free(text);
7494 vim_free(type);
Tom Praschanca6ac992023-08-11 23:26:12 +02007495 clear_tv(&user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007496
Bram Moolenaar9752c722018-12-22 16:49:34 +01007497 if (valid)
7498 *valid_entry = TRUE;
7499
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007500 return status;
7501}
7502
7503/*
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007504 * Check if `entry` is closer to the target than `other_entry`.
7505 *
7506 * Only returns TRUE if `entry` is definitively closer. If it's further
7507 * away, or there's not enough information to tell, return FALSE.
7508 */
7509 static int
7510entry_is_closer_to_target(
7511 qfline_T *entry,
7512 qfline_T *other_entry,
7513 int target_fnum,
7514 int target_lnum,
7515 int target_col)
7516{
7517 // First, compare entries to target file.
7518 if (!target_fnum)
7519 // Without a target file, we can't know which is closer.
7520 return FALSE;
7521
7522 int is_target_file = entry->qf_fnum && entry->qf_fnum == target_fnum;
7523 int other_is_target_file = other_entry->qf_fnum && other_entry->qf_fnum == target_fnum;
7524 if (!is_target_file && other_is_target_file)
7525 return FALSE;
7526 else if (is_target_file && !other_is_target_file)
7527 return TRUE;
7528
7529 // Both entries are pointing at the exact same file. Now compare line
7530 // numbers.
7531 if (!target_lnum)
7532 // Without a target line number, we can't know which is closer.
7533 return FALSE;
7534
7535 int line_distance = entry->qf_lnum ? labs(entry->qf_lnum - target_lnum) : INT_MAX;
7536 int other_line_distance = other_entry->qf_lnum ? labs(other_entry->qf_lnum - target_lnum) : INT_MAX;
7537 if (line_distance > other_line_distance)
7538 return FALSE;
7539 else if (line_distance < other_line_distance)
7540 return TRUE;
7541
7542 // Both entries are pointing at the exact same line number (or no line
7543 // number at all). Now compare columns.
7544 if (!target_col)
7545 // Without a target column, we can't know which is closer.
7546 return FALSE;
7547
7548 int column_distance = entry->qf_col ? abs(entry->qf_col - target_col) : INT_MAX;
7549 int other_column_distance = other_entry->qf_col ? abs(other_entry->qf_col - target_col): INT_MAX;
7550 if (column_distance > other_column_distance)
7551 return FALSE;
7552 else if (column_distance < other_column_distance)
7553 return TRUE;
7554
7555 // It's a complete tie! The exact same file, line, and column.
7556 return FALSE;
7557}
7558
7559/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007560 * Add list of entries to quickfix/location list. Each list entry is
7561 * a dictionary with item information.
7562 */
7563 static int
7564qf_add_entries(
7565 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007566 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007567 list_T *list,
7568 char_u *title,
7569 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007570{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007571 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007572 listitem_T *li;
7573 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007574 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007575 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007576 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007577
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007578 // If there's an entry selected in the quickfix list, remember its location
7579 // (file, line, column), so we can select the nearest entry in the updated
7580 // quickfix list.
7581 int prev_fnum = 0;
7582 int prev_lnum = 0;
7583 int prev_col = 0;
7584 if (qfl->qf_ptr)
7585 {
7586 prev_fnum = qfl->qf_ptr->qf_fnum;
7587 prev_lnum = qfl->qf_ptr->qf_lnum;
7588 prev_col = qfl->qf_ptr->qf_col;
7589 }
7590
7591 int select_first_entry = FALSE;
7592 int select_nearest_entry = FALSE;
7593
Bram Moolenaara3921f42017-06-04 15:30:34 +02007594 if (action == ' ' || qf_idx == qi->qf_listcount)
7595 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007596 select_first_entry = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007597 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007598 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007599 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007600 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007601 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007602 else if (action == 'a')
7603 {
7604 if (qf_list_empty(qfl))
7605 // Appending to empty list, select first entry.
7606 select_first_entry = TRUE;
7607 else
7608 // Adding to existing list, use last entry.
7609 old_last = qfl->qf_last;
7610 }
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007611 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007612 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007613 select_first_entry = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007614 qf_free_items(qfl);
7615 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007616 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007617 else if (action == 'u')
7618 {
7619 select_nearest_entry = TRUE;
7620 qf_free_items(qfl);
7621 qf_store_title(qfl, title);
7622 }
7623
7624 qfline_T *entry_to_select = NULL;
7625 int entry_to_select_index = 0;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007626
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007627 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007628 {
7629 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007630 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007631
7632 d = li->li_tv.vval.v_dict;
7633 if (d == NULL)
7634 continue;
7635
Bram Moolenaar0398e002019-03-21 21:12:49 +01007636 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007637 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007638 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007639 break;
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007640
7641 qfline_T *entry = qfl->qf_last;
7642 if (
7643 (select_first_entry && entry_to_select == NULL) ||
7644 (select_nearest_entry &&
7645 (entry_to_select == NULL ||
7646 entry_is_closer_to_target(entry, entry_to_select, prev_fnum,
7647 prev_lnum, prev_col))))
7648 {
7649 entry_to_select = entry;
7650 entry_to_select_index = qfl->qf_count;
7651 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007652 }
7653
Bram Moolenaar9752c722018-12-22 16:49:34 +01007654 // Check if any valid error entries are added to the list.
7655 if (valid_entry)
7656 qfl->qf_nonevalid = FALSE;
7657 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007658 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007659 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007660
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007661 // Set the current error.
7662 if (entry_to_select)
7663 {
7664 qfl->qf_ptr = entry_to_select;
7665 qfl->qf_index = entry_to_select_index;
7666 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007668 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007669 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007670
7671 return retval;
7672}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007673
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007674/*
7675 * Get the quickfix list index from 'nr' or 'id'
7676 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007677 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007678qf_setprop_get_qfidx(
7679 qf_info_T *qi,
7680 dict_T *what,
7681 int action,
7682 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007683{
7684 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007685 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007686
Bram Moolenaard823fa92016-08-12 16:29:27 +02007687 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7688 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007689 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007690 if (di->di_tv.v_type == VAR_NUMBER)
7691 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007692 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007693 if (di->di_tv.vval.v_number != 0)
7694 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007695
Bram Moolenaar55b69262017-08-13 13:42:01 +02007696 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7697 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007698 // When creating a new list, accept qf_idx pointing to the next
7699 // non-available list and add the new list at the end of the
7700 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007701 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007702 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007703 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007704 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007705 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007706 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007707 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007708 }
7709 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007710 && di->di_tv.vval.v_string != NULL
7711 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007712 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007713 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007714 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007715 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007716 qf_idx = 0;
7717 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007718 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007719 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007720 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007721 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007722 }
7723
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007724 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007725 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007726 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007727 if (di->di_tv.v_type != VAR_NUMBER)
7728 return INVALID_QFIDX;
7729
7730 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007731 }
7732
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007733 return qf_idx;
7734}
7735
7736/*
7737 * Set the quickfix list title.
7738 */
7739 static int
7740qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7741{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007742 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007743
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007744 if (di->di_tv.v_type != VAR_STRING)
7745 return FAIL;
7746
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007747 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007748 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007749 if (qf_idx == qi->qf_curlist)
7750 qf_update_win_titlevar(qi);
7751
7752 return OK;
7753}
7754
7755/*
7756 * Set quickfix list items/entries.
7757 */
7758 static int
7759qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7760{
7761 int retval = FAIL;
7762 char_u *title_save;
7763
7764 if (di->di_tv.v_type != VAR_LIST)
7765 return FAIL;
7766
7767 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7768 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7769 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007770 vim_free(title_save);
7771
7772 return retval;
7773}
7774
7775/*
7776 * Set quickfix list items/entries from a list of lines.
7777 */
7778 static int
7779qf_setprop_items_from_lines(
7780 qf_info_T *qi,
7781 int qf_idx,
7782 dict_T *what,
7783 dictitem_T *di,
7784 int action)
7785{
7786 char_u *errorformat = p_efm;
7787 dictitem_T *efm_di;
7788 int retval = FAIL;
7789
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007790 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007791 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7792 {
7793 if (efm_di->di_tv.v_type != VAR_STRING ||
7794 efm_di->di_tv.vval.v_string == NULL)
7795 return FAIL;
7796 errorformat = efm_di->di_tv.vval.v_string;
7797 }
7798
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007799 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007800 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7801 return FAIL;
7802
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007803 if (action == 'r' || action == 'u')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007804 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007805 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007806 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007807 retval = OK;
7808
7809 return retval;
7810}
7811
7812/*
7813 * Set quickfix list context.
7814 */
7815 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007816qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007817{
7818 typval_T *ctx;
7819
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007820 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007821 ctx = alloc_tv();
7822 if (ctx != NULL)
7823 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007824 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007825
7826 return OK;
7827}
7828
7829/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007830 * Set the current index in the specified quickfix list
7831 */
7832 static int
7833qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7834{
7835 int denote = FALSE;
7836 int newidx;
7837 int old_qfidx;
7838 qfline_T *qf_ptr;
7839
7840 // If the specified index is '$', then use the last entry
7841 if (di->di_tv.v_type == VAR_STRING
7842 && di->di_tv.vval.v_string != NULL
7843 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7844 newidx = qfl->qf_count;
7845 else
7846 {
7847 // Otherwise use the specified index
7848 newidx = tv_get_number_chk(&di->di_tv, &denote);
7849 if (denote)
7850 return FAIL;
7851 }
7852
7853 if (newidx < 1) // sanity check
7854 return FAIL;
7855 if (newidx > qfl->qf_count)
7856 newidx = qfl->qf_count;
7857
7858 old_qfidx = qfl->qf_index;
7859 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7860 if (qf_ptr == NULL)
7861 return FAIL;
7862 qfl->qf_ptr = qf_ptr;
7863 qfl->qf_index = newidx;
7864
7865 // If the current list is modified and it is displayed in the quickfix
7866 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007867 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007868 qf_win_pos_update(qi, old_qfidx);
7869
7870 return OK;
7871}
7872
7873/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007874 * Set the current index in the specified quickfix list
7875 */
7876 static int
7877qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7878{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007879 callback_T cb;
7880
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007881 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007882 cb = get_callback(&di->di_tv);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007883 if (cb.cb_name == NULL || *cb.cb_name == NUL)
7884 return OK;
7885
7886 set_callback(&qfl->qf_qftf_cb, &cb);
7887 if (cb.cb_free_name)
7888 vim_free(cb.cb_name);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007889
7890 return OK;
7891}
7892
7893/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007894 * Set quickfix/location list properties (title, items, context).
7895 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007896 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007897 */
7898 static int
7899qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7900{
7901 dictitem_T *di;
7902 int retval = FAIL;
7903 int qf_idx;
7904 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007905 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007906
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007907 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007908 newlist = TRUE;
7909
7910 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007911 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007912 return FAIL;
7913
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007914 if (newlist)
7915 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007916 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007917 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007918 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007919 }
7920
Bram Moolenaar0398e002019-03-21 21:12:49 +01007921 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007922 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007923 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007924 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007925 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007926 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007927 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007928 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007929 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007930 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7931 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007932 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7933 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007934
Bram Moolenaar287153c2020-11-29 14:20:27 +01007935 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007936 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007937 if (newlist)
7938 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007939
Bram Moolenaard823fa92016-08-12 16:29:27 +02007940 return retval;
7941}
7942
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007943/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007944 * Free the entire quickfix/location list stack.
7945 * If the quickfix/location list window is open, then clear it.
7946 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007947 static void
7948qf_free_stack(win_T *wp, qf_info_T *qi)
7949{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007950 win_T *qfwin = qf_find_win(qi);
7951 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007952
7953 if (qfwin != NULL)
7954 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007955 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007956 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007957 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007958 qf_update_buffer(qi, NULL);
7959 }
7960
7961 if (wp != NULL && IS_LL_WINDOW(wp))
7962 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007963 // If in the location list window, then use the non-location list
7964 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007965 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007966 if (llwin != NULL)
7967 wp = llwin;
7968 }
7969
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007970 qf_free_all(wp);
7971 if (wp == NULL)
7972 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007973 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007974 qi->qf_curlist = 0;
7975 qi->qf_listcount = 0;
7976 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007977 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007978 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007979 // If the location list window is open, then create a new empty
7980 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007981 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007982
Bram Moolenaar95946f12019-03-31 15:31:59 +02007983 if (new_ll != NULL)
7984 {
7985 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007986
Bram Moolenaar95946f12019-03-31 15:31:59 +02007987 // first free the list reference in the location list window
7988 ll_free_all(&qfwin->w_llist_ref);
7989
7990 qfwin->w_llist_ref = new_ll;
7991 if (wp != qfwin)
7992 win_set_loclist(wp, new_ll);
7993 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007994 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007995}
7996
Bram Moolenaard823fa92016-08-12 16:29:27 +02007997/*
7998 * Populate the quickfix list with the items supplied in the list
7999 * of dictionaries. "title" will be copied to w:quickfix_title.
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008000 * "action" is 'a' for add, 'r' for replace, 'u' for update. Otherwise
8001 * create a new list. When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02008002 */
8003 int
8004set_errorlist(
8005 win_T *wp,
8006 list_T *list,
8007 int action,
8008 char_u *title,
8009 dict_T *what)
8010{
8011 qf_info_T *qi = &ql_info;
8012 int retval = OK;
8013
8014 if (wp != NULL)
8015 {
8016 qi = ll_get_or_alloc_list(wp);
8017 if (qi == NULL)
8018 return FAIL;
8019 }
8020
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008021 if (action == 'f')
8022 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008023 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008024 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008025 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008026 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008027
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008028 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02008029 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008030 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008031 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008032 _("cannot have both a list and a \"what\" argument"));
8033 return FAIL;
8034 }
8035
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008036 incr_quickfix_busy();
8037
8038 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02008039 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02008040 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01008041 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02008042 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01008043 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008044 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01008045 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02008046
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008047 decr_quickfix_busy();
8048
Bram Moolenaard823fa92016-08-12 16:29:27 +02008049 return retval;
8050}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008051
Tom Praschanca6ac992023-08-11 23:26:12 +02008052static int mark_quickfix_user_data(qf_info_T *qi, int copyID)
8053{
8054 int abort = FALSE;
8055 for (int i = 0; i < LISTCOUNT && !abort; ++i)
8056 {
8057 qf_list_T *qfl = &qi->qf_lists[i];
8058 if (!qfl->qf_has_user_data)
8059 continue;
8060 qfline_T *qfp;
8061 int j;
8062 FOR_ALL_QFL_ITEMS(qfl, qfp, j)
8063 {
8064 typval_T* user_data = &qfp->qf_user_data;
8065 if (user_data != NULL && user_data->v_type != VAR_NUMBER
8066 && user_data->v_type != VAR_STRING && user_data->v_type != VAR_FLOAT)
8067 abort = abort || set_ref_in_item(user_data, copyID, NULL, NULL);
8068 }
8069 }
8070 return abort;
8071}
8072
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008073/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008074 * Mark the quickfix context and callback function as in use for all the lists
8075 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008076 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008077 static int
8078mark_quickfix_ctx(qf_info_T *qi, int copyID)
8079{
8080 int i;
8081 int abort = FALSE;
8082 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008083 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008084
8085 for (i = 0; i < LISTCOUNT && !abort; ++i)
8086 {
8087 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02008088 if (ctx != NULL && ctx->v_type != VAR_NUMBER
8089 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008090 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
8091
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01008092 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008093 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008094 }
8095
8096 return abort;
8097}
8098
8099/*
8100 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02008101 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008102 */
8103 int
8104set_ref_in_quickfix(int copyID)
8105{
8106 int abort = FALSE;
8107 tabpage_T *tp;
8108 win_T *win;
8109
8110 abort = mark_quickfix_ctx(&ql_info, copyID);
8111 if (abort)
8112 return abort;
8113
Tom Praschanca6ac992023-08-11 23:26:12 +02008114 abort = mark_quickfix_user_data(&ql_info, copyID);
8115 if (abort)
8116 return abort;
8117
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008118 abort = set_ref_in_callback(&qftf_cb, copyID);
8119 if (abort)
8120 return abort;
8121
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008122 FOR_ALL_TAB_WINDOWS(tp, win)
8123 {
8124 if (win->w_llist != NULL)
8125 {
8126 abort = mark_quickfix_ctx(win->w_llist, copyID);
8127 if (abort)
8128 return abort;
Tom Praschanca6ac992023-08-11 23:26:12 +02008129
8130 abort = mark_quickfix_user_data(win->w_llist, copyID);
8131 if (abort)
8132 return abort;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008133 }
Bram Moolenaar12237442017-12-19 12:38:52 +01008134 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
8135 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008136 // In a location list window and none of the other windows is
8137 // referring to this location list. Mark the location list
8138 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01008139 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
8140 if (abort)
8141 return abort;
zeertzjqbe4bd182024-09-14 10:32:31 +02008142
8143 abort = mark_quickfix_user_data(win->w_llist_ref, copyID);
8144 if (abort)
8145 return abort;
Bram Moolenaar12237442017-12-19 12:38:52 +01008146 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008147 }
8148
8149 return abort;
8150}
Bram Moolenaar05159a02005-02-26 23:04:13 +00008151#endif
8152
Bram Moolenaar81695252004-12-29 20:58:21 +00008153/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008154 * Return the autocmd name for the :cbuffer Ex commands
8155 */
8156 static char_u *
8157cbuffer_get_auname(cmdidx_T cmdidx)
8158{
8159 switch (cmdidx)
8160 {
8161 case CMD_cbuffer: return (char_u *)"cbuffer";
8162 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
8163 case CMD_caddbuffer: return (char_u *)"caddbuffer";
8164 case CMD_lbuffer: return (char_u *)"lbuffer";
8165 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
8166 case CMD_laddbuffer: return (char_u *)"laddbuffer";
8167 default: return NULL;
8168 }
8169}
8170
8171/*
8172 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
8173 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
8174 */
8175 static int
8176cbuffer_process_args(
8177 exarg_T *eap,
8178 buf_T **bufp,
8179 linenr_T *line1,
8180 linenr_T *line2)
8181{
8182 buf_T *buf = NULL;
8183
8184 if (*eap->arg == NUL)
8185 buf = curbuf;
8186 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
8187 buf = buflist_findnr(atoi((char *)eap->arg));
8188
8189 if (buf == NULL)
8190 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008191 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008192 return FAIL;
8193 }
8194
8195 if (buf->b_ml.ml_mfp == NULL)
8196 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008197 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008198 return FAIL;
8199 }
8200
8201 if (eap->addr_count == 0)
8202 {
8203 eap->line1 = 1;
8204 eap->line2 = buf->b_ml.ml_line_count;
8205 }
8206
8207 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
8208 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
8209 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008210 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008211 return FAIL;
8212 }
8213
8214 *line1 = eap->line1;
8215 *line2 = eap->line2;
8216 *bufp = buf;
8217
8218 return OK;
8219}
8220
8221/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00008222 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008223 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008224 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008225 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008226 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008227 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008228 */
8229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008230ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008231{
8232 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008233 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008234 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01008235 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02008236 int_u save_qfid;
8237 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01008238 char_u *qf_title;
8239 linenr_T line1;
8240 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008241
Bram Moolenaara16123a2019-03-28 20:31:07 +01008242 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01008243 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01008244 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008245 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008246#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008247 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008248 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008249#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008250 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008251
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008252 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008253 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8254 if (qi == NULL)
8255 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01008256
Bram Moolenaara16123a2019-03-28 20:31:07 +01008257 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
8258 return;
8259
8260 qf_title = qf_cmdtitle(*eap->cmdlinep);
8261
8262 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008263 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01008264 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
8265 (char *)qf_title, (char *)buf->b_sfname);
8266 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008267 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01008268
8269 incr_quickfix_busy();
8270
8271 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
8272 (eap->cmdidx != CMD_caddbuffer
8273 && eap->cmdidx != CMD_laddbuffer),
8274 line1, line2,
8275 qf_title, NULL);
8276 if (qf_stack_empty(qi))
8277 {
8278 decr_quickfix_busy();
8279 return;
8280 }
8281 if (res >= 0)
8282 qf_list_changed(qf_get_curlist(qi));
8283
8284 // Remember the current quickfix list identifier, so that we can
8285 // check for autocommands changing the current quickfix list.
8286 save_qfid = qf_get_curlist(qi)->qf_id;
8287 if (au_name != NULL)
8288 {
8289 buf_T *curbuf_old = curbuf;
8290
8291 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
8292 TRUE, curbuf);
8293 if (curbuf != curbuf_old)
8294 // Autocommands changed buffer, don't jump now, "qi" may
8295 // be invalid.
8296 res = 0;
8297 }
8298 // Jump to the first error for a new list and if autocmds didn't
8299 // free the list.
8300 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
8301 eap->cmdidx == CMD_lbuffer)
8302 && qflist_valid(wp, save_qfid))
8303 // display the first error
8304 qf_jump_first(qi, save_qfid, eap->forceit);
8305
8306 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00008307}
8308
Bram Moolenaar1e015462005-09-25 22:16:38 +00008309#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008310/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008311 * Return the autocmd name for the :cexpr Ex commands.
8312 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008313 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01008314cexpr_get_auname(cmdidx_T cmdidx)
8315{
8316 switch (cmdidx)
8317 {
8318 case CMD_cexpr: return (char_u *)"cexpr";
8319 case CMD_cgetexpr: return (char_u *)"cgetexpr";
8320 case CMD_caddexpr: return (char_u *)"caddexpr";
8321 case CMD_lexpr: return (char_u *)"lexpr";
8322 case CMD_lgetexpr: return (char_u *)"lgetexpr";
8323 case CMD_laddexpr: return (char_u *)"laddexpr";
8324 default: return NULL;
8325 }
8326}
8327
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008328 int
8329trigger_cexpr_autocmd(int cmdidx)
8330{
8331 char_u *au_name = cexpr_get_auname(cmdidx);
8332
8333 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8334 curbuf->b_fname, TRUE, curbuf))
8335 {
8336 if (aborting())
8337 return FAIL;
8338 }
8339 return OK;
8340}
8341
8342 int
8343cexpr_core(exarg_T *eap, typval_T *tv)
8344{
8345 qf_info_T *qi;
8346 win_T *wp = NULL;
8347
8348 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8349 if (qi == NULL)
8350 return FAIL;
8351
8352 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8353 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8354 {
8355 int res;
8356 int_u save_qfid;
8357 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8358
8359 incr_quickfix_busy();
8360 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8361 (eap->cmdidx != CMD_caddexpr
8362 && eap->cmdidx != CMD_laddexpr),
8363 (linenr_T)0, (linenr_T)0,
8364 qf_cmdtitle(*eap->cmdlinep), NULL);
8365 if (qf_stack_empty(qi))
8366 {
8367 decr_quickfix_busy();
8368 return FAIL;
8369 }
8370 if (res >= 0)
8371 qf_list_changed(qf_get_curlist(qi));
8372
8373 // Remember the current quickfix list identifier, so that we can
8374 // check for autocommands changing the current quickfix list.
8375 save_qfid = qf_get_curlist(qi)->qf_id;
8376 if (au_name != NULL)
8377 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8378 curbuf->b_fname, TRUE, curbuf);
8379
8380 // Jump to the first error for a new list and if autocmds didn't
8381 // free the list.
8382 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8383 && qflist_valid(wp, save_qfid))
8384 // display the first error
8385 qf_jump_first(qi, save_qfid, eap->forceit);
8386 decr_quickfix_busy();
8387 return OK;
8388 }
8389
Bram Moolenaar677658a2022-01-05 16:09:06 +00008390 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008391 return FAIL;
8392}
8393
Bram Moolenaara16123a2019-03-28 20:31:07 +01008394/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008395 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8396 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008397 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008398 */
8399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008400ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008401{
8402 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008403
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008404 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008405 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008406
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008407 // Evaluate the expression. When the result is a string or a list we can
8408 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008409 tv = eval_expr(eap->arg, eap);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008410 if (tv == NULL)
8411 return;
8412
8413 (void)cexpr_core(eap, tv);
8414 free_tv(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008415}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008416#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008417
8418/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008419 * Get the location list for ":lhelpgrep"
8420 */
8421 static qf_info_T *
8422hgr_get_ll(int *new_ll)
8423{
8424 win_T *wp;
8425 qf_info_T *qi;
8426
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008427 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008428 if (bt_help(curwin->w_buffer))
8429 wp = curwin;
8430 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008431 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008432 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008433
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008434 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008435 qi = NULL;
8436 else
8437 qi = wp->w_llist;
8438
8439 if (qi == NULL)
8440 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008441 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008442 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008443 return NULL;
8444 *new_ll = TRUE;
8445 }
8446
8447 return qi;
8448}
8449
8450/*
8451 * Search for a pattern in a help file.
8452 */
8453 static void
8454hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008455 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008456 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008457 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008458 regmatch_T *p_regmatch)
8459{
8460 FILE *fd;
8461 long lnum;
8462
8463 fd = mch_fopen((char *)fname, "r");
8464 if (fd == NULL)
8465 return;
8466
8467 lnum = 1;
8468 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8469 {
8470 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008471
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008472 // Convert a line if 'encoding' is not utf-8 and
8473 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008474 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008475 {
8476 line = string_convert(p_vc, IObuff, NULL);
8477 if (line == NULL)
8478 line = IObuff;
8479 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008480
8481 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8482 {
8483 int l = (int)STRLEN(line);
8484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008485 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008486 while (l > 0 && line[l - 1] <= ' ')
8487 line[--l] = NUL;
8488
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008489 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008490 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008491 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008492 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008493 0,
8494 line,
8495 lnum,
thinca6864efa2021-06-19 20:45:20 +02008496 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008497 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008498 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008499 (int)(p_regmatch->endp[0] - line)
8500 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008501 FALSE, // vis_col
8502 NULL, // search pattern
8503 0, // nr
8504 1, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02008505 NULL, // user_data
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008506 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008507 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008508 {
8509 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008510 if (line != IObuff)
8511 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008512 break;
8513 }
8514 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008515 if (line != IObuff)
8516 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008517 ++lnum;
8518 line_breakcheck();
8519 }
8520 fclose(fd);
8521}
8522
8523/*
8524 * Search for a pattern in all the help files in the doc directory under
8525 * the given directory.
8526 */
8527 static void
8528hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008529 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008530 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008531 regmatch_T *p_regmatch,
8532 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008533#ifdef FEAT_MULTI_LANG
8534 , char_u *lang
8535#endif
8536 )
8537{
8538 int fcount;
8539 char_u **fnames;
8540 int fi;
8541
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008542 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008543 add_pathsep(dirname);
8544 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8545 if (gen_expand_wildcards(1, &dirname, &fcount,
8546 &fnames, EW_FILE|EW_SILENT) == OK
8547 && fcount > 0)
8548 {
8549 for (fi = 0; fi < fcount && !got_int; ++fi)
8550 {
8551#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008552 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008553 if (lang != NULL
8554 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008555 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008556 && !(STRNICMP(lang, "en", 2) == 0
8557 && STRNICMP("txt", fnames[fi]
8558 + STRLEN(fnames[fi]) - 3, 3) == 0))
8559 continue;
8560#endif
8561
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008562 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008563 }
8564 FreeWild(fcount, fnames);
8565 }
8566}
8567
8568/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008569 * Search for a pattern in all the help files in the 'runtimepath'
8570 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008571 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008572 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008573 */
8574 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008575hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008576{
8577 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008578
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008579 vimconv_T vc;
8580
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008581 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8582 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008583 vc.vc_type = CONV_NONE;
8584 if (!enc_utf8)
8585 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008586
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008587 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008588 p = p_rtp;
8589 while (*p != NUL && !got_int)
8590 {
8591 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8592
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008593 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008594#ifdef FEAT_MULTI_LANG
8595 , lang
8596#endif
8597 );
8598 }
8599
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008600 if (vc.vc_type != CONV_NONE)
8601 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008602}
8603
8604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 * ":helpgrep {pattern}"
8606 */
8607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008608ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
8610 regmatch_T regmatch;
8611 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008612 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008613 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008614 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008615 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008616 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008617 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
Bram Moolenaar73633f82012-01-20 13:39:07 +01008619 switch (eap->cmdidx)
8620 {
8621 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8622 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8623 default: break;
8624 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008625 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8626 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008627 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008628#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008629 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008630 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008631#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008632 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008633
Bram Moolenaar39665952018-08-15 20:59:48 +02008634 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008635 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008636 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008637 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008638 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008639 }
8640
Bram Moolenaara16123a2019-03-28 20:31:07 +01008641 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8642 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008643 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008644 p_cpo = empty_option;
8645
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008646 incr_quickfix_busy();
8647
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008648#ifdef FEAT_MULTI_LANG
8649 // Check for a specified language
8650 lang = check_help_lang(eap->arg);
8651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8653 regmatch.rm_ic = FALSE;
8654 if (regmatch.regprog != NULL)
8655 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008656 qf_list_T *qfl;
8657
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008658 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008659 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008660 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008662 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008663
Bram Moolenaar473de612013-06-08 18:19:48 +02008664 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008666 qfl->qf_nonevalid = FALSE;
8667 qfl->qf_ptr = qfl->qf_start;
8668 qfl->qf_index = 1;
8669 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008670 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 }
8672
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008673 if (p_cpo == empty_option)
8674 p_cpo = save_cpo;
8675 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008676 {
8677 // Darn, some plugin changed the value. If it's still empty it was
8678 // changed and restored, need to restore in the complicated way.
8679 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008680 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008681 if (save_cpo_allocated)
8682 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008683 }
8684
8685 if (updated)
8686 // This may open a window and source scripts, do this after 'cpo' was
8687 // restored.
8688 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
Bram Moolenaar73633f82012-01-20 13:39:07 +01008690 if (au_name != NULL)
8691 {
8692 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8693 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008694 // When adding a location list to an existing location list stack,
8695 // if the autocmd made the stack invalid, then just return.
8696 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008697 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008698 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008699 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008700 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008701 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008702
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008703 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008704 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008705 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008706 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008707 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008708
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008709 decr_quickfix_busy();
8710
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008711 if (eap->cmdidx == CMD_lhelpgrep)
8712 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008713 // If the help window is not opened or if it already points to the
8714 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008715 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008716 {
8717 if (new_qi)
8718 ll_free_all(&qi);
8719 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008720 else if (curwin->w_llist == NULL && new_qi)
8721 // current window didn't have a location list associated with it
8722 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008723 curwin->w_llist = qi;
8724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008726
8727# if defined(EXITFREE) || defined(PROTO)
8728 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00008729free_quickfix(void)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008730{
8731 win_T *win;
8732 tabpage_T *tab;
8733
8734 qf_free_all(NULL);
8735 // Free all location lists
8736 FOR_ALL_TAB_WINDOWS(tab, win)
8737 qf_free_all(win);
8738
8739 ga_clear(&qfga);
8740}
8741# endif
8742
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008743#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008744
8745#if defined(FEAT_EVAL) || defined(PROTO)
8746# ifdef FEAT_QUICKFIX
8747 static void
8748get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8749{
8750 if (what_arg->v_type == VAR_UNKNOWN)
8751 {
8752 if (rettv_list_alloc(rettv) == OK)
8753 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008754 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008755 }
8756 else
8757 {
8758 if (rettv_dict_alloc(rettv) == OK)
8759 if (is_qf || (wp != NULL))
8760 {
8761 if (what_arg->v_type == VAR_DICT)
8762 {
8763 dict_T *d = what_arg->vval.v_dict;
8764
8765 if (d != NULL)
8766 qf_get_properties(wp, d, rettv->vval.v_dict);
8767 }
8768 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008769 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008770 }
8771 }
8772}
8773# endif
8774
8775/*
8776 * "getloclist()" function
8777 */
8778 void
8779f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8780{
8781# ifdef FEAT_QUICKFIX
8782 win_T *wp;
8783
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008784 if (in_vim9script()
8785 && (check_for_number_arg(argvars, 0) == FAIL
8786 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8787 return;
8788
Bram Moolenaare677df82019-09-02 22:31:11 +02008789 wp = find_win_by_nr_or_id(&argvars[0]);
8790 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8791# endif
8792}
8793
8794/*
8795 * "getqflist()" function
8796 */
8797 void
8798f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8799{
8800# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008801 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8802 return;
8803
Bram Moolenaare677df82019-09-02 22:31:11 +02008804 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8805# endif
8806}
8807
8808/*
8809 * Used by "setqflist()" and "setloclist()" functions
8810 */
8811 static void
8812set_qf_ll_list(
8813 win_T *wp UNUSED,
8814 typval_T *list_arg UNUSED,
8815 typval_T *action_arg UNUSED,
8816 typval_T *what_arg UNUSED,
8817 typval_T *rettv)
8818{
8819# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008820 char_u *act;
8821 int action = 0;
8822 static int recursive = 0;
8823# endif
8824
8825 rettv->vval.v_number = -1;
8826
8827# ifdef FEAT_QUICKFIX
8828 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008829 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008830 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008831 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008832 else
8833 {
8834 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008835 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008836 int valid_dict = TRUE;
8837
8838 if (action_arg->v_type == VAR_STRING)
8839 {
8840 act = tv_get_string_chk(action_arg);
8841 if (act == NULL)
8842 return; // type error; errmsg already given
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008843 if ((*act == 'a' || *act == 'r' || *act == 'u' || *act == ' ' || *act == 'f') &&
Bram Moolenaare677df82019-09-02 22:31:11 +02008844 act[1] == NUL)
8845 action = *act;
8846 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008847 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008848 }
8849 else if (action_arg->v_type == VAR_UNKNOWN)
8850 action = ' ';
8851 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008852 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008853
8854 if (action_arg->v_type != VAR_UNKNOWN
8855 && what_arg->v_type != VAR_UNKNOWN)
8856 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008857 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8858 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008859 else
8860 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008861 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008862 valid_dict = FALSE;
8863 }
8864 }
8865
8866 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008867 if (l != NULL && action && valid_dict
8868 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008869 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008870 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008871 rettv->vval.v_number = 0;
8872 --recursive;
8873 }
8874# endif
8875}
8876
8877/*
8878 * "setloclist()" function
8879 */
8880 void
8881f_setloclist(typval_T *argvars, typval_T *rettv)
8882{
8883 win_T *win;
8884
8885 rettv->vval.v_number = -1;
8886
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008887 if (in_vim9script()
8888 && (check_for_number_arg(argvars, 0) == FAIL
8889 || check_for_list_arg(argvars, 1) == FAIL
8890 || check_for_opt_string_arg(argvars, 2) == FAIL
8891 || (argvars[2].v_type != VAR_UNKNOWN
8892 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8893 return;
8894
Bram Moolenaare677df82019-09-02 22:31:11 +02008895 win = find_win_by_nr_or_id(&argvars[0]);
8896 if (win != NULL)
8897 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8898}
8899
8900/*
8901 * "setqflist()" function
8902 */
8903 void
8904f_setqflist(typval_T *argvars, typval_T *rettv)
8905{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008906 if (in_vim9script()
8907 && (check_for_list_arg(argvars, 0) == FAIL
8908 || check_for_opt_string_arg(argvars, 1) == FAIL
8909 || (argvars[1].v_type != VAR_UNKNOWN
8910 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8911 return;
8912
Bram Moolenaare677df82019-09-02 22:31:11 +02008913 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8914}
8915#endif