blob: 5895e89239c6d38bb425ef02b6f949eeed812bce [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
239 // Retain ga_data from previous use. Reset the length to zero.
240 qfga.ga_len = 0;
241
242 return &qfga;
243}
244
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200245/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200246 * Maximum number of bytes allowed per line while reading a errorfile.
247 */
248#define LINE_MAXLEN 4096
249
haya14busae023d492022-02-08 18:09:29 +0000250/*
251 * Patterns used. Keep in sync with qf_parse_fmt[].
252 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200253static struct fmtpattern
254{
255 char_u convchar;
256 char *pattern;
257} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200258 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200259 {'f', ".\\+"}, // only used when at end
haya14busae023d492022-02-08 18:09:29 +0000260 {'n', "\\d\\+"}, // 1
261 {'l', "\\d\\+"}, // 2
262 {'e', "\\d\\+"}, // 3
263 {'c', "\\d\\+"}, // 4
264 {'k', "\\d\\+"}, // 5
265 {'t', "."}, // 6
266#define FMT_PATTERN_M 7
267 {'m', ".\\+"}, // 7
268#define FMT_PATTERN_R 8
269 {'r', ".*"}, // 8
270 {'p', "[- .]*"}, // 9
271 {'v', "\\d\\+"}, // 10
272 {'s', ".\\+"}, // 11
273 {'o', ".\\+"} // 12
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200274 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200275
276/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200277 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 * See fmt_pat definition above for the list of supported patterns. The
279 * pattern specifier is supplied in "efmpat". The converted pattern is stored
280 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 */
282 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283efmpat_to_regpat(
284 char_u *efmpat,
285 char_u *regpat,
286 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200287 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100288 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289{
290 char_u *srcptr;
291
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200293 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200294 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000295 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200296 return NULL;
297 }
haya14busae023d492022-02-08 18:09:29 +0000298 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200299 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000300 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000303 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200304 return NULL;
305 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200306 efminfo->addr[idx] = (char_u)++round;
307 *regpat++ = '\\';
308 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200309#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200312 // Also match "c:" in the file name, even when
313 // checking for a colon next: "%f:".
314 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200315 STRCPY(regpat, "\\%(\\a:\\)\\=");
316 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200317 }
318#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200320 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200321 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200323 // A file name may contain spaces, but this isn't
324 // in "\f". For "%f:%l:%m" there may be a ":" in
325 // the file name. Use ".\{-1,}x" instead (x is
326 // the next character), the requirement that :999:
327 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200328 STRCPY(regpat, ".\\{-1,}");
329 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 }
331 else
332 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200333 // File name followed by '\\' or '%': include as
334 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200335 STRCPY(regpat, "\\f\\+");
336 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 }
338 }
339 else
340 {
341 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200342 while ((*regpat = *srcptr++) != NUL)
343 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200345 *regpat++ = '\\';
346 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200348 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200349}
350
351/*
352 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200353 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354 */
355 static char_u *
356scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200357 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200358 char_u *efm,
359 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100360 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200361{
362 char_u *efmp = *pefmp;
363
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200364 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200365 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200366 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367 {
368 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200369 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370 if (efmp < efm + len)
371 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200372 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200375 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200376 if (efmp == efm + len)
377 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000378 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200379 return NULL;
380 }
381 }
382 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200383 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 *regpat++ = *++efmp;
385 *regpat++ = '\\';
386 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200387 }
388 else
389 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200390 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000391 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392 return NULL;
393 }
394
395 *pefmp = efmp;
396
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200397 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200398}
399
400/*
401 * Analyze/parse an errorformat prefix.
402 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200403 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100404efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200405{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200406 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200407 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200408 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200409 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200410 else
411 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000412 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200413 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200414 }
415
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200416 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200417}
418
419/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200420 * Converts a 'errorformat' string part in 'efm' to a regular expression
421 * pattern. The resulting regex pattern is returned in "regpat". Additional
422 * information about the 'erroformat' pattern is returned in "fmt_ptr".
423 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200424 */
425 static int
426efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200427 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200428 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200429 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100430 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200431{
432 char_u *ptr;
433 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200434 int round;
435 int idx = 0;
436
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200437 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200438 ptr = regpat;
439 *ptr++ = '^';
440 round = 0;
441 for (efmp = efm; efmp < efm + len; ++efmp)
442 {
443 if (*efmp == '%')
444 {
445 ++efmp;
446 for (idx = 0; idx < FMT_PATTERNS; ++idx)
447 if (fmt_pat[idx].convchar == *efmp)
448 break;
449 if (idx < FMT_PATTERNS)
450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100451 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200452 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200453 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200454 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200455 }
456 else if (*efmp == '*')
457 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200458 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100459 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200460 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200461 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200462 }
463 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200464 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200465 else if (*efmp == '#')
466 *ptr++ = '*';
467 else if (*efmp == '>')
468 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200469 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200470 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200471 // prefix is allowed only at the beginning of the errorformat
472 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100473 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200474 if (efmp == NULL)
475 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200476 }
477 else
478 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000479 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200480 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200481 }
482 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200483 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200484 {
485 if (*efmp == '\\' && efmp + 1 < efm + len)
486 ++efmp;
487 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200488 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200489 if (*efmp)
490 *ptr++ = *efmp;
491 }
492 }
493 *ptr++ = '$';
494 *ptr = NUL;
495
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200496 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200497}
498
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200499/*
500 * Free the 'errorformat' information list
501 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200502 static void
503free_efm_list(efm_T **efm_first)
504{
505 efm_T *efm_ptr;
506
507 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
508 {
509 *efm_first = efm_ptr->next;
510 vim_regfree(efm_ptr->prog);
511 vim_free(efm_ptr);
512 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100513 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200514}
515
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200516/*
517 * Compute the size of the buffer used to convert a 'errorformat' pattern into
518 * a regular expression pattern.
519 */
520 static int
521efm_regpat_bufsz(char_u *efm)
522{
523 int sz;
524 int i;
525
526 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
527 for (i = FMT_PATTERNS; i > 0; )
528 sz += (int)STRLEN(fmt_pat[--i].pattern);
529#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200530 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200531#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200532 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200533#endif
534
535 return sz;
536}
537
538/*
539 * Return the length of a 'errorformat' option part (separated by ",").
540 */
541 static int
542efm_option_part_len(char_u *efm)
543{
544 int len;
545
546 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
547 if (efm[len] == '\\' && efm[len + 1] != NUL)
548 ++len;
549
550 return len;
551}
552
553/*
554 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
555 * are parsed and converted to regular expressions. Returns information about
556 * the parsed 'errorformat' option.
557 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200558 static efm_T *
559parse_efm_option(char_u *efm)
560{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200561 efm_T *fmt_ptr = NULL;
562 efm_T *fmt_first = NULL;
563 efm_T *fmt_last = NULL;
564 char_u *fmtstr = NULL;
565 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200566 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200568 // Each part of the format string is copied and modified from errorformat
569 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200570
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200571 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200572 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000573 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200574 goto parse_efm_error;
575
576 while (efm[0] != NUL)
577 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200578 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000579 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200580 if (fmt_ptr == NULL)
581 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200582 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200583 fmt_first = fmt_ptr;
584 else
585 fmt_last->next = fmt_ptr;
586 fmt_last = fmt_ptr;
587
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200588 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200589 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200590
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100591 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200592 goto parse_efm_error;
593 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
594 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200595 // Advance to next part
596 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200597 }
598
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200599 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000600 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200601
602 goto parse_efm_end;
603
604parse_efm_error:
605 free_efm_list(&fmt_first);
606
607parse_efm_end:
608 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200609
610 return fmt_first;
611}
612
Bram Moolenaare0d37972016-07-15 22:36:01 +0200613enum {
614 QF_FAIL = 0,
615 QF_OK = 1,
616 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200617 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200618 QF_IGNORE_LINE = 4,
619 QF_MULTISCAN = 5,
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +0100620 QF_ABORT = 6
Bram Moolenaare0d37972016-07-15 22:36:01 +0200621};
622
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200623/*
624 * State information used to parse lines and add entries to a quickfix/location
625 * list.
626 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200627typedef struct {
628 char_u *linebuf;
629 int linelen;
630 char_u *growbuf;
631 int growbufsiz;
632 FILE *fd;
633 typval_T *tv;
634 char_u *p_str;
635 listitem_T *p_li;
636 buf_T *buf;
637 linenr_T buflnum;
638 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100639 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200640} qfstate_T;
641
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200642/*
643 * Allocate more memory for the line buffer used for parsing lines.
644 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200645 static char_u *
646qf_grow_linebuf(qfstate_T *state, int newsz)
647{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200648 char_u *p;
649
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200650 // If the line exceeds LINE_MAXLEN exclude the last
651 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200652 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
653 if (state->growbuf == NULL)
654 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000655 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200656 if (state->growbuf == NULL)
657 return NULL;
658 state->growbufsiz = state->linelen;
659 }
660 else if (state->linelen > state->growbufsiz)
661 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200662 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200663 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200664 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200665 state->growbufsiz = state->linelen;
666 }
667 return state->growbuf;
668}
669
670/*
671 * Get the next string (separated by newline) from state->p_str.
672 */
673 static int
674qf_get_next_str_line(qfstate_T *state)
675{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200676 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200677 char_u *p_str = state->p_str;
678 char_u *p;
679 int len;
680
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200681 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200682 return QF_END_OF_INPUT;
683
684 p = vim_strchr(p_str, '\n');
685 if (p != NULL)
686 len = (int)(p - p_str) + 1;
687 else
688 len = (int)STRLEN(p_str);
689
690 if (len > IOSIZE - 2)
691 {
692 state->linebuf = qf_grow_linebuf(state, len);
693 if (state->linebuf == NULL)
694 return QF_NOMEM;
695 }
696 else
697 {
698 state->linebuf = IObuff;
699 state->linelen = len;
700 }
701 vim_strncpy(state->linebuf, p_str, state->linelen);
702
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200703 // Increment using len in order to discard the rest of the
704 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200705 p_str += len;
706 state->p_str = p_str;
707
708 return QF_OK;
709}
710
711/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000712 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200713 */
714 static int
715qf_get_next_list_line(qfstate_T *state)
716{
717 listitem_T *p_li = state->p_li;
718 int len;
719
720 while (p_li != NULL
721 && (p_li->li_tv.v_type != VAR_STRING
722 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200723 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200725 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200726 {
727 state->p_li = NULL;
728 return QF_END_OF_INPUT;
729 }
730
731 len = (int)STRLEN(p_li->li_tv.vval.v_string);
732 if (len > IOSIZE - 2)
733 {
734 state->linebuf = qf_grow_linebuf(state, len);
735 if (state->linebuf == NULL)
736 return QF_NOMEM;
737 }
738 else
739 {
740 state->linebuf = IObuff;
741 state->linelen = len;
742 }
743
744 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
745
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200746 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200747 return QF_OK;
748}
749
750/*
751 * Get the next string from state->buf.
752 */
753 static int
754qf_get_next_buf_line(qfstate_T *state)
755{
756 char_u *p_buf = NULL;
757 int len;
758
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200759 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200760 if (state->buflnum > state->lnumlast)
761 return QF_END_OF_INPUT;
762
763 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
764 state->buflnum += 1;
765
766 len = (int)STRLEN(p_buf);
767 if (len > IOSIZE - 2)
768 {
769 state->linebuf = qf_grow_linebuf(state, len);
770 if (state->linebuf == NULL)
771 return QF_NOMEM;
772 }
773 else
774 {
775 state->linebuf = IObuff;
776 state->linelen = len;
777 }
778 vim_strncpy(state->linebuf, p_buf, state->linelen);
779
780 return QF_OK;
781}
782
783/*
784 * Get the next string from file state->fd.
785 */
786 static int
787qf_get_next_file_line(qfstate_T *state)
788{
789 int discard;
790 int growbuflen;
791
792 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
793 return QF_END_OF_INPUT;
794
795 discard = FALSE;
796 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200799 // The current line exceeds IObuff, continue reading using
800 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200801 if (state->growbuf == NULL)
802 {
803 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000804 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805 if (state->growbuf == NULL)
806 return QF_NOMEM;
807 }
808
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200809 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200810 memcpy(state->growbuf, IObuff, IOSIZE - 1);
811 growbuflen = state->linelen;
812
813 for (;;)
814 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200815 char_u *p;
816
Bram Moolenaare0d37972016-07-15 22:36:01 +0200817 if (fgets((char *)state->growbuf + growbuflen,
818 state->growbufsiz - growbuflen, state->fd) == NULL)
819 break;
820 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
821 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200822 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200823 break;
824 if (state->growbufsiz == LINE_MAXLEN)
825 {
826 discard = TRUE;
827 break;
828 }
829
830 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
831 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200832 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200833 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200834 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200835 }
836
837 while (discard)
838 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200839 // The current line is longer than LINE_MAXLEN, continue
840 // reading but discard everything until EOL or EOF is
841 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200842 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
843 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200844 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200845 break;
846 }
847
848 state->linebuf = state->growbuf;
849 state->linelen = growbuflen;
850 }
851 else
852 state->linebuf = IObuff;
853
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200854 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200855 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
856 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100857 char_u *line;
858
859 line = string_convert(&state->vc, state->linebuf, &state->linelen);
860 if (line != NULL)
861 {
862 if (state->linelen < IOSIZE)
863 {
864 STRCPY(state->linebuf, line);
865 vim_free(line);
866 }
867 else
868 {
869 vim_free(state->growbuf);
870 state->linebuf = state->growbuf = line;
871 state->growbufsiz = state->linelen < LINE_MAXLEN
872 ? state->linelen : LINE_MAXLEN;
873 }
874 }
875 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100876
Bram Moolenaare0d37972016-07-15 22:36:01 +0200877 return QF_OK;
878}
879
880/*
881 * Get the next string from a file/buffer/list/string.
882 */
883 static int
884qf_get_nextline(qfstate_T *state)
885{
886 int status = QF_FAIL;
887
888 if (state->fd == NULL)
889 {
890 if (state->tv != NULL)
891 {
892 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200893 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200894 status = qf_get_next_str_line(state);
895 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200896 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200897 status = qf_get_next_list_line(state);
898 }
899 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200900 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200901 status = qf_get_next_buf_line(state);
902 }
903 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200904 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200905 status = qf_get_next_file_line(state);
906
907 if (status != QF_OK)
908 return status;
909
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200910 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200911 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200912 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200913 state->linebuf[state->linelen - 1] = NUL;
914#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200915 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
916 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200917#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200918 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200919
Bram Moolenaare0d37972016-07-15 22:36:01 +0200920 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200921
922 return QF_OK;
923}
924
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200925typedef struct {
926 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200927 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200928 char_u *errmsg;
929 int errmsglen;
930 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200931 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200932 int col;
thinca6864efa2021-06-19 20:45:20 +0200933 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200934 char_u use_viscol;
935 char_u *pattern;
936 int enr;
937 int type;
938 int valid;
939} qffields_T;
940
941/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200942 * Parse the match for filename ('%f') pattern in regmatch.
943 * Return the matched value in "fields->namebuf".
944 */
945 static int
946qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
947{
948 int c;
949
950 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
951 return QF_FAIL;
952
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200953 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200954 c = *rmp->endp[midx];
955 *rmp->endp[midx] = NUL;
956 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
957 *rmp->endp[midx] = c;
958
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200959 // For separate filename patterns (%O, %P and %Q), the specified file
960 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200961 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
962 && mch_getperm(fields->namebuf) == -1)
963 return QF_FAIL;
964
965 return QF_OK;
966}
967
968/*
969 * Parse the match for error number ('%n') pattern in regmatch.
970 * Return the matched value in "fields->enr".
971 */
972 static int
973qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
974{
975 if (rmp->startp[midx] == NULL)
976 return QF_FAIL;
977 fields->enr = (int)atol((char *)rmp->startp[midx]);
978 return QF_OK;
979}
980
981/*
haya14busae023d492022-02-08 18:09:29 +0000982 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200983 * Return the matched value in "fields->lnum".
984 */
985 static int
986qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
987{
988 if (rmp->startp[midx] == NULL)
989 return QF_FAIL;
990 fields->lnum = atol((char *)rmp->startp[midx]);
991 return QF_OK;
992}
993
994/*
haya14busae023d492022-02-08 18:09:29 +0000995 * Parse the match for end line number ('%e') pattern in regmatch.
996 * Return the matched value in "fields->end_lnum".
997 */
998 static int
999qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
1000{
1001 if (rmp->startp[midx] == NULL)
1002 return QF_FAIL;
1003 fields->end_lnum = atol((char *)rmp->startp[midx]);
1004 return QF_OK;
1005}
1006
1007/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001008 * Parse the match for column number ('%c') pattern in regmatch.
1009 * Return the matched value in "fields->col".
1010 */
1011 static int
1012qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
1013{
1014 if (rmp->startp[midx] == NULL)
1015 return QF_FAIL;
1016 fields->col = (int)atol((char *)rmp->startp[midx]);
1017 return QF_OK;
1018}
1019
1020/*
haya14busae023d492022-02-08 18:09:29 +00001021 * Parse the match for end column number ('%k') pattern in regmatch.
1022 * Return the matched value in "fields->end_col".
1023 */
1024 static int
1025qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1026{
1027 if (rmp->startp[midx] == NULL)
1028 return QF_FAIL;
1029 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1030 return QF_OK;
1031}
1032
1033/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001034 * Parse the match for error type ('%t') pattern in regmatch.
1035 * Return the matched value in "fields->type".
1036 */
1037 static int
1038qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1039{
1040 if (rmp->startp[midx] == NULL)
1041 return QF_FAIL;
1042 fields->type = *rmp->startp[midx];
1043 return QF_OK;
1044}
1045
1046/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001047 * Copy a non-error line into the error string. Return the matched line in
1048 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001049 */
1050 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001051copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001052{
1053 char_u *p;
1054
1055 if (linelen >= fields->errmsglen)
1056 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001057 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001058 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1059 return QF_NOMEM;
1060 fields->errmsg = p;
1061 fields->errmsglen = linelen + 1;
1062 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001063 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001064 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001065
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001066 return QF_OK;
1067}
1068
1069/*
1070 * Parse the match for error message ('%m') pattern in regmatch.
1071 * Return the matched value in "fields->errmsg".
1072 */
1073 static int
1074qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1075{
1076 char_u *p;
1077 int len;
1078
1079 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1080 return QF_FAIL;
1081 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1082 if (len >= fields->errmsglen)
1083 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001084 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001085 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1086 return QF_NOMEM;
1087 fields->errmsg = p;
1088 fields->errmsglen = len + 1;
1089 }
1090 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1091 return QF_OK;
1092}
1093
1094/*
1095 * Parse the match for rest of a single-line file message ('%r') pattern.
1096 * Return the matched value in "tail".
1097 */
1098 static int
1099qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1100{
1101 if (rmp->startp[midx] == NULL)
1102 return QF_FAIL;
1103 *tail = rmp->startp[midx];
1104 return QF_OK;
1105}
1106
1107/*
1108 * Parse the match for the pointer line ('%p') pattern in regmatch.
1109 * Return the matched value in "fields->col".
1110 */
1111 static int
1112qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1113{
1114 char_u *match_ptr;
1115
1116 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1117 return QF_FAIL;
1118 fields->col = 0;
1119 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1120 ++match_ptr)
1121 {
1122 ++fields->col;
1123 if (*match_ptr == TAB)
1124 {
1125 fields->col += 7;
1126 fields->col -= fields->col % 8;
1127 }
1128 }
1129 ++fields->col;
1130 fields->use_viscol = TRUE;
1131 return QF_OK;
1132}
1133
1134/*
1135 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1136 * Return the matched value in "fields->col".
1137 */
1138 static int
1139qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1140{
1141 if (rmp->startp[midx] == NULL)
1142 return QF_FAIL;
1143 fields->col = (int)atol((char *)rmp->startp[midx]);
1144 fields->use_viscol = TRUE;
1145 return QF_OK;
1146}
1147
1148/*
1149 * Parse the match for the search text ('%s') pattern in regmatch.
1150 * Return the matched value in "fields->pattern".
1151 */
1152 static int
1153qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1154{
1155 int len;
1156
1157 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1158 return QF_FAIL;
1159 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1160 if (len > CMDBUFFSIZE - 5)
1161 len = CMDBUFFSIZE - 5;
1162 STRCPY(fields->pattern, "^\\V");
1163 STRNCAT(fields->pattern, rmp->startp[midx], len);
1164 fields->pattern[len + 3] = '\\';
1165 fields->pattern[len + 4] = '$';
1166 fields->pattern[len + 5] = NUL;
1167 return QF_OK;
1168}
1169
1170/*
1171 * Parse the match for the module ('%o') pattern in regmatch.
1172 * Return the matched value in "fields->module".
1173 */
1174 static int
1175qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1176{
1177 int len;
1178
1179 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1180 return QF_FAIL;
1181 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1182 if (len > CMDBUFFSIZE)
1183 len = CMDBUFFSIZE;
1184 STRNCAT(fields->module, rmp->startp[midx], len);
1185 return QF_OK;
1186}
1187
1188/*
1189 * 'errorformat' format pattern parser functions.
1190 * The '%f' and '%r' formats are parsed differently from other formats.
1191 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001192 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001193 */
1194static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1195{
haya14busae023d492022-02-08 18:09:29 +00001196 NULL, // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001197 qf_parse_fmt_n,
1198 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001199 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001200 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001201 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001202 qf_parse_fmt_t,
1203 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001204 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001205 qf_parse_fmt_p,
1206 qf_parse_fmt_v,
1207 qf_parse_fmt_s,
1208 qf_parse_fmt_o
1209};
1210
1211/*
1212 * Parse the error format pattern matches in "regmatch" and set the values in
1213 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1214 * match. Returns QF_OK if all the matches are successfully parsed. On
1215 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001216 */
1217 static int
1218qf_parse_match(
1219 char_u *linebuf,
1220 int linelen,
1221 efm_T *fmt_ptr,
1222 regmatch_T *regmatch,
1223 qffields_T *fields,
1224 int qf_multiline,
1225 int qf_multiscan,
1226 char_u **tail)
1227{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001228 int idx = fmt_ptr->prefix;
1229 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001230 int midx;
1231 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001232
1233 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1234 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001235 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001236 fields->type = idx;
1237 else
1238 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001239
1240 // Extract error message data from matched line.
1241 // We check for an actual submatch, because "\[" and "\]" in
1242 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001243 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001244 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001245 status = QF_OK;
1246 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001247 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001248 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001249 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001250 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001251 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001252 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001253 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001254 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001255 }
haya14busae023d492022-02-08 18:09:29 +00001256 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001257 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001258 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001259 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001260
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001261 if (status != QF_OK)
1262 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001263 }
1264
1265 return QF_OK;
1266}
1267
1268/*
1269 * Parse an error line in 'linebuf' using a single error format string in
1270 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1271 * Returns QF_OK if the efm format matches completely and the fields are
1272 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1273 */
1274 static int
1275qf_parse_get_fields(
1276 char_u *linebuf,
1277 int linelen,
1278 efm_T *fmt_ptr,
1279 qffields_T *fields,
1280 int qf_multiline,
1281 int qf_multiscan,
1282 char_u **tail)
1283{
1284 regmatch_T regmatch;
1285 int status = QF_FAIL;
1286 int r;
1287
1288 if (qf_multiscan &&
1289 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1290 return QF_FAIL;
1291
1292 fields->namebuf[0] = NUL;
1293 fields->module[0] = NUL;
1294 fields->pattern[0] = NUL;
1295 if (!qf_multiscan)
1296 fields->errmsg[0] = NUL;
1297 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001298 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001299 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001300 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001301 fields->use_viscol = FALSE;
1302 fields->enr = -1;
1303 fields->type = 0;
1304 *tail = NULL;
1305
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001306 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001307 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001308 regmatch.regprog = fmt_ptr->prog;
1309 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1310 fmt_ptr->prog = regmatch.regprog;
1311 if (r)
1312 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1313 fields, qf_multiline, qf_multiscan, tail);
1314
1315 return status;
1316}
1317
1318/*
1319 * Parse directory error format prefixes (%D and %X).
1320 * Push and pop directories from the directory stack when scanning directory
1321 * names.
1322 */
1323 static int
1324qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1325{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001326 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001327 {
1328 if (*fields->namebuf == NUL)
1329 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001330 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001331 return QF_FAIL;
1332 }
1333 qfl->qf_directory =
1334 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1335 if (qfl->qf_directory == NULL)
1336 return QF_FAIL;
1337 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001338 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001339 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1340
1341 return QF_OK;
1342}
1343
1344/*
1345 * Parse global file name error format prefixes (%O, %P and %Q).
1346 */
1347 static int
1348qf_parse_file_pfx(
1349 int idx,
1350 qffields_T *fields,
1351 qf_list_T *qfl,
1352 char_u *tail)
1353{
1354 fields->valid = FALSE;
1355 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1356 {
1357 if (*fields->namebuf && idx == 'P')
1358 qfl->qf_currfile =
1359 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1360 else if (idx == 'Q')
1361 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1362 *fields->namebuf = NUL;
1363 if (tail && *tail)
1364 {
1365 STRMOVE(IObuff, skipwhite(tail));
1366 qfl->qf_multiscan = TRUE;
1367 return QF_MULTISCAN;
1368 }
1369 }
1370
1371 return QF_OK;
1372}
1373
1374/*
1375 * Parse a non-error line (a line which doesn't match any of the error
1376 * format in 'efm').
1377 */
1378 static int
1379qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1380{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001381 fields->namebuf[0] = NUL; // no match found, remove file name
1382 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001383 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001384
Bram Moolenaarf4140482020-02-15 23:06:45 +01001385 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001386}
1387
1388/*
1389 * Parse multi-line error format prefixes (%C and %Z)
1390 */
1391 static int
1392qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001393 int idx,
1394 qf_list_T *qfl,
1395 qffields_T *fields)
1396{
1397 char_u *ptr;
1398 int len;
1399
1400 if (!qfl->qf_multiignore)
1401 {
1402 qfline_T *qfprev = qfl->qf_last;
1403
1404 if (qfprev == NULL)
1405 return QF_FAIL;
1406 if (*fields->errmsg && !qfl->qf_multiignore)
1407 {
1408 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001409 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1410 aid_qf_multiline_pfx);
1411 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001412 return QF_FAIL;
1413 STRCPY(ptr, qfprev->qf_text);
1414 vim_free(qfprev->qf_text);
1415 qfprev->qf_text = ptr;
1416 *(ptr += len) = '\n';
1417 STRCPY(++ptr, fields->errmsg);
1418 }
1419 if (qfprev->qf_nr == -1)
1420 qfprev->qf_nr = fields->enr;
1421 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001422 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001423 qfprev->qf_type = fields->type;
1424
1425 if (!qfprev->qf_lnum)
1426 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001427 if (!qfprev->qf_end_lnum)
1428 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001429 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001430 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001431 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001432 qfprev->qf_viscol = fields->use_viscol;
1433 }
haya14busae023d492022-02-08 18:09:29 +00001434 if (!qfprev->qf_end_col)
1435 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001436 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001437 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001438 qfl->qf_directory,
1439 *fields->namebuf || qfl->qf_directory != NULL
1440 ? fields->namebuf
1441 : qfl->qf_currfile != NULL && fields->valid
1442 ? qfl->qf_currfile : 0);
1443 }
1444 if (idx == 'Z')
1445 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1446 line_breakcheck();
1447
1448 return QF_IGNORE_LINE;
1449}
1450
1451/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001452 * Parse a line and get the quickfix fields.
1453 * Return the QF_ status.
1454 */
1455 static int
1456qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001457 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 char_u *linebuf,
1459 int linelen,
1460 efm_T *fmt_first,
1461 qffields_T *fields)
1462{
1463 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001464 int idx = 0;
1465 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001466 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467
Bram Moolenaare333e792018-04-08 13:27:39 +02001468restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001469 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001470 if (fmt_start == NULL)
1471 fmt_ptr = fmt_first;
1472 else
1473 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001474 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001475 fmt_ptr = fmt_start;
1476 fmt_start = NULL;
1477 }
1478
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001479 // Try to match each part of 'errorformat' until we find a complete
1480 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001481 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001482 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1483 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001484 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001485 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1486 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1487 if (status == QF_NOMEM)
1488 return status;
1489 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001490 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001491 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001492 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001493
1494 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1495 {
1496 if (fmt_ptr != NULL)
1497 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001498 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001499 status = qf_parse_dir_pfx(idx, fields, qfl);
1500 if (status != QF_OK)
1501 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001502 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001503
1504 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1505 if (status != QF_OK)
1506 return status;
1507
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001508 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001509 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001510 }
1511 else if (fmt_ptr != NULL)
1512 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001513 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001514 if (fmt_ptr->conthere)
1515 fmt_start = fmt_ptr;
1516
Bram Moolenaare9283662020-06-07 14:10:47 +02001517 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001518 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001519 qfl->qf_multiline = TRUE; // start of a multi-line message
1520 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001521 }
1522 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001523 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001524 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001525 if (status != QF_OK)
1526 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001527 }
1528 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001529 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001530 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1531 if (status == QF_MULTISCAN)
1532 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001533 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001534 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001535 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001536 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001537 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001538 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001539 return QF_IGNORE_LINE;
1540 }
1541 }
1542
1543 return QF_OK;
1544}
1545
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001546/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001547 * Returns TRUE if the specified quickfix/location stack is empty
1548 */
1549 static int
1550qf_stack_empty(qf_info_T *qi)
1551{
1552 return qi == NULL || qi->qf_listcount <= 0;
1553}
1554
1555/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001556 * Returns TRUE if the specified quickfix/location list is empty.
1557 */
1558 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001559qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001560{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001561 return qfl == NULL || qfl->qf_count <= 0;
1562}
1563
1564/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001565 * Returns TRUE if the specified quickfix/location list is not empty and
1566 * has valid entries.
1567 */
1568 static int
1569qf_list_has_valid_entries(qf_list_T *qfl)
1570{
1571 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1572}
1573
1574/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001575 * Return a pointer to a list in the specified quickfix stack
1576 */
1577 static qf_list_T *
1578qf_get_list(qf_info_T *qi, int idx)
1579{
1580 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001581}
1582
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001583/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001584 * Allocate the fields used for parsing lines and populating a quickfix list.
1585 */
1586 static int
1587qf_alloc_fields(qffields_T *pfields)
1588{
1589 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1590 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1591 pfields->errmsglen = CMDBUFFSIZE + 1;
1592 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1593 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1594 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1595 || pfields->pattern == NULL || pfields->module == NULL)
1596 return FAIL;
1597
1598 return OK;
1599}
1600
1601/*
1602 * Free the fields used for parsing lines and populating a quickfix list.
1603 */
1604 static void
1605qf_free_fields(qffields_T *pfields)
1606{
1607 vim_free(pfields->namebuf);
1608 vim_free(pfields->module);
1609 vim_free(pfields->errmsg);
1610 vim_free(pfields->pattern);
1611}
1612
1613/*
1614 * Setup the state information used for parsing lines and populating a
1615 * quickfix list.
1616 */
1617 static int
1618qf_setup_state(
1619 qfstate_T *pstate,
1620 char_u *enc,
1621 char_u *efile,
1622 typval_T *tv,
1623 buf_T *buf,
1624 linenr_T lnumfirst,
1625 linenr_T lnumlast)
1626{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001627 pstate->vc.vc_type = CONV_NONE;
1628 if (enc != NULL && *enc != NUL)
1629 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001630
1631 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1632 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001633 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001634 return FAIL;
1635 }
1636
1637 if (tv != NULL)
1638 {
1639 if (tv->v_type == VAR_STRING)
1640 pstate->p_str = tv->vval.v_string;
1641 else if (tv->v_type == VAR_LIST)
1642 pstate->p_li = tv->vval.v_list->lv_first;
1643 pstate->tv = tv;
1644 }
1645 pstate->buf = buf;
1646 pstate->buflnum = lnumfirst;
1647 pstate->lnumlast = lnumlast;
1648
1649 return OK;
1650}
1651
1652/*
1653 * Cleanup the state information used for parsing lines and populating a
1654 * quickfix list.
1655 */
1656 static void
1657qf_cleanup_state(qfstate_T *pstate)
1658{
1659 if (pstate->fd != NULL)
1660 fclose(pstate->fd);
1661
1662 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001663 if (pstate->vc.vc_type != CONV_NONE)
1664 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001665}
1666
1667/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001668 * Process the next line from a file/buffer/list/string and add it
1669 * to the quickfix list 'qfl'.
1670 */
1671 static int
1672qf_init_process_nextline(
1673 qf_list_T *qfl,
1674 efm_T *fmt_first,
1675 qfstate_T *state,
1676 qffields_T *fields)
1677{
1678 int status;
1679
1680 // Get the next line from a file/buffer/list/string
1681 status = qf_get_nextline(state);
1682 if (status != QF_OK)
1683 return status;
1684
1685 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1686 fmt_first, fields);
1687 if (status != QF_OK)
1688 return status;
1689
1690 return qf_add_entry(qfl,
1691 qfl->qf_directory,
1692 (*fields->namebuf || qfl->qf_directory != NULL)
1693 ? fields->namebuf
1694 : ((qfl->qf_currfile != NULL && fields->valid)
1695 ? qfl->qf_currfile : (char_u *)NULL),
1696 fields->module,
1697 0,
1698 fields->errmsg,
1699 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001700 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001701 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001702 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001703 fields->use_viscol,
1704 fields->pattern,
1705 fields->enr,
1706 fields->type,
1707 fields->valid);
1708}
1709
1710/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001711 * Read the errorfile "efile" into memory, line by line, building the error
1712 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001713 * Alternative: when "efile" is NULL read errors from buffer "buf".
1714 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001715 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001716 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1717 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001718 * Return -1 for error, number of errors for success.
1719 */
1720 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001721qf_init_ext(
1722 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001723 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001724 char_u *efile,
1725 buf_T *buf,
1726 typval_T *tv,
1727 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001728 int newlist, // TRUE: start a new error list
1729 linenr_T lnumfirst, // first line number to use
1730 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001731 char_u *qf_title,
1732 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001733{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001734 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001735 qfstate_T state;
1736 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001737 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001738 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001739 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001741 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001742 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001743 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001745 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001746 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001747
Bram Moolenaara80faa82020-04-12 19:37:17 +02001748 CLEAR_FIELD(state);
1749 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001750 if ((qf_alloc_fields(&fields) == FAIL) ||
1751 (qf_setup_state(&state, enc, efile, tv, buf,
1752 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 goto qf_init_end;
1754
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001755 if (newlist || qf_idx == qi->qf_listcount)
1756 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001757 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001758 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001759 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001760 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001761 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001762 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001763 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001764 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001765 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001766 qfl = qf_get_list(qi, qf_idx);
1767 if (!qf_list_empty(qfl))
1768 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001771 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001772 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001773 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 else
1775 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001777 // If the errorformat didn't change between calls, then reuse the
1778 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001779 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1780 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001781 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001782 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001783 free_efm_list(&fmt_first);
1784
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001785 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001786 fmt_first = parse_efm_option(efm);
1787 if (fmt_first != NULL)
1788 last_efm = vim_strsave(efm);
1789 }
1790
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001791 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001794 // got_int is reset here, because it was probably set when killing the
1795 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 got_int = FALSE;
1797
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001798 // Read the lines in the error file one by one.
1799 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001800 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001802 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001803 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001804 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001805 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001806 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001807 if (status == QF_FAIL)
1808 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 line_breakcheck();
1811 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001812 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001814 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001816 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001817 qfl->qf_ptr = qfl->qf_start;
1818 qfl->qf_index = 1;
1819 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821 else
1822 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001823 qfl->qf_nonevalid = FALSE;
1824 if (qfl->qf_ptr == NULL)
1825 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001827 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001828 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001829 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001831 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001833 if (!adding)
1834 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001835 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001836 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001837 qi->qf_listcount--;
1838 if (qi->qf_curlist > 0)
1839 --qi->qf_curlist;
1840 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001841qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001842 if (qf_idx == qi->qf_curlist)
1843 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001844 qf_cleanup_state(&state);
1845 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 return retval;
1848}
1849
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001850/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001851 * Read the errorfile "efile" into memory, line by line, building the error
1852 * list. Set the error list's title to qf_title.
1853 * Return -1 for error, number of errors for success.
1854 */
1855 int
1856qf_init(win_T *wp,
1857 char_u *efile,
1858 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001859 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001860 char_u *qf_title,
1861 char_u *enc)
1862{
1863 qf_info_T *qi = &ql_info;
1864
1865 if (wp != NULL)
1866 {
1867 qi = ll_get_or_alloc_list(wp);
1868 if (qi == NULL)
1869 return FAIL;
1870 }
1871
1872 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1873 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1874}
1875
1876/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001877 * Set the title of the specified quickfix list. Frees the previous title.
1878 * Prepends ':' to the title.
1879 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001880 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001881qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001882{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001883 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001884
Bram Moolenaarfb604092014-07-23 15:55:00 +02001885 if (title != NULL)
1886 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001887 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001888
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001889 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001890 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001891 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001892 }
1893}
1894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001896 * The title of a quickfix/location list is set, by default, to the command
1897 * that created the quickfix list with the ":" prefix.
1898 * Create a quickfix list title string by prepending ":" to a user command.
1899 * Returns a pointer to a static buffer with the title.
1900 */
1901 static char_u *
1902qf_cmdtitle(char_u *cmd)
1903{
1904 static char_u qftitle_str[IOSIZE];
1905
1906 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1907 return qftitle_str;
1908}
1909
1910/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001911 * Return a pointer to the current list in the specified quickfix stack
1912 */
1913 static qf_list_T *
1914qf_get_curlist(qf_info_T *qi)
1915{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001916 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001917}
1918
1919/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001920 * Prepare for adding a new quickfix list. If the current list is in the
1921 * middle of the stack, then all the following lists are freed and then
1922 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
1924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001925qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926{
1927 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001928 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001930 // If the current entry is not the last entry, delete entries beyond
1931 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001932 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001934 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001936 // When the stack is full, remove to oldest entry
1937 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001938 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001940 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 qi->qf_lists[i - 1] = qi->qf_lists[i];
1943 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001947 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001948 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001949 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001950 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001951 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952}
1953
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001954/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001955 * Queue location list stack delete request.
1956 */
1957 static void
1958locstack_queue_delreq(qf_info_T *qi)
1959{
1960 qf_delq_T *q;
1961
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001962 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001963 if (q != NULL)
1964 {
1965 q->qi = qi;
1966 q->next = qf_delq_head;
1967 qf_delq_head = q;
1968 }
1969}
1970
1971/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001972 * Return the global quickfix stack window buffer number.
1973 */
1974 int
1975qf_stack_get_bufnr(void)
1976{
1977 return ql_info.qf_bufnr;
1978}
1979
1980/*
1981 * Wipe the quickfix window buffer (if present) for the specified
1982 * quickfix/location list.
1983 */
1984 static void
1985wipe_qf_buffer(qf_info_T *qi)
1986{
1987 buf_T *qfbuf;
1988
1989 if (qi->qf_bufnr != INVALID_QFBUFNR)
1990 {
1991 qfbuf = buflist_findnr(qi->qf_bufnr);
1992 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1993 {
1994 // If the quickfix buffer is not loaded in any window, then
1995 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001996 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001997 qi->qf_bufnr = INVALID_QFBUFNR;
1998 }
1999 }
2000}
2001
2002/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002003 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002004 */
2005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002006ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002007{
2008 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002010
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002011 qi = *pqi;
2012 if (qi == NULL)
2013 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002014 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002016 // If the location list is still in use, then queue the delete request
2017 // to be processed later.
2018 if (quickfix_busy > 0)
2019 {
2020 locstack_queue_delreq(qi);
2021 return;
2022 }
2023
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002024 qi->qf_refcount--;
2025 if (qi->qf_refcount < 1)
2026 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002027 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002028 // If the quickfix window buffer is loaded, then wipe it
2029 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002030
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002031 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002032 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002033 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002034 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002035}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002036
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002037/*
2038 * Free all the quickfix/location lists in the stack.
2039 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002040 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002041qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002042{
2043 int i;
2044 qf_info_T *qi = &ql_info;
2045
2046 if (wp != NULL)
2047 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002048 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002049 ll_free_all(&wp->w_llist);
2050 ll_free_all(&wp->w_llist_ref);
2051 }
2052 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002053 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002054 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002055 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002056}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002059 * Delay freeing of location list stacks when the quickfix code is running.
2060 * Used to avoid problems with autocmds freeing location list stacks when the
2061 * quickfix code is still referencing the stack.
2062 * Must always call decr_quickfix_busy() exactly once after this.
2063 */
2064 static void
2065incr_quickfix_busy(void)
2066{
2067 quickfix_busy++;
2068}
2069
2070/*
2071 * Safe to free location list stacks. Process any delayed delete requests.
2072 */
2073 static void
2074decr_quickfix_busy(void)
2075{
2076 if (--quickfix_busy == 0)
2077 {
2078 // No longer referencing the location lists. Process all the pending
2079 // delete requests.
2080 while (qf_delq_head != NULL)
2081 {
2082 qf_delq_T *q = qf_delq_head;
2083
2084 qf_delq_head = q->next;
2085 ll_free_all(&q->qi);
2086 vim_free(q);
2087 }
2088 }
2089#ifdef ABORT_ON_INTERNAL_ERROR
2090 if (quickfix_busy < 0)
2091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002092 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002093 abort();
2094 }
2095#endif
2096}
2097
2098#if defined(EXITFREE) || defined(PROTO)
2099 void
2100check_quickfix_busy(void)
2101{
2102 if (quickfix_busy != 0)
2103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002105# ifdef ABORT_ON_INTERNAL_ERROR
2106 abort();
2107# endif
2108 }
2109}
2110#endif
2111
2112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002114 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 */
2116 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002117qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002118 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002119 char_u *dir, // optional directory name
2120 char_u *fname, // file name or NULL
2121 char_u *module, // module name or NULL
2122 int bufnum, // buffer number or zero
2123 char_u *mesg, // message
2124 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002125 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002126 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002127 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002128 int vis_col, // using visual column
2129 char_u *pattern, // search pattern
2130 int nr, // error number
2131 int type, // type character
2132 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002134 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002135 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002137 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002138 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002139 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002140 {
2141 buf_T *buf = buflist_findnr(bufnum);
2142
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002143 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002144 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002145 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002146 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002147 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002148 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002149 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2151 {
2152 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002153 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
2155 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002156 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002158 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002159 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002160 if (pattern == NULL || *pattern == NUL)
2161 qfp->qf_pattern = NULL;
2162 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2163 {
2164 vim_free(qfp->qf_text);
2165 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002166 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002167 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002168 if (module == NULL || *module == NUL)
2169 qfp->qf_module = NULL;
2170 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2171 {
2172 vim_free(qfp->qf_text);
2173 vim_free(qfp->qf_pattern);
2174 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002175 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002178 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 type = 0;
2180 qfp->qf_type = type;
2181 qfp->qf_valid = valid;
2182
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002183 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002184 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002186 qfl->qf_start = qfp;
2187 qfl->qf_ptr = qfp;
2188 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002189 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 }
2191 else
2192 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002193 qfp->qf_prev = *lastp;
2194 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002196 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002198 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002199 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002200 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002202 qfl->qf_index = qfl->qf_count;
2203 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205
Bram Moolenaar95946f12019-03-31 15:31:59 +02002206 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207}
2208
2209/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002210 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002212 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002213qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002214{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002215 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002216
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002217 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002218 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002219 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002220 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002221 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002222 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002223 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002224 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002225}
2226
2227/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002228 * Return the location list stack for window 'wp'.
2229 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230 */
2231 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002232ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233{
2234 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002235 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236 return wp->w_llist_ref;
2237
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002238 // For a non-location list window, w_llist_ref should not point to a
2239 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002240 ll_free_all(&wp->w_llist_ref);
2241
2242 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002243 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 return wp->w_llist;
2245}
2246
2247/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002248 * Get the quickfix/location list stack to use for the specified Ex command.
2249 * For a location list command, returns the stack for the current window. If
2250 * the location list is not found, then returns NULL and prints an error
2251 * message if 'print_emsg' is TRUE.
2252 */
2253 static qf_info_T *
2254qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2255{
2256 qf_info_T *qi = &ql_info;
2257
2258 if (is_loclist_cmd(eap->cmdidx))
2259 {
2260 qi = GET_LOC_LIST(curwin);
2261 if (qi == NULL)
2262 {
2263 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002264 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002265 return NULL;
2266 }
2267 }
2268
2269 return qi;
2270}
2271
2272/*
2273 * Get the quickfix/location list stack to use for the specified Ex command.
2274 * For a location list command, returns the stack for the current window.
2275 * If the location list is not present, then allocates a new one.
2276 * Returns NULL if the allocation fails. For a location list command, sets
2277 * 'pwinp' to curwin.
2278 */
2279 static qf_info_T *
2280qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2281{
2282 qf_info_T *qi = &ql_info;
2283
2284 if (is_loclist_cmd(eap->cmdidx))
2285 {
2286 qi = ll_get_or_alloc_list(curwin);
2287 if (qi == NULL)
2288 return NULL;
2289 *pwinp = curwin;
2290 }
2291
2292 return qi;
2293}
2294
2295/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002296 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2297 */
2298 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002299copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002300{
2301 int i;
2302 qfline_T *from_qfp;
2303 qfline_T *prevp;
2304
2305 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002306 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002307 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002308 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002309 NULL,
2310 NULL,
2311 from_qfp->qf_module,
2312 0,
2313 from_qfp->qf_text,
2314 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002315 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002316 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002317 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002318 from_qfp->qf_viscol,
2319 from_qfp->qf_pattern,
2320 from_qfp->qf_nr,
2321 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002322 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002323 return FAIL;
2324
2325 // qf_add_entry() will not set the qf_num field, as the
2326 // directory and file names are not supplied. So the qf_fnum
2327 // field is copied here.
2328 prevp = to_qfl->qf_last;
2329 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2330 prevp->qf_type = from_qfp->qf_type; // error type
2331 if (from_qfl->qf_ptr == from_qfp)
2332 to_qfl->qf_ptr = prevp; // current location
2333 }
2334
2335 return OK;
2336}
2337
2338/*
2339 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2340 */
2341 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002342copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002343{
2344 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002345 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002346 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2347 to_qfl->qf_count = 0;
2348 to_qfl->qf_index = 0;
2349 to_qfl->qf_start = NULL;
2350 to_qfl->qf_last = NULL;
2351 to_qfl->qf_ptr = NULL;
2352 if (from_qfl->qf_title != NULL)
2353 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2354 else
2355 to_qfl->qf_title = NULL;
2356 if (from_qfl->qf_ctx != NULL)
2357 {
2358 to_qfl->qf_ctx = alloc_tv();
2359 if (to_qfl->qf_ctx != NULL)
2360 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2361 }
2362 else
2363 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002364 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2365 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002366 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002367 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002368
2369 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002370 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002371 return FAIL;
2372
2373 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2374
2375 // Assign a new ID for the location list
2376 to_qfl->qf_id = ++last_qf_id;
2377 to_qfl->qf_changedtick = 0L;
2378
2379 // When no valid entries are present in the list, qf_ptr points to
2380 // the first item in the list
2381 if (to_qfl->qf_nonevalid)
2382 {
2383 to_qfl->qf_ptr = to_qfl->qf_start;
2384 to_qfl->qf_index = 1;
2385 }
2386
2387 return OK;
2388}
2389
2390/*
2391 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002392 */
2393 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002394copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002395{
2396 qf_info_T *qi;
2397 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002398
Bram Moolenaar09037502018-09-25 22:08:14 +02002399 // When copying from a location list window, copy the referenced
2400 // location list. For other windows, copy the location list for
2401 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 if (IS_LL_WINDOW(from))
2403 qi = from->w_llist_ref;
2404 else
2405 qi = from->w_llist;
2406
Bram Moolenaar09037502018-09-25 22:08:14 +02002407 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002408 return;
2409
Bram Moolenaar09037502018-09-25 22:08:14 +02002410 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002411 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002412 return;
2413
2414 to->w_llist->qf_listcount = qi->qf_listcount;
2415
Bram Moolenaar09037502018-09-25 22:08:14 +02002416 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002417 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002418 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002419 to->w_llist->qf_curlist = idx;
2420
Bram Moolenaar0398e002019-03-21 21:12:49 +01002421 if (copy_loclist(qf_get_list(qi, idx),
2422 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002423 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002424 qf_free_all(to);
2425 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002426 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002427 }
2428
Bram Moolenaar09037502018-09-25 22:08:14 +02002429 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002430}
2431
2432/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002433 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002434 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002437qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
Bram Moolenaar82404332016-07-10 17:00:38 +02002439 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002440 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002441 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002442
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002443 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
Bram Moolenaare60acc12011-05-10 16:41:25 +02002446#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002447 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002448#endif
2449#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002450 if (directory != NULL)
2451 slash_adjust(directory);
2452 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002453#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002454 if (directory != NULL && !vim_isAbsName(fname)
2455 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2456 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002457 // Here we check if the file really exists.
2458 // This should normally be true, but if make works without
2459 // "leaving directory"-messages we might have missed a
2460 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002461 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002464 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002465 if (directory)
2466 ptr = concat_fnames(directory, fname, TRUE);
2467 else
2468 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002470 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002471 bufname = ptr;
2472 }
2473 else
2474 bufname = fname;
2475
2476 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002477 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002478 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002479 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002480 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002482 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002483 {
2484 vim_free(qf_last_bufname);
2485 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2486 if (bufname == ptr)
2487 qf_last_bufname = bufname;
2488 else
2489 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002490 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002491 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002492 if (buf == NULL)
2493 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002494
Bram Moolenaarc1542742016-07-20 21:44:37 +02002495 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002496 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002497 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498}
2499
2500/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002501 * Push dirbuf onto the directory stack and return pointer to actual dir or
2502 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
2504 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002505qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
2507 struct dir_stack_T *ds_new;
2508 struct dir_stack_T *ds_ptr;
2509
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002510 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002511 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (ds_new == NULL)
2513 return NULL;
2514
2515 ds_new->next = *stackptr;
2516 *stackptr = ds_new;
2517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002518 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (vim_isAbsName(dirbuf)
2520 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002521 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 (*stackptr)->dirname = vim_strsave(dirbuf);
2523 else
2524 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002525 // Okay we don't have an absolute path.
2526 // dirbuf must be a subdir of one of the directories on the stack.
2527 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 ds_new = (*stackptr)->next;
2529 (*stackptr)->dirname = NULL;
2530 while (ds_new)
2531 {
2532 vim_free((*stackptr)->dirname);
2533 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2534 TRUE);
2535 if (mch_isdir((*stackptr)->dirname) == TRUE)
2536 break;
2537
2538 ds_new = ds_new->next;
2539 }
2540
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002541 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 while ((*stackptr)->next != ds_new)
2543 {
2544 ds_ptr = (*stackptr)->next;
2545 (*stackptr)->next = (*stackptr)->next->next;
2546 vim_free(ds_ptr->dirname);
2547 vim_free(ds_ptr);
2548 }
2549
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002550 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (ds_new == NULL)
2552 {
2553 vim_free((*stackptr)->dirname);
2554 (*stackptr)->dirname = vim_strsave(dirbuf);
2555 }
2556 }
2557
2558 if ((*stackptr)->dirname != NULL)
2559 return (*stackptr)->dirname;
2560 else
2561 {
2562 ds_ptr = *stackptr;
2563 *stackptr = (*stackptr)->next;
2564 vim_free(ds_ptr);
2565 return NULL;
2566 }
2567}
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569/*
2570 * pop dirbuf from the directory stack and return previous directory or NULL if
2571 * stack is empty
2572 */
2573 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002574qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 struct dir_stack_T *ds_ptr;
2577
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002578 // TODO: Should we check if dirbuf is the directory on top of the stack?
2579 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002581 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (*stackptr != NULL)
2583 {
2584 ds_ptr = *stackptr;
2585 *stackptr = (*stackptr)->next;
2586 vim_free(ds_ptr->dirname);
2587 vim_free(ds_ptr);
2588 }
2589
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002590 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return *stackptr ? (*stackptr)->dirname : NULL;
2592}
2593
2594/*
2595 * clean up directory stack
2596 */
2597 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002598qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 struct dir_stack_T *ds_ptr;
2601
2602 while ((ds_ptr = *stackptr) != NULL)
2603 {
2604 *stackptr = (*stackptr)->next;
2605 vim_free(ds_ptr->dirname);
2606 vim_free(ds_ptr);
2607 }
2608}
2609
2610/*
2611 * Check in which directory of the directory stack the given file can be
2612 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002613 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 * Cleans up intermediate directory entries.
2615 *
2616 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002617 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 * ./
2619 * ./aa
2620 * ./aa/bb
2621 * ./bb
2622 * ./bb/x.c
2623 * and make says:
2624 * making all in aa
2625 * making all in bb
2626 * x.c:9: Error
2627 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2628 * qf_guess_filepath will return NULL.
2629 */
2630 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002631qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 struct dir_stack_T *ds_ptr;
2634 struct dir_stack_T *ds_tmp;
2635 char_u *fullname;
2636
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002637 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002638 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return NULL;
2640
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002641 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 fullname = NULL;
2643 while (ds_ptr)
2644 {
2645 vim_free(fullname);
2646 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2647
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002648 // If concat_fnames failed, just go on. The worst thing that can happen
2649 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2651 break;
2652
2653 ds_ptr = ds_ptr->next;
2654 }
2655
2656 vim_free(fullname);
2657
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002658 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002659 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002661 ds_tmp = qfl->qf_dir_stack->next;
2662 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 vim_free(ds_tmp->dirname);
2664 vim_free(ds_tmp);
2665 }
2666
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002667 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668}
2669
2670/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002671 * Returns TRUE if a quickfix/location list with the given identifier exists.
2672 */
2673 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002674qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002675{
2676 qf_info_T *qi = &ql_info;
2677 int i;
2678
2679 if (wp != NULL)
2680 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002681 if (!win_valid(wp))
2682 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002683 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002684 if (qi == NULL)
2685 return FALSE;
2686 }
2687
2688 for (i = 0; i < qi->qf_listcount; ++i)
2689 if (qi->qf_lists[i].qf_id == qf_id)
2690 return TRUE;
2691
2692 return FALSE;
2693}
2694
2695/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002696 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002697 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002698 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002699 * Similar to location list.
2700 */
2701 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002702is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002703{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002704 qfline_T *qfp;
2705 int i;
2706
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002707 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002708 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2709 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002710 break;
2711
Bram Moolenaar95946f12019-03-31 15:31:59 +02002712 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002713 return FALSE;
2714
2715 return TRUE;
2716}
2717
2718/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002719 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002720 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002721 */
2722 static qfline_T *
2723get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002724 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002725 qfline_T *qf_ptr,
2726 int *qf_index,
2727 int dir)
2728{
2729 int idx;
2730 int old_qf_fnum;
2731
2732 idx = *qf_index;
2733 old_qf_fnum = qf_ptr->qf_fnum;
2734
2735 do
2736 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002737 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002738 return NULL;
2739 ++idx;
2740 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002741 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002742 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2743
2744 *qf_index = idx;
2745 return qf_ptr;
2746}
2747
2748/*
2749 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002750 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002751 */
2752 static qfline_T *
2753get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002754 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002755 qfline_T *qf_ptr,
2756 int *qf_index,
2757 int dir)
2758{
2759 int idx;
2760 int old_qf_fnum;
2761
2762 idx = *qf_index;
2763 old_qf_fnum = qf_ptr->qf_fnum;
2764
2765 do
2766 {
2767 if (idx == 1 || qf_ptr->qf_prev == NULL)
2768 return NULL;
2769 --idx;
2770 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002771 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002772 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2773
2774 *qf_index = idx;
2775 return qf_ptr;
2776}
2777
2778/*
2779 * Get the n'th (errornr) previous/next valid entry from the current entry in
2780 * the quickfix list.
2781 * dir == FORWARD or FORWARD_FILE: next valid entry
2782 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2783 */
2784 static qfline_T *
2785get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002786 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002787 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002788 int dir,
2789 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002790{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002791 qfline_T *qf_ptr = qfl->qf_ptr;
2792 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002793 qfline_T *prev_qf_ptr;
2794 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002795 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002796
2797 while (errornr--)
2798 {
2799 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002800 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002801
2802 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002803 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002804 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002805 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002806 if (qf_ptr == NULL)
2807 {
2808 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002809 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002810 if (err != NULL)
2811 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002812 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002813 return NULL;
2814 }
2815 break;
2816 }
2817
2818 err = NULL;
2819 }
2820
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002821 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002822 return qf_ptr;
2823}
2824
2825/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002826 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2827 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002828 */
2829 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002830get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002831{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002832 qfline_T *qf_ptr = qfl->qf_ptr;
2833 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002834
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002835 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002836 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2837 {
2838 --qf_idx;
2839 qf_ptr = qf_ptr->qf_prev;
2840 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002841 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002842 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2843 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002844 {
2845 ++qf_idx;
2846 qf_ptr = qf_ptr->qf_next;
2847 }
2848
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002849 *new_qfidx = qf_idx;
2850 return qf_ptr;
2851}
2852
2853/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002854 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002855 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2856 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2857 * Returns a pointer to the entry and the index of the new entry is stored in
2858 * 'new_qfidx'.
2859 */
2860 static qfline_T *
2861qf_get_entry(
2862 qf_list_T *qfl,
2863 int errornr,
2864 int dir,
2865 int *new_qfidx)
2866{
2867 qfline_T *qf_ptr = qfl->qf_ptr;
2868 int qfidx = qfl->qf_index;
2869
2870 if (dir != 0) // next/prev valid entry
2871 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2872 else if (errornr != 0) // go to specified number
2873 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2874
2875 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002876 return qf_ptr;
2877}
2878
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002879/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002880 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002881 */
2882 static win_T *
2883qf_find_help_win(void)
2884{
2885 win_T *wp;
2886
2887 FOR_ALL_WINDOWS(wp)
2888 if (bt_help(wp->w_buffer))
2889 return wp;
2890
2891 return NULL;
2892}
2893
2894/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002895 * Set the location list for the specified window to 'qi'.
2896 */
2897 static void
2898win_set_loclist(win_T *wp, qf_info_T *qi)
2899{
2900 wp->w_llist = qi;
2901 qi->qf_refcount++;
2902}
2903
2904/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002905 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2906 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002907 */
2908 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002909jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002910{
2911 win_T *wp;
2912 int flags;
2913
Bram Moolenaare1004402020-10-24 20:49:43 +02002914 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002915 wp = NULL;
2916 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002917 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002918 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2919 win_enter(wp, TRUE);
2920 else
2921 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002922 // Split off help window; put it at far top if no position
2923 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002924 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002925 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002926 && curwin->w_width < 80)
2927 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002928 // If the user asks to open a new window, then copy the location list.
2929 // Otherwise, don't copy the location list.
2930 if (IS_LL_STACK(qi) && !newwin)
2931 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002932
2933 if (win_split(0, flags) == FAIL)
2934 return FAIL;
2935
2936 *opened_window = TRUE;
2937
2938 if (curwin->w_height < p_hh)
2939 win_setheight((int)p_hh);
2940
Bram Moolenaarb2443732018-11-11 22:50:27 +01002941 // When using location list, the new window should use the supplied
2942 // location list. If the user asks to open a new window, then the new
2943 // window will get a copy of the location list.
2944 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002945 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002946 }
2947
2948 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002949 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002950
2951 return OK;
2952}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002953
2954/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002955 * Find a non-quickfix window using the given location list stack in the
2956 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 * Returns NULL if a matching window is not found.
2958 */
2959 static win_T *
2960qf_find_win_with_loclist(qf_info_T *ll)
2961{
2962 win_T *wp;
2963
2964 FOR_ALL_WINDOWS(wp)
2965 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2966 return wp;
2967
2968 return NULL;
2969}
2970
2971/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002972 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 */
2974 static win_T *
2975qf_find_win_with_normal_buf(void)
2976{
2977 win_T *wp;
2978
2979 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002980 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002981 return wp;
2982
2983 return NULL;
2984}
2985
2986/*
2987 * Go to a window in any tabpage containing the specified file. Returns TRUE
2988 * if successfully jumped to the window. Otherwise returns FALSE.
2989 */
2990 static int
2991qf_goto_tabwin_with_file(int fnum)
2992{
2993 tabpage_T *tp;
2994 win_T *wp;
2995
2996 FOR_ALL_TAB_WINDOWS(tp, wp)
2997 if (wp->w_buffer->b_fnum == fnum)
2998 {
2999 goto_tabpage_win(tp, wp);
3000 return TRUE;
3001 }
3002
3003 return FALSE;
3004}
3005
3006/*
3007 * Create a new window to show a file above the quickfix window. Called when
3008 * only the quickfix window is present.
3009 */
3010 static int
3011qf_open_new_file_win(qf_info_T *ll_ref)
3012{
3013 int flags;
3014
3015 flags = WSP_ABOVE;
3016 if (ll_ref != NULL)
3017 flags |= WSP_NEWLOC;
3018 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003019 return FAIL; // not enough room for window
3020 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003021 swb_flags = 0;
3022 RESET_BINDING(curwin);
3023 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003024 // The new window should use the location list from the
3025 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003026 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003027 return OK;
3028}
3029
3030/*
3031 * Go to a window that shows the right buffer. If the window is not found, go
3032 * to the window just above the location list window. This is used for opening
3033 * a file from a location window and not from a quickfix window. If some usable
3034 * window is previously found, then it is supplied in 'use_win'.
3035 */
3036 static void
3037qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3038{
3039 win_T *win = use_win;
3040
3041 if (win == NULL)
3042 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003043 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003044 FOR_ALL_WINDOWS(win)
3045 if (win->w_buffer->b_fnum == qf_fnum)
3046 break;
3047 if (win == NULL)
3048 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003049 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003050 win = curwin;
3051 do
3052 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003053 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003054 break;
3055 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003056 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003057 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003058 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003059 } while (win != curwin);
3060 }
3061 }
3062 win_goto(win);
3063
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003064 // If the location list for the window is not set, then set it
3065 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003066 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003067 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003068}
3069
3070/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003071 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3072 * not found, then go to the window just above the quickfix window. This is
3073 * used for opening a file from a quickfix window and not from a location
3074 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003075 */
3076 static void
3077qf_goto_win_with_qfl_file(int qf_fnum)
3078{
3079 win_T *win;
3080 win_T *altwin;
3081
3082 win = curwin;
3083 altwin = NULL;
3084 for (;;)
3085 {
3086 if (win->w_buffer->b_fnum == qf_fnum)
3087 break;
3088 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003089 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003090 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003091 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003092
3093 if (IS_QF_WINDOW(win))
3094 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003095 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003096 // window, unless 'switchbuf' contains 'uselast': in this case we
3097 // try to jump to the previously used window first.
3098 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3099 win = prevwin;
3100 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003101 win = altwin;
3102 else if (curwin->w_prev != NULL)
3103 win = curwin->w_prev;
3104 else
3105 win = curwin->w_next;
3106 break;
3107 }
3108
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003109 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003110 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003111 altwin = win;
3112 }
3113
3114 win_goto(win);
3115}
3116
3117/*
3118 * Find a suitable window for opening a file (qf_fnum) from the
3119 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003120 * window, jump to it. Otherwise open a new window to display the file. If
3121 * 'newwin' is TRUE, then always open a new window. This is called from either
3122 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003123 */
3124 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003125qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003126{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003127 win_T *usable_wp = NULL;
3128 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003129 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003130
Bram Moolenaarb2443732018-11-11 22:50:27 +01003131 // If opening a new window, then don't use the location list referred by
3132 // the current window. Otherwise two windows will refer to the same
3133 // location list.
3134 if (!newwin)
3135 ll_ref = curwin->w_llist_ref;
3136
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003137 if (ll_ref != NULL)
3138 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003139 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003140 usable_wp = qf_find_win_with_loclist(ll_ref);
3141 if (usable_wp != NULL)
3142 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003143 }
3144
3145 if (!usable_win)
3146 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003147 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003148 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003149 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003150 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003151 }
3152
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003153 // If no usable window is found and 'switchbuf' contains "usetab"
3154 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003155 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003156 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003157
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003158 // If there is only one window and it is the quickfix window, create a
3159 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003160 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003161 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003162 if (qf_open_new_file_win(ll_ref) != OK)
3163 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003164 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003165 }
3166 else
3167 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003168 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003169 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003170 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003171 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003172 }
3173
3174 return OK;
3175}
3176
3177/*
3178 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003179 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003180 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003181 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 */
3183 static int
3184qf_jump_edit_buffer(
3185 qf_info_T *qi,
3186 qfline_T *qf_ptr,
3187 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003188 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003189 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003190{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003191 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003192 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003193 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003194 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003195 int old_qf_curlist = qi->qf_curlist;
3196 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003197
3198 if (qf_ptr->qf_type == 1)
3199 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003200 // Open help file (do_ecmd() will set b_help flag, readfile() will
3201 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003202 if (!can_abandon(curbuf, forceit))
3203 {
3204 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003205 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003206 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003207
3208 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3209 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003210 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003211 }
3212 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003213 retval = buflist_getfile(qf_ptr->qf_fnum,
3214 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003215
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003216 // If a location list, check whether the associated window is still
3217 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003218 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003219 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003220 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003221
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003222 if (wp == NULL && curwin->w_llist != qi)
3223 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003224 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003225 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003226 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003227 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003228 }
3229
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003230 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003231 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003232 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003233 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003234 }
3235
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003236 // Check if the list was changed. The pointers may happen to be identical,
3237 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003238 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003239 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003240 || !is_qf_entry_present(qfl, qf_ptr))
3241 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003242 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003243 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003244 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003245 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003246 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003247 }
3248
3249 return retval;
3250}
3251
3252/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003253 * Go to the error line in the current file using either line/column number or
3254 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003255 */
3256 static void
3257qf_jump_goto_line(
3258 linenr_T qf_lnum,
3259 int qf_col,
3260 char_u qf_viscol,
3261 char_u *qf_pattern)
3262{
3263 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003264
3265 if (qf_pattern == NULL)
3266 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003267 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003268 i = qf_lnum;
3269 if (i > 0)
3270 {
3271 if (i > curbuf->b_ml.ml_line_count)
3272 i = curbuf->b_ml.ml_line_count;
3273 curwin->w_cursor.lnum = i;
3274 }
3275 if (qf_col > 0)
3276 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003277 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003278 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003279 coladvance(qf_col - 1);
3280 else
3281 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003282 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003283 check_cursor();
3284 }
3285 else
3286 beginline(BL_WHITE | BL_FIX);
3287 }
3288 else
3289 {
3290 pos_T save_cursor;
3291
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003292 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003293 save_cursor = curwin->w_cursor;
3294 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003295 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003296 curwin->w_cursor = save_cursor;
3297 }
3298}
3299
3300/*
3301 * Display quickfix list index and size message
3302 */
3303 static void
3304qf_jump_print_msg(
3305 qf_info_T *qi,
3306 int qf_index,
3307 qfline_T *qf_ptr,
3308 buf_T *old_curbuf,
3309 linenr_T old_lnum)
3310{
3311 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003312 garray_T *gap;
3313
3314 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003315
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003316 // Update the screen before showing the message, unless the screen
3317 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003318 if (!msg_scrolled)
3319 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003320 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003321 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003322 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3323 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003324 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003325 ga_concat(gap, IObuff);
3326 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003327
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003328 // Output the message. Overwrite to avoid scrolling when the 'O'
3329 // flag is present in 'shortmess'; But when not jumping, print the
3330 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003331 i = msg_scroll;
3332 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3333 msg_scroll = TRUE;
3334 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3335 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003336 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003337 msg_scroll = i;
3338}
3339
3340/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003341 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003342 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3343 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003344 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3345 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003346 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3347 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003348 */
3349 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003350qf_jump_open_window(
3351 qf_info_T *qi,
3352 qfline_T *qf_ptr,
3353 int newwin,
3354 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003355{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003356 qf_list_T *qfl = qf_get_curlist(qi);
3357 int old_changedtick = qfl->qf_changedtick;
3358 int old_qf_curlist = qi->qf_curlist;
3359 qfltype_T qfl_type = qfl->qfl_type;
3360
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003361 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003362 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3363 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003364 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003365 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003366 if (old_qf_curlist != qi->qf_curlist
3367 || old_changedtick != qfl->qf_changedtick
3368 || !is_qf_entry_present(qfl, qf_ptr))
3369 {
3370 if (qfl_type == QFLT_QUICKFIX)
3371 emsg(_(e_current_quickfix_list_was_changed));
3372 else
3373 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003374 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003375 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003376
3377 // If currently in the quickfix window, find another window to show the
3378 // file in.
3379 if (bt_quickfix(curbuf) && !*opened_window)
3380 {
3381 // If there is no file specified, we don't know where to go.
3382 // But do advance, otherwise ":cn" gets stuck.
3383 if (qf_ptr->qf_fnum == 0)
3384 return NOTDONE;
3385
Bram Moolenaarb2443732018-11-11 22:50:27 +01003386 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3387 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003388 return FAIL;
3389 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003390 if (old_qf_curlist != qi->qf_curlist
3391 || old_changedtick != qfl->qf_changedtick
3392 || !is_qf_entry_present(qfl, qf_ptr))
3393 {
3394 if (qfl_type == QFLT_QUICKFIX)
3395 emsg(_(e_current_quickfix_list_was_changed));
3396 else
3397 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003398 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003399 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003400
3401 return OK;
3402}
3403
3404/*
3405 * Edit a selected file from the quickfix/location list and jump to a
3406 * particular line/column, adjust the folds and display a message about the
3407 * jump.
3408 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003409 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003410 * the file.
3411 */
3412 static int
3413qf_jump_to_buffer(
3414 qf_info_T *qi,
3415 int qf_index,
3416 qfline_T *qf_ptr,
3417 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003418 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003419 int *opened_window,
3420 int openfold,
3421 int print_message)
3422{
3423 buf_T *old_curbuf;
3424 linenr_T old_lnum;
3425 int retval = OK;
3426
3427 // If there is a file name, read the wanted file if needed, and check
3428 // autowrite etc.
3429 old_curbuf = curbuf;
3430 old_lnum = curwin->w_cursor.lnum;
3431
3432 if (qf_ptr->qf_fnum != 0)
3433 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003434 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003435 opened_window);
3436 if (retval != OK)
3437 return retval;
3438 }
3439
3440 // When not switched to another buffer, still need to set pc mark
3441 if (curbuf == old_curbuf)
3442 setpcmark();
3443
3444 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3445 qf_ptr->qf_pattern);
3446
3447#ifdef FEAT_FOLDING
3448 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3449 foldOpenCursor();
3450#endif
3451 if (print_message)
3452 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3453
3454 return retval;
3455}
3456
3457/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003458 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
3460 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003461qf_jump(qf_info_T *qi,
3462 int dir,
3463 int errornr,
3464 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003466 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3467}
3468
3469/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003470 * Jump to a quickfix line.
3471 * If dir == 0 go to entry "errornr".
3472 * If dir == FORWARD go "errornr" valid entries forward.
3473 * If dir == BACKWARD go "errornr" valid entries backward.
3474 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3475 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3476 * else if "errornr" is zero, redisplay the same line
3477 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003478 * If 'newwin' is TRUE, then open the file in a new window.
3479 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003480 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003481qf_jump_newwin(qf_info_T *qi,
3482 int dir,
3483 int errornr,
3484 int forceit,
3485 int newwin)
3486{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003487 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003488 qfline_T *qf_ptr;
3489 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003492 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003493 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003494 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003497 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003498 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003500 if (qi == NULL)
3501 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003502
Bram Moolenaar0398e002019-03-21 21:12:49 +01003503 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003505 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 return;
3507 }
3508
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003509 incr_quickfix_busy();
3510
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003511 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003512
3513 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003515 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003517
3518 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3519 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003521 qf_ptr = old_qf_ptr;
3522 qf_index = old_qf_index;
3523 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003526 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003527 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003528 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003529 // No need to print the error message if it's visible in the error
3530 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 print_message = FALSE;
3532
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003533 prev_winid = curwin->w_id;
3534
Bram Moolenaarb2443732018-11-11 22:50:27 +01003535 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003536 if (retval == FAIL)
3537 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003538 if (retval == QF_ABORT)
3539 {
3540 qi = NULL;
3541 qf_ptr = NULL;
3542 goto theend;
3543 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003544 if (retval == NOTDONE)
3545 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003546
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003547 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003548 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003549 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003551 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003552 qi = NULL;
3553 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003556 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003559 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003560 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003562 // Couldn't open file, so put index back where it was. This could
3563 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 qf_ptr = old_qf_ptr;
3566 qf_index = old_qf_index;
3567 }
3568 }
3569theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003570 if (qi != NULL)
3571 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003572 qfl->qf_ptr = qf_ptr;
3573 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003574 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003575 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003577 // Restore old 'switchbuf' value, but not when an autocommand or
3578 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003579 p_swb = old_swb;
3580 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003582 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583}
3584
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003585// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003586static int qfFileAttr;
3587static int qfSepAttr;
3588static int qfLineAttr;
3589
3590/*
3591 * Display information about a single entry from the quickfix/location list.
3592 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003593 * 'cursel' will be set to TRUE for the currently selected entry in the
3594 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003595 */
3596 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003597qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003598{
3599 char_u *fname;
3600 buf_T *buf;
3601 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003602 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003603
3604 fname = NULL;
3605 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3606 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3607 (char *)qfp->qf_module);
3608 else {
3609 if (qfp->qf_fnum != 0
3610 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3611 {
3612 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003613 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003614 fname = gettail(fname);
3615 }
3616 if (fname == NULL)
3617 sprintf((char *)IObuff, "%2d", qf_idx);
3618 else
3619 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3620 qf_idx, (char *)fname);
3621 }
3622
3623 // Support for filtering entries using :filter /pat/ clist
3624 // Match against the module name, file name, search pattern and
3625 // text of the entry.
3626 filter_entry = TRUE;
3627 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3628 filter_entry &= message_filtered(qfp->qf_module);
3629 if (filter_entry && fname != NULL)
3630 filter_entry &= message_filtered(fname);
3631 if (filter_entry && qfp->qf_pattern != NULL)
3632 filter_entry &= message_filtered(qfp->qf_pattern);
3633 if (filter_entry)
3634 filter_entry &= message_filtered(qfp->qf_text);
3635 if (filter_entry)
3636 return;
3637
3638 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003639 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003640
3641 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003642 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003643 gap = qfga_get();
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003644 if (qfp->qf_lnum == 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003645 ga_append(gap, NUL);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003646 else
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003647 qf_range_text(gap, qfp);
3648 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
3649 ga_append(gap, NUL);
3650 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003651 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003652 if (qfp->qf_pattern != NULL)
3653 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003654 gap = qfga_get();
3655 qf_fmt_text(gap, qfp->qf_pattern);
3656 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003657 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003658 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003659 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003660
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003661 // Remove newlines and leading whitespace from the text. For an
3662 // unrecognized line keep the indent, the compiler may mark a word
3663 // with ^^^^.
3664 gap = qfga_get();
3665 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
3666 ? skipwhite(qfp->qf_text) : qfp->qf_text);
3667 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003668 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003669}
3670
3671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003673 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003676qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003678 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003679 qfline_T *qfp;
3680 int i;
3681 int idx1 = 1;
3682 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003683 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003684 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003685 int all = eap->forceit; // if not :cl!, only show
3686 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003687 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003689 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3690 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003691
Bram Moolenaar0398e002019-03-21 21:12:49 +01003692 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003694 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 return;
3696 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003697 if (*arg == '+')
3698 {
3699 ++arg;
3700 plus = TRUE;
3701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3703 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003704 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 return;
3706 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003707 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003708 if (plus)
3709 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003710 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003711 idx2 = i + idx1;
3712 idx1 = i;
3713 }
3714 else
3715 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003716 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003717 if (idx1 < 0)
3718 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3719 if (idx2 < 0)
3720 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003723 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003724 shorten_fnames(FALSE);
3725
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003726 // Get the attributes for the different quickfix highlight items. Note
3727 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003728 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3729 if (qfFileAttr == 0)
3730 qfFileAttr = HL_ATTR(HLF_D);
3731 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3732 if (qfSepAttr == 0)
3733 qfSepAttr = HL_ATTR(HLF_D);
3734 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3735 if (qfLineAttr == 0)
3736 qfLineAttr = HL_ATTR(HLF_N);
3737
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003738 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003740 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003743 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 ui_breakcheck();
3746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747}
3748
3749/*
3750 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003751 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 */
3753 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003754qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 char_u *p = text;
3757
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003758 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
3760 if (*p == '\n')
3761 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003762 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003764 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 break;
3766 }
3767 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003768 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003770
3771 ga_append(gap, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772}
3773
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003774/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003775 * Add the range information from the lnum, col, end_lnum, and end_col values
3776 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02003777 */
3778 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003779qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02003780{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003781 char_u *buf = IObuff;
3782 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02003783 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003784
thinca6864efa2021-06-19 20:45:20 +02003785 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3786 len = (int)STRLEN(buf);
3787
3788 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3789 {
3790 vim_snprintf((char *)buf + len, bufsize - len,
3791 "-%ld", qfp->qf_end_lnum);
3792 len += (int)STRLEN(buf + len);
3793 }
3794 if (qfp->qf_col > 0)
3795 {
3796 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3797 len += (int)STRLEN(buf + len);
3798 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3799 {
3800 vim_snprintf((char *)buf + len, bufsize - len,
3801 "-%d", qfp->qf_end_col);
3802 len += (int)STRLEN(buf + len);
3803 }
3804 }
3805 buf[len] = NUL;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003806
3807 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02003808}
3809
3810/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003811 * Display information (list number, list size and the title) about a
3812 * quickfix/location list.
3813 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003814 static void
3815qf_msg(qf_info_T *qi, int which, char *lead)
3816{
3817 char *title = (char *)qi->qf_lists[which].qf_title;
3818 int count = qi->qf_lists[which].qf_count;
3819 char_u buf[IOSIZE];
3820
3821 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3822 lead,
3823 which + 1,
3824 qi->qf_listcount,
3825 count);
3826
3827 if (title != NULL)
3828 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003829 size_t len = STRLEN(buf);
3830
3831 if (len < 34)
3832 {
3833 vim_memset(buf + len, ' ', 34 - len);
3834 buf[34] = NUL;
3835 }
3836 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003837 }
3838 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003839 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003840}
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842/*
3843 * ":colder [count]": Up in the quickfix stack.
3844 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003845 * ":lolder [count]": Up in the location list stack.
3846 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
3848 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003849qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003851 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 int count;
3853
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003854 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3855 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (eap->addr_count != 0)
3858 count = eap->line2;
3859 else
3860 count = 1;
3861 while (count--)
3862 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003863 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003865 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003867 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003868 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003870 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
3872 else
3873 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003874 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003876 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003877 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003879 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003882 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003883 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884}
3885
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003886/*
3887 * Display the information about all the quickfix/location lists in the stack
3888 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003889 void
3890qf_history(exarg_T *eap)
3891{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003892 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003893 int i;
3894
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003895 if (eap->addr_count > 0)
3896 {
3897 if (qi == NULL)
3898 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003899 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003900 return;
3901 }
3902
3903 // Jump to the specified quickfix list
3904 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3905 {
3906 qi->qf_curlist = eap->line2 - 1;
3907 qf_msg(qi, qi->qf_curlist, "");
3908 qf_update_buffer(qi, NULL);
3909 }
3910 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003911 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003912
3913 return;
3914 }
3915
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003916 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003917 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003918 else
3919 for (i = 0; i < qi->qf_listcount; ++i)
3920 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3921}
3922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003924 * Free all the entries in the error list "idx". Note that other information
3925 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 */
3927 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003928qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003930 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003931 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003932 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003934 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003936 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003937 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003938 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003939 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003940 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003941 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003942 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003943 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003944 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003945 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003946 // Somehow qf_count may have an incorrect value, set it to 1
3947 // to avoid crashing when it's wrong.
3948 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003949 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003950 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003951 qfl->qf_start = qfpnext;
3952 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003954
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003955 qfl->qf_index = 0;
3956 qfl->qf_start = NULL;
3957 qfl->qf_last = NULL;
3958 qfl->qf_ptr = NULL;
3959 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003960
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003961 qf_clean_dir_stack(&qfl->qf_dir_stack);
3962 qfl->qf_directory = NULL;
3963 qf_clean_dir_stack(&qfl->qf_file_stack);
3964 qfl->qf_currfile = NULL;
3965 qfl->qf_multiline = FALSE;
3966 qfl->qf_multiignore = FALSE;
3967 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968}
3969
3970/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003971 * Free error list "idx". Frees all the entries in the quickfix list,
3972 * associated context information and the title.
3973 */
3974 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003975qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003976{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003977 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003978
Bram Moolenaard23a8232018-02-10 18:45:26 +01003979 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003980 free_tv(qfl->qf_ctx);
3981 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01003982 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003983 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003984 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003985}
3986
3987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * qf_mark_adjust: adjust marks
3989 */
3990 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003991qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003992 win_T *wp,
3993 linenr_T line1,
3994 linenr_T line2,
3995 long amount,
3996 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003998 int i;
3999 qfline_T *qfp;
4000 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004001 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004002 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004003 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaarc1542742016-07-20 21:44:37 +02004005 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004006 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004007 if (wp != NULL)
4008 {
4009 if (wp->w_llist == NULL)
4010 return;
4011 qi = wp->w_llist;
4012 }
4013
4014 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004015 {
4016 qf_list_T *qfl = qf_get_list(qi, idx);
4017
4018 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004019 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (qfp->qf_fnum == curbuf->b_fnum)
4021 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004022 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4024 {
4025 if (amount == MAXLNUM)
4026 qfp->qf_cleared = TRUE;
4027 else
4028 qfp->qf_lnum += amount;
4029 }
4030 else if (amount_after && qfp->qf_lnum > line2)
4031 qfp->qf_lnum += amount_after;
4032 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004033 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004034
4035 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004036 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037}
4038
4039/*
4040 * Make a nice message out of the error character and the error number:
4041 * char number message
4042 * e or E 0 " error"
4043 * w or W 0 " warning"
4044 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004045 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 * 0 0 ""
4047 * other 0 " c"
4048 * e or E n " error n"
4049 * w or W n " warning n"
4050 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004051 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 * 0 n " error n"
4053 * other n " c n"
4054 * 1 x "" :helpgrep
4055 */
4056 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004057qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
4059 static char_u buf[20];
4060 static char_u cc[3];
4061 char_u *p;
4062
4063 if (c == 'W' || c == 'w')
4064 p = (char_u *)" warning";
4065 else if (c == 'I' || c == 'i')
4066 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004067 else if (c == 'N' || c == 'n')
4068 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4070 p = (char_u *)" error";
4071 else if (c == 0 || c == 1)
4072 p = (char_u *)"";
4073 else
4074 {
4075 cc[0] = ' ';
4076 cc[1] = c;
4077 cc[2] = NUL;
4078 p = cc;
4079 }
4080
4081 if (nr <= 0)
4082 return p;
4083
4084 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4085 return buf;
4086}
4087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004089 * When "split" is FALSE: Open the entry/result under the cursor.
4090 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4091 */
4092 void
4093qf_view_result(int split)
4094{
4095 qf_info_T *qi = &ql_info;
4096
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004097 if (IS_LL_WINDOW(curwin))
4098 qi = GET_LOC_LIST(curwin);
4099
Bram Moolenaar0398e002019-03-21 21:12:49 +01004100 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004101 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004102 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004103 return;
4104 }
4105
4106 if (split)
4107 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004108 // Open the selected entry in a new window
4109 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4110 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004111 return;
4112 }
4113
4114 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4115}
4116
4117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * ":cwindow": open the quickfix window if we have errors to display,
4119 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004120 * ":lwindow": open the location list window if we have locations to display,
4121 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 */
4123 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004124ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004126 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004127 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 win_T *win;
4129
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004130 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4131 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004132
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004133 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004134
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004135 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004136 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004138 // If a quickfix window is open but we have no errors to display,
4139 // close the window. If a quickfix window is not open, then open
4140 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004141 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004142 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004143 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 {
4145 if (win != NULL)
4146 ex_cclose(eap);
4147 }
4148 else if (win == NULL)
4149 ex_copen(eap);
4150}
4151
4152/*
4153 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004154 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004157ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004159 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004160 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004162 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4163 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004165 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004166 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 if (win != NULL)
4168 win_close(win, FALSE);
4169}
4170
4171/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004172 * Set "w:quickfix_title" if "qi" has a title.
4173 */
4174 static void
4175qf_set_title_var(qf_list_T *qfl)
4176{
4177 if (qfl->qf_title != NULL)
4178 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4179}
4180
4181/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004182 * Goto a quickfix or location list window (if present).
4183 * Returns OK if the window is found, FAIL otherwise.
4184 */
4185 static int
4186qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4187{
4188 win_T *win;
4189
4190 win = qf_find_win(qi);
4191 if (win == NULL)
4192 return FAIL;
4193
4194 win_goto(win);
4195 if (resize)
4196 {
4197 if (vertsplit)
4198 {
4199 if (sz != win->w_width)
4200 win_setwidth(sz);
4201 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004202 else if (sz != win->w_height && win->w_height
4203 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004204 win_setheight(sz);
4205 }
4206
4207 return OK;
4208}
4209
4210/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004211 * Set options for the buffer in the quickfix or location list window.
4212 */
4213 static void
4214qf_set_cwindow_options(void)
4215{
4216 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004217 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4218 set_option_value_give_err((char_u *)"bt",
4219 0L, (char_u *)"quickfix", OPT_LOCAL);
4220 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004221 RESET_BINDING(curwin);
4222#ifdef FEAT_DIFF
4223 curwin->w_p_diff = FALSE;
4224#endif
4225#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004226 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004227 OPT_LOCAL);
4228#endif
4229}
4230
4231/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004232 * Open a new quickfix or location list window, load the quickfix buffer and
4233 * set the appropriate options for the window.
4234 * Returns FAIL if the window could not be opened.
4235 */
4236 static int
4237qf_open_new_cwindow(qf_info_T *qi, int height)
4238{
4239 buf_T *qf_buf;
4240 win_T *oldwin = curwin;
4241 tabpage_T *prevtab = curtab;
4242 int flags = 0;
4243 win_T *win;
4244
4245 qf_buf = qf_find_buf(qi);
4246
4247 // The current window becomes the previous window afterwards.
4248 win = curwin;
4249
Bram Moolenaare1004402020-10-24 20:49:43 +02004250 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004251 // Create the new quickfix window at the very bottom, except when
4252 // :belowright or :aboveleft is used.
4253 win_goto(lastwin);
4254 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004255 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004256 flags = WSP_BELOW;
4257 flags |= WSP_NEWLOC;
4258 if (win_split(height, flags) == FAIL)
4259 return FAIL; // not enough room for window
4260 RESET_BINDING(curwin);
4261
4262 if (IS_LL_STACK(qi))
4263 {
4264 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004265 // location list stack from the window 'win'.
4266 curwin->w_llist_ref = qi;
4267 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004268 }
4269
4270 if (oldwin != curwin)
4271 oldwin = NULL; // don't store info when in another window
4272 if (qf_buf != NULL)
4273 {
4274 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004275 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004276 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004277 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004278 }
4279 else
4280 {
4281 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004282 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4283 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004284 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004285
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004286 // save the number of the new buffer
4287 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004288 }
4289
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004290 // Set the options for the quickfix buffer/window (if not already done)
4291 // Do this even if the quickfix buffer was already present, as an autocmd
4292 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004293 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004294 qf_set_cwindow_options();
4295
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004296 // Only set the height when still in the same tab page and there is no
4297 // window to the side.
4298 if (curtab == prevtab && curwin->w_width == Columns)
4299 win_setheight(height);
4300 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4301 if (win_valid(win))
4302 prevwin = win;
4303
4304 return OK;
4305}
4306
4307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004309 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 */
4311 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004312ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004314 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004315 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004317 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004318 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004320 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4321 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004322
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004323 incr_quickfix_busy();
4324
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (eap->addr_count != 0)
4326 height = eap->line2;
4327 else
4328 height = QF_WINHEIGHT;
4329
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004330 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331#ifdef FEAT_GUI
4332 need_mouse_correct = TRUE;
4333#endif
4334
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004335 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004336 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004337 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004338 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004339 if (status == FAIL)
4340 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004341 {
4342 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004343 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004346 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004347 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004348 // Save the current index here, as updating the quickfix buffer may free
4349 // the quickfix list
4350 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004351
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004352 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004353 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004355 decr_quickfix_busy();
4356
4357 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 curwin->w_cursor.col = 0;
4359 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004360 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361}
4362
4363/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004364 * Move the cursor in the quickfix window to "lnum".
4365 */
4366 static void
4367qf_win_goto(win_T *win, linenr_T lnum)
4368{
4369 win_T *old_curwin = curwin;
4370
4371 curwin = win;
4372 curbuf = win->w_buffer;
4373 curwin->w_cursor.lnum = lnum;
4374 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004375 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004376 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004377 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004378 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004379 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004380 curwin = old_curwin;
4381 curbuf = curwin->w_buffer;
4382}
4383
4384/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004385 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004386 */
4387 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004388ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004389{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004390 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004391 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004392
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004393 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4394 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004395
4396 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004397 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4398 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4399}
4400
4401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 * Return the number of the current entry (line number in the quickfix
4403 * window).
4404 */
4405 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004406qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004408 qf_info_T *qi = &ql_info;
4409
4410 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004411 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004412 qi = wp->w_llist_ref;
4413
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004414 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415}
4416
4417/*
4418 * Update the cursor position in the quickfix window to the current error.
4419 * Return TRUE if there is a quickfix window.
4420 */
4421 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004422qf_win_pos_update(
4423 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004424 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
4426 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004427 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004429 // Put the cursor on the current error in the quickfix window, so that
4430 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004431 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 if (win != NULL
4433 && qf_index <= win->w_buffer->b_ml.ml_line_count
4434 && old_qf_index != qf_index)
4435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 if (qf_index > old_qf_index)
4437 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004438 win->w_redraw_top = old_qf_index;
4439 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 else
4442 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004443 win->w_redraw_top = qf_index;
4444 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004446 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 return win != NULL;
4449}
4450
4451/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004452 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004453 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004454 */
4455 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004456is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004457{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004458 // A window displaying the quickfix buffer will have the w_llist_ref field
4459 // set to NULL.
4460 // A window displaying a location list buffer will have the w_llist_ref
4461 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004462 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004463 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4464 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004465 return TRUE;
4466
4467 return FALSE;
4468}
4469
4470/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004471 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4472 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004473 */
4474 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004475qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004476{
4477 win_T *win;
4478
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004479 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004480 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004481 return win;
4482 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004483}
4484
4485/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004486 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004487 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 */
4489 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004490qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004492 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004493 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004495 if (qi->qf_bufnr != INVALID_QFBUFNR)
4496 {
4497 buf_T *qfbuf;
4498 qfbuf = buflist_findnr(qi->qf_bufnr);
4499 if (qfbuf != NULL)
4500 return qfbuf;
4501 // buffer is no longer present
4502 qi->qf_bufnr = INVALID_QFBUFNR;
4503 }
4504
Bram Moolenaar9c102382006-05-03 21:26:49 +00004505 FOR_ALL_TAB_WINDOWS(tp, win)
4506 if (is_qf_win(win, qi))
4507 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004508
Bram Moolenaar9c102382006-05-03 21:26:49 +00004509 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510}
4511
4512/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004513 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004514 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004515 */
4516 int
4517qf_process_qftf_option(void)
4518{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004519 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004520}
4521
4522/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004523 * Update the w:quickfix_title variable in the quickfix/location list window in
4524 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004525 */
4526 static void
4527qf_update_win_titlevar(qf_info_T *qi)
4528{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004529 qf_list_T *qfl = qf_get_curlist(qi);
4530 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004531 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004532 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004533
Bram Moolenaar530bed92020-12-16 21:02:56 +01004534 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004535 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004536 if (is_qf_win(win, qi))
4537 {
4538 curwin = win;
4539 qf_set_title_var(qfl);
4540 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004541 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004542 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004543}
4544
4545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 * Find the quickfix buffer. If it exists, update the contents.
4547 */
4548 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004549qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
4551 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004552 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004555 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004556 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (buf != NULL)
4558 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004559 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004560 int qf_winid = 0;
4561
4562 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004563 {
4564 if (curwin->w_llist == qi)
4565 win = curwin;
4566 else
4567 {
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004568 // Find the file window (non-quickfix) with this location list
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004569 win = qf_find_win_with_loclist(qi);
4570 if (win == NULL)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004571 // File window is not found. Find the location list window.
4572 win = qf_find_win(qi);
4573 if (win == NULL)
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004574 return;
4575 }
4576 qf_winid = win->w_id;
4577 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004578
4579 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004580 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004581 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
Bram Moolenaard823fa92016-08-12 16:29:27 +02004583 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004584
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004585 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004586 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004587
Bram Moolenaar864293a2016-06-02 13:40:04 +02004588 if (old_last == NULL)
4589 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004590 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004591
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004592 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004593 aucmd_restbuf(&aco);
4594 }
4595
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004596 // Only redraw when added lines are visible. This avoids flickering
4597 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004598 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004599 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601}
4602
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004603/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004604 * Add an error line to the quickfix buffer.
4605 */
4606 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004607qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004608 buf_T *buf, // quickfix window buffer
4609 linenr_T lnum,
4610 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004611 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004612 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004613 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004614{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004615 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004616 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004617
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004618 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004619
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004620 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004621 // for this entry, then use it.
4622 if (qftf_str != NULL && *qftf_str != NUL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004623 ga_concat(gap, qftf_str);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004624 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004625 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004626 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004627 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004628 else if (qfp->qf_fnum != 0
4629 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4630 && errbuf->b_fname != NULL)
4631 {
4632 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004633 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02004634 else
4635 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004636 // Shorten the file name if not done already.
4637 // For optimization, do this only for the first entry in a
4638 // buffer.
4639 if (first_bufline && (errbuf->b_sfname == NULL
4640 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004641 {
4642 if (*dirname == NUL)
4643 mch_dirname(dirname, MAXPATHL);
4644 shorten_buf_fname(errbuf, dirname, FALSE);
4645 }
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004646 ga_concat(gap, errbuf->b_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004647 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004648 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004649
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004650 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02004651
4652 if (qfp->qf_lnum > 0)
4653 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004654 qf_range_text(gap, qfp);
4655 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004656 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004657 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004658 qf_fmt_text(gap, qfp->qf_pattern);
4659 ga_append(gap, '|');
4660 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004661
Bram Moolenaar858ba062020-05-31 23:11:59 +02004662 // Remove newlines and leading whitespace from the text.
4663 // For an unrecognized line keep the indent, the compiler may
4664 // mark a word with ^^^^.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004665 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004666 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004667
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004668 ga_append(gap, NUL);
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004669
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004670 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len + 1, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004671 return FAIL;
4672
4673 return OK;
4674}
4675
Bram Moolenaard43906d2020-07-20 21:31:32 +02004676/*
4677 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4678 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4679 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004680 static list_T *
4681call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4682{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004683 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004684 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004685 static int recursive = FALSE;
4686
4687 if (recursive)
4688 return NULL; // this doesn't work properly recursively
4689 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004690
4691 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4692 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4693 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004694 if (qfl->qf_qftf_cb.cb_name != NULL)
4695 cb = &qfl->qf_qftf_cb;
4696 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004697 {
4698 typval_T args[1];
4699 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004700 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004701
4702 // create the dict argument
4703 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01004704 {
4705 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004706 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004707 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004708 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4709 dict_add_number(d, "winid", (long)qf_winid);
4710 dict_add_number(d, "id", (long)qfl->qf_id);
4711 dict_add_number(d, "start_idx", start_idx);
4712 dict_add_number(d, "end_idx", end_idx);
4713 ++d->dv_refcount;
4714 args[0].v_type = VAR_DICT;
4715 args[0].vval.v_dict = d;
4716
Bram Moolenaard43906d2020-07-20 21:31:32 +02004717 qftf_list = NULL;
4718 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4719 {
4720 if (rettv.v_type == VAR_LIST)
4721 {
4722 qftf_list = rettv.vval.v_list;
4723 qftf_list->lv_refcount++;
4724 }
4725 clear_tv(&rettv);
4726 }
4727 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004728 }
4729
Bram Moolenaard6c67622022-08-24 20:07:22 +01004730 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004731 return qftf_list;
4732}
4733
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 * Fill current buffer with quickfix errors, replacing any previous contents.
4736 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004737 * If "old_last" is not NULL append the items after this one.
4738 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4739 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 */
4741 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004742qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004744 linenr_T lnum;
4745 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004746 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004747 list_T *qftf_list = NULL;
4748 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaar864293a2016-06-02 13:40:04 +02004750 if (old_last == NULL)
4751 {
4752 if (buf != curbuf)
4753 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004754 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004755 return;
4756 }
4757
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004758 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004759 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004760 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004763 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004764 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004766 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004767 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004768 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004769
4770 *dirname = NUL;
4771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004772 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004773 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004774 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004775 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004776 lnum = 0;
4777 }
4778 else
4779 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004780 if (old_last->qf_next != NULL)
4781 qfp = old_last->qf_next;
4782 else
4783 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004784 lnum = buf->b_ml.ml_line_count;
4785 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004786
4787 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4788 (long)qfl->qf_count);
4789 if (qftf_list != NULL)
4790 qftf_li = qftf_list->lv_first;
4791
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004792 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004794 char_u *qftf_str = NULL;
4795
Bram Moolenaard43906d2020-07-20 21:31:32 +02004796 // Use the text supplied by the user defined function (if any).
4797 // If the returned value is not string, then ignore the rest
4798 // of the returned values and use the default.
4799 if (qftf_li != NULL && !invalid_val)
4800 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004801 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004802 if (qftf_str == NULL)
4803 invalid_val = TRUE;
4804 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004805
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004806 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4807 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004809
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004810 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004811 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004813 if (qfp == NULL)
4814 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004815
4816 if (qftf_li != NULL)
4817 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004819
4820 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004821 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004822 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
4824
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004825 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 check_lnums(TRUE);
4827
Bram Moolenaar864293a2016-06-02 13:40:04 +02004828 if (old_last == NULL)
4829 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004830 // Set the 'filetype' to "qf" each time after filling the buffer.
4831 // This resembles reading a file into a buffer, it's more logical when
4832 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004833 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004834 set_option_value_give_err((char_u *)"ft",
4835 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004836 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004838 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004839 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004841 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004843 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004844 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004845
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004846 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004847 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004850 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 KeyTyped = old_KeyTyped;
4852}
4853
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004854/*
4855 * For every change made to the quickfix list, update the changed tick.
4856 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004857 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004858qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004859{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004860 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004861}
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004864 * Return the quickfix/location list number with the given identifier.
4865 * Returns -1 if list is not found.
4866 */
4867 static int
4868qf_id2nr(qf_info_T *qi, int_u qfid)
4869{
4870 int qf_idx;
4871
4872 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4873 if (qi->qf_lists[qf_idx].qf_id == qfid)
4874 return qf_idx;
4875 return INVALID_QFIDX;
4876}
4877
4878/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004879 * If the current list is not "save_qfid" and we can find the list with that ID
4880 * then make it the current list.
4881 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004882 * Returns OK if successfully restored the list. Returns FAIL if the list with
4883 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004884 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004885 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004886qf_restore_list(qf_info_T *qi, int_u save_qfid)
4887{
4888 int curlist;
4889
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004890 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004891 {
4892 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004893 if (curlist < 0)
4894 // list is not present
4895 return FAIL;
4896 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004897 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004898 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004899}
4900
4901/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004902 * Jump to the first entry if there is one.
4903 */
4904 static void
4905qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4906{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004907 if (qf_restore_list(qi, save_qfid) == FAIL)
4908 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004909
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004910 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004911 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004912 qf_jump(qi, 0, 0, forceit);
4913}
4914
4915/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004916 * Return TRUE when using ":vimgrep" for ":grep".
4917 */
4918 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004919grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004920{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004921 return ((cmdidx == CMD_grep
4922 || cmdidx == CMD_lgrep
4923 || cmdidx == CMD_grepadd
4924 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004925 && STRCMP("internal",
4926 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4927}
4928
4929/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004930 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004932 static char_u *
4933make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004935 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004936 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004937 case CMD_make: return (char_u *)"make";
4938 case CMD_lmake: return (char_u *)"lmake";
4939 case CMD_grep: return (char_u *)"grep";
4940 case CMD_lgrep: return (char_u *)"lgrep";
4941 case CMD_grepadd: return (char_u *)"grepadd";
4942 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4943 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945}
4946
4947/*
4948 * Return the name for the errorfile, in allocated memory.
4949 * Find a new unique name when 'makeef' contains "##".
4950 * Returns NULL for error.
4951 */
4952 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004953get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 char_u *p;
4956 char_u *name;
4957 static int start = -1;
4958 static int off = 0;
4959#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004960 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#endif
4962
4963 if (*p_mef == NUL)
4964 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004965 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004967 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 return name;
4969 }
4970
4971 for (p = p_mef; *p; ++p)
4972 if (p[0] == '#' && p[1] == '#')
4973 break;
4974
4975 if (*p == NUL)
4976 return vim_strsave(p_mef);
4977
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004978 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 for (;;)
4980 {
4981 if (start == -1)
4982 start = mch_get_pid();
4983 else
4984 off += 19;
4985
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004986 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (name == NULL)
4988 break;
4989 STRCPY(name, p_mef);
4990 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4991 STRCAT(name, p + 2);
4992 if (mch_getperm(name) < 0
4993#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004994 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 && mch_lstat((char *)name, &sb) < 0
4996#endif
4997 )
4998 break;
4999 vim_free(name);
5000 }
5001 return name;
5002}
5003
5004/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005005 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5006 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5007 */
5008 static char_u *
5009make_get_fullcmd(char_u *makecmd, char_u *fname)
5010{
5011 char_u *cmd;
5012 unsigned len;
5013
5014 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5015 if (*p_sp != NUL)
5016 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005017 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005018 if (cmd == NULL)
5019 return NULL;
5020 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5021 (char *)p_shq);
5022
5023 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5024 if (*p_sp != NUL)
5025 append_redir(cmd, len, p_sp, fname);
5026
5027 // Display the fully formed command. Output a newline if there's something
5028 // else than the :make command that was typed (in which case the cursor is
5029 // in column 0).
5030 if (msg_col == 0)
5031 msg_didout = FALSE;
5032 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005033 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005034 msg_outtrans(cmd); // show what we are doing
5035
5036 return cmd;
5037}
5038
5039/*
5040 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5041 */
5042 void
5043ex_make(exarg_T *eap)
5044{
5045 char_u *fname;
5046 char_u *cmd;
5047 char_u *enc = NULL;
5048 win_T *wp = NULL;
5049 qf_info_T *qi = &ql_info;
5050 int res;
5051 char_u *au_name = NULL;
5052 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005053 char_u *errorformat = p_efm;
5054 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005055
5056 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5057 if (grep_internal(eap->cmdidx))
5058 {
5059 ex_vimgrep(eap);
5060 return;
5061 }
5062
5063 au_name = make_get_auname(eap->cmdidx);
5064 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5065 curbuf->b_fname, TRUE, curbuf))
5066 {
5067#ifdef FEAT_EVAL
5068 if (aborting())
5069 return;
5070#endif
5071 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005072 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005073
5074 if (is_loclist_cmd(eap->cmdidx))
5075 wp = curwin;
5076
5077 autowrite_all();
5078 fname = get_mef_name();
5079 if (fname == NULL)
5080 return;
5081 mch_remove(fname); // in case it's not unique
5082
5083 cmd = make_get_fullcmd(eap->arg, fname);
5084 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005085 {
5086 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005087 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005088 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005089
5090 // let the shell know if we are redirecting output or not
5091 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5092
5093#ifdef AMIGA
5094 out_flush();
5095 // read window status report and redraw before message
5096 (void)char_avail();
5097#endif
5098
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005099 incr_quickfix_busy();
5100
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005101 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5102 errorformat = p_gefm;
5103 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5104 newlist = FALSE;
5105
5106 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5107 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005108 if (wp != NULL)
5109 {
5110 qi = GET_LOC_LIST(wp);
5111 if (qi == NULL)
5112 goto cleanup;
5113 }
5114 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005115 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005116
5117 // Remember the current quickfix list identifier, so that we can
5118 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005119 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005120 if (au_name != NULL)
5121 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5122 curbuf->b_fname, TRUE, curbuf);
5123 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5124 // display the first error
5125 qf_jump_first(qi, save_qfid, FALSE);
5126
5127cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005128 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005129 mch_remove(fname);
5130 vim_free(fname);
5131 vim_free(cmd);
5132}
5133
5134/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005135 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005136 */
5137 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005138qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005139{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005140 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005141
5142 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5143 return 0;
5144 return qf_get_curlist(qi)->qf_count;
5145}
5146
5147/*
5148 * Returns the number of valid entries in the current quickfix/location list.
5149 */
5150 int
5151qf_get_valid_size(exarg_T *eap)
5152{
5153 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005154 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005155 qfline_T *qfp;
5156 int i, sz = 0;
5157 int prev_fnum = 0;
5158
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005159 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5160 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005161
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005162 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005163 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005164 {
5165 if (qfp->qf_valid)
5166 {
5167 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005168 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005169 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5170 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005171 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005172 sz++;
5173 prev_fnum = qfp->qf_fnum;
5174 }
5175 }
5176 }
5177
5178 return sz;
5179}
5180
5181/*
5182 * Returns the current index of the quickfix/location list.
5183 * Returns 0 if there is an error.
5184 */
5185 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005186qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005187{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005188 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005189
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005190 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5191 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005192
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005193 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005194}
5195
5196/*
5197 * Returns the current index in the quickfix/location list (counting only valid
5198 * entries). If no valid entries are in the list, then returns 1.
5199 */
5200 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005201qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005202{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005203 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005204 qf_list_T *qfl;
5205 qfline_T *qfp;
5206 int i, eidx = 0;
5207 int prev_fnum = 0;
5208
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005209 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5210 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005211
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005212 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005213 qfp = qfl->qf_start;
5214
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005215 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005216 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005217 return 1;
5218
5219 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5220 {
5221 if (qfp->qf_valid)
5222 {
5223 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5224 {
5225 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005227 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005228 eidx++;
5229 prev_fnum = qfp->qf_fnum;
5230 }
5231 }
5232 else
5233 eidx++;
5234 }
5235 }
5236
5237 return eidx ? eidx : 1;
5238}
5239
5240/*
5241 * Get the 'n'th valid error entry in the quickfix or location list.
5242 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5243 * For :cdo and :ldo returns the 'n'th valid error entry.
5244 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5245 */
5246 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005247qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005248{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005249 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005250 int i, eidx;
5251 int prev_fnum = 0;
5252
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005253 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005254 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005255 return 1;
5256
Bram Moolenaar95946f12019-03-31 15:31:59 +02005257 eidx = 0;
5258 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005259 {
5260 if (qfp->qf_valid)
5261 {
5262 if (fdo)
5263 {
5264 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5265 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005266 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005267 eidx++;
5268 prev_fnum = qfp->qf_fnum;
5269 }
5270 }
5271 else
5272 eidx++;
5273 }
5274
5275 if (eidx == n)
5276 break;
5277 }
5278
5279 if (i <= qfl->qf_count)
5280 return i;
5281 else
5282 return 1;
5283}
5284
5285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005287 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005288 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 */
5290 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005291ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005293 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005294 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005295
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005296 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5297 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005298
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005299 if (eap->addr_count > 0)
5300 errornr = (int)eap->line2;
5301 else
5302 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005303 switch (eap->cmdidx)
5304 {
5305 case CMD_cc: case CMD_ll:
5306 errornr = 0;
5307 break;
5308 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5309 case CMD_lfirst:
5310 errornr = 1;
5311 break;
5312 default:
5313 errornr = 32767;
5314 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005315 }
5316
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005317 // For cdo and ldo commands, jump to the nth valid error.
5318 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005319 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5320 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005321 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005322 eap->addr_count > 0 ? (int)eap->line1 : 1,
5323 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5324
5325 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326}
5327
5328/*
5329 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005330 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005331 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 */
5333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005334ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005336 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005337 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005338 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005339
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005340 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5341 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005342
Bram Moolenaar55b69262017-08-13 13:42:01 +02005343 if (eap->addr_count > 0
5344 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5345 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005346 errornr = (int)eap->line2;
5347 else
5348 errornr = 1;
5349
Bram Moolenaar39665952018-08-15 20:59:48 +02005350 // Depending on the command jump to either next or previous entry/file.
5351 switch (eap->cmdidx)
5352 {
5353 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5354 dir = FORWARD;
5355 break;
5356 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5357 case CMD_lNext:
5358 dir = BACKWARD;
5359 break;
5360 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5361 dir = FORWARD_FILE;
5362 break;
5363 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5364 dir = BACKWARD_FILE;
5365 break;
5366 default:
5367 dir = FORWARD;
5368 break;
5369 }
5370
5371 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372}
5373
5374/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005375 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5376 * The index of the entry is stored in 'errornr'.
5377 * Returns NULL if an entry is not found.
5378 */
5379 static qfline_T *
5380qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5381{
5382 qfline_T *qfp = NULL;
5383 int idx = 0;
5384
5385 // Find the first entry in this file
5386 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5387 if (qfp->qf_fnum == bnr)
5388 break;
5389
5390 *errornr = idx;
5391 return qfp;
5392}
5393
5394/*
5395 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5396 * with the error number for the first entry. Assumes the entries are sorted in
5397 * the quickfix list by line number.
5398 */
5399 static qfline_T *
5400qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5401{
5402 while (!got_int
5403 && entry->qf_prev != NULL
5404 && entry->qf_fnum == entry->qf_prev->qf_fnum
5405 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5406 {
5407 entry = entry->qf_prev;
5408 --*errornr;
5409 }
5410
5411 return entry;
5412}
5413
5414/*
5415 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5416 * with the error number for the last entry. Assumes the entries are sorted in
5417 * the quickfix list by line number.
5418 */
5419 static qfline_T *
5420qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5421{
5422 while (!got_int &&
5423 entry->qf_next != NULL
5424 && entry->qf_fnum == entry->qf_next->qf_fnum
5425 && entry->qf_lnum == entry->qf_next->qf_lnum)
5426 {
5427 entry = entry->qf_next;
5428 ++*errornr;
5429 }
5430
5431 return entry;
5432}
5433
5434/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005435 * Returns TRUE if the specified quickfix entry is
5436 * after the given line (linewise is TRUE)
5437 * or after the line and column.
5438 */
5439 static int
5440qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5441{
5442 if (linewise)
5443 return qfp->qf_lnum > pos->lnum;
5444 else
5445 return (qfp->qf_lnum > pos->lnum ||
5446 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5447}
5448
5449/*
5450 * Returns TRUE if the specified quickfix entry is
5451 * before the given line (linewise is TRUE)
5452 * or before the line and column.
5453 */
5454 static int
5455qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5456{
5457 if (linewise)
5458 return qfp->qf_lnum < pos->lnum;
5459 else
5460 return (qfp->qf_lnum < pos->lnum ||
5461 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5462}
5463
5464/*
5465 * Returns TRUE if the specified quickfix entry is
5466 * on or after the given line (linewise is TRUE)
5467 * or on or after the line and column.
5468 */
5469 static int
5470qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5471{
5472 if (linewise)
5473 return qfp->qf_lnum >= pos->lnum;
5474 else
5475 return (qfp->qf_lnum > pos->lnum ||
5476 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5477}
5478
5479/*
5480 * Returns TRUE if the specified quickfix entry is
5481 * on or before the given line (linewise is TRUE)
5482 * or on or before the line and column.
5483 */
5484 static int
5485qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5486{
5487 if (linewise)
5488 return qfp->qf_lnum <= pos->lnum;
5489 else
5490 return (qfp->qf_lnum < pos->lnum ||
5491 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5492}
5493
5494/*
5495 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5496 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5497 * multiple entries on a single line as one. Otherwise returns the entry after
5498 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005499 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5500 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005501 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005502 */
5503 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005504qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005505 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005506 pos_T *pos,
5507 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005508 qfline_T *qfp,
5509 int *errornr)
5510{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005511 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005512 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005513 return qfp;
5514
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005515 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005516 while (qfp->qf_next != NULL
5517 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005518 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005519 {
5520 qfp = qfp->qf_next;
5521 ++*errornr;
5522 }
5523
5524 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005525 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005526 return NULL;
5527
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005528 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005529 qfp = qfp->qf_next;
5530 ++*errornr;
5531
5532 return qfp;
5533}
5534
5535/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005536 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5537 * If 'linewise' is TRUE, returns the entry before the specified line and
5538 * treats multiple entries on a single line as one. Otherwise returns the entry
5539 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005540 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5541 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005542 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005543 */
5544 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005545qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005546 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005547 pos_T *pos,
5548 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005549 qfline_T *qfp,
5550 int *errornr)
5551{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005552 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005553 while (qfp->qf_next != NULL
5554 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005555 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005556 {
5557 qfp = qfp->qf_next;
5558 ++*errornr;
5559 }
5560
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005561 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005562 return NULL;
5563
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005564 if (linewise)
5565 // If multiple entries are on the same line, then use the first entry
5566 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005567
5568 return qfp;
5569}
5570
5571/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005572 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005573 * the direction 'dir'.
5574 */
5575 static qfline_T *
5576qf_find_closest_entry(
5577 qf_list_T *qfl,
5578 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005579 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005580 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005581 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005582 int *errornr)
5583{
5584 qfline_T *qfp;
5585
5586 *errornr = 0;
5587
5588 // Find the first entry in this file
5589 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5590 if (qfp == NULL)
5591 return NULL; // no entry in this file
5592
5593 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005594 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005595 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005596 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005597
5598 return qfp;
5599}
5600
5601/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005602 * Get the nth quickfix entry below the specified entry. Searches forward in
5603 * the list. If linewise is TRUE, then treat multiple entries on a single line
5604 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005605 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005606 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005607qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005608{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005609 qfline_T *entry = entry_arg;
5610
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005611 while (n-- > 0 && !got_int)
5612 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005613 int first_errornr = *errornr;
5614
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005615 if (linewise)
5616 // Treat all the entries on the same line in this file as one
5617 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005618
5619 if (entry->qf_next == NULL
5620 || entry->qf_next->qf_fnum != entry->qf_fnum)
5621 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005622 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005623 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005624 break;
5625 }
5626
5627 entry = entry->qf_next;
5628 ++*errornr;
5629 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005630}
5631
5632/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005633 * Get the nth quickfix entry above the specified entry. Searches backwards in
5634 * the list. If linewise is TRUE, then treat multiple entries on a single line
5635 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005636 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005637 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005638qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005639{
5640 while (n-- > 0 && !got_int)
5641 {
5642 if (entry->qf_prev == NULL
5643 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5644 break;
5645
5646 entry = entry->qf_prev;
5647 --*errornr;
5648
5649 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005650 if (linewise)
5651 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005652 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005653}
5654
5655/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005656 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5657 * the specified direction. Returns the error number in the quickfix list or 0
5658 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005659 */
5660 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005661qf_find_nth_adj_entry(
5662 qf_list_T *qfl,
5663 int bnr,
5664 pos_T *pos,
5665 int n,
5666 int dir,
5667 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005668{
5669 qfline_T *adj_entry;
5670 int errornr;
5671
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005672 // Find an entry closest to the specified position
5673 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005674 if (adj_entry == NULL)
5675 return 0;
5676
5677 if (--n > 0)
5678 {
5679 // Go to the n'th entry in the current buffer
5680 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005681 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005682 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005683 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005684 }
5685
5686 return errornr;
5687}
5688
5689/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005690 * Jump to a quickfix entry in the current file nearest to the current line or
5691 * current line/col.
5692 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5693 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005694 */
5695 void
5696ex_cbelow(exarg_T *eap)
5697{
5698 qf_info_T *qi;
5699 qf_list_T *qfl;
5700 int dir;
5701 int buf_has_flag;
5702 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005703 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005704
5705 if (eap->addr_count > 0 && eap->line2 <= 0)
5706 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005707 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005708 return;
5709 }
5710
5711 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005712 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5713 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005714 buf_has_flag = BUF_HAS_QF_ENTRY;
5715 else
5716 buf_has_flag = BUF_HAS_LL_ENTRY;
5717 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5718 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005719 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005720 return;
5721 }
5722
5723 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5724 return;
5725
5726 qfl = qf_get_curlist(qi);
5727 // check if the list has valid errors
5728 if (!qf_list_has_valid_entries(qfl))
5729 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005730 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005731 return;
5732 }
5733
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005734 if (eap->cmdidx == CMD_cbelow
5735 || eap->cmdidx == CMD_lbelow
5736 || eap->cmdidx == CMD_cafter
5737 || eap->cmdidx == CMD_lafter)
5738 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005739 dir = FORWARD;
5740 else
5741 dir = BACKWARD;
5742
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005743 pos = curwin->w_cursor;
5744 // A quickfix entry column number is 1 based whereas cursor column
5745 // number is 0 based. Adjust the column number.
5746 pos.col++;
5747 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5748 eap->addr_count > 0 ? eap->line2 : 0, dir,
5749 eap->cmdidx == CMD_cbelow
5750 || eap->cmdidx == CMD_lbelow
5751 || eap->cmdidx == CMD_cabove
5752 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005753
5754 if (errornr > 0)
5755 qf_jump(qi, 0, errornr, FALSE);
5756 else
5757 emsg(_(e_no_more_items));
5758}
5759
5760/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005761 * Return the autocmd name for the :cfile Ex commands
5762 */
5763 static char_u *
5764cfile_get_auname(cmdidx_T cmdidx)
5765{
5766 switch (cmdidx)
5767 {
5768 case CMD_cfile: return (char_u *)"cfile";
5769 case CMD_cgetfile: return (char_u *)"cgetfile";
5770 case CMD_caddfile: return (char_u *)"caddfile";
5771 case CMD_lfile: return (char_u *)"lfile";
5772 case CMD_lgetfile: return (char_u *)"lgetfile";
5773 case CMD_laddfile: return (char_u *)"laddfile";
5774 default: return NULL;
5775 }
5776}
5777
5778/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005779 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005780 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 */
5782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005783ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005785 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005786 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005787 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005788 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005789 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005790 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005791
Bram Moolenaara16123a2019-03-28 20:31:07 +01005792 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005793 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5794 NULL, FALSE, curbuf))
5795 {
5796#ifdef FEAT_EVAL
5797 if (aborting())
5798 return;
5799#endif
5800 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005801
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005802 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005803#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005804 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005805 {
5806 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005807 NULL, NULL,
5808 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005809 if (browse_file == NULL)
5810 return;
5811 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5812 vim_free(browse_file);
5813 }
5814 else
5815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005817 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005818
Bram Moolenaar39665952018-08-15 20:59:48 +02005819 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005820 wp = curwin;
5821
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005822 incr_quickfix_busy();
5823
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005824 // This function is used by the :cfile, :cgetfile and :caddfile
5825 // commands.
5826 // :cfile always creates a new quickfix list and jumps to the
5827 // first error.
5828 // :cgetfile creates a new quickfix list but doesn't jump to the
5829 // first error.
5830 // :caddfile adds to an existing quickfix list. If there is no
5831 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005832 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005833 && eap->cmdidx != CMD_laddfile),
5834 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005835 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005836 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005837 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005838 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005839 {
5840 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005841 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005842 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005843 }
5844 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005845 qf_list_changed(qf_get_curlist(qi));
5846 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005847 if (au_name != NULL)
5848 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005849
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005850 // Jump to the first error for a new list and if autocmds didn't
5851 // free the list.
5852 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5853 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005854 // display the first error
5855 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005856
5857 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005858}
5859
5860/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005861 * Return the vimgrep autocmd name.
5862 */
5863 static char_u *
5864vgr_get_auname(cmdidx_T cmdidx)
5865{
5866 switch (cmdidx)
5867 {
5868 case CMD_vimgrep: return (char_u *)"vimgrep";
5869 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5870 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5871 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5872 case CMD_grep: return (char_u *)"grep";
5873 case CMD_lgrep: return (char_u *)"lgrep";
5874 case CMD_grepadd: return (char_u *)"grepadd";
5875 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5876 default: return NULL;
5877 }
5878}
5879
5880/*
5881 * Initialize the regmatch used by vimgrep for pattern "s".
5882 */
5883 static void
5884vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5885{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005886 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005887 regmatch->regprog = NULL;
5888
5889 if (s == NULL || *s == NUL)
5890 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005891 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005892 if (last_search_pat() == NULL)
5893 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005894 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005895 return;
5896 }
5897 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5898 }
5899 else
5900 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5901
5902 regmatch->rmm_ic = p_ic;
5903 regmatch->rmm_maxcol = 0;
5904}
5905
5906/*
5907 * Display a file name when vimgrep is running.
5908 */
5909 static void
5910vgr_display_fname(char_u *fname)
5911{
5912 char_u *p;
5913
5914 msg_start();
5915 p = msg_strtrunc(fname, TRUE);
5916 if (p == NULL)
5917 msg_outtrans(fname);
5918 else
5919 {
5920 msg_outtrans(p);
5921 vim_free(p);
5922 }
5923 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005924 msg_didout = FALSE; // overwrite this message
5925 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005926 msg_col = 0;
5927 out_flush();
5928}
5929
5930/*
5931 * Load a dummy buffer to search for a pattern using vimgrep.
5932 */
5933 static buf_T *
5934vgr_load_dummy_buf(
5935 char_u *fname,
5936 char_u *dirname_start,
5937 char_u *dirname_now)
5938{
5939 int save_mls;
5940#if defined(FEAT_SYN_HL)
5941 char_u *save_ei = NULL;
5942#endif
5943 buf_T *buf;
5944
5945#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005946 // Don't do Filetype autocommands to avoid loading syntax and
5947 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005948 save_ei = au_event_disable(",Filetype");
5949#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005950 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005951 save_mls = p_mls;
5952 p_mls = 0;
5953
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005954 // Load file into a buffer, so that 'fileencoding' is detected,
5955 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005956 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5957
5958 p_mls = save_mls;
5959#if defined(FEAT_SYN_HL)
5960 au_event_restore(save_ei);
5961#endif
5962
5963 return buf;
5964}
5965
5966/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005967 * Check whether a quickfix/location list is valid. Autocmds may remove or
5968 * change a quickfix list when vimgrep is running. If the list is not found,
5969 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005970 */
5971 static int
5972vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005973 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005974 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005975 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005976 char_u *title)
5977{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005978 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005979 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005980 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005981 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005982 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005983 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005984 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005985 return FALSE;
5986 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005987 else
5988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005989 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005990 qf_new_list(qi, title);
5991 return TRUE;
5992 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005993 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005994
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005995 if (qf_restore_list(qi, qfid) == FAIL)
5996 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005997
5998 return TRUE;
5999}
6000
6001/*
6002 * Search for a pattern in all the lines in a buffer and add the matching lines
6003 * to a quickfix list.
6004 */
6005 static int
6006vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006007 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006008 char_u *fname,
6009 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006010 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006011 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006012 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006013 int duplicate_name,
6014 int flags)
6015{
6016 int found_match = FALSE;
6017 long lnum;
6018 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006019 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006020
Bram Moolenaar1c299432018-10-28 14:36:09 +01006021 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006022 {
6023 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006024 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006025 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006026 // Regular expression match
6027 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Paul Ollis65745772022-06-05 16:55:54 +01006028 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006029 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006030 // Pass the buffer number so that it gets used even for a
6031 // dummy buffer, unless duplicate_name is set, then the
6032 // buffer will be wiped out below.
6033 if (qf_add_entry(qfl,
6034 NULL, // dir
6035 fname,
6036 NULL,
6037 duplicate_name ? 0 : buf->b_fnum,
6038 ml_get_buf(buf,
6039 regmatch->startpos[0].lnum + lnum, FALSE),
6040 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006041 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006042 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006043 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006044 FALSE, // vis_col
6045 NULL, // search pattern
6046 0, // nr
6047 0, // type
6048 TRUE // valid
6049 ) == QF_FAIL)
6050 {
6051 got_int = TRUE;
6052 break;
6053 }
6054 found_match = TRUE;
6055 if (--*tomatch == 0)
6056 break;
6057 if ((flags & VGR_GLOBAL) == 0
6058 || regmatch->endpos[0].lnum > 0)
6059 break;
6060 col = regmatch->endpos[0].col
6061 + (col == regmatch->endpos[0].col);
6062 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6063 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006064 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006065 }
6066 else
6067 {
6068 char_u *str = ml_get_buf(buf, lnum, FALSE);
6069 int score;
6070 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006071 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006072
6073 // Fuzzy string match
6074 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6075 {
6076 // Pass the buffer number so that it gets used even for a
6077 // dummy buffer, unless duplicate_name is set, then the
6078 // buffer will be wiped out below.
6079 if (qf_add_entry(qfl,
6080 NULL, // dir
6081 fname,
6082 NULL,
6083 duplicate_name ? 0 : buf->b_fnum,
6084 str,
6085 lnum,
thinca6864efa2021-06-19 20:45:20 +02006086 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006087 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006088 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006089 FALSE, // vis_col
6090 NULL, // search pattern
6091 0, // nr
6092 0, // type
6093 TRUE // valid
6094 ) == QF_FAIL)
6095 {
6096 got_int = TRUE;
6097 break;
6098 }
6099 found_match = TRUE;
6100 if (--*tomatch == 0)
6101 break;
6102 if ((flags & VGR_GLOBAL) == 0)
6103 break;
6104 col = matches[pat_len - 1] + col + 1;
6105 if (col > (colnr_T)STRLEN(str))
6106 break;
6107 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006108 }
6109 line_breakcheck();
6110 if (got_int)
6111 break;
6112 }
6113
6114 return found_match;
6115}
6116
6117/*
6118 * Jump to the first match and update the directory.
6119 */
6120 static void
6121vgr_jump_to_match(
6122 qf_info_T *qi,
6123 int forceit,
6124 int *redraw_for_dummy,
6125 buf_T *first_match_buf,
6126 char_u *target_dir)
6127{
6128 buf_T *buf;
6129
6130 buf = curbuf;
6131 qf_jump(qi, 0, 0, forceit);
6132 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006133 // If we jumped to another buffer redrawing will already be
6134 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006135 *redraw_for_dummy = FALSE;
6136
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006137 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006138 if (curbuf == first_match_buf && target_dir != NULL)
6139 {
6140 exarg_T ea;
6141
Bram Moolenaara80faa82020-04-12 19:37:17 +02006142 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006143 ea.arg = target_dir;
6144 ea.cmdidx = CMD_lcd;
6145 ex_cd(&ea);
6146 }
6147}
6148
6149/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006150 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006151 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006152typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006153{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006154 long tomatch; // maximum number of matches to find
6155 char_u *spat; // search pattern
6156 int flags; // search modifier
6157 char_u **fnames; // list of files to search
6158 int fcount; // number of files
6159 regmmatch_T regmatch; // compiled search pattern
6160 char_u *qf_title; // quickfix list title
6161} vgr_args_T;
6162
6163/*
6164 * Process :vimgrep command arguments. The command syntax is:
6165 *
6166 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6167 */
6168 static int
6169vgr_process_args(
6170 exarg_T *eap,
6171 vgr_args_T *args)
6172{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006173 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006174
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006175 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006176
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006177 args->regmatch.regprog = NULL;
6178 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006179
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006180 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006181 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006182 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006183 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006184
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006185 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006186 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006187 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006188 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006189 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006190 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006191 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006192
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006193 vgr_init_regmatch(&args->regmatch, args->spat);
6194 if (args->regmatch.regprog == NULL)
6195 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006196
6197 p = skipwhite(p);
6198 if (*p == NUL)
6199 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006200 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006201 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006202 }
6203
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006204 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006205 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6206 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006207 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006208 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006209 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006210 }
6211
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006212 return OK;
6213}
6214
6215/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006216 * Return TRUE if "buf" had an existing swap file, the current swap file does
6217 * not end in ".swp".
6218 */
6219 static int
6220existing_swapfile(buf_T *buf)
6221{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006222 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006223 {
6224 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6225 size_t len = STRLEN(fname);
6226
6227 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6228 }
6229 return FALSE;
6230}
6231
6232/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006233 * Search for a pattern in a list of files and populate the quickfix list with
6234 * the matches.
6235 */
6236 static int
6237vgr_process_files(
6238 win_T *wp,
6239 qf_info_T *qi,
6240 vgr_args_T *cmd_args,
6241 int *redraw_for_dummy,
6242 buf_T **first_match_buf,
6243 char_u **target_dir)
6244{
6245 int status = FAIL;
6246 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6247 time_t seconds = 0;
6248 char_u *fname;
6249 int fi;
6250 buf_T *buf;
6251 int duplicate_name = FALSE;
6252 int using_dummy;
6253 char_u *dirname_start = NULL;
6254 char_u *dirname_now = NULL;
6255 int found_match;
6256 aco_save_T aco;
6257
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006258 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6259 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006260 if (dirname_start == NULL || dirname_now == NULL)
6261 goto theend;
6262
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006263 // Remember the current directory, because a BufRead autocommand that does
6264 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006265 mch_dirname(dirname_start, MAXPATHL);
6266
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006267 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006268 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6269 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006270 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006271 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006272 if (time(NULL) > seconds)
6273 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006274 // Display the file name every second or so, show the user we are
6275 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006276 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006277 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006278 }
6279
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006280 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006281 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6282 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006283 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006284 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006285 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006286 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006287
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006288 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006289 }
6290 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006291 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006292 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006293
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006294 // Check whether the quickfix list is still valid. When loading a
6295 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006296 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006297 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006298
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006299 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006300
Bram Moolenaar81695252004-12-29 20:58:21 +00006301 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006302 {
6303 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006304 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006305 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006306 else
6307 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006308 // Try for a match in all lines of the buffer.
6309 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006310 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006311 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006312 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006313
Bram Moolenaar81695252004-12-29 20:58:21 +00006314 if (using_dummy)
6315 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006316 if (found_match && *first_match_buf == NULL)
6317 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006318 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006319 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006320 // Never keep a dummy buffer if there is another buffer
6321 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006322 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006323 buf = NULL;
6324 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006325 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006326 || buf->b_p_bh[0] == 'u' // "unload"
6327 || buf->b_p_bh[0] == 'w' // "wipe"
6328 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006329 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006330 // When no match was found we don't need to remember the
6331 // buffer, wipe it out. If there was a match and it
6332 // wasn't the first one or we won't jump there: only
6333 // unload the buffer.
6334 // Ignore 'hidden' here, because it may lead to having too
6335 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006336 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006337 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006338 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006339 buf = NULL;
6340 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006341 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006342 || (cmd_args->flags & VGR_NOJUMP)
6343 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006344 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006345 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006346 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006347 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006348 buf = NULL;
6349 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006350 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006351
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006352 if (buf != NULL)
6353 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006354 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006355 buf->b_flags &= ~BF_DUMMY;
6356
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006357 // If the buffer is still loaded we need to use the
6358 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006359 if (buf == *first_match_buf
6360 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006361 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006362 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006363
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006364 // The buffer is still loaded, the Filetype autocommands
6365 // need to be done now, in that buffer. And the modelines
6366 // need to be done (again). But not the window-local
6367 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006368 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006369#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006370 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6371 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006372#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006373 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006374 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006375 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006376 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006377 }
6378 }
6379
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006380 status = OK;
6381
6382theend:
6383 vim_free(dirname_now);
6384 vim_free(dirname_start);
6385 return status;
6386}
6387
6388/*
6389 * ":vimgrep {pattern} file(s)"
6390 * ":vimgrepadd {pattern} file(s)"
6391 * ":lvimgrep {pattern} file(s)"
6392 * ":lvimgrepadd {pattern} file(s)"
6393 */
6394 void
6395ex_vimgrep(exarg_T *eap)
6396{
6397 vgr_args_T args;
6398 qf_info_T *qi;
6399 qf_list_T *qfl;
6400 int_u save_qfid;
6401 win_T *wp = NULL;
6402 int redraw_for_dummy = FALSE;
6403 buf_T *first_match_buf = NULL;
6404 char_u *target_dir = NULL;
6405 char_u *au_name = NULL;
6406 int status;
6407
6408 au_name = vgr_get_auname(eap->cmdidx);
6409 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6410 curbuf->b_fname, TRUE, curbuf))
6411 {
6412#ifdef FEAT_EVAL
6413 if (aborting())
6414 return;
6415#endif
6416 }
6417
6418 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6419 if (qi == NULL)
6420 return;
6421
6422 if (vgr_process_args(eap, &args) == FAIL)
6423 goto theend;
6424
6425 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6426 && eap->cmdidx != CMD_vimgrepadd
6427 && eap->cmdidx != CMD_lvimgrepadd)
6428 || qf_stack_empty(qi))
6429 // make place for a new list
6430 qf_new_list(qi, args.qf_title);
6431
6432 incr_quickfix_busy();
6433
6434 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6435 &first_match_buf, &target_dir);
6436 if (status != OK)
6437 {
6438 FreeWild(args.fcount, args.fnames);
6439 decr_quickfix_busy();
6440 goto theend;
6441 }
6442
6443 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006444
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006445 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006446 qfl->qf_nonevalid = FALSE;
6447 qfl->qf_ptr = qfl->qf_start;
6448 qfl->qf_index = 1;
6449 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006450
Bram Moolenaar864293a2016-06-02 13:40:04 +02006451 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006452
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006453 // Remember the current quickfix list identifier, so that we can check for
6454 // autocommands changing the current quickfix list.
6455 save_qfid = qf_get_curlist(qi)->qf_id;
6456
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006457 if (au_name != NULL)
6458 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6459 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006460 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6461 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006462 if (!qflist_valid(wp, save_qfid)
6463 || qf_restore_list(qi, save_qfid) == FAIL)
6464 {
6465 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006466 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006467 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006468
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006469 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006470 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006471 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006472 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006473 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6474 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006475 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006476 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006477 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006478
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006479 decr_quickfix_busy();
6480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006481 // If we loaded a dummy buffer into the current window, the autocommands
6482 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006483 if (redraw_for_dummy)
6484 {
6485#ifdef FEAT_FOLDING
6486 foldUpdateAll(curwin);
6487#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006488 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006489#endif
6490 }
6491
Bram Moolenaar86b68352004-12-27 21:59:20 +00006492theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006493 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006494 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006495 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006496}
6497
6498/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006499 * Restore current working directory to "dirname_start" if they differ, taking
6500 * into account whether it is set locally or globally.
6501 */
6502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006503restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006504{
6505 char_u *dirname_now = alloc(MAXPATHL);
6506
6507 if (NULL != dirname_now)
6508 {
6509 mch_dirname(dirname_now, MAXPATHL);
6510 if (STRCMP(dirname_start, dirname_now) != 0)
6511 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006512 // If the directory has changed, change it back by building up an
6513 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006514 exarg_T ea;
6515
Bram Moolenaara80faa82020-04-12 19:37:17 +02006516 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006517 ea.arg = dirname_start;
6518 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6519 ex_cd(&ea);
6520 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006521 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006522 }
6523}
6524
6525/*
6526 * Load file "fname" into a dummy buffer and return the buffer pointer,
6527 * placing the directory resulting from the buffer load into the
6528 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6529 * prior to calling this function. Restores directory to "dirname_start" prior
6530 * to returning, if autocmds or the 'autochdir' option have changed it.
6531 *
6532 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6533 * or wipe_dummy_buffer() later!
6534 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006535 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006536 */
6537 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006538load_dummy_buffer(
6539 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006540 char_u *dirname_start, // in: old directory
6541 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006542{
6543 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006544 bufref_T newbufref;
6545 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006546 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006547 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006548 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006549
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006550 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006551 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6552 if (newbuf == NULL)
6553 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006554 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006555
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006556 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006557 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6558
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006559 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006560 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006561 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006562 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006563 ++newbuf->b_locked;
6564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006565 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006566 aucmd_prepbuf(&aco, newbuf);
6567
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006568 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006569 (void)setfname(curbuf, fname, NULL, FALSE);
6570
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006571 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006572 check_need_swap(TRUE);
6573
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006574 // Remove the "dummy" flag, otherwise autocommands may not
6575 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006576 curbuf->b_flags &= ~BF_DUMMY;
6577
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006578 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006579 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006580 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006581 NULL, READ_NEW | READ_DUMMY);
6582 --newbuf->b_locked;
6583 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006584 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006585 && !(curbuf->b_flags & BF_NEW))
6586 {
6587 failed = FALSE;
6588 if (curbuf != newbuf)
6589 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006590 // Bloody autocommands changed the buffer! Can happen when
6591 // using netrw and editing a remote file. Use the current
6592 // buffer instead, delete the dummy one after restoring the
6593 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006594 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006595 newbuf = curbuf;
6596 }
6597 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006598
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006599 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006600 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006601 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6602 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006603
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006604 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6605 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006606 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006607 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006608
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006609 // When autocommands/'autochdir' option changed directory: go back.
6610 // Let the caller know what the resulting dir was first, in case it is
6611 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006612 mch_dirname(resulting_dir, MAXPATHL);
6613 restore_start_dir(dirname_start);
6614
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006615 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006616 return NULL;
6617 if (failed)
6618 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006619 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006620 return NULL;
6621 }
6622 return newbuf;
6623}
6624
6625/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006626 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6627 * directory to "dirname_start" prior to returning, if autocmds or the
6628 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006629 */
6630 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006631wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006632{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006633 // If any autocommand opened a window on the dummy buffer, close that
6634 // window. If we can't close them all then give up.
6635 while (buf->b_nwindows > 0)
6636 {
6637 int did_one = FALSE;
6638 win_T *wp;
6639
6640 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006641 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006642 if (wp->w_buffer == buf)
6643 {
6644 if (win_close(wp, FALSE) == OK)
6645 did_one = TRUE;
6646 break;
6647 }
6648 if (!did_one)
6649 return;
6650 }
6651
6652 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006653 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006654#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006655 cleanup_T cs;
6656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006657 // Reset the error/interrupt/exception state here so that aborting()
6658 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6659 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006660 enter_cleanup(&cs);
6661#endif
6662
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006663 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006664
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006665#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006666 // Restore the error/interrupt/exception state if not discarded by a
6667 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006668 leave_cleanup(&cs);
6669#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006670 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006671 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006672 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006673}
6674
6675/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006676 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6677 * directory to "dirname_start" prior to returning, if autocmds or the
6678 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006679 */
6680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006681unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006682{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006683 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006684 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006685 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006686
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006687 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006688 restore_start_dir(dirname_start);
6689 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006690}
6691
Bram Moolenaar05159a02005-02-26 23:04:13 +00006692#if defined(FEAT_EVAL) || defined(PROTO)
6693/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006694 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006695 * to 'list'. Returns OK on success.
6696 */
6697 static int
6698get_qfline_items(qfline_T *qfp, list_T *list)
6699{
6700 int bufnum;
6701 dict_T *dict;
6702 char_u buf[2];
6703
6704 // Handle entries with a non-existing buffer number.
6705 bufnum = qfp->qf_fnum;
6706 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6707 bufnum = 0;
6708
6709 if ((dict = dict_alloc()) == NULL)
6710 return FAIL;
6711 if (list_append_dict(list, dict) == FAIL)
6712 return FAIL;
6713
6714 buf[0] = qfp->qf_type;
6715 buf[1] = NUL;
6716 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006717 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6718 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6719 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6720 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6721 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6722 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006723 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6724 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6725 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6726 || dict_add_string(dict, "type", buf) == FAIL
6727 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6728 return FAIL;
6729
6730 return OK;
6731}
6732
6733/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006734 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006735 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006736 * If eidx is not 0, then return only the specified entry. Otherwise return
6737 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006738 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006739 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006740get_errorlist(
6741 qf_info_T *qi_arg,
6742 win_T *wp,
6743 int qf_idx,
6744 int eidx,
6745 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006746{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006747 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006748 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006749 qfline_T *qfp;
6750 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006751
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006752 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006753 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006754 qi = &ql_info;
6755 if (wp != NULL)
6756 {
6757 qi = GET_LOC_LIST(wp);
6758 if (qi == NULL)
6759 return FAIL;
6760 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006761 }
6762
Bram Moolenaar858ba062020-05-31 23:11:59 +02006763 if (eidx < 0)
6764 return OK;
6765
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006766 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006767 qf_idx = qi->qf_curlist;
6768
Bram Moolenaar0398e002019-03-21 21:12:49 +01006769 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006770 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006771
Bram Moolenaar0398e002019-03-21 21:12:49 +01006772 qfl = qf_get_list(qi, qf_idx);
6773 if (qf_list_empty(qfl))
6774 return FAIL;
6775
Bram Moolenaara16123a2019-03-28 20:31:07 +01006776 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006777 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006778 if (eidx > 0)
6779 {
6780 if (eidx == i)
6781 return get_qfline_items(qfp, list);
6782 }
6783 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006784 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006785 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006786
Bram Moolenaar05159a02005-02-26 23:04:13 +00006787 return OK;
6788}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006789
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006790// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006791enum {
6792 QF_GETLIST_NONE = 0x0,
6793 QF_GETLIST_TITLE = 0x1,
6794 QF_GETLIST_ITEMS = 0x2,
6795 QF_GETLIST_NR = 0x4,
6796 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006797 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006798 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006799 QF_GETLIST_IDX = 0x40,
6800 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006801 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006802 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006803 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006804 QF_GETLIST_QFTF = 0x800,
6805 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006806};
6807
6808/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006809 * Parse text from 'di' and return the quickfix list items.
6810 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006811 */
6812 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006813qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006814{
6815 int status = FAIL;
6816 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006817 char_u *errorformat = p_efm;
6818 dictitem_T *efm_di;
6819 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006820
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006821 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006822 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006823 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006824 // If errorformat is supplied then use it, otherwise use the 'efm'
6825 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006826 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6827 {
6828 if (efm_di->di_tv.v_type != VAR_STRING ||
6829 efm_di->di_tv.vval.v_string == NULL)
6830 return FAIL;
6831 errorformat = efm_di->di_tv.vval.v_string;
6832 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006833
Bram Moolenaar36538222017-09-02 19:51:44 +02006834 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006835 if (l == NULL)
6836 return FAIL;
6837
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006838 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006839 if (qi != NULL)
6840 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006841 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006842 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6843 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006844 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006845 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006846 }
6847 free(qi);
6848 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006849 dict_add_list(retdict, "items", l);
6850 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006851 }
6852
6853 return status;
6854}
6855
6856/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006857 * Return the quickfix/location list window identifier in the current tabpage.
6858 */
6859 static int
6860qf_winid(qf_info_T *qi)
6861{
6862 win_T *win;
6863
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006864 // The quickfix window can be opened even if the quickfix list is not set
6865 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006866 if (qi == NULL)
6867 return 0;
6868 win = qf_find_win(qi);
6869 if (win != NULL)
6870 return win->w_id;
6871 return 0;
6872}
6873
6874/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006875 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006876 * window. If there is no buffer associated with the list or the buffer is
6877 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006878 */
6879 static int
6880qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6881{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006882 int bufnum = 0;
6883
6884 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6885 bufnum = qi->qf_bufnr;
6886
6887 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006888}
6889
6890/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006891 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006892 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006893 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006894qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006895{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006896 int flags = QF_GETLIST_NONE;
6897
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006898 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006899 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006900 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006901 if (!loclist)
6902 // File window ID is applicable only to location list windows
6903 flags &= ~ QF_GETLIST_FILEWINID;
6904 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006905
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006906 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006907 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006908
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006909 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006910 flags |= QF_GETLIST_NR;
6911
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006912 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006913 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006914
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006915 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006916 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006917
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006918 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006919 flags |= QF_GETLIST_ID;
6920
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006921 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006922 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006923
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006924 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006925 flags |= QF_GETLIST_IDX;
6926
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006927 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006928 flags |= QF_GETLIST_SIZE;
6929
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006930 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01006931 flags |= QF_GETLIST_TICK;
6932
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006933 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006934 flags |= QF_GETLIST_FILEWINID;
6935
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006936 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006937 flags |= QF_GETLIST_QFBUFNR;
6938
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006939 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02006940 flags |= QF_GETLIST_QFTF;
6941
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006942 return flags;
6943}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006944
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006945/*
6946 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6947 * If 'nr' and 'id' are not present in 'what' then return the current
6948 * quickfix list index.
6949 * If 'nr' is zero then return the current quickfix list index.
6950 * If 'nr' is '$' then return the last quickfix list index.
6951 * If 'id' is present then return the index of the quickfix list with that id.
6952 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6953 * Return -1, if quickfix list is not present or if the stack is empty.
6954 */
6955 static int
6956qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6957{
6958 int qf_idx;
6959 dictitem_T *di;
6960
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006961 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006962 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6963 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006964 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006965 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006966 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006967 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006968 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006969 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006970 qf_idx = di->di_tv.vval.v_number - 1;
6971 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006972 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006973 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006974 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006975 else if (di->di_tv.v_type == VAR_STRING
6976 && di->di_tv.vval.v_string != NULL
6977 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006978 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006979 qf_idx = qi->qf_listcount - 1;
6980 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006981 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006982 }
6983
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006984 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6985 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006986 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006987 if (di->di_tv.v_type == VAR_NUMBER)
6988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006989 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006990 if (di->di_tv.vval.v_number != 0)
6991 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6992 }
6993 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006994 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006995 }
6996
6997 return qf_idx;
6998}
6999
7000/*
7001 * Return default values for quickfix list properties in retdict.
7002 */
7003 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007004qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007005{
7006 int status = OK;
7007
7008 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007009 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007010 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7011 {
7012 list_T *l = list_alloc();
7013 if (l != NULL)
7014 status = dict_add_list(retdict, "items", l);
7015 else
7016 status = FAIL;
7017 }
7018 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007019 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007020 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007021 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007022 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007023 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007024 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007025 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007026 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007027 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007028 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007029 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007030 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007031 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007032 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007033 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007034 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7035 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007036 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7037 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007038
7039 return status;
7040}
7041
7042/*
7043 * Return the quickfix list title as 'title' in retdict
7044 */
7045 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007046qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007047{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007048 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007049}
7050
7051/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007052 * Returns the identifier of the window used to display files from a location
7053 * list. If there is no associated window, then returns 0. Useful only when
7054 * called from a location list window.
7055 */
7056 static int
7057qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7058{
7059 int winid = 0;
7060
7061 if (wp != NULL && IS_LL_WINDOW(wp))
7062 {
7063 win_T *ll_wp = qf_find_win_with_loclist(qi);
7064 if (ll_wp != NULL)
7065 winid = ll_wp->w_id;
7066 }
7067
7068 return dict_add_number(retdict, "filewinid", winid);
7069}
7070
7071/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007072 * Return the quickfix list items/entries as 'items' in retdict.
7073 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007074 */
7075 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007076qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007077{
7078 int status = OK;
7079 list_T *l = list_alloc();
7080 if (l != NULL)
7081 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007082 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007083 dict_add_list(retdict, "items", l);
7084 }
7085 else
7086 status = FAIL;
7087
7088 return status;
7089}
7090
7091/*
7092 * Return the quickfix list context (if any) as 'context' in retdict.
7093 */
7094 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007095qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007096{
7097 int status;
7098 dictitem_T *di;
7099
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007100 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007101 {
7102 di = dictitem_alloc((char_u *)"context");
7103 if (di != NULL)
7104 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007105 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007106 status = dict_add(retdict, di);
7107 if (status == FAIL)
7108 dictitem_free(di);
7109 }
7110 else
7111 status = FAIL;
7112 }
7113 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007114 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007115
7116 return status;
7117}
7118
7119/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007120 * Return the current quickfix list index as 'idx' in retdict.
7121 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007122 */
7123 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007124qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007125{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007126 if (eidx == 0)
7127 {
7128 eidx = qfl->qf_index;
7129 if (qf_list_empty(qfl))
7130 // For empty lists, current index is set to 0
7131 eidx = 0;
7132 }
7133 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007134}
7135
7136/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007137 * Return the 'quickfixtextfunc' function of a quickfix/location list
7138 */
7139 static int
7140qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7141{
7142 int status;
7143
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007144 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007145 {
7146 typval_T tv;
7147
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007148 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007149 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7150 clear_tv(&tv);
7151 }
7152 else
7153 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7154
7155 return status;
7156}
7157
7158/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007159 * Return quickfix/location list details (title) as a
7160 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7161 * then current list is used. Otherwise the specified list is used.
7162 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007163 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007164qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7165{
7166 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007167 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007168 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007169 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007170 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007171 dictitem_T *di;
7172 int flags = QF_GETLIST_NONE;
7173
7174 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7175 return qf_get_list_from_lines(what, di, retdict);
7176
7177 if (wp != NULL)
7178 qi = GET_LOC_LIST(wp);
7179
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007180 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007181
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007182 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007183 qf_idx = qf_getprop_qfidx(qi, what);
7184
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007185 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007186 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007187 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007188
Bram Moolenaar0398e002019-03-21 21:12:49 +01007189 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007190
Bram Moolenaar858ba062020-05-31 23:11:59 +02007191 // If an entry index is specified, use that
7192 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7193 {
7194 if (di->di_tv.v_type != VAR_NUMBER)
7195 return FAIL;
7196 eidx = di->di_tv.vval.v_number;
7197 }
7198
Bram Moolenaard823fa92016-08-12 16:29:27 +02007199 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007200 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007201 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007202 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007203 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007204 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007205 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007206 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007207 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007208 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007209 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007210 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007211 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007212 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007213 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007214 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007215 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007216 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007217 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7218 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007219 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7220 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007221 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7222 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007223
Bram Moolenaard823fa92016-08-12 16:29:27 +02007224 return status;
7225}
7226
7227/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007228 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007229 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7230 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007231 */
7232 static int
7233qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007234 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007235 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007236 int first_entry,
7237 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007238{
7239 static int did_bufnr_emsg;
7240 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007241 int bufnum, valid, status, col, end_col, vcol, nr;
7242 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007243
7244 if (first_entry)
7245 did_bufnr_emsg = FALSE;
7246
Bram Moolenaard61efa52022-07-23 09:52:04 +01007247 filename = dict_get_string(d, "filename", TRUE);
7248 module = dict_get_string(d, "module", TRUE);
7249 bufnum = (int)dict_get_number(d, "bufnr");
7250 lnum = (int)dict_get_number(d, "lnum");
7251 end_lnum = (int)dict_get_number(d, "end_lnum");
7252 col = (int)dict_get_number(d, "col");
7253 end_col = (int)dict_get_number(d, "end_col");
7254 vcol = (int)dict_get_number(d, "vcol");
7255 nr = (int)dict_get_number(d, "nr");
7256 type = dict_get_string(d, "type", TRUE);
7257 pattern = dict_get_string(d, "pattern", TRUE);
7258 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007259 if (text == NULL)
7260 text = vim_strsave((char_u *)"");
7261
7262 valid = TRUE;
7263 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7264 valid = FALSE;
7265
7266 // Mark entries with non-existing buffer number as not valid. Give the
7267 // error message only once.
7268 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7269 {
7270 if (!did_bufnr_emsg)
7271 {
7272 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007273 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007274 }
7275 valid = FALSE;
7276 bufnum = 0;
7277 }
7278
7279 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007280 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007281 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007282
Bram Moolenaar0398e002019-03-21 21:12:49 +01007283 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007284 NULL, // dir
7285 filename,
7286 module,
7287 bufnum,
7288 text,
7289 lnum,
thinca6864efa2021-06-19 20:45:20 +02007290 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007291 col,
thinca6864efa2021-06-19 20:45:20 +02007292 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007293 vcol, // vis_col
7294 pattern, // search pattern
7295 nr,
7296 type == NULL ? NUL : *type,
7297 valid);
7298
7299 vim_free(filename);
7300 vim_free(module);
7301 vim_free(pattern);
7302 vim_free(text);
7303 vim_free(type);
7304
Bram Moolenaar9752c722018-12-22 16:49:34 +01007305 if (valid)
7306 *valid_entry = TRUE;
7307
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007308 return status;
7309}
7310
7311/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007312 * Add list of entries to quickfix/location list. Each list entry is
7313 * a dictionary with item information.
7314 */
7315 static int
7316qf_add_entries(
7317 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007318 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007319 list_T *list,
7320 char_u *title,
7321 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007322{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007323 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007324 listitem_T *li;
7325 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007326 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007327 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007328 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007329
Bram Moolenaara3921f42017-06-04 15:30:34 +02007330 if (action == ' ' || qf_idx == qi->qf_listcount)
7331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007332 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007333 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007334 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007335 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007336 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007337 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007338 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007339 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007340 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007341 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007342 qf_free_items(qfl);
7343 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007344 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007345
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007346 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007347 {
7348 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007349 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007350
7351 d = li->li_tv.vval.v_dict;
7352 if (d == NULL)
7353 continue;
7354
Bram Moolenaar0398e002019-03-21 21:12:49 +01007355 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007356 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007357 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007358 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007359 }
7360
Bram Moolenaar9752c722018-12-22 16:49:34 +01007361 // Check if any valid error entries are added to the list.
7362 if (valid_entry)
7363 qfl->qf_nonevalid = FALSE;
7364 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007365 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007366 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007367
7368 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007369 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007370 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007371
7372 // Update the current error index if not appending to the list or if the
7373 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007374 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007375 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007376
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007377 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007378 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007379
7380 return retval;
7381}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007382
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007383/*
7384 * Get the quickfix list index from 'nr' or 'id'
7385 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007386 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007387qf_setprop_get_qfidx(
7388 qf_info_T *qi,
7389 dict_T *what,
7390 int action,
7391 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007392{
7393 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007394 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007395
Bram Moolenaard823fa92016-08-12 16:29:27 +02007396 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7397 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007398 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007399 if (di->di_tv.v_type == VAR_NUMBER)
7400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007401 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007402 if (di->di_tv.vval.v_number != 0)
7403 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007404
Bram Moolenaar55b69262017-08-13 13:42:01 +02007405 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7406 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007407 // When creating a new list, accept qf_idx pointing to the next
7408 // non-available list and add the new list at the end of the
7409 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007410 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007411 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007412 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007413 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007414 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007415 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007416 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007417 }
7418 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007419 && di->di_tv.vval.v_string != NULL
7420 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007421 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007422 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007423 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007424 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007425 qf_idx = 0;
7426 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007427 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007428 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007429 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007430 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007431 }
7432
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007433 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007434 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007435 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007436 if (di->di_tv.v_type != VAR_NUMBER)
7437 return INVALID_QFIDX;
7438
7439 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007440 }
7441
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007442 return qf_idx;
7443}
7444
7445/*
7446 * Set the quickfix list title.
7447 */
7448 static int
7449qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7450{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007451 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007452
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007453 if (di->di_tv.v_type != VAR_STRING)
7454 return FAIL;
7455
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007456 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007457 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007458 if (qf_idx == qi->qf_curlist)
7459 qf_update_win_titlevar(qi);
7460
7461 return OK;
7462}
7463
7464/*
7465 * Set quickfix list items/entries.
7466 */
7467 static int
7468qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7469{
7470 int retval = FAIL;
7471 char_u *title_save;
7472
7473 if (di->di_tv.v_type != VAR_LIST)
7474 return FAIL;
7475
7476 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7477 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7478 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007479 vim_free(title_save);
7480
7481 return retval;
7482}
7483
7484/*
7485 * Set quickfix list items/entries from a list of lines.
7486 */
7487 static int
7488qf_setprop_items_from_lines(
7489 qf_info_T *qi,
7490 int qf_idx,
7491 dict_T *what,
7492 dictitem_T *di,
7493 int action)
7494{
7495 char_u *errorformat = p_efm;
7496 dictitem_T *efm_di;
7497 int retval = FAIL;
7498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007499 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007500 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7501 {
7502 if (efm_di->di_tv.v_type != VAR_STRING ||
7503 efm_di->di_tv.vval.v_string == NULL)
7504 return FAIL;
7505 errorformat = efm_di->di_tv.vval.v_string;
7506 }
7507
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007508 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007509 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7510 return FAIL;
7511
7512 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007513 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007514 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007515 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007516 retval = OK;
7517
7518 return retval;
7519}
7520
7521/*
7522 * Set quickfix list context.
7523 */
7524 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007525qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007526{
7527 typval_T *ctx;
7528
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007529 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007530 ctx = alloc_tv();
7531 if (ctx != NULL)
7532 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007533 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007534
7535 return OK;
7536}
7537
7538/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007539 * Set the current index in the specified quickfix list
7540 */
7541 static int
7542qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7543{
7544 int denote = FALSE;
7545 int newidx;
7546 int old_qfidx;
7547 qfline_T *qf_ptr;
7548
7549 // If the specified index is '$', then use the last entry
7550 if (di->di_tv.v_type == VAR_STRING
7551 && di->di_tv.vval.v_string != NULL
7552 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7553 newidx = qfl->qf_count;
7554 else
7555 {
7556 // Otherwise use the specified index
7557 newidx = tv_get_number_chk(&di->di_tv, &denote);
7558 if (denote)
7559 return FAIL;
7560 }
7561
7562 if (newidx < 1) // sanity check
7563 return FAIL;
7564 if (newidx > qfl->qf_count)
7565 newidx = qfl->qf_count;
7566
7567 old_qfidx = qfl->qf_index;
7568 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7569 if (qf_ptr == NULL)
7570 return FAIL;
7571 qfl->qf_ptr = qf_ptr;
7572 qfl->qf_index = newidx;
7573
7574 // If the current list is modified and it is displayed in the quickfix
7575 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007576 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007577 qf_win_pos_update(qi, old_qfidx);
7578
7579 return OK;
7580}
7581
7582/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007583 * Set the current index in the specified quickfix list
7584 */
7585 static int
7586qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7587{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007588 callback_T cb;
7589
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007590 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007591 cb = get_callback(&di->di_tv);
7592 if (cb.cb_name != NULL && *cb.cb_name != NUL)
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007593 set_callback(&qfl->qf_qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007594
7595 return OK;
7596}
7597
7598/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007599 * Set quickfix/location list properties (title, items, context).
7600 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007601 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007602 */
7603 static int
7604qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7605{
7606 dictitem_T *di;
7607 int retval = FAIL;
7608 int qf_idx;
7609 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007610 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007611
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007612 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007613 newlist = TRUE;
7614
7615 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007616 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007617 return FAIL;
7618
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007619 if (newlist)
7620 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007621 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007622 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007623 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007624 }
7625
Bram Moolenaar0398e002019-03-21 21:12:49 +01007626 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007627 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007628 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007629 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007630 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007631 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007632 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007633 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007634 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007635 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7636 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007637 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7638 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007639
Bram Moolenaar287153c2020-11-29 14:20:27 +01007640 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007641 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007642 if (newlist)
7643 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007644
Bram Moolenaard823fa92016-08-12 16:29:27 +02007645 return retval;
7646}
7647
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007648/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007649 * Free the entire quickfix/location list stack.
7650 * If the quickfix/location list window is open, then clear it.
7651 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007652 static void
7653qf_free_stack(win_T *wp, qf_info_T *qi)
7654{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007655 win_T *qfwin = qf_find_win(qi);
7656 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007657
7658 if (qfwin != NULL)
7659 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007660 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007661 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007662 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007663 qf_update_buffer(qi, NULL);
7664 }
7665
7666 if (wp != NULL && IS_LL_WINDOW(wp))
7667 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007668 // If in the location list window, then use the non-location list
7669 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007670 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007671 if (llwin != NULL)
7672 wp = llwin;
7673 }
7674
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007675 qf_free_all(wp);
7676 if (wp == NULL)
7677 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007678 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007679 qi->qf_curlist = 0;
7680 qi->qf_listcount = 0;
7681 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007682 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007683 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007684 // If the location list window is open, then create a new empty
7685 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007686 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007687
Bram Moolenaar95946f12019-03-31 15:31:59 +02007688 if (new_ll != NULL)
7689 {
7690 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007691
Bram Moolenaar95946f12019-03-31 15:31:59 +02007692 // first free the list reference in the location list window
7693 ll_free_all(&qfwin->w_llist_ref);
7694
7695 qfwin->w_llist_ref = new_ll;
7696 if (wp != qfwin)
7697 win_set_loclist(wp, new_ll);
7698 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007699 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007700}
7701
Bram Moolenaard823fa92016-08-12 16:29:27 +02007702/*
7703 * Populate the quickfix list with the items supplied in the list
7704 * of dictionaries. "title" will be copied to w:quickfix_title.
7705 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007706 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007707 */
7708 int
7709set_errorlist(
7710 win_T *wp,
7711 list_T *list,
7712 int action,
7713 char_u *title,
7714 dict_T *what)
7715{
7716 qf_info_T *qi = &ql_info;
7717 int retval = OK;
7718
7719 if (wp != NULL)
7720 {
7721 qi = ll_get_or_alloc_list(wp);
7722 if (qi == NULL)
7723 return FAIL;
7724 }
7725
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007726 if (action == 'f')
7727 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007728 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007729 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007730 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007731 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007732
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007733 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007734 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007735 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007736 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007737 _("cannot have both a list and a \"what\" argument"));
7738 return FAIL;
7739 }
7740
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007741 incr_quickfix_busy();
7742
7743 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007744 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007745 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007746 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007747 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007748 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007749 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007750 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007751
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007752 decr_quickfix_busy();
7753
Bram Moolenaard823fa92016-08-12 16:29:27 +02007754 return retval;
7755}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007756
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007757/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007758 * Mark the quickfix context and callback function as in use for all the lists
7759 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007760 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007761 static int
7762mark_quickfix_ctx(qf_info_T *qi, int copyID)
7763{
7764 int i;
7765 int abort = FALSE;
7766 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007767 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007768
7769 for (i = 0; i < LISTCOUNT && !abort; ++i)
7770 {
7771 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007772 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7773 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007774 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7775
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007776 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007777 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007778 }
7779
7780 return abort;
7781}
7782
7783/*
7784 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007785 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007786 */
7787 int
7788set_ref_in_quickfix(int copyID)
7789{
7790 int abort = FALSE;
7791 tabpage_T *tp;
7792 win_T *win;
7793
7794 abort = mark_quickfix_ctx(&ql_info, copyID);
7795 if (abort)
7796 return abort;
7797
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007798 abort = set_ref_in_callback(&qftf_cb, copyID);
7799 if (abort)
7800 return abort;
7801
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007802 FOR_ALL_TAB_WINDOWS(tp, win)
7803 {
7804 if (win->w_llist != NULL)
7805 {
7806 abort = mark_quickfix_ctx(win->w_llist, copyID);
7807 if (abort)
7808 return abort;
7809 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007810 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7811 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007812 // In a location list window and none of the other windows is
7813 // referring to this location list. Mark the location list
7814 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007815 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7816 if (abort)
7817 return abort;
7818 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007819 }
7820
7821 return abort;
7822}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007823#endif
7824
Bram Moolenaar81695252004-12-29 20:58:21 +00007825/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007826 * Return the autocmd name for the :cbuffer Ex commands
7827 */
7828 static char_u *
7829cbuffer_get_auname(cmdidx_T cmdidx)
7830{
7831 switch (cmdidx)
7832 {
7833 case CMD_cbuffer: return (char_u *)"cbuffer";
7834 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7835 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7836 case CMD_lbuffer: return (char_u *)"lbuffer";
7837 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7838 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7839 default: return NULL;
7840 }
7841}
7842
7843/*
7844 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7845 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7846 */
7847 static int
7848cbuffer_process_args(
7849 exarg_T *eap,
7850 buf_T **bufp,
7851 linenr_T *line1,
7852 linenr_T *line2)
7853{
7854 buf_T *buf = NULL;
7855
7856 if (*eap->arg == NUL)
7857 buf = curbuf;
7858 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7859 buf = buflist_findnr(atoi((char *)eap->arg));
7860
7861 if (buf == NULL)
7862 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007863 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007864 return FAIL;
7865 }
7866
7867 if (buf->b_ml.ml_mfp == NULL)
7868 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007869 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007870 return FAIL;
7871 }
7872
7873 if (eap->addr_count == 0)
7874 {
7875 eap->line1 = 1;
7876 eap->line2 = buf->b_ml.ml_line_count;
7877 }
7878
7879 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7880 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7881 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007882 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007883 return FAIL;
7884 }
7885
7886 *line1 = eap->line1;
7887 *line2 = eap->line2;
7888 *bufp = buf;
7889
7890 return OK;
7891}
7892
7893/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007894 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007895 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007896 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007897 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007898 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007899 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007900 */
7901 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007902ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007903{
7904 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007905 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007906 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007907 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007908 int_u save_qfid;
7909 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007910 char_u *qf_title;
7911 linenr_T line1;
7912 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007913
Bram Moolenaara16123a2019-03-28 20:31:07 +01007914 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007915 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007916 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007917 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007918#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007919 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007920 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007921#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007922 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007923
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007924 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007925 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7926 if (qi == NULL)
7927 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007928
Bram Moolenaara16123a2019-03-28 20:31:07 +01007929 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7930 return;
7931
7932 qf_title = qf_cmdtitle(*eap->cmdlinep);
7933
7934 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007935 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007936 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7937 (char *)qf_title, (char *)buf->b_sfname);
7938 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007939 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007940
7941 incr_quickfix_busy();
7942
7943 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7944 (eap->cmdidx != CMD_caddbuffer
7945 && eap->cmdidx != CMD_laddbuffer),
7946 line1, line2,
7947 qf_title, NULL);
7948 if (qf_stack_empty(qi))
7949 {
7950 decr_quickfix_busy();
7951 return;
7952 }
7953 if (res >= 0)
7954 qf_list_changed(qf_get_curlist(qi));
7955
7956 // Remember the current quickfix list identifier, so that we can
7957 // check for autocommands changing the current quickfix list.
7958 save_qfid = qf_get_curlist(qi)->qf_id;
7959 if (au_name != NULL)
7960 {
7961 buf_T *curbuf_old = curbuf;
7962
7963 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7964 TRUE, curbuf);
7965 if (curbuf != curbuf_old)
7966 // Autocommands changed buffer, don't jump now, "qi" may
7967 // be invalid.
7968 res = 0;
7969 }
7970 // Jump to the first error for a new list and if autocmds didn't
7971 // free the list.
7972 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7973 eap->cmdidx == CMD_lbuffer)
7974 && qflist_valid(wp, save_qfid))
7975 // display the first error
7976 qf_jump_first(qi, save_qfid, eap->forceit);
7977
7978 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007979}
7980
Bram Moolenaar1e015462005-09-25 22:16:38 +00007981#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007982/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007983 * Return the autocmd name for the :cexpr Ex commands.
7984 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007985 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007986cexpr_get_auname(cmdidx_T cmdidx)
7987{
7988 switch (cmdidx)
7989 {
7990 case CMD_cexpr: return (char_u *)"cexpr";
7991 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7992 case CMD_caddexpr: return (char_u *)"caddexpr";
7993 case CMD_lexpr: return (char_u *)"lexpr";
7994 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7995 case CMD_laddexpr: return (char_u *)"laddexpr";
7996 default: return NULL;
7997 }
7998}
7999
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008000 int
8001trigger_cexpr_autocmd(int cmdidx)
8002{
8003 char_u *au_name = cexpr_get_auname(cmdidx);
8004
8005 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8006 curbuf->b_fname, TRUE, curbuf))
8007 {
8008 if (aborting())
8009 return FAIL;
8010 }
8011 return OK;
8012}
8013
8014 int
8015cexpr_core(exarg_T *eap, typval_T *tv)
8016{
8017 qf_info_T *qi;
8018 win_T *wp = NULL;
8019
8020 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8021 if (qi == NULL)
8022 return FAIL;
8023
8024 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8025 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8026 {
8027 int res;
8028 int_u save_qfid;
8029 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8030
8031 incr_quickfix_busy();
8032 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8033 (eap->cmdidx != CMD_caddexpr
8034 && eap->cmdidx != CMD_laddexpr),
8035 (linenr_T)0, (linenr_T)0,
8036 qf_cmdtitle(*eap->cmdlinep), NULL);
8037 if (qf_stack_empty(qi))
8038 {
8039 decr_quickfix_busy();
8040 return FAIL;
8041 }
8042 if (res >= 0)
8043 qf_list_changed(qf_get_curlist(qi));
8044
8045 // Remember the current quickfix list identifier, so that we can
8046 // check for autocommands changing the current quickfix list.
8047 save_qfid = qf_get_curlist(qi)->qf_id;
8048 if (au_name != NULL)
8049 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8050 curbuf->b_fname, TRUE, curbuf);
8051
8052 // Jump to the first error for a new list and if autocmds didn't
8053 // free the list.
8054 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8055 && qflist_valid(wp, save_qfid))
8056 // display the first error
8057 qf_jump_first(qi, save_qfid, eap->forceit);
8058 decr_quickfix_busy();
8059 return OK;
8060 }
8061
Bram Moolenaar677658a2022-01-05 16:09:06 +00008062 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008063 return FAIL;
8064}
8065
Bram Moolenaara16123a2019-03-28 20:31:07 +01008066/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008067 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8068 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008069 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008070 */
8071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008072ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008073{
8074 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008075
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008076 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008077 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008078
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008079 // Evaluate the expression. When the result is a string or a list we can
8080 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008081 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008082 if (tv != NULL)
8083 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008084 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008085 free_tv(tv);
8086 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008087}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008088#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008089
8090/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008091 * Get the location list for ":lhelpgrep"
8092 */
8093 static qf_info_T *
8094hgr_get_ll(int *new_ll)
8095{
8096 win_T *wp;
8097 qf_info_T *qi;
8098
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008099 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008100 if (bt_help(curwin->w_buffer))
8101 wp = curwin;
8102 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008103 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008104 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008105
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008106 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008107 qi = NULL;
8108 else
8109 qi = wp->w_llist;
8110
8111 if (qi == NULL)
8112 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008113 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008114 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008115 return NULL;
8116 *new_ll = TRUE;
8117 }
8118
8119 return qi;
8120}
8121
8122/*
8123 * Search for a pattern in a help file.
8124 */
8125 static void
8126hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008127 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008128 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008129 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008130 regmatch_T *p_regmatch)
8131{
8132 FILE *fd;
8133 long lnum;
8134
8135 fd = mch_fopen((char *)fname, "r");
8136 if (fd == NULL)
8137 return;
8138
8139 lnum = 1;
8140 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8141 {
8142 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008143
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008144 // Convert a line if 'encoding' is not utf-8 and
8145 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008146 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008147 {
8148 line = string_convert(p_vc, IObuff, NULL);
8149 if (line == NULL)
8150 line = IObuff;
8151 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008152
8153 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8154 {
8155 int l = (int)STRLEN(line);
8156
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008157 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008158 while (l > 0 && line[l - 1] <= ' ')
8159 line[--l] = NUL;
8160
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008161 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008162 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008163 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008164 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008165 0,
8166 line,
8167 lnum,
thinca6864efa2021-06-19 20:45:20 +02008168 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008169 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008170 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008171 (int)(p_regmatch->endp[0] - line)
8172 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008173 FALSE, // vis_col
8174 NULL, // search pattern
8175 0, // nr
8176 1, // type
8177 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008178 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008179 {
8180 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008181 if (line != IObuff)
8182 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008183 break;
8184 }
8185 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008186 if (line != IObuff)
8187 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008188 ++lnum;
8189 line_breakcheck();
8190 }
8191 fclose(fd);
8192}
8193
8194/*
8195 * Search for a pattern in all the help files in the doc directory under
8196 * the given directory.
8197 */
8198 static void
8199hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008200 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008201 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008202 regmatch_T *p_regmatch,
8203 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008204#ifdef FEAT_MULTI_LANG
8205 , char_u *lang
8206#endif
8207 )
8208{
8209 int fcount;
8210 char_u **fnames;
8211 int fi;
8212
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008213 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008214 add_pathsep(dirname);
8215 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8216 if (gen_expand_wildcards(1, &dirname, &fcount,
8217 &fnames, EW_FILE|EW_SILENT) == OK
8218 && fcount > 0)
8219 {
8220 for (fi = 0; fi < fcount && !got_int; ++fi)
8221 {
8222#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008223 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008224 if (lang != NULL
8225 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008226 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008227 && !(STRNICMP(lang, "en", 2) == 0
8228 && STRNICMP("txt", fnames[fi]
8229 + STRLEN(fnames[fi]) - 3, 3) == 0))
8230 continue;
8231#endif
8232
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008233 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008234 }
8235 FreeWild(fcount, fnames);
8236 }
8237}
8238
8239/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008240 * Search for a pattern in all the help files in the 'runtimepath'
8241 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008242 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008243 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008244 */
8245 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008246hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008247{
8248 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008249
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008250 vimconv_T vc;
8251
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008252 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8253 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008254 vc.vc_type = CONV_NONE;
8255 if (!enc_utf8)
8256 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008257
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008258 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008259 p = p_rtp;
8260 while (*p != NUL && !got_int)
8261 {
8262 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8263
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008264 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008265#ifdef FEAT_MULTI_LANG
8266 , lang
8267#endif
8268 );
8269 }
8270
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008271 if (vc.vc_type != CONV_NONE)
8272 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008273}
8274
8275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 * ":helpgrep {pattern}"
8277 */
8278 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008279ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
8281 regmatch_T regmatch;
8282 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008283 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008284 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008285 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008286 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008287 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008288 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
Bram Moolenaar73633f82012-01-20 13:39:07 +01008290 switch (eap->cmdidx)
8291 {
8292 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8293 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8294 default: break;
8295 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008296 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8297 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008298 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008299#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008300 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008301 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008302#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008303 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008304
Bram Moolenaar39665952018-08-15 20:59:48 +02008305 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008306 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008307 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008308 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008309 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008310 }
8311
Bram Moolenaara16123a2019-03-28 20:31:07 +01008312 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8313 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008314 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008315 p_cpo = empty_option;
8316
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008317 incr_quickfix_busy();
8318
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008319#ifdef FEAT_MULTI_LANG
8320 // Check for a specified language
8321 lang = check_help_lang(eap->arg);
8322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8324 regmatch.rm_ic = FALSE;
8325 if (regmatch.regprog != NULL)
8326 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008327 qf_list_T *qfl;
8328
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008329 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008330 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008331 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008333 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008334
Bram Moolenaar473de612013-06-08 18:19:48 +02008335 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008337 qfl->qf_nonevalid = FALSE;
8338 qfl->qf_ptr = qfl->qf_start;
8339 qfl->qf_index = 1;
8340 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008341 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008344 if (p_cpo == empty_option)
8345 p_cpo = save_cpo;
8346 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008347 {
8348 // Darn, some plugin changed the value. If it's still empty it was
8349 // changed and restored, need to restore in the complicated way.
8350 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008351 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008352 if (save_cpo_allocated)
8353 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008354 }
8355
8356 if (updated)
8357 // This may open a window and source scripts, do this after 'cpo' was
8358 // restored.
8359 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaar73633f82012-01-20 13:39:07 +01008361 if (au_name != NULL)
8362 {
8363 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8364 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008365 // When adding a location list to an existing location list stack,
8366 // if the autocmd made the stack invalid, then just return.
8367 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008368 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008369 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008370 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008371 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008372 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008373
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008374 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008375 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008376 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008377 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008378 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008379
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008380 decr_quickfix_busy();
8381
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008382 if (eap->cmdidx == CMD_lhelpgrep)
8383 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008384 // If the help window is not opened or if it already points to the
8385 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008386 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008387 {
8388 if (new_qi)
8389 ll_free_all(&qi);
8390 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008391 else if (curwin->w_llist == NULL && new_qi)
8392 // current window didn't have a location list associated with it
8393 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008394 curwin->w_llist = qi;
8395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008397
8398# if defined(EXITFREE) || defined(PROTO)
8399 void
8400free_quickfix()
8401{
8402 win_T *win;
8403 tabpage_T *tab;
8404
8405 qf_free_all(NULL);
8406 // Free all location lists
8407 FOR_ALL_TAB_WINDOWS(tab, win)
8408 qf_free_all(win);
8409
8410 ga_clear(&qfga);
8411}
8412# endif
8413
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008414#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008415
8416#if defined(FEAT_EVAL) || defined(PROTO)
8417# ifdef FEAT_QUICKFIX
8418 static void
8419get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8420{
8421 if (what_arg->v_type == VAR_UNKNOWN)
8422 {
8423 if (rettv_list_alloc(rettv) == OK)
8424 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008425 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008426 }
8427 else
8428 {
8429 if (rettv_dict_alloc(rettv) == OK)
8430 if (is_qf || (wp != NULL))
8431 {
8432 if (what_arg->v_type == VAR_DICT)
8433 {
8434 dict_T *d = what_arg->vval.v_dict;
8435
8436 if (d != NULL)
8437 qf_get_properties(wp, d, rettv->vval.v_dict);
8438 }
8439 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008440 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008441 }
8442 }
8443}
8444# endif
8445
8446/*
8447 * "getloclist()" function
8448 */
8449 void
8450f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8451{
8452# ifdef FEAT_QUICKFIX
8453 win_T *wp;
8454
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008455 if (in_vim9script()
8456 && (check_for_number_arg(argvars, 0) == FAIL
8457 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8458 return;
8459
Bram Moolenaare677df82019-09-02 22:31:11 +02008460 wp = find_win_by_nr_or_id(&argvars[0]);
8461 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8462# endif
8463}
8464
8465/*
8466 * "getqflist()" function
8467 */
8468 void
8469f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8470{
8471# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008472 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8473 return;
8474
Bram Moolenaare677df82019-09-02 22:31:11 +02008475 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8476# endif
8477}
8478
8479/*
8480 * Used by "setqflist()" and "setloclist()" functions
8481 */
8482 static void
8483set_qf_ll_list(
8484 win_T *wp UNUSED,
8485 typval_T *list_arg UNUSED,
8486 typval_T *action_arg UNUSED,
8487 typval_T *what_arg UNUSED,
8488 typval_T *rettv)
8489{
8490# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008491 char_u *act;
8492 int action = 0;
8493 static int recursive = 0;
8494# endif
8495
8496 rettv->vval.v_number = -1;
8497
8498# ifdef FEAT_QUICKFIX
8499 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008500 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008501 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008502 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008503 else
8504 {
8505 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008506 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008507 int valid_dict = TRUE;
8508
8509 if (action_arg->v_type == VAR_STRING)
8510 {
8511 act = tv_get_string_chk(action_arg);
8512 if (act == NULL)
8513 return; // type error; errmsg already given
8514 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8515 act[1] == NUL)
8516 action = *act;
8517 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008518 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008519 }
8520 else if (action_arg->v_type == VAR_UNKNOWN)
8521 action = ' ';
8522 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008523 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008524
8525 if (action_arg->v_type != VAR_UNKNOWN
8526 && what_arg->v_type != VAR_UNKNOWN)
8527 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008528 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8529 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008530 else
8531 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008532 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008533 valid_dict = FALSE;
8534 }
8535 }
8536
8537 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008538 if (l != NULL && action && valid_dict
8539 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008540 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008541 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008542 rettv->vval.v_number = 0;
8543 --recursive;
8544 }
8545# endif
8546}
8547
8548/*
8549 * "setloclist()" function
8550 */
8551 void
8552f_setloclist(typval_T *argvars, typval_T *rettv)
8553{
8554 win_T *win;
8555
8556 rettv->vval.v_number = -1;
8557
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008558 if (in_vim9script()
8559 && (check_for_number_arg(argvars, 0) == FAIL
8560 || check_for_list_arg(argvars, 1) == FAIL
8561 || check_for_opt_string_arg(argvars, 2) == FAIL
8562 || (argvars[2].v_type != VAR_UNKNOWN
8563 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8564 return;
8565
Bram Moolenaare677df82019-09-02 22:31:11 +02008566 win = find_win_by_nr_or_id(&argvars[0]);
8567 if (win != NULL)
8568 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8569}
8570
8571/*
8572 * "setqflist()" function
8573 */
8574 void
8575f_setqflist(typval_T *argvars, typval_T *rettv)
8576{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008577 if (in_vim9script()
8578 && (check_for_list_arg(argvars, 0) == FAIL
8579 || check_for_opt_string_arg(argvars, 1) == FAIL
8580 || (argvars[1].v_type != VAR_UNKNOWN
8581 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8582 return;
8583
Bram Moolenaare677df82019-09-02 22:31:11 +02008584 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8585}
8586#endif