blob: 11bfa822411b1b8ee1a828b44dfa50f4bd5b7356 [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 {
Christian Brabandtce80c592025-03-28 19:13:32 +01002038 int buf_was_null = FALSE;
2039 // can happen when curwin is going to be closed e.g. curwin->w_buffer
2040 // was already closed in win_close(), and we are now closing the
2041 // window related location list buffer from win_free_mem()
2042 // but close_buffer() calls CHECK_CURBUF() macro and requires
2043 // curwin->w_buffer == curbuf
2044 if (curwin->w_buffer == NULL)
2045 {
2046 curwin->w_buffer = curbuf;
2047 buf_was_null = TRUE;
2048 }
2049
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002050 // If the quickfix buffer is not loaded in any window, then
2051 // wipe the buffer.
2052 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
2053 qi->qf_bufnr = INVALID_QFBUFNR;
Christian Brabandtce80c592025-03-28 19:13:32 +01002054 if (buf_was_null)
2055 curwin->w_buffer = NULL;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002056 }
2057}
2058
2059/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002060 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002061 */
2062 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002063ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002064{
2065 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002066 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002067
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002068 qi = *pqi;
2069 if (qi == NULL)
2070 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002071 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002072
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002073 // If the location list is still in use, then queue the delete request
2074 // to be processed later.
2075 if (quickfix_busy > 0)
2076 {
2077 locstack_queue_delreq(qi);
2078 return;
2079 }
2080
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002081 qi->qf_refcount--;
2082 if (qi->qf_refcount < 1)
2083 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002084 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002085 // If the quickfix window buffer is loaded, then wipe it
2086 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002087
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002088 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002089 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002090 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002091 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002092}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002093
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002094/*
2095 * Free all the quickfix/location lists in the stack.
2096 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002097 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002098qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002099{
2100 int i;
2101 qf_info_T *qi = &ql_info;
2102
2103 if (wp != NULL)
2104 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002105 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106 ll_free_all(&wp->w_llist);
2107 ll_free_all(&wp->w_llist_ref);
2108 }
2109 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002110 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002112 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002116 * Delay freeing of location list stacks when the quickfix code is running.
2117 * Used to avoid problems with autocmds freeing location list stacks when the
2118 * quickfix code is still referencing the stack.
2119 * Must always call decr_quickfix_busy() exactly once after this.
2120 */
2121 static void
2122incr_quickfix_busy(void)
2123{
2124 quickfix_busy++;
2125}
2126
2127/*
2128 * Safe to free location list stacks. Process any delayed delete requests.
2129 */
2130 static void
2131decr_quickfix_busy(void)
2132{
2133 if (--quickfix_busy == 0)
2134 {
2135 // No longer referencing the location lists. Process all the pending
2136 // delete requests.
2137 while (qf_delq_head != NULL)
2138 {
2139 qf_delq_T *q = qf_delq_head;
2140
2141 qf_delq_head = q->next;
2142 ll_free_all(&q->qi);
2143 vim_free(q);
2144 }
2145 }
2146#ifdef ABORT_ON_INTERNAL_ERROR
2147 if (quickfix_busy < 0)
2148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002149 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002150 abort();
2151 }
2152#endif
2153}
2154
2155#if defined(EXITFREE) || defined(PROTO)
2156 void
2157check_quickfix_busy(void)
2158{
2159 if (quickfix_busy != 0)
2160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002161 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002162# ifdef ABORT_ON_INTERNAL_ERROR
2163 abort();
2164# endif
2165 }
2166}
2167#endif
2168
2169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002171 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 */
2173 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002174qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002175 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002176 char_u *dir, // optional directory name
2177 char_u *fname, // file name or NULL
2178 char_u *module, // module name or NULL
2179 int bufnum, // buffer number or zero
2180 char_u *mesg, // message
2181 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002182 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002183 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002184 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002185 int vis_col, // using visual column
2186 char_u *pattern, // search pattern
2187 int nr, // error number
2188 int type, // type character
Tom Praschanca6ac992023-08-11 23:26:12 +02002189 typval_T *user_data, // custom user data or NULL
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002190 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
Austin Chang29822992024-10-03 10:50:05 +02002192 buf_T *buf;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002193 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002194 qfline_T **lastp; // pointer to qf_last or NULL
Austin Chang29822992024-10-03 10:50:05 +02002195 char_u *fullname = NULL;
2196 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002198 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002199 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002200 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002201 {
Austin Chang29822992024-10-03 10:50:05 +02002202 buf = buflist_findnr(bufnum);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002203
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002204 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002205 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002206 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002207 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002208 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002209 else
Austin Chang29822992024-10-03 10:50:05 +02002210 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002211 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Austin Chang29822992024-10-03 10:50:05 +02002212 buf = buflist_findnr(qfp->qf_fnum);
2213 }
2214 if (fname != NULL)
2215 fullname = fix_fname(fname);
2216 qfp->qf_fname = NULL;
2217 if (buf != NULL &&
2218 buf->b_ffname != NULL && fullname != NULL)
2219 {
2220 if (fnamecmp(fullname, buf->b_ffname) != 0)
2221 {
2222 p = shorten_fname1(fullname);
2223 if (p != NULL)
2224 qfp->qf_fname = vim_strsave(p);
2225 }
2226 }
2227 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2229 {
2230 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002231 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002234 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002236 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002237 qfp->qf_viscol = vis_col;
Tom Praschanca6ac992023-08-11 23:26:12 +02002238 if (user_data == NULL || user_data->v_type == VAR_UNKNOWN)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002239 qfp->qf_user_data.v_type = VAR_UNKNOWN;
Tom Praschanca6ac992023-08-11 23:26:12 +02002240 else
2241 {
2242 copy_tv(user_data, &qfp->qf_user_data);
2243 qfl->qf_has_user_data = TRUE;
2244 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002245 if (pattern == NULL || *pattern == NUL)
2246 qfp->qf_pattern = NULL;
2247 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2248 {
2249 vim_free(qfp->qf_text);
2250 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002251 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002252 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002253 if (module == NULL || *module == NUL)
2254 qfp->qf_module = NULL;
2255 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2256 {
2257 vim_free(qfp->qf_text);
2258 vim_free(qfp->qf_pattern);
2259 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002260 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002263 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 type = 0;
2265 qfp->qf_type = type;
2266 qfp->qf_valid = valid;
2267
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002268 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002269 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002271 qfl->qf_start = qfp;
2272 qfl->qf_ptr = qfp;
2273 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002274 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 else
2277 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002278 qfp->qf_prev = *lastp;
2279 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002281 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002283 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002284 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002285 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002287 qfl->qf_index = qfl->qf_count;
2288 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290
Bram Moolenaar95946f12019-03-31 15:31:59 +02002291 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292}
2293
2294/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002295 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002296 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002297 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002298qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002299{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002300 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002301
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002302 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002303 if (qi == NULL)
2304 return NULL;
2305
2306 qi->qf_refcount++;
2307 qi->qfl_type = qfltype;
2308 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002309 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002310}
2311
2312/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002313 * Return the location list stack for window 'wp'.
2314 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002315 */
2316 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002317ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002318{
2319 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002320 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002321 return wp->w_llist_ref;
2322
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002323 // For a non-location list window, w_llist_ref should not point to a
2324 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002325 ll_free_all(&wp->w_llist_ref);
2326
2327 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002328 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 return wp->w_llist;
2330}
2331
2332/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002333 * Get the quickfix/location list stack to use for the specified Ex command.
2334 * For a location list command, returns the stack for the current window. If
2335 * the location list is not found, then returns NULL and prints an error
2336 * message if 'print_emsg' is TRUE.
2337 */
2338 static qf_info_T *
2339qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2340{
2341 qf_info_T *qi = &ql_info;
2342
2343 if (is_loclist_cmd(eap->cmdidx))
2344 {
2345 qi = GET_LOC_LIST(curwin);
2346 if (qi == NULL)
2347 {
2348 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002349 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002350 return NULL;
2351 }
2352 }
2353
2354 return qi;
2355}
2356
2357/*
2358 * Get the quickfix/location list stack to use for the specified Ex command.
2359 * For a location list command, returns the stack for the current window.
2360 * If the location list is not present, then allocates a new one.
2361 * Returns NULL if the allocation fails. For a location list command, sets
2362 * 'pwinp' to curwin.
2363 */
2364 static qf_info_T *
2365qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2366{
2367 qf_info_T *qi = &ql_info;
2368
2369 if (is_loclist_cmd(eap->cmdidx))
2370 {
2371 qi = ll_get_or_alloc_list(curwin);
2372 if (qi == NULL)
2373 return NULL;
2374 *pwinp = curwin;
2375 }
2376
2377 return qi;
2378}
2379
2380/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002381 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2382 */
2383 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002384copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002385{
2386 int i;
2387 qfline_T *from_qfp;
2388 qfline_T *prevp;
2389
2390 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002391 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002392 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002393 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002394 NULL,
2395 NULL,
2396 from_qfp->qf_module,
2397 0,
2398 from_qfp->qf_text,
2399 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002400 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002401 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002402 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002403 from_qfp->qf_viscol,
2404 from_qfp->qf_pattern,
2405 from_qfp->qf_nr,
2406 0,
Tom Praschanca6ac992023-08-11 23:26:12 +02002407 &from_qfp->qf_user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002408 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002409 return FAIL;
2410
2411 // qf_add_entry() will not set the qf_num field, as the
2412 // directory and file names are not supplied. So the qf_fnum
2413 // field is copied here.
2414 prevp = to_qfl->qf_last;
2415 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2416 prevp->qf_type = from_qfp->qf_type; // error type
2417 if (from_qfl->qf_ptr == from_qfp)
2418 to_qfl->qf_ptr = prevp; // current location
2419 }
2420
2421 return OK;
2422}
2423
2424/*
2425 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2426 */
2427 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002428copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002429{
2430 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002431 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002432 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
Tom Praschanca6ac992023-08-11 23:26:12 +02002433 to_qfl->qf_has_user_data = from_qfl->qf_has_user_data;
Bram Moolenaar09037502018-09-25 22:08:14 +02002434 to_qfl->qf_count = 0;
2435 to_qfl->qf_index = 0;
2436 to_qfl->qf_start = NULL;
2437 to_qfl->qf_last = NULL;
2438 to_qfl->qf_ptr = NULL;
2439 if (from_qfl->qf_title != NULL)
2440 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2441 else
2442 to_qfl->qf_title = NULL;
2443 if (from_qfl->qf_ctx != NULL)
2444 {
2445 to_qfl->qf_ctx = alloc_tv();
2446 if (to_qfl->qf_ctx != NULL)
2447 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2448 }
2449 else
2450 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002451 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2452 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002453 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002454 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002455
2456 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002457 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002458 return FAIL;
2459
2460 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2461
2462 // Assign a new ID for the location list
2463 to_qfl->qf_id = ++last_qf_id;
2464 to_qfl->qf_changedtick = 0L;
2465
2466 // When no valid entries are present in the list, qf_ptr points to
2467 // the first item in the list
2468 if (to_qfl->qf_nonevalid)
2469 {
2470 to_qfl->qf_ptr = to_qfl->qf_start;
2471 to_qfl->qf_index = 1;
2472 }
2473
2474 return OK;
2475}
2476
2477/*
2478 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002479 */
2480 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002481copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002482{
2483 qf_info_T *qi;
2484 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002485
Bram Moolenaar09037502018-09-25 22:08:14 +02002486 // When copying from a location list window, copy the referenced
2487 // location list. For other windows, copy the location list for
2488 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002489 if (IS_LL_WINDOW(from))
2490 qi = from->w_llist_ref;
2491 else
2492 qi = from->w_llist;
2493
Bram Moolenaar09037502018-09-25 22:08:14 +02002494 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002495 return;
2496
Bram Moolenaar09037502018-09-25 22:08:14 +02002497 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002498 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002499 return;
2500
2501 to->w_llist->qf_listcount = qi->qf_listcount;
2502
Bram Moolenaar09037502018-09-25 22:08:14 +02002503 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002504 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002505 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002506 to->w_llist->qf_curlist = idx;
2507
Bram Moolenaar0398e002019-03-21 21:12:49 +01002508 if (copy_loclist(qf_get_list(qi, idx),
2509 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002510 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002511 qf_free_all(to);
2512 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002513 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002514 }
2515
Bram Moolenaar09037502018-09-25 22:08:14 +02002516 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002517}
2518
2519/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002520 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002521 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
2523 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002524qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
Bram Moolenaar82404332016-07-10 17:00:38 +02002526 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002527 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002528 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002529
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002530 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaare60acc12011-05-10 16:41:25 +02002533#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002534 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002535#endif
2536#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002537 if (directory != NULL)
2538 slash_adjust(directory);
2539 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002540#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002541 if (directory != NULL && !vim_isAbsName(fname)
2542 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2543 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002544 // Here we check if the file really exists.
2545 // This should normally be true, but if make works without
2546 // "leaving directory"-messages we might have missed a
2547 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002548 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002551 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002552 if (directory)
2553 ptr = concat_fnames(directory, fname, TRUE);
2554 else
2555 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002557 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002558 bufname = ptr;
2559 }
2560 else
2561 bufname = fname;
2562
2563 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002564 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002565 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002566 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002567 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002569 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002570 {
2571 vim_free(qf_last_bufname);
2572 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2573 if (bufname == ptr)
2574 qf_last_bufname = bufname;
2575 else
2576 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002577 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002578 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002579 if (buf == NULL)
2580 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002581
Bram Moolenaarc1542742016-07-20 21:44:37 +02002582 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002583 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002584 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585}
2586
2587/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002588 * Push dirbuf onto the directory stack and return pointer to actual dir or
2589 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 */
2591 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002592qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 struct dir_stack_T *ds_new;
2595 struct dir_stack_T *ds_ptr;
2596
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002597 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002598 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (ds_new == NULL)
2600 return NULL;
2601
2602 ds_new->next = *stackptr;
2603 *stackptr = ds_new;
2604
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002605 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (vim_isAbsName(dirbuf)
2607 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002608 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 (*stackptr)->dirname = vim_strsave(dirbuf);
2610 else
2611 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002612 // Okay we don't have an absolute path.
2613 // dirbuf must be a subdir of one of the directories on the stack.
2614 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 ds_new = (*stackptr)->next;
2616 (*stackptr)->dirname = NULL;
2617 while (ds_new)
2618 {
2619 vim_free((*stackptr)->dirname);
2620 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2621 TRUE);
2622 if (mch_isdir((*stackptr)->dirname) == TRUE)
2623 break;
2624
2625 ds_new = ds_new->next;
2626 }
2627
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002628 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 while ((*stackptr)->next != ds_new)
2630 {
2631 ds_ptr = (*stackptr)->next;
2632 (*stackptr)->next = (*stackptr)->next->next;
2633 vim_free(ds_ptr->dirname);
2634 vim_free(ds_ptr);
2635 }
2636
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002637 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (ds_new == NULL)
2639 {
2640 vim_free((*stackptr)->dirname);
2641 (*stackptr)->dirname = vim_strsave(dirbuf);
2642 }
2643 }
2644
2645 if ((*stackptr)->dirname != NULL)
2646 return (*stackptr)->dirname;
2647 else
2648 {
2649 ds_ptr = *stackptr;
2650 *stackptr = (*stackptr)->next;
2651 vim_free(ds_ptr);
2652 return NULL;
2653 }
2654}
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656/*
2657 * pop dirbuf from the directory stack and return previous directory or NULL if
2658 * stack is empty
2659 */
2660 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002661qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663 struct dir_stack_T *ds_ptr;
2664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002665 // TODO: Should we check if dirbuf is the directory on top of the stack?
2666 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002668 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (*stackptr != NULL)
2670 {
2671 ds_ptr = *stackptr;
2672 *stackptr = (*stackptr)->next;
2673 vim_free(ds_ptr->dirname);
2674 vim_free(ds_ptr);
2675 }
2676
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002677 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 return *stackptr ? (*stackptr)->dirname : NULL;
2679}
2680
2681/*
2682 * clean up directory stack
2683 */
2684 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002685qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 struct dir_stack_T *ds_ptr;
2688
2689 while ((ds_ptr = *stackptr) != NULL)
2690 {
2691 *stackptr = (*stackptr)->next;
2692 vim_free(ds_ptr->dirname);
2693 vim_free(ds_ptr);
2694 }
2695}
2696
2697/*
2698 * Check in which directory of the directory stack the given file can be
2699 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002700 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 * Cleans up intermediate directory entries.
2702 *
2703 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002704 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 * ./
2706 * ./aa
2707 * ./aa/bb
2708 * ./bb
2709 * ./bb/x.c
2710 * and make says:
2711 * making all in aa
2712 * making all in bb
2713 * x.c:9: Error
2714 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2715 * qf_guess_filepath will return NULL.
2716 */
2717 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002718qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719{
2720 struct dir_stack_T *ds_ptr;
2721 struct dir_stack_T *ds_tmp;
2722 char_u *fullname;
2723
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002724 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002725 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 return NULL;
2727
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002728 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 fullname = NULL;
2730 while (ds_ptr)
2731 {
2732 vim_free(fullname);
2733 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002735 // If concat_fnames failed, just go on. The worst thing that can happen
2736 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2738 break;
2739
2740 ds_ptr = ds_ptr->next;
2741 }
2742
2743 vim_free(fullname);
2744
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002745 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002746 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002748 ds_tmp = qfl->qf_dir_stack->next;
2749 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 vim_free(ds_tmp->dirname);
2751 vim_free(ds_tmp);
2752 }
2753
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002754 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755}
2756
2757/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002758 * Returns TRUE if a quickfix/location list with the given identifier exists.
2759 */
2760 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002761qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002762{
2763 qf_info_T *qi = &ql_info;
2764 int i;
2765
2766 if (wp != NULL)
2767 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002768 if (!win_valid(wp))
2769 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002770 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002771 if (qi == NULL)
2772 return FALSE;
2773 }
2774
2775 for (i = 0; i < qi->qf_listcount; ++i)
2776 if (qi->qf_lists[i].qf_id == qf_id)
2777 return TRUE;
2778
2779 return FALSE;
2780}
2781
2782/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002783 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002784 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002785 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002786 * Similar to location list.
2787 */
2788 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002789is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002790{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002791 qfline_T *qfp;
2792 int i;
2793
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002794 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002795 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2796 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002797 break;
2798
Bram Moolenaar95946f12019-03-31 15:31:59 +02002799 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002800 return FALSE;
2801
2802 return TRUE;
2803}
2804
2805/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002806 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002807 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002808 */
2809 static qfline_T *
2810get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002811 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002812 qfline_T *qf_ptr,
2813 int *qf_index,
2814 int dir)
2815{
2816 int idx;
2817 int old_qf_fnum;
2818
2819 idx = *qf_index;
2820 old_qf_fnum = qf_ptr->qf_fnum;
2821
2822 do
2823 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002824 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002825 return NULL;
2826 ++idx;
2827 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002828 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002829 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2830
2831 *qf_index = idx;
2832 return qf_ptr;
2833}
2834
2835/*
2836 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002837 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002838 */
2839 static qfline_T *
2840get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002841 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002842 qfline_T *qf_ptr,
2843 int *qf_index,
2844 int dir)
2845{
2846 int idx;
2847 int old_qf_fnum;
2848
2849 idx = *qf_index;
2850 old_qf_fnum = qf_ptr->qf_fnum;
2851
2852 do
2853 {
2854 if (idx == 1 || qf_ptr->qf_prev == NULL)
2855 return NULL;
2856 --idx;
2857 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002858 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002859 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2860
2861 *qf_index = idx;
2862 return qf_ptr;
2863}
2864
2865/*
2866 * Get the n'th (errornr) previous/next valid entry from the current entry in
2867 * the quickfix list.
2868 * dir == FORWARD or FORWARD_FILE: next valid entry
2869 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2870 */
2871 static qfline_T *
2872get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002873 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002874 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002875 int dir,
2876 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002877{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002878 qfline_T *qf_ptr = qfl->qf_ptr;
2879 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002880 qfline_T *prev_qf_ptr;
2881 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002882 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002883
2884 while (errornr--)
2885 {
2886 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002887 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002888
2889 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002890 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002891 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002892 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002893 if (qf_ptr == NULL)
2894 {
2895 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002896 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002897 if (err != NULL)
2898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002899 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002900 return NULL;
2901 }
2902 break;
2903 }
2904
2905 err = NULL;
2906 }
2907
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002908 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002909 return qf_ptr;
2910}
2911
2912/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002913 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2914 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002915 */
2916 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002917get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002918{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002919 qfline_T *qf_ptr = qfl->qf_ptr;
2920 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002921
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002922 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002923 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2924 {
2925 --qf_idx;
2926 qf_ptr = qf_ptr->qf_prev;
2927 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002928 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002929 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2930 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002931 {
2932 ++qf_idx;
2933 qf_ptr = qf_ptr->qf_next;
2934 }
2935
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002936 *new_qfidx = qf_idx;
2937 return qf_ptr;
2938}
2939
2940/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002941 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002942 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2943 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2944 * Returns a pointer to the entry and the index of the new entry is stored in
2945 * 'new_qfidx'.
2946 */
2947 static qfline_T *
2948qf_get_entry(
2949 qf_list_T *qfl,
2950 int errornr,
2951 int dir,
2952 int *new_qfidx)
2953{
2954 qfline_T *qf_ptr = qfl->qf_ptr;
2955 int qfidx = qfl->qf_index;
2956
2957 if (dir != 0) // next/prev valid entry
2958 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2959 else if (errornr != 0) // go to specified number
2960 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2961
2962 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002963 return qf_ptr;
2964}
2965
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002966/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002967 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002968 */
2969 static win_T *
2970qf_find_help_win(void)
2971{
2972 win_T *wp;
2973
2974 FOR_ALL_WINDOWS(wp)
2975 if (bt_help(wp->w_buffer))
2976 return wp;
2977
2978 return NULL;
2979}
2980
2981/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002982 * Set the location list for the specified window to 'qi'.
2983 */
2984 static void
2985win_set_loclist(win_T *wp, qf_info_T *qi)
2986{
2987 wp->w_llist = qi;
2988 qi->qf_refcount++;
2989}
2990
2991/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002992 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2993 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002994 */
2995 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002996jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002997{
2998 win_T *wp;
2999 int flags;
3000
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003002 wp = NULL;
3003 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003004 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003005 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
3006 win_enter(wp, TRUE);
3007 else
3008 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003009 // Split off help window; put it at far top if no position
3010 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003011 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02003012 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003013 && curwin->w_width < 80)
3014 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003015 // If the user asks to open a new window, then copy the location list.
3016 // Otherwise, don't copy the location list.
3017 if (IS_LL_STACK(qi) && !newwin)
3018 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003019
3020 if (win_split(0, flags) == FAIL)
3021 return FAIL;
3022
3023 *opened_window = TRUE;
3024
3025 if (curwin->w_height < p_hh)
3026 win_setheight((int)p_hh);
3027
Bram Moolenaarb2443732018-11-11 22:50:27 +01003028 // When using location list, the new window should use the supplied
3029 // location list. If the user asks to open a new window, then the new
3030 // window will get a copy of the location list.
3031 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003032 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003033 }
3034
3035 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003036 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003037
3038 return OK;
3039}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003040
3041/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003042 * Find a non-quickfix window using the given location list stack in the
3043 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003044 * Returns NULL if a matching window is not found.
3045 */
3046 static win_T *
3047qf_find_win_with_loclist(qf_info_T *ll)
3048{
3049 win_T *wp;
3050
3051 FOR_ALL_WINDOWS(wp)
3052 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
3053 return wp;
3054
3055 return NULL;
3056}
3057
3058/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003059 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003060 */
3061 static win_T *
3062qf_find_win_with_normal_buf(void)
3063{
3064 win_T *wp;
3065
3066 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02003067 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003068 return wp;
3069
3070 return NULL;
3071}
3072
3073/*
3074 * Go to a window in any tabpage containing the specified file. Returns TRUE
3075 * if successfully jumped to the window. Otherwise returns FALSE.
3076 */
3077 static int
3078qf_goto_tabwin_with_file(int fnum)
3079{
3080 tabpage_T *tp;
3081 win_T *wp;
3082
3083 FOR_ALL_TAB_WINDOWS(tp, wp)
3084 if (wp->w_buffer->b_fnum == fnum)
3085 {
3086 goto_tabpage_win(tp, wp);
3087 return TRUE;
3088 }
3089
3090 return FALSE;
3091}
3092
3093/*
3094 * Create a new window to show a file above the quickfix window. Called when
3095 * only the quickfix window is present.
3096 */
3097 static int
3098qf_open_new_file_win(qf_info_T *ll_ref)
3099{
3100 int flags;
3101
3102 flags = WSP_ABOVE;
3103 if (ll_ref != NULL)
3104 flags |= WSP_NEWLOC;
3105 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003106 return FAIL; // not enough room for window
3107 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003108 swb_flags = 0;
3109 RESET_BINDING(curwin);
3110 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003111 // The new window should use the location list from the
3112 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003113 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003114 return OK;
3115}
3116
3117/*
3118 * Go to a window that shows the right buffer. If the window is not found, go
3119 * to the window just above the location list window. This is used for opening
3120 * a file from a location window and not from a quickfix window. If some usable
3121 * window is previously found, then it is supplied in 'use_win'.
3122 */
3123 static void
3124qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3125{
3126 win_T *win = use_win;
3127
3128 if (win == NULL)
3129 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003130 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003131 FOR_ALL_WINDOWS(win)
3132 if (win->w_buffer->b_fnum == qf_fnum)
3133 break;
3134 if (win == NULL)
3135 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003136 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003137 win = curwin;
3138 do
3139 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003140 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003141 break;
3142 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003143 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003144 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003145 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003146 } while (win != curwin);
3147 }
3148 }
3149 win_goto(win);
3150
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003151 // If the location list for the window is not set, then set it
3152 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003153 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003154 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003155}
3156
3157/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003158 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3159 * not found, then go to the window just above the quickfix window. This is
3160 * used for opening a file from a quickfix window and not from a location
3161 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003162 */
3163 static void
3164qf_goto_win_with_qfl_file(int qf_fnum)
3165{
3166 win_T *win;
3167 win_T *altwin;
3168
3169 win = curwin;
3170 altwin = NULL;
3171 for (;;)
3172 {
3173 if (win->w_buffer->b_fnum == qf_fnum)
3174 break;
3175 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003176 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003177 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003178 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003179
3180 if (IS_QF_WINDOW(win))
3181 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003182 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003183 // window, unless 'switchbuf' contains 'uselast': in this case we
3184 // try to jump to the previously used window first.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003185 if ((swb_flags & SWB_USELAST) && win_valid(prevwin) &&
3186 !prevwin->w_p_wfb)
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003187 win = prevwin;
3188 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003189 win = altwin;
3190 else if (curwin->w_prev != NULL)
3191 win = curwin->w_prev;
3192 else
3193 win = curwin->w_next;
3194 break;
3195 }
3196
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003197 // Remember a usable window.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003198 if (altwin == NULL && !win->w_p_pvw && !win->w_p_wfb &&
3199 bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003200 altwin = win;
3201 }
3202
3203 win_goto(win);
3204}
3205
3206/*
3207 * Find a suitable window for opening a file (qf_fnum) from the
3208 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003209 * window, jump to it. Otherwise open a new window to display the file. If
3210 * 'newwin' is TRUE, then always open a new window. This is called from either
3211 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003212 */
3213 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003214qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003215{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003216 win_T *usable_wp = NULL;
3217 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003218 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003219
Bram Moolenaarb2443732018-11-11 22:50:27 +01003220 // If opening a new window, then don't use the location list referred by
3221 // the current window. Otherwise two windows will refer to the same
3222 // location list.
3223 if (!newwin)
3224 ll_ref = curwin->w_llist_ref;
3225
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003226 if (ll_ref != NULL)
3227 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003228 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003229 usable_wp = qf_find_win_with_loclist(ll_ref);
3230 if (usable_wp != NULL)
3231 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003232 }
3233
3234 if (!usable_win)
3235 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003236 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003237 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003238 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003239 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003240 }
3241
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003242 // If no usable window is found and 'switchbuf' contains "usetab"
3243 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003244 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003245 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003246
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003247 // If there is only one window and it is the quickfix window, create a
3248 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003249 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003250 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003251 if (qf_open_new_file_win(ll_ref) != OK)
3252 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003253 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003254 }
3255 else
3256 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003257 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003258 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003259 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003260 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003261 }
3262
3263 return OK;
3264}
3265
3266/*
3267 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003268 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003269 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003270 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003271 */
3272 static int
3273qf_jump_edit_buffer(
3274 qf_info_T *qi,
3275 qfline_T *qf_ptr,
3276 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003277 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003278 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003279{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003280 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003281 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003282 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003283 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003284 int old_qf_curlist = qi->qf_curlist;
3285 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003286
3287 if (qf_ptr->qf_type == 1)
3288 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003289 // Open help file (do_ecmd() will set b_help flag, readfile() will
3290 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003291 if (!can_abandon(curbuf, forceit))
3292 {
3293 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003294 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003295 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003296
3297 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3298 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003299 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003300 }
3301 else
Colin Kennedy21570352024-03-03 16:16:47 +01003302 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003303 int fnum = qf_ptr->qf_fnum;
3304
3305 if (!forceit && curwin->w_p_wfb && curbuf->b_fnum != fnum)
Colin Kennedy21570352024-03-03 16:16:47 +01003306 {
3307 if (qi->qfl_type == QFLT_LOCATION)
3308 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003309 // Location lists cannot split or reassign their window
3310 // so 'winfixbuf' windows must fail
3311 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3312 return FAIL;
Colin Kennedy21570352024-03-03 16:16:47 +01003313 }
3314
Sean Dewar4bb505e2024-03-05 20:39:07 +01003315 if (win_valid(prevwin) && !prevwin->w_p_wfb &&
3316 !bt_quickfix(prevwin->w_buffer))
Colin Kennedy21570352024-03-03 16:16:47 +01003317 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003318 // 'winfixbuf' is set; attempt to change to a window without it
3319 // that isn't a quickfix/location list window.
3320 win_goto(prevwin);
3321 }
3322 if (curwin->w_p_wfb)
3323 {
3324 // Split the window, which will be 'nowinfixbuf', and set curwin
3325 // to that
3326 if (win_split(0, 0) == OK)
3327 *opened_window = TRUE;
3328
3329 if (curwin->w_p_wfb)
3330 {
3331 // Autocommands set 'winfixbuf' or sent us to another window
Sean Dewar769eb2d2024-03-07 21:37:50 +01003332 // with it set, or we failed to split the window. Give up,
3333 // but don't return immediately, as they may have messed
3334 // with the list.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003335 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3336 retval = FAIL;
3337 }
Colin Kennedy21570352024-03-03 16:16:47 +01003338 }
3339 }
3340
Sean Dewar4bb505e2024-03-05 20:39:07 +01003341 if (retval == OK)
3342 {
3343 retval = buflist_getfile(fnum,
3344 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
3345 }
Colin Kennedy21570352024-03-03 16:16:47 +01003346 }
Bram Moolenaar3c097222017-12-21 20:54:49 +01003347
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003348 // If a location list, check whether the associated window is still
3349 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003350 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003351 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003352 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003353
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003354 if (wp == NULL && curwin->w_llist != qi)
3355 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003356 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003357 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003358 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003359 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003360 }
3361
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003362 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003363 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003364 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003365 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003366 }
3367
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003368 // Check if the list was changed. The pointers may happen to be identical,
3369 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003370 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003371 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003372 || !is_qf_entry_present(qfl, qf_ptr))
3373 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003374 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003375 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003376 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003377 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003378 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003379 }
3380
3381 return retval;
3382}
3383
3384/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003385 * Go to the error line in the current file using either line/column number or
3386 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003387 */
3388 static void
3389qf_jump_goto_line(
3390 linenr_T qf_lnum,
3391 int qf_col,
3392 char_u qf_viscol,
3393 char_u *qf_pattern)
3394{
3395 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003396
3397 if (qf_pattern == NULL)
3398 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003399 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003400 i = qf_lnum;
3401 if (i > 0)
3402 {
3403 if (i > curbuf->b_ml.ml_line_count)
3404 i = curbuf->b_ml.ml_line_count;
3405 curwin->w_cursor.lnum = i;
3406 }
3407 if (qf_col > 0)
3408 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003409 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003410 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003411 coladvance(qf_col - 1);
3412 else
3413 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003414 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003415 check_cursor();
3416 }
3417 else
3418 beginline(BL_WHITE | BL_FIX);
3419 }
3420 else
3421 {
3422 pos_T save_cursor;
3423
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003424 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003425 save_cursor = curwin->w_cursor;
3426 curwin->w_cursor.lnum = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02003427 if (!do_search(NULL, '/', '/', qf_pattern, STRLEN(qf_pattern), (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003428 curwin->w_cursor = save_cursor;
3429 }
3430}
3431
3432/*
3433 * Display quickfix list index and size message
3434 */
3435 static void
3436qf_jump_print_msg(
3437 qf_info_T *qi,
3438 int qf_index,
3439 qfline_T *qf_ptr,
3440 buf_T *old_curbuf,
3441 linenr_T old_lnum)
3442{
3443 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003444 garray_T *gap;
3445
3446 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003447
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003448 // Update the screen before showing the message, unless the screen
3449 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003450 if (!msg_scrolled)
3451 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003452 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003453 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003454 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3455 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003456 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003457 ga_concat(gap, IObuff);
3458 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Shane Harper5bf04282023-06-07 19:09:57 +01003459 ga_append(gap, NUL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003461 // Output the message. Overwrite to avoid scrolling when the 'O'
3462 // flag is present in 'shortmess'; But when not jumping, print the
3463 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003464 i = msg_scroll;
3465 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3466 msg_scroll = TRUE;
3467 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3468 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003469 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003470 msg_scroll = i;
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003471
3472 qfga_clear();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003473}
3474
3475/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003476 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003477 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3478 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003479 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3480 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003481 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3482 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003483 */
3484 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003485qf_jump_open_window(
3486 qf_info_T *qi,
3487 qfline_T *qf_ptr,
3488 int newwin,
3489 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003490{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003491 qf_list_T *qfl = qf_get_curlist(qi);
3492 int old_changedtick = qfl->qf_changedtick;
3493 int old_qf_curlist = qi->qf_curlist;
3494 qfltype_T qfl_type = qfl->qfl_type;
3495
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003496 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003497 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3498 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003499 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003500 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003501 if (old_qf_curlist != qi->qf_curlist
3502 || old_changedtick != qfl->qf_changedtick
3503 || !is_qf_entry_present(qfl, qf_ptr))
3504 {
3505 if (qfl_type == QFLT_QUICKFIX)
3506 emsg(_(e_current_quickfix_list_was_changed));
3507 else
3508 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003509 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003510 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003511
3512 // If currently in the quickfix window, find another window to show the
3513 // file in.
3514 if (bt_quickfix(curbuf) && !*opened_window)
3515 {
3516 // If there is no file specified, we don't know where to go.
3517 // But do advance, otherwise ":cn" gets stuck.
3518 if (qf_ptr->qf_fnum == 0)
3519 return NOTDONE;
3520
Bram Moolenaarb2443732018-11-11 22:50:27 +01003521 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3522 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003523 return FAIL;
3524 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003525 if (old_qf_curlist != qi->qf_curlist
3526 || old_changedtick != qfl->qf_changedtick
3527 || !is_qf_entry_present(qfl, qf_ptr))
3528 {
3529 if (qfl_type == QFLT_QUICKFIX)
3530 emsg(_(e_current_quickfix_list_was_changed));
3531 else
3532 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003533 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003534 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003535
3536 return OK;
3537}
3538
3539/*
3540 * Edit a selected file from the quickfix/location list and jump to a
3541 * particular line/column, adjust the folds and display a message about the
3542 * jump.
3543 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003544 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003545 * the file.
3546 */
3547 static int
3548qf_jump_to_buffer(
3549 qf_info_T *qi,
3550 int qf_index,
3551 qfline_T *qf_ptr,
3552 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003553 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003554 int *opened_window,
3555 int openfold,
3556 int print_message)
3557{
3558 buf_T *old_curbuf;
3559 linenr_T old_lnum;
3560 int retval = OK;
3561
3562 // If there is a file name, read the wanted file if needed, and check
3563 // autowrite etc.
3564 old_curbuf = curbuf;
3565 old_lnum = curwin->w_cursor.lnum;
3566
3567 if (qf_ptr->qf_fnum != 0)
3568 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003569 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003570 opened_window);
3571 if (retval != OK)
3572 return retval;
3573 }
3574
3575 // When not switched to another buffer, still need to set pc mark
3576 if (curbuf == old_curbuf)
3577 setpcmark();
3578
3579 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3580 qf_ptr->qf_pattern);
3581
3582#ifdef FEAT_FOLDING
3583 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3584 foldOpenCursor();
3585#endif
3586 if (print_message)
3587 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3588
3589 return retval;
3590}
3591
3592/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003593 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 */
3595 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003596qf_jump(qf_info_T *qi,
3597 int dir,
3598 int errornr,
3599 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003601 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3602}
3603
3604/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003605 * Jump to a quickfix line.
3606 * If dir == 0 go to entry "errornr".
3607 * If dir == FORWARD go "errornr" valid entries forward.
3608 * If dir == BACKWARD go "errornr" valid entries backward.
3609 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3610 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3611 * else if "errornr" is zero, redisplay the same line
3612 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003613 * If 'newwin' is TRUE, then open the file in a new window.
3614 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003615 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003616qf_jump_newwin(qf_info_T *qi,
3617 int dir,
3618 int errornr,
3619 int forceit,
3620 int newwin)
3621{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003622 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003623 qfline_T *qf_ptr;
3624 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003627 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003628 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003629 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003632 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003633 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003635 if (qi == NULL)
3636 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003637
Bram Moolenaar0398e002019-03-21 21:12:49 +01003638 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003640 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 return;
3642 }
3643
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003644 incr_quickfix_busy();
3645
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003646 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003647
3648 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003650 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003652
3653 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3654 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003656 qf_ptr = old_qf_ptr;
3657 qf_index = old_qf_index;
3658 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003661 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003662 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003663 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003664 // No need to print the error message if it's visible in the error
3665 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 print_message = FALSE;
3667
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003668 prev_winid = curwin->w_id;
3669
Bram Moolenaarb2443732018-11-11 22:50:27 +01003670 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003671 if (retval == FAIL)
3672 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003673 if (retval == QF_ABORT)
3674 {
3675 qi = NULL;
3676 qf_ptr = NULL;
3677 goto theend;
3678 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003679 if (retval == NOTDONE)
3680 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003681
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003682 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003683 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003684 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003686 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003687 qi = NULL;
3688 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003691 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003694 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003695 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003697 // Couldn't open file, so put index back where it was. This could
3698 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 qf_ptr = old_qf_ptr;
3701 qf_index = old_qf_index;
3702 }
3703 }
3704theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003705 if (qi != NULL)
3706 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003707 qfl->qf_ptr = qf_ptr;
3708 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003709 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003710 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003712 // Restore old 'switchbuf' value, but not when an autocommand or
3713 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003714 p_swb = old_swb;
3715 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003717 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718}
3719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003720// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003721static int qfFileAttr;
3722static int qfSepAttr;
3723static int qfLineAttr;
3724
3725/*
3726 * Display information about a single entry from the quickfix/location list.
3727 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003728 * 'cursel' will be set to TRUE for the currently selected entry in the
3729 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003730 */
3731 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003732qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003733{
3734 char_u *fname;
3735 buf_T *buf;
3736 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003737 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003738
3739 fname = NULL;
3740 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3741 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3742 (char *)qfp->qf_module);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003743 else
3744 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003745 if (qfp->qf_fnum != 0
3746 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3747 {
Austin Chang29822992024-10-03 10:50:05 +02003748 if (qfp->qf_fname == NULL)
3749 fname = buf->b_fname;
3750 else
3751 fname = qfp->qf_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003752 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003753 fname = gettail(fname);
3754 }
3755 if (fname == NULL)
3756 sprintf((char *)IObuff, "%2d", qf_idx);
3757 else
3758 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3759 qf_idx, (char *)fname);
3760 }
3761
3762 // Support for filtering entries using :filter /pat/ clist
3763 // Match against the module name, file name, search pattern and
3764 // text of the entry.
3765 filter_entry = TRUE;
3766 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3767 filter_entry &= message_filtered(qfp->qf_module);
3768 if (filter_entry && fname != NULL)
3769 filter_entry &= message_filtered(fname);
3770 if (filter_entry && qfp->qf_pattern != NULL)
3771 filter_entry &= message_filtered(qfp->qf_pattern);
3772 if (filter_entry)
3773 filter_entry &= message_filtered(qfp->qf_text);
3774 if (filter_entry)
3775 return;
3776
3777 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003778 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003779
3780 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003781 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003782 gap = qfga_get();
Shane Harper5bf04282023-06-07 19:09:57 +01003783 if (qfp->qf_lnum != 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003784 qf_range_text(gap, qfp);
3785 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
3786 ga_append(gap, NUL);
3787 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003788 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003789 if (qfp->qf_pattern != NULL)
3790 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003791 gap = qfga_get();
3792 qf_fmt_text(gap, qfp->qf_pattern);
Shane Harper5bf04282023-06-07 19:09:57 +01003793 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003794 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003795 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003796 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003797 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003798
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003799 // Remove newlines and leading whitespace from the text. For an
3800 // unrecognized line keep the indent, the compiler may mark a word
3801 // with ^^^^.
3802 gap = qfga_get();
3803 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
Shane Harper5bf04282023-06-07 19:09:57 +01003804 ? skipwhite(qfp->qf_text) : qfp->qf_text);
3805 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003806 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003807 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003808}
3809
3810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003812 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 */
3814 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003815qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003817 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003818 qfline_T *qfp;
3819 int i;
3820 int idx1 = 1;
3821 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003822 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003823 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003824 int all = eap->forceit; // if not :cl!, only show
3825 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003826 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003828 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3829 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003830
Bram Moolenaar0398e002019-03-21 21:12:49 +01003831 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003833 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 return;
3835 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003836 if (*arg == '+')
3837 {
3838 ++arg;
3839 plus = TRUE;
3840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3842 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003843 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 return;
3845 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003846 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003847 if (plus)
3848 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003849 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003850 idx2 = i + idx1;
3851 idx1 = i;
3852 }
3853 else
3854 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003855 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003856 if (idx1 < 0)
3857 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3858 if (idx2 < 0)
3859 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003862 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003863 shorten_fnames(FALSE);
3864
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003865 // Get the attributes for the different quickfix highlight items. Note
3866 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003867 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3868 if (qfFileAttr == 0)
3869 qfFileAttr = HL_ATTR(HLF_D);
3870 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3871 if (qfSepAttr == 0)
3872 qfSepAttr = HL_ATTR(HLF_D);
3873 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3874 if (qfLineAttr == 0)
3875 qfLineAttr = HL_ATTR(HLF_N);
3876
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003877 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003879 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
3881 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003882 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 ui_breakcheck();
3885 }
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003886 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887}
3888
3889/*
3890 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003891 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
3893 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003894qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 char_u *p = text;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003897 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
3899 if (*p == '\n')
3900 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003901 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003903 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 break;
3905 }
3906 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003907 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909}
3910
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003911/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003912 * Add the range information from the lnum, col, end_lnum, and end_col values
3913 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02003914 */
3915 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003916qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02003917{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003918 char_u *buf = IObuff;
3919 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02003920 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003921
thinca6864efa2021-06-19 20:45:20 +02003922 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3923 len = (int)STRLEN(buf);
3924
3925 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3926 {
Shane Harper5bf04282023-06-07 19:09:57 +01003927 vim_snprintf((char *)buf + len, bufsize - len, "-%ld",
3928 qfp->qf_end_lnum);
thinca6864efa2021-06-19 20:45:20 +02003929 len += (int)STRLEN(buf + len);
3930 }
3931 if (qfp->qf_col > 0)
3932 {
3933 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3934 len += (int)STRLEN(buf + len);
3935 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3936 {
Shane Harper5bf04282023-06-07 19:09:57 +01003937 vim_snprintf((char *)buf + len, bufsize - len, "-%d",
3938 qfp->qf_end_col);
thinca6864efa2021-06-19 20:45:20 +02003939 len += (int)STRLEN(buf + len);
3940 }
3941 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003942
3943 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02003944}
3945
3946/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003947 * Display information (list number, list size and the title) about a
3948 * quickfix/location list.
3949 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003950 static void
3951qf_msg(qf_info_T *qi, int which, char *lead)
3952{
3953 char *title = (char *)qi->qf_lists[which].qf_title;
3954 int count = qi->qf_lists[which].qf_count;
3955 char_u buf[IOSIZE];
3956
3957 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3958 lead,
3959 which + 1,
3960 qi->qf_listcount,
3961 count);
3962
3963 if (title != NULL)
3964 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003965 size_t len = STRLEN(buf);
3966
3967 if (len < 34)
3968 {
3969 vim_memset(buf + len, ' ', 34 - len);
3970 buf[34] = NUL;
3971 }
3972 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003973 }
3974 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003975 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003976}
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978/*
3979 * ":colder [count]": Up in the quickfix stack.
3980 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003981 * ":lolder [count]": Up in the location list stack.
3982 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 */
3984 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003985qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003987 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int count;
3989
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003990 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3991 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (eap->addr_count != 0)
3994 count = eap->line2;
3995 else
3996 count = 1;
3997 while (count--)
3998 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003999 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004001 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004003 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02004004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004006 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
4008 else
4009 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004010 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004012 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02004013 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004015 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004018 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004019 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020}
4021
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004022/*
4023 * Display the information about all the quickfix/location lists in the stack
4024 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004025 void
4026qf_history(exarg_T *eap)
4027{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004028 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004029 int i;
4030
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004031 if (eap->addr_count > 0)
4032 {
4033 if (qi == NULL)
4034 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004035 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004036 return;
4037 }
4038
4039 // Jump to the specified quickfix list
4040 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
4041 {
4042 qi->qf_curlist = eap->line2 - 1;
4043 qf_msg(qi, qi->qf_curlist, "");
4044 qf_update_buffer(qi, NULL);
4045 }
4046 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004047 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004048
4049 return;
4050 }
4051
Bram Moolenaar5b69c222019-01-11 14:50:06 +01004052 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004053 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004054 else
4055 for (i = 0; i < qi->qf_listcount; ++i)
4056 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
4057}
4058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004060 * Free all the entries in the error list "idx". Note that other information
4061 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 */
4063 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004064qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004066 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004067 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01004068 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004070 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004072 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004073 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004074 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004075 {
Austin Chang29822992024-10-03 10:50:05 +02004076 vim_free(qfp->qf_fname);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004077 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004078 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004079 vim_free(qfp->qf_pattern);
Tom Praschanca6ac992023-08-11 23:26:12 +02004080 clear_tv(&qfp->qf_user_data);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004081 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004082 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01004083 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004084 // Somehow qf_count may have an incorrect value, set it to 1
4085 // to avoid crashing when it's wrong.
4086 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004087 qfl->qf_count = 1;
Christian Brabandt567cae22023-11-19 16:19:27 +01004088 else
4089 qfl->qf_start = qfpnext;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004090 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004091 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004093
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004094 qfl->qf_index = 0;
4095 qfl->qf_start = NULL;
4096 qfl->qf_last = NULL;
4097 qfl->qf_ptr = NULL;
4098 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02004099
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004100 qf_clean_dir_stack(&qfl->qf_dir_stack);
4101 qfl->qf_directory = NULL;
4102 qf_clean_dir_stack(&qfl->qf_file_stack);
4103 qfl->qf_currfile = NULL;
4104 qfl->qf_multiline = FALSE;
4105 qfl->qf_multiignore = FALSE;
4106 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107}
4108
4109/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004110 * Free error list "idx". Frees all the entries in the quickfix list,
4111 * associated context information and the title.
4112 */
4113 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004114qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004115{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004116 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004117
Bram Moolenaard23a8232018-02-10 18:45:26 +01004118 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004119 free_tv(qfl->qf_ctx);
4120 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004121 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004122 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004123 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004124}
4125
4126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 * qf_mark_adjust: adjust marks
4128 */
4129 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004130qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02004131 win_T *wp,
4132 linenr_T line1,
4133 linenr_T line2,
4134 long amount,
4135 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004137 int i;
4138 qfline_T *qfp;
4139 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004140 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004141 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004142 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
Bram Moolenaarc1542742016-07-20 21:44:37 +02004144 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004145 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004146 if (wp != NULL)
4147 {
4148 if (wp->w_llist == NULL)
4149 return;
4150 qi = wp->w_llist;
4151 }
4152
4153 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004154 {
4155 qf_list_T *qfl = qf_get_list(qi, idx);
4156
4157 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004158 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if (qfp->qf_fnum == curbuf->b_fnum)
4160 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004161 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4163 {
4164 if (amount == MAXLNUM)
4165 qfp->qf_cleared = TRUE;
4166 else
4167 qfp->qf_lnum += amount;
4168 }
4169 else if (amount_after && qfp->qf_lnum > line2)
4170 qfp->qf_lnum += amount_after;
4171 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004172 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004173
4174 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004175 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176}
4177
4178/*
4179 * Make a nice message out of the error character and the error number:
4180 * char number message
4181 * e or E 0 " error"
4182 * w or W 0 " warning"
4183 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004184 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 * 0 0 ""
4186 * other 0 " c"
4187 * e or E n " error n"
4188 * w or W n " warning n"
4189 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004190 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * 0 n " error n"
4192 * other n " c n"
4193 * 1 x "" :helpgrep
4194 */
4195 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004196qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
4198 static char_u buf[20];
4199 static char_u cc[3];
4200 char_u *p;
4201
4202 if (c == 'W' || c == 'w')
4203 p = (char_u *)" warning";
4204 else if (c == 'I' || c == 'i')
4205 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004206 else if (c == 'N' || c == 'n')
4207 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4209 p = (char_u *)" error";
4210 else if (c == 0 || c == 1)
4211 p = (char_u *)"";
4212 else
4213 {
4214 cc[0] = ' ';
4215 cc[1] = c;
4216 cc[2] = NUL;
4217 p = cc;
4218 }
4219
4220 if (nr <= 0)
4221 return p;
4222
4223 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4224 return buf;
4225}
4226
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004228 * When "split" is FALSE: Open the entry/result under the cursor.
4229 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4230 */
4231 void
4232qf_view_result(int split)
4233{
4234 qf_info_T *qi = &ql_info;
4235
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004236 if (IS_LL_WINDOW(curwin))
4237 qi = GET_LOC_LIST(curwin);
4238
Bram Moolenaar0398e002019-03-21 21:12:49 +01004239 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004240 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004241 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004242 return;
4243 }
4244
4245 if (split)
4246 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004247 // Open the selected entry in a new window
4248 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4249 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004250 return;
4251 }
4252
4253 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4254}
4255
4256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 * ":cwindow": open the quickfix window if we have errors to display,
4258 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004259 * ":lwindow": open the location list window if we have locations to display,
4260 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 */
4262 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004263ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004265 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004266 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 win_T *win;
4268
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004269 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4270 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004271
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004272 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004273
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004274 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004275 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004277 // If a quickfix window is open but we have no errors to display,
4278 // close the window. If a quickfix window is not open, then open
4279 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004280 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004281 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004282 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 {
4284 if (win != NULL)
4285 ex_cclose(eap);
4286 }
4287 else if (win == NULL)
4288 ex_copen(eap);
4289}
4290
4291/*
4292 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004293 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004296ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004298 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004299 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004301 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4302 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004304 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004305 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (win != NULL)
4307 win_close(win, FALSE);
4308}
4309
4310/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004311 * Set "w:quickfix_title" if "qi" has a title.
4312 */
4313 static void
4314qf_set_title_var(qf_list_T *qfl)
4315{
4316 if (qfl->qf_title != NULL)
4317 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4318}
4319
4320/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004321 * Goto a quickfix or location list window (if present).
4322 * Returns OK if the window is found, FAIL otherwise.
4323 */
4324 static int
4325qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4326{
4327 win_T *win;
4328
4329 win = qf_find_win(qi);
4330 if (win == NULL)
4331 return FAIL;
4332
4333 win_goto(win);
4334 if (resize)
4335 {
4336 if (vertsplit)
4337 {
4338 if (sz != win->w_width)
4339 win_setwidth(sz);
4340 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004341 else if (sz != win->w_height && win->w_height
4342 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004343 win_setheight(sz);
4344 }
4345
4346 return OK;
4347}
4348
4349/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004350 * Set options for the buffer in the quickfix or location list window.
4351 */
4352 static void
4353qf_set_cwindow_options(void)
4354{
4355 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004356 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4357 set_option_value_give_err((char_u *)"bt",
4358 0L, (char_u *)"quickfix", OPT_LOCAL);
4359 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004360 RESET_BINDING(curwin);
4361#ifdef FEAT_DIFF
4362 curwin->w_p_diff = FALSE;
4363#endif
4364#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004365 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004366 OPT_LOCAL);
4367#endif
4368}
4369
4370/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004371 * Open a new quickfix or location list window, load the quickfix buffer and
4372 * set the appropriate options for the window.
4373 * Returns FAIL if the window could not be opened.
4374 */
4375 static int
4376qf_open_new_cwindow(qf_info_T *qi, int height)
4377{
4378 buf_T *qf_buf;
4379 win_T *oldwin = curwin;
4380 tabpage_T *prevtab = curtab;
4381 int flags = 0;
4382 win_T *win;
4383
4384 qf_buf = qf_find_buf(qi);
4385
4386 // The current window becomes the previous window afterwards.
4387 win = curwin;
4388
Bram Moolenaare1004402020-10-24 20:49:43 +02004389 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004390 // Create the new quickfix window at the very bottom, except when
4391 // :belowright or :aboveleft is used.
4392 win_goto(lastwin);
4393 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004394 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004395 flags = WSP_BELOW;
4396 flags |= WSP_NEWLOC;
4397 if (win_split(height, flags) == FAIL)
4398 return FAIL; // not enough room for window
4399 RESET_BINDING(curwin);
4400
4401 if (IS_LL_STACK(qi))
4402 {
4403 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004404 // location list stack from the window 'win'.
4405 curwin->w_llist_ref = qi;
4406 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004407 }
4408
4409 if (oldwin != curwin)
4410 oldwin = NULL; // don't store info when in another window
4411 if (qf_buf != NULL)
4412 {
4413 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004414 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004415 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004416 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004417 }
4418 else
4419 {
4420 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004421 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4422 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004423 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004424
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004425 // save the number of the new buffer
4426 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004427 }
4428
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004429 // Set the options for the quickfix buffer/window (if not already done)
4430 // Do this even if the quickfix buffer was already present, as an autocmd
4431 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004432 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004433 qf_set_cwindow_options();
4434
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004435 // Only set the height when still in the same tab page and there is no
4436 // window to the side.
4437 if (curtab == prevtab && curwin->w_width == Columns)
4438 win_setheight(height);
4439 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4440 if (win_valid(win))
4441 prevwin = win;
4442
4443 return OK;
4444}
4445
4446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004448 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 */
4450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004451ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004453 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004454 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004456 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004457 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004459 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4460 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004461
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004462 incr_quickfix_busy();
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 if (eap->addr_count != 0)
4465 height = eap->line2;
4466 else
4467 height = QF_WINHEIGHT;
4468
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004469 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#ifdef FEAT_GUI
4471 need_mouse_correct = TRUE;
4472#endif
4473
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004474 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004475 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004476 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004477 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004478 if (status == FAIL)
4479 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004480 {
4481 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004482 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004485 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004486 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004487 // Save the current index here, as updating the quickfix buffer may free
4488 // the quickfix list
4489 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004490
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004491 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004492 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004494 decr_quickfix_busy();
4495
4496 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 curwin->w_cursor.col = 0;
4498 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004499 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500}
4501
4502/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004503 * Move the cursor in the quickfix window to "lnum".
4504 */
4505 static void
4506qf_win_goto(win_T *win, linenr_T lnum)
4507{
4508 win_T *old_curwin = curwin;
4509
4510 curwin = win;
4511 curbuf = win->w_buffer;
4512 curwin->w_cursor.lnum = lnum;
4513 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004514 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004515 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004516 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004517 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004518 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004519 curwin = old_curwin;
4520 curbuf = curwin->w_buffer;
4521}
4522
4523/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004524 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004525 */
4526 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004527ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004528{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004529 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004530 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004531
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004532 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4533 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004534
4535 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004536 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4537 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4538}
4539
4540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 * Return the number of the current entry (line number in the quickfix
4542 * window).
4543 */
4544 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004545qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004547 qf_info_T *qi = &ql_info;
4548
4549 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004550 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004551 qi = wp->w_llist_ref;
4552
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004553 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554}
4555
4556/*
4557 * Update the cursor position in the quickfix window to the current error.
4558 * Return TRUE if there is a quickfix window.
4559 */
4560 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004561qf_win_pos_update(
4562 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004563 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564{
4565 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004566 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004568 // Put the cursor on the current error in the quickfix window, so that
4569 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004570 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (win != NULL
4572 && qf_index <= win->w_buffer->b_ml.ml_line_count
4573 && old_qf_index != qf_index)
4574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (qf_index > old_qf_index)
4576 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004577 win->w_redraw_top = old_qf_index;
4578 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 else
4581 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004582 win->w_redraw_top = qf_index;
4583 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004585 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
4587 return win != NULL;
4588}
4589
4590/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004591 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004592 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004593 */
4594 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004595is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004596{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004597 // A window displaying the quickfix buffer will have the w_llist_ref field
4598 // set to NULL.
4599 // A window displaying a location list buffer will have the w_llist_ref
4600 // pointing to the location list.
Christian Brabandtfc682992023-09-03 20:20:52 +02004601 if (buf_valid(win->w_buffer) && bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004602 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4603 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004604 return TRUE;
4605
4606 return FALSE;
4607}
4608
4609/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004610 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4611 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004612 */
4613 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004614qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004615{
4616 win_T *win;
4617
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004618 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004619 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004620 return win;
4621 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004622}
4623
4624/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004625 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004626 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 */
4628 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004629qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004631 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004632 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004634 if (qi->qf_bufnr != INVALID_QFBUFNR)
4635 {
4636 buf_T *qfbuf;
4637 qfbuf = buflist_findnr(qi->qf_bufnr);
4638 if (qfbuf != NULL)
4639 return qfbuf;
4640 // buffer is no longer present
4641 qi->qf_bufnr = INVALID_QFBUFNR;
4642 }
4643
Bram Moolenaar9c102382006-05-03 21:26:49 +00004644 FOR_ALL_TAB_WINDOWS(tp, win)
4645 if (is_qf_win(win, qi))
4646 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004647
Bram Moolenaar9c102382006-05-03 21:26:49 +00004648 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649}
4650
4651/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004652 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004653 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004654 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004655 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004656did_set_quickfixtextfunc(optset_T *args UNUSED)
Bram Moolenaard43906d2020-07-20 21:31:32 +02004657{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004658 if (option_set_callback_func(p_qftf, &qftf_cb) == FAIL)
4659 return e_invalid_argument;
4660
4661 return NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004662}
4663
4664/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004665 * Update the w:quickfix_title variable in the quickfix/location list window in
4666 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004667 */
4668 static void
4669qf_update_win_titlevar(qf_info_T *qi)
4670{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004671 qf_list_T *qfl = qf_get_curlist(qi);
4672 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004673 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004674 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004675
Bram Moolenaar530bed92020-12-16 21:02:56 +01004676 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004677 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004678 if (is_qf_win(win, qi))
4679 {
4680 curwin = win;
4681 qf_set_title_var(qfl);
4682 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004683 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004684 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004685}
4686
4687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 * Find the quickfix buffer. If it exists, update the contents.
4689 */
4690 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004691qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004694 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004697 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004698 buf = qf_find_buf(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004699 if (buf == NULL)
4700 return;
4701
4702 linenr_T old_line_count = buf->b_ml.ml_line_count;
4703 int qf_winid = 0;
4704
4705 if (IS_LL_STACK(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004707 if (curwin->w_llist == qi)
4708 win = curwin;
4709 else
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004710 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004711 // Find the file window (non-quickfix) with this location list
4712 win = qf_find_win_with_loclist(qi);
4713 if (win == NULL)
4714 // File window is not found. Find the location list window.
4715 win = qf_find_win(qi);
4716 if (win == NULL)
4717 return;
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004718 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004719 qf_winid = win->w_id;
4720 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004721
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004722 // autocommands may cause trouble
4723 incr_quickfix_busy();
Bram Moolenaard0fab102022-10-20 16:03:33 +01004724
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004725 int do_fill = TRUE;
4726 if (old_last == NULL)
4727 {
4728 // set curwin/curbuf to buf and save a few things
4729 aucmd_prepbuf(&aco, buf);
4730 if (curbuf != buf)
4731 do_fill = FALSE; // failed to find a window for "buf"
4732 }
4733
4734 if (do_fill)
4735 {
4736 qf_update_win_titlevar(qi);
4737
4738 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
4739 ++CHANGEDTICK(buf);
4740
Bram Moolenaar864293a2016-06-02 13:40:04 +02004741 if (old_last == NULL)
4742 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004743 (void)qf_win_pos_update(qi, 0);
4744
4745 // restore curwin/curbuf and a few other things
4746 aucmd_restbuf(&aco);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004749
4750 // Only redraw when added lines are visible. This avoids flickering
4751 // when the added lines are not visible.
4752 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4753 redraw_buf_later(buf, UPD_NOT_VALID);
4754
4755 // always called after incr_quickfix_busy()
4756 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757}
4758
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004759/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004760 * Add an error line to the quickfix buffer.
4761 */
4762 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004763qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004764 buf_T *buf, // quickfix window buffer
4765 linenr_T lnum,
4766 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004767 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004768 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004769 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004770{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004771 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004772 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004773
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004774 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004775
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004776 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004777 // for this entry, then use it.
4778 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004779 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004780 ga_concat(gap, qftf_str);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004781 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004782 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004783 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004784 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004785 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004786 else if (qfp->qf_fnum != 0
4787 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4788 && errbuf->b_fname != NULL)
4789 {
4790 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004791 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02004792 else
4793 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004794 // Shorten the file name if not done already.
4795 // For optimization, do this only for the first entry in a
4796 // buffer.
4797 if (first_bufline && (errbuf->b_sfname == NULL
4798 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004799 {
4800 if (*dirname == NUL)
4801 mch_dirname(dirname, MAXPATHL);
4802 shorten_buf_fname(errbuf, dirname, FALSE);
4803 }
Austin Chang29822992024-10-03 10:50:05 +02004804 if (qfp->qf_fname == NULL)
4805 ga_concat(gap, errbuf->b_fname);
4806 else
4807 ga_concat(gap, qfp->qf_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004808 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004809 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004810
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004811 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02004812
4813 if (qfp->qf_lnum > 0)
4814 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004815 qf_range_text(gap, qfp);
4816 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004817 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004818 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004819 qf_fmt_text(gap, qfp->qf_pattern);
4820 ga_append(gap, '|');
4821 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004822
Bram Moolenaar858ba062020-05-31 23:11:59 +02004823 // Remove newlines and leading whitespace from the text.
4824 // For an unrecognized line keep the indent, the compiler may
4825 // mark a word with ^^^^.
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004826 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text)
4827 : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004828 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004829
Shane Harper5bf04282023-06-07 19:09:57 +01004830 ga_append(gap, NUL);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004831 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004832 return FAIL;
4833
4834 return OK;
4835}
4836
Bram Moolenaard43906d2020-07-20 21:31:32 +02004837/*
4838 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4839 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4840 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004841 static list_T *
4842call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4843{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004844 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004845 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004846 static int recursive = FALSE;
4847
4848 if (recursive)
4849 return NULL; // this doesn't work properly recursively
4850 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004851
4852 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4853 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4854 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004855 if (qfl->qf_qftf_cb.cb_name != NULL)
4856 cb = &qfl->qf_qftf_cb;
4857 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004858 {
4859 typval_T args[1];
4860 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004861 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004862
4863 // create the dict argument
4864 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01004865 {
4866 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004867 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004868 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004869 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4870 dict_add_number(d, "winid", (long)qf_winid);
4871 dict_add_number(d, "id", (long)qfl->qf_id);
4872 dict_add_number(d, "start_idx", start_idx);
4873 dict_add_number(d, "end_idx", end_idx);
4874 ++d->dv_refcount;
4875 args[0].v_type = VAR_DICT;
4876 args[0].vval.v_dict = d;
4877
Bram Moolenaard43906d2020-07-20 21:31:32 +02004878 qftf_list = NULL;
4879 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4880 {
4881 if (rettv.v_type == VAR_LIST)
4882 {
4883 qftf_list = rettv.vval.v_list;
4884 qftf_list->lv_refcount++;
4885 }
4886 clear_tv(&rettv);
4887 }
4888 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004889 }
4890
Bram Moolenaard6c67622022-08-24 20:07:22 +01004891 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004892 return qftf_list;
4893}
4894
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 * Fill current buffer with quickfix errors, replacing any previous contents.
4897 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004898 * If "old_last" is not NULL append the items after this one.
4899 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4900 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 */
4902 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004903qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004905 linenr_T lnum;
4906 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004907 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004908 list_T *qftf_list = NULL;
4909 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910
Bram Moolenaar864293a2016-06-02 13:40:04 +02004911 if (old_last == NULL)
4912 {
zeertzjqc7a8eb52024-05-08 20:22:40 +02004913 win_T *wp;
4914 tabpage_T *tp;
4915
Bram Moolenaar864293a2016-06-02 13:40:04 +02004916 if (buf != curbuf)
4917 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004918 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004919 return;
4920 }
4921
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004922 // delete all existing lines
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004923 //
4924 // Note: we cannot store undo information, because
4925 // qf buffer is usually not allowed to be modified.
4926 //
4927 // So we need to clean up undo information
4928 // otherwise autocommands may invalidate the undo stack
Bram Moolenaar864293a2016-06-02 13:40:04 +02004929 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004930 (void)ml_delete((linenr_T)1);
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004931
zeertzjqc7a8eb52024-05-08 20:22:40 +02004932 FOR_ALL_TAB_WINDOWS(tp, wp)
4933 if (wp->w_buffer == curbuf)
4934 wp->w_skipcol = 0;
4935
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004936 // Remove all undo information
Christian Brabandt9071ed82024-02-15 20:17:37 +01004937 u_clearallandblockfree(curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004940 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004941 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004943 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004944 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004945 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004946
4947 *dirname = NUL;
4948
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004949 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004950 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004951 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004952 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004953 lnum = 0;
4954 }
4955 else
4956 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004957 if (old_last->qf_next != NULL)
4958 qfp = old_last->qf_next;
4959 else
4960 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004961 lnum = buf->b_ml.ml_line_count;
4962 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004963
4964 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4965 (long)qfl->qf_count);
4966 if (qftf_list != NULL)
4967 qftf_li = qftf_list->lv_first;
4968
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004969 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004971 char_u *qftf_str = NULL;
4972
Bram Moolenaard43906d2020-07-20 21:31:32 +02004973 // Use the text supplied by the user defined function (if any).
4974 // If the returned value is not string, then ignore the rest
4975 // of the returned values and use the default.
4976 if (qftf_li != NULL && !invalid_val)
4977 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004978 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004979 if (qftf_str == NULL)
4980 invalid_val = TRUE;
4981 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004982
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004983 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4984 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004986
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004987 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004988 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004990 if (qfp == NULL)
4991 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004992
4993 if (qftf_li != NULL)
4994 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004996
4997 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004998 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004999 (void)ml_delete(lnum + 1);
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01005000
5001 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005004 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 check_lnums(TRUE);
5006
Bram Moolenaar864293a2016-06-02 13:40:04 +02005007 if (old_last == NULL)
5008 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005009 // Set the 'filetype' to "qf" each time after filling the buffer.
5010 // This resembles reading a file into a buffer, it's more logical when
5011 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02005012 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005013 set_option_value_give_err((char_u *)"ft",
5014 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005015 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
zeertzjq5bf6c212024-03-31 18:41:27 +02005017 curbuf->b_keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02005018 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005020 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 FALSE, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +02005022 curbuf->b_keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02005023 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005024
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005025 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005026 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005029 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 KeyTyped = old_KeyTyped;
5031}
5032
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005033/*
5034 * For every change made to the quickfix list, update the changed tick.
5035 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01005036 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005037qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01005038{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005039 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005040}
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005043 * Return the quickfix/location list number with the given identifier.
5044 * Returns -1 if list is not found.
5045 */
5046 static int
5047qf_id2nr(qf_info_T *qi, int_u qfid)
5048{
5049 int qf_idx;
5050
5051 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
5052 if (qi->qf_lists[qf_idx].qf_id == qfid)
5053 return qf_idx;
5054 return INVALID_QFIDX;
5055}
5056
5057/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005058 * If the current list is not "save_qfid" and we can find the list with that ID
5059 * then make it the current list.
5060 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005061 * Returns OK if successfully restored the list. Returns FAIL if the list with
5062 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005063 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005064 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005065qf_restore_list(qf_info_T *qi, int_u save_qfid)
5066{
5067 int curlist;
5068
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00005069 if (qf_get_curlist(qi)->qf_id == save_qfid)
5070 return OK;
5071
5072 curlist = qf_id2nr(qi, save_qfid);
5073 if (curlist < 0)
5074 // list is not present
5075 return FAIL;
5076 qi->qf_curlist = curlist;
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005077 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005078}
5079
5080/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005081 * Jump to the first entry if there is one.
5082 */
5083 static void
5084qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
5085{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005086 if (qf_restore_list(qi, save_qfid) == FAIL)
5087 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005088
Colin Kennedy21570352024-03-03 16:16:47 +01005089
5090 if (!check_can_set_curbuf_forceit(forceit))
5091 return;
5092
5093
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005094 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005095 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005096 qf_jump(qi, 0, 0, forceit);
5097}
5098
5099/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005100 * Return TRUE when using ":vimgrep" for ":grep".
5101 */
5102 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005103grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005104{
Bram Moolenaar754b5602006-02-09 23:53:20 +00005105 return ((cmdidx == CMD_grep
5106 || cmdidx == CMD_lgrep
5107 || cmdidx == CMD_grepadd
5108 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005109 && STRCMP("internal",
5110 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
5111}
5112
5113/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005114 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005116 static char_u *
5117make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005119 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005120 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005121 case CMD_make: return (char_u *)"make";
5122 case CMD_lmake: return (char_u *)"lmake";
5123 case CMD_grep: return (char_u *)"grep";
5124 case CMD_lgrep: return (char_u *)"lgrep";
5125 case CMD_grepadd: return (char_u *)"grepadd";
5126 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5127 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129}
5130
5131/*
5132 * Return the name for the errorfile, in allocated memory.
5133 * Find a new unique name when 'makeef' contains "##".
5134 * Returns NULL for error.
5135 */
5136 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01005137get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
5139 char_u *p;
5140 char_u *name;
5141 static int start = -1;
5142 static int off = 0;
5143#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02005144 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#endif
5146
5147 if (*p_mef == NUL)
5148 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005149 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005151 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 return name;
5153 }
5154
5155 for (p = p_mef; *p; ++p)
5156 if (p[0] == '#' && p[1] == '#')
5157 break;
5158
5159 if (*p == NUL)
5160 return vim_strsave(p_mef);
5161
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005162 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 for (;;)
5164 {
5165 if (start == -1)
5166 start = mch_get_pid();
5167 else
5168 off += 19;
5169
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005170 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (name == NULL)
5172 break;
5173 STRCPY(name, p_mef);
5174 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
5175 STRCAT(name, p + 2);
5176 if (mch_getperm(name) < 0
5177#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005178 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 && mch_lstat((char *)name, &sb) < 0
5180#endif
5181 )
5182 break;
5183 vim_free(name);
5184 }
5185 return name;
5186}
5187
5188/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005189 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5190 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5191 */
5192 static char_u *
5193make_get_fullcmd(char_u *makecmd, char_u *fname)
5194{
5195 char_u *cmd;
5196 unsigned len;
5197
5198 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5199 if (*p_sp != NUL)
5200 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005201 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005202 if (cmd == NULL)
5203 return NULL;
5204 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5205 (char *)p_shq);
5206
5207 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5208 if (*p_sp != NUL)
5209 append_redir(cmd, len, p_sp, fname);
5210
5211 // Display the fully formed command. Output a newline if there's something
5212 // else than the :make command that was typed (in which case the cursor is
5213 // in column 0).
5214 if (msg_col == 0)
5215 msg_didout = FALSE;
5216 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005217 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005218 msg_outtrans(cmd); // show what we are doing
5219
5220 return cmd;
5221}
5222
5223/*
5224 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5225 */
5226 void
5227ex_make(exarg_T *eap)
5228{
5229 char_u *fname;
5230 char_u *cmd;
5231 char_u *enc = NULL;
5232 win_T *wp = NULL;
5233 qf_info_T *qi = &ql_info;
5234 int res;
5235 char_u *au_name = NULL;
5236 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005237 char_u *errorformat = p_efm;
5238 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005239
5240 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5241 if (grep_internal(eap->cmdidx))
5242 {
5243 ex_vimgrep(eap);
5244 return;
5245 }
5246
5247 au_name = make_get_auname(eap->cmdidx);
5248 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5249 curbuf->b_fname, TRUE, curbuf))
5250 {
5251#ifdef FEAT_EVAL
5252 if (aborting())
5253 return;
5254#endif
5255 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005256 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005257
5258 if (is_loclist_cmd(eap->cmdidx))
5259 wp = curwin;
5260
5261 autowrite_all();
5262 fname = get_mef_name();
5263 if (fname == NULL)
5264 return;
5265 mch_remove(fname); // in case it's not unique
5266
5267 cmd = make_get_fullcmd(eap->arg, fname);
5268 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005269 {
5270 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005271 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005272 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005273
5274 // let the shell know if we are redirecting output or not
5275 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5276
5277#ifdef AMIGA
5278 out_flush();
5279 // read window status report and redraw before message
5280 (void)char_avail();
5281#endif
5282
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005283 incr_quickfix_busy();
5284
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005285 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5286 errorformat = p_gefm;
5287 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5288 newlist = FALSE;
5289
5290 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5291 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005292 if (wp != NULL)
5293 {
5294 qi = GET_LOC_LIST(wp);
5295 if (qi == NULL)
5296 goto cleanup;
5297 }
5298 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005299 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005300
5301 // Remember the current quickfix list identifier, so that we can
5302 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005303 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005304 if (au_name != NULL)
5305 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5306 curbuf->b_fname, TRUE, curbuf);
5307 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5308 // display the first error
5309 qf_jump_first(qi, save_qfid, FALSE);
5310
5311cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005312 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005313 mch_remove(fname);
5314 vim_free(fname);
5315 vim_free(cmd);
5316}
5317
5318/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005319 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005320 */
5321 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005322qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005323{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005324 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005325
5326 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5327 return 0;
5328 return qf_get_curlist(qi)->qf_count;
5329}
5330
5331/*
5332 * Returns the number of valid entries in the current quickfix/location list.
5333 */
5334 int
5335qf_get_valid_size(exarg_T *eap)
5336{
5337 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005338 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005339 qfline_T *qfp;
5340 int i, sz = 0;
5341 int prev_fnum = 0;
5342
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005343 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5344 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005345
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005346 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005347 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005348 {
5349 if (qfp->qf_valid)
5350 {
5351 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005352 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005353 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5354 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005355 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005356 sz++;
5357 prev_fnum = qfp->qf_fnum;
5358 }
5359 }
5360 }
5361
5362 return sz;
5363}
5364
5365/*
5366 * Returns the current index of the quickfix/location list.
5367 * Returns 0 if there is an error.
5368 */
5369 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005370qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005371{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005372 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005373
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005374 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5375 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005376
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005377 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005378}
5379
5380/*
5381 * Returns the current index in the quickfix/location list (counting only valid
5382 * entries). If no valid entries are in the list, then returns 1.
5383 */
5384 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005385qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005386{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005387 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005388 qf_list_T *qfl;
5389 qfline_T *qfp;
5390 int i, eidx = 0;
5391 int prev_fnum = 0;
5392
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005393 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5394 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005395
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005396 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005397 qfp = qfl->qf_start;
5398
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005399 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005400 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005401 return 1;
5402
5403 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5404 {
5405 if (qfp->qf_valid)
5406 {
5407 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5408 {
5409 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5410 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005411 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005412 eidx++;
5413 prev_fnum = qfp->qf_fnum;
5414 }
5415 }
5416 else
5417 eidx++;
5418 }
5419 }
5420
5421 return eidx ? eidx : 1;
5422}
5423
5424/*
5425 * Get the 'n'th valid error entry in the quickfix or location list.
5426 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5427 * For :cdo and :ldo returns the 'n'th valid error entry.
5428 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5429 */
5430 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005431qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005432{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005433 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005434 int i, eidx;
5435 int prev_fnum = 0;
5436
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005437 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005438 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005439 return 1;
5440
Bram Moolenaar95946f12019-03-31 15:31:59 +02005441 eidx = 0;
5442 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005443 {
5444 if (qfp->qf_valid)
5445 {
5446 if (fdo)
5447 {
5448 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5449 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005450 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005451 eidx++;
5452 prev_fnum = qfp->qf_fnum;
5453 }
5454 }
5455 else
5456 eidx++;
5457 }
5458
5459 if (eidx == n)
5460 break;
5461 }
5462
5463 if (i <= qfl->qf_count)
5464 return i;
5465 else
5466 return 1;
5467}
5468
5469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005471 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005472 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 */
5474 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005475ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005477 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005478 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005479
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005480 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5481 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005482
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005483 if (eap->addr_count > 0)
5484 errornr = (int)eap->line2;
5485 else
5486 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005487 switch (eap->cmdidx)
5488 {
5489 case CMD_cc: case CMD_ll:
5490 errornr = 0;
5491 break;
5492 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5493 case CMD_lfirst:
5494 errornr = 1;
5495 break;
5496 default:
5497 errornr = 32767;
5498 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005499 }
5500
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005501 // For cdo and ldo commands, jump to the nth valid error.
5502 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005503 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5504 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005505 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005506 eap->addr_count > 0 ? (int)eap->line1 : 1,
5507 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5508
5509 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510}
5511
5512/*
5513 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005514 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005515 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 */
5517 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005518ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005520 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005521 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005522 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005523
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005524 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5525 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005526
Bram Moolenaar55b69262017-08-13 13:42:01 +02005527 if (eap->addr_count > 0
5528 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5529 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005530 errornr = (int)eap->line2;
5531 else
5532 errornr = 1;
5533
Bram Moolenaar39665952018-08-15 20:59:48 +02005534 // Depending on the command jump to either next or previous entry/file.
5535 switch (eap->cmdidx)
5536 {
5537 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5538 dir = FORWARD;
5539 break;
5540 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5541 case CMD_lNext:
5542 dir = BACKWARD;
5543 break;
5544 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5545 dir = FORWARD_FILE;
5546 break;
5547 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5548 dir = BACKWARD_FILE;
5549 break;
5550 default:
5551 dir = FORWARD;
5552 break;
5553 }
5554
5555 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556}
5557
5558/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005559 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5560 * The index of the entry is stored in 'errornr'.
5561 * Returns NULL if an entry is not found.
5562 */
5563 static qfline_T *
5564qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5565{
5566 qfline_T *qfp = NULL;
5567 int idx = 0;
5568
5569 // Find the first entry in this file
5570 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5571 if (qfp->qf_fnum == bnr)
5572 break;
5573
5574 *errornr = idx;
5575 return qfp;
5576}
5577
5578/*
5579 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5580 * with the error number for the first entry. Assumes the entries are sorted in
5581 * the quickfix list by line number.
5582 */
5583 static qfline_T *
5584qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5585{
5586 while (!got_int
5587 && entry->qf_prev != NULL
5588 && entry->qf_fnum == entry->qf_prev->qf_fnum
5589 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5590 {
5591 entry = entry->qf_prev;
5592 --*errornr;
5593 }
5594
5595 return entry;
5596}
5597
5598/*
5599 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5600 * with the error number for the last entry. Assumes the entries are sorted in
5601 * the quickfix list by line number.
5602 */
5603 static qfline_T *
5604qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5605{
5606 while (!got_int &&
5607 entry->qf_next != NULL
5608 && entry->qf_fnum == entry->qf_next->qf_fnum
5609 && entry->qf_lnum == entry->qf_next->qf_lnum)
5610 {
5611 entry = entry->qf_next;
5612 ++*errornr;
5613 }
5614
5615 return entry;
5616}
5617
5618/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005619 * Returns TRUE if the specified quickfix entry is
5620 * after the given line (linewise is TRUE)
5621 * or after the line and column.
5622 */
5623 static int
5624qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5625{
5626 if (linewise)
5627 return qfp->qf_lnum > pos->lnum;
5628 else
5629 return (qfp->qf_lnum > pos->lnum ||
5630 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5631}
5632
5633/*
5634 * Returns TRUE if the specified quickfix entry is
5635 * before the given line (linewise is TRUE)
5636 * or before the line and column.
5637 */
5638 static int
5639qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5640{
5641 if (linewise)
5642 return qfp->qf_lnum < pos->lnum;
5643 else
5644 return (qfp->qf_lnum < pos->lnum ||
5645 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5646}
5647
5648/*
5649 * Returns TRUE if the specified quickfix entry is
5650 * on or after the given line (linewise is TRUE)
5651 * or on or after the line and column.
5652 */
5653 static int
5654qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5655{
5656 if (linewise)
5657 return qfp->qf_lnum >= pos->lnum;
5658 else
5659 return (qfp->qf_lnum > pos->lnum ||
5660 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5661}
5662
5663/*
5664 * Returns TRUE if the specified quickfix entry is
5665 * on or before the given line (linewise is TRUE)
5666 * or on or before the line and column.
5667 */
5668 static int
5669qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5670{
5671 if (linewise)
5672 return qfp->qf_lnum <= pos->lnum;
5673 else
5674 return (qfp->qf_lnum < pos->lnum ||
5675 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5676}
5677
5678/*
5679 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5680 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5681 * multiple entries on a single line as one. Otherwise returns the entry after
5682 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005683 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5684 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005685 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005686 */
5687 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005688qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005689 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005690 pos_T *pos,
5691 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005692 qfline_T *qfp,
5693 int *errornr)
5694{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005695 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005696 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005697 return qfp;
5698
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005699 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005700 while (qfp->qf_next != NULL
5701 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005702 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005703 {
5704 qfp = qfp->qf_next;
5705 ++*errornr;
5706 }
5707
5708 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005709 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005710 return NULL;
5711
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005712 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005713 qfp = qfp->qf_next;
5714 ++*errornr;
5715
5716 return qfp;
5717}
5718
5719/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005720 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5721 * If 'linewise' is TRUE, returns the entry before the specified line and
5722 * treats multiple entries on a single line as one. Otherwise returns the entry
5723 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005724 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5725 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005726 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005727 */
5728 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005729qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005730 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005731 pos_T *pos,
5732 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005733 qfline_T *qfp,
5734 int *errornr)
5735{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005736 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005737 while (qfp->qf_next != NULL
5738 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005739 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005740 {
5741 qfp = qfp->qf_next;
5742 ++*errornr;
5743 }
5744
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005745 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005746 return NULL;
5747
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005748 if (linewise)
5749 // If multiple entries are on the same line, then use the first entry
5750 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005751
5752 return qfp;
5753}
5754
5755/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005756 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005757 * the direction 'dir'.
5758 */
5759 static qfline_T *
5760qf_find_closest_entry(
5761 qf_list_T *qfl,
5762 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005763 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005764 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005765 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005766 int *errornr)
5767{
5768 qfline_T *qfp;
5769
5770 *errornr = 0;
5771
5772 // Find the first entry in this file
5773 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5774 if (qfp == NULL)
5775 return NULL; // no entry in this file
5776
5777 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005778 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005779 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005780 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005781
5782 return qfp;
5783}
5784
5785/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005786 * Get the nth quickfix entry below the specified entry. Searches forward in
5787 * the list. If linewise is TRUE, then treat multiple entries on a single line
5788 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005789 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005790 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005791qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005792{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005793 qfline_T *entry = entry_arg;
5794
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005795 while (n-- > 0 && !got_int)
5796 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005797 int first_errornr = *errornr;
5798
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005799 if (linewise)
5800 // Treat all the entries on the same line in this file as one
5801 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005802
5803 if (entry->qf_next == NULL
5804 || entry->qf_next->qf_fnum != entry->qf_fnum)
5805 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005806 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005807 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005808 break;
5809 }
5810
5811 entry = entry->qf_next;
5812 ++*errornr;
5813 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005814}
5815
5816/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005817 * Get the nth quickfix entry above the specified entry. Searches backwards in
5818 * the list. If linewise is TRUE, then treat multiple entries on a single line
5819 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005820 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005821 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005822qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005823{
5824 while (n-- > 0 && !got_int)
5825 {
5826 if (entry->qf_prev == NULL
5827 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5828 break;
5829
5830 entry = entry->qf_prev;
5831 --*errornr;
5832
5833 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005834 if (linewise)
5835 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005836 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005837}
5838
5839/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005840 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5841 * the specified direction. Returns the error number in the quickfix list or 0
5842 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005843 */
5844 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005845qf_find_nth_adj_entry(
5846 qf_list_T *qfl,
5847 int bnr,
5848 pos_T *pos,
5849 int n,
5850 int dir,
5851 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005852{
5853 qfline_T *adj_entry;
5854 int errornr;
5855
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005856 // Find an entry closest to the specified position
5857 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005858 if (adj_entry == NULL)
5859 return 0;
5860
5861 if (--n > 0)
5862 {
5863 // Go to the n'th entry in the current buffer
5864 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005865 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005866 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005867 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005868 }
5869
5870 return errornr;
5871}
5872
5873/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005874 * Jump to a quickfix entry in the current file nearest to the current line or
5875 * current line/col.
5876 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5877 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005878 */
5879 void
5880ex_cbelow(exarg_T *eap)
5881{
5882 qf_info_T *qi;
5883 qf_list_T *qfl;
5884 int dir;
5885 int buf_has_flag;
5886 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005887 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005888
5889 if (eap->addr_count > 0 && eap->line2 <= 0)
5890 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005891 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005892 return;
5893 }
5894
5895 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005896 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5897 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005898 buf_has_flag = BUF_HAS_QF_ENTRY;
5899 else
5900 buf_has_flag = BUF_HAS_LL_ENTRY;
5901 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5902 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005903 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005904 return;
5905 }
5906
5907 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5908 return;
5909
5910 qfl = qf_get_curlist(qi);
5911 // check if the list has valid errors
5912 if (!qf_list_has_valid_entries(qfl))
5913 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005914 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005915 return;
5916 }
5917
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005918 if (eap->cmdidx == CMD_cbelow
5919 || eap->cmdidx == CMD_lbelow
5920 || eap->cmdidx == CMD_cafter
5921 || eap->cmdidx == CMD_lafter)
5922 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005923 dir = FORWARD;
5924 else
5925 dir = BACKWARD;
5926
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005927 pos = curwin->w_cursor;
5928 // A quickfix entry column number is 1 based whereas cursor column
5929 // number is 0 based. Adjust the column number.
5930 pos.col++;
5931 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5932 eap->addr_count > 0 ? eap->line2 : 0, dir,
5933 eap->cmdidx == CMD_cbelow
5934 || eap->cmdidx == CMD_lbelow
5935 || eap->cmdidx == CMD_cabove
5936 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005937
5938 if (errornr > 0)
5939 qf_jump(qi, 0, errornr, FALSE);
5940 else
5941 emsg(_(e_no_more_items));
5942}
5943
5944/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005945 * Return the autocmd name for the :cfile Ex commands
5946 */
5947 static char_u *
5948cfile_get_auname(cmdidx_T cmdidx)
5949{
5950 switch (cmdidx)
5951 {
5952 case CMD_cfile: return (char_u *)"cfile";
5953 case CMD_cgetfile: return (char_u *)"cgetfile";
5954 case CMD_caddfile: return (char_u *)"caddfile";
5955 case CMD_lfile: return (char_u *)"lfile";
5956 case CMD_lgetfile: return (char_u *)"lgetfile";
5957 case CMD_laddfile: return (char_u *)"laddfile";
5958 default: return NULL;
5959 }
5960}
5961
5962/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005963 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005964 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 */
5966 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005967ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005969 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005970 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005971 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005972 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005973 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005974 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005975
Bram Moolenaara16123a2019-03-28 20:31:07 +01005976 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005977 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5978 NULL, FALSE, curbuf))
5979 {
5980#ifdef FEAT_EVAL
5981 if (aborting())
5982 return;
5983#endif
5984 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005985
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005986 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005987#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005988 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005989 {
5990 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005991 NULL, NULL,
5992 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005993 if (browse_file == NULL)
5994 return;
5995 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5996 vim_free(browse_file);
5997 }
5998 else
5999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006001 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006002
Bram Moolenaar39665952018-08-15 20:59:48 +02006003 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01006004 wp = curwin;
6005
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006006 incr_quickfix_busy();
6007
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006008 // This function is used by the :cfile, :cgetfile and :caddfile
6009 // commands.
Colin Kennedy21570352024-03-03 16:16:47 +01006010 // :cfile always creates a new quickfix list and may jump to the
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006011 // first error.
6012 // :cgetfile creates a new quickfix list but doesn't jump to the
6013 // first error.
6014 // :caddfile adds to an existing quickfix list. If there is no
6015 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006016 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006017 && eap->cmdidx != CMD_laddfile),
6018 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006019 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006020 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01006021 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006022 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006023 {
6024 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006025 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006026 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006027 }
6028 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006029 qf_list_changed(qf_get_curlist(qi));
6030 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006031 if (au_name != NULL)
6032 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01006033
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006034 // Jump to the first error for a new list and if autocmds didn't
6035 // free the list.
6036 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
6037 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006038 // display the first error
6039 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006040
6041 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006042}
6043
6044/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006045 * Return the vimgrep autocmd name.
6046 */
6047 static char_u *
6048vgr_get_auname(cmdidx_T cmdidx)
6049{
6050 switch (cmdidx)
6051 {
6052 case CMD_vimgrep: return (char_u *)"vimgrep";
6053 case CMD_lvimgrep: return (char_u *)"lvimgrep";
6054 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
6055 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
6056 case CMD_grep: return (char_u *)"grep";
6057 case CMD_lgrep: return (char_u *)"lgrep";
6058 case CMD_grepadd: return (char_u *)"grepadd";
6059 case CMD_lgrepadd: return (char_u *)"lgrepadd";
6060 default: return NULL;
6061 }
6062}
6063
6064/*
6065 * Initialize the regmatch used by vimgrep for pattern "s".
6066 */
6067 static void
6068vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
6069{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006070 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006071 regmatch->regprog = NULL;
6072
6073 if (s == NULL || *s == NUL)
6074 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006075 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006076 if (last_search_pat() == NULL)
6077 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006078 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006079 return;
6080 }
6081 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
6082 }
6083 else
6084 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
6085
6086 regmatch->rmm_ic = p_ic;
6087 regmatch->rmm_maxcol = 0;
6088}
6089
6090/*
6091 * Display a file name when vimgrep is running.
6092 */
6093 static void
6094vgr_display_fname(char_u *fname)
6095{
6096 char_u *p;
6097
6098 msg_start();
6099 p = msg_strtrunc(fname, TRUE);
6100 if (p == NULL)
6101 msg_outtrans(fname);
6102 else
6103 {
6104 msg_outtrans(p);
6105 vim_free(p);
6106 }
6107 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006108 msg_didout = FALSE; // overwrite this message
6109 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006110 msg_col = 0;
6111 out_flush();
6112}
6113
6114/*
6115 * Load a dummy buffer to search for a pattern using vimgrep.
6116 */
6117 static buf_T *
6118vgr_load_dummy_buf(
6119 char_u *fname,
6120 char_u *dirname_start,
6121 char_u *dirname_now)
6122{
6123 int save_mls;
6124#if defined(FEAT_SYN_HL)
6125 char_u *save_ei = NULL;
6126#endif
6127 buf_T *buf;
6128
6129#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006130 // Don't do Filetype autocommands to avoid loading syntax and
6131 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006132 save_ei = au_event_disable(",Filetype");
6133#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006134 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006135 save_mls = p_mls;
6136 p_mls = 0;
6137
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006138 // Load file into a buffer, so that 'fileencoding' is detected,
6139 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006140 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
6141
6142 p_mls = save_mls;
6143#if defined(FEAT_SYN_HL)
6144 au_event_restore(save_ei);
6145#endif
6146
6147 return buf;
6148}
6149
6150/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006151 * Check whether a quickfix/location list is valid. Autocmds may remove or
6152 * change a quickfix list when vimgrep is running. If the list is not found,
6153 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006154 */
6155 static int
6156vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006157 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006158 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006159 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006160 char_u *title)
6161{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006162 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006163 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006164 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006165 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006166 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006167 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02006168 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006169 return FALSE;
6170 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006171 else
6172 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006173 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006174 qf_new_list(qi, title);
6175 return TRUE;
6176 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006177 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006178
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02006179 if (qf_restore_list(qi, qfid) == FAIL)
6180 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006181
6182 return TRUE;
6183}
6184
6185/*
6186 * Search for a pattern in all the lines in a buffer and add the matching lines
6187 * to a quickfix list.
6188 */
6189 static int
6190vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006191 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006192 char_u *fname,
6193 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006194 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006195 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006196 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006197 int duplicate_name,
6198 int flags)
6199{
6200 int found_match = FALSE;
6201 long lnum;
6202 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006203 int pat_len = (int)STRLEN(spat);
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006204 if (pat_len > MAX_FUZZY_MATCHES)
6205 pat_len = MAX_FUZZY_MATCHES;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006206
Bram Moolenaar1c299432018-10-28 14:36:09 +01006207 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006208 {
6209 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006210 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006211 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006212 // Regular expression match
6213 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006214 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006215 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006216 // Pass the buffer number so that it gets used even for a
6217 // dummy buffer, unless duplicate_name is set, then the
6218 // buffer will be wiped out below.
6219 if (qf_add_entry(qfl,
6220 NULL, // dir
6221 fname,
6222 NULL,
6223 duplicate_name ? 0 : buf->b_fnum,
6224 ml_get_buf(buf,
6225 regmatch->startpos[0].lnum + lnum, FALSE),
6226 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006227 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006228 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006229 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006230 FALSE, // vis_col
6231 NULL, // search pattern
6232 0, // nr
6233 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006234 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006235 TRUE // valid
6236 ) == QF_FAIL)
6237 {
6238 got_int = TRUE;
6239 break;
6240 }
6241 found_match = TRUE;
6242 if (--*tomatch == 0)
6243 break;
6244 if ((flags & VGR_GLOBAL) == 0
6245 || regmatch->endpos[0].lnum > 0)
6246 break;
6247 col = regmatch->endpos[0].col
6248 + (col == regmatch->endpos[0].col);
zeertzjq94b7c322024-03-12 21:50:32 +01006249 if (col > ml_get_buf_len(buf, lnum))
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006250 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006251 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006252 }
6253 else
6254 {
6255 char_u *str = ml_get_buf(buf, lnum, FALSE);
zeertzjq8c55d602024-03-13 20:42:26 +01006256 colnr_T linelen = ml_get_buf_len(buf, lnum);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006257 int score;
6258 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006259 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006260
6261 // Fuzzy string match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006262 CLEAR_FIELD(matches);
glepnir28e40a72025-03-16 21:24:22 +01006263 while (fuzzy_match(str + col, spat, FALSE, &score,
6264 matches, sz, TRUE) > 0)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006265 {
6266 // Pass the buffer number so that it gets used even for a
6267 // dummy buffer, unless duplicate_name is set, then the
6268 // buffer will be wiped out below.
6269 if (qf_add_entry(qfl,
6270 NULL, // dir
6271 fname,
6272 NULL,
6273 duplicate_name ? 0 : buf->b_fnum,
6274 str,
6275 lnum,
thinca6864efa2021-06-19 20:45:20 +02006276 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006277 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006278 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006279 FALSE, // vis_col
6280 NULL, // search pattern
6281 0, // nr
6282 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006283 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006284 TRUE // valid
6285 ) == QF_FAIL)
6286 {
6287 got_int = TRUE;
6288 break;
6289 }
6290 found_match = TRUE;
6291 if (--*tomatch == 0)
6292 break;
6293 if ((flags & VGR_GLOBAL) == 0)
6294 break;
6295 col = matches[pat_len - 1] + col + 1;
zeertzjq8c55d602024-03-13 20:42:26 +01006296 if (col > linelen)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006297 break;
6298 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006299 }
6300 line_breakcheck();
6301 if (got_int)
6302 break;
6303 }
6304
6305 return found_match;
6306}
6307
6308/*
6309 * Jump to the first match and update the directory.
6310 */
6311 static void
6312vgr_jump_to_match(
6313 qf_info_T *qi,
6314 int forceit,
6315 int *redraw_for_dummy,
6316 buf_T *first_match_buf,
6317 char_u *target_dir)
6318{
6319 buf_T *buf;
6320
6321 buf = curbuf;
6322 qf_jump(qi, 0, 0, forceit);
6323 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006324 // If we jumped to another buffer redrawing will already be
6325 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006326 *redraw_for_dummy = FALSE;
6327
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006328 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006329 if (curbuf == first_match_buf && target_dir != NULL)
6330 {
6331 exarg_T ea;
6332
Bram Moolenaara80faa82020-04-12 19:37:17 +02006333 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006334 ea.arg = target_dir;
6335 ea.cmdidx = CMD_lcd;
6336 ex_cd(&ea);
6337 }
6338}
6339
6340/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006341 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006342 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006343typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006344{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006345 long tomatch; // maximum number of matches to find
6346 char_u *spat; // search pattern
6347 int flags; // search modifier
6348 char_u **fnames; // list of files to search
6349 int fcount; // number of files
6350 regmmatch_T regmatch; // compiled search pattern
6351 char_u *qf_title; // quickfix list title
6352} vgr_args_T;
6353
6354/*
6355 * Process :vimgrep command arguments. The command syntax is:
6356 *
6357 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6358 */
6359 static int
6360vgr_process_args(
6361 exarg_T *eap,
6362 vgr_args_T *args)
6363{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006364 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006365
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00006366 CLEAR_POINTER(args);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006367
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006368 args->regmatch.regprog = NULL;
6369 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006370
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006371 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006372 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006373 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006374 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006375
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006376 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006377 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006378 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006379 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006380 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006381 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006382 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006383
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006384 vgr_init_regmatch(&args->regmatch, args->spat);
6385 if (args->regmatch.regprog == NULL)
6386 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006387
6388 p = skipwhite(p);
6389 if (*p == NUL)
6390 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006391 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006392 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006393 }
6394
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006395 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006396 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6397 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006398 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006399 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006400 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006401 }
6402
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006403 return OK;
6404}
6405
6406/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006407 * Return TRUE if "buf" had an existing swap file, the current swap file does
6408 * not end in ".swp".
6409 */
6410 static int
6411existing_swapfile(buf_T *buf)
6412{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006413 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006414 {
6415 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6416 size_t len = STRLEN(fname);
6417
6418 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6419 }
6420 return FALSE;
6421}
6422
6423/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006424 * Search for a pattern in a list of files and populate the quickfix list with
6425 * the matches.
6426 */
6427 static int
6428vgr_process_files(
6429 win_T *wp,
6430 qf_info_T *qi,
6431 vgr_args_T *cmd_args,
6432 int *redraw_for_dummy,
6433 buf_T **first_match_buf,
6434 char_u **target_dir)
6435{
6436 int status = FAIL;
6437 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6438 time_t seconds = 0;
6439 char_u *fname;
6440 int fi;
6441 buf_T *buf;
6442 int duplicate_name = FALSE;
6443 int using_dummy;
6444 char_u *dirname_start = NULL;
6445 char_u *dirname_now = NULL;
6446 int found_match;
6447 aco_save_T aco;
6448
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006449 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6450 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006451 if (dirname_start == NULL || dirname_now == NULL)
6452 goto theend;
6453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006454 // Remember the current directory, because a BufRead autocommand that does
6455 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006456 mch_dirname(dirname_start, MAXPATHL);
6457
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006458 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006459 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6460 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006461 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006462 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006463 if (time(NULL) > seconds)
6464 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006465 // Display the file name every second or so, show the user we are
6466 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006467 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006468 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006469 }
6470
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006471 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006472 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6473 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006474 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006475 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006476 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006477 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006478
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006479 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006480 }
6481 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006482 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006483 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006485 // Check whether the quickfix list is still valid. When loading a
6486 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006487 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006488 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006489
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006490 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006491
Bram Moolenaar81695252004-12-29 20:58:21 +00006492 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006493 {
6494 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006495 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006496 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006497 else
6498 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006499 // Try for a match in all lines of the buffer.
6500 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006501 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006502 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006503 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006504
Bram Moolenaar81695252004-12-29 20:58:21 +00006505 if (using_dummy)
6506 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006507 if (found_match && *first_match_buf == NULL)
6508 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006509 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006510 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006511 // Never keep a dummy buffer if there is another buffer
6512 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006513 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006514 buf = NULL;
6515 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006516 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006517 || buf->b_p_bh[0] == 'u' // "unload"
6518 || buf->b_p_bh[0] == 'w' // "wipe"
6519 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006520 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006521 // When no match was found we don't need to remember the
6522 // buffer, wipe it out. If there was a match and it
6523 // wasn't the first one or we won't jump there: only
6524 // unload the buffer.
6525 // Ignore 'hidden' here, because it may lead to having too
6526 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006527 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006528 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006529 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006530 buf = NULL;
6531 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006532 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006533 || (cmd_args->flags & VGR_NOJUMP)
6534 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006535 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006536 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006537 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006538 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006539 buf = NULL;
6540 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006541 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006542
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006543 if (buf != NULL)
6544 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006545 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006546 buf->b_flags &= ~BF_DUMMY;
6547
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006548 // If the buffer is still loaded we need to use the
6549 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006550 if (buf == *first_match_buf
6551 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006552 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006553 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006554
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006555 // The buffer is still loaded, the Filetype autocommands
6556 // need to be done now, in that buffer. And the modelines
6557 // need to be done (again). But not the window-local
6558 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006559 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006560 if (curbuf == buf)
6561 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006562#if defined(FEAT_SYN_HL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00006563 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006564 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006565#endif
Bram Moolenaare76062c2022-11-28 18:51:43 +00006566 do_modelines(OPT_NOWIN);
6567 aucmd_restbuf(&aco);
6568 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006569 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006570 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006571 }
6572 }
6573
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006574 status = OK;
6575
6576theend:
6577 vim_free(dirname_now);
6578 vim_free(dirname_start);
6579 return status;
6580}
6581
6582/*
6583 * ":vimgrep {pattern} file(s)"
6584 * ":vimgrepadd {pattern} file(s)"
6585 * ":lvimgrep {pattern} file(s)"
6586 * ":lvimgrepadd {pattern} file(s)"
6587 */
6588 void
6589ex_vimgrep(exarg_T *eap)
6590{
6591 vgr_args_T args;
6592 qf_info_T *qi;
6593 qf_list_T *qfl;
6594 int_u save_qfid;
6595 win_T *wp = NULL;
6596 int redraw_for_dummy = FALSE;
6597 buf_T *first_match_buf = NULL;
6598 char_u *target_dir = NULL;
6599 char_u *au_name = NULL;
6600 int status;
6601
Colin Kennedy21570352024-03-03 16:16:47 +01006602 if (!check_can_set_curbuf_forceit(eap->forceit))
6603 return;
6604
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006605 au_name = vgr_get_auname(eap->cmdidx);
6606 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6607 curbuf->b_fname, TRUE, curbuf))
6608 {
6609#ifdef FEAT_EVAL
6610 if (aborting())
6611 return;
6612#endif
6613 }
6614
6615 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6616 if (qi == NULL)
6617 return;
6618
6619 if (vgr_process_args(eap, &args) == FAIL)
6620 goto theend;
6621
6622 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6623 && eap->cmdidx != CMD_vimgrepadd
6624 && eap->cmdidx != CMD_lvimgrepadd)
6625 || qf_stack_empty(qi))
6626 // make place for a new list
6627 qf_new_list(qi, args.qf_title);
6628
6629 incr_quickfix_busy();
6630
6631 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6632 &first_match_buf, &target_dir);
6633 if (status != OK)
6634 {
6635 FreeWild(args.fcount, args.fnames);
6636 decr_quickfix_busy();
6637 goto theend;
6638 }
6639
6640 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006641
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006642 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006643 qfl->qf_nonevalid = FALSE;
6644 qfl->qf_ptr = qfl->qf_start;
6645 qfl->qf_index = 1;
6646 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006647
Bram Moolenaar864293a2016-06-02 13:40:04 +02006648 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006649
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006650 // Remember the current quickfix list identifier, so that we can check for
6651 // autocommands changing the current quickfix list.
6652 save_qfid = qf_get_curlist(qi)->qf_id;
6653
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006654 if (au_name != NULL)
6655 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6656 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006657 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6658 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006659 if (!qflist_valid(wp, save_qfid)
6660 || qf_restore_list(qi, save_qfid) == FAIL)
6661 {
6662 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006663 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006664 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006665
zeertzjq8c55d602024-03-13 20:42:26 +01006666 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006667 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006668 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006669 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006670 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6671 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006672 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006673 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006674 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006675
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006676 decr_quickfix_busy();
6677
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006678 // If we loaded a dummy buffer into the current window, the autocommands
6679 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006680 if (redraw_for_dummy)
6681 {
6682#ifdef FEAT_FOLDING
6683 foldUpdateAll(curwin);
6684#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006685 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006686#endif
6687 }
6688
Bram Moolenaar86b68352004-12-27 21:59:20 +00006689theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006690 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006691 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006692 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006693}
6694
6695/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006696 * Restore current working directory to "dirname_start" if they differ, taking
6697 * into account whether it is set locally or globally.
6698 */
6699 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006700restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006701{
6702 char_u *dirname_now = alloc(MAXPATHL);
6703
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006704 if (dirname_now == NULL)
6705 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006706
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006707 mch_dirname(dirname_now, MAXPATHL);
6708 if (STRCMP(dirname_start, dirname_now) != 0)
6709 {
6710 // If the directory has changed, change it back by building up an
6711 // appropriate ex command and executing it.
6712 exarg_T ea;
6713
6714 CLEAR_FIELD(ea);
6715 ea.arg = dirname_start;
6716 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6717 ex_cd(&ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006718 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006719 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006720}
6721
6722/*
6723 * Load file "fname" into a dummy buffer and return the buffer pointer,
6724 * placing the directory resulting from the buffer load into the
6725 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6726 * prior to calling this function. Restores directory to "dirname_start" prior
6727 * to returning, if autocmds or the 'autochdir' option have changed it.
6728 *
6729 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6730 * or wipe_dummy_buffer() later!
6731 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006732 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006733 */
6734 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006735load_dummy_buffer(
6736 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006737 char_u *dirname_start, // in: old directory
6738 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006739{
6740 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006741 bufref_T newbufref;
6742 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006743 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006744 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006745 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006746
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006747 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006748 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6749 if (newbuf == NULL)
6750 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006751 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006752
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006753 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006754 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6755
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006756 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006757 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006758 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006759 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006760 ++newbuf->b_locked;
6761
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006762 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006763 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006764 if (curbuf == newbuf)
Bram Moolenaar81695252004-12-29 20:58:21 +00006765 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006766 // Need to set the filename for autocommands.
6767 (void)setfname(curbuf, fname, NULL, FALSE);
6768
6769 // Create swap file now to avoid the ATTENTION message.
6770 check_need_swap(TRUE);
6771
6772 // Remove the "dummy" flag, otherwise autocommands may not
6773 // work.
6774 curbuf->b_flags &= ~BF_DUMMY;
6775
6776 newbuf_to_wipe.br_buf = NULL;
6777 readfile_result = readfile(fname, NULL,
6778 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6779 NULL, READ_NEW | READ_DUMMY);
6780 --newbuf->b_locked;
6781 if (readfile_result == OK
6782 && !got_int
6783 && !(curbuf->b_flags & BF_NEW))
Bram Moolenaar81695252004-12-29 20:58:21 +00006784 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006785 failed = FALSE;
6786 if (curbuf != newbuf)
6787 {
6788 // Bloody autocommands changed the buffer! Can happen when
6789 // using netrw and editing a remote file. Use the current
6790 // buffer instead, delete the dummy one after restoring the
6791 // window stuff.
6792 set_bufref(&newbuf_to_wipe, newbuf);
6793 newbuf = curbuf;
6794 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006795 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00006796
6797 // restore curwin/curbuf and a few other things
6798 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00006799
Bram Moolenaar84497cd2022-11-28 20:34:52 +00006800 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6801 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
6802 }
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006803
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006804 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6805 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006806 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006807 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006808
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006809 // When autocommands/'autochdir' option changed directory: go back.
6810 // Let the caller know what the resulting dir was first, in case it is
6811 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006812 mch_dirname(resulting_dir, MAXPATHL);
6813 restore_start_dir(dirname_start);
6814
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006815 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006816 return NULL;
6817 if (failed)
6818 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006819 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006820 return NULL;
6821 }
6822 return newbuf;
6823}
6824
6825/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006826 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6827 * directory to "dirname_start" prior to returning, if autocmds or the
6828 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006829 */
6830 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006831wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006832{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006833 // If any autocommand opened a window on the dummy buffer, close that
6834 // window. If we can't close them all then give up.
6835 while (buf->b_nwindows > 0)
6836 {
6837 int did_one = FALSE;
6838 win_T *wp;
6839
6840 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006841 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006842 if (wp->w_buffer == buf)
6843 {
6844 if (win_close(wp, FALSE) == OK)
6845 did_one = TRUE;
6846 break;
6847 }
6848 if (!did_one)
6849 return;
6850 }
6851
6852 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006853 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006854#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006855 cleanup_T cs;
6856
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006857 // Reset the error/interrupt/exception state here so that aborting()
6858 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6859 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006860 enter_cleanup(&cs);
6861#endif
6862
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006863 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006864
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006865#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006866 // Restore the error/interrupt/exception state if not discarded by a
6867 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006868 leave_cleanup(&cs);
6869#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006870 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006871 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006872 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006873}
6874
6875/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006876 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6877 * directory to "dirname_start" prior to returning, if autocmds or the
6878 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006879 */
6880 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006881unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006882{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006883 if (curbuf == buf) // safety check
6884 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006885
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006886 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
6887
6888 // When autocommands/'autochdir' option changed directory: go back.
6889 restore_start_dir(dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006890}
6891
Bram Moolenaar05159a02005-02-26 23:04:13 +00006892#if defined(FEAT_EVAL) || defined(PROTO)
6893/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006894 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006895 * to 'list'. Returns OK on success.
6896 */
6897 static int
6898get_qfline_items(qfline_T *qfp, list_T *list)
6899{
6900 int bufnum;
6901 dict_T *dict;
6902 char_u buf[2];
6903
6904 // Handle entries with a non-existing buffer number.
6905 bufnum = qfp->qf_fnum;
6906 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6907 bufnum = 0;
6908
6909 if ((dict = dict_alloc()) == NULL)
6910 return FAIL;
6911 if (list_append_dict(list, dict) == FAIL)
6912 return FAIL;
6913
6914 buf[0] = qfp->qf_type;
6915 buf[1] = NUL;
6916 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006917 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6918 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6919 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6920 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6921 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6922 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006923 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6924 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6925 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6926 || dict_add_string(dict, "type", buf) == FAIL
Tom Praschanca6ac992023-08-11 23:26:12 +02006927 || (qfp->qf_user_data.v_type != VAR_UNKNOWN
6928 && dict_add_tv(dict, "user_data", &qfp->qf_user_data) == FAIL )
Bram Moolenaara16123a2019-03-28 20:31:07 +01006929 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6930 return FAIL;
6931
6932 return OK;
6933}
6934
6935/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006936 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006937 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006938 * If eidx is not 0, then return only the specified entry. Otherwise return
6939 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006940 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006941 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006942get_errorlist(
6943 qf_info_T *qi_arg,
6944 win_T *wp,
6945 int qf_idx,
6946 int eidx,
6947 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006948{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006949 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006950 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006951 qfline_T *qfp;
6952 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006953
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006954 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006955 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006956 qi = &ql_info;
6957 if (wp != NULL)
6958 {
6959 qi = GET_LOC_LIST(wp);
6960 if (qi == NULL)
6961 return FAIL;
6962 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006963 }
6964
Bram Moolenaar858ba062020-05-31 23:11:59 +02006965 if (eidx < 0)
6966 return OK;
6967
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006968 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006969 qf_idx = qi->qf_curlist;
6970
Bram Moolenaar0398e002019-03-21 21:12:49 +01006971 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006972 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006973
Bram Moolenaar0398e002019-03-21 21:12:49 +01006974 qfl = qf_get_list(qi, qf_idx);
6975 if (qf_list_empty(qfl))
6976 return FAIL;
6977
Bram Moolenaara16123a2019-03-28 20:31:07 +01006978 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006979 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006980 if (eidx > 0)
6981 {
6982 if (eidx == i)
6983 return get_qfline_items(qfp, list);
6984 }
6985 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006986 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006987 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006988
Bram Moolenaar05159a02005-02-26 23:04:13 +00006989 return OK;
6990}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006991
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006992// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006993enum {
6994 QF_GETLIST_NONE = 0x0,
6995 QF_GETLIST_TITLE = 0x1,
6996 QF_GETLIST_ITEMS = 0x2,
6997 QF_GETLIST_NR = 0x4,
6998 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006999 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007000 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007001 QF_GETLIST_IDX = 0x40,
7002 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01007003 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007004 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007005 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02007006 QF_GETLIST_QFTF = 0x800,
7007 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007008};
7009
7010/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007011 * Parse text from 'di' and return the quickfix list items.
7012 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007013 */
7014 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02007015qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007016{
7017 int status = FAIL;
7018 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02007019 char_u *errorformat = p_efm;
7020 dictitem_T *efm_di;
7021 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007022
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007023 // Only a List value is supported
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007024 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7025 return FAIL;
7026
7027 // If errorformat is supplied then use it, otherwise use the 'efm'
7028 // option setting
7029 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007030 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007031 if (efm_di->di_tv.v_type != VAR_STRING ||
7032 efm_di->di_tv.vval.v_string == NULL)
Bram Moolenaarda732532017-08-31 20:58:02 +02007033 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007034 errorformat = efm_di->di_tv.vval.v_string;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007035 }
7036
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007037 l = list_alloc();
7038 if (l == NULL)
7039 return FAIL;
7040
7041 qi = qf_alloc_stack(QFLT_INTERNAL);
7042 if (qi != NULL)
7043 {
7044 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
7045 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7046 {
7047 (void)get_errorlist(qi, NULL, 0, 0, l);
7048 qf_free(&qi->qf_lists[0]);
7049 }
7050 free(qi);
7051 }
7052 dict_add_list(retdict, "items", l);
7053 status = OK;
7054
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007055 return status;
7056}
7057
7058/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007059 * Return the quickfix/location list window identifier in the current tabpage.
7060 */
7061 static int
7062qf_winid(qf_info_T *qi)
7063{
7064 win_T *win;
7065
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007066 // The quickfix window can be opened even if the quickfix list is not set
7067 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007068 if (qi == NULL)
7069 return 0;
7070 win = qf_find_win(qi);
7071 if (win != NULL)
7072 return win->w_id;
7073 return 0;
7074}
7075
7076/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007077 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007078 * window. If there is no buffer associated with the list or the buffer is
7079 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007080 */
7081 static int
7082qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
7083{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007084 int bufnum = 0;
7085
7086 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
7087 bufnum = qi->qf_bufnr;
7088
7089 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007090}
7091
7092/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007093 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007094 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007095 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007096qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007097{
Bram Moolenaard823fa92016-08-12 16:29:27 +02007098 int flags = QF_GETLIST_NONE;
7099
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007100 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007101 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007102 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007103 if (!loclist)
7104 // File window ID is applicable only to location list windows
7105 flags &= ~ QF_GETLIST_FILEWINID;
7106 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007107
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007108 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007109 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007110
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007111 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007112 flags |= QF_GETLIST_NR;
7113
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007114 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007115 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007116
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007117 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007118 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007119
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007120 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007121 flags |= QF_GETLIST_ID;
7122
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007123 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007124 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007125
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007126 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007127 flags |= QF_GETLIST_IDX;
7128
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007129 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007130 flags |= QF_GETLIST_SIZE;
7131
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007132 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01007133 flags |= QF_GETLIST_TICK;
7134
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007135 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007136 flags |= QF_GETLIST_FILEWINID;
7137
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007138 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007139 flags |= QF_GETLIST_QFBUFNR;
7140
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007141 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02007142 flags |= QF_GETLIST_QFTF;
7143
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007144 return flags;
7145}
Bram Moolenaara6d48492017-12-12 22:45:31 +01007146
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007147/*
7148 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
7149 * If 'nr' and 'id' are not present in 'what' then return the current
7150 * quickfix list index.
7151 * If 'nr' is zero then return the current quickfix list index.
7152 * If 'nr' is '$' then return the last quickfix list index.
7153 * If 'id' is present then return the index of the quickfix list with that id.
7154 * If 'id' is zero then return the quickfix list index specified by 'nr'.
7155 * Return -1, if quickfix list is not present or if the stack is empty.
7156 */
7157 static int
7158qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
7159{
7160 int qf_idx;
7161 dictitem_T *di;
7162
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007163 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007164 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7165 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007166 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007167 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007168 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007169 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007170 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007171 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007172 qf_idx = di->di_tv.vval.v_number - 1;
7173 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007174 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007175 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01007176 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007177 else if (di->di_tv.v_type == VAR_STRING
7178 && di->di_tv.vval.v_string != NULL
7179 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007180 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007181 qf_idx = qi->qf_listcount - 1;
7182 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007183 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007184 }
7185
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007186 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
7187 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007188 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007189 if (di->di_tv.v_type == VAR_NUMBER)
7190 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007191 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007192 if (di->di_tv.vval.v_number != 0)
7193 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
7194 }
7195 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007196 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007197 }
7198
7199 return qf_idx;
7200}
7201
7202/*
7203 * Return default values for quickfix list properties in retdict.
7204 */
7205 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007206qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007207{
7208 int status = OK;
7209
7210 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007211 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007212 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7213 {
7214 list_T *l = list_alloc();
7215 if (l != NULL)
7216 status = dict_add_list(retdict, "items", l);
7217 else
7218 status = FAIL;
7219 }
7220 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007221 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007222 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007223 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007224 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007225 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007226 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007227 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007228 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007229 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007230 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007231 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007232 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007233 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007234 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007235 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007236 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7237 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007238 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7239 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007240
7241 return status;
7242}
7243
7244/*
7245 * Return the quickfix list title as 'title' in retdict
7246 */
7247 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007248qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007249{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007250 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007251}
7252
7253/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007254 * Returns the identifier of the window used to display files from a location
7255 * list. If there is no associated window, then returns 0. Useful only when
7256 * called from a location list window.
7257 */
7258 static int
7259qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7260{
7261 int winid = 0;
7262
7263 if (wp != NULL && IS_LL_WINDOW(wp))
7264 {
7265 win_T *ll_wp = qf_find_win_with_loclist(qi);
7266 if (ll_wp != NULL)
7267 winid = ll_wp->w_id;
7268 }
7269
7270 return dict_add_number(retdict, "filewinid", winid);
7271}
7272
7273/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007274 * Return the quickfix list items/entries as 'items' in retdict.
7275 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007276 */
7277 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007278qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007279{
7280 int status = OK;
7281 list_T *l = list_alloc();
7282 if (l != NULL)
7283 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007284 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007285 dict_add_list(retdict, "items", l);
7286 }
7287 else
7288 status = FAIL;
7289
7290 return status;
7291}
7292
7293/*
7294 * Return the quickfix list context (if any) as 'context' in retdict.
7295 */
7296 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007297qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007298{
7299 int status;
7300 dictitem_T *di;
7301
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007302 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007303 {
7304 di = dictitem_alloc((char_u *)"context");
7305 if (di != NULL)
7306 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007307 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007308 status = dict_add(retdict, di);
7309 if (status == FAIL)
7310 dictitem_free(di);
7311 }
7312 else
7313 status = FAIL;
7314 }
7315 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007316 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007317
7318 return status;
7319}
7320
7321/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007322 * Return the current quickfix list index as 'idx' in retdict.
7323 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007324 */
7325 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007326qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007327{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007328 if (eidx == 0)
7329 {
7330 eidx = qfl->qf_index;
7331 if (qf_list_empty(qfl))
7332 // For empty lists, current index is set to 0
7333 eidx = 0;
7334 }
7335 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007336}
7337
7338/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007339 * Return the 'quickfixtextfunc' function of a quickfix/location list
7340 */
7341 static int
7342qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7343{
7344 int status;
7345
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007346 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007347 {
7348 typval_T tv;
7349
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007350 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007351 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7352 clear_tv(&tv);
7353 }
7354 else
7355 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7356
7357 return status;
7358}
7359
7360/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007361 * Return quickfix/location list details (title) as a
7362 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7363 * then current list is used. Otherwise the specified list is used.
7364 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007365 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007366qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7367{
7368 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007369 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007370 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007371 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007372 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007373 dictitem_T *di;
7374 int flags = QF_GETLIST_NONE;
7375
7376 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7377 return qf_get_list_from_lines(what, di, retdict);
7378
7379 if (wp != NULL)
7380 qi = GET_LOC_LIST(wp);
7381
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007382 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007383
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007384 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007385 qf_idx = qf_getprop_qfidx(qi, what);
7386
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007387 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007388 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007389 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007390
Bram Moolenaar0398e002019-03-21 21:12:49 +01007391 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007392
Bram Moolenaar858ba062020-05-31 23:11:59 +02007393 // If an entry index is specified, use that
7394 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7395 {
7396 if (di->di_tv.v_type != VAR_NUMBER)
7397 return FAIL;
7398 eidx = di->di_tv.vval.v_number;
7399 }
7400
Bram Moolenaard823fa92016-08-12 16:29:27 +02007401 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007402 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007403 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007404 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007405 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007406 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007407 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007408 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007409 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007410 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007411 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007412 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007413 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007414 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007415 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007416 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007417 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007418 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007419 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7420 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007421 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7422 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007423 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7424 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007425
Bram Moolenaard823fa92016-08-12 16:29:27 +02007426 return status;
7427}
7428
7429/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007430 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007431 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7432 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007433 */
7434 static int
7435qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007436 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007437 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007438 int first_entry,
7439 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007440{
7441 static int did_bufnr_emsg;
7442 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007443 int bufnum, valid, status, col, end_col, vcol, nr;
7444 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007445
7446 if (first_entry)
7447 did_bufnr_emsg = FALSE;
7448
Bram Moolenaard61efa52022-07-23 09:52:04 +01007449 filename = dict_get_string(d, "filename", TRUE);
7450 module = dict_get_string(d, "module", TRUE);
7451 bufnum = (int)dict_get_number(d, "bufnr");
7452 lnum = (int)dict_get_number(d, "lnum");
7453 end_lnum = (int)dict_get_number(d, "end_lnum");
7454 col = (int)dict_get_number(d, "col");
7455 end_col = (int)dict_get_number(d, "end_col");
7456 vcol = (int)dict_get_number(d, "vcol");
7457 nr = (int)dict_get_number(d, "nr");
7458 type = dict_get_string(d, "type", TRUE);
7459 pattern = dict_get_string(d, "pattern", TRUE);
7460 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007461 if (text == NULL)
7462 text = vim_strsave((char_u *)"");
Tom Praschanca6ac992023-08-11 23:26:12 +02007463 typval_T user_data;
7464 user_data.v_type = VAR_UNKNOWN;
7465 dict_get_tv(d, "user_data", &user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007466
7467 valid = TRUE;
7468 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7469 valid = FALSE;
7470
7471 // Mark entries with non-existing buffer number as not valid. Give the
7472 // error message only once.
7473 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7474 {
7475 if (!did_bufnr_emsg)
7476 {
7477 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007478 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007479 }
7480 valid = FALSE;
7481 bufnum = 0;
7482 }
7483
7484 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007485 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007486 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007487
Bram Moolenaar0398e002019-03-21 21:12:49 +01007488 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007489 NULL, // dir
7490 filename,
7491 module,
7492 bufnum,
7493 text,
7494 lnum,
thinca6864efa2021-06-19 20:45:20 +02007495 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007496 col,
thinca6864efa2021-06-19 20:45:20 +02007497 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007498 vcol, // vis_col
7499 pattern, // search pattern
7500 nr,
7501 type == NULL ? NUL : *type,
Tom Praschanca6ac992023-08-11 23:26:12 +02007502 &user_data,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007503 valid);
7504
7505 vim_free(filename);
7506 vim_free(module);
7507 vim_free(pattern);
7508 vim_free(text);
7509 vim_free(type);
Tom Praschanca6ac992023-08-11 23:26:12 +02007510 clear_tv(&user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007511
Bram Moolenaar9752c722018-12-22 16:49:34 +01007512 if (valid)
7513 *valid_entry = TRUE;
7514
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007515 return status;
7516}
7517
7518/*
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007519 * Check if `entry` is closer to the target than `other_entry`.
7520 *
7521 * Only returns TRUE if `entry` is definitively closer. If it's further
7522 * away, or there's not enough information to tell, return FALSE.
7523 */
7524 static int
7525entry_is_closer_to_target(
7526 qfline_T *entry,
7527 qfline_T *other_entry,
7528 int target_fnum,
7529 int target_lnum,
7530 int target_col)
7531{
7532 // First, compare entries to target file.
7533 if (!target_fnum)
7534 // Without a target file, we can't know which is closer.
7535 return FALSE;
7536
7537 int is_target_file = entry->qf_fnum && entry->qf_fnum == target_fnum;
7538 int other_is_target_file = other_entry->qf_fnum && other_entry->qf_fnum == target_fnum;
7539 if (!is_target_file && other_is_target_file)
7540 return FALSE;
7541 else if (is_target_file && !other_is_target_file)
7542 return TRUE;
7543
7544 // Both entries are pointing at the exact same file. Now compare line
7545 // numbers.
7546 if (!target_lnum)
7547 // Without a target line number, we can't know which is closer.
7548 return FALSE;
7549
7550 int line_distance = entry->qf_lnum ? labs(entry->qf_lnum - target_lnum) : INT_MAX;
7551 int other_line_distance = other_entry->qf_lnum ? labs(other_entry->qf_lnum - target_lnum) : INT_MAX;
7552 if (line_distance > other_line_distance)
7553 return FALSE;
7554 else if (line_distance < other_line_distance)
7555 return TRUE;
7556
7557 // Both entries are pointing at the exact same line number (or no line
7558 // number at all). Now compare columns.
7559 if (!target_col)
7560 // Without a target column, we can't know which is closer.
7561 return FALSE;
7562
7563 int column_distance = entry->qf_col ? abs(entry->qf_col - target_col) : INT_MAX;
7564 int other_column_distance = other_entry->qf_col ? abs(other_entry->qf_col - target_col): INT_MAX;
7565 if (column_distance > other_column_distance)
7566 return FALSE;
7567 else if (column_distance < other_column_distance)
7568 return TRUE;
7569
7570 // It's a complete tie! The exact same file, line, and column.
7571 return FALSE;
7572}
7573
7574/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007575 * Add list of entries to quickfix/location list. Each list entry is
7576 * a dictionary with item information.
7577 */
7578 static int
7579qf_add_entries(
7580 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007581 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007582 list_T *list,
7583 char_u *title,
7584 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007585{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007586 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007587 listitem_T *li;
7588 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007589 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007590 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007591 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007592
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007593 // If there's an entry selected in the quickfix list, remember its location
7594 // (file, line, column), so we can select the nearest entry in the updated
7595 // quickfix list.
7596 int prev_fnum = 0;
7597 int prev_lnum = 0;
7598 int prev_col = 0;
7599 if (qfl->qf_ptr)
7600 {
7601 prev_fnum = qfl->qf_ptr->qf_fnum;
7602 prev_lnum = qfl->qf_ptr->qf_lnum;
7603 prev_col = qfl->qf_ptr->qf_col;
7604 }
7605
7606 int select_first_entry = FALSE;
7607 int select_nearest_entry = FALSE;
7608
Bram Moolenaara3921f42017-06-04 15:30:34 +02007609 if (action == ' ' || qf_idx == qi->qf_listcount)
7610 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007611 select_first_entry = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007612 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007613 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007614 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007615 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007616 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007617 else if (action == 'a')
7618 {
7619 if (qf_list_empty(qfl))
7620 // Appending to empty list, select first entry.
7621 select_first_entry = TRUE;
7622 else
7623 // Adding to existing list, use last entry.
7624 old_last = qfl->qf_last;
7625 }
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007626 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007627 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007628 select_first_entry = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007629 qf_free_items(qfl);
7630 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007631 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007632 else if (action == 'u')
7633 {
7634 select_nearest_entry = TRUE;
7635 qf_free_items(qfl);
7636 qf_store_title(qfl, title);
7637 }
7638
7639 qfline_T *entry_to_select = NULL;
7640 int entry_to_select_index = 0;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007641
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007642 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007643 {
7644 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007645 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007646
7647 d = li->li_tv.vval.v_dict;
7648 if (d == NULL)
7649 continue;
7650
Bram Moolenaar0398e002019-03-21 21:12:49 +01007651 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007652 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007653 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007654 break;
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007655
7656 qfline_T *entry = qfl->qf_last;
7657 if (
7658 (select_first_entry && entry_to_select == NULL) ||
7659 (select_nearest_entry &&
7660 (entry_to_select == NULL ||
7661 entry_is_closer_to_target(entry, entry_to_select, prev_fnum,
7662 prev_lnum, prev_col))))
7663 {
7664 entry_to_select = entry;
7665 entry_to_select_index = qfl->qf_count;
7666 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007667 }
7668
Bram Moolenaar9752c722018-12-22 16:49:34 +01007669 // Check if any valid error entries are added to the list.
7670 if (valid_entry)
7671 qfl->qf_nonevalid = FALSE;
7672 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007673 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007674 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007675
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007676 // Set the current error.
7677 if (entry_to_select)
7678 {
7679 qfl->qf_ptr = entry_to_select;
7680 qfl->qf_index = entry_to_select_index;
7681 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007682
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007683 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007684 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007685
7686 return retval;
7687}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007688
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007689/*
7690 * Get the quickfix list index from 'nr' or 'id'
7691 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007692 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007693qf_setprop_get_qfidx(
7694 qf_info_T *qi,
7695 dict_T *what,
7696 int action,
7697 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007698{
7699 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007700 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007701
Bram Moolenaard823fa92016-08-12 16:29:27 +02007702 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7703 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007704 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007705 if (di->di_tv.v_type == VAR_NUMBER)
7706 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007707 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007708 if (di->di_tv.vval.v_number != 0)
7709 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007710
Bram Moolenaar55b69262017-08-13 13:42:01 +02007711 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7712 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007713 // When creating a new list, accept qf_idx pointing to the next
7714 // non-available list and add the new list at the end of the
7715 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007716 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007717 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007718 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007719 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007720 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007721 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007722 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007723 }
7724 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007725 && di->di_tv.vval.v_string != NULL
7726 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007727 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007728 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007729 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007730 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007731 qf_idx = 0;
7732 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007733 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007734 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007735 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007736 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007737 }
7738
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007739 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007740 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007741 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007742 if (di->di_tv.v_type != VAR_NUMBER)
7743 return INVALID_QFIDX;
7744
7745 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007746 }
7747
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007748 return qf_idx;
7749}
7750
7751/*
7752 * Set the quickfix list title.
7753 */
7754 static int
7755qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7756{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007757 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007758
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007759 if (di->di_tv.v_type != VAR_STRING)
7760 return FAIL;
7761
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007762 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007763 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007764 if (qf_idx == qi->qf_curlist)
7765 qf_update_win_titlevar(qi);
7766
7767 return OK;
7768}
7769
7770/*
7771 * Set quickfix list items/entries.
7772 */
7773 static int
7774qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7775{
7776 int retval = FAIL;
7777 char_u *title_save;
7778
7779 if (di->di_tv.v_type != VAR_LIST)
7780 return FAIL;
7781
7782 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7783 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7784 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007785 vim_free(title_save);
7786
7787 return retval;
7788}
7789
7790/*
7791 * Set quickfix list items/entries from a list of lines.
7792 */
7793 static int
7794qf_setprop_items_from_lines(
7795 qf_info_T *qi,
7796 int qf_idx,
7797 dict_T *what,
7798 dictitem_T *di,
7799 int action)
7800{
7801 char_u *errorformat = p_efm;
7802 dictitem_T *efm_di;
7803 int retval = FAIL;
7804
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007805 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007806 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7807 {
7808 if (efm_di->di_tv.v_type != VAR_STRING ||
7809 efm_di->di_tv.vval.v_string == NULL)
7810 return FAIL;
7811 errorformat = efm_di->di_tv.vval.v_string;
7812 }
7813
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007814 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007815 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7816 return FAIL;
7817
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007818 if (action == 'r' || action == 'u')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007819 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007820 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007821 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007822 retval = OK;
7823
7824 return retval;
7825}
7826
7827/*
7828 * Set quickfix list context.
7829 */
7830 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007831qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007832{
7833 typval_T *ctx;
7834
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007835 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007836 ctx = alloc_tv();
7837 if (ctx != NULL)
7838 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007839 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007840
7841 return OK;
7842}
7843
7844/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007845 * Set the current index in the specified quickfix list
7846 */
7847 static int
7848qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7849{
7850 int denote = FALSE;
7851 int newidx;
7852 int old_qfidx;
7853 qfline_T *qf_ptr;
7854
7855 // If the specified index is '$', then use the last entry
7856 if (di->di_tv.v_type == VAR_STRING
7857 && di->di_tv.vval.v_string != NULL
7858 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7859 newidx = qfl->qf_count;
7860 else
7861 {
7862 // Otherwise use the specified index
7863 newidx = tv_get_number_chk(&di->di_tv, &denote);
7864 if (denote)
7865 return FAIL;
7866 }
7867
7868 if (newidx < 1) // sanity check
7869 return FAIL;
7870 if (newidx > qfl->qf_count)
7871 newidx = qfl->qf_count;
7872
7873 old_qfidx = qfl->qf_index;
7874 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7875 if (qf_ptr == NULL)
7876 return FAIL;
7877 qfl->qf_ptr = qf_ptr;
7878 qfl->qf_index = newidx;
7879
7880 // If the current list is modified and it is displayed in the quickfix
7881 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007882 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007883 qf_win_pos_update(qi, old_qfidx);
7884
7885 return OK;
7886}
7887
7888/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007889 * Set the current index in the specified quickfix list
7890 */
7891 static int
7892qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7893{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007894 callback_T cb;
7895
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007896 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007897 cb = get_callback(&di->di_tv);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007898 if (cb.cb_name == NULL || *cb.cb_name == NUL)
7899 return OK;
7900
7901 set_callback(&qfl->qf_qftf_cb, &cb);
7902 if (cb.cb_free_name)
7903 vim_free(cb.cb_name);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007904
7905 return OK;
7906}
7907
7908/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007909 * Set quickfix/location list properties (title, items, context).
7910 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007911 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007912 */
7913 static int
7914qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7915{
7916 dictitem_T *di;
7917 int retval = FAIL;
7918 int qf_idx;
7919 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007920 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007921
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007922 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007923 newlist = TRUE;
7924
7925 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007926 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007927 return FAIL;
7928
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007929 if (newlist)
7930 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007931 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007932 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007933 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007934 }
7935
Bram Moolenaar0398e002019-03-21 21:12:49 +01007936 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007937 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007938 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007939 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007940 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007941 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007942 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007943 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007944 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007945 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7946 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007947 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7948 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007949
Bram Moolenaar287153c2020-11-29 14:20:27 +01007950 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007951 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007952 if (newlist)
7953 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007954
Bram Moolenaard823fa92016-08-12 16:29:27 +02007955 return retval;
7956}
7957
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007958/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007959 * Free the entire quickfix/location list stack.
7960 * If the quickfix/location list window is open, then clear it.
7961 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007962 static void
7963qf_free_stack(win_T *wp, qf_info_T *qi)
7964{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007965 win_T *qfwin = qf_find_win(qi);
7966 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007967
7968 if (qfwin != NULL)
7969 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007970 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007971 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007972 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007973 qf_update_buffer(qi, NULL);
7974 }
7975
7976 if (wp != NULL && IS_LL_WINDOW(wp))
7977 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007978 // If in the location list window, then use the non-location list
7979 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007980 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007981 if (llwin != NULL)
7982 wp = llwin;
7983 }
7984
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007985 qf_free_all(wp);
7986 if (wp == NULL)
7987 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007988 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007989 qi->qf_curlist = 0;
7990 qi->qf_listcount = 0;
7991 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007992 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007993 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007994 // If the location list window is open, then create a new empty
7995 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007996 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007997
Bram Moolenaar95946f12019-03-31 15:31:59 +02007998 if (new_ll != NULL)
7999 {
8000 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02008001
Bram Moolenaar95946f12019-03-31 15:31:59 +02008002 // first free the list reference in the location list window
8003 ll_free_all(&qfwin->w_llist_ref);
8004
8005 qfwin->w_llist_ref = new_ll;
8006 if (wp != qfwin)
8007 win_set_loclist(wp, new_ll);
8008 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008009 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008010}
8011
Bram Moolenaard823fa92016-08-12 16:29:27 +02008012/*
8013 * Populate the quickfix list with the items supplied in the list
8014 * of dictionaries. "title" will be copied to w:quickfix_title.
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008015 * "action" is 'a' for add, 'r' for replace, 'u' for update. Otherwise
8016 * create a new list. When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02008017 */
8018 int
8019set_errorlist(
8020 win_T *wp,
8021 list_T *list,
8022 int action,
8023 char_u *title,
8024 dict_T *what)
8025{
8026 qf_info_T *qi = &ql_info;
8027 int retval = OK;
8028
8029 if (wp != NULL)
8030 {
8031 qi = ll_get_or_alloc_list(wp);
8032 if (qi == NULL)
8033 return FAIL;
8034 }
8035
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008036 if (action == 'f')
8037 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008038 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008039 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008040 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008041 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008042
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008043 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02008044 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008045 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008046 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008047 _("cannot have both a list and a \"what\" argument"));
8048 return FAIL;
8049 }
8050
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008051 incr_quickfix_busy();
8052
8053 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02008054 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02008055 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01008056 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02008057 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01008058 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008059 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01008060 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02008061
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008062 decr_quickfix_busy();
8063
Bram Moolenaard823fa92016-08-12 16:29:27 +02008064 return retval;
8065}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008066
Tom Praschanca6ac992023-08-11 23:26:12 +02008067static int mark_quickfix_user_data(qf_info_T *qi, int copyID)
8068{
8069 int abort = FALSE;
8070 for (int i = 0; i < LISTCOUNT && !abort; ++i)
8071 {
8072 qf_list_T *qfl = &qi->qf_lists[i];
8073 if (!qfl->qf_has_user_data)
8074 continue;
8075 qfline_T *qfp;
8076 int j;
8077 FOR_ALL_QFL_ITEMS(qfl, qfp, j)
8078 {
8079 typval_T* user_data = &qfp->qf_user_data;
8080 if (user_data != NULL && user_data->v_type != VAR_NUMBER
8081 && user_data->v_type != VAR_STRING && user_data->v_type != VAR_FLOAT)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008082 abort = abort || set_ref_in_item(user_data, copyID, NULL, NULL, NULL);
Tom Praschanca6ac992023-08-11 23:26:12 +02008083 }
8084 }
8085 return abort;
8086}
8087
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008088/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008089 * Mark the quickfix context and callback function as in use for all the lists
8090 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008091 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008092 static int
8093mark_quickfix_ctx(qf_info_T *qi, int copyID)
8094{
8095 int i;
8096 int abort = FALSE;
8097 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008098 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008099
8100 for (i = 0; i < LISTCOUNT && !abort; ++i)
8101 {
8102 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02008103 if (ctx != NULL && ctx->v_type != VAR_NUMBER
8104 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008105 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL, NULL);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008106
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01008107 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008108 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008109 }
8110
8111 return abort;
8112}
8113
8114/*
8115 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02008116 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008117 */
8118 int
8119set_ref_in_quickfix(int copyID)
8120{
8121 int abort = FALSE;
8122 tabpage_T *tp;
8123 win_T *win;
8124
8125 abort = mark_quickfix_ctx(&ql_info, copyID);
8126 if (abort)
8127 return abort;
8128
Tom Praschanca6ac992023-08-11 23:26:12 +02008129 abort = mark_quickfix_user_data(&ql_info, copyID);
8130 if (abort)
8131 return abort;
8132
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008133 abort = set_ref_in_callback(&qftf_cb, copyID);
8134 if (abort)
8135 return abort;
8136
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008137 FOR_ALL_TAB_WINDOWS(tp, win)
8138 {
8139 if (win->w_llist != NULL)
8140 {
8141 abort = mark_quickfix_ctx(win->w_llist, copyID);
8142 if (abort)
8143 return abort;
Tom Praschanca6ac992023-08-11 23:26:12 +02008144
8145 abort = mark_quickfix_user_data(win->w_llist, copyID);
8146 if (abort)
8147 return abort;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008148 }
Bram Moolenaar12237442017-12-19 12:38:52 +01008149 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
8150 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008151 // In a location list window and none of the other windows is
8152 // referring to this location list. Mark the location list
8153 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01008154 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
8155 if (abort)
8156 return abort;
zeertzjqbe4bd182024-09-14 10:32:31 +02008157
8158 abort = mark_quickfix_user_data(win->w_llist_ref, copyID);
8159 if (abort)
8160 return abort;
Bram Moolenaar12237442017-12-19 12:38:52 +01008161 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008162 }
8163
8164 return abort;
8165}
Bram Moolenaar05159a02005-02-26 23:04:13 +00008166#endif
8167
Bram Moolenaar81695252004-12-29 20:58:21 +00008168/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008169 * Return the autocmd name for the :cbuffer Ex commands
8170 */
8171 static char_u *
8172cbuffer_get_auname(cmdidx_T cmdidx)
8173{
8174 switch (cmdidx)
8175 {
8176 case CMD_cbuffer: return (char_u *)"cbuffer";
8177 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
8178 case CMD_caddbuffer: return (char_u *)"caddbuffer";
8179 case CMD_lbuffer: return (char_u *)"lbuffer";
8180 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
8181 case CMD_laddbuffer: return (char_u *)"laddbuffer";
8182 default: return NULL;
8183 }
8184}
8185
8186/*
8187 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
8188 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
8189 */
8190 static int
8191cbuffer_process_args(
8192 exarg_T *eap,
8193 buf_T **bufp,
8194 linenr_T *line1,
8195 linenr_T *line2)
8196{
8197 buf_T *buf = NULL;
8198
8199 if (*eap->arg == NUL)
8200 buf = curbuf;
8201 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
8202 buf = buflist_findnr(atoi((char *)eap->arg));
8203
8204 if (buf == NULL)
8205 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008206 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008207 return FAIL;
8208 }
8209
8210 if (buf->b_ml.ml_mfp == NULL)
8211 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008212 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008213 return FAIL;
8214 }
8215
8216 if (eap->addr_count == 0)
8217 {
8218 eap->line1 = 1;
8219 eap->line2 = buf->b_ml.ml_line_count;
8220 }
8221
8222 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
8223 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
8224 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008225 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008226 return FAIL;
8227 }
8228
8229 *line1 = eap->line1;
8230 *line2 = eap->line2;
8231 *bufp = buf;
8232
8233 return OK;
8234}
8235
8236/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00008237 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008238 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008239 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008240 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008241 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008242 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008243 */
8244 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008245ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008246{
8247 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008248 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008249 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01008250 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02008251 int_u save_qfid;
8252 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01008253 char_u *qf_title;
8254 linenr_T line1;
8255 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008256
Bram Moolenaara16123a2019-03-28 20:31:07 +01008257 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01008258 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01008259 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008260 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008261#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008262 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008263 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008264#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008265 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008266
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008267 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008268 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8269 if (qi == NULL)
8270 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01008271
Bram Moolenaara16123a2019-03-28 20:31:07 +01008272 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
8273 return;
8274
8275 qf_title = qf_cmdtitle(*eap->cmdlinep);
8276
8277 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008278 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01008279 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
8280 (char *)qf_title, (char *)buf->b_sfname);
8281 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008282 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01008283
8284 incr_quickfix_busy();
8285
8286 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
8287 (eap->cmdidx != CMD_caddbuffer
8288 && eap->cmdidx != CMD_laddbuffer),
8289 line1, line2,
8290 qf_title, NULL);
8291 if (qf_stack_empty(qi))
8292 {
8293 decr_quickfix_busy();
8294 return;
8295 }
8296 if (res >= 0)
8297 qf_list_changed(qf_get_curlist(qi));
8298
8299 // Remember the current quickfix list identifier, so that we can
8300 // check for autocommands changing the current quickfix list.
8301 save_qfid = qf_get_curlist(qi)->qf_id;
8302 if (au_name != NULL)
8303 {
8304 buf_T *curbuf_old = curbuf;
8305
8306 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
8307 TRUE, curbuf);
8308 if (curbuf != curbuf_old)
8309 // Autocommands changed buffer, don't jump now, "qi" may
8310 // be invalid.
8311 res = 0;
8312 }
8313 // Jump to the first error for a new list and if autocmds didn't
8314 // free the list.
8315 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
8316 eap->cmdidx == CMD_lbuffer)
8317 && qflist_valid(wp, save_qfid))
8318 // display the first error
8319 qf_jump_first(qi, save_qfid, eap->forceit);
8320
8321 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00008322}
8323
Bram Moolenaar1e015462005-09-25 22:16:38 +00008324#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008325/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008326 * Return the autocmd name for the :cexpr Ex commands.
8327 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008328 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01008329cexpr_get_auname(cmdidx_T cmdidx)
8330{
8331 switch (cmdidx)
8332 {
8333 case CMD_cexpr: return (char_u *)"cexpr";
8334 case CMD_cgetexpr: return (char_u *)"cgetexpr";
8335 case CMD_caddexpr: return (char_u *)"caddexpr";
8336 case CMD_lexpr: return (char_u *)"lexpr";
8337 case CMD_lgetexpr: return (char_u *)"lgetexpr";
8338 case CMD_laddexpr: return (char_u *)"laddexpr";
8339 default: return NULL;
8340 }
8341}
8342
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008343 int
8344trigger_cexpr_autocmd(int cmdidx)
8345{
8346 char_u *au_name = cexpr_get_auname(cmdidx);
8347
8348 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8349 curbuf->b_fname, TRUE, curbuf))
8350 {
8351 if (aborting())
8352 return FAIL;
8353 }
8354 return OK;
8355}
8356
8357 int
8358cexpr_core(exarg_T *eap, typval_T *tv)
8359{
8360 qf_info_T *qi;
8361 win_T *wp = NULL;
8362
8363 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8364 if (qi == NULL)
8365 return FAIL;
8366
8367 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8368 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8369 {
8370 int res;
8371 int_u save_qfid;
8372 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8373
8374 incr_quickfix_busy();
8375 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8376 (eap->cmdidx != CMD_caddexpr
8377 && eap->cmdidx != CMD_laddexpr),
8378 (linenr_T)0, (linenr_T)0,
8379 qf_cmdtitle(*eap->cmdlinep), NULL);
8380 if (qf_stack_empty(qi))
8381 {
8382 decr_quickfix_busy();
8383 return FAIL;
8384 }
8385 if (res >= 0)
8386 qf_list_changed(qf_get_curlist(qi));
8387
8388 // Remember the current quickfix list identifier, so that we can
8389 // check for autocommands changing the current quickfix list.
8390 save_qfid = qf_get_curlist(qi)->qf_id;
8391 if (au_name != NULL)
8392 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8393 curbuf->b_fname, TRUE, curbuf);
8394
8395 // Jump to the first error for a new list and if autocmds didn't
8396 // free the list.
8397 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8398 && qflist_valid(wp, save_qfid))
8399 // display the first error
8400 qf_jump_first(qi, save_qfid, eap->forceit);
8401 decr_quickfix_busy();
8402 return OK;
8403 }
8404
Bram Moolenaar677658a2022-01-05 16:09:06 +00008405 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008406 return FAIL;
8407}
8408
Bram Moolenaara16123a2019-03-28 20:31:07 +01008409/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008410 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8411 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008412 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008413 */
8414 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008415ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008416{
8417 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008418
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008419 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008420 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008421
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008422 // Evaluate the expression. When the result is a string or a list we can
8423 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008424 tv = eval_expr(eap->arg, eap);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008425 if (tv == NULL)
8426 return;
8427
8428 (void)cexpr_core(eap, tv);
8429 free_tv(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008430}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008431#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008432
8433/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008434 * Get the location list for ":lhelpgrep"
8435 */
8436 static qf_info_T *
8437hgr_get_ll(int *new_ll)
8438{
8439 win_T *wp;
8440 qf_info_T *qi;
8441
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008442 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008443 if (bt_help(curwin->w_buffer))
8444 wp = curwin;
8445 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008446 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008447 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008448
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008449 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008450 qi = NULL;
8451 else
8452 qi = wp->w_llist;
8453
8454 if (qi == NULL)
8455 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008456 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008457 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008458 return NULL;
8459 *new_ll = TRUE;
8460 }
8461
8462 return qi;
8463}
8464
8465/*
8466 * Search for a pattern in a help file.
8467 */
8468 static void
8469hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008470 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008471 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008472 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008473 regmatch_T *p_regmatch)
8474{
8475 FILE *fd;
8476 long lnum;
8477
8478 fd = mch_fopen((char *)fname, "r");
8479 if (fd == NULL)
8480 return;
8481
8482 lnum = 1;
8483 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8484 {
8485 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008486
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008487 // Convert a line if 'encoding' is not utf-8 and
8488 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008489 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008490 {
8491 line = string_convert(p_vc, IObuff, NULL);
8492 if (line == NULL)
8493 line = IObuff;
8494 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008495
8496 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8497 {
8498 int l = (int)STRLEN(line);
8499
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008500 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008501 while (l > 0 && line[l - 1] <= ' ')
8502 line[--l] = NUL;
8503
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008504 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008505 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008506 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008507 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008508 0,
8509 line,
8510 lnum,
thinca6864efa2021-06-19 20:45:20 +02008511 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008512 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008513 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008514 (int)(p_regmatch->endp[0] - line)
8515 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008516 FALSE, // vis_col
8517 NULL, // search pattern
8518 0, // nr
8519 1, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02008520 NULL, // user_data
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008521 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008522 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008523 {
8524 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008525 if (line != IObuff)
8526 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008527 break;
8528 }
8529 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008530 if (line != IObuff)
8531 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008532 ++lnum;
8533 line_breakcheck();
8534 }
8535 fclose(fd);
8536}
8537
8538/*
8539 * Search for a pattern in all the help files in the doc directory under
8540 * the given directory.
8541 */
8542 static void
8543hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008544 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008545 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008546 regmatch_T *p_regmatch,
8547 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008548#ifdef FEAT_MULTI_LANG
8549 , char_u *lang
8550#endif
8551 )
8552{
8553 int fcount;
8554 char_u **fnames;
8555 int fi;
8556
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008557 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008558 add_pathsep(dirname);
8559 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8560 if (gen_expand_wildcards(1, &dirname, &fcount,
8561 &fnames, EW_FILE|EW_SILENT) == OK
8562 && fcount > 0)
8563 {
8564 for (fi = 0; fi < fcount && !got_int; ++fi)
8565 {
8566#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008567 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008568 if (lang != NULL
8569 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008570 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008571 && !(STRNICMP(lang, "en", 2) == 0
8572 && STRNICMP("txt", fnames[fi]
8573 + STRLEN(fnames[fi]) - 3, 3) == 0))
8574 continue;
8575#endif
8576
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008577 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008578 }
8579 FreeWild(fcount, fnames);
8580 }
8581}
8582
8583/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008584 * Search for a pattern in all the help files in the 'runtimepath'
8585 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008586 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008587 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008588 */
8589 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008590hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008591{
8592 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008593
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008594 vimconv_T vc;
8595
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008596 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8597 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008598 vc.vc_type = CONV_NONE;
8599 if (!enc_utf8)
8600 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008601
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008602 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008603 p = p_rtp;
8604 while (*p != NUL && !got_int)
8605 {
8606 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8607
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008608 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008609#ifdef FEAT_MULTI_LANG
8610 , lang
8611#endif
8612 );
8613 }
8614
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008615 if (vc.vc_type != CONV_NONE)
8616 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008617}
8618
8619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 * ":helpgrep {pattern}"
8621 */
8622 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008623ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
8625 regmatch_T regmatch;
8626 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008627 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008628 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008629 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008630 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008631 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008632 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
Bram Moolenaar73633f82012-01-20 13:39:07 +01008634 switch (eap->cmdidx)
8635 {
8636 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8637 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8638 default: break;
8639 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008640 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8641 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008642 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008643#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008644 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008645 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008646#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008647 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008648
Bram Moolenaar39665952018-08-15 20:59:48 +02008649 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008650 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008651 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008652 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008653 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008654 }
8655
Bram Moolenaara16123a2019-03-28 20:31:07 +01008656 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8657 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008658 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008659 p_cpo = empty_option;
8660
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008661 incr_quickfix_busy();
8662
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008663#ifdef FEAT_MULTI_LANG
8664 // Check for a specified language
8665 lang = check_help_lang(eap->arg);
8666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8668 regmatch.rm_ic = FALSE;
8669 if (regmatch.regprog != NULL)
8670 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008671 qf_list_T *qfl;
8672
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008673 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008674 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008675 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008677 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008678
Bram Moolenaar473de612013-06-08 18:19:48 +02008679 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008681 qfl->qf_nonevalid = FALSE;
8682 qfl->qf_ptr = qfl->qf_start;
8683 qfl->qf_index = 1;
8684 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008685 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 }
8687
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008688 if (p_cpo == empty_option)
8689 p_cpo = save_cpo;
8690 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008691 {
8692 // Darn, some plugin changed the value. If it's still empty it was
8693 // changed and restored, need to restore in the complicated way.
8694 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008695 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008696 if (save_cpo_allocated)
8697 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008698 }
8699
8700 if (updated)
8701 // This may open a window and source scripts, do this after 'cpo' was
8702 // restored.
8703 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704
Bram Moolenaar73633f82012-01-20 13:39:07 +01008705 if (au_name != NULL)
8706 {
8707 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8708 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008709 // When adding a location list to an existing location list stack,
8710 // if the autocmd made the stack invalid, then just return.
8711 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008712 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008713 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008714 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008715 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008716 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008717
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008718 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008719 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008720 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008721 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008722 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008723
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008724 decr_quickfix_busy();
8725
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008726 if (eap->cmdidx == CMD_lhelpgrep)
8727 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008728 // If the help window is not opened or if it already points to the
8729 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008730 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008731 {
8732 if (new_qi)
8733 ll_free_all(&qi);
8734 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008735 else if (curwin->w_llist == NULL && new_qi)
8736 // current window didn't have a location list associated with it
8737 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008738 curwin->w_llist = qi;
8739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008741
8742# if defined(EXITFREE) || defined(PROTO)
8743 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00008744free_quickfix(void)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008745{
8746 win_T *win;
8747 tabpage_T *tab;
8748
8749 qf_free_all(NULL);
8750 // Free all location lists
8751 FOR_ALL_TAB_WINDOWS(tab, win)
8752 qf_free_all(win);
8753
8754 ga_clear(&qfga);
8755}
8756# endif
8757
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008758#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008759
8760#if defined(FEAT_EVAL) || defined(PROTO)
8761# ifdef FEAT_QUICKFIX
8762 static void
8763get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8764{
8765 if (what_arg->v_type == VAR_UNKNOWN)
8766 {
8767 if (rettv_list_alloc(rettv) == OK)
8768 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008769 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008770 }
8771 else
8772 {
8773 if (rettv_dict_alloc(rettv) == OK)
8774 if (is_qf || (wp != NULL))
8775 {
8776 if (what_arg->v_type == VAR_DICT)
8777 {
8778 dict_T *d = what_arg->vval.v_dict;
8779
8780 if (d != NULL)
8781 qf_get_properties(wp, d, rettv->vval.v_dict);
8782 }
8783 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008784 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008785 }
8786 }
8787}
8788# endif
8789
8790/*
8791 * "getloclist()" function
8792 */
8793 void
8794f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8795{
8796# ifdef FEAT_QUICKFIX
8797 win_T *wp;
8798
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008799 if (in_vim9script()
8800 && (check_for_number_arg(argvars, 0) == FAIL
8801 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8802 return;
8803
Bram Moolenaare677df82019-09-02 22:31:11 +02008804 wp = find_win_by_nr_or_id(&argvars[0]);
8805 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8806# endif
8807}
8808
8809/*
8810 * "getqflist()" function
8811 */
8812 void
8813f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8814{
8815# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008816 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8817 return;
8818
Bram Moolenaare677df82019-09-02 22:31:11 +02008819 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8820# endif
8821}
8822
8823/*
8824 * Used by "setqflist()" and "setloclist()" functions
8825 */
8826 static void
8827set_qf_ll_list(
8828 win_T *wp UNUSED,
8829 typval_T *list_arg UNUSED,
8830 typval_T *action_arg UNUSED,
8831 typval_T *what_arg UNUSED,
8832 typval_T *rettv)
8833{
8834# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008835 char_u *act;
8836 int action = 0;
8837 static int recursive = 0;
8838# endif
8839
8840 rettv->vval.v_number = -1;
8841
8842# ifdef FEAT_QUICKFIX
8843 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008844 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008845 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008846 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008847 else
8848 {
8849 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008850 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008851 int valid_dict = TRUE;
8852
8853 if (action_arg->v_type == VAR_STRING)
8854 {
8855 act = tv_get_string_chk(action_arg);
8856 if (act == NULL)
8857 return; // type error; errmsg already given
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008858 if ((*act == 'a' || *act == 'r' || *act == 'u' || *act == ' ' || *act == 'f') &&
Bram Moolenaare677df82019-09-02 22:31:11 +02008859 act[1] == NUL)
8860 action = *act;
8861 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008862 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008863 }
8864 else if (action_arg->v_type == VAR_UNKNOWN)
8865 action = ' ';
8866 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008867 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008868
8869 if (action_arg->v_type != VAR_UNKNOWN
8870 && what_arg->v_type != VAR_UNKNOWN)
8871 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008872 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8873 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008874 else
8875 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008876 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008877 valid_dict = FALSE;
8878 }
8879 }
8880
8881 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008882 if (l != NULL && action && valid_dict
8883 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008884 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008885 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008886 rettv->vval.v_number = 0;
8887 --recursive;
8888 }
8889# endif
8890}
8891
8892/*
8893 * "setloclist()" function
8894 */
8895 void
8896f_setloclist(typval_T *argvars, typval_T *rettv)
8897{
8898 win_T *win;
8899
8900 rettv->vval.v_number = -1;
8901
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008902 if (in_vim9script()
8903 && (check_for_number_arg(argvars, 0) == FAIL
8904 || check_for_list_arg(argvars, 1) == FAIL
8905 || check_for_opt_string_arg(argvars, 2) == FAIL
8906 || (argvars[2].v_type != VAR_UNKNOWN
8907 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8908 return;
8909
Bram Moolenaare677df82019-09-02 22:31:11 +02008910 win = find_win_by_nr_or_id(&argvars[0]);
8911 if (win != NULL)
8912 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8913}
8914
8915/*
8916 * "setqflist()" function
8917 */
8918 void
8919f_setqflist(typval_T *argvars, typval_T *rettv)
8920{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008921 if (in_vim9script()
8922 && (check_for_list_arg(argvars, 0) == FAIL
8923 || check_for_opt_string_arg(argvars, 1) == FAIL
8924 || (argvars[1].v_type != VAR_UNKNOWN
8925 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8926 return;
8927
Bram Moolenaare677df82019-09-02 22:31:11 +02008928 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8929}
8930#endif