blob: f85fff56f23de4dc94ecfad62704368a7292cd53 [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
39 char_u *qf_pattern; // search pattern for the error
40 char_u *qf_text; // description of the error
thinca6864efa2021-06-19 20:45:20 +020041 char_u qf_viscol; // set to TRUE if qf_col and qf_end_col is
42 // screen column
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020043 char_u qf_cleared; // set to TRUE if line has been deleted
44 char_u qf_type; // type of the error (mostly 'E'); 1 for
45 // :helpgrep
46 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000047};
48
49/*
50 * There is a stack of error lists.
51 */
52#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020053#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010054#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020056/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010057 * Quickfix list type.
58 */
59typedef enum
60{
61 QFLT_QUICKFIX, // Quickfix list - global list
62 QFLT_LOCATION, // Location list - per window list
63 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
64} qfltype_T;
65
66/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 * Quickfix/Location list definition
68 * Contains a list of entries (qfline_T). qf_start points to the first entry
69 * and qf_last points to the last entry. qf_count contains the list size.
70 *
71 * Usually the list contains one or more entries. But an empty list can be
72 * created using setqflist()/setloclist() with a title and/or user context
73 * information and entries can be added later using setqflist()/setloclist().
74 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000075typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020077 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010078 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020079 qfline_T *qf_start; // pointer to the first error
80 qfline_T *qf_last; // pointer to the last error
81 qfline_T *qf_ptr; // pointer to the current error
82 int qf_count; // number of errors (0 means empty list)
83 int qf_index; // current index in the error list
84 int qf_nonevalid; // TRUE if not a single valid entry found
85 char_u *qf_title; // title derived from the command that created
86 // the error list or set by setqflist
87 typval_T *qf_ctx; // context set by setqflist/setloclist
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +010088 callback_T qf_qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020089
90 struct dir_stack_T *qf_dir_stack;
91 char_u *qf_directory;
92 struct dir_stack_T *qf_file_stack;
93 char_u *qf_currfile;
94 int qf_multiline;
95 int qf_multiignore;
96 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010097 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000098} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar6a8958d2017-06-22 21:33:20 +0200100/*
101 * Quickfix/Location list stack definition
102 * Contains a list of quickfix/location lists (qf_list_T)
103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000104struct qf_info_S
105{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 // Count of references to this list. Used only for location lists.
107 // When a location list window reference this list, qf_refcount
108 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
109 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200111 int qf_listcount; // current number of lists
112 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100114 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100115 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000116};
117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200118static qf_info_T ql_info; // global quickfix list
119static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
haya14busae023d492022-02-08 18:09:29 +0000121#define FMT_PATTERNS 13 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
124 * Structure used to hold the info of one part of 'errorformat'
125 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000126typedef struct efm_S efm_T;
127struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200129 regprog_T *prog; // pre-formatted part of 'errorformat'
130 efm_T *next; // pointer to next (NULL if last)
131 char_u addr[FMT_PATTERNS]; // indices of used % patterns
132 char_u prefix; // prefix of this format line:
133 // 'D' enter directory
134 // 'X' leave directory
135 // 'A' start of multi-line message
136 // 'E' error message
137 // 'W' warning message
138 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200139 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200140 // 'C' continuation line
141 // 'Z' end of multi-line message
142 // 'G' general, unspecific message
143 // 'P' push file (partial) message
144 // 'Q' pop/quit file (partial) message
145 // 'O' overread (partial) message
146 char_u flags; // additional flags given in prefix
147 // '-' do not include this line
148 // '+' include whole line in message
149 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150};
151
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200152// List of location lists to be deleted.
153// Used to delay the deletion of locations lists by autocmds.
154typedef struct qf_delq_S
155{
156 struct qf_delq_S *next;
157 qf_info_T *qi;
158} qf_delq_T;
159static qf_delq_T *qf_delq_head = NULL;
160
161// Counter to prevent autocmds from freeing up location lists when they are
162// still being used.
163static int quickfix_busy = 0;
164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200165static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100166
Bram Moolenaard43906d2020-07-20 21:31:32 +0200167// callback function for 'quickfixtextfunc'
168static callback_T qftf_cb;
169
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_new_list(qf_info_T *qi, char_u *qf_title);
thinca6864efa2021-06-19 20:45:20 +0200171static 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, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200172static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100174static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200175static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200177static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200178static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +0100179static void qf_fmt_text(garray_T *gap, char_u *text);
180static void qf_range_text(garray_T *gap, qfline_T *qfp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100181static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static win_T *qf_find_win(qf_info_T *qi);
183static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200184static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200185static 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 +0100186static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
187static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
188static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
189static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200191// Quickfix window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000192#define IS_QF_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200193// Location list window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000194#define IS_LL_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200195
196// Quickfix and location list stack check helper macros
kylo252ae6f1d82022-02-16 19:24:07 +0000197#define IS_QF_STACK(qi) ((qi)->qfl_type == QFLT_QUICKFIX)
198#define IS_LL_STACK(qi) ((qi)->qfl_type == QFLT_LOCATION)
199#define IS_QF_LIST(qfl) ((qfl)->qfl_type == QFLT_QUICKFIX)
200#define IS_LL_LIST(qfl) ((qfl)->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200201
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000202/*
203 * Return location list for window 'wp'
204 * For location list window, return the referenced location list
205 */
kylo252ae6f1d82022-02-16 19:24:07 +0000206#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? (wp)->w_llist_ref : (wp)->w_llist)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000207
Bram Moolenaar95946f12019-03-31 15:31:59 +0200208// Macro to loop through all the items in a quickfix list
209// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100210#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
kylo252ae6f1d82022-02-16 19:24:07 +0000211 for ((i) = 1, (qfp) = (qfl)->qf_start; \
212 !got_int && (i) <= (qfl)->qf_count && (qfp) != NULL; \
213 ++(i), (qfp) = (qfp)->qf_next)
Bram Moolenaara16123a2019-03-28 20:31:07 +0100214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200216 * Looking up a buffer can be slow if there are many. Remember the last one
217 * to make this a lot faster if there are multiple matches in the same file.
218 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200219static char_u *qf_last_bufname = NULL;
220static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200221
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +0100222static garray_T qfga;
223
224/*
225 * Get a growarray to buffer text in. Shared between various commands to avoid
226 * many alloc/free calls.
227 */
228 static garray_T *
229qfga_get(void)
230{
231 static int initialized = FALSE;
232
233 if (!initialized)
234 {
235 initialized = TRUE;
236 ga_init2(&qfga, 1, 256);
237 }
238
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +0100239 // Reset the length to zero. Retain ga_data from previous use to avoid
240 // many alloc/free calls.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +0100241 qfga.ga_len = 0;
242
243 return &qfga;
244}
245
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200246/*
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +0100247 * The "qfga" grow array buffer is reused across multiple quickfix commands as
248 * a temporary buffer to reduce the number of alloc/free calls. But if the
249 * buffer size is large, then to avoid holding on to that memory, clear the
250 * grow array. Otherwise just reset the grow array length.
251 */
252 static void
253qfga_clear(void)
254{
255 if (qfga.ga_maxlen > 1000)
256 ga_clear(&qfga);
257 else
258 qfga.ga_len = 0;
259}
260
261/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200262 * Maximum number of bytes allowed per line while reading a errorfile.
263 */
264#define LINE_MAXLEN 4096
265
haya14busae023d492022-02-08 18:09:29 +0000266/*
267 * Patterns used. Keep in sync with qf_parse_fmt[].
268 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200269static struct fmtpattern
270{
271 char_u convchar;
272 char *pattern;
273} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200274 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200275 {'f', ".\\+"}, // only used when at end
haya14busae023d492022-02-08 18:09:29 +0000276 {'n', "\\d\\+"}, // 1
277 {'l', "\\d\\+"}, // 2
278 {'e', "\\d\\+"}, // 3
279 {'c', "\\d\\+"}, // 4
280 {'k', "\\d\\+"}, // 5
281 {'t', "."}, // 6
282#define FMT_PATTERN_M 7
283 {'m', ".\\+"}, // 7
284#define FMT_PATTERN_R 8
285 {'r', ".*"}, // 8
286 {'p', "[- .]*"}, // 9
287 {'v', "\\d\\+"}, // 10
288 {'s', ".\\+"}, // 11
289 {'o', ".\\+"} // 12
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200290 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200291
292/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200293 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200294 * See fmt_pat definition above for the list of supported patterns. The
295 * pattern specifier is supplied in "efmpat". The converted pattern is stored
296 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 */
298 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200299efmpat_to_regpat(
300 char_u *efmpat,
301 char_u *regpat,
302 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100304 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305{
306 char_u *srcptr;
307
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200308 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200309 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200310 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000311 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312 return NULL;
313 }
haya14busae023d492022-02-08 18:09:29 +0000314 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200315 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000316 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200317 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200318 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000319 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200320 return NULL;
321 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200322 efminfo->addr[idx] = (char_u)++round;
323 *regpat++ = '\\';
324 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200326 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200327 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200328 // Also match "c:" in the file name, even when
329 // checking for a colon next: "%f:".
330 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200331 STRCPY(regpat, "\\%(\\a:\\)\\=");
332 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 }
334#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200335 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200337 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200339 // A file name may contain spaces, but this isn't
340 // in "\f". For "%f:%l:%m" there may be a ":" in
341 // the file name. Use ".\{-1,}x" instead (x is
342 // the next character), the requirement that :999:
343 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200344 STRCPY(regpat, ".\\{-1,}");
345 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 }
347 else
348 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200349 // File name followed by '\\' or '%': include as
350 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 STRCPY(regpat, "\\f\\+");
352 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353 }
354 }
355 else
356 {
357 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200358 while ((*regpat = *srcptr++) != NUL)
359 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200360 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 *regpat++ = '\\';
362 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200363
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200364 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200365}
366
367/*
368 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200369 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370 */
371 static char_u *
372scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200373 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200374 char_u *efm,
375 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100376 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200377{
378 char_u *efmp = *pefmp;
379
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200381 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200382 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200383 {
384 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200385 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200386 if (efmp < efm + len)
387 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200388 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200389 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200391 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392 if (efmp == efm + len)
393 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000394 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200395 return NULL;
396 }
397 }
398 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200399 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200400 *regpat++ = *++efmp;
401 *regpat++ = '\\';
402 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200403 }
404 else
405 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200406 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000407 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200408 return NULL;
409 }
410
411 *pefmp = efmp;
412
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200413 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200414}
415
416/*
417 * Analyze/parse an errorformat prefix.
418 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200419 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100420efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200421{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200422 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200423 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200424 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200425 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200426 else
427 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000428 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200429 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200430 }
431
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200432 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200433}
434
435/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200436 * Converts a 'errorformat' string part in 'efm' to a regular expression
437 * pattern. The resulting regex pattern is returned in "regpat". Additional
438 * information about the 'erroformat' pattern is returned in "fmt_ptr".
439 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200440 */
441 static int
442efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200443 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200444 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200445 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100446 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447{
448 char_u *ptr;
449 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200450 int round;
451 int idx = 0;
452
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200453 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200454 ptr = regpat;
455 *ptr++ = '^';
456 round = 0;
457 for (efmp = efm; efmp < efm + len; ++efmp)
458 {
459 if (*efmp == '%')
460 {
461 ++efmp;
462 for (idx = 0; idx < FMT_PATTERNS; ++idx)
463 if (fmt_pat[idx].convchar == *efmp)
464 break;
465 if (idx < FMT_PATTERNS)
466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100467 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200468 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200469 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200470 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200471 }
472 else if (*efmp == '*')
473 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200474 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100475 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200476 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200477 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200478 }
479 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200480 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200481 else if (*efmp == '#')
482 *ptr++ = '*';
483 else if (*efmp == '>')
484 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200485 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200486 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200487 // prefix is allowed only at the beginning of the errorformat
488 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100489 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200490 if (efmp == NULL)
491 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200492 }
493 else
494 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000495 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200496 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200497 }
498 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200499 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200500 {
501 if (*efmp == '\\' && efmp + 1 < efm + len)
502 ++efmp;
503 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200504 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200505 if (*efmp)
506 *ptr++ = *efmp;
507 }
508 }
509 *ptr++ = '$';
510 *ptr = NUL;
511
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200512 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200513}
514
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200515/*
516 * Free the 'errorformat' information list
517 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200518 static void
519free_efm_list(efm_T **efm_first)
520{
521 efm_T *efm_ptr;
522
523 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
524 {
525 *efm_first = efm_ptr->next;
526 vim_regfree(efm_ptr->prog);
527 vim_free(efm_ptr);
528 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100529 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200530}
531
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200532/*
533 * Compute the size of the buffer used to convert a 'errorformat' pattern into
534 * a regular expression pattern.
535 */
536 static int
537efm_regpat_bufsz(char_u *efm)
538{
539 int sz;
540 int i;
541
542 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
543 for (i = FMT_PATTERNS; i > 0; )
544 sz += (int)STRLEN(fmt_pat[--i].pattern);
545#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200546 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200547#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200548 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200549#endif
550
551 return sz;
552}
553
554/*
555 * Return the length of a 'errorformat' option part (separated by ",").
556 */
557 static int
558efm_option_part_len(char_u *efm)
559{
560 int len;
561
562 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
563 if (efm[len] == '\\' && efm[len + 1] != NUL)
564 ++len;
565
566 return len;
567}
568
569/*
570 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
571 * are parsed and converted to regular expressions. Returns information about
572 * the parsed 'errorformat' option.
573 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200574 static efm_T *
575parse_efm_option(char_u *efm)
576{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200577 efm_T *fmt_ptr = NULL;
578 efm_T *fmt_first = NULL;
579 efm_T *fmt_last = NULL;
580 char_u *fmtstr = NULL;
581 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200582 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200583
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200584 // Each part of the format string is copied and modified from errorformat
585 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200586
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200587 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200588 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000589 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200590 goto parse_efm_error;
591
592 while (efm[0] != NUL)
593 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200594 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000595 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200596 if (fmt_ptr == NULL)
597 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200598 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200599 fmt_first = fmt_ptr;
600 else
601 fmt_last->next = fmt_ptr;
602 fmt_last = fmt_ptr;
603
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200604 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200605 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200606
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100607 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200608 goto parse_efm_error;
609 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
610 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200611 // Advance to next part
612 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200613 }
614
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200615 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000616 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200617
618 goto parse_efm_end;
619
620parse_efm_error:
621 free_efm_list(&fmt_first);
622
623parse_efm_end:
624 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200625
626 return fmt_first;
627}
628
Bram Moolenaare0d37972016-07-15 22:36:01 +0200629enum {
630 QF_FAIL = 0,
631 QF_OK = 1,
632 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200633 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200634 QF_IGNORE_LINE = 4,
635 QF_MULTISCAN = 5,
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +0100636 QF_ABORT = 6
Bram Moolenaare0d37972016-07-15 22:36:01 +0200637};
638
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200639/*
640 * State information used to parse lines and add entries to a quickfix/location
641 * list.
642 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200643typedef struct {
644 char_u *linebuf;
645 int linelen;
646 char_u *growbuf;
647 int growbufsiz;
648 FILE *fd;
649 typval_T *tv;
650 char_u *p_str;
651 listitem_T *p_li;
652 buf_T *buf;
653 linenr_T buflnum;
654 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100655 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200656} qfstate_T;
657
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200658/*
659 * Allocate more memory for the line buffer used for parsing lines.
660 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200661 static char_u *
662qf_grow_linebuf(qfstate_T *state, int newsz)
663{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200664 char_u *p;
665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200666 // If the line exceeds LINE_MAXLEN exclude the last
667 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200668 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
669 if (state->growbuf == NULL)
670 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000671 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200672 if (state->growbuf == NULL)
673 return NULL;
674 state->growbufsiz = state->linelen;
675 }
676 else if (state->linelen > state->growbufsiz)
677 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200678 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200679 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200680 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 state->growbufsiz = state->linelen;
682 }
683 return state->growbuf;
684}
685
686/*
687 * Get the next string (separated by newline) from state->p_str.
688 */
689 static int
690qf_get_next_str_line(qfstate_T *state)
691{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200692 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200693 char_u *p_str = state->p_str;
694 char_u *p;
695 int len;
696
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200697 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200698 return QF_END_OF_INPUT;
699
700 p = vim_strchr(p_str, '\n');
701 if (p != NULL)
702 len = (int)(p - p_str) + 1;
703 else
704 len = (int)STRLEN(p_str);
705
706 if (len > IOSIZE - 2)
707 {
708 state->linebuf = qf_grow_linebuf(state, len);
709 if (state->linebuf == NULL)
710 return QF_NOMEM;
711 }
712 else
713 {
714 state->linebuf = IObuff;
715 state->linelen = len;
716 }
717 vim_strncpy(state->linebuf, p_str, state->linelen);
718
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200719 // Increment using len in order to discard the rest of the
720 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200721 p_str += len;
722 state->p_str = p_str;
723
724 return QF_OK;
725}
726
727/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000728 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200729 */
730 static int
731qf_get_next_list_line(qfstate_T *state)
732{
733 listitem_T *p_li = state->p_li;
734 int len;
735
736 while (p_li != NULL
737 && (p_li->li_tv.v_type != VAR_STRING
738 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200739 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200740
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200741 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200742 {
743 state->p_li = NULL;
744 return QF_END_OF_INPUT;
745 }
746
747 len = (int)STRLEN(p_li->li_tv.vval.v_string);
748 if (len > IOSIZE - 2)
749 {
750 state->linebuf = qf_grow_linebuf(state, len);
751 if (state->linebuf == NULL)
752 return QF_NOMEM;
753 }
754 else
755 {
756 state->linebuf = IObuff;
757 state->linelen = len;
758 }
759
760 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
761
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200762 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 return QF_OK;
764}
765
766/*
767 * Get the next string from state->buf.
768 */
769 static int
770qf_get_next_buf_line(qfstate_T *state)
771{
772 char_u *p_buf = NULL;
773 int len;
774
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200775 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200776 if (state->buflnum > state->lnumlast)
777 return QF_END_OF_INPUT;
778
779 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
780 state->buflnum += 1;
781
782 len = (int)STRLEN(p_buf);
783 if (len > IOSIZE - 2)
784 {
785 state->linebuf = qf_grow_linebuf(state, len);
786 if (state->linebuf == NULL)
787 return QF_NOMEM;
788 }
789 else
790 {
791 state->linebuf = IObuff;
792 state->linelen = len;
793 }
794 vim_strncpy(state->linebuf, p_buf, state->linelen);
795
796 return QF_OK;
797}
798
799/*
800 * Get the next string from file state->fd.
801 */
802 static int
803qf_get_next_file_line(qfstate_T *state)
804{
805 int discard;
806 int growbuflen;
807
808 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
809 return QF_END_OF_INPUT;
810
811 discard = FALSE;
812 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200813 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200814 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200815 // The current line exceeds IObuff, continue reading using
816 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200817 if (state->growbuf == NULL)
818 {
819 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000820 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200821 if (state->growbuf == NULL)
822 return QF_NOMEM;
823 }
824
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200825 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200826 memcpy(state->growbuf, IObuff, IOSIZE - 1);
827 growbuflen = state->linelen;
828
829 for (;;)
830 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200831 char_u *p;
832
Bram Moolenaare0d37972016-07-15 22:36:01 +0200833 if (fgets((char *)state->growbuf + growbuflen,
834 state->growbufsiz - growbuflen, state->fd) == NULL)
835 break;
836 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
837 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200838 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200839 break;
840 if (state->growbufsiz == LINE_MAXLEN)
841 {
842 discard = TRUE;
843 break;
844 }
845
846 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
847 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200848 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200849 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200850 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200851 }
852
853 while (discard)
854 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200855 // The current line is longer than LINE_MAXLEN, continue
856 // reading but discard everything until EOL or EOF is
857 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200858 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
859 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200860 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200861 break;
862 }
863
864 state->linebuf = state->growbuf;
865 state->linelen = growbuflen;
866 }
867 else
868 state->linebuf = IObuff;
869
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200870 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200871 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
872 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100873 char_u *line;
874
875 line = string_convert(&state->vc, state->linebuf, &state->linelen);
876 if (line != NULL)
877 {
878 if (state->linelen < IOSIZE)
879 {
880 STRCPY(state->linebuf, line);
881 vim_free(line);
882 }
883 else
884 {
885 vim_free(state->growbuf);
886 state->linebuf = state->growbuf = line;
887 state->growbufsiz = state->linelen < LINE_MAXLEN
888 ? state->linelen : LINE_MAXLEN;
889 }
890 }
891 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100892
Bram Moolenaare0d37972016-07-15 22:36:01 +0200893 return QF_OK;
894}
895
896/*
897 * Get the next string from a file/buffer/list/string.
898 */
899 static int
900qf_get_nextline(qfstate_T *state)
901{
902 int status = QF_FAIL;
903
904 if (state->fd == NULL)
905 {
906 if (state->tv != NULL)
907 {
908 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200909 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200910 status = qf_get_next_str_line(state);
911 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200912 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200913 status = qf_get_next_list_line(state);
914 }
915 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200916 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200917 status = qf_get_next_buf_line(state);
918 }
919 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200920 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200921 status = qf_get_next_file_line(state);
922
923 if (status != QF_OK)
924 return status;
925
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200926 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200927 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200928 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200929 state->linebuf[state->linelen - 1] = NUL;
930#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200931 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
932 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200933#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200934 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200935
Bram Moolenaare0d37972016-07-15 22:36:01 +0200936 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200937
938 return QF_OK;
939}
940
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200941typedef struct {
942 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200943 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200944 char_u *errmsg;
945 int errmsglen;
946 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200947 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200948 int col;
thinca6864efa2021-06-19 20:45:20 +0200949 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200950 char_u use_viscol;
951 char_u *pattern;
952 int enr;
953 int type;
954 int valid;
955} qffields_T;
956
957/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200958 * Parse the match for filename ('%f') pattern in regmatch.
959 * Return the matched value in "fields->namebuf".
960 */
961 static int
962qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
963{
964 int c;
965
966 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
967 return QF_FAIL;
968
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200969 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200970 c = *rmp->endp[midx];
971 *rmp->endp[midx] = NUL;
972 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
973 *rmp->endp[midx] = c;
974
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200975 // For separate filename patterns (%O, %P and %Q), the specified file
976 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200977 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
978 && mch_getperm(fields->namebuf) == -1)
979 return QF_FAIL;
980
981 return QF_OK;
982}
983
984/*
985 * Parse the match for error number ('%n') pattern in regmatch.
986 * Return the matched value in "fields->enr".
987 */
988 static int
989qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
990{
991 if (rmp->startp[midx] == NULL)
992 return QF_FAIL;
993 fields->enr = (int)atol((char *)rmp->startp[midx]);
994 return QF_OK;
995}
996
997/*
haya14busae023d492022-02-08 18:09:29 +0000998 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200999 * Return the matched value in "fields->lnum".
1000 */
1001 static int
1002qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
1003{
1004 if (rmp->startp[midx] == NULL)
1005 return QF_FAIL;
1006 fields->lnum = atol((char *)rmp->startp[midx]);
1007 return QF_OK;
1008}
1009
1010/*
haya14busae023d492022-02-08 18:09:29 +00001011 * Parse the match for end line number ('%e') pattern in regmatch.
1012 * Return the matched value in "fields->end_lnum".
1013 */
1014 static int
1015qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
1016{
1017 if (rmp->startp[midx] == NULL)
1018 return QF_FAIL;
1019 fields->end_lnum = atol((char *)rmp->startp[midx]);
1020 return QF_OK;
1021}
1022
1023/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001024 * Parse the match for column number ('%c') pattern in regmatch.
1025 * Return the matched value in "fields->col".
1026 */
1027 static int
1028qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
1029{
1030 if (rmp->startp[midx] == NULL)
1031 return QF_FAIL;
1032 fields->col = (int)atol((char *)rmp->startp[midx]);
1033 return QF_OK;
1034}
1035
1036/*
haya14busae023d492022-02-08 18:09:29 +00001037 * Parse the match for end column number ('%k') pattern in regmatch.
1038 * Return the matched value in "fields->end_col".
1039 */
1040 static int
1041qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1042{
1043 if (rmp->startp[midx] == NULL)
1044 return QF_FAIL;
1045 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1046 return QF_OK;
1047}
1048
1049/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001050 * Parse the match for error type ('%t') pattern in regmatch.
1051 * Return the matched value in "fields->type".
1052 */
1053 static int
1054qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1055{
1056 if (rmp->startp[midx] == NULL)
1057 return QF_FAIL;
1058 fields->type = *rmp->startp[midx];
1059 return QF_OK;
1060}
1061
1062/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001063 * Copy a non-error line into the error string. Return the matched line in
1064 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001065 */
1066 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001067copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001068{
1069 char_u *p;
1070
1071 if (linelen >= fields->errmsglen)
1072 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001073 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001074 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1075 return QF_NOMEM;
1076 fields->errmsg = p;
1077 fields->errmsglen = linelen + 1;
1078 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001079 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001080 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001081
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001082 return QF_OK;
1083}
1084
1085/*
1086 * Parse the match for error message ('%m') pattern in regmatch.
1087 * Return the matched value in "fields->errmsg".
1088 */
1089 static int
1090qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1091{
1092 char_u *p;
1093 int len;
1094
1095 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1096 return QF_FAIL;
1097 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1098 if (len >= fields->errmsglen)
1099 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001100 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001101 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1102 return QF_NOMEM;
1103 fields->errmsg = p;
1104 fields->errmsglen = len + 1;
1105 }
1106 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1107 return QF_OK;
1108}
1109
1110/*
1111 * Parse the match for rest of a single-line file message ('%r') pattern.
1112 * Return the matched value in "tail".
1113 */
1114 static int
1115qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1116{
1117 if (rmp->startp[midx] == NULL)
1118 return QF_FAIL;
1119 *tail = rmp->startp[midx];
1120 return QF_OK;
1121}
1122
1123/*
1124 * Parse the match for the pointer line ('%p') pattern in regmatch.
1125 * Return the matched value in "fields->col".
1126 */
1127 static int
1128qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1129{
1130 char_u *match_ptr;
1131
1132 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1133 return QF_FAIL;
1134 fields->col = 0;
1135 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1136 ++match_ptr)
1137 {
1138 ++fields->col;
1139 if (*match_ptr == TAB)
1140 {
1141 fields->col += 7;
1142 fields->col -= fields->col % 8;
1143 }
1144 }
1145 ++fields->col;
1146 fields->use_viscol = TRUE;
1147 return QF_OK;
1148}
1149
1150/*
1151 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1152 * Return the matched value in "fields->col".
1153 */
1154 static int
1155qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1156{
1157 if (rmp->startp[midx] == NULL)
1158 return QF_FAIL;
1159 fields->col = (int)atol((char *)rmp->startp[midx]);
1160 fields->use_viscol = TRUE;
1161 return QF_OK;
1162}
1163
1164/*
1165 * Parse the match for the search text ('%s') pattern in regmatch.
1166 * Return the matched value in "fields->pattern".
1167 */
1168 static int
1169qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1170{
1171 int len;
1172
1173 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1174 return QF_FAIL;
1175 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1176 if (len > CMDBUFFSIZE - 5)
1177 len = CMDBUFFSIZE - 5;
1178 STRCPY(fields->pattern, "^\\V");
1179 STRNCAT(fields->pattern, rmp->startp[midx], len);
1180 fields->pattern[len + 3] = '\\';
1181 fields->pattern[len + 4] = '$';
1182 fields->pattern[len + 5] = NUL;
1183 return QF_OK;
1184}
1185
1186/*
1187 * Parse the match for the module ('%o') pattern in regmatch.
1188 * Return the matched value in "fields->module".
1189 */
1190 static int
1191qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1192{
1193 int len;
1194
1195 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1196 return QF_FAIL;
1197 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1198 if (len > CMDBUFFSIZE)
1199 len = CMDBUFFSIZE;
1200 STRNCAT(fields->module, rmp->startp[midx], len);
1201 return QF_OK;
1202}
1203
1204/*
1205 * 'errorformat' format pattern parser functions.
1206 * The '%f' and '%r' formats are parsed differently from other formats.
1207 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001208 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001209 */
1210static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1211{
haya14busae023d492022-02-08 18:09:29 +00001212 NULL, // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001213 qf_parse_fmt_n,
1214 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001215 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001216 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001217 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001218 qf_parse_fmt_t,
1219 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001220 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001221 qf_parse_fmt_p,
1222 qf_parse_fmt_v,
1223 qf_parse_fmt_s,
1224 qf_parse_fmt_o
1225};
1226
1227/*
1228 * Parse the error format pattern matches in "regmatch" and set the values in
1229 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1230 * match. Returns QF_OK if all the matches are successfully parsed. On
1231 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001232 */
1233 static int
1234qf_parse_match(
1235 char_u *linebuf,
1236 int linelen,
1237 efm_T *fmt_ptr,
1238 regmatch_T *regmatch,
1239 qffields_T *fields,
1240 int qf_multiline,
1241 int qf_multiscan,
1242 char_u **tail)
1243{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001244 int idx = fmt_ptr->prefix;
1245 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001246 int midx;
1247 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001248
1249 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1250 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001251 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001252 fields->type = idx;
1253 else
1254 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001255
1256 // Extract error message data from matched line.
1257 // We check for an actual submatch, because "\[" and "\]" in
1258 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001259 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001260 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001261 status = QF_OK;
1262 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001263 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001264 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001265 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001266 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001267 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001268 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001269 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001270 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001271 }
haya14busae023d492022-02-08 18:09:29 +00001272 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001273 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001274 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001275 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001276
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001277 if (status != QF_OK)
1278 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001279 }
1280
1281 return QF_OK;
1282}
1283
1284/*
1285 * Parse an error line in 'linebuf' using a single error format string in
1286 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1287 * Returns QF_OK if the efm format matches completely and the fields are
1288 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1289 */
1290 static int
1291qf_parse_get_fields(
1292 char_u *linebuf,
1293 int linelen,
1294 efm_T *fmt_ptr,
1295 qffields_T *fields,
1296 int qf_multiline,
1297 int qf_multiscan,
1298 char_u **tail)
1299{
1300 regmatch_T regmatch;
1301 int status = QF_FAIL;
1302 int r;
1303
1304 if (qf_multiscan &&
1305 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1306 return QF_FAIL;
1307
1308 fields->namebuf[0] = NUL;
1309 fields->module[0] = NUL;
1310 fields->pattern[0] = NUL;
1311 if (!qf_multiscan)
1312 fields->errmsg[0] = NUL;
1313 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001314 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001316 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001317 fields->use_viscol = FALSE;
1318 fields->enr = -1;
1319 fields->type = 0;
1320 *tail = NULL;
1321
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001322 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001323 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001324 regmatch.regprog = fmt_ptr->prog;
1325 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1326 fmt_ptr->prog = regmatch.regprog;
1327 if (r)
1328 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1329 fields, qf_multiline, qf_multiscan, tail);
1330
1331 return status;
1332}
1333
1334/*
1335 * Parse directory error format prefixes (%D and %X).
1336 * Push and pop directories from the directory stack when scanning directory
1337 * names.
1338 */
1339 static int
1340qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1341{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001342 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001343 {
1344 if (*fields->namebuf == NUL)
1345 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001346 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001347 return QF_FAIL;
1348 }
1349 qfl->qf_directory =
1350 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1351 if (qfl->qf_directory == NULL)
1352 return QF_FAIL;
1353 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001354 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001355 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1356
1357 return QF_OK;
1358}
1359
1360/*
1361 * Parse global file name error format prefixes (%O, %P and %Q).
1362 */
1363 static int
1364qf_parse_file_pfx(
1365 int idx,
1366 qffields_T *fields,
1367 qf_list_T *qfl,
1368 char_u *tail)
1369{
1370 fields->valid = FALSE;
1371 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1372 {
1373 if (*fields->namebuf && idx == 'P')
1374 qfl->qf_currfile =
1375 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1376 else if (idx == 'Q')
1377 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1378 *fields->namebuf = NUL;
1379 if (tail && *tail)
1380 {
1381 STRMOVE(IObuff, skipwhite(tail));
1382 qfl->qf_multiscan = TRUE;
1383 return QF_MULTISCAN;
1384 }
1385 }
1386
1387 return QF_OK;
1388}
1389
1390/*
1391 * Parse a non-error line (a line which doesn't match any of the error
1392 * format in 'efm').
1393 */
1394 static int
1395qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1396{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001397 fields->namebuf[0] = NUL; // no match found, remove file name
1398 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001399 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001400
Bram Moolenaarf4140482020-02-15 23:06:45 +01001401 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001402}
1403
1404/*
1405 * Parse multi-line error format prefixes (%C and %Z)
1406 */
1407 static int
1408qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001409 int idx,
1410 qf_list_T *qfl,
1411 qffields_T *fields)
1412{
1413 char_u *ptr;
1414 int len;
1415
1416 if (!qfl->qf_multiignore)
1417 {
1418 qfline_T *qfprev = qfl->qf_last;
1419
1420 if (qfprev == NULL)
1421 return QF_FAIL;
1422 if (*fields->errmsg && !qfl->qf_multiignore)
1423 {
1424 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001425 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1426 aid_qf_multiline_pfx);
1427 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001428 return QF_FAIL;
1429 STRCPY(ptr, qfprev->qf_text);
1430 vim_free(qfprev->qf_text);
1431 qfprev->qf_text = ptr;
1432 *(ptr += len) = '\n';
1433 STRCPY(++ptr, fields->errmsg);
1434 }
1435 if (qfprev->qf_nr == -1)
1436 qfprev->qf_nr = fields->enr;
1437 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001438 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001439 qfprev->qf_type = fields->type;
1440
1441 if (!qfprev->qf_lnum)
1442 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001443 if (!qfprev->qf_end_lnum)
1444 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001445 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001446 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001447 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001448 qfprev->qf_viscol = fields->use_viscol;
1449 }
haya14busae023d492022-02-08 18:09:29 +00001450 if (!qfprev->qf_end_col)
1451 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001452 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001453 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001454 qfl->qf_directory,
1455 *fields->namebuf || qfl->qf_directory != NULL
1456 ? fields->namebuf
1457 : qfl->qf_currfile != NULL && fields->valid
1458 ? qfl->qf_currfile : 0);
1459 }
1460 if (idx == 'Z')
1461 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1462 line_breakcheck();
1463
1464 return QF_IGNORE_LINE;
1465}
1466
1467/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468 * Parse a line and get the quickfix fields.
1469 * Return the QF_ status.
1470 */
1471 static int
1472qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001473 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001474 char_u *linebuf,
1475 int linelen,
1476 efm_T *fmt_first,
1477 qffields_T *fields)
1478{
1479 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001480 int idx = 0;
1481 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001482 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001483
Bram Moolenaare333e792018-04-08 13:27:39 +02001484restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001485 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001486 if (fmt_start == NULL)
1487 fmt_ptr = fmt_first;
1488 else
1489 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001490 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001491 fmt_ptr = fmt_start;
1492 fmt_start = NULL;
1493 }
1494
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001495 // Try to match each part of 'errorformat' until we find a complete
1496 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001497 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001498 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1499 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001500 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001501 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1502 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1503 if (status == QF_NOMEM)
1504 return status;
1505 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001506 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001507 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001508 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001509
1510 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1511 {
1512 if (fmt_ptr != NULL)
1513 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001514 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001515 status = qf_parse_dir_pfx(idx, fields, qfl);
1516 if (status != QF_OK)
1517 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001518 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001519
1520 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1521 if (status != QF_OK)
1522 return status;
1523
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001524 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001525 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001526 }
1527 else if (fmt_ptr != NULL)
1528 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001529 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001530 if (fmt_ptr->conthere)
1531 fmt_start = fmt_ptr;
1532
Bram Moolenaare9283662020-06-07 14:10:47 +02001533 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001534 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001535 qfl->qf_multiline = TRUE; // start of a multi-line message
1536 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001537 }
1538 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001539 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001540 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001541 if (status != QF_OK)
1542 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001543 }
1544 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001545 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001546 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1547 if (status == QF_MULTISCAN)
1548 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001549 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001550 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001551 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001552 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001553 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001554 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001555 return QF_IGNORE_LINE;
1556 }
1557 }
1558
1559 return QF_OK;
1560}
1561
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001562/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001563 * Returns TRUE if the specified quickfix/location stack is empty
1564 */
1565 static int
1566qf_stack_empty(qf_info_T *qi)
1567{
1568 return qi == NULL || qi->qf_listcount <= 0;
1569}
1570
1571/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001572 * Returns TRUE if the specified quickfix/location list is empty.
1573 */
1574 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001575qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001576{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001577 return qfl == NULL || qfl->qf_count <= 0;
1578}
1579
1580/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001581 * Returns TRUE if the specified quickfix/location list is not empty and
1582 * has valid entries.
1583 */
1584 static int
1585qf_list_has_valid_entries(qf_list_T *qfl)
1586{
1587 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1588}
1589
1590/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001591 * Return a pointer to a list in the specified quickfix stack
1592 */
1593 static qf_list_T *
1594qf_get_list(qf_info_T *qi, int idx)
1595{
1596 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001597}
1598
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001599/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001600 * Allocate the fields used for parsing lines and populating a quickfix list.
1601 */
1602 static int
1603qf_alloc_fields(qffields_T *pfields)
1604{
1605 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1606 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1607 pfields->errmsglen = CMDBUFFSIZE + 1;
1608 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1609 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1610 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1611 || pfields->pattern == NULL || pfields->module == NULL)
1612 return FAIL;
1613
1614 return OK;
1615}
1616
1617/*
1618 * Free the fields used for parsing lines and populating a quickfix list.
1619 */
1620 static void
1621qf_free_fields(qffields_T *pfields)
1622{
1623 vim_free(pfields->namebuf);
1624 vim_free(pfields->module);
1625 vim_free(pfields->errmsg);
1626 vim_free(pfields->pattern);
1627}
1628
1629/*
1630 * Setup the state information used for parsing lines and populating a
1631 * quickfix list.
1632 */
1633 static int
1634qf_setup_state(
1635 qfstate_T *pstate,
1636 char_u *enc,
1637 char_u *efile,
1638 typval_T *tv,
1639 buf_T *buf,
1640 linenr_T lnumfirst,
1641 linenr_T lnumlast)
1642{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001643 pstate->vc.vc_type = CONV_NONE;
1644 if (enc != NULL && *enc != NUL)
1645 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001646
1647 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1648 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001649 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001650 return FAIL;
1651 }
1652
1653 if (tv != NULL)
1654 {
1655 if (tv->v_type == VAR_STRING)
1656 pstate->p_str = tv->vval.v_string;
1657 else if (tv->v_type == VAR_LIST)
1658 pstate->p_li = tv->vval.v_list->lv_first;
1659 pstate->tv = tv;
1660 }
1661 pstate->buf = buf;
1662 pstate->buflnum = lnumfirst;
1663 pstate->lnumlast = lnumlast;
1664
1665 return OK;
1666}
1667
1668/*
1669 * Cleanup the state information used for parsing lines and populating a
1670 * quickfix list.
1671 */
1672 static void
1673qf_cleanup_state(qfstate_T *pstate)
1674{
1675 if (pstate->fd != NULL)
1676 fclose(pstate->fd);
1677
1678 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001679 if (pstate->vc.vc_type != CONV_NONE)
1680 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001681}
1682
1683/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001684 * Process the next line from a file/buffer/list/string and add it
1685 * to the quickfix list 'qfl'.
1686 */
1687 static int
1688qf_init_process_nextline(
1689 qf_list_T *qfl,
1690 efm_T *fmt_first,
1691 qfstate_T *state,
1692 qffields_T *fields)
1693{
1694 int status;
1695
1696 // Get the next line from a file/buffer/list/string
1697 status = qf_get_nextline(state);
1698 if (status != QF_OK)
1699 return status;
1700
1701 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1702 fmt_first, fields);
1703 if (status != QF_OK)
1704 return status;
1705
1706 return qf_add_entry(qfl,
1707 qfl->qf_directory,
1708 (*fields->namebuf || qfl->qf_directory != NULL)
1709 ? fields->namebuf
1710 : ((qfl->qf_currfile != NULL && fields->valid)
1711 ? qfl->qf_currfile : (char_u *)NULL),
1712 fields->module,
1713 0,
1714 fields->errmsg,
1715 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001716 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001717 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001718 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001719 fields->use_viscol,
1720 fields->pattern,
1721 fields->enr,
1722 fields->type,
1723 fields->valid);
1724}
1725
1726/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001727 * Read the errorfile "efile" into memory, line by line, building the error
1728 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001729 * Alternative: when "efile" is NULL read errors from buffer "buf".
1730 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001731 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001732 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1733 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001734 * Return -1 for error, number of errors for success.
1735 */
1736 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001737qf_init_ext(
1738 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001739 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001740 char_u *efile,
1741 buf_T *buf,
1742 typval_T *tv,
1743 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001744 int newlist, // TRUE: start a new error list
1745 linenr_T lnumfirst, // first line number to use
1746 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001747 char_u *qf_title,
1748 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001749{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001750 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001751 qfstate_T state;
1752 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001753 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001754 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001755 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001757 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001758 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001759 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001761 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001762 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001763
Bram Moolenaara80faa82020-04-12 19:37:17 +02001764 CLEAR_FIELD(state);
1765 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001766 if ((qf_alloc_fields(&fields) == FAIL) ||
1767 (qf_setup_state(&state, enc, efile, tv, buf,
1768 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 goto qf_init_end;
1770
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001771 if (newlist || qf_idx == qi->qf_listcount)
1772 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001773 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001774 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001775 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001776 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001777 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001778 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001779 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001780 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001781 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001782 qfl = qf_get_list(qi, qf_idx);
1783 if (!qf_list_empty(qfl))
1784 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001787 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001788 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001789 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 else
1791 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001793 // If the errorformat didn't change between calls, then reuse the
1794 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001795 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1796 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001797 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001798 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001799 free_efm_list(&fmt_first);
1800
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001801 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001802 fmt_first = parse_efm_option(efm);
1803 if (fmt_first != NULL)
1804 last_efm = vim_strsave(efm);
1805 }
1806
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001807 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001810 // got_int is reset here, because it was probably set when killing the
1811 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 got_int = FALSE;
1813
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001814 // Read the lines in the error file one by one.
1815 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001816 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001818 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001819 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001820 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001821 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001822 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001823 if (status == QF_FAIL)
1824 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 line_breakcheck();
1827 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001828 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001830 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001832 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001833 qfl->qf_ptr = qfl->qf_start;
1834 qfl->qf_index = 1;
1835 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837 else
1838 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001839 qfl->qf_nonevalid = FALSE;
1840 if (qfl->qf_ptr == NULL)
1841 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001843 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001844 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001845 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001847 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001849 if (!adding)
1850 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001851 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001852 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001853 qi->qf_listcount--;
1854 if (qi->qf_curlist > 0)
1855 --qi->qf_curlist;
1856 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001857qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001858 if (qf_idx == qi->qf_curlist)
1859 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001860 qf_cleanup_state(&state);
1861 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863 return retval;
1864}
1865
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001866/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001867 * Read the errorfile "efile" into memory, line by line, building the error
1868 * list. Set the error list's title to qf_title.
1869 * Return -1 for error, number of errors for success.
1870 */
1871 int
1872qf_init(win_T *wp,
1873 char_u *efile,
1874 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001875 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001876 char_u *qf_title,
1877 char_u *enc)
1878{
1879 qf_info_T *qi = &ql_info;
1880
1881 if (wp != NULL)
1882 {
1883 qi = ll_get_or_alloc_list(wp);
1884 if (qi == NULL)
1885 return FAIL;
1886 }
1887
1888 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1889 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1890}
1891
1892/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001893 * Set the title of the specified quickfix list. Frees the previous title.
1894 * Prepends ':' to the title.
1895 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001896 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001897qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001898{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001899 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001900
Bram Moolenaarfb604092014-07-23 15:55:00 +02001901 if (title != NULL)
1902 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001903 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001904
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001905 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001906 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001907 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001908 }
1909}
1910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001912 * The title of a quickfix/location list is set, by default, to the command
1913 * that created the quickfix list with the ":" prefix.
1914 * Create a quickfix list title string by prepending ":" to a user command.
1915 * Returns a pointer to a static buffer with the title.
1916 */
1917 static char_u *
1918qf_cmdtitle(char_u *cmd)
1919{
1920 static char_u qftitle_str[IOSIZE];
1921
1922 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1923 return qftitle_str;
1924}
1925
1926/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001927 * Return a pointer to the current list in the specified quickfix stack
1928 */
1929 static qf_list_T *
1930qf_get_curlist(qf_info_T *qi)
1931{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001932 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001933}
1934
1935/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001936 * Prepare for adding a new quickfix list. If the current list is in the
1937 * middle of the stack, then all the following lists are freed and then
1938 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
1940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001941qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942{
1943 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001944 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001946 // If the current entry is not the last entry, delete entries beyond
1947 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001948 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001949 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001950 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001952 // When the stack is full, remove to oldest entry
1953 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001954 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001956 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001958 qi->qf_lists[i - 1] = qi->qf_lists[i];
1959 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001962 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001963 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001964 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001965 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001966 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001967 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968}
1969
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001970/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001971 * Queue location list stack delete request.
1972 */
1973 static void
1974locstack_queue_delreq(qf_info_T *qi)
1975{
1976 qf_delq_T *q;
1977
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001978 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001979 if (q != NULL)
1980 {
1981 q->qi = qi;
1982 q->next = qf_delq_head;
1983 qf_delq_head = q;
1984 }
1985}
1986
1987/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001988 * Return the global quickfix stack window buffer number.
1989 */
1990 int
1991qf_stack_get_bufnr(void)
1992{
1993 return ql_info.qf_bufnr;
1994}
1995
1996/*
1997 * Wipe the quickfix window buffer (if present) for the specified
1998 * quickfix/location list.
1999 */
2000 static void
2001wipe_qf_buffer(qf_info_T *qi)
2002{
2003 buf_T *qfbuf;
2004
2005 if (qi->qf_bufnr != INVALID_QFBUFNR)
2006 {
2007 qfbuf = buflist_findnr(qi->qf_bufnr);
2008 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
2009 {
2010 // If the quickfix buffer is not loaded in any window, then
2011 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01002012 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002013 qi->qf_bufnr = INVALID_QFBUFNR;
2014 }
2015 }
2016}
2017
2018/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002019 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002020 */
2021 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002022ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002023{
2024 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002026
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002027 qi = *pqi;
2028 if (qi == NULL)
2029 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002030 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002031
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002032 // If the location list is still in use, then queue the delete request
2033 // to be processed later.
2034 if (quickfix_busy > 0)
2035 {
2036 locstack_queue_delreq(qi);
2037 return;
2038 }
2039
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002040 qi->qf_refcount--;
2041 if (qi->qf_refcount < 1)
2042 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002043 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002044 // If the quickfix window buffer is loaded, then wipe it
2045 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002046
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002047 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002048 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002049 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002050 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002051}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002053/*
2054 * Free all the quickfix/location lists in the stack.
2055 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002056 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002057qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058{
2059 int i;
2060 qf_info_T *qi = &ql_info;
2061
2062 if (wp != NULL)
2063 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002064 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002065 ll_free_all(&wp->w_llist);
2066 ll_free_all(&wp->w_llist_ref);
2067 }
2068 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002069 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002070 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002071 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002072}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002075 * Delay freeing of location list stacks when the quickfix code is running.
2076 * Used to avoid problems with autocmds freeing location list stacks when the
2077 * quickfix code is still referencing the stack.
2078 * Must always call decr_quickfix_busy() exactly once after this.
2079 */
2080 static void
2081incr_quickfix_busy(void)
2082{
2083 quickfix_busy++;
2084}
2085
2086/*
2087 * Safe to free location list stacks. Process any delayed delete requests.
2088 */
2089 static void
2090decr_quickfix_busy(void)
2091{
2092 if (--quickfix_busy == 0)
2093 {
2094 // No longer referencing the location lists. Process all the pending
2095 // delete requests.
2096 while (qf_delq_head != NULL)
2097 {
2098 qf_delq_T *q = qf_delq_head;
2099
2100 qf_delq_head = q->next;
2101 ll_free_all(&q->qi);
2102 vim_free(q);
2103 }
2104 }
2105#ifdef ABORT_ON_INTERNAL_ERROR
2106 if (quickfix_busy < 0)
2107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002108 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002109 abort();
2110 }
2111#endif
2112}
2113
2114#if defined(EXITFREE) || defined(PROTO)
2115 void
2116check_quickfix_busy(void)
2117{
2118 if (quickfix_busy != 0)
2119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002120 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002121# ifdef ABORT_ON_INTERNAL_ERROR
2122 abort();
2123# endif
2124 }
2125}
2126#endif
2127
2128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002130 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 */
2132 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002133qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002134 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002135 char_u *dir, // optional directory name
2136 char_u *fname, // file name or NULL
2137 char_u *module, // module name or NULL
2138 int bufnum, // buffer number or zero
2139 char_u *mesg, // message
2140 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002141 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002142 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002143 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002144 int vis_col, // using visual column
2145 char_u *pattern, // search pattern
2146 int nr, // error number
2147 int type, // type character
2148 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002150 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002151 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002153 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002154 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002155 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002156 {
2157 buf_T *buf = buflist_findnr(bufnum);
2158
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002159 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002160 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002161 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002162 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002163 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002164 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002165 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2167 {
2168 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002169 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002172 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002174 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002175 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002176 if (pattern == NULL || *pattern == NUL)
2177 qfp->qf_pattern = NULL;
2178 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2179 {
2180 vim_free(qfp->qf_text);
2181 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002182 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002183 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002184 if (module == NULL || *module == NUL)
2185 qfp->qf_module = NULL;
2186 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2187 {
2188 vim_free(qfp->qf_text);
2189 vim_free(qfp->qf_pattern);
2190 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002191 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002194 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 type = 0;
2196 qfp->qf_type = type;
2197 qfp->qf_valid = valid;
2198
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002199 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002200 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002202 qfl->qf_start = qfp;
2203 qfl->qf_ptr = qfp;
2204 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002205 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 else
2208 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002209 qfp->qf_prev = *lastp;
2210 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002212 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002214 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002215 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002216 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002218 qfl->qf_index = qfl->qf_count;
2219 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 }
2221
Bram Moolenaar95946f12019-03-31 15:31:59 +02002222 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223}
2224
2225/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002226 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002228 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002229qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002231 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002232
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002233 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002234 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002235 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002236 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002237 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002238 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002239 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002240 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002241}
2242
2243/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002244 * Return the location list stack for window 'wp'.
2245 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002246 */
2247 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002248ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002249{
2250 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002251 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002252 return wp->w_llist_ref;
2253
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002254 // For a non-location list window, w_llist_ref should not point to a
2255 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002256 ll_free_all(&wp->w_llist_ref);
2257
2258 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002259 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002260 return wp->w_llist;
2261}
2262
2263/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002264 * Get the quickfix/location list stack to use for the specified Ex command.
2265 * For a location list command, returns the stack for the current window. If
2266 * the location list is not found, then returns NULL and prints an error
2267 * message if 'print_emsg' is TRUE.
2268 */
2269 static qf_info_T *
2270qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2271{
2272 qf_info_T *qi = &ql_info;
2273
2274 if (is_loclist_cmd(eap->cmdidx))
2275 {
2276 qi = GET_LOC_LIST(curwin);
2277 if (qi == NULL)
2278 {
2279 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002280 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002281 return NULL;
2282 }
2283 }
2284
2285 return qi;
2286}
2287
2288/*
2289 * Get the quickfix/location list stack to use for the specified Ex command.
2290 * For a location list command, returns the stack for the current window.
2291 * If the location list is not present, then allocates a new one.
2292 * Returns NULL if the allocation fails. For a location list command, sets
2293 * 'pwinp' to curwin.
2294 */
2295 static qf_info_T *
2296qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2297{
2298 qf_info_T *qi = &ql_info;
2299
2300 if (is_loclist_cmd(eap->cmdidx))
2301 {
2302 qi = ll_get_or_alloc_list(curwin);
2303 if (qi == NULL)
2304 return NULL;
2305 *pwinp = curwin;
2306 }
2307
2308 return qi;
2309}
2310
2311/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002312 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2313 */
2314 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002315copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002316{
2317 int i;
2318 qfline_T *from_qfp;
2319 qfline_T *prevp;
2320
2321 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002322 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002323 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002324 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002325 NULL,
2326 NULL,
2327 from_qfp->qf_module,
2328 0,
2329 from_qfp->qf_text,
2330 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002331 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002332 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002333 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002334 from_qfp->qf_viscol,
2335 from_qfp->qf_pattern,
2336 from_qfp->qf_nr,
2337 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002338 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002339 return FAIL;
2340
2341 // qf_add_entry() will not set the qf_num field, as the
2342 // directory and file names are not supplied. So the qf_fnum
2343 // field is copied here.
2344 prevp = to_qfl->qf_last;
2345 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2346 prevp->qf_type = from_qfp->qf_type; // error type
2347 if (from_qfl->qf_ptr == from_qfp)
2348 to_qfl->qf_ptr = prevp; // current location
2349 }
2350
2351 return OK;
2352}
2353
2354/*
2355 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2356 */
2357 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002358copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002359{
2360 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002361 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002362 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2363 to_qfl->qf_count = 0;
2364 to_qfl->qf_index = 0;
2365 to_qfl->qf_start = NULL;
2366 to_qfl->qf_last = NULL;
2367 to_qfl->qf_ptr = NULL;
2368 if (from_qfl->qf_title != NULL)
2369 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2370 else
2371 to_qfl->qf_title = NULL;
2372 if (from_qfl->qf_ctx != NULL)
2373 {
2374 to_qfl->qf_ctx = alloc_tv();
2375 if (to_qfl->qf_ctx != NULL)
2376 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2377 }
2378 else
2379 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002380 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2381 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002382 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002383 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002384
2385 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002386 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002387 return FAIL;
2388
2389 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2390
2391 // Assign a new ID for the location list
2392 to_qfl->qf_id = ++last_qf_id;
2393 to_qfl->qf_changedtick = 0L;
2394
2395 // When no valid entries are present in the list, qf_ptr points to
2396 // the first item in the list
2397 if (to_qfl->qf_nonevalid)
2398 {
2399 to_qfl->qf_ptr = to_qfl->qf_start;
2400 to_qfl->qf_index = 1;
2401 }
2402
2403 return OK;
2404}
2405
2406/*
2407 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002408 */
2409 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002410copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002411{
2412 qf_info_T *qi;
2413 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002414
Bram Moolenaar09037502018-09-25 22:08:14 +02002415 // When copying from a location list window, copy the referenced
2416 // location list. For other windows, copy the location list for
2417 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002418 if (IS_LL_WINDOW(from))
2419 qi = from->w_llist_ref;
2420 else
2421 qi = from->w_llist;
2422
Bram Moolenaar09037502018-09-25 22:08:14 +02002423 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002424 return;
2425
Bram Moolenaar09037502018-09-25 22:08:14 +02002426 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002427 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002428 return;
2429
2430 to->w_llist->qf_listcount = qi->qf_listcount;
2431
Bram Moolenaar09037502018-09-25 22:08:14 +02002432 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002433 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002434 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002435 to->w_llist->qf_curlist = idx;
2436
Bram Moolenaar0398e002019-03-21 21:12:49 +01002437 if (copy_loclist(qf_get_list(qi, idx),
2438 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002439 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002440 qf_free_all(to);
2441 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002442 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002443 }
2444
Bram Moolenaar09037502018-09-25 22:08:14 +02002445 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002446}
2447
2448/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002449 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002450 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
2452 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002453qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
Bram Moolenaar82404332016-07-10 17:00:38 +02002455 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002456 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002457 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002458
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002459 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
Bram Moolenaare60acc12011-05-10 16:41:25 +02002462#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002463 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002464#endif
2465#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002466 if (directory != NULL)
2467 slash_adjust(directory);
2468 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002469#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002470 if (directory != NULL && !vim_isAbsName(fname)
2471 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2472 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002473 // Here we check if the file really exists.
2474 // This should normally be true, but if make works without
2475 // "leaving directory"-messages we might have missed a
2476 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002477 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002480 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002481 if (directory)
2482 ptr = concat_fnames(directory, fname, TRUE);
2483 else
2484 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002486 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002487 bufname = ptr;
2488 }
2489 else
2490 bufname = fname;
2491
2492 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002493 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002494 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002495 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002496 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002498 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002499 {
2500 vim_free(qf_last_bufname);
2501 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2502 if (bufname == ptr)
2503 qf_last_bufname = bufname;
2504 else
2505 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002506 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002507 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002508 if (buf == NULL)
2509 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002510
Bram Moolenaarc1542742016-07-20 21:44:37 +02002511 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002512 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002513 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514}
2515
2516/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002517 * Push dirbuf onto the directory stack and return pointer to actual dir or
2518 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 */
2520 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002521qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523 struct dir_stack_T *ds_new;
2524 struct dir_stack_T *ds_ptr;
2525
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002526 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002527 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (ds_new == NULL)
2529 return NULL;
2530
2531 ds_new->next = *stackptr;
2532 *stackptr = ds_new;
2533
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002534 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (vim_isAbsName(dirbuf)
2536 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002537 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 (*stackptr)->dirname = vim_strsave(dirbuf);
2539 else
2540 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002541 // Okay we don't have an absolute path.
2542 // dirbuf must be a subdir of one of the directories on the stack.
2543 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 ds_new = (*stackptr)->next;
2545 (*stackptr)->dirname = NULL;
2546 while (ds_new)
2547 {
2548 vim_free((*stackptr)->dirname);
2549 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2550 TRUE);
2551 if (mch_isdir((*stackptr)->dirname) == TRUE)
2552 break;
2553
2554 ds_new = ds_new->next;
2555 }
2556
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002557 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 while ((*stackptr)->next != ds_new)
2559 {
2560 ds_ptr = (*stackptr)->next;
2561 (*stackptr)->next = (*stackptr)->next->next;
2562 vim_free(ds_ptr->dirname);
2563 vim_free(ds_ptr);
2564 }
2565
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002566 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (ds_new == NULL)
2568 {
2569 vim_free((*stackptr)->dirname);
2570 (*stackptr)->dirname = vim_strsave(dirbuf);
2571 }
2572 }
2573
2574 if ((*stackptr)->dirname != NULL)
2575 return (*stackptr)->dirname;
2576 else
2577 {
2578 ds_ptr = *stackptr;
2579 *stackptr = (*stackptr)->next;
2580 vim_free(ds_ptr);
2581 return NULL;
2582 }
2583}
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585/*
2586 * pop dirbuf from the directory stack and return previous directory or NULL if
2587 * stack is empty
2588 */
2589 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002590qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 struct dir_stack_T *ds_ptr;
2593
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002594 // TODO: Should we check if dirbuf is the directory on top of the stack?
2595 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002597 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (*stackptr != NULL)
2599 {
2600 ds_ptr = *stackptr;
2601 *stackptr = (*stackptr)->next;
2602 vim_free(ds_ptr->dirname);
2603 vim_free(ds_ptr);
2604 }
2605
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002606 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 return *stackptr ? (*stackptr)->dirname : NULL;
2608}
2609
2610/*
2611 * clean up directory stack
2612 */
2613 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002614qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 struct dir_stack_T *ds_ptr;
2617
2618 while ((ds_ptr = *stackptr) != NULL)
2619 {
2620 *stackptr = (*stackptr)->next;
2621 vim_free(ds_ptr->dirname);
2622 vim_free(ds_ptr);
2623 }
2624}
2625
2626/*
2627 * Check in which directory of the directory stack the given file can be
2628 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002629 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 * Cleans up intermediate directory entries.
2631 *
2632 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002633 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 * ./
2635 * ./aa
2636 * ./aa/bb
2637 * ./bb
2638 * ./bb/x.c
2639 * and make says:
2640 * making all in aa
2641 * making all in bb
2642 * x.c:9: Error
2643 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2644 * qf_guess_filepath will return NULL.
2645 */
2646 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002647qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 struct dir_stack_T *ds_ptr;
2650 struct dir_stack_T *ds_tmp;
2651 char_u *fullname;
2652
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002653 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002654 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 return NULL;
2656
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002657 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 fullname = NULL;
2659 while (ds_ptr)
2660 {
2661 vim_free(fullname);
2662 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2663
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002664 // If concat_fnames failed, just go on. The worst thing that can happen
2665 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2667 break;
2668
2669 ds_ptr = ds_ptr->next;
2670 }
2671
2672 vim_free(fullname);
2673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002674 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002675 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002677 ds_tmp = qfl->qf_dir_stack->next;
2678 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 vim_free(ds_tmp->dirname);
2680 vim_free(ds_tmp);
2681 }
2682
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002683 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684}
2685
2686/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002687 * Returns TRUE if a quickfix/location list with the given identifier exists.
2688 */
2689 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002690qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002691{
2692 qf_info_T *qi = &ql_info;
2693 int i;
2694
2695 if (wp != NULL)
2696 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002697 if (!win_valid(wp))
2698 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002699 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002700 if (qi == NULL)
2701 return FALSE;
2702 }
2703
2704 for (i = 0; i < qi->qf_listcount; ++i)
2705 if (qi->qf_lists[i].qf_id == qf_id)
2706 return TRUE;
2707
2708 return FALSE;
2709}
2710
2711/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002712 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002713 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002714 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002715 * Similar to location list.
2716 */
2717 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002718is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002719{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002720 qfline_T *qfp;
2721 int i;
2722
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002723 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002724 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2725 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002726 break;
2727
Bram Moolenaar95946f12019-03-31 15:31:59 +02002728 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002729 return FALSE;
2730
2731 return TRUE;
2732}
2733
2734/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002735 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002736 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002737 */
2738 static qfline_T *
2739get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002740 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002741 qfline_T *qf_ptr,
2742 int *qf_index,
2743 int dir)
2744{
2745 int idx;
2746 int old_qf_fnum;
2747
2748 idx = *qf_index;
2749 old_qf_fnum = qf_ptr->qf_fnum;
2750
2751 do
2752 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002753 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002754 return NULL;
2755 ++idx;
2756 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002757 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002758 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2759
2760 *qf_index = idx;
2761 return qf_ptr;
2762}
2763
2764/*
2765 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002766 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002767 */
2768 static qfline_T *
2769get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002770 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002771 qfline_T *qf_ptr,
2772 int *qf_index,
2773 int dir)
2774{
2775 int idx;
2776 int old_qf_fnum;
2777
2778 idx = *qf_index;
2779 old_qf_fnum = qf_ptr->qf_fnum;
2780
2781 do
2782 {
2783 if (idx == 1 || qf_ptr->qf_prev == NULL)
2784 return NULL;
2785 --idx;
2786 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002787 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002788 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2789
2790 *qf_index = idx;
2791 return qf_ptr;
2792}
2793
2794/*
2795 * Get the n'th (errornr) previous/next valid entry from the current entry in
2796 * the quickfix list.
2797 * dir == FORWARD or FORWARD_FILE: next valid entry
2798 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2799 */
2800 static qfline_T *
2801get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002802 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002803 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002804 int dir,
2805 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002806{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002807 qfline_T *qf_ptr = qfl->qf_ptr;
2808 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002809 qfline_T *prev_qf_ptr;
2810 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002811 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002812
2813 while (errornr--)
2814 {
2815 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002816 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002817
2818 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002819 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002820 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002821 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002822 if (qf_ptr == NULL)
2823 {
2824 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002825 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002826 if (err != NULL)
2827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002828 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002829 return NULL;
2830 }
2831 break;
2832 }
2833
2834 err = NULL;
2835 }
2836
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002837 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002838 return qf_ptr;
2839}
2840
2841/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002842 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2843 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002844 */
2845 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002846get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002847{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002848 qfline_T *qf_ptr = qfl->qf_ptr;
2849 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002850
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002851 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002852 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2853 {
2854 --qf_idx;
2855 qf_ptr = qf_ptr->qf_prev;
2856 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002857 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002858 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2859 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002860 {
2861 ++qf_idx;
2862 qf_ptr = qf_ptr->qf_next;
2863 }
2864
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002865 *new_qfidx = qf_idx;
2866 return qf_ptr;
2867}
2868
2869/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002870 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002871 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2872 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2873 * Returns a pointer to the entry and the index of the new entry is stored in
2874 * 'new_qfidx'.
2875 */
2876 static qfline_T *
2877qf_get_entry(
2878 qf_list_T *qfl,
2879 int errornr,
2880 int dir,
2881 int *new_qfidx)
2882{
2883 qfline_T *qf_ptr = qfl->qf_ptr;
2884 int qfidx = qfl->qf_index;
2885
2886 if (dir != 0) // next/prev valid entry
2887 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2888 else if (errornr != 0) // go to specified number
2889 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2890
2891 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002892 return qf_ptr;
2893}
2894
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002895/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002896 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002897 */
2898 static win_T *
2899qf_find_help_win(void)
2900{
2901 win_T *wp;
2902
2903 FOR_ALL_WINDOWS(wp)
2904 if (bt_help(wp->w_buffer))
2905 return wp;
2906
2907 return NULL;
2908}
2909
2910/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002911 * Set the location list for the specified window to 'qi'.
2912 */
2913 static void
2914win_set_loclist(win_T *wp, qf_info_T *qi)
2915{
2916 wp->w_llist = qi;
2917 qi->qf_refcount++;
2918}
2919
2920/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002921 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2922 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002923 */
2924 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002925jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002926{
2927 win_T *wp;
2928 int flags;
2929
Bram Moolenaare1004402020-10-24 20:49:43 +02002930 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002931 wp = NULL;
2932 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002933 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002934 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2935 win_enter(wp, TRUE);
2936 else
2937 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002938 // Split off help window; put it at far top if no position
2939 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002940 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002941 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002942 && curwin->w_width < 80)
2943 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002944 // If the user asks to open a new window, then copy the location list.
2945 // Otherwise, don't copy the location list.
2946 if (IS_LL_STACK(qi) && !newwin)
2947 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002948
2949 if (win_split(0, flags) == FAIL)
2950 return FAIL;
2951
2952 *opened_window = TRUE;
2953
2954 if (curwin->w_height < p_hh)
2955 win_setheight((int)p_hh);
2956
Bram Moolenaarb2443732018-11-11 22:50:27 +01002957 // When using location list, the new window should use the supplied
2958 // location list. If the user asks to open a new window, then the new
2959 // window will get a copy of the location list.
2960 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002961 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002962 }
2963
2964 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002966
2967 return OK;
2968}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002969
2970/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002971 * Find a non-quickfix window using the given location list stack in the
2972 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 * Returns NULL if a matching window is not found.
2974 */
2975 static win_T *
2976qf_find_win_with_loclist(qf_info_T *ll)
2977{
2978 win_T *wp;
2979
2980 FOR_ALL_WINDOWS(wp)
2981 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2982 return wp;
2983
2984 return NULL;
2985}
2986
2987/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002988 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002989 */
2990 static win_T *
2991qf_find_win_with_normal_buf(void)
2992{
2993 win_T *wp;
2994
2995 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002996 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002997 return wp;
2998
2999 return NULL;
3000}
3001
3002/*
3003 * Go to a window in any tabpage containing the specified file. Returns TRUE
3004 * if successfully jumped to the window. Otherwise returns FALSE.
3005 */
3006 static int
3007qf_goto_tabwin_with_file(int fnum)
3008{
3009 tabpage_T *tp;
3010 win_T *wp;
3011
3012 FOR_ALL_TAB_WINDOWS(tp, wp)
3013 if (wp->w_buffer->b_fnum == fnum)
3014 {
3015 goto_tabpage_win(tp, wp);
3016 return TRUE;
3017 }
3018
3019 return FALSE;
3020}
3021
3022/*
3023 * Create a new window to show a file above the quickfix window. Called when
3024 * only the quickfix window is present.
3025 */
3026 static int
3027qf_open_new_file_win(qf_info_T *ll_ref)
3028{
3029 int flags;
3030
3031 flags = WSP_ABOVE;
3032 if (ll_ref != NULL)
3033 flags |= WSP_NEWLOC;
3034 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003035 return FAIL; // not enough room for window
3036 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003037 swb_flags = 0;
3038 RESET_BINDING(curwin);
3039 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003040 // The new window should use the location list from the
3041 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003042 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003043 return OK;
3044}
3045
3046/*
3047 * Go to a window that shows the right buffer. If the window is not found, go
3048 * to the window just above the location list window. This is used for opening
3049 * a file from a location window and not from a quickfix window. If some usable
3050 * window is previously found, then it is supplied in 'use_win'.
3051 */
3052 static void
3053qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3054{
3055 win_T *win = use_win;
3056
3057 if (win == NULL)
3058 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003059 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003060 FOR_ALL_WINDOWS(win)
3061 if (win->w_buffer->b_fnum == qf_fnum)
3062 break;
3063 if (win == NULL)
3064 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003065 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003066 win = curwin;
3067 do
3068 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003069 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003070 break;
3071 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003072 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003073 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003074 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003075 } while (win != curwin);
3076 }
3077 }
3078 win_goto(win);
3079
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003080 // If the location list for the window is not set, then set it
3081 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003082 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003083 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003084}
3085
3086/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003087 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3088 * not found, then go to the window just above the quickfix window. This is
3089 * used for opening a file from a quickfix window and not from a location
3090 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003091 */
3092 static void
3093qf_goto_win_with_qfl_file(int qf_fnum)
3094{
3095 win_T *win;
3096 win_T *altwin;
3097
3098 win = curwin;
3099 altwin = NULL;
3100 for (;;)
3101 {
3102 if (win->w_buffer->b_fnum == qf_fnum)
3103 break;
3104 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003105 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003106 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003107 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003108
3109 if (IS_QF_WINDOW(win))
3110 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003111 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003112 // window, unless 'switchbuf' contains 'uselast': in this case we
3113 // try to jump to the previously used window first.
3114 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3115 win = prevwin;
3116 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003117 win = altwin;
3118 else if (curwin->w_prev != NULL)
3119 win = curwin->w_prev;
3120 else
3121 win = curwin->w_next;
3122 break;
3123 }
3124
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003125 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003126 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003127 altwin = win;
3128 }
3129
3130 win_goto(win);
3131}
3132
3133/*
3134 * Find a suitable window for opening a file (qf_fnum) from the
3135 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003136 * window, jump to it. Otherwise open a new window to display the file. If
3137 * 'newwin' is TRUE, then always open a new window. This is called from either
3138 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003139 */
3140 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003141qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003142{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003143 win_T *usable_wp = NULL;
3144 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003145 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003146
Bram Moolenaarb2443732018-11-11 22:50:27 +01003147 // If opening a new window, then don't use the location list referred by
3148 // the current window. Otherwise two windows will refer to the same
3149 // location list.
3150 if (!newwin)
3151 ll_ref = curwin->w_llist_ref;
3152
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003153 if (ll_ref != NULL)
3154 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003155 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003156 usable_wp = qf_find_win_with_loclist(ll_ref);
3157 if (usable_wp != NULL)
3158 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003159 }
3160
3161 if (!usable_win)
3162 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003163 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003164 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003165 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003166 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003167 }
3168
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003169 // If no usable window is found and 'switchbuf' contains "usetab"
3170 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003171 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003172 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003173
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003174 // If there is only one window and it is the quickfix window, create a
3175 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003176 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003177 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003178 if (qf_open_new_file_win(ll_ref) != OK)
3179 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003180 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003181 }
3182 else
3183 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003184 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003185 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003186 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003187 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003188 }
3189
3190 return OK;
3191}
3192
3193/*
3194 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003195 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003196 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003197 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003198 */
3199 static int
3200qf_jump_edit_buffer(
3201 qf_info_T *qi,
3202 qfline_T *qf_ptr,
3203 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003204 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003205 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003206{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003207 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003208 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003209 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003210 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003211 int old_qf_curlist = qi->qf_curlist;
3212 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003213
3214 if (qf_ptr->qf_type == 1)
3215 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003216 // Open help file (do_ecmd() will set b_help flag, readfile() will
3217 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003218 if (!can_abandon(curbuf, forceit))
3219 {
3220 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003221 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003222 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003223
3224 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3225 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003226 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003227 }
3228 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003229 retval = buflist_getfile(qf_ptr->qf_fnum,
3230 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003231
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003232 // If a location list, check whether the associated window is still
3233 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003234 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003235 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003236 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003237
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003238 if (wp == NULL && curwin->w_llist != qi)
3239 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003240 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003241 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003242 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003243 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003244 }
3245
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003246 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003247 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003248 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003249 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003250 }
3251
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003252 // Check if the list was changed. The pointers may happen to be identical,
3253 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003254 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003255 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003256 || !is_qf_entry_present(qfl, qf_ptr))
3257 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003258 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003259 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003260 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003261 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003262 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003263 }
3264
3265 return retval;
3266}
3267
3268/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003269 * Go to the error line in the current file using either line/column number or
3270 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003271 */
3272 static void
3273qf_jump_goto_line(
3274 linenr_T qf_lnum,
3275 int qf_col,
3276 char_u qf_viscol,
3277 char_u *qf_pattern)
3278{
3279 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003280
3281 if (qf_pattern == NULL)
3282 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003283 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003284 i = qf_lnum;
3285 if (i > 0)
3286 {
3287 if (i > curbuf->b_ml.ml_line_count)
3288 i = curbuf->b_ml.ml_line_count;
3289 curwin->w_cursor.lnum = i;
3290 }
3291 if (qf_col > 0)
3292 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003293 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003294 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003295 coladvance(qf_col - 1);
3296 else
3297 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003298 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003299 check_cursor();
3300 }
3301 else
3302 beginline(BL_WHITE | BL_FIX);
3303 }
3304 else
3305 {
3306 pos_T save_cursor;
3307
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003308 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003309 save_cursor = curwin->w_cursor;
3310 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003311 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003312 curwin->w_cursor = save_cursor;
3313 }
3314}
3315
3316/*
3317 * Display quickfix list index and size message
3318 */
3319 static void
3320qf_jump_print_msg(
3321 qf_info_T *qi,
3322 int qf_index,
3323 qfline_T *qf_ptr,
3324 buf_T *old_curbuf,
3325 linenr_T old_lnum)
3326{
3327 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003328 garray_T *gap;
3329
3330 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003331
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003332 // Update the screen before showing the message, unless the screen
3333 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003334 if (!msg_scrolled)
3335 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003336 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003337 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003338 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3339 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003340 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003341 ga_concat(gap, IObuff);
3342 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003343
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003344 // Output the message. Overwrite to avoid scrolling when the 'O'
3345 // flag is present in 'shortmess'; But when not jumping, print the
3346 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003347 i = msg_scroll;
3348 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3349 msg_scroll = TRUE;
3350 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3351 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003352 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003353 msg_scroll = i;
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003354
3355 qfga_clear();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003356}
3357
3358/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003359 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003360 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3361 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003362 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3363 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003364 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3365 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003366 */
3367 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003368qf_jump_open_window(
3369 qf_info_T *qi,
3370 qfline_T *qf_ptr,
3371 int newwin,
3372 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003373{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003374 qf_list_T *qfl = qf_get_curlist(qi);
3375 int old_changedtick = qfl->qf_changedtick;
3376 int old_qf_curlist = qi->qf_curlist;
3377 qfltype_T qfl_type = qfl->qfl_type;
3378
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003379 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003380 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3381 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003382 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003383 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003384 if (old_qf_curlist != qi->qf_curlist
3385 || old_changedtick != qfl->qf_changedtick
3386 || !is_qf_entry_present(qfl, qf_ptr))
3387 {
3388 if (qfl_type == QFLT_QUICKFIX)
3389 emsg(_(e_current_quickfix_list_was_changed));
3390 else
3391 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003392 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003393 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003394
3395 // If currently in the quickfix window, find another window to show the
3396 // file in.
3397 if (bt_quickfix(curbuf) && !*opened_window)
3398 {
3399 // If there is no file specified, we don't know where to go.
3400 // But do advance, otherwise ":cn" gets stuck.
3401 if (qf_ptr->qf_fnum == 0)
3402 return NOTDONE;
3403
Bram Moolenaarb2443732018-11-11 22:50:27 +01003404 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3405 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003406 return FAIL;
3407 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003408 if (old_qf_curlist != qi->qf_curlist
3409 || old_changedtick != qfl->qf_changedtick
3410 || !is_qf_entry_present(qfl, qf_ptr))
3411 {
3412 if (qfl_type == QFLT_QUICKFIX)
3413 emsg(_(e_current_quickfix_list_was_changed));
3414 else
3415 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003416 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003417 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003418
3419 return OK;
3420}
3421
3422/*
3423 * Edit a selected file from the quickfix/location list and jump to a
3424 * particular line/column, adjust the folds and display a message about the
3425 * jump.
3426 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003427 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003428 * the file.
3429 */
3430 static int
3431qf_jump_to_buffer(
3432 qf_info_T *qi,
3433 int qf_index,
3434 qfline_T *qf_ptr,
3435 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003436 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003437 int *opened_window,
3438 int openfold,
3439 int print_message)
3440{
3441 buf_T *old_curbuf;
3442 linenr_T old_lnum;
3443 int retval = OK;
3444
3445 // If there is a file name, read the wanted file if needed, and check
3446 // autowrite etc.
3447 old_curbuf = curbuf;
3448 old_lnum = curwin->w_cursor.lnum;
3449
3450 if (qf_ptr->qf_fnum != 0)
3451 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003452 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003453 opened_window);
3454 if (retval != OK)
3455 return retval;
3456 }
3457
3458 // When not switched to another buffer, still need to set pc mark
3459 if (curbuf == old_curbuf)
3460 setpcmark();
3461
3462 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3463 qf_ptr->qf_pattern);
3464
3465#ifdef FEAT_FOLDING
3466 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3467 foldOpenCursor();
3468#endif
3469 if (print_message)
3470 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3471
3472 return retval;
3473}
3474
3475/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003476 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 */
3478 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003479qf_jump(qf_info_T *qi,
3480 int dir,
3481 int errornr,
3482 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003484 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3485}
3486
3487/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003488 * Jump to a quickfix line.
3489 * If dir == 0 go to entry "errornr".
3490 * If dir == FORWARD go "errornr" valid entries forward.
3491 * If dir == BACKWARD go "errornr" valid entries backward.
3492 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3493 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3494 * else if "errornr" is zero, redisplay the same line
3495 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003496 * If 'newwin' is TRUE, then open the file in a new window.
3497 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003498 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003499qf_jump_newwin(qf_info_T *qi,
3500 int dir,
3501 int errornr,
3502 int forceit,
3503 int newwin)
3504{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003505 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003506 qfline_T *qf_ptr;
3507 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003510 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003511 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003512 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003515 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003516 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003518 if (qi == NULL)
3519 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003520
Bram Moolenaar0398e002019-03-21 21:12:49 +01003521 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003523 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 return;
3525 }
3526
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003527 incr_quickfix_busy();
3528
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003529 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003530
3531 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003533 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003535
3536 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3537 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003539 qf_ptr = old_qf_ptr;
3540 qf_index = old_qf_index;
3541 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003544 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003545 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003546 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003547 // No need to print the error message if it's visible in the error
3548 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 print_message = FALSE;
3550
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003551 prev_winid = curwin->w_id;
3552
Bram Moolenaarb2443732018-11-11 22:50:27 +01003553 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003554 if (retval == FAIL)
3555 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003556 if (retval == QF_ABORT)
3557 {
3558 qi = NULL;
3559 qf_ptr = NULL;
3560 goto theend;
3561 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003562 if (retval == NOTDONE)
3563 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003564
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003565 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003566 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003567 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003569 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003570 qi = NULL;
3571 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003574 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003577 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003578 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003580 // Couldn't open file, so put index back where it was. This could
3581 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 qf_ptr = old_qf_ptr;
3584 qf_index = old_qf_index;
3585 }
3586 }
3587theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003588 if (qi != NULL)
3589 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003590 qfl->qf_ptr = qf_ptr;
3591 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003592 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003593 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003595 // Restore old 'switchbuf' value, but not when an autocommand or
3596 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003597 p_swb = old_swb;
3598 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003600 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601}
3602
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003603// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003604static int qfFileAttr;
3605static int qfSepAttr;
3606static int qfLineAttr;
3607
3608/*
3609 * Display information about a single entry from the quickfix/location list.
3610 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003611 * 'cursel' will be set to TRUE for the currently selected entry in the
3612 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003613 */
3614 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003615qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003616{
3617 char_u *fname;
3618 buf_T *buf;
3619 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003620 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003621
3622 fname = NULL;
3623 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3624 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3625 (char *)qfp->qf_module);
3626 else {
3627 if (qfp->qf_fnum != 0
3628 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3629 {
3630 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003631 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003632 fname = gettail(fname);
3633 }
3634 if (fname == NULL)
3635 sprintf((char *)IObuff, "%2d", qf_idx);
3636 else
3637 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3638 qf_idx, (char *)fname);
3639 }
3640
3641 // Support for filtering entries using :filter /pat/ clist
3642 // Match against the module name, file name, search pattern and
3643 // text of the entry.
3644 filter_entry = TRUE;
3645 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3646 filter_entry &= message_filtered(qfp->qf_module);
3647 if (filter_entry && fname != NULL)
3648 filter_entry &= message_filtered(fname);
3649 if (filter_entry && qfp->qf_pattern != NULL)
3650 filter_entry &= message_filtered(qfp->qf_pattern);
3651 if (filter_entry)
3652 filter_entry &= message_filtered(qfp->qf_text);
3653 if (filter_entry)
3654 return;
3655
3656 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003657 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003658
3659 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003660 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003661 gap = qfga_get();
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003662 if (qfp->qf_lnum == 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003663 ga_append(gap, NUL);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003664 else
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003665 qf_range_text(gap, qfp);
3666 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
3667 ga_append(gap, NUL);
3668 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003669 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003670 if (qfp->qf_pattern != NULL)
3671 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003672 gap = qfga_get();
3673 qf_fmt_text(gap, qfp->qf_pattern);
3674 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003675 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003676 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003677 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003678
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003679 // Remove newlines and leading whitespace from the text. For an
3680 // unrecognized line keep the indent, the compiler may mark a word
3681 // with ^^^^.
3682 gap = qfga_get();
3683 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
3684 ? skipwhite(qfp->qf_text) : qfp->qf_text);
3685 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003686 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003687}
3688
3689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003691 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 */
3693 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003694qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003696 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003697 qfline_T *qfp;
3698 int i;
3699 int idx1 = 1;
3700 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003701 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003702 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003703 int all = eap->forceit; // if not :cl!, only show
3704 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003705 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003707 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3708 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003709
Bram Moolenaar0398e002019-03-21 21:12:49 +01003710 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003712 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return;
3714 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003715 if (*arg == '+')
3716 {
3717 ++arg;
3718 plus = TRUE;
3719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3721 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003722 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 return;
3724 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003725 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003726 if (plus)
3727 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003728 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003729 idx2 = i + idx1;
3730 idx1 = i;
3731 }
3732 else
3733 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003734 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003735 if (idx1 < 0)
3736 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3737 if (idx2 < 0)
3738 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003741 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003742 shorten_fnames(FALSE);
3743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003744 // Get the attributes for the different quickfix highlight items. Note
3745 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003746 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3747 if (qfFileAttr == 0)
3748 qfFileAttr = HL_ATTR(HLF_D);
3749 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3750 if (qfSepAttr == 0)
3751 qfSepAttr = HL_ATTR(HLF_D);
3752 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3753 if (qfLineAttr == 0)
3754 qfLineAttr = HL_ATTR(HLF_N);
3755
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003756 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003758 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
3760 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003761 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 ui_breakcheck();
3764 }
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003765 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766}
3767
3768/*
3769 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003770 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 */
3772 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003773qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 char_u *p = text;
3776
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003777 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
3779 if (*p == '\n')
3780 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003781 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003783 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 break;
3785 }
3786 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003787 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003789
3790 ga_append(gap, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791}
3792
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003793/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003794 * Add the range information from the lnum, col, end_lnum, and end_col values
3795 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02003796 */
3797 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003798qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02003799{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003800 char_u *buf = IObuff;
3801 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02003802 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003803
thinca6864efa2021-06-19 20:45:20 +02003804 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3805 len = (int)STRLEN(buf);
3806
3807 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3808 {
3809 vim_snprintf((char *)buf + len, bufsize - len,
3810 "-%ld", qfp->qf_end_lnum);
3811 len += (int)STRLEN(buf + len);
3812 }
3813 if (qfp->qf_col > 0)
3814 {
3815 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3816 len += (int)STRLEN(buf + len);
3817 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3818 {
3819 vim_snprintf((char *)buf + len, bufsize - len,
3820 "-%d", qfp->qf_end_col);
3821 len += (int)STRLEN(buf + len);
3822 }
3823 }
3824 buf[len] = NUL;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003825
3826 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02003827}
3828
3829/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003830 * Display information (list number, list size and the title) about a
3831 * quickfix/location list.
3832 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003833 static void
3834qf_msg(qf_info_T *qi, int which, char *lead)
3835{
3836 char *title = (char *)qi->qf_lists[which].qf_title;
3837 int count = qi->qf_lists[which].qf_count;
3838 char_u buf[IOSIZE];
3839
3840 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3841 lead,
3842 which + 1,
3843 qi->qf_listcount,
3844 count);
3845
3846 if (title != NULL)
3847 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003848 size_t len = STRLEN(buf);
3849
3850 if (len < 34)
3851 {
3852 vim_memset(buf + len, ' ', 34 - len);
3853 buf[34] = NUL;
3854 }
3855 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003856 }
3857 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003858 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003859}
3860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861/*
3862 * ":colder [count]": Up in the quickfix stack.
3863 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003864 * ":lolder [count]": Up in the location list stack.
3865 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 */
3867 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003868qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003870 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 int count;
3872
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003873 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3874 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (eap->addr_count != 0)
3877 count = eap->line2;
3878 else
3879 count = 1;
3880 while (count--)
3881 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003882 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003884 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003886 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003887 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003889 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891 else
3892 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003893 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003895 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003896 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003898 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003901 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003902 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903}
3904
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003905/*
3906 * Display the information about all the quickfix/location lists in the stack
3907 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003908 void
3909qf_history(exarg_T *eap)
3910{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003911 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003912 int i;
3913
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003914 if (eap->addr_count > 0)
3915 {
3916 if (qi == NULL)
3917 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003918 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003919 return;
3920 }
3921
3922 // Jump to the specified quickfix list
3923 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3924 {
3925 qi->qf_curlist = eap->line2 - 1;
3926 qf_msg(qi, qi->qf_curlist, "");
3927 qf_update_buffer(qi, NULL);
3928 }
3929 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003930 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003931
3932 return;
3933 }
3934
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003935 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003936 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003937 else
3938 for (i = 0; i < qi->qf_listcount; ++i)
3939 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3940}
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003943 * Free all the entries in the error list "idx". Note that other information
3944 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 */
3946 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003947qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003949 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003950 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003951 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003953 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003955 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003956 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003957 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003958 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003959 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003960 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003961 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003962 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003963 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003964 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003965 // Somehow qf_count may have an incorrect value, set it to 1
3966 // to avoid crashing when it's wrong.
3967 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003968 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003969 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003970 qfl->qf_start = qfpnext;
3971 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003973
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003974 qfl->qf_index = 0;
3975 qfl->qf_start = NULL;
3976 qfl->qf_last = NULL;
3977 qfl->qf_ptr = NULL;
3978 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003979
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003980 qf_clean_dir_stack(&qfl->qf_dir_stack);
3981 qfl->qf_directory = NULL;
3982 qf_clean_dir_stack(&qfl->qf_file_stack);
3983 qfl->qf_currfile = NULL;
3984 qfl->qf_multiline = FALSE;
3985 qfl->qf_multiignore = FALSE;
3986 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987}
3988
3989/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003990 * Free error list "idx". Frees all the entries in the quickfix list,
3991 * associated context information and the title.
3992 */
3993 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003994qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003995{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003996 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003997
Bram Moolenaard23a8232018-02-10 18:45:26 +01003998 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003999 free_tv(qfl->qf_ctx);
4000 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004001 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004002 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004003 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004004}
4005
4006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 * qf_mark_adjust: adjust marks
4008 */
4009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004010qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02004011 win_T *wp,
4012 linenr_T line1,
4013 linenr_T line2,
4014 long amount,
4015 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004017 int i;
4018 qfline_T *qfp;
4019 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004020 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004021 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004022 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
Bram Moolenaarc1542742016-07-20 21:44:37 +02004024 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004025 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004026 if (wp != NULL)
4027 {
4028 if (wp->w_llist == NULL)
4029 return;
4030 qi = wp->w_llist;
4031 }
4032
4033 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004034 {
4035 qf_list_T *qfl = qf_get_list(qi, idx);
4036
4037 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004038 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (qfp->qf_fnum == curbuf->b_fnum)
4040 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004041 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4043 {
4044 if (amount == MAXLNUM)
4045 qfp->qf_cleared = TRUE;
4046 else
4047 qfp->qf_lnum += amount;
4048 }
4049 else if (amount_after && qfp->qf_lnum > line2)
4050 qfp->qf_lnum += amount_after;
4051 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004052 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004053
4054 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004055 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056}
4057
4058/*
4059 * Make a nice message out of the error character and the error number:
4060 * char number message
4061 * e or E 0 " error"
4062 * w or W 0 " warning"
4063 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004064 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 * 0 0 ""
4066 * other 0 " c"
4067 * e or E n " error n"
4068 * w or W n " warning n"
4069 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004070 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 * 0 n " error n"
4072 * other n " c n"
4073 * 1 x "" :helpgrep
4074 */
4075 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004076qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077{
4078 static char_u buf[20];
4079 static char_u cc[3];
4080 char_u *p;
4081
4082 if (c == 'W' || c == 'w')
4083 p = (char_u *)" warning";
4084 else if (c == 'I' || c == 'i')
4085 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004086 else if (c == 'N' || c == 'n')
4087 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4089 p = (char_u *)" error";
4090 else if (c == 0 || c == 1)
4091 p = (char_u *)"";
4092 else
4093 {
4094 cc[0] = ' ';
4095 cc[1] = c;
4096 cc[2] = NUL;
4097 p = cc;
4098 }
4099
4100 if (nr <= 0)
4101 return p;
4102
4103 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4104 return buf;
4105}
4106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004108 * When "split" is FALSE: Open the entry/result under the cursor.
4109 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4110 */
4111 void
4112qf_view_result(int split)
4113{
4114 qf_info_T *qi = &ql_info;
4115
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004116 if (IS_LL_WINDOW(curwin))
4117 qi = GET_LOC_LIST(curwin);
4118
Bram Moolenaar0398e002019-03-21 21:12:49 +01004119 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004120 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004121 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004122 return;
4123 }
4124
4125 if (split)
4126 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004127 // Open the selected entry in a new window
4128 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4129 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004130 return;
4131 }
4132
4133 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4134}
4135
4136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 * ":cwindow": open the quickfix window if we have errors to display,
4138 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004139 * ":lwindow": open the location list window if we have locations to display,
4140 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 */
4142 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004143ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004145 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004146 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 win_T *win;
4148
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004149 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4150 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004151
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004152 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004153
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004154 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004155 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004157 // If a quickfix window is open but we have no errors to display,
4158 // close the window. If a quickfix window is not open, then open
4159 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004160 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004161 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004162 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
4164 if (win != NULL)
4165 ex_cclose(eap);
4166 }
4167 else if (win == NULL)
4168 ex_copen(eap);
4169}
4170
4171/*
4172 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004173 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004176ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004178 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004179 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004181 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4182 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004184 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004185 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 if (win != NULL)
4187 win_close(win, FALSE);
4188}
4189
4190/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004191 * Set "w:quickfix_title" if "qi" has a title.
4192 */
4193 static void
4194qf_set_title_var(qf_list_T *qfl)
4195{
4196 if (qfl->qf_title != NULL)
4197 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4198}
4199
4200/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004201 * Goto a quickfix or location list window (if present).
4202 * Returns OK if the window is found, FAIL otherwise.
4203 */
4204 static int
4205qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4206{
4207 win_T *win;
4208
4209 win = qf_find_win(qi);
4210 if (win == NULL)
4211 return FAIL;
4212
4213 win_goto(win);
4214 if (resize)
4215 {
4216 if (vertsplit)
4217 {
4218 if (sz != win->w_width)
4219 win_setwidth(sz);
4220 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004221 else if (sz != win->w_height && win->w_height
4222 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004223 win_setheight(sz);
4224 }
4225
4226 return OK;
4227}
4228
4229/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004230 * Set options for the buffer in the quickfix or location list window.
4231 */
4232 static void
4233qf_set_cwindow_options(void)
4234{
4235 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004236 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4237 set_option_value_give_err((char_u *)"bt",
4238 0L, (char_u *)"quickfix", OPT_LOCAL);
4239 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004240 RESET_BINDING(curwin);
4241#ifdef FEAT_DIFF
4242 curwin->w_p_diff = FALSE;
4243#endif
4244#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004245 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004246 OPT_LOCAL);
4247#endif
4248}
4249
4250/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004251 * Open a new quickfix or location list window, load the quickfix buffer and
4252 * set the appropriate options for the window.
4253 * Returns FAIL if the window could not be opened.
4254 */
4255 static int
4256qf_open_new_cwindow(qf_info_T *qi, int height)
4257{
4258 buf_T *qf_buf;
4259 win_T *oldwin = curwin;
4260 tabpage_T *prevtab = curtab;
4261 int flags = 0;
4262 win_T *win;
4263
4264 qf_buf = qf_find_buf(qi);
4265
4266 // The current window becomes the previous window afterwards.
4267 win = curwin;
4268
Bram Moolenaare1004402020-10-24 20:49:43 +02004269 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004270 // Create the new quickfix window at the very bottom, except when
4271 // :belowright or :aboveleft is used.
4272 win_goto(lastwin);
4273 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004274 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004275 flags = WSP_BELOW;
4276 flags |= WSP_NEWLOC;
4277 if (win_split(height, flags) == FAIL)
4278 return FAIL; // not enough room for window
4279 RESET_BINDING(curwin);
4280
4281 if (IS_LL_STACK(qi))
4282 {
4283 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004284 // location list stack from the window 'win'.
4285 curwin->w_llist_ref = qi;
4286 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004287 }
4288
4289 if (oldwin != curwin)
4290 oldwin = NULL; // don't store info when in another window
4291 if (qf_buf != NULL)
4292 {
4293 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004294 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004295 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004296 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004297 }
4298 else
4299 {
4300 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004301 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4302 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004303 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004304
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004305 // save the number of the new buffer
4306 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004307 }
4308
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004309 // Set the options for the quickfix buffer/window (if not already done)
4310 // Do this even if the quickfix buffer was already present, as an autocmd
4311 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004312 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004313 qf_set_cwindow_options();
4314
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004315 // Only set the height when still in the same tab page and there is no
4316 // window to the side.
4317 if (curtab == prevtab && curwin->w_width == Columns)
4318 win_setheight(height);
4319 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4320 if (win_valid(win))
4321 prevwin = win;
4322
4323 return OK;
4324}
4325
4326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004328 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 */
4330 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004331ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004333 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004334 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004336 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004337 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004339 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4340 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004341
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004342 incr_quickfix_busy();
4343
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 if (eap->addr_count != 0)
4345 height = eap->line2;
4346 else
4347 height = QF_WINHEIGHT;
4348
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004349 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350#ifdef FEAT_GUI
4351 need_mouse_correct = TRUE;
4352#endif
4353
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004354 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004355 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004356 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004357 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004358 if (status == FAIL)
4359 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004360 {
4361 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004362 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004365 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004366 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004367 // Save the current index here, as updating the quickfix buffer may free
4368 // the quickfix list
4369 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004370
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004371 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004372 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004374 decr_quickfix_busy();
4375
4376 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 curwin->w_cursor.col = 0;
4378 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004379 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380}
4381
4382/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004383 * Move the cursor in the quickfix window to "lnum".
4384 */
4385 static void
4386qf_win_goto(win_T *win, linenr_T lnum)
4387{
4388 win_T *old_curwin = curwin;
4389
4390 curwin = win;
4391 curbuf = win->w_buffer;
4392 curwin->w_cursor.lnum = lnum;
4393 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004394 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004395 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004396 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004397 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004398 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004399 curwin = old_curwin;
4400 curbuf = curwin->w_buffer;
4401}
4402
4403/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004404 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004405 */
4406 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004407ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004408{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004409 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004410 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004411
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004412 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4413 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004414
4415 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004416 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4417 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4418}
4419
4420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 * Return the number of the current entry (line number in the quickfix
4422 * window).
4423 */
4424 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004425qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004427 qf_info_T *qi = &ql_info;
4428
4429 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004430 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004431 qi = wp->w_llist_ref;
4432
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004433 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434}
4435
4436/*
4437 * Update the cursor position in the quickfix window to the current error.
4438 * Return TRUE if there is a quickfix window.
4439 */
4440 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004441qf_win_pos_update(
4442 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004443 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444{
4445 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004446 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004448 // Put the cursor on the current error in the quickfix window, so that
4449 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004450 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if (win != NULL
4452 && qf_index <= win->w_buffer->b_ml.ml_line_count
4453 && old_qf_index != qf_index)
4454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (qf_index > old_qf_index)
4456 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004457 win->w_redraw_top = old_qf_index;
4458 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 else
4461 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004462 win->w_redraw_top = qf_index;
4463 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004465 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467 return win != NULL;
4468}
4469
4470/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004471 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004472 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004473 */
4474 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004475is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004476{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004477 // A window displaying the quickfix buffer will have the w_llist_ref field
4478 // set to NULL.
4479 // A window displaying a location list buffer will have the w_llist_ref
4480 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004481 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004482 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4483 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004484 return TRUE;
4485
4486 return FALSE;
4487}
4488
4489/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004490 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4491 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004492 */
4493 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004494qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004495{
4496 win_T *win;
4497
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004498 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004499 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004500 return win;
4501 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004502}
4503
4504/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004505 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004506 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 */
4508 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004509qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004511 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004512 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004514 if (qi->qf_bufnr != INVALID_QFBUFNR)
4515 {
4516 buf_T *qfbuf;
4517 qfbuf = buflist_findnr(qi->qf_bufnr);
4518 if (qfbuf != NULL)
4519 return qfbuf;
4520 // buffer is no longer present
4521 qi->qf_bufnr = INVALID_QFBUFNR;
4522 }
4523
Bram Moolenaar9c102382006-05-03 21:26:49 +00004524 FOR_ALL_TAB_WINDOWS(tp, win)
4525 if (is_qf_win(win, qi))
4526 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004527
Bram Moolenaar9c102382006-05-03 21:26:49 +00004528 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529}
4530
4531/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004532 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004533 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004534 */
4535 int
4536qf_process_qftf_option(void)
4537{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004538 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004539}
4540
4541/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004542 * Update the w:quickfix_title variable in the quickfix/location list window in
4543 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004544 */
4545 static void
4546qf_update_win_titlevar(qf_info_T *qi)
4547{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004548 qf_list_T *qfl = qf_get_curlist(qi);
4549 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004550 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004551 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004552
Bram Moolenaar530bed92020-12-16 21:02:56 +01004553 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004554 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004555 if (is_qf_win(win, qi))
4556 {
4557 curwin = win;
4558 qf_set_title_var(qfl);
4559 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004560 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004561 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004562}
4563
4564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 * Find the quickfix buffer. If it exists, update the contents.
4566 */
4567 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004568qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569{
4570 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004571 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004574 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004575 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (buf != NULL)
4577 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004578 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004579 int qf_winid = 0;
4580
4581 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004582 {
4583 if (curwin->w_llist == qi)
4584 win = curwin;
4585 else
4586 {
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004587 // Find the file window (non-quickfix) with this location list
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004588 win = qf_find_win_with_loclist(qi);
4589 if (win == NULL)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004590 // File window is not found. Find the location list window.
4591 win = qf_find_win(qi);
4592 if (win == NULL)
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004593 return;
4594 }
4595 qf_winid = win->w_id;
4596 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004597
Bram Moolenaard0fab102022-10-20 16:03:33 +01004598 // autocommands may cause trouble
4599 incr_quickfix_busy();
4600
Bram Moolenaar864293a2016-06-02 13:40:04 +02004601 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004602 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004603 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
Bram Moolenaard823fa92016-08-12 16:29:27 +02004605 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004606
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004607 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004608 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004609
Bram Moolenaar864293a2016-06-02 13:40:04 +02004610 if (old_last == NULL)
4611 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004612 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004613
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004614 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004615 aucmd_restbuf(&aco);
4616 }
4617
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004618 // Only redraw when added lines are visible. This avoids flickering
4619 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004620 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004621 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaard0fab102022-10-20 16:03:33 +01004622
4623 // always called after incr_quickfix_busy()
4624 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626}
4627
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004628/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004629 * Add an error line to the quickfix buffer.
4630 */
4631 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004632qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004633 buf_T *buf, // quickfix window buffer
4634 linenr_T lnum,
4635 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004636 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004637 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004638 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004639{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004640 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004641 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004642
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004643 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004644
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004645 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004646 // for this entry, then use it.
4647 if (qftf_str != NULL && *qftf_str != NUL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004648 ga_concat(gap, qftf_str);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004649 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004650 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004651 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004652 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004653 else if (qfp->qf_fnum != 0
4654 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4655 && errbuf->b_fname != NULL)
4656 {
4657 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004658 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02004659 else
4660 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004661 // Shorten the file name if not done already.
4662 // For optimization, do this only for the first entry in a
4663 // buffer.
4664 if (first_bufline && (errbuf->b_sfname == NULL
4665 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004666 {
4667 if (*dirname == NUL)
4668 mch_dirname(dirname, MAXPATHL);
4669 shorten_buf_fname(errbuf, dirname, FALSE);
4670 }
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004671 ga_concat(gap, errbuf->b_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004672 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004673 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004674
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004675 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02004676
4677 if (qfp->qf_lnum > 0)
4678 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004679 qf_range_text(gap, qfp);
4680 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004681 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004682 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004683 qf_fmt_text(gap, qfp->qf_pattern);
4684 ga_append(gap, '|');
4685 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004686
Bram Moolenaar858ba062020-05-31 23:11:59 +02004687 // Remove newlines and leading whitespace from the text.
4688 // For an unrecognized line keep the indent, the compiler may
4689 // mark a word with ^^^^.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004690 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004691 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004692
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004693 ga_append(gap, NUL);
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004694
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004695 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len + 1, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004696 return FAIL;
4697
4698 return OK;
4699}
4700
Bram Moolenaard43906d2020-07-20 21:31:32 +02004701/*
4702 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4703 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4704 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004705 static list_T *
4706call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4707{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004708 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004709 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004710 static int recursive = FALSE;
4711
4712 if (recursive)
4713 return NULL; // this doesn't work properly recursively
4714 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004715
4716 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4717 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4718 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004719 if (qfl->qf_qftf_cb.cb_name != NULL)
4720 cb = &qfl->qf_qftf_cb;
4721 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004722 {
4723 typval_T args[1];
4724 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004725 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004726
4727 // create the dict argument
4728 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01004729 {
4730 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004731 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004732 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004733 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4734 dict_add_number(d, "winid", (long)qf_winid);
4735 dict_add_number(d, "id", (long)qfl->qf_id);
4736 dict_add_number(d, "start_idx", start_idx);
4737 dict_add_number(d, "end_idx", end_idx);
4738 ++d->dv_refcount;
4739 args[0].v_type = VAR_DICT;
4740 args[0].vval.v_dict = d;
4741
Bram Moolenaard43906d2020-07-20 21:31:32 +02004742 qftf_list = NULL;
4743 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4744 {
4745 if (rettv.v_type == VAR_LIST)
4746 {
4747 qftf_list = rettv.vval.v_list;
4748 qftf_list->lv_refcount++;
4749 }
4750 clear_tv(&rettv);
4751 }
4752 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004753 }
4754
Bram Moolenaard6c67622022-08-24 20:07:22 +01004755 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004756 return qftf_list;
4757}
4758
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 * Fill current buffer with quickfix errors, replacing any previous contents.
4761 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004762 * If "old_last" is not NULL append the items after this one.
4763 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4764 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 */
4766 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004767qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004769 linenr_T lnum;
4770 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004771 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004772 list_T *qftf_list = NULL;
4773 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
Bram Moolenaar864293a2016-06-02 13:40:04 +02004775 if (old_last == NULL)
4776 {
4777 if (buf != curbuf)
4778 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004779 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004780 return;
4781 }
4782
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004783 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004784 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004785 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004788 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004789 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004791 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004792 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004793 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004794
4795 *dirname = NUL;
4796
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004797 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004798 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004799 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004800 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004801 lnum = 0;
4802 }
4803 else
4804 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004805 if (old_last->qf_next != NULL)
4806 qfp = old_last->qf_next;
4807 else
4808 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004809 lnum = buf->b_ml.ml_line_count;
4810 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004811
4812 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4813 (long)qfl->qf_count);
4814 if (qftf_list != NULL)
4815 qftf_li = qftf_list->lv_first;
4816
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004817 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004819 char_u *qftf_str = NULL;
4820
Bram Moolenaard43906d2020-07-20 21:31:32 +02004821 // Use the text supplied by the user defined function (if any).
4822 // If the returned value is not string, then ignore the rest
4823 // of the returned values and use the default.
4824 if (qftf_li != NULL && !invalid_val)
4825 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004826 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004827 if (qftf_str == NULL)
4828 invalid_val = TRUE;
4829 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004830
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004831 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4832 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004834
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004835 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004836 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004838 if (qfp == NULL)
4839 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004840
4841 if (qftf_li != NULL)
4842 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004844
4845 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004846 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004847 (void)ml_delete(lnum + 1);
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01004848
4849 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004852 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 check_lnums(TRUE);
4854
Bram Moolenaar864293a2016-06-02 13:40:04 +02004855 if (old_last == NULL)
4856 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004857 // Set the 'filetype' to "qf" each time after filling the buffer.
4858 // This resembles reading a file into a buffer, it's more logical when
4859 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004860 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004861 set_option_value_give_err((char_u *)"ft",
4862 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004863 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004865 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004866 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004868 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004870 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004871 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004872
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004873 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004874 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004877 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 KeyTyped = old_KeyTyped;
4879}
4880
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004881/*
4882 * For every change made to the quickfix list, update the changed tick.
4883 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004884 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004885qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004886{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004887 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004888}
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004891 * Return the quickfix/location list number with the given identifier.
4892 * Returns -1 if list is not found.
4893 */
4894 static int
4895qf_id2nr(qf_info_T *qi, int_u qfid)
4896{
4897 int qf_idx;
4898
4899 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4900 if (qi->qf_lists[qf_idx].qf_id == qfid)
4901 return qf_idx;
4902 return INVALID_QFIDX;
4903}
4904
4905/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004906 * If the current list is not "save_qfid" and we can find the list with that ID
4907 * then make it the current list.
4908 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004909 * Returns OK if successfully restored the list. Returns FAIL if the list with
4910 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004911 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004912 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004913qf_restore_list(qf_info_T *qi, int_u save_qfid)
4914{
4915 int curlist;
4916
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004917 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004918 {
4919 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004920 if (curlist < 0)
4921 // list is not present
4922 return FAIL;
4923 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004924 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004925 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004926}
4927
4928/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004929 * Jump to the first entry if there is one.
4930 */
4931 static void
4932qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4933{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004934 if (qf_restore_list(qi, save_qfid) == FAIL)
4935 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004936
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004937 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004938 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004939 qf_jump(qi, 0, 0, forceit);
4940}
4941
4942/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004943 * Return TRUE when using ":vimgrep" for ":grep".
4944 */
4945 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004946grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004947{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004948 return ((cmdidx == CMD_grep
4949 || cmdidx == CMD_lgrep
4950 || cmdidx == CMD_grepadd
4951 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004952 && STRCMP("internal",
4953 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4954}
4955
4956/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004957 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004959 static char_u *
4960make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004962 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004963 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004964 case CMD_make: return (char_u *)"make";
4965 case CMD_lmake: return (char_u *)"lmake";
4966 case CMD_grep: return (char_u *)"grep";
4967 case CMD_lgrep: return (char_u *)"lgrep";
4968 case CMD_grepadd: return (char_u *)"grepadd";
4969 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4970 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972}
4973
4974/*
4975 * Return the name for the errorfile, in allocated memory.
4976 * Find a new unique name when 'makeef' contains "##".
4977 * Returns NULL for error.
4978 */
4979 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004980get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
4982 char_u *p;
4983 char_u *name;
4984 static int start = -1;
4985 static int off = 0;
4986#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004987 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988#endif
4989
4990 if (*p_mef == NUL)
4991 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004992 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004994 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 return name;
4996 }
4997
4998 for (p = p_mef; *p; ++p)
4999 if (p[0] == '#' && p[1] == '#')
5000 break;
5001
5002 if (*p == NUL)
5003 return vim_strsave(p_mef);
5004
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005005 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 for (;;)
5007 {
5008 if (start == -1)
5009 start = mch_get_pid();
5010 else
5011 off += 19;
5012
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005013 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (name == NULL)
5015 break;
5016 STRCPY(name, p_mef);
5017 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
5018 STRCAT(name, p + 2);
5019 if (mch_getperm(name) < 0
5020#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005021 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 && mch_lstat((char *)name, &sb) < 0
5023#endif
5024 )
5025 break;
5026 vim_free(name);
5027 }
5028 return name;
5029}
5030
5031/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005032 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5033 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5034 */
5035 static char_u *
5036make_get_fullcmd(char_u *makecmd, char_u *fname)
5037{
5038 char_u *cmd;
5039 unsigned len;
5040
5041 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5042 if (*p_sp != NUL)
5043 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005044 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005045 if (cmd == NULL)
5046 return NULL;
5047 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5048 (char *)p_shq);
5049
5050 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5051 if (*p_sp != NUL)
5052 append_redir(cmd, len, p_sp, fname);
5053
5054 // Display the fully formed command. Output a newline if there's something
5055 // else than the :make command that was typed (in which case the cursor is
5056 // in column 0).
5057 if (msg_col == 0)
5058 msg_didout = FALSE;
5059 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005060 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005061 msg_outtrans(cmd); // show what we are doing
5062
5063 return cmd;
5064}
5065
5066/*
5067 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5068 */
5069 void
5070ex_make(exarg_T *eap)
5071{
5072 char_u *fname;
5073 char_u *cmd;
5074 char_u *enc = NULL;
5075 win_T *wp = NULL;
5076 qf_info_T *qi = &ql_info;
5077 int res;
5078 char_u *au_name = NULL;
5079 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005080 char_u *errorformat = p_efm;
5081 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005082
5083 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5084 if (grep_internal(eap->cmdidx))
5085 {
5086 ex_vimgrep(eap);
5087 return;
5088 }
5089
5090 au_name = make_get_auname(eap->cmdidx);
5091 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5092 curbuf->b_fname, TRUE, curbuf))
5093 {
5094#ifdef FEAT_EVAL
5095 if (aborting())
5096 return;
5097#endif
5098 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005099 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005100
5101 if (is_loclist_cmd(eap->cmdidx))
5102 wp = curwin;
5103
5104 autowrite_all();
5105 fname = get_mef_name();
5106 if (fname == NULL)
5107 return;
5108 mch_remove(fname); // in case it's not unique
5109
5110 cmd = make_get_fullcmd(eap->arg, fname);
5111 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005112 {
5113 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005114 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005115 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005116
5117 // let the shell know if we are redirecting output or not
5118 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5119
5120#ifdef AMIGA
5121 out_flush();
5122 // read window status report and redraw before message
5123 (void)char_avail();
5124#endif
5125
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005126 incr_quickfix_busy();
5127
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005128 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5129 errorformat = p_gefm;
5130 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5131 newlist = FALSE;
5132
5133 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5134 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005135 if (wp != NULL)
5136 {
5137 qi = GET_LOC_LIST(wp);
5138 if (qi == NULL)
5139 goto cleanup;
5140 }
5141 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005142 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005143
5144 // Remember the current quickfix list identifier, so that we can
5145 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005146 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005147 if (au_name != NULL)
5148 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5149 curbuf->b_fname, TRUE, curbuf);
5150 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5151 // display the first error
5152 qf_jump_first(qi, save_qfid, FALSE);
5153
5154cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005155 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005156 mch_remove(fname);
5157 vim_free(fname);
5158 vim_free(cmd);
5159}
5160
5161/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005162 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005163 */
5164 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005165qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005166{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005167 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005168
5169 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5170 return 0;
5171 return qf_get_curlist(qi)->qf_count;
5172}
5173
5174/*
5175 * Returns the number of valid entries in the current quickfix/location list.
5176 */
5177 int
5178qf_get_valid_size(exarg_T *eap)
5179{
5180 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005181 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005182 qfline_T *qfp;
5183 int i, sz = 0;
5184 int prev_fnum = 0;
5185
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005186 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5187 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005188
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005189 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005190 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005191 {
5192 if (qfp->qf_valid)
5193 {
5194 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005195 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005196 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5197 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005198 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005199 sz++;
5200 prev_fnum = qfp->qf_fnum;
5201 }
5202 }
5203 }
5204
5205 return sz;
5206}
5207
5208/*
5209 * Returns the current index of the quickfix/location list.
5210 * Returns 0 if there is an error.
5211 */
5212 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005213qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005214{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005215 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005216
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005217 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5218 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005219
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005220 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005221}
5222
5223/*
5224 * Returns the current index in the quickfix/location list (counting only valid
5225 * entries). If no valid entries are in the list, then returns 1.
5226 */
5227 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005228qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005229{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005230 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005231 qf_list_T *qfl;
5232 qfline_T *qfp;
5233 int i, eidx = 0;
5234 int prev_fnum = 0;
5235
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005236 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5237 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005238
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005239 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005240 qfp = qfl->qf_start;
5241
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005242 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005243 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005244 return 1;
5245
5246 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5247 {
5248 if (qfp->qf_valid)
5249 {
5250 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5251 {
5252 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5253 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005254 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005255 eidx++;
5256 prev_fnum = qfp->qf_fnum;
5257 }
5258 }
5259 else
5260 eidx++;
5261 }
5262 }
5263
5264 return eidx ? eidx : 1;
5265}
5266
5267/*
5268 * Get the 'n'th valid error entry in the quickfix or location list.
5269 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5270 * For :cdo and :ldo returns the 'n'th valid error entry.
5271 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5272 */
5273 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005274qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005275{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005276 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005277 int i, eidx;
5278 int prev_fnum = 0;
5279
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005280 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005281 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005282 return 1;
5283
Bram Moolenaar95946f12019-03-31 15:31:59 +02005284 eidx = 0;
5285 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005286 {
5287 if (qfp->qf_valid)
5288 {
5289 if (fdo)
5290 {
5291 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5292 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005293 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005294 eidx++;
5295 prev_fnum = qfp->qf_fnum;
5296 }
5297 }
5298 else
5299 eidx++;
5300 }
5301
5302 if (eidx == n)
5303 break;
5304 }
5305
5306 if (i <= qfl->qf_count)
5307 return i;
5308 else
5309 return 1;
5310}
5311
5312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005314 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005315 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 */
5317 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005318ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005320 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005321 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005322
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005323 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5324 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005325
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005326 if (eap->addr_count > 0)
5327 errornr = (int)eap->line2;
5328 else
5329 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005330 switch (eap->cmdidx)
5331 {
5332 case CMD_cc: case CMD_ll:
5333 errornr = 0;
5334 break;
5335 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5336 case CMD_lfirst:
5337 errornr = 1;
5338 break;
5339 default:
5340 errornr = 32767;
5341 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005342 }
5343
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005344 // For cdo and ldo commands, jump to the nth valid error.
5345 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005346 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5347 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005348 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005349 eap->addr_count > 0 ? (int)eap->line1 : 1,
5350 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5351
5352 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353}
5354
5355/*
5356 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005357 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005358 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 */
5360 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005361ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005363 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005364 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005365 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005366
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005367 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5368 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005369
Bram Moolenaar55b69262017-08-13 13:42:01 +02005370 if (eap->addr_count > 0
5371 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5372 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005373 errornr = (int)eap->line2;
5374 else
5375 errornr = 1;
5376
Bram Moolenaar39665952018-08-15 20:59:48 +02005377 // Depending on the command jump to either next or previous entry/file.
5378 switch (eap->cmdidx)
5379 {
5380 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5381 dir = FORWARD;
5382 break;
5383 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5384 case CMD_lNext:
5385 dir = BACKWARD;
5386 break;
5387 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5388 dir = FORWARD_FILE;
5389 break;
5390 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5391 dir = BACKWARD_FILE;
5392 break;
5393 default:
5394 dir = FORWARD;
5395 break;
5396 }
5397
5398 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399}
5400
5401/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005402 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5403 * The index of the entry is stored in 'errornr'.
5404 * Returns NULL if an entry is not found.
5405 */
5406 static qfline_T *
5407qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5408{
5409 qfline_T *qfp = NULL;
5410 int idx = 0;
5411
5412 // Find the first entry in this file
5413 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5414 if (qfp->qf_fnum == bnr)
5415 break;
5416
5417 *errornr = idx;
5418 return qfp;
5419}
5420
5421/*
5422 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5423 * with the error number for the first entry. Assumes the entries are sorted in
5424 * the quickfix list by line number.
5425 */
5426 static qfline_T *
5427qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5428{
5429 while (!got_int
5430 && entry->qf_prev != NULL
5431 && entry->qf_fnum == entry->qf_prev->qf_fnum
5432 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5433 {
5434 entry = entry->qf_prev;
5435 --*errornr;
5436 }
5437
5438 return entry;
5439}
5440
5441/*
5442 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5443 * with the error number for the last entry. Assumes the entries are sorted in
5444 * the quickfix list by line number.
5445 */
5446 static qfline_T *
5447qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5448{
5449 while (!got_int &&
5450 entry->qf_next != NULL
5451 && entry->qf_fnum == entry->qf_next->qf_fnum
5452 && entry->qf_lnum == entry->qf_next->qf_lnum)
5453 {
5454 entry = entry->qf_next;
5455 ++*errornr;
5456 }
5457
5458 return entry;
5459}
5460
5461/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005462 * Returns TRUE if the specified quickfix entry is
5463 * after the given line (linewise is TRUE)
5464 * or after the line and column.
5465 */
5466 static int
5467qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5468{
5469 if (linewise)
5470 return qfp->qf_lnum > pos->lnum;
5471 else
5472 return (qfp->qf_lnum > pos->lnum ||
5473 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5474}
5475
5476/*
5477 * Returns TRUE if the specified quickfix entry is
5478 * before the given line (linewise is TRUE)
5479 * or before the line and column.
5480 */
5481 static int
5482qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5483{
5484 if (linewise)
5485 return qfp->qf_lnum < pos->lnum;
5486 else
5487 return (qfp->qf_lnum < pos->lnum ||
5488 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5489}
5490
5491/*
5492 * Returns TRUE if the specified quickfix entry is
5493 * on or after the given line (linewise is TRUE)
5494 * or on or after the line and column.
5495 */
5496 static int
5497qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5498{
5499 if (linewise)
5500 return qfp->qf_lnum >= pos->lnum;
5501 else
5502 return (qfp->qf_lnum > pos->lnum ||
5503 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5504}
5505
5506/*
5507 * Returns TRUE if the specified quickfix entry is
5508 * on or before the given line (linewise is TRUE)
5509 * or on or before the line and column.
5510 */
5511 static int
5512qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5513{
5514 if (linewise)
5515 return qfp->qf_lnum <= pos->lnum;
5516 else
5517 return (qfp->qf_lnum < pos->lnum ||
5518 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5519}
5520
5521/*
5522 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5523 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5524 * multiple entries on a single line as one. Otherwise returns the entry after
5525 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005526 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5527 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005528 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005529 */
5530 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005531qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005532 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005533 pos_T *pos,
5534 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005535 qfline_T *qfp,
5536 int *errornr)
5537{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005538 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005539 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005540 return qfp;
5541
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005542 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005543 while (qfp->qf_next != NULL
5544 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005545 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005546 {
5547 qfp = qfp->qf_next;
5548 ++*errornr;
5549 }
5550
5551 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005552 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005553 return NULL;
5554
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005555 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005556 qfp = qfp->qf_next;
5557 ++*errornr;
5558
5559 return qfp;
5560}
5561
5562/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005563 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5564 * If 'linewise' is TRUE, returns the entry before the specified line and
5565 * treats multiple entries on a single line as one. Otherwise returns the entry
5566 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005567 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5568 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005569 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005570 */
5571 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005572qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005573 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005574 pos_T *pos,
5575 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005576 qfline_T *qfp,
5577 int *errornr)
5578{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005579 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005580 while (qfp->qf_next != NULL
5581 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005582 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005583 {
5584 qfp = qfp->qf_next;
5585 ++*errornr;
5586 }
5587
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005588 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005589 return NULL;
5590
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005591 if (linewise)
5592 // If multiple entries are on the same line, then use the first entry
5593 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005594
5595 return qfp;
5596}
5597
5598/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005599 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005600 * the direction 'dir'.
5601 */
5602 static qfline_T *
5603qf_find_closest_entry(
5604 qf_list_T *qfl,
5605 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005606 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005607 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005608 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005609 int *errornr)
5610{
5611 qfline_T *qfp;
5612
5613 *errornr = 0;
5614
5615 // Find the first entry in this file
5616 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5617 if (qfp == NULL)
5618 return NULL; // no entry in this file
5619
5620 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005621 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005622 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005623 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005624
5625 return qfp;
5626}
5627
5628/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005629 * Get the nth quickfix entry below the specified entry. Searches forward in
5630 * the list. If linewise is TRUE, then treat multiple entries on a single line
5631 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005632 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005633 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005634qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005635{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005636 qfline_T *entry = entry_arg;
5637
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005638 while (n-- > 0 && !got_int)
5639 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005640 int first_errornr = *errornr;
5641
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005642 if (linewise)
5643 // Treat all the entries on the same line in this file as one
5644 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005645
5646 if (entry->qf_next == NULL
5647 || entry->qf_next->qf_fnum != entry->qf_fnum)
5648 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005649 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005650 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005651 break;
5652 }
5653
5654 entry = entry->qf_next;
5655 ++*errornr;
5656 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005657}
5658
5659/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005660 * Get the nth quickfix entry above the specified entry. Searches backwards in
5661 * the list. If linewise is TRUE, then treat multiple entries on a single line
5662 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005663 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005664 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005665qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005666{
5667 while (n-- > 0 && !got_int)
5668 {
5669 if (entry->qf_prev == NULL
5670 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5671 break;
5672
5673 entry = entry->qf_prev;
5674 --*errornr;
5675
5676 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005677 if (linewise)
5678 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005679 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005680}
5681
5682/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005683 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5684 * the specified direction. Returns the error number in the quickfix list or 0
5685 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005686 */
5687 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005688qf_find_nth_adj_entry(
5689 qf_list_T *qfl,
5690 int bnr,
5691 pos_T *pos,
5692 int n,
5693 int dir,
5694 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005695{
5696 qfline_T *adj_entry;
5697 int errornr;
5698
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005699 // Find an entry closest to the specified position
5700 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005701 if (adj_entry == NULL)
5702 return 0;
5703
5704 if (--n > 0)
5705 {
5706 // Go to the n'th entry in the current buffer
5707 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005708 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005709 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005710 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005711 }
5712
5713 return errornr;
5714}
5715
5716/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005717 * Jump to a quickfix entry in the current file nearest to the current line or
5718 * current line/col.
5719 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5720 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005721 */
5722 void
5723ex_cbelow(exarg_T *eap)
5724{
5725 qf_info_T *qi;
5726 qf_list_T *qfl;
5727 int dir;
5728 int buf_has_flag;
5729 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005730 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005731
5732 if (eap->addr_count > 0 && eap->line2 <= 0)
5733 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005734 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005735 return;
5736 }
5737
5738 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005739 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5740 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005741 buf_has_flag = BUF_HAS_QF_ENTRY;
5742 else
5743 buf_has_flag = BUF_HAS_LL_ENTRY;
5744 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5745 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005746 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005747 return;
5748 }
5749
5750 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5751 return;
5752
5753 qfl = qf_get_curlist(qi);
5754 // check if the list has valid errors
5755 if (!qf_list_has_valid_entries(qfl))
5756 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005757 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005758 return;
5759 }
5760
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005761 if (eap->cmdidx == CMD_cbelow
5762 || eap->cmdidx == CMD_lbelow
5763 || eap->cmdidx == CMD_cafter
5764 || eap->cmdidx == CMD_lafter)
5765 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005766 dir = FORWARD;
5767 else
5768 dir = BACKWARD;
5769
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005770 pos = curwin->w_cursor;
5771 // A quickfix entry column number is 1 based whereas cursor column
5772 // number is 0 based. Adjust the column number.
5773 pos.col++;
5774 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5775 eap->addr_count > 0 ? eap->line2 : 0, dir,
5776 eap->cmdidx == CMD_cbelow
5777 || eap->cmdidx == CMD_lbelow
5778 || eap->cmdidx == CMD_cabove
5779 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005780
5781 if (errornr > 0)
5782 qf_jump(qi, 0, errornr, FALSE);
5783 else
5784 emsg(_(e_no_more_items));
5785}
5786
5787/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005788 * Return the autocmd name for the :cfile Ex commands
5789 */
5790 static char_u *
5791cfile_get_auname(cmdidx_T cmdidx)
5792{
5793 switch (cmdidx)
5794 {
5795 case CMD_cfile: return (char_u *)"cfile";
5796 case CMD_cgetfile: return (char_u *)"cgetfile";
5797 case CMD_caddfile: return (char_u *)"caddfile";
5798 case CMD_lfile: return (char_u *)"lfile";
5799 case CMD_lgetfile: return (char_u *)"lgetfile";
5800 case CMD_laddfile: return (char_u *)"laddfile";
5801 default: return NULL;
5802 }
5803}
5804
5805/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005806 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005807 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 */
5809 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005810ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005812 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005813 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005814 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005815 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005816 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005817 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005818
Bram Moolenaara16123a2019-03-28 20:31:07 +01005819 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005820 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5821 NULL, FALSE, curbuf))
5822 {
5823#ifdef FEAT_EVAL
5824 if (aborting())
5825 return;
5826#endif
5827 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005828
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005829 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005830#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005831 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005832 {
5833 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005834 NULL, NULL,
5835 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005836 if (browse_file == NULL)
5837 return;
5838 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5839 vim_free(browse_file);
5840 }
5841 else
5842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005844 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005845
Bram Moolenaar39665952018-08-15 20:59:48 +02005846 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005847 wp = curwin;
5848
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005849 incr_quickfix_busy();
5850
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005851 // This function is used by the :cfile, :cgetfile and :caddfile
5852 // commands.
5853 // :cfile always creates a new quickfix list and jumps to the
5854 // first error.
5855 // :cgetfile creates a new quickfix list but doesn't jump to the
5856 // first error.
5857 // :caddfile adds to an existing quickfix list. If there is no
5858 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005859 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005860 && eap->cmdidx != CMD_laddfile),
5861 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005862 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005863 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005864 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005865 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005866 {
5867 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005868 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005869 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005870 }
5871 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005872 qf_list_changed(qf_get_curlist(qi));
5873 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005874 if (au_name != NULL)
5875 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005876
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005877 // Jump to the first error for a new list and if autocmds didn't
5878 // free the list.
5879 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5880 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005881 // display the first error
5882 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005883
5884 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005885}
5886
5887/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005888 * Return the vimgrep autocmd name.
5889 */
5890 static char_u *
5891vgr_get_auname(cmdidx_T cmdidx)
5892{
5893 switch (cmdidx)
5894 {
5895 case CMD_vimgrep: return (char_u *)"vimgrep";
5896 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5897 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5898 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5899 case CMD_grep: return (char_u *)"grep";
5900 case CMD_lgrep: return (char_u *)"lgrep";
5901 case CMD_grepadd: return (char_u *)"grepadd";
5902 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5903 default: return NULL;
5904 }
5905}
5906
5907/*
5908 * Initialize the regmatch used by vimgrep for pattern "s".
5909 */
5910 static void
5911vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5912{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005913 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005914 regmatch->regprog = NULL;
5915
5916 if (s == NULL || *s == NUL)
5917 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005918 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005919 if (last_search_pat() == NULL)
5920 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005921 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005922 return;
5923 }
5924 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5925 }
5926 else
5927 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5928
5929 regmatch->rmm_ic = p_ic;
5930 regmatch->rmm_maxcol = 0;
5931}
5932
5933/*
5934 * Display a file name when vimgrep is running.
5935 */
5936 static void
5937vgr_display_fname(char_u *fname)
5938{
5939 char_u *p;
5940
5941 msg_start();
5942 p = msg_strtrunc(fname, TRUE);
5943 if (p == NULL)
5944 msg_outtrans(fname);
5945 else
5946 {
5947 msg_outtrans(p);
5948 vim_free(p);
5949 }
5950 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005951 msg_didout = FALSE; // overwrite this message
5952 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005953 msg_col = 0;
5954 out_flush();
5955}
5956
5957/*
5958 * Load a dummy buffer to search for a pattern using vimgrep.
5959 */
5960 static buf_T *
5961vgr_load_dummy_buf(
5962 char_u *fname,
5963 char_u *dirname_start,
5964 char_u *dirname_now)
5965{
5966 int save_mls;
5967#if defined(FEAT_SYN_HL)
5968 char_u *save_ei = NULL;
5969#endif
5970 buf_T *buf;
5971
5972#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005973 // Don't do Filetype autocommands to avoid loading syntax and
5974 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005975 save_ei = au_event_disable(",Filetype");
5976#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005977 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005978 save_mls = p_mls;
5979 p_mls = 0;
5980
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005981 // Load file into a buffer, so that 'fileencoding' is detected,
5982 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005983 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5984
5985 p_mls = save_mls;
5986#if defined(FEAT_SYN_HL)
5987 au_event_restore(save_ei);
5988#endif
5989
5990 return buf;
5991}
5992
5993/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005994 * Check whether a quickfix/location list is valid. Autocmds may remove or
5995 * change a quickfix list when vimgrep is running. If the list is not found,
5996 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005997 */
5998 static int
5999vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006000 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006001 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006002 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006003 char_u *title)
6004{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006005 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006006 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006007 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006008 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006009 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006010 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02006011 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006012 return FALSE;
6013 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006014 else
6015 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006016 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006017 qf_new_list(qi, title);
6018 return TRUE;
6019 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006020 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006021
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02006022 if (qf_restore_list(qi, qfid) == FAIL)
6023 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006024
6025 return TRUE;
6026}
6027
6028/*
6029 * Search for a pattern in all the lines in a buffer and add the matching lines
6030 * to a quickfix list.
6031 */
6032 static int
6033vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006034 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006035 char_u *fname,
6036 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006037 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006038 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006039 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006040 int duplicate_name,
6041 int flags)
6042{
6043 int found_match = FALSE;
6044 long lnum;
6045 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006046 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006047
Bram Moolenaar1c299432018-10-28 14:36:09 +01006048 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006049 {
6050 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006051 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006052 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006053 // Regular expression match
6054 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Paul Ollis65745772022-06-05 16:55:54 +01006055 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006056 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006057 // Pass the buffer number so that it gets used even for a
6058 // dummy buffer, unless duplicate_name is set, then the
6059 // buffer will be wiped out below.
6060 if (qf_add_entry(qfl,
6061 NULL, // dir
6062 fname,
6063 NULL,
6064 duplicate_name ? 0 : buf->b_fnum,
6065 ml_get_buf(buf,
6066 regmatch->startpos[0].lnum + lnum, FALSE),
6067 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006068 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006069 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006070 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006071 FALSE, // vis_col
6072 NULL, // search pattern
6073 0, // nr
6074 0, // type
6075 TRUE // valid
6076 ) == QF_FAIL)
6077 {
6078 got_int = TRUE;
6079 break;
6080 }
6081 found_match = TRUE;
6082 if (--*tomatch == 0)
6083 break;
6084 if ((flags & VGR_GLOBAL) == 0
6085 || regmatch->endpos[0].lnum > 0)
6086 break;
6087 col = regmatch->endpos[0].col
6088 + (col == regmatch->endpos[0].col);
6089 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6090 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006091 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006092 }
6093 else
6094 {
6095 char_u *str = ml_get_buf(buf, lnum, FALSE);
6096 int score;
6097 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006098 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006099
6100 // Fuzzy string match
6101 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6102 {
6103 // Pass the buffer number so that it gets used even for a
6104 // dummy buffer, unless duplicate_name is set, then the
6105 // buffer will be wiped out below.
6106 if (qf_add_entry(qfl,
6107 NULL, // dir
6108 fname,
6109 NULL,
6110 duplicate_name ? 0 : buf->b_fnum,
6111 str,
6112 lnum,
thinca6864efa2021-06-19 20:45:20 +02006113 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006114 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006115 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006116 FALSE, // vis_col
6117 NULL, // search pattern
6118 0, // nr
6119 0, // type
6120 TRUE // valid
6121 ) == QF_FAIL)
6122 {
6123 got_int = TRUE;
6124 break;
6125 }
6126 found_match = TRUE;
6127 if (--*tomatch == 0)
6128 break;
6129 if ((flags & VGR_GLOBAL) == 0)
6130 break;
6131 col = matches[pat_len - 1] + col + 1;
6132 if (col > (colnr_T)STRLEN(str))
6133 break;
6134 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006135 }
6136 line_breakcheck();
6137 if (got_int)
6138 break;
6139 }
6140
6141 return found_match;
6142}
6143
6144/*
6145 * Jump to the first match and update the directory.
6146 */
6147 static void
6148vgr_jump_to_match(
6149 qf_info_T *qi,
6150 int forceit,
6151 int *redraw_for_dummy,
6152 buf_T *first_match_buf,
6153 char_u *target_dir)
6154{
6155 buf_T *buf;
6156
6157 buf = curbuf;
6158 qf_jump(qi, 0, 0, forceit);
6159 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006160 // If we jumped to another buffer redrawing will already be
6161 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006162 *redraw_for_dummy = FALSE;
6163
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006164 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006165 if (curbuf == first_match_buf && target_dir != NULL)
6166 {
6167 exarg_T ea;
6168
Bram Moolenaara80faa82020-04-12 19:37:17 +02006169 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006170 ea.arg = target_dir;
6171 ea.cmdidx = CMD_lcd;
6172 ex_cd(&ea);
6173 }
6174}
6175
6176/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006177 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006178 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006179typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006180{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006181 long tomatch; // maximum number of matches to find
6182 char_u *spat; // search pattern
6183 int flags; // search modifier
6184 char_u **fnames; // list of files to search
6185 int fcount; // number of files
6186 regmmatch_T regmatch; // compiled search pattern
6187 char_u *qf_title; // quickfix list title
6188} vgr_args_T;
6189
6190/*
6191 * Process :vimgrep command arguments. The command syntax is:
6192 *
6193 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6194 */
6195 static int
6196vgr_process_args(
6197 exarg_T *eap,
6198 vgr_args_T *args)
6199{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006200 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006201
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006202 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006203
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006204 args->regmatch.regprog = NULL;
6205 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006206
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006207 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006208 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006209 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006210 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006211
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006212 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006213 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006214 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006215 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006216 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006217 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006218 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006219
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006220 vgr_init_regmatch(&args->regmatch, args->spat);
6221 if (args->regmatch.regprog == NULL)
6222 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006223
6224 p = skipwhite(p);
6225 if (*p == NUL)
6226 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006227 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006228 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006229 }
6230
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006231 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006232 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6233 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006234 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006235 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006236 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006237 }
6238
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006239 return OK;
6240}
6241
6242/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006243 * Return TRUE if "buf" had an existing swap file, the current swap file does
6244 * not end in ".swp".
6245 */
6246 static int
6247existing_swapfile(buf_T *buf)
6248{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006249 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006250 {
6251 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6252 size_t len = STRLEN(fname);
6253
6254 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6255 }
6256 return FALSE;
6257}
6258
6259/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006260 * Search for a pattern in a list of files and populate the quickfix list with
6261 * the matches.
6262 */
6263 static int
6264vgr_process_files(
6265 win_T *wp,
6266 qf_info_T *qi,
6267 vgr_args_T *cmd_args,
6268 int *redraw_for_dummy,
6269 buf_T **first_match_buf,
6270 char_u **target_dir)
6271{
6272 int status = FAIL;
6273 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6274 time_t seconds = 0;
6275 char_u *fname;
6276 int fi;
6277 buf_T *buf;
6278 int duplicate_name = FALSE;
6279 int using_dummy;
6280 char_u *dirname_start = NULL;
6281 char_u *dirname_now = NULL;
6282 int found_match;
6283 aco_save_T aco;
6284
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006285 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6286 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006287 if (dirname_start == NULL || dirname_now == NULL)
6288 goto theend;
6289
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006290 // Remember the current directory, because a BufRead autocommand that does
6291 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006292 mch_dirname(dirname_start, MAXPATHL);
6293
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006294 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006295 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6296 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006297 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006298 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006299 if (time(NULL) > seconds)
6300 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006301 // Display the file name every second or so, show the user we are
6302 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006303 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006304 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006305 }
6306
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006307 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006308 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6309 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006310 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006311 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006312 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006313 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006314
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006315 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006316 }
6317 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006318 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006319 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006320
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006321 // Check whether the quickfix list is still valid. When loading a
6322 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006323 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006324 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006325
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006326 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006327
Bram Moolenaar81695252004-12-29 20:58:21 +00006328 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006329 {
6330 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006331 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006332 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006333 else
6334 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006335 // Try for a match in all lines of the buffer.
6336 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006337 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006338 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006339 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006340
Bram Moolenaar81695252004-12-29 20:58:21 +00006341 if (using_dummy)
6342 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006343 if (found_match && *first_match_buf == NULL)
6344 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006345 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006346 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006347 // Never keep a dummy buffer if there is another buffer
6348 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006349 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006350 buf = NULL;
6351 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006352 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006353 || buf->b_p_bh[0] == 'u' // "unload"
6354 || buf->b_p_bh[0] == 'w' // "wipe"
6355 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006356 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006357 // When no match was found we don't need to remember the
6358 // buffer, wipe it out. If there was a match and it
6359 // wasn't the first one or we won't jump there: only
6360 // unload the buffer.
6361 // Ignore 'hidden' here, because it may lead to having too
6362 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006363 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006364 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006365 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006366 buf = NULL;
6367 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006368 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006369 || (cmd_args->flags & VGR_NOJUMP)
6370 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006371 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006372 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006373 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006374 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006375 buf = NULL;
6376 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006377 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006378
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006379 if (buf != NULL)
6380 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006381 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006382 buf->b_flags &= ~BF_DUMMY;
6383
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006384 // If the buffer is still loaded we need to use the
6385 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006386 if (buf == *first_match_buf
6387 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006388 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006389 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006390
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006391 // The buffer is still loaded, the Filetype autocommands
6392 // need to be done now, in that buffer. And the modelines
6393 // need to be done (again). But not the window-local
6394 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006395 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006396#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006397 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6398 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006399#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006400 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006401 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006402 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006403 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006404 }
6405 }
6406
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006407 status = OK;
6408
6409theend:
6410 vim_free(dirname_now);
6411 vim_free(dirname_start);
6412 return status;
6413}
6414
6415/*
6416 * ":vimgrep {pattern} file(s)"
6417 * ":vimgrepadd {pattern} file(s)"
6418 * ":lvimgrep {pattern} file(s)"
6419 * ":lvimgrepadd {pattern} file(s)"
6420 */
6421 void
6422ex_vimgrep(exarg_T *eap)
6423{
6424 vgr_args_T args;
6425 qf_info_T *qi;
6426 qf_list_T *qfl;
6427 int_u save_qfid;
6428 win_T *wp = NULL;
6429 int redraw_for_dummy = FALSE;
6430 buf_T *first_match_buf = NULL;
6431 char_u *target_dir = NULL;
6432 char_u *au_name = NULL;
6433 int status;
6434
6435 au_name = vgr_get_auname(eap->cmdidx);
6436 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6437 curbuf->b_fname, TRUE, curbuf))
6438 {
6439#ifdef FEAT_EVAL
6440 if (aborting())
6441 return;
6442#endif
6443 }
6444
6445 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6446 if (qi == NULL)
6447 return;
6448
6449 if (vgr_process_args(eap, &args) == FAIL)
6450 goto theend;
6451
6452 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6453 && eap->cmdidx != CMD_vimgrepadd
6454 && eap->cmdidx != CMD_lvimgrepadd)
6455 || qf_stack_empty(qi))
6456 // make place for a new list
6457 qf_new_list(qi, args.qf_title);
6458
6459 incr_quickfix_busy();
6460
6461 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6462 &first_match_buf, &target_dir);
6463 if (status != OK)
6464 {
6465 FreeWild(args.fcount, args.fnames);
6466 decr_quickfix_busy();
6467 goto theend;
6468 }
6469
6470 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006471
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006472 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006473 qfl->qf_nonevalid = FALSE;
6474 qfl->qf_ptr = qfl->qf_start;
6475 qfl->qf_index = 1;
6476 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006477
Bram Moolenaar864293a2016-06-02 13:40:04 +02006478 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006479
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006480 // Remember the current quickfix list identifier, so that we can check for
6481 // autocommands changing the current quickfix list.
6482 save_qfid = qf_get_curlist(qi)->qf_id;
6483
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006484 if (au_name != NULL)
6485 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6486 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006487 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6488 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006489 if (!qflist_valid(wp, save_qfid)
6490 || qf_restore_list(qi, save_qfid) == FAIL)
6491 {
6492 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006493 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006494 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006495
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006496 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006497 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006498 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006499 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006500 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6501 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006502 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006503 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006504 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006505
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006506 decr_quickfix_busy();
6507
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006508 // If we loaded a dummy buffer into the current window, the autocommands
6509 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006510 if (redraw_for_dummy)
6511 {
6512#ifdef FEAT_FOLDING
6513 foldUpdateAll(curwin);
6514#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006515 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006516#endif
6517 }
6518
Bram Moolenaar86b68352004-12-27 21:59:20 +00006519theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006520 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006521 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006522 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006523}
6524
6525/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006526 * Restore current working directory to "dirname_start" if they differ, taking
6527 * into account whether it is set locally or globally.
6528 */
6529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006530restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006531{
6532 char_u *dirname_now = alloc(MAXPATHL);
6533
6534 if (NULL != dirname_now)
6535 {
6536 mch_dirname(dirname_now, MAXPATHL);
6537 if (STRCMP(dirname_start, dirname_now) != 0)
6538 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006539 // If the directory has changed, change it back by building up an
6540 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006541 exarg_T ea;
6542
Bram Moolenaara80faa82020-04-12 19:37:17 +02006543 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006544 ea.arg = dirname_start;
6545 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6546 ex_cd(&ea);
6547 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006548 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006549 }
6550}
6551
6552/*
6553 * Load file "fname" into a dummy buffer and return the buffer pointer,
6554 * placing the directory resulting from the buffer load into the
6555 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6556 * prior to calling this function. Restores directory to "dirname_start" prior
6557 * to returning, if autocmds or the 'autochdir' option have changed it.
6558 *
6559 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6560 * or wipe_dummy_buffer() later!
6561 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006562 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006563 */
6564 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006565load_dummy_buffer(
6566 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006567 char_u *dirname_start, // in: old directory
6568 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006569{
6570 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006571 bufref_T newbufref;
6572 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006573 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006574 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006575 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006576
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006577 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006578 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6579 if (newbuf == NULL)
6580 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006581 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006582
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006583 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006584 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6585
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006586 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006587 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006588 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006589 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006590 ++newbuf->b_locked;
6591
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006592 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006593 aucmd_prepbuf(&aco, newbuf);
6594
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006595 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006596 (void)setfname(curbuf, fname, NULL, FALSE);
6597
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006598 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006599 check_need_swap(TRUE);
6600
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006601 // Remove the "dummy" flag, otherwise autocommands may not
6602 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006603 curbuf->b_flags &= ~BF_DUMMY;
6604
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006605 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006606 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006607 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006608 NULL, READ_NEW | READ_DUMMY);
6609 --newbuf->b_locked;
6610 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006611 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006612 && !(curbuf->b_flags & BF_NEW))
6613 {
6614 failed = FALSE;
6615 if (curbuf != newbuf)
6616 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006617 // Bloody autocommands changed the buffer! Can happen when
6618 // using netrw and editing a remote file. Use the current
6619 // buffer instead, delete the dummy one after restoring the
6620 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006621 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006622 newbuf = curbuf;
6623 }
6624 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006625
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006626 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006627 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006628 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6629 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006630
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006631 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6632 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006633 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006634 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006635
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006636 // When autocommands/'autochdir' option changed directory: go back.
6637 // Let the caller know what the resulting dir was first, in case it is
6638 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006639 mch_dirname(resulting_dir, MAXPATHL);
6640 restore_start_dir(dirname_start);
6641
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006642 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006643 return NULL;
6644 if (failed)
6645 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006646 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006647 return NULL;
6648 }
6649 return newbuf;
6650}
6651
6652/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006653 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6654 * directory to "dirname_start" prior to returning, if autocmds or the
6655 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006656 */
6657 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006658wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006659{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006660 // If any autocommand opened a window on the dummy buffer, close that
6661 // window. If we can't close them all then give up.
6662 while (buf->b_nwindows > 0)
6663 {
6664 int did_one = FALSE;
6665 win_T *wp;
6666
6667 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006668 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006669 if (wp->w_buffer == buf)
6670 {
6671 if (win_close(wp, FALSE) == OK)
6672 did_one = TRUE;
6673 break;
6674 }
6675 if (!did_one)
6676 return;
6677 }
6678
6679 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006680 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006681#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006682 cleanup_T cs;
6683
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006684 // Reset the error/interrupt/exception state here so that aborting()
6685 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6686 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006687 enter_cleanup(&cs);
6688#endif
6689
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006690 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006691
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006692#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006693 // Restore the error/interrupt/exception state if not discarded by a
6694 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006695 leave_cleanup(&cs);
6696#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006697 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006698 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006699 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006700}
6701
6702/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006703 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6704 * directory to "dirname_start" prior to returning, if autocmds or the
6705 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006706 */
6707 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006708unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006709{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006710 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006711 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006712 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006713
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006714 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006715 restore_start_dir(dirname_start);
6716 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006717}
6718
Bram Moolenaar05159a02005-02-26 23:04:13 +00006719#if defined(FEAT_EVAL) || defined(PROTO)
6720/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006721 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006722 * to 'list'. Returns OK on success.
6723 */
6724 static int
6725get_qfline_items(qfline_T *qfp, list_T *list)
6726{
6727 int bufnum;
6728 dict_T *dict;
6729 char_u buf[2];
6730
6731 // Handle entries with a non-existing buffer number.
6732 bufnum = qfp->qf_fnum;
6733 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6734 bufnum = 0;
6735
6736 if ((dict = dict_alloc()) == NULL)
6737 return FAIL;
6738 if (list_append_dict(list, dict) == FAIL)
6739 return FAIL;
6740
6741 buf[0] = qfp->qf_type;
6742 buf[1] = NUL;
6743 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006744 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6745 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6746 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6747 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6748 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6749 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006750 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6751 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6752 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6753 || dict_add_string(dict, "type", buf) == FAIL
6754 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6755 return FAIL;
6756
6757 return OK;
6758}
6759
6760/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006761 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006762 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006763 * If eidx is not 0, then return only the specified entry. Otherwise return
6764 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006765 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006766 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006767get_errorlist(
6768 qf_info_T *qi_arg,
6769 win_T *wp,
6770 int qf_idx,
6771 int eidx,
6772 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006773{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006774 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006775 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006776 qfline_T *qfp;
6777 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006778
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006779 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006780 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006781 qi = &ql_info;
6782 if (wp != NULL)
6783 {
6784 qi = GET_LOC_LIST(wp);
6785 if (qi == NULL)
6786 return FAIL;
6787 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006788 }
6789
Bram Moolenaar858ba062020-05-31 23:11:59 +02006790 if (eidx < 0)
6791 return OK;
6792
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006793 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006794 qf_idx = qi->qf_curlist;
6795
Bram Moolenaar0398e002019-03-21 21:12:49 +01006796 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006797 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006798
Bram Moolenaar0398e002019-03-21 21:12:49 +01006799 qfl = qf_get_list(qi, qf_idx);
6800 if (qf_list_empty(qfl))
6801 return FAIL;
6802
Bram Moolenaara16123a2019-03-28 20:31:07 +01006803 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006804 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006805 if (eidx > 0)
6806 {
6807 if (eidx == i)
6808 return get_qfline_items(qfp, list);
6809 }
6810 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006811 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006812 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006813
Bram Moolenaar05159a02005-02-26 23:04:13 +00006814 return OK;
6815}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006816
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006817// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006818enum {
6819 QF_GETLIST_NONE = 0x0,
6820 QF_GETLIST_TITLE = 0x1,
6821 QF_GETLIST_ITEMS = 0x2,
6822 QF_GETLIST_NR = 0x4,
6823 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006824 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006825 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006826 QF_GETLIST_IDX = 0x40,
6827 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006828 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006829 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006830 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006831 QF_GETLIST_QFTF = 0x800,
6832 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006833};
6834
6835/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006836 * Parse text from 'di' and return the quickfix list items.
6837 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006838 */
6839 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006840qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006841{
6842 int status = FAIL;
6843 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006844 char_u *errorformat = p_efm;
6845 dictitem_T *efm_di;
6846 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006847
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006848 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006849 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006850 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006851 // If errorformat is supplied then use it, otherwise use the 'efm'
6852 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006853 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6854 {
6855 if (efm_di->di_tv.v_type != VAR_STRING ||
6856 efm_di->di_tv.vval.v_string == NULL)
6857 return FAIL;
6858 errorformat = efm_di->di_tv.vval.v_string;
6859 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006860
Bram Moolenaar36538222017-09-02 19:51:44 +02006861 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006862 if (l == NULL)
6863 return FAIL;
6864
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006865 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006866 if (qi != NULL)
6867 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006868 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006869 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6870 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006871 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006872 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006873 }
6874 free(qi);
6875 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006876 dict_add_list(retdict, "items", l);
6877 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006878 }
6879
6880 return status;
6881}
6882
6883/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006884 * Return the quickfix/location list window identifier in the current tabpage.
6885 */
6886 static int
6887qf_winid(qf_info_T *qi)
6888{
6889 win_T *win;
6890
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006891 // The quickfix window can be opened even if the quickfix list is not set
6892 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006893 if (qi == NULL)
6894 return 0;
6895 win = qf_find_win(qi);
6896 if (win != NULL)
6897 return win->w_id;
6898 return 0;
6899}
6900
6901/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006902 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006903 * window. If there is no buffer associated with the list or the buffer is
6904 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006905 */
6906 static int
6907qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6908{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006909 int bufnum = 0;
6910
6911 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6912 bufnum = qi->qf_bufnr;
6913
6914 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006915}
6916
6917/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006918 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006919 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006920 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006921qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006922{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006923 int flags = QF_GETLIST_NONE;
6924
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006925 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006926 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006927 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006928 if (!loclist)
6929 // File window ID is applicable only to location list windows
6930 flags &= ~ QF_GETLIST_FILEWINID;
6931 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006932
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006933 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006934 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006935
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006936 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006937 flags |= QF_GETLIST_NR;
6938
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006939 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006940 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006941
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006942 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006943 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006944
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006945 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006946 flags |= QF_GETLIST_ID;
6947
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006948 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006949 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006950
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006951 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006952 flags |= QF_GETLIST_IDX;
6953
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006954 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006955 flags |= QF_GETLIST_SIZE;
6956
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006957 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01006958 flags |= QF_GETLIST_TICK;
6959
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006960 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006961 flags |= QF_GETLIST_FILEWINID;
6962
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006963 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006964 flags |= QF_GETLIST_QFBUFNR;
6965
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006966 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02006967 flags |= QF_GETLIST_QFTF;
6968
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006969 return flags;
6970}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006971
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006972/*
6973 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6974 * If 'nr' and 'id' are not present in 'what' then return the current
6975 * quickfix list index.
6976 * If 'nr' is zero then return the current quickfix list index.
6977 * If 'nr' is '$' then return the last quickfix list index.
6978 * If 'id' is present then return the index of the quickfix list with that id.
6979 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6980 * Return -1, if quickfix list is not present or if the stack is empty.
6981 */
6982 static int
6983qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6984{
6985 int qf_idx;
6986 dictitem_T *di;
6987
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006988 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006989 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6990 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006991 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006992 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006993 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006994 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006995 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006996 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006997 qf_idx = di->di_tv.vval.v_number - 1;
6998 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006999 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007000 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01007001 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007002 else if (di->di_tv.v_type == VAR_STRING
7003 && di->di_tv.vval.v_string != NULL
7004 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007005 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007006 qf_idx = qi->qf_listcount - 1;
7007 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007008 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007009 }
7010
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007011 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
7012 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007013 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007014 if (di->di_tv.v_type == VAR_NUMBER)
7015 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007016 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007017 if (di->di_tv.vval.v_number != 0)
7018 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
7019 }
7020 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007021 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007022 }
7023
7024 return qf_idx;
7025}
7026
7027/*
7028 * Return default values for quickfix list properties in retdict.
7029 */
7030 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007031qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007032{
7033 int status = OK;
7034
7035 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007036 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007037 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7038 {
7039 list_T *l = list_alloc();
7040 if (l != NULL)
7041 status = dict_add_list(retdict, "items", l);
7042 else
7043 status = FAIL;
7044 }
7045 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007046 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007047 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007048 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007049 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007050 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007051 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007052 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007053 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007054 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007055 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007056 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007057 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007058 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007059 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007060 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007061 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7062 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007063 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7064 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007065
7066 return status;
7067}
7068
7069/*
7070 * Return the quickfix list title as 'title' in retdict
7071 */
7072 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007073qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007074{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007075 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007076}
7077
7078/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007079 * Returns the identifier of the window used to display files from a location
7080 * list. If there is no associated window, then returns 0. Useful only when
7081 * called from a location list window.
7082 */
7083 static int
7084qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7085{
7086 int winid = 0;
7087
7088 if (wp != NULL && IS_LL_WINDOW(wp))
7089 {
7090 win_T *ll_wp = qf_find_win_with_loclist(qi);
7091 if (ll_wp != NULL)
7092 winid = ll_wp->w_id;
7093 }
7094
7095 return dict_add_number(retdict, "filewinid", winid);
7096}
7097
7098/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007099 * Return the quickfix list items/entries as 'items' in retdict.
7100 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007101 */
7102 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007103qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007104{
7105 int status = OK;
7106 list_T *l = list_alloc();
7107 if (l != NULL)
7108 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007109 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007110 dict_add_list(retdict, "items", l);
7111 }
7112 else
7113 status = FAIL;
7114
7115 return status;
7116}
7117
7118/*
7119 * Return the quickfix list context (if any) as 'context' in retdict.
7120 */
7121 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007122qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007123{
7124 int status;
7125 dictitem_T *di;
7126
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007127 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007128 {
7129 di = dictitem_alloc((char_u *)"context");
7130 if (di != NULL)
7131 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007132 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007133 status = dict_add(retdict, di);
7134 if (status == FAIL)
7135 dictitem_free(di);
7136 }
7137 else
7138 status = FAIL;
7139 }
7140 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007141 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007142
7143 return status;
7144}
7145
7146/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007147 * Return the current quickfix list index as 'idx' in retdict.
7148 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007149 */
7150 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007151qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007152{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007153 if (eidx == 0)
7154 {
7155 eidx = qfl->qf_index;
7156 if (qf_list_empty(qfl))
7157 // For empty lists, current index is set to 0
7158 eidx = 0;
7159 }
7160 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007161}
7162
7163/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007164 * Return the 'quickfixtextfunc' function of a quickfix/location list
7165 */
7166 static int
7167qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7168{
7169 int status;
7170
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007171 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007172 {
7173 typval_T tv;
7174
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007175 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007176 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7177 clear_tv(&tv);
7178 }
7179 else
7180 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7181
7182 return status;
7183}
7184
7185/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007186 * Return quickfix/location list details (title) as a
7187 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7188 * then current list is used. Otherwise the specified list is used.
7189 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007190 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007191qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7192{
7193 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007194 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007195 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007196 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007197 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007198 dictitem_T *di;
7199 int flags = QF_GETLIST_NONE;
7200
7201 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7202 return qf_get_list_from_lines(what, di, retdict);
7203
7204 if (wp != NULL)
7205 qi = GET_LOC_LIST(wp);
7206
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007207 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007208
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007209 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007210 qf_idx = qf_getprop_qfidx(qi, what);
7211
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007212 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007213 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007214 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007215
Bram Moolenaar0398e002019-03-21 21:12:49 +01007216 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007217
Bram Moolenaar858ba062020-05-31 23:11:59 +02007218 // If an entry index is specified, use that
7219 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7220 {
7221 if (di->di_tv.v_type != VAR_NUMBER)
7222 return FAIL;
7223 eidx = di->di_tv.vval.v_number;
7224 }
7225
Bram Moolenaard823fa92016-08-12 16:29:27 +02007226 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007227 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007228 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007229 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007230 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007231 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007232 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007233 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007234 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007235 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007236 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007237 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007238 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007239 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007240 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007241 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007242 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007243 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007244 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7245 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007246 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7247 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007248 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7249 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007250
Bram Moolenaard823fa92016-08-12 16:29:27 +02007251 return status;
7252}
7253
7254/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007255 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007256 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7257 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007258 */
7259 static int
7260qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007261 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007262 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007263 int first_entry,
7264 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007265{
7266 static int did_bufnr_emsg;
7267 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007268 int bufnum, valid, status, col, end_col, vcol, nr;
7269 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007270
7271 if (first_entry)
7272 did_bufnr_emsg = FALSE;
7273
Bram Moolenaard61efa52022-07-23 09:52:04 +01007274 filename = dict_get_string(d, "filename", TRUE);
7275 module = dict_get_string(d, "module", TRUE);
7276 bufnum = (int)dict_get_number(d, "bufnr");
7277 lnum = (int)dict_get_number(d, "lnum");
7278 end_lnum = (int)dict_get_number(d, "end_lnum");
7279 col = (int)dict_get_number(d, "col");
7280 end_col = (int)dict_get_number(d, "end_col");
7281 vcol = (int)dict_get_number(d, "vcol");
7282 nr = (int)dict_get_number(d, "nr");
7283 type = dict_get_string(d, "type", TRUE);
7284 pattern = dict_get_string(d, "pattern", TRUE);
7285 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007286 if (text == NULL)
7287 text = vim_strsave((char_u *)"");
7288
7289 valid = TRUE;
7290 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7291 valid = FALSE;
7292
7293 // Mark entries with non-existing buffer number as not valid. Give the
7294 // error message only once.
7295 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7296 {
7297 if (!did_bufnr_emsg)
7298 {
7299 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007300 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007301 }
7302 valid = FALSE;
7303 bufnum = 0;
7304 }
7305
7306 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007307 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007308 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007309
Bram Moolenaar0398e002019-03-21 21:12:49 +01007310 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007311 NULL, // dir
7312 filename,
7313 module,
7314 bufnum,
7315 text,
7316 lnum,
thinca6864efa2021-06-19 20:45:20 +02007317 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007318 col,
thinca6864efa2021-06-19 20:45:20 +02007319 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007320 vcol, // vis_col
7321 pattern, // search pattern
7322 nr,
7323 type == NULL ? NUL : *type,
7324 valid);
7325
7326 vim_free(filename);
7327 vim_free(module);
7328 vim_free(pattern);
7329 vim_free(text);
7330 vim_free(type);
7331
Bram Moolenaar9752c722018-12-22 16:49:34 +01007332 if (valid)
7333 *valid_entry = TRUE;
7334
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007335 return status;
7336}
7337
7338/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007339 * Add list of entries to quickfix/location list. Each list entry is
7340 * a dictionary with item information.
7341 */
7342 static int
7343qf_add_entries(
7344 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007345 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007346 list_T *list,
7347 char_u *title,
7348 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007349{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007350 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007351 listitem_T *li;
7352 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007353 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007354 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007355 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007356
Bram Moolenaara3921f42017-06-04 15:30:34 +02007357 if (action == ' ' || qf_idx == qi->qf_listcount)
7358 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007359 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007360 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007361 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007362 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007363 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007364 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007365 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007366 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007367 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007368 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007369 qf_free_items(qfl);
7370 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007371 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007372
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007373 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007374 {
7375 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007376 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007377
7378 d = li->li_tv.vval.v_dict;
7379 if (d == NULL)
7380 continue;
7381
Bram Moolenaar0398e002019-03-21 21:12:49 +01007382 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007383 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007384 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007385 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007386 }
7387
Bram Moolenaar9752c722018-12-22 16:49:34 +01007388 // Check if any valid error entries are added to the list.
7389 if (valid_entry)
7390 qfl->qf_nonevalid = FALSE;
7391 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007392 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007393 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007394
7395 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007396 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007397 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007398
7399 // Update the current error index if not appending to the list or if the
7400 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007401 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007402 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007404 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007405 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007406
7407 return retval;
7408}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007409
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007410/*
7411 * Get the quickfix list index from 'nr' or 'id'
7412 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007413 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007414qf_setprop_get_qfidx(
7415 qf_info_T *qi,
7416 dict_T *what,
7417 int action,
7418 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007419{
7420 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007421 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007422
Bram Moolenaard823fa92016-08-12 16:29:27 +02007423 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7424 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007425 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007426 if (di->di_tv.v_type == VAR_NUMBER)
7427 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007428 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007429 if (di->di_tv.vval.v_number != 0)
7430 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007431
Bram Moolenaar55b69262017-08-13 13:42:01 +02007432 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7433 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007434 // When creating a new list, accept qf_idx pointing to the next
7435 // non-available list and add the new list at the end of the
7436 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007437 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007438 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007439 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007440 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007441 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007442 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007443 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007444 }
7445 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007446 && di->di_tv.vval.v_string != NULL
7447 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007448 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007449 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007450 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007451 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007452 qf_idx = 0;
7453 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007454 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007455 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007456 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007457 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007458 }
7459
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007460 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007461 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007462 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007463 if (di->di_tv.v_type != VAR_NUMBER)
7464 return INVALID_QFIDX;
7465
7466 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007467 }
7468
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007469 return qf_idx;
7470}
7471
7472/*
7473 * Set the quickfix list title.
7474 */
7475 static int
7476qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7477{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007478 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007479
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007480 if (di->di_tv.v_type != VAR_STRING)
7481 return FAIL;
7482
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007483 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007484 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007485 if (qf_idx == qi->qf_curlist)
7486 qf_update_win_titlevar(qi);
7487
7488 return OK;
7489}
7490
7491/*
7492 * Set quickfix list items/entries.
7493 */
7494 static int
7495qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7496{
7497 int retval = FAIL;
7498 char_u *title_save;
7499
7500 if (di->di_tv.v_type != VAR_LIST)
7501 return FAIL;
7502
7503 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7504 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7505 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007506 vim_free(title_save);
7507
7508 return retval;
7509}
7510
7511/*
7512 * Set quickfix list items/entries from a list of lines.
7513 */
7514 static int
7515qf_setprop_items_from_lines(
7516 qf_info_T *qi,
7517 int qf_idx,
7518 dict_T *what,
7519 dictitem_T *di,
7520 int action)
7521{
7522 char_u *errorformat = p_efm;
7523 dictitem_T *efm_di;
7524 int retval = FAIL;
7525
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007526 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007527 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7528 {
7529 if (efm_di->di_tv.v_type != VAR_STRING ||
7530 efm_di->di_tv.vval.v_string == NULL)
7531 return FAIL;
7532 errorformat = efm_di->di_tv.vval.v_string;
7533 }
7534
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007535 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007536 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7537 return FAIL;
7538
7539 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007540 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007541 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007542 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007543 retval = OK;
7544
7545 return retval;
7546}
7547
7548/*
7549 * Set quickfix list context.
7550 */
7551 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007552qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007553{
7554 typval_T *ctx;
7555
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007556 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007557 ctx = alloc_tv();
7558 if (ctx != NULL)
7559 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007560 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007561
7562 return OK;
7563}
7564
7565/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007566 * Set the current index in the specified quickfix list
7567 */
7568 static int
7569qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7570{
7571 int denote = FALSE;
7572 int newidx;
7573 int old_qfidx;
7574 qfline_T *qf_ptr;
7575
7576 // If the specified index is '$', then use the last entry
7577 if (di->di_tv.v_type == VAR_STRING
7578 && di->di_tv.vval.v_string != NULL
7579 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7580 newidx = qfl->qf_count;
7581 else
7582 {
7583 // Otherwise use the specified index
7584 newidx = tv_get_number_chk(&di->di_tv, &denote);
7585 if (denote)
7586 return FAIL;
7587 }
7588
7589 if (newidx < 1) // sanity check
7590 return FAIL;
7591 if (newidx > qfl->qf_count)
7592 newidx = qfl->qf_count;
7593
7594 old_qfidx = qfl->qf_index;
7595 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7596 if (qf_ptr == NULL)
7597 return FAIL;
7598 qfl->qf_ptr = qf_ptr;
7599 qfl->qf_index = newidx;
7600
7601 // If the current list is modified and it is displayed in the quickfix
7602 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007603 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007604 qf_win_pos_update(qi, old_qfidx);
7605
7606 return OK;
7607}
7608
7609/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007610 * Set the current index in the specified quickfix list
7611 */
7612 static int
7613qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7614{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007615 callback_T cb;
7616
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007617 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007618 cb = get_callback(&di->di_tv);
7619 if (cb.cb_name != NULL && *cb.cb_name != NUL)
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007620 set_callback(&qfl->qf_qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007621
7622 return OK;
7623}
7624
7625/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007626 * Set quickfix/location list properties (title, items, context).
7627 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007628 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007629 */
7630 static int
7631qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7632{
7633 dictitem_T *di;
7634 int retval = FAIL;
7635 int qf_idx;
7636 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007637 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007638
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007639 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007640 newlist = TRUE;
7641
7642 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007643 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007644 return FAIL;
7645
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007646 if (newlist)
7647 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007648 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007649 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007650 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007651 }
7652
Bram Moolenaar0398e002019-03-21 21:12:49 +01007653 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007654 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007655 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007656 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007657 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007658 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007659 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007660 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007661 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007662 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7663 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007664 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7665 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007666
Bram Moolenaar287153c2020-11-29 14:20:27 +01007667 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007668 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007669 if (newlist)
7670 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007671
Bram Moolenaard823fa92016-08-12 16:29:27 +02007672 return retval;
7673}
7674
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007675/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007676 * Free the entire quickfix/location list stack.
7677 * If the quickfix/location list window is open, then clear it.
7678 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007679 static void
7680qf_free_stack(win_T *wp, qf_info_T *qi)
7681{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007682 win_T *qfwin = qf_find_win(qi);
7683 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007684
7685 if (qfwin != NULL)
7686 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007687 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007688 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007689 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007690 qf_update_buffer(qi, NULL);
7691 }
7692
7693 if (wp != NULL && IS_LL_WINDOW(wp))
7694 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007695 // If in the location list window, then use the non-location list
7696 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007697 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007698 if (llwin != NULL)
7699 wp = llwin;
7700 }
7701
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007702 qf_free_all(wp);
7703 if (wp == NULL)
7704 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007705 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007706 qi->qf_curlist = 0;
7707 qi->qf_listcount = 0;
7708 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007709 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007710 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007711 // If the location list window is open, then create a new empty
7712 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007713 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007714
Bram Moolenaar95946f12019-03-31 15:31:59 +02007715 if (new_ll != NULL)
7716 {
7717 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007718
Bram Moolenaar95946f12019-03-31 15:31:59 +02007719 // first free the list reference in the location list window
7720 ll_free_all(&qfwin->w_llist_ref);
7721
7722 qfwin->w_llist_ref = new_ll;
7723 if (wp != qfwin)
7724 win_set_loclist(wp, new_ll);
7725 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007726 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007727}
7728
Bram Moolenaard823fa92016-08-12 16:29:27 +02007729/*
7730 * Populate the quickfix list with the items supplied in the list
7731 * of dictionaries. "title" will be copied to w:quickfix_title.
7732 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007733 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007734 */
7735 int
7736set_errorlist(
7737 win_T *wp,
7738 list_T *list,
7739 int action,
7740 char_u *title,
7741 dict_T *what)
7742{
7743 qf_info_T *qi = &ql_info;
7744 int retval = OK;
7745
7746 if (wp != NULL)
7747 {
7748 qi = ll_get_or_alloc_list(wp);
7749 if (qi == NULL)
7750 return FAIL;
7751 }
7752
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007753 if (action == 'f')
7754 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007755 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007756 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007757 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007758 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007759
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007760 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007761 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007762 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007763 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007764 _("cannot have both a list and a \"what\" argument"));
7765 return FAIL;
7766 }
7767
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007768 incr_quickfix_busy();
7769
7770 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007771 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007772 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007773 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007774 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007775 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007776 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007777 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007778
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007779 decr_quickfix_busy();
7780
Bram Moolenaard823fa92016-08-12 16:29:27 +02007781 return retval;
7782}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007783
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007784/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007785 * Mark the quickfix context and callback function as in use for all the lists
7786 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007787 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007788 static int
7789mark_quickfix_ctx(qf_info_T *qi, int copyID)
7790{
7791 int i;
7792 int abort = FALSE;
7793 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007794 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007795
7796 for (i = 0; i < LISTCOUNT && !abort; ++i)
7797 {
7798 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007799 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7800 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007801 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7802
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007803 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007804 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007805 }
7806
7807 return abort;
7808}
7809
7810/*
7811 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007812 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007813 */
7814 int
7815set_ref_in_quickfix(int copyID)
7816{
7817 int abort = FALSE;
7818 tabpage_T *tp;
7819 win_T *win;
7820
7821 abort = mark_quickfix_ctx(&ql_info, copyID);
7822 if (abort)
7823 return abort;
7824
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007825 abort = set_ref_in_callback(&qftf_cb, copyID);
7826 if (abort)
7827 return abort;
7828
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007829 FOR_ALL_TAB_WINDOWS(tp, win)
7830 {
7831 if (win->w_llist != NULL)
7832 {
7833 abort = mark_quickfix_ctx(win->w_llist, copyID);
7834 if (abort)
7835 return abort;
7836 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007837 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7838 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007839 // In a location list window and none of the other windows is
7840 // referring to this location list. Mark the location list
7841 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007842 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7843 if (abort)
7844 return abort;
7845 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007846 }
7847
7848 return abort;
7849}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007850#endif
7851
Bram Moolenaar81695252004-12-29 20:58:21 +00007852/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007853 * Return the autocmd name for the :cbuffer Ex commands
7854 */
7855 static char_u *
7856cbuffer_get_auname(cmdidx_T cmdidx)
7857{
7858 switch (cmdidx)
7859 {
7860 case CMD_cbuffer: return (char_u *)"cbuffer";
7861 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7862 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7863 case CMD_lbuffer: return (char_u *)"lbuffer";
7864 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7865 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7866 default: return NULL;
7867 }
7868}
7869
7870/*
7871 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7872 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7873 */
7874 static int
7875cbuffer_process_args(
7876 exarg_T *eap,
7877 buf_T **bufp,
7878 linenr_T *line1,
7879 linenr_T *line2)
7880{
7881 buf_T *buf = NULL;
7882
7883 if (*eap->arg == NUL)
7884 buf = curbuf;
7885 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7886 buf = buflist_findnr(atoi((char *)eap->arg));
7887
7888 if (buf == NULL)
7889 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007890 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007891 return FAIL;
7892 }
7893
7894 if (buf->b_ml.ml_mfp == NULL)
7895 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007896 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007897 return FAIL;
7898 }
7899
7900 if (eap->addr_count == 0)
7901 {
7902 eap->line1 = 1;
7903 eap->line2 = buf->b_ml.ml_line_count;
7904 }
7905
7906 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7907 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7908 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007909 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007910 return FAIL;
7911 }
7912
7913 *line1 = eap->line1;
7914 *line2 = eap->line2;
7915 *bufp = buf;
7916
7917 return OK;
7918}
7919
7920/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007921 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007922 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007923 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007924 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007925 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007926 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007927 */
7928 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007929ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007930{
7931 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007932 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007933 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007934 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007935 int_u save_qfid;
7936 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007937 char_u *qf_title;
7938 linenr_T line1;
7939 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007940
Bram Moolenaara16123a2019-03-28 20:31:07 +01007941 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007942 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007943 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007944 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007945#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007946 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007947 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007948#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007949 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007950
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007951 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007952 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7953 if (qi == NULL)
7954 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007955
Bram Moolenaara16123a2019-03-28 20:31:07 +01007956 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7957 return;
7958
7959 qf_title = qf_cmdtitle(*eap->cmdlinep);
7960
7961 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007962 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007963 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7964 (char *)qf_title, (char *)buf->b_sfname);
7965 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007966 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007967
7968 incr_quickfix_busy();
7969
7970 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7971 (eap->cmdidx != CMD_caddbuffer
7972 && eap->cmdidx != CMD_laddbuffer),
7973 line1, line2,
7974 qf_title, NULL);
7975 if (qf_stack_empty(qi))
7976 {
7977 decr_quickfix_busy();
7978 return;
7979 }
7980 if (res >= 0)
7981 qf_list_changed(qf_get_curlist(qi));
7982
7983 // Remember the current quickfix list identifier, so that we can
7984 // check for autocommands changing the current quickfix list.
7985 save_qfid = qf_get_curlist(qi)->qf_id;
7986 if (au_name != NULL)
7987 {
7988 buf_T *curbuf_old = curbuf;
7989
7990 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7991 TRUE, curbuf);
7992 if (curbuf != curbuf_old)
7993 // Autocommands changed buffer, don't jump now, "qi" may
7994 // be invalid.
7995 res = 0;
7996 }
7997 // Jump to the first error for a new list and if autocmds didn't
7998 // free the list.
7999 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
8000 eap->cmdidx == CMD_lbuffer)
8001 && qflist_valid(wp, save_qfid))
8002 // display the first error
8003 qf_jump_first(qi, save_qfid, eap->forceit);
8004
8005 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00008006}
8007
Bram Moolenaar1e015462005-09-25 22:16:38 +00008008#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008009/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008010 * Return the autocmd name for the :cexpr Ex commands.
8011 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008012 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01008013cexpr_get_auname(cmdidx_T cmdidx)
8014{
8015 switch (cmdidx)
8016 {
8017 case CMD_cexpr: return (char_u *)"cexpr";
8018 case CMD_cgetexpr: return (char_u *)"cgetexpr";
8019 case CMD_caddexpr: return (char_u *)"caddexpr";
8020 case CMD_lexpr: return (char_u *)"lexpr";
8021 case CMD_lgetexpr: return (char_u *)"lgetexpr";
8022 case CMD_laddexpr: return (char_u *)"laddexpr";
8023 default: return NULL;
8024 }
8025}
8026
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008027 int
8028trigger_cexpr_autocmd(int cmdidx)
8029{
8030 char_u *au_name = cexpr_get_auname(cmdidx);
8031
8032 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8033 curbuf->b_fname, TRUE, curbuf))
8034 {
8035 if (aborting())
8036 return FAIL;
8037 }
8038 return OK;
8039}
8040
8041 int
8042cexpr_core(exarg_T *eap, typval_T *tv)
8043{
8044 qf_info_T *qi;
8045 win_T *wp = NULL;
8046
8047 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8048 if (qi == NULL)
8049 return FAIL;
8050
8051 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8052 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8053 {
8054 int res;
8055 int_u save_qfid;
8056 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8057
8058 incr_quickfix_busy();
8059 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8060 (eap->cmdidx != CMD_caddexpr
8061 && eap->cmdidx != CMD_laddexpr),
8062 (linenr_T)0, (linenr_T)0,
8063 qf_cmdtitle(*eap->cmdlinep), NULL);
8064 if (qf_stack_empty(qi))
8065 {
8066 decr_quickfix_busy();
8067 return FAIL;
8068 }
8069 if (res >= 0)
8070 qf_list_changed(qf_get_curlist(qi));
8071
8072 // Remember the current quickfix list identifier, so that we can
8073 // check for autocommands changing the current quickfix list.
8074 save_qfid = qf_get_curlist(qi)->qf_id;
8075 if (au_name != NULL)
8076 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8077 curbuf->b_fname, TRUE, curbuf);
8078
8079 // Jump to the first error for a new list and if autocmds didn't
8080 // free the list.
8081 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8082 && qflist_valid(wp, save_qfid))
8083 // display the first error
8084 qf_jump_first(qi, save_qfid, eap->forceit);
8085 decr_quickfix_busy();
8086 return OK;
8087 }
8088
Bram Moolenaar677658a2022-01-05 16:09:06 +00008089 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008090 return FAIL;
8091}
8092
Bram Moolenaara16123a2019-03-28 20:31:07 +01008093/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008094 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8095 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008096 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008097 */
8098 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008099ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008100{
8101 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008102
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008103 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008104 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008105
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008106 // Evaluate the expression. When the result is a string or a list we can
8107 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008108 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008109 if (tv != NULL)
8110 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008111 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008112 free_tv(tv);
8113 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008114}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008115#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008116
8117/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008118 * Get the location list for ":lhelpgrep"
8119 */
8120 static qf_info_T *
8121hgr_get_ll(int *new_ll)
8122{
8123 win_T *wp;
8124 qf_info_T *qi;
8125
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008126 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008127 if (bt_help(curwin->w_buffer))
8128 wp = curwin;
8129 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008130 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008131 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008132
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008133 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008134 qi = NULL;
8135 else
8136 qi = wp->w_llist;
8137
8138 if (qi == NULL)
8139 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008140 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008141 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008142 return NULL;
8143 *new_ll = TRUE;
8144 }
8145
8146 return qi;
8147}
8148
8149/*
8150 * Search for a pattern in a help file.
8151 */
8152 static void
8153hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008154 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008155 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008156 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008157 regmatch_T *p_regmatch)
8158{
8159 FILE *fd;
8160 long lnum;
8161
8162 fd = mch_fopen((char *)fname, "r");
8163 if (fd == NULL)
8164 return;
8165
8166 lnum = 1;
8167 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8168 {
8169 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008170
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008171 // Convert a line if 'encoding' is not utf-8 and
8172 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008173 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008174 {
8175 line = string_convert(p_vc, IObuff, NULL);
8176 if (line == NULL)
8177 line = IObuff;
8178 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008179
8180 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8181 {
8182 int l = (int)STRLEN(line);
8183
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008184 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008185 while (l > 0 && line[l - 1] <= ' ')
8186 line[--l] = NUL;
8187
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008188 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008189 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008190 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008191 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008192 0,
8193 line,
8194 lnum,
thinca6864efa2021-06-19 20:45:20 +02008195 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008196 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008197 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008198 (int)(p_regmatch->endp[0] - line)
8199 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008200 FALSE, // vis_col
8201 NULL, // search pattern
8202 0, // nr
8203 1, // type
8204 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008205 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008206 {
8207 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008208 if (line != IObuff)
8209 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008210 break;
8211 }
8212 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008213 if (line != IObuff)
8214 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008215 ++lnum;
8216 line_breakcheck();
8217 }
8218 fclose(fd);
8219}
8220
8221/*
8222 * Search for a pattern in all the help files in the doc directory under
8223 * the given directory.
8224 */
8225 static void
8226hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008227 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008228 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008229 regmatch_T *p_regmatch,
8230 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008231#ifdef FEAT_MULTI_LANG
8232 , char_u *lang
8233#endif
8234 )
8235{
8236 int fcount;
8237 char_u **fnames;
8238 int fi;
8239
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008240 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008241 add_pathsep(dirname);
8242 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8243 if (gen_expand_wildcards(1, &dirname, &fcount,
8244 &fnames, EW_FILE|EW_SILENT) == OK
8245 && fcount > 0)
8246 {
8247 for (fi = 0; fi < fcount && !got_int; ++fi)
8248 {
8249#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008250 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008251 if (lang != NULL
8252 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008253 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008254 && !(STRNICMP(lang, "en", 2) == 0
8255 && STRNICMP("txt", fnames[fi]
8256 + STRLEN(fnames[fi]) - 3, 3) == 0))
8257 continue;
8258#endif
8259
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008260 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008261 }
8262 FreeWild(fcount, fnames);
8263 }
8264}
8265
8266/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008267 * Search for a pattern in all the help files in the 'runtimepath'
8268 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008269 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008270 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008271 */
8272 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008273hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008274{
8275 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008276
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008277 vimconv_T vc;
8278
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008279 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8280 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008281 vc.vc_type = CONV_NONE;
8282 if (!enc_utf8)
8283 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008284
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008285 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008286 p = p_rtp;
8287 while (*p != NUL && !got_int)
8288 {
8289 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8290
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008291 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008292#ifdef FEAT_MULTI_LANG
8293 , lang
8294#endif
8295 );
8296 }
8297
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008298 if (vc.vc_type != CONV_NONE)
8299 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008300}
8301
8302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 * ":helpgrep {pattern}"
8304 */
8305 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008306ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307{
8308 regmatch_T regmatch;
8309 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008310 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008311 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008312 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008313 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008314 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008315 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar73633f82012-01-20 13:39:07 +01008317 switch (eap->cmdidx)
8318 {
8319 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8320 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8321 default: break;
8322 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008323 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8324 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008325 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008326#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008327 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008328 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008329#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008330 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008331
Bram Moolenaar39665952018-08-15 20:59:48 +02008332 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008333 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008334 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008335 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008336 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008337 }
8338
Bram Moolenaara16123a2019-03-28 20:31:07 +01008339 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8340 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008341 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008342 p_cpo = empty_option;
8343
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008344 incr_quickfix_busy();
8345
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008346#ifdef FEAT_MULTI_LANG
8347 // Check for a specified language
8348 lang = check_help_lang(eap->arg);
8349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8351 regmatch.rm_ic = FALSE;
8352 if (regmatch.regprog != NULL)
8353 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008354 qf_list_T *qfl;
8355
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008356 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008357 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008358 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008360 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008361
Bram Moolenaar473de612013-06-08 18:19:48 +02008362 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008364 qfl->qf_nonevalid = FALSE;
8365 qfl->qf_ptr = qfl->qf_start;
8366 qfl->qf_index = 1;
8367 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008368 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 }
8370
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008371 if (p_cpo == empty_option)
8372 p_cpo = save_cpo;
8373 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008374 {
8375 // Darn, some plugin changed the value. If it's still empty it was
8376 // changed and restored, need to restore in the complicated way.
8377 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008378 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008379 if (save_cpo_allocated)
8380 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008381 }
8382
8383 if (updated)
8384 // This may open a window and source scripts, do this after 'cpo' was
8385 // restored.
8386 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387
Bram Moolenaar73633f82012-01-20 13:39:07 +01008388 if (au_name != NULL)
8389 {
8390 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8391 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008392 // When adding a location list to an existing location list stack,
8393 // if the autocmd made the stack invalid, then just return.
8394 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008395 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008396 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008397 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008398 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008399 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008400
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008401 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008402 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008403 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008404 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008405 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008406
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008407 decr_quickfix_busy();
8408
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008409 if (eap->cmdidx == CMD_lhelpgrep)
8410 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008411 // If the help window is not opened or if it already points to the
8412 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008413 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008414 {
8415 if (new_qi)
8416 ll_free_all(&qi);
8417 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008418 else if (curwin->w_llist == NULL && new_qi)
8419 // current window didn't have a location list associated with it
8420 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008421 curwin->w_llist = qi;
8422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008424
8425# if defined(EXITFREE) || defined(PROTO)
8426 void
8427free_quickfix()
8428{
8429 win_T *win;
8430 tabpage_T *tab;
8431
8432 qf_free_all(NULL);
8433 // Free all location lists
8434 FOR_ALL_TAB_WINDOWS(tab, win)
8435 qf_free_all(win);
8436
8437 ga_clear(&qfga);
8438}
8439# endif
8440
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008441#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008442
8443#if defined(FEAT_EVAL) || defined(PROTO)
8444# ifdef FEAT_QUICKFIX
8445 static void
8446get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8447{
8448 if (what_arg->v_type == VAR_UNKNOWN)
8449 {
8450 if (rettv_list_alloc(rettv) == OK)
8451 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008452 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008453 }
8454 else
8455 {
8456 if (rettv_dict_alloc(rettv) == OK)
8457 if (is_qf || (wp != NULL))
8458 {
8459 if (what_arg->v_type == VAR_DICT)
8460 {
8461 dict_T *d = what_arg->vval.v_dict;
8462
8463 if (d != NULL)
8464 qf_get_properties(wp, d, rettv->vval.v_dict);
8465 }
8466 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008467 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008468 }
8469 }
8470}
8471# endif
8472
8473/*
8474 * "getloclist()" function
8475 */
8476 void
8477f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8478{
8479# ifdef FEAT_QUICKFIX
8480 win_T *wp;
8481
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008482 if (in_vim9script()
8483 && (check_for_number_arg(argvars, 0) == FAIL
8484 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8485 return;
8486
Bram Moolenaare677df82019-09-02 22:31:11 +02008487 wp = find_win_by_nr_or_id(&argvars[0]);
8488 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8489# endif
8490}
8491
8492/*
8493 * "getqflist()" function
8494 */
8495 void
8496f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8497{
8498# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008499 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8500 return;
8501
Bram Moolenaare677df82019-09-02 22:31:11 +02008502 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8503# endif
8504}
8505
8506/*
8507 * Used by "setqflist()" and "setloclist()" functions
8508 */
8509 static void
8510set_qf_ll_list(
8511 win_T *wp UNUSED,
8512 typval_T *list_arg UNUSED,
8513 typval_T *action_arg UNUSED,
8514 typval_T *what_arg UNUSED,
8515 typval_T *rettv)
8516{
8517# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008518 char_u *act;
8519 int action = 0;
8520 static int recursive = 0;
8521# endif
8522
8523 rettv->vval.v_number = -1;
8524
8525# ifdef FEAT_QUICKFIX
8526 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008527 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008528 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008529 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008530 else
8531 {
8532 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008533 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008534 int valid_dict = TRUE;
8535
8536 if (action_arg->v_type == VAR_STRING)
8537 {
8538 act = tv_get_string_chk(action_arg);
8539 if (act == NULL)
8540 return; // type error; errmsg already given
8541 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8542 act[1] == NUL)
8543 action = *act;
8544 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008545 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008546 }
8547 else if (action_arg->v_type == VAR_UNKNOWN)
8548 action = ' ';
8549 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008550 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008551
8552 if (action_arg->v_type != VAR_UNKNOWN
8553 && what_arg->v_type != VAR_UNKNOWN)
8554 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008555 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8556 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008557 else
8558 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008559 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008560 valid_dict = FALSE;
8561 }
8562 }
8563
8564 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008565 if (l != NULL && action && valid_dict
8566 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008567 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008568 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008569 rettv->vval.v_number = 0;
8570 --recursive;
8571 }
8572# endif
8573}
8574
8575/*
8576 * "setloclist()" function
8577 */
8578 void
8579f_setloclist(typval_T *argvars, typval_T *rettv)
8580{
8581 win_T *win;
8582
8583 rettv->vval.v_number = -1;
8584
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008585 if (in_vim9script()
8586 && (check_for_number_arg(argvars, 0) == FAIL
8587 || check_for_list_arg(argvars, 1) == FAIL
8588 || check_for_opt_string_arg(argvars, 2) == FAIL
8589 || (argvars[2].v_type != VAR_UNKNOWN
8590 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8591 return;
8592
Bram Moolenaare677df82019-09-02 22:31:11 +02008593 win = find_win_by_nr_or_id(&argvars[0]);
8594 if (win != NULL)
8595 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8596}
8597
8598/*
8599 * "setqflist()" function
8600 */
8601 void
8602f_setqflist(typval_T *argvars, typval_T *rettv)
8603{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008604 if (in_vim9script()
8605 && (check_for_list_arg(argvars, 0) == FAIL
8606 || check_for_opt_string_arg(argvars, 1) == FAIL
8607 || (argvars[1].v_type != VAR_UNKNOWN
8608 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8609 return;
8610
Bram Moolenaare677df82019-09-02 22:31:11 +02008611 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8612}
8613#endif