blob: db607171a19719471b4c314490bdcf405b323c99 [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
Bram Moolenaard43906d2020-07-20 21:31:32 +020088 callback_T 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);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
thinca6864efa2021-06-19 20:45:20 +0200180static void qf_range_text(qfline_T *qfp, char_u *buf, int bufsize);
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
222/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200223 * Maximum number of bytes allowed per line while reading a errorfile.
224 */
225#define LINE_MAXLEN 4096
226
haya14busae023d492022-02-08 18:09:29 +0000227/*
228 * Patterns used. Keep in sync with qf_parse_fmt[].
229 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200230static struct fmtpattern
231{
232 char_u convchar;
233 char *pattern;
234} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200235 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200236 {'f', ".\\+"}, // only used when at end
haya14busae023d492022-02-08 18:09:29 +0000237 {'n', "\\d\\+"}, // 1
238 {'l', "\\d\\+"}, // 2
239 {'e', "\\d\\+"}, // 3
240 {'c', "\\d\\+"}, // 4
241 {'k', "\\d\\+"}, // 5
242 {'t', "."}, // 6
243#define FMT_PATTERN_M 7
244 {'m', ".\\+"}, // 7
245#define FMT_PATTERN_R 8
246 {'r', ".*"}, // 8
247 {'p', "[- .]*"}, // 9
248 {'v', "\\d\\+"}, // 10
249 {'s', ".\\+"}, // 11
250 {'o', ".\\+"} // 12
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200251 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200252
253/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 * See fmt_pat definition above for the list of supported patterns. The
256 * pattern specifier is supplied in "efmpat". The converted pattern is stored
257 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 */
259 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260efmpat_to_regpat(
261 char_u *efmpat,
262 char_u *regpat,
263 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200264 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100265 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266{
267 char_u *srcptr;
268
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200271 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000272 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200273 return NULL;
274 }
haya14busae023d492022-02-08 18:09:29 +0000275 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000277 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000280 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 return NULL;
282 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283 efminfo->addr[idx] = (char_u)++round;
284 *regpat++ = '\\';
285 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200288 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200289 // Also match "c:" in the file name, even when
290 // checking for a colon next: "%f:".
291 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 STRCPY(regpat, "\\%(\\a:\\)\\=");
293 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294 }
295#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200300 // A file name may contain spaces, but this isn't
301 // in "\f". For "%f:%l:%m" there may be a ":" in
302 // the file name. Use ".\{-1,}x" instead (x is
303 // the next character), the requirement that :999:
304 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 STRCPY(regpat, ".\\{-1,}");
306 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 }
308 else
309 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200310 // File name followed by '\\' or '%': include as
311 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 STRCPY(regpat, "\\f\\+");
313 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314 }
315 }
316 else
317 {
318 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 while ((*regpat = *srcptr++) != NUL)
320 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200322 *regpat++ = '\\';
323 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200325 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326}
327
328/*
329 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 */
332 static char_u *
333scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 char_u *efm,
336 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100337 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338{
339 char_u *efmp = *pefmp;
340
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200343 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 {
345 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200346 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347 if (efmp < efm + len)
348 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200349 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200350 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200352 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353 if (efmp == efm + len)
354 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000355 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 return NULL;
357 }
358 }
359 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200360 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 *regpat++ = *++efmp;
362 *regpat++ = '\\';
363 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 }
365 else
366 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200367 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000368 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369 return NULL;
370 }
371
372 *pefmp = efmp;
373
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375}
376
377/*
378 * Analyze/parse an errorformat prefix.
379 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100381efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200383 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200385 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200386 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200387 else
388 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000389 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 }
392
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394}
395
396/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200397 * Converts a 'errorformat' string part in 'efm' to a regular expression
398 * pattern. The resulting regex pattern is returned in "regpat". Additional
399 * information about the 'erroformat' pattern is returned in "fmt_ptr".
400 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200401 */
402 static int
403efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200404 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200405 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200406 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100407 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200408{
409 char_u *ptr;
410 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200411 int round;
412 int idx = 0;
413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200414 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200415 ptr = regpat;
416 *ptr++ = '^';
417 round = 0;
418 for (efmp = efm; efmp < efm + len; ++efmp)
419 {
420 if (*efmp == '%')
421 {
422 ++efmp;
423 for (idx = 0; idx < FMT_PATTERNS; ++idx)
424 if (fmt_pat[idx].convchar == *efmp)
425 break;
426 if (idx < FMT_PATTERNS)
427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100428 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200429 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200430 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200431 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200432 }
433 else if (*efmp == '*')
434 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200435 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100436 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200437 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200438 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200439 }
440 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200441 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 else if (*efmp == '#')
443 *ptr++ = '*';
444 else if (*efmp == '>')
445 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200446 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200448 // prefix is allowed only at the beginning of the errorformat
449 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100450 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200451 if (efmp == NULL)
452 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 }
454 else
455 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000456 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200457 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458 }
459 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200460 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200461 {
462 if (*efmp == '\\' && efmp + 1 < efm + len)
463 ++efmp;
464 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200465 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200466 if (*efmp)
467 *ptr++ = *efmp;
468 }
469 }
470 *ptr++ = '$';
471 *ptr = NUL;
472
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200473 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200474}
475
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200476/*
477 * Free the 'errorformat' information list
478 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200479 static void
480free_efm_list(efm_T **efm_first)
481{
482 efm_T *efm_ptr;
483
484 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
485 {
486 *efm_first = efm_ptr->next;
487 vim_regfree(efm_ptr->prog);
488 vim_free(efm_ptr);
489 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100490 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200491}
492
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200493/*
494 * Compute the size of the buffer used to convert a 'errorformat' pattern into
495 * a regular expression pattern.
496 */
497 static int
498efm_regpat_bufsz(char_u *efm)
499{
500 int sz;
501 int i;
502
503 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
504 for (i = FMT_PATTERNS; i > 0; )
505 sz += (int)STRLEN(fmt_pat[--i].pattern);
506#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200507 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200508#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200509 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200510#endif
511
512 return sz;
513}
514
515/*
516 * Return the length of a 'errorformat' option part (separated by ",").
517 */
518 static int
519efm_option_part_len(char_u *efm)
520{
521 int len;
522
523 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
524 if (efm[len] == '\\' && efm[len + 1] != NUL)
525 ++len;
526
527 return len;
528}
529
530/*
531 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
532 * are parsed and converted to regular expressions. Returns information about
533 * the parsed 'errorformat' option.
534 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200535 static efm_T *
536parse_efm_option(char_u *efm)
537{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200538 efm_T *fmt_ptr = NULL;
539 efm_T *fmt_first = NULL;
540 efm_T *fmt_last = NULL;
541 char_u *fmtstr = NULL;
542 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200543 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200545 // Each part of the format string is copied and modified from errorformat
546 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200547
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200548 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200549 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000550 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 goto parse_efm_error;
552
553 while (efm[0] != NUL)
554 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200555 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000556 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200557 if (fmt_ptr == NULL)
558 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200559 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200560 fmt_first = fmt_ptr;
561 else
562 fmt_last->next = fmt_ptr;
563 fmt_last = fmt_ptr;
564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200565 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200566 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100568 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200569 goto parse_efm_error;
570 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
571 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200572 // Advance to next part
573 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200574 }
575
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200576 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000577 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200578
579 goto parse_efm_end;
580
581parse_efm_error:
582 free_efm_list(&fmt_first);
583
584parse_efm_end:
585 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200586
587 return fmt_first;
588}
589
Bram Moolenaare0d37972016-07-15 22:36:01 +0200590enum {
591 QF_FAIL = 0,
592 QF_OK = 1,
593 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200594 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200595 QF_IGNORE_LINE = 4,
596 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200597};
598
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200599/*
600 * State information used to parse lines and add entries to a quickfix/location
601 * list.
602 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200603typedef struct {
604 char_u *linebuf;
605 int linelen;
606 char_u *growbuf;
607 int growbufsiz;
608 FILE *fd;
609 typval_T *tv;
610 char_u *p_str;
611 listitem_T *p_li;
612 buf_T *buf;
613 linenr_T buflnum;
614 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100615 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616} qfstate_T;
617
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200618/*
619 * Allocate more memory for the line buffer used for parsing lines.
620 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200621 static char_u *
622qf_grow_linebuf(qfstate_T *state, int newsz)
623{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200624 char_u *p;
625
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200626 // If the line exceeds LINE_MAXLEN exclude the last
627 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
629 if (state->growbuf == NULL)
630 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000631 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200632 if (state->growbuf == NULL)
633 return NULL;
634 state->growbufsiz = state->linelen;
635 }
636 else if (state->linelen > state->growbufsiz)
637 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200638 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200639 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200640 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200641 state->growbufsiz = state->linelen;
642 }
643 return state->growbuf;
644}
645
646/*
647 * Get the next string (separated by newline) from state->p_str.
648 */
649 static int
650qf_get_next_str_line(qfstate_T *state)
651{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200652 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200653 char_u *p_str = state->p_str;
654 char_u *p;
655 int len;
656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200657 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200658 return QF_END_OF_INPUT;
659
660 p = vim_strchr(p_str, '\n');
661 if (p != NULL)
662 len = (int)(p - p_str) + 1;
663 else
664 len = (int)STRLEN(p_str);
665
666 if (len > IOSIZE - 2)
667 {
668 state->linebuf = qf_grow_linebuf(state, len);
669 if (state->linebuf == NULL)
670 return QF_NOMEM;
671 }
672 else
673 {
674 state->linebuf = IObuff;
675 state->linelen = len;
676 }
677 vim_strncpy(state->linebuf, p_str, state->linelen);
678
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200679 // Increment using len in order to discard the rest of the
680 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 p_str += len;
682 state->p_str = p_str;
683
684 return QF_OK;
685}
686
687/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000688 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200689 */
690 static int
691qf_get_next_list_line(qfstate_T *state)
692{
693 listitem_T *p_li = state->p_li;
694 int len;
695
696 while (p_li != NULL
697 && (p_li->li_tv.v_type != VAR_STRING
698 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200699 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200700
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200701 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200702 {
703 state->p_li = NULL;
704 return QF_END_OF_INPUT;
705 }
706
707 len = (int)STRLEN(p_li->li_tv.vval.v_string);
708 if (len > IOSIZE - 2)
709 {
710 state->linebuf = qf_grow_linebuf(state, len);
711 if (state->linebuf == NULL)
712 return QF_NOMEM;
713 }
714 else
715 {
716 state->linebuf = IObuff;
717 state->linelen = len;
718 }
719
720 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200722 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200723 return QF_OK;
724}
725
726/*
727 * Get the next string from state->buf.
728 */
729 static int
730qf_get_next_buf_line(qfstate_T *state)
731{
732 char_u *p_buf = NULL;
733 int len;
734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200735 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200736 if (state->buflnum > state->lnumlast)
737 return QF_END_OF_INPUT;
738
739 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
740 state->buflnum += 1;
741
742 len = (int)STRLEN(p_buf);
743 if (len > IOSIZE - 2)
744 {
745 state->linebuf = qf_grow_linebuf(state, len);
746 if (state->linebuf == NULL)
747 return QF_NOMEM;
748 }
749 else
750 {
751 state->linebuf = IObuff;
752 state->linelen = len;
753 }
754 vim_strncpy(state->linebuf, p_buf, state->linelen);
755
756 return QF_OK;
757}
758
759/*
760 * Get the next string from file state->fd.
761 */
762 static int
763qf_get_next_file_line(qfstate_T *state)
764{
765 int discard;
766 int growbuflen;
767
768 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
769 return QF_END_OF_INPUT;
770
771 discard = FALSE;
772 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200773 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200774 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200775 // The current line exceeds IObuff, continue reading using
776 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200777 if (state->growbuf == NULL)
778 {
779 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000780 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200781 if (state->growbuf == NULL)
782 return QF_NOMEM;
783 }
784
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200785 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 memcpy(state->growbuf, IObuff, IOSIZE - 1);
787 growbuflen = state->linelen;
788
789 for (;;)
790 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200791 char_u *p;
792
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793 if (fgets((char *)state->growbuf + growbuflen,
794 state->growbufsiz - growbuflen, state->fd) == NULL)
795 break;
796 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
797 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200798 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 break;
800 if (state->growbufsiz == LINE_MAXLEN)
801 {
802 discard = TRUE;
803 break;
804 }
805
806 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
807 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200808 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200809 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200810 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200811 }
812
813 while (discard)
814 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200815 // The current line is longer than LINE_MAXLEN, continue
816 // reading but discard everything until EOL or EOF is
817 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200818 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
819 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200820 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200821 break;
822 }
823
824 state->linebuf = state->growbuf;
825 state->linelen = growbuflen;
826 }
827 else
828 state->linebuf = IObuff;
829
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200830 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200831 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
832 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100833 char_u *line;
834
835 line = string_convert(&state->vc, state->linebuf, &state->linelen);
836 if (line != NULL)
837 {
838 if (state->linelen < IOSIZE)
839 {
840 STRCPY(state->linebuf, line);
841 vim_free(line);
842 }
843 else
844 {
845 vim_free(state->growbuf);
846 state->linebuf = state->growbuf = line;
847 state->growbufsiz = state->linelen < LINE_MAXLEN
848 ? state->linelen : LINE_MAXLEN;
849 }
850 }
851 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100852
Bram Moolenaare0d37972016-07-15 22:36:01 +0200853 return QF_OK;
854}
855
856/*
857 * Get the next string from a file/buffer/list/string.
858 */
859 static int
860qf_get_nextline(qfstate_T *state)
861{
862 int status = QF_FAIL;
863
864 if (state->fd == NULL)
865 {
866 if (state->tv != NULL)
867 {
868 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200869 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200870 status = qf_get_next_str_line(state);
871 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200872 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200873 status = qf_get_next_list_line(state);
874 }
875 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200876 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200877 status = qf_get_next_buf_line(state);
878 }
879 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200880 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200881 status = qf_get_next_file_line(state);
882
883 if (status != QF_OK)
884 return status;
885
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200886 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200887 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200888 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200889 state->linebuf[state->linelen - 1] = NUL;
890#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200891 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
892 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200893#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200894 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200895
Bram Moolenaare0d37972016-07-15 22:36:01 +0200896 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200897
898 return QF_OK;
899}
900
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200901typedef struct {
902 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200903 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200904 char_u *errmsg;
905 int errmsglen;
906 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200907 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200908 int col;
thinca6864efa2021-06-19 20:45:20 +0200909 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200910 char_u use_viscol;
911 char_u *pattern;
912 int enr;
913 int type;
914 int valid;
915} qffields_T;
916
917/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200918 * Parse the match for filename ('%f') pattern in regmatch.
919 * Return the matched value in "fields->namebuf".
920 */
921 static int
922qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
923{
924 int c;
925
926 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
927 return QF_FAIL;
928
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200929 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200930 c = *rmp->endp[midx];
931 *rmp->endp[midx] = NUL;
932 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
933 *rmp->endp[midx] = c;
934
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200935 // For separate filename patterns (%O, %P and %Q), the specified file
936 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200937 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
938 && mch_getperm(fields->namebuf) == -1)
939 return QF_FAIL;
940
941 return QF_OK;
942}
943
944/*
945 * Parse the match for error number ('%n') pattern in regmatch.
946 * Return the matched value in "fields->enr".
947 */
948 static int
949qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
950{
951 if (rmp->startp[midx] == NULL)
952 return QF_FAIL;
953 fields->enr = (int)atol((char *)rmp->startp[midx]);
954 return QF_OK;
955}
956
957/*
haya14busae023d492022-02-08 18:09:29 +0000958 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200959 * Return the matched value in "fields->lnum".
960 */
961 static int
962qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
963{
964 if (rmp->startp[midx] == NULL)
965 return QF_FAIL;
966 fields->lnum = atol((char *)rmp->startp[midx]);
967 return QF_OK;
968}
969
970/*
haya14busae023d492022-02-08 18:09:29 +0000971 * Parse the match for end line number ('%e') pattern in regmatch.
972 * Return the matched value in "fields->end_lnum".
973 */
974 static int
975qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
976{
977 if (rmp->startp[midx] == NULL)
978 return QF_FAIL;
979 fields->end_lnum = atol((char *)rmp->startp[midx]);
980 return QF_OK;
981}
982
983/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200984 * Parse the match for column number ('%c') pattern in regmatch.
985 * Return the matched value in "fields->col".
986 */
987 static int
988qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
989{
990 if (rmp->startp[midx] == NULL)
991 return QF_FAIL;
992 fields->col = (int)atol((char *)rmp->startp[midx]);
993 return QF_OK;
994}
995
996/*
haya14busae023d492022-02-08 18:09:29 +0000997 * Parse the match for end column number ('%k') pattern in regmatch.
998 * Return the matched value in "fields->end_col".
999 */
1000 static int
1001qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1002{
1003 if (rmp->startp[midx] == NULL)
1004 return QF_FAIL;
1005 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1006 return QF_OK;
1007}
1008
1009/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001010 * Parse the match for error type ('%t') pattern in regmatch.
1011 * Return the matched value in "fields->type".
1012 */
1013 static int
1014qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1015{
1016 if (rmp->startp[midx] == NULL)
1017 return QF_FAIL;
1018 fields->type = *rmp->startp[midx];
1019 return QF_OK;
1020}
1021
1022/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001023 * Copy a non-error line into the error string. Return the matched line in
1024 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001025 */
1026 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001027copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001028{
1029 char_u *p;
1030
1031 if (linelen >= fields->errmsglen)
1032 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001033 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001034 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1035 return QF_NOMEM;
1036 fields->errmsg = p;
1037 fields->errmsglen = linelen + 1;
1038 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001039 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001040 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001041
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001042 return QF_OK;
1043}
1044
1045/*
1046 * Parse the match for error message ('%m') pattern in regmatch.
1047 * Return the matched value in "fields->errmsg".
1048 */
1049 static int
1050qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1051{
1052 char_u *p;
1053 int len;
1054
1055 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1056 return QF_FAIL;
1057 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1058 if (len >= fields->errmsglen)
1059 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001060 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001061 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1062 return QF_NOMEM;
1063 fields->errmsg = p;
1064 fields->errmsglen = len + 1;
1065 }
1066 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1067 return QF_OK;
1068}
1069
1070/*
1071 * Parse the match for rest of a single-line file message ('%r') pattern.
1072 * Return the matched value in "tail".
1073 */
1074 static int
1075qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1076{
1077 if (rmp->startp[midx] == NULL)
1078 return QF_FAIL;
1079 *tail = rmp->startp[midx];
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the pointer line ('%p') pattern in regmatch.
1085 * Return the matched value in "fields->col".
1086 */
1087 static int
1088qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 char_u *match_ptr;
1091
1092 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1093 return QF_FAIL;
1094 fields->col = 0;
1095 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1096 ++match_ptr)
1097 {
1098 ++fields->col;
1099 if (*match_ptr == TAB)
1100 {
1101 fields->col += 7;
1102 fields->col -= fields->col % 8;
1103 }
1104 }
1105 ++fields->col;
1106 fields->use_viscol = TRUE;
1107 return QF_OK;
1108}
1109
1110/*
1111 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1112 * Return the matched value in "fields->col".
1113 */
1114 static int
1115qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1116{
1117 if (rmp->startp[midx] == NULL)
1118 return QF_FAIL;
1119 fields->col = (int)atol((char *)rmp->startp[midx]);
1120 fields->use_viscol = TRUE;
1121 return QF_OK;
1122}
1123
1124/*
1125 * Parse the match for the search text ('%s') pattern in regmatch.
1126 * Return the matched value in "fields->pattern".
1127 */
1128 static int
1129qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1130{
1131 int len;
1132
1133 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1134 return QF_FAIL;
1135 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1136 if (len > CMDBUFFSIZE - 5)
1137 len = CMDBUFFSIZE - 5;
1138 STRCPY(fields->pattern, "^\\V");
1139 STRNCAT(fields->pattern, rmp->startp[midx], len);
1140 fields->pattern[len + 3] = '\\';
1141 fields->pattern[len + 4] = '$';
1142 fields->pattern[len + 5] = NUL;
1143 return QF_OK;
1144}
1145
1146/*
1147 * Parse the match for the module ('%o') pattern in regmatch.
1148 * Return the matched value in "fields->module".
1149 */
1150 static int
1151qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1152{
1153 int len;
1154
1155 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1156 return QF_FAIL;
1157 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1158 if (len > CMDBUFFSIZE)
1159 len = CMDBUFFSIZE;
1160 STRNCAT(fields->module, rmp->startp[midx], len);
1161 return QF_OK;
1162}
1163
1164/*
1165 * 'errorformat' format pattern parser functions.
1166 * The '%f' and '%r' formats are parsed differently from other formats.
1167 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001168 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001169 */
1170static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1171{
haya14busae023d492022-02-08 18:09:29 +00001172 NULL, // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001173 qf_parse_fmt_n,
1174 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001175 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001177 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 qf_parse_fmt_t,
1179 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001180 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 qf_parse_fmt_p,
1182 qf_parse_fmt_v,
1183 qf_parse_fmt_s,
1184 qf_parse_fmt_o
1185};
1186
1187/*
1188 * Parse the error format pattern matches in "regmatch" and set the values in
1189 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1190 * match. Returns QF_OK if all the matches are successfully parsed. On
1191 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001192 */
1193 static int
1194qf_parse_match(
1195 char_u *linebuf,
1196 int linelen,
1197 efm_T *fmt_ptr,
1198 regmatch_T *regmatch,
1199 qffields_T *fields,
1200 int qf_multiline,
1201 int qf_multiscan,
1202 char_u **tail)
1203{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001204 int idx = fmt_ptr->prefix;
1205 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001206 int midx;
1207 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001208
1209 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1210 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001211 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001212 fields->type = idx;
1213 else
1214 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001215
1216 // Extract error message data from matched line.
1217 // We check for an actual submatch, because "\[" and "\]" in
1218 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001219 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001220 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001221 status = QF_OK;
1222 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001223 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001224 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001225 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001227 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001228 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001229 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001230 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001231 }
haya14busae023d492022-02-08 18:09:29 +00001232 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001233 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001234 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001235 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001236
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001237 if (status != QF_OK)
1238 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001239 }
1240
1241 return QF_OK;
1242}
1243
1244/*
1245 * Parse an error line in 'linebuf' using a single error format string in
1246 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1247 * Returns QF_OK if the efm format matches completely and the fields are
1248 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1249 */
1250 static int
1251qf_parse_get_fields(
1252 char_u *linebuf,
1253 int linelen,
1254 efm_T *fmt_ptr,
1255 qffields_T *fields,
1256 int qf_multiline,
1257 int qf_multiscan,
1258 char_u **tail)
1259{
1260 regmatch_T regmatch;
1261 int status = QF_FAIL;
1262 int r;
1263
1264 if (qf_multiscan &&
1265 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1266 return QF_FAIL;
1267
1268 fields->namebuf[0] = NUL;
1269 fields->module[0] = NUL;
1270 fields->pattern[0] = NUL;
1271 if (!qf_multiscan)
1272 fields->errmsg[0] = NUL;
1273 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001274 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001275 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001276 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001277 fields->use_viscol = FALSE;
1278 fields->enr = -1;
1279 fields->type = 0;
1280 *tail = NULL;
1281
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001282 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001283 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001284 regmatch.regprog = fmt_ptr->prog;
1285 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1286 fmt_ptr->prog = regmatch.regprog;
1287 if (r)
1288 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1289 fields, qf_multiline, qf_multiscan, tail);
1290
1291 return status;
1292}
1293
1294/*
1295 * Parse directory error format prefixes (%D and %X).
1296 * Push and pop directories from the directory stack when scanning directory
1297 * names.
1298 */
1299 static int
1300qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1301{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001302 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001303 {
1304 if (*fields->namebuf == NUL)
1305 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001306 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001307 return QF_FAIL;
1308 }
1309 qfl->qf_directory =
1310 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1311 if (qfl->qf_directory == NULL)
1312 return QF_FAIL;
1313 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001314 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1316
1317 return QF_OK;
1318}
1319
1320/*
1321 * Parse global file name error format prefixes (%O, %P and %Q).
1322 */
1323 static int
1324qf_parse_file_pfx(
1325 int idx,
1326 qffields_T *fields,
1327 qf_list_T *qfl,
1328 char_u *tail)
1329{
1330 fields->valid = FALSE;
1331 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1332 {
1333 if (*fields->namebuf && idx == 'P')
1334 qfl->qf_currfile =
1335 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1336 else if (idx == 'Q')
1337 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1338 *fields->namebuf = NUL;
1339 if (tail && *tail)
1340 {
1341 STRMOVE(IObuff, skipwhite(tail));
1342 qfl->qf_multiscan = TRUE;
1343 return QF_MULTISCAN;
1344 }
1345 }
1346
1347 return QF_OK;
1348}
1349
1350/*
1351 * Parse a non-error line (a line which doesn't match any of the error
1352 * format in 'efm').
1353 */
1354 static int
1355qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1356{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001357 fields->namebuf[0] = NUL; // no match found, remove file name
1358 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001359 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001360
Bram Moolenaarf4140482020-02-15 23:06:45 +01001361 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001362}
1363
1364/*
1365 * Parse multi-line error format prefixes (%C and %Z)
1366 */
1367 static int
1368qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001369 int idx,
1370 qf_list_T *qfl,
1371 qffields_T *fields)
1372{
1373 char_u *ptr;
1374 int len;
1375
1376 if (!qfl->qf_multiignore)
1377 {
1378 qfline_T *qfprev = qfl->qf_last;
1379
1380 if (qfprev == NULL)
1381 return QF_FAIL;
1382 if (*fields->errmsg && !qfl->qf_multiignore)
1383 {
1384 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001385 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1386 aid_qf_multiline_pfx);
1387 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001388 return QF_FAIL;
1389 STRCPY(ptr, qfprev->qf_text);
1390 vim_free(qfprev->qf_text);
1391 qfprev->qf_text = ptr;
1392 *(ptr += len) = '\n';
1393 STRCPY(++ptr, fields->errmsg);
1394 }
1395 if (qfprev->qf_nr == -1)
1396 qfprev->qf_nr = fields->enr;
1397 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001398 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001399 qfprev->qf_type = fields->type;
1400
1401 if (!qfprev->qf_lnum)
1402 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001403 if (!qfprev->qf_end_lnum)
1404 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001405 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001406 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001407 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001408 qfprev->qf_viscol = fields->use_viscol;
1409 }
haya14busae023d492022-02-08 18:09:29 +00001410 if (!qfprev->qf_end_col)
1411 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001412 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001413 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001414 qfl->qf_directory,
1415 *fields->namebuf || qfl->qf_directory != NULL
1416 ? fields->namebuf
1417 : qfl->qf_currfile != NULL && fields->valid
1418 ? qfl->qf_currfile : 0);
1419 }
1420 if (idx == 'Z')
1421 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1422 line_breakcheck();
1423
1424 return QF_IGNORE_LINE;
1425}
1426
1427/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001428 * Parse a line and get the quickfix fields.
1429 * Return the QF_ status.
1430 */
1431 static int
1432qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001433 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 char_u *linebuf,
1435 int linelen,
1436 efm_T *fmt_first,
1437 qffields_T *fields)
1438{
1439 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001440 int idx = 0;
1441 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001442 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443
Bram Moolenaare333e792018-04-08 13:27:39 +02001444restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001445 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446 if (fmt_start == NULL)
1447 fmt_ptr = fmt_first;
1448 else
1449 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001450 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001451 fmt_ptr = fmt_start;
1452 fmt_start = NULL;
1453 }
1454
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001455 // Try to match each part of 'errorformat' until we find a complete
1456 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001457 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1459 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001461 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1462 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1463 if (status == QF_NOMEM)
1464 return status;
1465 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001468 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001469
1470 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1471 {
1472 if (fmt_ptr != NULL)
1473 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001474 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001475 status = qf_parse_dir_pfx(idx, fields, qfl);
1476 if (status != QF_OK)
1477 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001478 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001479
1480 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1481 if (status != QF_OK)
1482 return status;
1483
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001484 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001485 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001486 }
1487 else if (fmt_ptr != NULL)
1488 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001489 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001490 if (fmt_ptr->conthere)
1491 fmt_start = fmt_ptr;
1492
Bram Moolenaare9283662020-06-07 14:10:47 +02001493 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001494 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001495 qfl->qf_multiline = TRUE; // start of a multi-line message
1496 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001497 }
1498 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001499 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001500 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001501 if (status != QF_OK)
1502 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001503 }
1504 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001505 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001506 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1507 if (status == QF_MULTISCAN)
1508 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001509 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001510 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001511 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001512 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001513 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001514 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001515 return QF_IGNORE_LINE;
1516 }
1517 }
1518
1519 return QF_OK;
1520}
1521
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001522/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001523 * Returns TRUE if the specified quickfix/location stack is empty
1524 */
1525 static int
1526qf_stack_empty(qf_info_T *qi)
1527{
1528 return qi == NULL || qi->qf_listcount <= 0;
1529}
1530
1531/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001532 * Returns TRUE if the specified quickfix/location list is empty.
1533 */
1534 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001535qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001536{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001537 return qfl == NULL || qfl->qf_count <= 0;
1538}
1539
1540/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001541 * Returns TRUE if the specified quickfix/location list is not empty and
1542 * has valid entries.
1543 */
1544 static int
1545qf_list_has_valid_entries(qf_list_T *qfl)
1546{
1547 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1548}
1549
1550/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001551 * Return a pointer to a list in the specified quickfix stack
1552 */
1553 static qf_list_T *
1554qf_get_list(qf_info_T *qi, int idx)
1555{
1556 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001557}
1558
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001559/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001560 * Allocate the fields used for parsing lines and populating a quickfix list.
1561 */
1562 static int
1563qf_alloc_fields(qffields_T *pfields)
1564{
1565 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1566 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1567 pfields->errmsglen = CMDBUFFSIZE + 1;
1568 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1569 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1570 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1571 || pfields->pattern == NULL || pfields->module == NULL)
1572 return FAIL;
1573
1574 return OK;
1575}
1576
1577/*
1578 * Free the fields used for parsing lines and populating a quickfix list.
1579 */
1580 static void
1581qf_free_fields(qffields_T *pfields)
1582{
1583 vim_free(pfields->namebuf);
1584 vim_free(pfields->module);
1585 vim_free(pfields->errmsg);
1586 vim_free(pfields->pattern);
1587}
1588
1589/*
1590 * Setup the state information used for parsing lines and populating a
1591 * quickfix list.
1592 */
1593 static int
1594qf_setup_state(
1595 qfstate_T *pstate,
1596 char_u *enc,
1597 char_u *efile,
1598 typval_T *tv,
1599 buf_T *buf,
1600 linenr_T lnumfirst,
1601 linenr_T lnumlast)
1602{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001603 pstate->vc.vc_type = CONV_NONE;
1604 if (enc != NULL && *enc != NUL)
1605 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001606
1607 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1608 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001609 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001610 return FAIL;
1611 }
1612
1613 if (tv != NULL)
1614 {
1615 if (tv->v_type == VAR_STRING)
1616 pstate->p_str = tv->vval.v_string;
1617 else if (tv->v_type == VAR_LIST)
1618 pstate->p_li = tv->vval.v_list->lv_first;
1619 pstate->tv = tv;
1620 }
1621 pstate->buf = buf;
1622 pstate->buflnum = lnumfirst;
1623 pstate->lnumlast = lnumlast;
1624
1625 return OK;
1626}
1627
1628/*
1629 * Cleanup the state information used for parsing lines and populating a
1630 * quickfix list.
1631 */
1632 static void
1633qf_cleanup_state(qfstate_T *pstate)
1634{
1635 if (pstate->fd != NULL)
1636 fclose(pstate->fd);
1637
1638 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001639 if (pstate->vc.vc_type != CONV_NONE)
1640 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001641}
1642
1643/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001644 * Process the next line from a file/buffer/list/string and add it
1645 * to the quickfix list 'qfl'.
1646 */
1647 static int
1648qf_init_process_nextline(
1649 qf_list_T *qfl,
1650 efm_T *fmt_first,
1651 qfstate_T *state,
1652 qffields_T *fields)
1653{
1654 int status;
1655
1656 // Get the next line from a file/buffer/list/string
1657 status = qf_get_nextline(state);
1658 if (status != QF_OK)
1659 return status;
1660
1661 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1662 fmt_first, fields);
1663 if (status != QF_OK)
1664 return status;
1665
1666 return qf_add_entry(qfl,
1667 qfl->qf_directory,
1668 (*fields->namebuf || qfl->qf_directory != NULL)
1669 ? fields->namebuf
1670 : ((qfl->qf_currfile != NULL && fields->valid)
1671 ? qfl->qf_currfile : (char_u *)NULL),
1672 fields->module,
1673 0,
1674 fields->errmsg,
1675 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001676 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001677 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001678 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001679 fields->use_viscol,
1680 fields->pattern,
1681 fields->enr,
1682 fields->type,
1683 fields->valid);
1684}
1685
1686/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001687 * Read the errorfile "efile" into memory, line by line, building the error
1688 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001689 * Alternative: when "efile" is NULL read errors from buffer "buf".
1690 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001691 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001692 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1693 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001694 * Return -1 for error, number of errors for success.
1695 */
1696 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001697qf_init_ext(
1698 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001699 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001700 char_u *efile,
1701 buf_T *buf,
1702 typval_T *tv,
1703 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001704 int newlist, // TRUE: start a new error list
1705 linenr_T lnumfirst, // first line number to use
1706 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001707 char_u *qf_title,
1708 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001709{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001710 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001711 qfstate_T state;
1712 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001713 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001714 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001715 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001717 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001718 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001719 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001721 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001722 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001723
Bram Moolenaara80faa82020-04-12 19:37:17 +02001724 CLEAR_FIELD(state);
1725 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001726 if ((qf_alloc_fields(&fields) == FAIL) ||
1727 (qf_setup_state(&state, enc, efile, tv, buf,
1728 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 goto qf_init_end;
1730
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001731 if (newlist || qf_idx == qi->qf_listcount)
1732 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001733 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001734 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001735 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001736 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001738 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001739 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001740 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001741 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001742 qfl = qf_get_list(qi, qf_idx);
1743 if (!qf_list_empty(qfl))
1744 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001747 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001748 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001749 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 else
1751 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001753 // If the errorformat didn't change between calls, then reuse the
1754 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001755 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1756 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001757 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001758 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001759 free_efm_list(&fmt_first);
1760
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001761 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001762 fmt_first = parse_efm_option(efm);
1763 if (fmt_first != NULL)
1764 last_efm = vim_strsave(efm);
1765 }
1766
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001767 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001770 // got_int is reset here, because it was probably set when killing the
1771 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 got_int = FALSE;
1773
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001774 // Read the lines in the error file one by one.
1775 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001776 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001778 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001779 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001780 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001781 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001782 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001783 if (status == QF_FAIL)
1784 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 line_breakcheck();
1787 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001788 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001790 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001792 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001793 qfl->qf_ptr = qfl->qf_start;
1794 qfl->qf_index = 1;
1795 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
1797 else
1798 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001799 qfl->qf_nonevalid = FALSE;
1800 if (qfl->qf_ptr == NULL)
1801 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001803 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001804 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001805 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001807 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001809 if (!adding)
1810 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001811 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001812 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001813 qi->qf_listcount--;
1814 if (qi->qf_curlist > 0)
1815 --qi->qf_curlist;
1816 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001817qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001818 if (qf_idx == qi->qf_curlist)
1819 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001820 qf_cleanup_state(&state);
1821 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823 return retval;
1824}
1825
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001826/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001827 * Read the errorfile "efile" into memory, line by line, building the error
1828 * list. Set the error list's title to qf_title.
1829 * Return -1 for error, number of errors for success.
1830 */
1831 int
1832qf_init(win_T *wp,
1833 char_u *efile,
1834 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001835 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001836 char_u *qf_title,
1837 char_u *enc)
1838{
1839 qf_info_T *qi = &ql_info;
1840
1841 if (wp != NULL)
1842 {
1843 qi = ll_get_or_alloc_list(wp);
1844 if (qi == NULL)
1845 return FAIL;
1846 }
1847
1848 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1849 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1850}
1851
1852/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001853 * Set the title of the specified quickfix list. Frees the previous title.
1854 * Prepends ':' to the title.
1855 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001856 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001857qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001858{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001859 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001860
Bram Moolenaarfb604092014-07-23 15:55:00 +02001861 if (title != NULL)
1862 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001863 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001864
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001865 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001866 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001867 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001868 }
1869}
1870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001872 * The title of a quickfix/location list is set, by default, to the command
1873 * that created the quickfix list with the ":" prefix.
1874 * Create a quickfix list title string by prepending ":" to a user command.
1875 * Returns a pointer to a static buffer with the title.
1876 */
1877 static char_u *
1878qf_cmdtitle(char_u *cmd)
1879{
1880 static char_u qftitle_str[IOSIZE];
1881
1882 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1883 return qftitle_str;
1884}
1885
1886/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001887 * Return a pointer to the current list in the specified quickfix stack
1888 */
1889 static qf_list_T *
1890qf_get_curlist(qf_info_T *qi)
1891{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001892 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001893}
1894
1895/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001896 * Prepare for adding a new quickfix list. If the current list is in the
1897 * middle of the stack, then all the following lists are freed and then
1898 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 */
1900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001901qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
1903 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001904 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001906 // If the current entry is not the last entry, delete entries beyond
1907 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001908 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001909 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001910 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001912 // When the stack is full, remove to oldest entry
1913 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001914 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001916 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001918 qi->qf_lists[i - 1] = qi->qf_lists[i];
1919 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001922 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001923 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001924 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001925 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001926 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001927 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928}
1929
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001930/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001931 * Queue location list stack delete request.
1932 */
1933 static void
1934locstack_queue_delreq(qf_info_T *qi)
1935{
1936 qf_delq_T *q;
1937
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001938 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001939 if (q != NULL)
1940 {
1941 q->qi = qi;
1942 q->next = qf_delq_head;
1943 qf_delq_head = q;
1944 }
1945}
1946
1947/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001948 * Return the global quickfix stack window buffer number.
1949 */
1950 int
1951qf_stack_get_bufnr(void)
1952{
1953 return ql_info.qf_bufnr;
1954}
1955
1956/*
1957 * Wipe the quickfix window buffer (if present) for the specified
1958 * quickfix/location list.
1959 */
1960 static void
1961wipe_qf_buffer(qf_info_T *qi)
1962{
1963 buf_T *qfbuf;
1964
1965 if (qi->qf_bufnr != INVALID_QFBUFNR)
1966 {
1967 qfbuf = buflist_findnr(qi->qf_bufnr);
1968 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1969 {
1970 // If the quickfix buffer is not loaded in any window, then
1971 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001972 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001973 qi->qf_bufnr = INVALID_QFBUFNR;
1974 }
1975 }
1976}
1977
1978/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001979 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 */
1981 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001982ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001983{
1984 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001985 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001986
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001987 qi = *pqi;
1988 if (qi == NULL)
1989 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001990 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001991
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001992 // If the location list is still in use, then queue the delete request
1993 // to be processed later.
1994 if (quickfix_busy > 0)
1995 {
1996 locstack_queue_delreq(qi);
1997 return;
1998 }
1999
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002000 qi->qf_refcount--;
2001 if (qi->qf_refcount < 1)
2002 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002003 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002004 // If the quickfix window buffer is loaded, then wipe it
2005 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002006
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002007 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002008 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002009 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002010 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002011}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002012
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002013/*
2014 * Free all the quickfix/location lists in the stack.
2015 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002016 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002017qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002018{
2019 int i;
2020 qf_info_T *qi = &ql_info;
2021
2022 if (wp != NULL)
2023 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002024 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025 ll_free_all(&wp->w_llist);
2026 ll_free_all(&wp->w_llist_ref);
2027 }
2028 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002029 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002031 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002032}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002033
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002035 * Delay freeing of location list stacks when the quickfix code is running.
2036 * Used to avoid problems with autocmds freeing location list stacks when the
2037 * quickfix code is still referencing the stack.
2038 * Must always call decr_quickfix_busy() exactly once after this.
2039 */
2040 static void
2041incr_quickfix_busy(void)
2042{
2043 quickfix_busy++;
2044}
2045
2046/*
2047 * Safe to free location list stacks. Process any delayed delete requests.
2048 */
2049 static void
2050decr_quickfix_busy(void)
2051{
2052 if (--quickfix_busy == 0)
2053 {
2054 // No longer referencing the location lists. Process all the pending
2055 // delete requests.
2056 while (qf_delq_head != NULL)
2057 {
2058 qf_delq_T *q = qf_delq_head;
2059
2060 qf_delq_head = q->next;
2061 ll_free_all(&q->qi);
2062 vim_free(q);
2063 }
2064 }
2065#ifdef ABORT_ON_INTERNAL_ERROR
2066 if (quickfix_busy < 0)
2067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002068 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002069 abort();
2070 }
2071#endif
2072}
2073
2074#if defined(EXITFREE) || defined(PROTO)
2075 void
2076check_quickfix_busy(void)
2077{
2078 if (quickfix_busy != 0)
2079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002081# ifdef ABORT_ON_INTERNAL_ERROR
2082 abort();
2083# endif
2084 }
2085}
2086#endif
2087
2088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002090 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 */
2092 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002093qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002094 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002095 char_u *dir, // optional directory name
2096 char_u *fname, // file name or NULL
2097 char_u *module, // module name or NULL
2098 int bufnum, // buffer number or zero
2099 char_u *mesg, // message
2100 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002101 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002102 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002103 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002104 int vis_col, // using visual column
2105 char_u *pattern, // search pattern
2106 int nr, // error number
2107 int type, // type character
2108 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002110 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002111 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002113 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002114 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002115 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002116 {
2117 buf_T *buf = buflist_findnr(bufnum);
2118
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002119 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002120 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002121 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002122 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002123 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002124 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002125 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2127 {
2128 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002129 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002132 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002134 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002135 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002136 if (pattern == NULL || *pattern == NUL)
2137 qfp->qf_pattern = NULL;
2138 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2139 {
2140 vim_free(qfp->qf_text);
2141 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002142 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002143 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002144 if (module == NULL || *module == NUL)
2145 qfp->qf_module = NULL;
2146 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2147 {
2148 vim_free(qfp->qf_text);
2149 vim_free(qfp->qf_pattern);
2150 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002151 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002154 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 type = 0;
2156 qfp->qf_type = type;
2157 qfp->qf_valid = valid;
2158
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002159 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002160 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002162 qfl->qf_start = qfp;
2163 qfl->qf_ptr = qfp;
2164 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002165 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167 else
2168 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002169 qfp->qf_prev = *lastp;
2170 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002172 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002174 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002175 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002176 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002178 qfl->qf_index = qfl->qf_count;
2179 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
2181
Bram Moolenaar95946f12019-03-31 15:31:59 +02002182 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183}
2184
2185/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002186 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002187 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002188 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002189qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002191 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002192
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002193 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002194 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002195 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002196 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002197 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002198 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002199 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002200 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002201}
2202
2203/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002204 * Return the location list stack for window 'wp'.
2205 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002206 */
2207 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002208ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002209{
2210 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002211 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002212 return wp->w_llist_ref;
2213
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002214 // For a non-location list window, w_llist_ref should not point to a
2215 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002216 ll_free_all(&wp->w_llist_ref);
2217
2218 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002219 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220 return wp->w_llist;
2221}
2222
2223/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002224 * Get the quickfix/location list stack to use for the specified Ex command.
2225 * For a location list command, returns the stack for the current window. If
2226 * the location list is not found, then returns NULL and prints an error
2227 * message if 'print_emsg' is TRUE.
2228 */
2229 static qf_info_T *
2230qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2231{
2232 qf_info_T *qi = &ql_info;
2233
2234 if (is_loclist_cmd(eap->cmdidx))
2235 {
2236 qi = GET_LOC_LIST(curwin);
2237 if (qi == NULL)
2238 {
2239 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002240 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002241 return NULL;
2242 }
2243 }
2244
2245 return qi;
2246}
2247
2248/*
2249 * Get the quickfix/location list stack to use for the specified Ex command.
2250 * For a location list command, returns the stack for the current window.
2251 * If the location list is not present, then allocates a new one.
2252 * Returns NULL if the allocation fails. For a location list command, sets
2253 * 'pwinp' to curwin.
2254 */
2255 static qf_info_T *
2256qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2257{
2258 qf_info_T *qi = &ql_info;
2259
2260 if (is_loclist_cmd(eap->cmdidx))
2261 {
2262 qi = ll_get_or_alloc_list(curwin);
2263 if (qi == NULL)
2264 return NULL;
2265 *pwinp = curwin;
2266 }
2267
2268 return qi;
2269}
2270
2271/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002272 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2273 */
2274 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002275copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002276{
2277 int i;
2278 qfline_T *from_qfp;
2279 qfline_T *prevp;
2280
2281 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002282 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002283 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002284 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002285 NULL,
2286 NULL,
2287 from_qfp->qf_module,
2288 0,
2289 from_qfp->qf_text,
2290 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002291 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002292 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002293 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002294 from_qfp->qf_viscol,
2295 from_qfp->qf_pattern,
2296 from_qfp->qf_nr,
2297 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002298 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002299 return FAIL;
2300
2301 // qf_add_entry() will not set the qf_num field, as the
2302 // directory and file names are not supplied. So the qf_fnum
2303 // field is copied here.
2304 prevp = to_qfl->qf_last;
2305 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2306 prevp->qf_type = from_qfp->qf_type; // error type
2307 if (from_qfl->qf_ptr == from_qfp)
2308 to_qfl->qf_ptr = prevp; // current location
2309 }
2310
2311 return OK;
2312}
2313
2314/*
2315 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2316 */
2317 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002318copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002319{
2320 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002321 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002322 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2323 to_qfl->qf_count = 0;
2324 to_qfl->qf_index = 0;
2325 to_qfl->qf_start = NULL;
2326 to_qfl->qf_last = NULL;
2327 to_qfl->qf_ptr = NULL;
2328 if (from_qfl->qf_title != NULL)
2329 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2330 else
2331 to_qfl->qf_title = NULL;
2332 if (from_qfl->qf_ctx != NULL)
2333 {
2334 to_qfl->qf_ctx = alloc_tv();
2335 if (to_qfl->qf_ctx != NULL)
2336 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2337 }
2338 else
2339 to_qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002340 if (from_qfl->qftf_cb.cb_name != NULL)
2341 copy_callback(&to_qfl->qftf_cb, &from_qfl->qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002342 else
Bram Moolenaard43906d2020-07-20 21:31:32 +02002343 to_qfl->qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002344
2345 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002346 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002347 return FAIL;
2348
2349 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2350
2351 // Assign a new ID for the location list
2352 to_qfl->qf_id = ++last_qf_id;
2353 to_qfl->qf_changedtick = 0L;
2354
2355 // When no valid entries are present in the list, qf_ptr points to
2356 // the first item in the list
2357 if (to_qfl->qf_nonevalid)
2358 {
2359 to_qfl->qf_ptr = to_qfl->qf_start;
2360 to_qfl->qf_index = 1;
2361 }
2362
2363 return OK;
2364}
2365
2366/*
2367 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002368 */
2369 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002370copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002371{
2372 qf_info_T *qi;
2373 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002374
Bram Moolenaar09037502018-09-25 22:08:14 +02002375 // When copying from a location list window, copy the referenced
2376 // location list. For other windows, copy the location list for
2377 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002378 if (IS_LL_WINDOW(from))
2379 qi = from->w_llist_ref;
2380 else
2381 qi = from->w_llist;
2382
Bram Moolenaar09037502018-09-25 22:08:14 +02002383 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002384 return;
2385
Bram Moolenaar09037502018-09-25 22:08:14 +02002386 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002387 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002388 return;
2389
2390 to->w_llist->qf_listcount = qi->qf_listcount;
2391
Bram Moolenaar09037502018-09-25 22:08:14 +02002392 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002393 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002394 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002395 to->w_llist->qf_curlist = idx;
2396
Bram Moolenaar0398e002019-03-21 21:12:49 +01002397 if (copy_loclist(qf_get_list(qi, idx),
2398 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002399 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002400 qf_free_all(to);
2401 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002402 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002403 }
2404
Bram Moolenaar09037502018-09-25 22:08:14 +02002405 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002406}
2407
2408/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002409 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002410 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 */
2412 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002413qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
Bram Moolenaar82404332016-07-10 17:00:38 +02002415 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002416 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002417 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002418
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002419 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
Bram Moolenaare60acc12011-05-10 16:41:25 +02002422#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002423 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002424#endif
2425#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002426 if (directory != NULL)
2427 slash_adjust(directory);
2428 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002429#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002430 if (directory != NULL && !vim_isAbsName(fname)
2431 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2432 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002433 // Here we check if the file really exists.
2434 // This should normally be true, but if make works without
2435 // "leaving directory"-messages we might have missed a
2436 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002437 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002440 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002441 if (directory)
2442 ptr = concat_fnames(directory, fname, TRUE);
2443 else
2444 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002446 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002447 bufname = ptr;
2448 }
2449 else
2450 bufname = fname;
2451
2452 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002453 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002454 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002455 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002456 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002458 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002459 {
2460 vim_free(qf_last_bufname);
2461 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2462 if (bufname == ptr)
2463 qf_last_bufname = bufname;
2464 else
2465 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002466 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002467 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002468 if (buf == NULL)
2469 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002470
Bram Moolenaarc1542742016-07-20 21:44:37 +02002471 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002472 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002473 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474}
2475
2476/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002477 * Push dirbuf onto the directory stack and return pointer to actual dir or
2478 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 */
2480 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002481qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 struct dir_stack_T *ds_new;
2484 struct dir_stack_T *ds_ptr;
2485
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002486 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002487 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (ds_new == NULL)
2489 return NULL;
2490
2491 ds_new->next = *stackptr;
2492 *stackptr = ds_new;
2493
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002494 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (vim_isAbsName(dirbuf)
2496 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002497 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 (*stackptr)->dirname = vim_strsave(dirbuf);
2499 else
2500 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002501 // Okay we don't have an absolute path.
2502 // dirbuf must be a subdir of one of the directories on the stack.
2503 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 ds_new = (*stackptr)->next;
2505 (*stackptr)->dirname = NULL;
2506 while (ds_new)
2507 {
2508 vim_free((*stackptr)->dirname);
2509 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2510 TRUE);
2511 if (mch_isdir((*stackptr)->dirname) == TRUE)
2512 break;
2513
2514 ds_new = ds_new->next;
2515 }
2516
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002517 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 while ((*stackptr)->next != ds_new)
2519 {
2520 ds_ptr = (*stackptr)->next;
2521 (*stackptr)->next = (*stackptr)->next->next;
2522 vim_free(ds_ptr->dirname);
2523 vim_free(ds_ptr);
2524 }
2525
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002526 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (ds_new == NULL)
2528 {
2529 vim_free((*stackptr)->dirname);
2530 (*stackptr)->dirname = vim_strsave(dirbuf);
2531 }
2532 }
2533
2534 if ((*stackptr)->dirname != NULL)
2535 return (*stackptr)->dirname;
2536 else
2537 {
2538 ds_ptr = *stackptr;
2539 *stackptr = (*stackptr)->next;
2540 vim_free(ds_ptr);
2541 return NULL;
2542 }
2543}
2544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545/*
2546 * pop dirbuf from the directory stack and return previous directory or NULL if
2547 * stack is empty
2548 */
2549 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002550qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 struct dir_stack_T *ds_ptr;
2553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002554 // TODO: Should we check if dirbuf is the directory on top of the stack?
2555 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002557 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (*stackptr != NULL)
2559 {
2560 ds_ptr = *stackptr;
2561 *stackptr = (*stackptr)->next;
2562 vim_free(ds_ptr->dirname);
2563 vim_free(ds_ptr);
2564 }
2565
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002566 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 return *stackptr ? (*stackptr)->dirname : NULL;
2568}
2569
2570/*
2571 * clean up directory stack
2572 */
2573 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002574qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 struct dir_stack_T *ds_ptr;
2577
2578 while ((ds_ptr = *stackptr) != NULL)
2579 {
2580 *stackptr = (*stackptr)->next;
2581 vim_free(ds_ptr->dirname);
2582 vim_free(ds_ptr);
2583 }
2584}
2585
2586/*
2587 * Check in which directory of the directory stack the given file can be
2588 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002589 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 * Cleans up intermediate directory entries.
2591 *
2592 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002593 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * ./
2595 * ./aa
2596 * ./aa/bb
2597 * ./bb
2598 * ./bb/x.c
2599 * and make says:
2600 * making all in aa
2601 * making all in bb
2602 * x.c:9: Error
2603 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2604 * qf_guess_filepath will return NULL.
2605 */
2606 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002607qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 struct dir_stack_T *ds_ptr;
2610 struct dir_stack_T *ds_tmp;
2611 char_u *fullname;
2612
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002613 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002614 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 return NULL;
2616
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002617 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 fullname = NULL;
2619 while (ds_ptr)
2620 {
2621 vim_free(fullname);
2622 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2623
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002624 // If concat_fnames failed, just go on. The worst thing that can happen
2625 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2627 break;
2628
2629 ds_ptr = ds_ptr->next;
2630 }
2631
2632 vim_free(fullname);
2633
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002634 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002635 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002637 ds_tmp = qfl->qf_dir_stack->next;
2638 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 vim_free(ds_tmp->dirname);
2640 vim_free(ds_tmp);
2641 }
2642
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002643 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644}
2645
2646/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002647 * Returns TRUE if a quickfix/location list with the given identifier exists.
2648 */
2649 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002650qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002651{
2652 qf_info_T *qi = &ql_info;
2653 int i;
2654
2655 if (wp != NULL)
2656 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002657 if (!win_valid(wp))
2658 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002659 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002660 if (qi == NULL)
2661 return FALSE;
2662 }
2663
2664 for (i = 0; i < qi->qf_listcount; ++i)
2665 if (qi->qf_lists[i].qf_id == qf_id)
2666 return TRUE;
2667
2668 return FALSE;
2669}
2670
2671/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002672 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002673 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002674 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002675 * Similar to location list.
2676 */
2677 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002678is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002679{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002680 qfline_T *qfp;
2681 int i;
2682
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002683 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002684 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2685 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002686 break;
2687
Bram Moolenaar95946f12019-03-31 15:31:59 +02002688 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002689 return FALSE;
2690
2691 return TRUE;
2692}
2693
2694/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002695 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002696 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002697 */
2698 static qfline_T *
2699get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002700 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002701 qfline_T *qf_ptr,
2702 int *qf_index,
2703 int dir)
2704{
2705 int idx;
2706 int old_qf_fnum;
2707
2708 idx = *qf_index;
2709 old_qf_fnum = qf_ptr->qf_fnum;
2710
2711 do
2712 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002713 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002714 return NULL;
2715 ++idx;
2716 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002717 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002718 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2719
2720 *qf_index = idx;
2721 return qf_ptr;
2722}
2723
2724/*
2725 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002726 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002727 */
2728 static qfline_T *
2729get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002730 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731 qfline_T *qf_ptr,
2732 int *qf_index,
2733 int dir)
2734{
2735 int idx;
2736 int old_qf_fnum;
2737
2738 idx = *qf_index;
2739 old_qf_fnum = qf_ptr->qf_fnum;
2740
2741 do
2742 {
2743 if (idx == 1 || qf_ptr->qf_prev == NULL)
2744 return NULL;
2745 --idx;
2746 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002747 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2749
2750 *qf_index = idx;
2751 return qf_ptr;
2752}
2753
2754/*
2755 * Get the n'th (errornr) previous/next valid entry from the current entry in
2756 * the quickfix list.
2757 * dir == FORWARD or FORWARD_FILE: next valid entry
2758 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2759 */
2760 static qfline_T *
2761get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002762 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002763 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002764 int dir,
2765 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002766{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002767 qfline_T *qf_ptr = qfl->qf_ptr;
2768 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002769 qfline_T *prev_qf_ptr;
2770 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002771 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002772
2773 while (errornr--)
2774 {
2775 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002776 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002777
2778 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002779 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002780 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002781 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002782 if (qf_ptr == NULL)
2783 {
2784 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002785 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002786 if (err != NULL)
2787 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002788 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002789 return NULL;
2790 }
2791 break;
2792 }
2793
2794 err = NULL;
2795 }
2796
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002797 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002798 return qf_ptr;
2799}
2800
2801/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002802 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2803 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002804 */
2805 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002806get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002807{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002808 qfline_T *qf_ptr = qfl->qf_ptr;
2809 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002810
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002811 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002812 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2813 {
2814 --qf_idx;
2815 qf_ptr = qf_ptr->qf_prev;
2816 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002817 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002818 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2819 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002820 {
2821 ++qf_idx;
2822 qf_ptr = qf_ptr->qf_next;
2823 }
2824
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002825 *new_qfidx = qf_idx;
2826 return qf_ptr;
2827}
2828
2829/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002830 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002831 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2832 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2833 * Returns a pointer to the entry and the index of the new entry is stored in
2834 * 'new_qfidx'.
2835 */
2836 static qfline_T *
2837qf_get_entry(
2838 qf_list_T *qfl,
2839 int errornr,
2840 int dir,
2841 int *new_qfidx)
2842{
2843 qfline_T *qf_ptr = qfl->qf_ptr;
2844 int qfidx = qfl->qf_index;
2845
2846 if (dir != 0) // next/prev valid entry
2847 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2848 else if (errornr != 0) // go to specified number
2849 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2850
2851 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002852 return qf_ptr;
2853}
2854
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002855/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002856 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002857 */
2858 static win_T *
2859qf_find_help_win(void)
2860{
2861 win_T *wp;
2862
2863 FOR_ALL_WINDOWS(wp)
2864 if (bt_help(wp->w_buffer))
2865 return wp;
2866
2867 return NULL;
2868}
2869
2870/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002871 * Set the location list for the specified window to 'qi'.
2872 */
2873 static void
2874win_set_loclist(win_T *wp, qf_info_T *qi)
2875{
2876 wp->w_llist = qi;
2877 qi->qf_refcount++;
2878}
2879
2880/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002881 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2882 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002883 */
2884 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002885jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002886{
2887 win_T *wp;
2888 int flags;
2889
Bram Moolenaare1004402020-10-24 20:49:43 +02002890 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002891 wp = NULL;
2892 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002893 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002894 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2895 win_enter(wp, TRUE);
2896 else
2897 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002898 // Split off help window; put it at far top if no position
2899 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002900 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002901 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002902 && curwin->w_width < 80)
2903 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002904 // If the user asks to open a new window, then copy the location list.
2905 // Otherwise, don't copy the location list.
2906 if (IS_LL_STACK(qi) && !newwin)
2907 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002908
2909 if (win_split(0, flags) == FAIL)
2910 return FAIL;
2911
2912 *opened_window = TRUE;
2913
2914 if (curwin->w_height < p_hh)
2915 win_setheight((int)p_hh);
2916
Bram Moolenaarb2443732018-11-11 22:50:27 +01002917 // When using location list, the new window should use the supplied
2918 // location list. If the user asks to open a new window, then the new
2919 // window will get a copy of the location list.
2920 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002921 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002922 }
2923
2924 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002925 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002926
2927 return OK;
2928}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002929
2930/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002931 * Find a non-quickfix window using the given location list stack in the
2932 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002933 * Returns NULL if a matching window is not found.
2934 */
2935 static win_T *
2936qf_find_win_with_loclist(qf_info_T *ll)
2937{
2938 win_T *wp;
2939
2940 FOR_ALL_WINDOWS(wp)
2941 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2942 return wp;
2943
2944 return NULL;
2945}
2946
2947/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002948 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002949 */
2950 static win_T *
2951qf_find_win_with_normal_buf(void)
2952{
2953 win_T *wp;
2954
2955 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002956 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 return wp;
2958
2959 return NULL;
2960}
2961
2962/*
2963 * Go to a window in any tabpage containing the specified file. Returns TRUE
2964 * if successfully jumped to the window. Otherwise returns FALSE.
2965 */
2966 static int
2967qf_goto_tabwin_with_file(int fnum)
2968{
2969 tabpage_T *tp;
2970 win_T *wp;
2971
2972 FOR_ALL_TAB_WINDOWS(tp, wp)
2973 if (wp->w_buffer->b_fnum == fnum)
2974 {
2975 goto_tabpage_win(tp, wp);
2976 return TRUE;
2977 }
2978
2979 return FALSE;
2980}
2981
2982/*
2983 * Create a new window to show a file above the quickfix window. Called when
2984 * only the quickfix window is present.
2985 */
2986 static int
2987qf_open_new_file_win(qf_info_T *ll_ref)
2988{
2989 int flags;
2990
2991 flags = WSP_ABOVE;
2992 if (ll_ref != NULL)
2993 flags |= WSP_NEWLOC;
2994 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002995 return FAIL; // not enough room for window
2996 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002997 swb_flags = 0;
2998 RESET_BINDING(curwin);
2999 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003000 // The new window should use the location list from the
3001 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003002 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003003 return OK;
3004}
3005
3006/*
3007 * Go to a window that shows the right buffer. If the window is not found, go
3008 * to the window just above the location list window. This is used for opening
3009 * a file from a location window and not from a quickfix window. If some usable
3010 * window is previously found, then it is supplied in 'use_win'.
3011 */
3012 static void
3013qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3014{
3015 win_T *win = use_win;
3016
3017 if (win == NULL)
3018 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003019 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003020 FOR_ALL_WINDOWS(win)
3021 if (win->w_buffer->b_fnum == qf_fnum)
3022 break;
3023 if (win == NULL)
3024 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003025 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003026 win = curwin;
3027 do
3028 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003029 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003030 break;
3031 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003032 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003033 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003034 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003035 } while (win != curwin);
3036 }
3037 }
3038 win_goto(win);
3039
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003040 // If the location list for the window is not set, then set it
3041 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003042 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003043 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003044}
3045
3046/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003047 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3048 * not found, then go to the window just above the quickfix window. This is
3049 * used for opening a file from a quickfix window and not from a location
3050 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003051 */
3052 static void
3053qf_goto_win_with_qfl_file(int qf_fnum)
3054{
3055 win_T *win;
3056 win_T *altwin;
3057
3058 win = curwin;
3059 altwin = NULL;
3060 for (;;)
3061 {
3062 if (win->w_buffer->b_fnum == qf_fnum)
3063 break;
3064 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003065 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003066 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003067 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003068
3069 if (IS_QF_WINDOW(win))
3070 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003071 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003072 // window, unless 'switchbuf' contains 'uselast': in this case we
3073 // try to jump to the previously used window first.
3074 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3075 win = prevwin;
3076 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003077 win = altwin;
3078 else if (curwin->w_prev != NULL)
3079 win = curwin->w_prev;
3080 else
3081 win = curwin->w_next;
3082 break;
3083 }
3084
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003085 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003086 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003087 altwin = win;
3088 }
3089
3090 win_goto(win);
3091}
3092
3093/*
3094 * Find a suitable window for opening a file (qf_fnum) from the
3095 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003096 * window, jump to it. Otherwise open a new window to display the file. If
3097 * 'newwin' is TRUE, then always open a new window. This is called from either
3098 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003099 */
3100 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003101qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003102{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003103 win_T *usable_wp = NULL;
3104 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003105 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003106
Bram Moolenaarb2443732018-11-11 22:50:27 +01003107 // If opening a new window, then don't use the location list referred by
3108 // the current window. Otherwise two windows will refer to the same
3109 // location list.
3110 if (!newwin)
3111 ll_ref = curwin->w_llist_ref;
3112
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003113 if (ll_ref != NULL)
3114 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003115 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003116 usable_wp = qf_find_win_with_loclist(ll_ref);
3117 if (usable_wp != NULL)
3118 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003119 }
3120
3121 if (!usable_win)
3122 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003123 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003124 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003125 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003126 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003127 }
3128
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003129 // If no usable window is found and 'switchbuf' contains "usetab"
3130 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003131 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003132 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003133
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003134 // If there is only one window and it is the quickfix window, create a
3135 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003136 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003137 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003138 if (qf_open_new_file_win(ll_ref) != OK)
3139 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003140 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003141 }
3142 else
3143 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003144 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003145 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003146 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003147 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003148 }
3149
3150 return OK;
3151}
3152
3153/*
3154 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003155 * Returns OK if successfully edited the file, FAIL on failing to open the
3156 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3157 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003158 */
3159 static int
3160qf_jump_edit_buffer(
3161 qf_info_T *qi,
3162 qfline_T *qf_ptr,
3163 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003164 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003165 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003166{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003167 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003168 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003169 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003170 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003171 int old_qf_curlist = qi->qf_curlist;
3172 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003173
3174 if (qf_ptr->qf_type == 1)
3175 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003176 // Open help file (do_ecmd() will set b_help flag, readfile() will
3177 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003178 if (!can_abandon(curbuf, forceit))
3179 {
3180 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003181 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003183
3184 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3185 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003186 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003187 }
3188 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003189 retval = buflist_getfile(qf_ptr->qf_fnum,
3190 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003191
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003192 // If a location list, check whether the associated window is still
3193 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003194 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003195 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003196 win_T *wp = win_id2wp(prev_winid);
3197 if (wp == NULL && curwin->w_llist != qi)
3198 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003199 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003200 *opened_window = FALSE;
3201 return NOTDONE;
3202 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003203 }
3204
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003205 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003206 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003207 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003208 return NOTDONE;
3209 }
3210
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003211 // Check if the list was changed. The pointers may happen to be identical,
3212 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003213 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003214 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003215 || !is_qf_entry_present(qfl, qf_ptr))
3216 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003217 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003218 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003219 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003220 emsg(_(e_current_location_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003221 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003222 }
3223
3224 return retval;
3225}
3226
3227/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003228 * Go to the error line in the current file using either line/column number or
3229 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003230 */
3231 static void
3232qf_jump_goto_line(
3233 linenr_T qf_lnum,
3234 int qf_col,
3235 char_u qf_viscol,
3236 char_u *qf_pattern)
3237{
3238 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003239
3240 if (qf_pattern == NULL)
3241 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003242 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003243 i = qf_lnum;
3244 if (i > 0)
3245 {
3246 if (i > curbuf->b_ml.ml_line_count)
3247 i = curbuf->b_ml.ml_line_count;
3248 curwin->w_cursor.lnum = i;
3249 }
3250 if (qf_col > 0)
3251 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003252 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003253 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003254 coladvance(qf_col - 1);
3255 else
3256 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003257 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003258 check_cursor();
3259 }
3260 else
3261 beginline(BL_WHITE | BL_FIX);
3262 }
3263 else
3264 {
3265 pos_T save_cursor;
3266
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003267 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003268 save_cursor = curwin->w_cursor;
3269 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003270 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003271 curwin->w_cursor = save_cursor;
3272 }
3273}
3274
3275/*
3276 * Display quickfix list index and size message
3277 */
3278 static void
3279qf_jump_print_msg(
3280 qf_info_T *qi,
3281 int qf_index,
3282 qfline_T *qf_ptr,
3283 buf_T *old_curbuf,
3284 linenr_T old_lnum)
3285{
3286 linenr_T i;
3287 int len;
3288
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003289 // Update the screen before showing the message, unless the screen
3290 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003291 if (!msg_scrolled)
3292 update_topline_redraw();
3293 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003294 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003295 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3296 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003297 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003298 len = (int)STRLEN(IObuff);
3299 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3300
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003301 // Output the message. Overwrite to avoid scrolling when the 'O'
3302 // flag is present in 'shortmess'; But when not jumping, print the
3303 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003304 i = msg_scroll;
3305 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3306 msg_scroll = TRUE;
3307 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3308 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003309 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003310 msg_scroll = i;
3311}
3312
3313/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003314 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003315 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3316 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003317 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3318 * able to jump/open a window. Returns NOTDONE if a file is not associated
3319 * with the entry.
3320 */
3321 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003322qf_jump_open_window(
3323 qf_info_T *qi,
3324 qfline_T *qf_ptr,
3325 int newwin,
3326 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003327{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003328 qf_list_T *qfl = qf_get_curlist(qi);
3329 int old_changedtick = qfl->qf_changedtick;
3330 int old_qf_curlist = qi->qf_curlist;
3331 qfltype_T qfl_type = qfl->qfl_type;
3332
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003333 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003334 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3335 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003336 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003337 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003338 if (old_qf_curlist != qi->qf_curlist
3339 || old_changedtick != qfl->qf_changedtick
3340 || !is_qf_entry_present(qfl, qf_ptr))
3341 {
3342 if (qfl_type == QFLT_QUICKFIX)
3343 emsg(_(e_current_quickfix_list_was_changed));
3344 else
3345 emsg(_(e_current_location_list_was_changed));
3346 return FAIL;
3347 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003348
3349 // If currently in the quickfix window, find another window to show the
3350 // file in.
3351 if (bt_quickfix(curbuf) && !*opened_window)
3352 {
3353 // If there is no file specified, we don't know where to go.
3354 // But do advance, otherwise ":cn" gets stuck.
3355 if (qf_ptr->qf_fnum == 0)
3356 return NOTDONE;
3357
Bram Moolenaarb2443732018-11-11 22:50:27 +01003358 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3359 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003360 return FAIL;
3361 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003362 if (old_qf_curlist != qi->qf_curlist
3363 || old_changedtick != qfl->qf_changedtick
3364 || !is_qf_entry_present(qfl, qf_ptr))
3365 {
3366 if (qfl_type == QFLT_QUICKFIX)
3367 emsg(_(e_current_quickfix_list_was_changed));
3368 else
3369 emsg(_(e_current_location_list_was_changed));
3370 return FAIL;
3371 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003372
3373 return OK;
3374}
3375
3376/*
3377 * Edit a selected file from the quickfix/location list and jump to a
3378 * particular line/column, adjust the folds and display a message about the
3379 * jump.
3380 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3381 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3382 * the file.
3383 */
3384 static int
3385qf_jump_to_buffer(
3386 qf_info_T *qi,
3387 int qf_index,
3388 qfline_T *qf_ptr,
3389 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003390 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003391 int *opened_window,
3392 int openfold,
3393 int print_message)
3394{
3395 buf_T *old_curbuf;
3396 linenr_T old_lnum;
3397 int retval = OK;
3398
3399 // If there is a file name, read the wanted file if needed, and check
3400 // autowrite etc.
3401 old_curbuf = curbuf;
3402 old_lnum = curwin->w_cursor.lnum;
3403
3404 if (qf_ptr->qf_fnum != 0)
3405 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003406 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003407 opened_window);
3408 if (retval != OK)
3409 return retval;
3410 }
3411
3412 // When not switched to another buffer, still need to set pc mark
3413 if (curbuf == old_curbuf)
3414 setpcmark();
3415
3416 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3417 qf_ptr->qf_pattern);
3418
3419#ifdef FEAT_FOLDING
3420 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3421 foldOpenCursor();
3422#endif
3423 if (print_message)
3424 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3425
3426 return retval;
3427}
3428
3429/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003430 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 */
3432 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003433qf_jump(qf_info_T *qi,
3434 int dir,
3435 int errornr,
3436 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003438 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3439}
3440
3441/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003442 * Jump to a quickfix line.
3443 * If dir == 0 go to entry "errornr".
3444 * If dir == FORWARD go "errornr" valid entries forward.
3445 * If dir == BACKWARD go "errornr" valid entries backward.
3446 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3447 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3448 * else if "errornr" is zero, redisplay the same line
3449 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003450 * If 'newwin' is TRUE, then open the file in a new window.
3451 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003452 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003453qf_jump_newwin(qf_info_T *qi,
3454 int dir,
3455 int errornr,
3456 int forceit,
3457 int newwin)
3458{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003459 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003460 qfline_T *qf_ptr;
3461 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003464 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003465 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003466 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003469 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003470 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003472 if (qi == NULL)
3473 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003474
Bram Moolenaar0398e002019-03-21 21:12:49 +01003475 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003477 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 return;
3479 }
3480
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003481 incr_quickfix_busy();
3482
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003483 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003484
3485 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003487 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003489
3490 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3491 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003493 qf_ptr = old_qf_ptr;
3494 qf_index = old_qf_index;
3495 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003498 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003499 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003500 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003501 // No need to print the error message if it's visible in the error
3502 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 print_message = FALSE;
3504
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003505 prev_winid = curwin->w_id;
3506
Bram Moolenaarb2443732018-11-11 22:50:27 +01003507 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003508 if (retval == FAIL)
3509 goto failed;
3510 if (retval == NOTDONE)
3511 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003512
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003513 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003514 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003515 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003517 // Quickfix/location list is freed by an autocmd
3518 qi = NULL;
3519 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003522 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003525 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003526 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003528 // Couldn't open file, so put index back where it was. This could
3529 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 qf_ptr = old_qf_ptr;
3532 qf_index = old_qf_index;
3533 }
3534 }
3535theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003536 if (qi != NULL)
3537 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003538 qfl->qf_ptr = qf_ptr;
3539 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003540 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003541 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003543 // Restore old 'switchbuf' value, but not when an autocommand or
3544 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003545 p_swb = old_swb;
3546 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003548 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003551// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003552static int qfFileAttr;
3553static int qfSepAttr;
3554static int qfLineAttr;
3555
3556/*
3557 * Display information about a single entry from the quickfix/location list.
3558 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003559 * 'cursel' will be set to TRUE for the currently selected entry in the
3560 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003561 */
3562 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003563qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003564{
3565 char_u *fname;
3566 buf_T *buf;
3567 int filter_entry;
3568
3569 fname = NULL;
3570 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3571 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3572 (char *)qfp->qf_module);
3573 else {
3574 if (qfp->qf_fnum != 0
3575 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3576 {
3577 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003578 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003579 fname = gettail(fname);
3580 }
3581 if (fname == NULL)
3582 sprintf((char *)IObuff, "%2d", qf_idx);
3583 else
3584 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3585 qf_idx, (char *)fname);
3586 }
3587
3588 // Support for filtering entries using :filter /pat/ clist
3589 // Match against the module name, file name, search pattern and
3590 // text of the entry.
3591 filter_entry = TRUE;
3592 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3593 filter_entry &= message_filtered(qfp->qf_module);
3594 if (filter_entry && fname != NULL)
3595 filter_entry &= message_filtered(fname);
3596 if (filter_entry && qfp->qf_pattern != NULL)
3597 filter_entry &= message_filtered(qfp->qf_pattern);
3598 if (filter_entry)
3599 filter_entry &= message_filtered(qfp->qf_text);
3600 if (filter_entry)
3601 return;
3602
3603 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003604 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003605
3606 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003607 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003608 if (qfp->qf_lnum == 0)
3609 IObuff[0] = NUL;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003610 else
thinca6864efa2021-06-19 20:45:20 +02003611 qf_range_text(qfp, IObuff, IOSIZE);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003612 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3613 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003614 msg_puts_attr((char *)IObuff, qfLineAttr);
3615 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003616 if (qfp->qf_pattern != NULL)
3617 {
3618 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003619 msg_puts((char *)IObuff);
3620 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003621 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003622 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003623
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003624 // Remove newlines and leading whitespace from the text. For an
3625 // unrecognized line keep the indent, the compiler may mark a word
3626 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003627 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3628 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3629 IObuff, IOSIZE);
3630 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003631 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003632}
3633
3634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003636 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 */
3638 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003639qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003641 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003642 qfline_T *qfp;
3643 int i;
3644 int idx1 = 1;
3645 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003646 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003647 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003648 int all = eap->forceit; // if not :cl!, only show
3649 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003650 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003652 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3653 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003654
Bram Moolenaar0398e002019-03-21 21:12:49 +01003655 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003657 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return;
3659 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003660 if (*arg == '+')
3661 {
3662 ++arg;
3663 plus = TRUE;
3664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3666 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003667 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 return;
3669 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003670 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003671 if (plus)
3672 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003673 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003674 idx2 = i + idx1;
3675 idx1 = i;
3676 }
3677 else
3678 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003679 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003680 if (idx1 < 0)
3681 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3682 if (idx2 < 0)
3683 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003686 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003687 shorten_fnames(FALSE);
3688
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003689 // Get the attributes for the different quickfix highlight items. Note
3690 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003691 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3692 if (qfFileAttr == 0)
3693 qfFileAttr = HL_ATTR(HLF_D);
3694 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3695 if (qfSepAttr == 0)
3696 qfSepAttr = HL_ATTR(HLF_D);
3697 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3698 if (qfLineAttr == 0)
3699 qfLineAttr = HL_ATTR(HLF_N);
3700
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003701 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003703 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
3705 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003706 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 ui_breakcheck();
3709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710}
3711
3712/*
3713 * Remove newlines and leading whitespace from an error message.
3714 * Put the result in "buf[bufsize]".
3715 */
3716 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003717qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
3719 int i;
3720 char_u *p = text;
3721
3722 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3723 {
3724 if (*p == '\n')
3725 {
3726 buf[i] = ' ';
3727 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003728 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 break;
3730 }
3731 else
3732 buf[i] = *p++;
3733 }
3734 buf[i] = NUL;
3735}
3736
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003737/*
thinca6864efa2021-06-19 20:45:20 +02003738 * Range information from lnum, col, end_lnum, and end_col.
3739 * Put the result in "buf[bufsize]".
3740 */
3741 static void
3742qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3743{
3744 int len;
3745 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3746 len = (int)STRLEN(buf);
3747
3748 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3749 {
3750 vim_snprintf((char *)buf + len, bufsize - len,
3751 "-%ld", qfp->qf_end_lnum);
3752 len += (int)STRLEN(buf + len);
3753 }
3754 if (qfp->qf_col > 0)
3755 {
3756 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3757 len += (int)STRLEN(buf + len);
3758 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3759 {
3760 vim_snprintf((char *)buf + len, bufsize - len,
3761 "-%d", qfp->qf_end_col);
3762 len += (int)STRLEN(buf + len);
3763 }
3764 }
3765 buf[len] = NUL;
3766}
3767
3768/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003769 * Display information (list number, list size and the title) about a
3770 * quickfix/location list.
3771 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003772 static void
3773qf_msg(qf_info_T *qi, int which, char *lead)
3774{
3775 char *title = (char *)qi->qf_lists[which].qf_title;
3776 int count = qi->qf_lists[which].qf_count;
3777 char_u buf[IOSIZE];
3778
3779 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3780 lead,
3781 which + 1,
3782 qi->qf_listcount,
3783 count);
3784
3785 if (title != NULL)
3786 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003787 size_t len = STRLEN(buf);
3788
3789 if (len < 34)
3790 {
3791 vim_memset(buf + len, ' ', 34 - len);
3792 buf[34] = NUL;
3793 }
3794 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003795 }
3796 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003797 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003798}
3799
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800/*
3801 * ":colder [count]": Up in the quickfix stack.
3802 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003803 * ":lolder [count]": Up in the location list stack.
3804 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 */
3806 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003807qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003809 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 int count;
3811
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003812 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3813 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003814
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (eap->addr_count != 0)
3816 count = eap->line2;
3817 else
3818 count = 1;
3819 while (count--)
3820 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003821 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003823 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003825 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003826 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003828 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 else
3831 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003832 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003834 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003837 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003840 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003841 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842}
3843
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003844/*
3845 * Display the information about all the quickfix/location lists in the stack
3846 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003847 void
3848qf_history(exarg_T *eap)
3849{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003850 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003851 int i;
3852
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003853 if (eap->addr_count > 0)
3854 {
3855 if (qi == NULL)
3856 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003857 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003858 return;
3859 }
3860
3861 // Jump to the specified quickfix list
3862 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3863 {
3864 qi->qf_curlist = eap->line2 - 1;
3865 qf_msg(qi, qi->qf_curlist, "");
3866 qf_update_buffer(qi, NULL);
3867 }
3868 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003869 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003870
3871 return;
3872 }
3873
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003874 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003875 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003876 else
3877 for (i = 0; i < qi->qf_listcount; ++i)
3878 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3879}
3880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003882 * Free all the entries in the error list "idx". Note that other information
3883 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 */
3885 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003886qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003888 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003889 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003890 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003892 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003894 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003895 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003896 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003897 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003898 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003899 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003900 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003901 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003902 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003903 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003904 // Somehow qf_count may have an incorrect value, set it to 1
3905 // to avoid crashing when it's wrong.
3906 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003907 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003908 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003909 qfl->qf_start = qfpnext;
3910 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003912
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003913 qfl->qf_index = 0;
3914 qfl->qf_start = NULL;
3915 qfl->qf_last = NULL;
3916 qfl->qf_ptr = NULL;
3917 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003918
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003919 qf_clean_dir_stack(&qfl->qf_dir_stack);
3920 qfl->qf_directory = NULL;
3921 qf_clean_dir_stack(&qfl->qf_file_stack);
3922 qfl->qf_currfile = NULL;
3923 qfl->qf_multiline = FALSE;
3924 qfl->qf_multiignore = FALSE;
3925 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926}
3927
3928/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003929 * Free error list "idx". Frees all the entries in the quickfix list,
3930 * associated context information and the title.
3931 */
3932 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003933qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003934{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003935 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003936
Bram Moolenaard23a8232018-02-10 18:45:26 +01003937 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003938 free_tv(qfl->qf_ctx);
3939 qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02003940 free_callback(&qfl->qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003941 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003942 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003943}
3944
3945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 * qf_mark_adjust: adjust marks
3947 */
3948 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003949qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003950 win_T *wp,
3951 linenr_T line1,
3952 linenr_T line2,
3953 long amount,
3954 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003956 int i;
3957 qfline_T *qfp;
3958 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003959 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003960 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003961 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
Bram Moolenaarc1542742016-07-20 21:44:37 +02003963 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003964 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003965 if (wp != NULL)
3966 {
3967 if (wp->w_llist == NULL)
3968 return;
3969 qi = wp->w_llist;
3970 }
3971
3972 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003973 {
3974 qf_list_T *qfl = qf_get_list(qi, idx);
3975
3976 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003977 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (qfp->qf_fnum == curbuf->b_fnum)
3979 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003980 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3982 {
3983 if (amount == MAXLNUM)
3984 qfp->qf_cleared = TRUE;
3985 else
3986 qfp->qf_lnum += amount;
3987 }
3988 else if (amount_after && qfp->qf_lnum > line2)
3989 qfp->qf_lnum += amount_after;
3990 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003991 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003992
3993 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003994 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995}
3996
3997/*
3998 * Make a nice message out of the error character and the error number:
3999 * char number message
4000 * e or E 0 " error"
4001 * w or W 0 " warning"
4002 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004003 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * 0 0 ""
4005 * other 0 " c"
4006 * e or E n " error n"
4007 * w or W n " warning n"
4008 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004009 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 * 0 n " error n"
4011 * other n " c n"
4012 * 1 x "" :helpgrep
4013 */
4014 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004015qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016{
4017 static char_u buf[20];
4018 static char_u cc[3];
4019 char_u *p;
4020
4021 if (c == 'W' || c == 'w')
4022 p = (char_u *)" warning";
4023 else if (c == 'I' || c == 'i')
4024 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004025 else if (c == 'N' || c == 'n')
4026 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4028 p = (char_u *)" error";
4029 else if (c == 0 || c == 1)
4030 p = (char_u *)"";
4031 else
4032 {
4033 cc[0] = ' ';
4034 cc[1] = c;
4035 cc[2] = NUL;
4036 p = cc;
4037 }
4038
4039 if (nr <= 0)
4040 return p;
4041
4042 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4043 return buf;
4044}
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004047 * When "split" is FALSE: Open the entry/result under the cursor.
4048 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4049 */
4050 void
4051qf_view_result(int split)
4052{
4053 qf_info_T *qi = &ql_info;
4054
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004055 if (IS_LL_WINDOW(curwin))
4056 qi = GET_LOC_LIST(curwin);
4057
Bram Moolenaar0398e002019-03-21 21:12:49 +01004058 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004059 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004060 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004061 return;
4062 }
4063
4064 if (split)
4065 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004066 // Open the selected entry in a new window
4067 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4068 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004069 return;
4070 }
4071
4072 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4073}
4074
4075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 * ":cwindow": open the quickfix window if we have errors to display,
4077 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004078 * ":lwindow": open the location list window if we have locations to display,
4079 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 */
4081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004082ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004084 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004085 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 win_T *win;
4087
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004088 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4089 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004090
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004091 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004092
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004093 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004094 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004096 // If a quickfix window is open but we have no errors to display,
4097 // close the window. If a quickfix window is not open, then open
4098 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004099 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004100 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004101 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
4103 if (win != NULL)
4104 ex_cclose(eap);
4105 }
4106 else if (win == NULL)
4107 ex_copen(eap);
4108}
4109
4110/*
4111 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004112 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004115ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004117 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004118 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004120 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4121 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004123 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004124 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (win != NULL)
4126 win_close(win, FALSE);
4127}
4128
4129/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004130 * Set "w:quickfix_title" if "qi" has a title.
4131 */
4132 static void
4133qf_set_title_var(qf_list_T *qfl)
4134{
4135 if (qfl->qf_title != NULL)
4136 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4137}
4138
4139/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004140 * Goto a quickfix or location list window (if present).
4141 * Returns OK if the window is found, FAIL otherwise.
4142 */
4143 static int
4144qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4145{
4146 win_T *win;
4147
4148 win = qf_find_win(qi);
4149 if (win == NULL)
4150 return FAIL;
4151
4152 win_goto(win);
4153 if (resize)
4154 {
4155 if (vertsplit)
4156 {
4157 if (sz != win->w_width)
4158 win_setwidth(sz);
4159 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004160 else if (sz != win->w_height && win->w_height
4161 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004162 win_setheight(sz);
4163 }
4164
4165 return OK;
4166}
4167
4168/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004169 * Set options for the buffer in the quickfix or location list window.
4170 */
4171 static void
4172qf_set_cwindow_options(void)
4173{
4174 // switch off 'swapfile'
4175 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4176 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4177 OPT_LOCAL);
4178 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4179 RESET_BINDING(curwin);
4180#ifdef FEAT_DIFF
4181 curwin->w_p_diff = FALSE;
4182#endif
4183#ifdef FEAT_FOLDING
4184 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4185 OPT_LOCAL);
4186#endif
4187}
4188
4189/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004190 * Open a new quickfix or location list window, load the quickfix buffer and
4191 * set the appropriate options for the window.
4192 * Returns FAIL if the window could not be opened.
4193 */
4194 static int
4195qf_open_new_cwindow(qf_info_T *qi, int height)
4196{
4197 buf_T *qf_buf;
4198 win_T *oldwin = curwin;
4199 tabpage_T *prevtab = curtab;
4200 int flags = 0;
4201 win_T *win;
4202
4203 qf_buf = qf_find_buf(qi);
4204
4205 // The current window becomes the previous window afterwards.
4206 win = curwin;
4207
Bram Moolenaare1004402020-10-24 20:49:43 +02004208 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004209 // Create the new quickfix window at the very bottom, except when
4210 // :belowright or :aboveleft is used.
4211 win_goto(lastwin);
4212 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004213 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004214 flags = WSP_BELOW;
4215 flags |= WSP_NEWLOC;
4216 if (win_split(height, flags) == FAIL)
4217 return FAIL; // not enough room for window
4218 RESET_BINDING(curwin);
4219
4220 if (IS_LL_STACK(qi))
4221 {
4222 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004223 // location list stack from the window 'win'.
4224 curwin->w_llist_ref = qi;
4225 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004226 }
4227
4228 if (oldwin != curwin)
4229 oldwin = NULL; // don't store info when in another window
4230 if (qf_buf != NULL)
4231 {
4232 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004233 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004234 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004235 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004236 }
4237 else
4238 {
4239 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004240 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4241 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004242 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004243
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004244 // save the number of the new buffer
4245 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004246 }
4247
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004248 // Set the options for the quickfix buffer/window (if not already done)
4249 // Do this even if the quickfix buffer was already present, as an autocmd
4250 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004251 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004252 qf_set_cwindow_options();
4253
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004254 // Only set the height when still in the same tab page and there is no
4255 // window to the side.
4256 if (curtab == prevtab && curwin->w_width == Columns)
4257 win_setheight(height);
4258 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4259 if (win_valid(win))
4260 prevwin = win;
4261
4262 return OK;
4263}
4264
4265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004267 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 */
4269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004270ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004272 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004273 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004275 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004276 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004278 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4279 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004280
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004281 incr_quickfix_busy();
4282
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 if (eap->addr_count != 0)
4284 height = eap->line2;
4285 else
4286 height = QF_WINHEIGHT;
4287
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004288 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289#ifdef FEAT_GUI
4290 need_mouse_correct = TRUE;
4291#endif
4292
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004293 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004294 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004295 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004296 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004297 if (status == FAIL)
4298 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004299 {
4300 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004301 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004304 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004305 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004306 // Save the current index here, as updating the quickfix buffer may free
4307 // the quickfix list
4308 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004309
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004310 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004311 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004313 decr_quickfix_busy();
4314
4315 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 curwin->w_cursor.col = 0;
4317 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004318 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319}
4320
4321/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004322 * Move the cursor in the quickfix window to "lnum".
4323 */
4324 static void
4325qf_win_goto(win_T *win, linenr_T lnum)
4326{
4327 win_T *old_curwin = curwin;
4328
4329 curwin = win;
4330 curbuf = win->w_buffer;
4331 curwin->w_cursor.lnum = lnum;
4332 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004333 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004334 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004335 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004336 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004337 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004338 curwin = old_curwin;
4339 curbuf = curwin->w_buffer;
4340}
4341
4342/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004343 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004344 */
4345 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004346ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004347{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004348 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004349 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004350
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004351 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4352 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004353
4354 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004355 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4356 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4357}
4358
4359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 * Return the number of the current entry (line number in the quickfix
4361 * window).
4362 */
4363 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004364qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004366 qf_info_T *qi = &ql_info;
4367
4368 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004369 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004370 qi = wp->w_llist_ref;
4371
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004372 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373}
4374
4375/*
4376 * Update the cursor position in the quickfix window to the current error.
4377 * Return TRUE if there is a quickfix window.
4378 */
4379 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004380qf_win_pos_update(
4381 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004382 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004385 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004387 // Put the cursor on the current error in the quickfix window, so that
4388 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004389 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 if (win != NULL
4391 && qf_index <= win->w_buffer->b_ml.ml_line_count
4392 && old_qf_index != qf_index)
4393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (qf_index > old_qf_index)
4395 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004396 win->w_redraw_top = old_qf_index;
4397 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
4399 else
4400 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004401 win->w_redraw_top = qf_index;
4402 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004404 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 return win != NULL;
4407}
4408
4409/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004410 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004411 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004412 */
4413 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004414is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004415{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004416 // A window displaying the quickfix buffer will have the w_llist_ref field
4417 // set to NULL.
4418 // A window displaying a location list buffer will have the w_llist_ref
4419 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004420 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004421 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4422 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004423 return TRUE;
4424
4425 return FALSE;
4426}
4427
4428/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004429 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4430 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004431 */
4432 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004433qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004434{
4435 win_T *win;
4436
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004437 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004438 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004439 return win;
4440 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004441}
4442
4443/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004444 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004445 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 */
4447 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004448qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004450 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004451 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004453 if (qi->qf_bufnr != INVALID_QFBUFNR)
4454 {
4455 buf_T *qfbuf;
4456 qfbuf = buflist_findnr(qi->qf_bufnr);
4457 if (qfbuf != NULL)
4458 return qfbuf;
4459 // buffer is no longer present
4460 qi->qf_bufnr = INVALID_QFBUFNR;
4461 }
4462
Bram Moolenaar9c102382006-05-03 21:26:49 +00004463 FOR_ALL_TAB_WINDOWS(tp, win)
4464 if (is_qf_win(win, qi))
4465 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004466
Bram Moolenaar9c102382006-05-03 21:26:49 +00004467 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468}
4469
4470/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004471 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004472 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004473 */
4474 int
4475qf_process_qftf_option(void)
4476{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004477 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004478}
4479
4480/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004481 * Update the w:quickfix_title variable in the quickfix/location list window in
4482 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004483 */
4484 static void
4485qf_update_win_titlevar(qf_info_T *qi)
4486{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004487 qf_list_T *qfl = qf_get_curlist(qi);
4488 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004489 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004490 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004491
Bram Moolenaar530bed92020-12-16 21:02:56 +01004492 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004493 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004494 if (is_qf_win(win, qi))
4495 {
4496 curwin = win;
4497 qf_set_title_var(qfl);
4498 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004499 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004500 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004501}
4502
4503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 * Find the quickfix buffer. If it exists, update the contents.
4505 */
4506 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004507qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508{
4509 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004510 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004513 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004514 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 if (buf != NULL)
4516 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004517 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004518 int qf_winid = 0;
4519
4520 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004521 {
4522 if (curwin->w_llist == qi)
4523 win = curwin;
4524 else
4525 {
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004526 // Find the file window (non-quickfix) with this location list
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004527 win = qf_find_win_with_loclist(qi);
4528 if (win == NULL)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004529 // File window is not found. Find the location list window.
4530 win = qf_find_win(qi);
4531 if (win == NULL)
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004532 return;
4533 }
4534 qf_winid = win->w_id;
4535 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004536
4537 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004538 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004539 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
Bram Moolenaard823fa92016-08-12 16:29:27 +02004541 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004542
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004543 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004544 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004545
Bram Moolenaar864293a2016-06-02 13:40:04 +02004546 if (old_last == NULL)
4547 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004548 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004549
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004550 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004551 aucmd_restbuf(&aco);
4552 }
4553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004554 // Only redraw when added lines are visible. This avoids flickering
4555 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004556 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4557 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559}
4560
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004561/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004562 * Add an error line to the quickfix buffer.
4563 */
4564 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004565qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004566 buf_T *buf, // quickfix window buffer
4567 linenr_T lnum,
4568 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004569 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004570 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004571 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004572{
4573 int len;
4574 buf_T *errbuf;
4575
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004576 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004577 // for this entry, then use it.
4578 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004579 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004580 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004581 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004582 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004583 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004584 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4585 len = (int)STRLEN(IObuff);
4586 }
4587 else if (qfp->qf_fnum != 0
4588 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4589 && errbuf->b_fname != NULL)
4590 {
4591 if (qfp->qf_type == 1) // :helpgrep
4592 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4593 else
4594 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004595 // Shorten the file name if not done already.
4596 // For optimization, do this only for the first entry in a
4597 // buffer.
4598 if (first_bufline && (errbuf->b_sfname == NULL
4599 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004600 {
4601 if (*dirname == NUL)
4602 mch_dirname(dirname, MAXPATHL);
4603 shorten_buf_fname(errbuf, dirname, FALSE);
4604 }
4605 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4606 }
4607 len = (int)STRLEN(IObuff);
4608 }
4609 else
4610 len = 0;
4611
4612 if (len < IOSIZE - 1)
4613 IObuff[len++] = '|';
4614
4615 if (qfp->qf_lnum > 0)
4616 {
thinca6864efa2021-06-19 20:45:20 +02004617 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004618 len += (int)STRLEN(IObuff + len);
4619
Bram Moolenaar858ba062020-05-31 23:11:59 +02004620 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4621 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004622 len += (int)STRLEN(IObuff + len);
4623 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004624 else if (qfp->qf_pattern != NULL)
4625 {
4626 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4627 len += (int)STRLEN(IObuff + len);
4628 }
4629 if (len < IOSIZE - 2)
4630 {
4631 IObuff[len++] = '|';
4632 IObuff[len++] = ' ';
4633 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004634
Bram Moolenaar858ba062020-05-31 23:11:59 +02004635 // Remove newlines and leading whitespace from the text.
4636 // For an unrecognized line keep the indent, the compiler may
4637 // mark a word with ^^^^.
4638 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4639 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004640 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004641
4642 if (ml_append_buf(buf, lnum, IObuff,
4643 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4644 return FAIL;
4645
4646 return OK;
4647}
4648
Bram Moolenaard43906d2020-07-20 21:31:32 +02004649/*
4650 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4651 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4652 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004653 static list_T *
4654call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4655{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004656 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004657 list_T *qftf_list = NULL;
4658
4659 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4660 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4661 // set.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004662 if (qfl->qftf_cb.cb_name != NULL)
4663 cb = &qfl->qftf_cb;
4664 if (cb != NULL && cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004665 {
4666 typval_T args[1];
4667 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004668 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004669
4670 // create the dict argument
4671 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4672 return NULL;
4673 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4674 dict_add_number(d, "winid", (long)qf_winid);
4675 dict_add_number(d, "id", (long)qfl->qf_id);
4676 dict_add_number(d, "start_idx", start_idx);
4677 dict_add_number(d, "end_idx", end_idx);
4678 ++d->dv_refcount;
4679 args[0].v_type = VAR_DICT;
4680 args[0].vval.v_dict = d;
4681
Bram Moolenaard43906d2020-07-20 21:31:32 +02004682 qftf_list = NULL;
4683 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4684 {
4685 if (rettv.v_type == VAR_LIST)
4686 {
4687 qftf_list = rettv.vval.v_list;
4688 qftf_list->lv_refcount++;
4689 }
4690 clear_tv(&rettv);
4691 }
4692 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004693 }
4694
4695 return qftf_list;
4696}
4697
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 * Fill current buffer with quickfix errors, replacing any previous contents.
4700 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004701 * If "old_last" is not NULL append the items after this one.
4702 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4703 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 */
4705 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004706qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004708 linenr_T lnum;
4709 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004710 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004711 list_T *qftf_list = NULL;
4712 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
Bram Moolenaar864293a2016-06-02 13:40:04 +02004714 if (old_last == NULL)
4715 {
4716 if (buf != curbuf)
4717 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004718 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004719 return;
4720 }
4721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004722 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004723 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004724 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004727 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004728 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004730 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004731 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004732 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004733
4734 *dirname = NUL;
4735
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004736 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004737 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004738 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004739 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004740 lnum = 0;
4741 }
4742 else
4743 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004744 if (old_last->qf_next != NULL)
4745 qfp = old_last->qf_next;
4746 else
4747 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004748 lnum = buf->b_ml.ml_line_count;
4749 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004750
4751 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4752 (long)qfl->qf_count);
4753 if (qftf_list != NULL)
4754 qftf_li = qftf_list->lv_first;
4755
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004756 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004758 char_u *qftf_str = NULL;
4759
Bram Moolenaard43906d2020-07-20 21:31:32 +02004760 // Use the text supplied by the user defined function (if any).
4761 // If the returned value is not string, then ignore the rest
4762 // of the returned values and use the default.
4763 if (qftf_li != NULL && !invalid_val)
4764 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004765 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004766 if (qftf_str == NULL)
4767 invalid_val = TRUE;
4768 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004769
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004770 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4771 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004773
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004774 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004775 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004777 if (qfp == NULL)
4778 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004779
4780 if (qftf_li != NULL)
4781 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004783
4784 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004785 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004786 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004789 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 check_lnums(TRUE);
4791
Bram Moolenaar864293a2016-06-02 13:40:04 +02004792 if (old_last == NULL)
4793 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004794 // Set the 'filetype' to "qf" each time after filling the buffer.
4795 // This resembles reading a file into a buffer, it's more logical when
4796 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004797 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004798 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4799 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004801 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004802 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004804 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004806 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004807 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004808
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004809 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004810 redraw_curbuf_later(NOT_VALID);
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004813 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 KeyTyped = old_KeyTyped;
4815}
4816
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004817/*
4818 * For every change made to the quickfix list, update the changed tick.
4819 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004820 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004821qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004822{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004823 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004824}
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004827 * Return the quickfix/location list number with the given identifier.
4828 * Returns -1 if list is not found.
4829 */
4830 static int
4831qf_id2nr(qf_info_T *qi, int_u qfid)
4832{
4833 int qf_idx;
4834
4835 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4836 if (qi->qf_lists[qf_idx].qf_id == qfid)
4837 return qf_idx;
4838 return INVALID_QFIDX;
4839}
4840
4841/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004842 * If the current list is not "save_qfid" and we can find the list with that ID
4843 * then make it the current list.
4844 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004845 * Returns OK if successfully restored the list. Returns FAIL if the list with
4846 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004847 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004848 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004849qf_restore_list(qf_info_T *qi, int_u save_qfid)
4850{
4851 int curlist;
4852
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004853 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004854 {
4855 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004856 if (curlist < 0)
4857 // list is not present
4858 return FAIL;
4859 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004860 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004861 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004862}
4863
4864/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004865 * Jump to the first entry if there is one.
4866 */
4867 static void
4868qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4869{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004870 if (qf_restore_list(qi, save_qfid) == FAIL)
4871 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004872
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004873 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004874 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004875 qf_jump(qi, 0, 0, forceit);
4876}
4877
4878/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004879 * Return TRUE when using ":vimgrep" for ":grep".
4880 */
4881 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004882grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004883{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004884 return ((cmdidx == CMD_grep
4885 || cmdidx == CMD_lgrep
4886 || cmdidx == CMD_grepadd
4887 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004888 && STRCMP("internal",
4889 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4890}
4891
4892/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004893 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004895 static char_u *
4896make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004898 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004899 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004900 case CMD_make: return (char_u *)"make";
4901 case CMD_lmake: return (char_u *)"lmake";
4902 case CMD_grep: return (char_u *)"grep";
4903 case CMD_lgrep: return (char_u *)"lgrep";
4904 case CMD_grepadd: return (char_u *)"grepadd";
4905 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4906 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908}
4909
4910/*
4911 * Return the name for the errorfile, in allocated memory.
4912 * Find a new unique name when 'makeef' contains "##".
4913 * Returns NULL for error.
4914 */
4915 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004916get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 char_u *p;
4919 char_u *name;
4920 static int start = -1;
4921 static int off = 0;
4922#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004923 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#endif
4925
4926 if (*p_mef == NUL)
4927 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004928 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004930 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 return name;
4932 }
4933
4934 for (p = p_mef; *p; ++p)
4935 if (p[0] == '#' && p[1] == '#')
4936 break;
4937
4938 if (*p == NUL)
4939 return vim_strsave(p_mef);
4940
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004941 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 for (;;)
4943 {
4944 if (start == -1)
4945 start = mch_get_pid();
4946 else
4947 off += 19;
4948
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004949 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (name == NULL)
4951 break;
4952 STRCPY(name, p_mef);
4953 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4954 STRCAT(name, p + 2);
4955 if (mch_getperm(name) < 0
4956#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004957 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 && mch_lstat((char *)name, &sb) < 0
4959#endif
4960 )
4961 break;
4962 vim_free(name);
4963 }
4964 return name;
4965}
4966
4967/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004968 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4969 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4970 */
4971 static char_u *
4972make_get_fullcmd(char_u *makecmd, char_u *fname)
4973{
4974 char_u *cmd;
4975 unsigned len;
4976
4977 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4978 if (*p_sp != NUL)
4979 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004980 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004981 if (cmd == NULL)
4982 return NULL;
4983 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4984 (char *)p_shq);
4985
4986 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4987 if (*p_sp != NUL)
4988 append_redir(cmd, len, p_sp, fname);
4989
4990 // Display the fully formed command. Output a newline if there's something
4991 // else than the :make command that was typed (in which case the cursor is
4992 // in column 0).
4993 if (msg_col == 0)
4994 msg_didout = FALSE;
4995 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004996 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004997 msg_outtrans(cmd); // show what we are doing
4998
4999 return cmd;
5000}
5001
5002/*
5003 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5004 */
5005 void
5006ex_make(exarg_T *eap)
5007{
5008 char_u *fname;
5009 char_u *cmd;
5010 char_u *enc = NULL;
5011 win_T *wp = NULL;
5012 qf_info_T *qi = &ql_info;
5013 int res;
5014 char_u *au_name = NULL;
5015 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005016 char_u *errorformat = p_efm;
5017 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005018
5019 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5020 if (grep_internal(eap->cmdidx))
5021 {
5022 ex_vimgrep(eap);
5023 return;
5024 }
5025
5026 au_name = make_get_auname(eap->cmdidx);
5027 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5028 curbuf->b_fname, TRUE, curbuf))
5029 {
5030#ifdef FEAT_EVAL
5031 if (aborting())
5032 return;
5033#endif
5034 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005035 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005036
5037 if (is_loclist_cmd(eap->cmdidx))
5038 wp = curwin;
5039
5040 autowrite_all();
5041 fname = get_mef_name();
5042 if (fname == NULL)
5043 return;
5044 mch_remove(fname); // in case it's not unique
5045
5046 cmd = make_get_fullcmd(eap->arg, fname);
5047 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005048 {
5049 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005050 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005051 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005052
5053 // let the shell know if we are redirecting output or not
5054 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5055
5056#ifdef AMIGA
5057 out_flush();
5058 // read window status report and redraw before message
5059 (void)char_avail();
5060#endif
5061
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005062 incr_quickfix_busy();
5063
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005064 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5065 errorformat = p_gefm;
5066 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5067 newlist = FALSE;
5068
5069 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5070 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005071 if (wp != NULL)
5072 {
5073 qi = GET_LOC_LIST(wp);
5074 if (qi == NULL)
5075 goto cleanup;
5076 }
5077 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005078 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005079
5080 // Remember the current quickfix list identifier, so that we can
5081 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005082 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005083 if (au_name != NULL)
5084 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5085 curbuf->b_fname, TRUE, curbuf);
5086 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5087 // display the first error
5088 qf_jump_first(qi, save_qfid, FALSE);
5089
5090cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005091 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005092 mch_remove(fname);
5093 vim_free(fname);
5094 vim_free(cmd);
5095}
5096
5097/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005098 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005099 */
5100 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005101qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005102{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005103 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005104
5105 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5106 return 0;
5107 return qf_get_curlist(qi)->qf_count;
5108}
5109
5110/*
5111 * Returns the number of valid entries in the current quickfix/location list.
5112 */
5113 int
5114qf_get_valid_size(exarg_T *eap)
5115{
5116 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005117 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005118 qfline_T *qfp;
5119 int i, sz = 0;
5120 int prev_fnum = 0;
5121
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005122 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5123 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005124
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005125 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005126 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005127 {
5128 if (qfp->qf_valid)
5129 {
5130 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005131 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005132 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5133 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005134 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005135 sz++;
5136 prev_fnum = qfp->qf_fnum;
5137 }
5138 }
5139 }
5140
5141 return sz;
5142}
5143
5144/*
5145 * Returns the current index of the quickfix/location list.
5146 * Returns 0 if there is an error.
5147 */
5148 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005149qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005150{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005151 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005152
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005153 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5154 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005155
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005156 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005157}
5158
5159/*
5160 * Returns the current index in the quickfix/location list (counting only valid
5161 * entries). If no valid entries are in the list, then returns 1.
5162 */
5163 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005164qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005165{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005166 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005167 qf_list_T *qfl;
5168 qfline_T *qfp;
5169 int i, eidx = 0;
5170 int prev_fnum = 0;
5171
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005172 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5173 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005174
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005175 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005176 qfp = qfl->qf_start;
5177
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005178 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005179 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005180 return 1;
5181
5182 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5183 {
5184 if (qfp->qf_valid)
5185 {
5186 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5187 {
5188 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5189 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005190 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005191 eidx++;
5192 prev_fnum = qfp->qf_fnum;
5193 }
5194 }
5195 else
5196 eidx++;
5197 }
5198 }
5199
5200 return eidx ? eidx : 1;
5201}
5202
5203/*
5204 * Get the 'n'th valid error entry in the quickfix or location list.
5205 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5206 * For :cdo and :ldo returns the 'n'th valid error entry.
5207 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5208 */
5209 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005210qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005211{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005212 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005213 int i, eidx;
5214 int prev_fnum = 0;
5215
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005216 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005217 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005218 return 1;
5219
Bram Moolenaar95946f12019-03-31 15:31:59 +02005220 eidx = 0;
5221 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005222 {
5223 if (qfp->qf_valid)
5224 {
5225 if (fdo)
5226 {
5227 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5228 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005229 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005230 eidx++;
5231 prev_fnum = qfp->qf_fnum;
5232 }
5233 }
5234 else
5235 eidx++;
5236 }
5237
5238 if (eidx == n)
5239 break;
5240 }
5241
5242 if (i <= qfl->qf_count)
5243 return i;
5244 else
5245 return 1;
5246}
5247
5248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005250 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005251 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 */
5253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005254ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005256 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005257 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005258
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005259 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5260 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005261
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005262 if (eap->addr_count > 0)
5263 errornr = (int)eap->line2;
5264 else
5265 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005266 switch (eap->cmdidx)
5267 {
5268 case CMD_cc: case CMD_ll:
5269 errornr = 0;
5270 break;
5271 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5272 case CMD_lfirst:
5273 errornr = 1;
5274 break;
5275 default:
5276 errornr = 32767;
5277 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005278 }
5279
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005280 // For cdo and ldo commands, jump to the nth valid error.
5281 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005282 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5283 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005284 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005285 eap->addr_count > 0 ? (int)eap->line1 : 1,
5286 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5287
5288 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289}
5290
5291/*
5292 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005293 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005294 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 */
5296 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005297ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005299 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005300 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005301 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005302
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005303 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5304 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005305
Bram Moolenaar55b69262017-08-13 13:42:01 +02005306 if (eap->addr_count > 0
5307 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5308 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005309 errornr = (int)eap->line2;
5310 else
5311 errornr = 1;
5312
Bram Moolenaar39665952018-08-15 20:59:48 +02005313 // Depending on the command jump to either next or previous entry/file.
5314 switch (eap->cmdidx)
5315 {
5316 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5317 dir = FORWARD;
5318 break;
5319 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5320 case CMD_lNext:
5321 dir = BACKWARD;
5322 break;
5323 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5324 dir = FORWARD_FILE;
5325 break;
5326 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5327 dir = BACKWARD_FILE;
5328 break;
5329 default:
5330 dir = FORWARD;
5331 break;
5332 }
5333
5334 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335}
5336
5337/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005338 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5339 * The index of the entry is stored in 'errornr'.
5340 * Returns NULL if an entry is not found.
5341 */
5342 static qfline_T *
5343qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5344{
5345 qfline_T *qfp = NULL;
5346 int idx = 0;
5347
5348 // Find the first entry in this file
5349 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5350 if (qfp->qf_fnum == bnr)
5351 break;
5352
5353 *errornr = idx;
5354 return qfp;
5355}
5356
5357/*
5358 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5359 * with the error number for the first entry. Assumes the entries are sorted in
5360 * the quickfix list by line number.
5361 */
5362 static qfline_T *
5363qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5364{
5365 while (!got_int
5366 && entry->qf_prev != NULL
5367 && entry->qf_fnum == entry->qf_prev->qf_fnum
5368 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5369 {
5370 entry = entry->qf_prev;
5371 --*errornr;
5372 }
5373
5374 return entry;
5375}
5376
5377/*
5378 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5379 * with the error number for the last entry. Assumes the entries are sorted in
5380 * the quickfix list by line number.
5381 */
5382 static qfline_T *
5383qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5384{
5385 while (!got_int &&
5386 entry->qf_next != NULL
5387 && entry->qf_fnum == entry->qf_next->qf_fnum
5388 && entry->qf_lnum == entry->qf_next->qf_lnum)
5389 {
5390 entry = entry->qf_next;
5391 ++*errornr;
5392 }
5393
5394 return entry;
5395}
5396
5397/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005398 * Returns TRUE if the specified quickfix entry is
5399 * after the given line (linewise is TRUE)
5400 * or after the line and column.
5401 */
5402 static int
5403qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5404{
5405 if (linewise)
5406 return qfp->qf_lnum > pos->lnum;
5407 else
5408 return (qfp->qf_lnum > pos->lnum ||
5409 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5410}
5411
5412/*
5413 * Returns TRUE if the specified quickfix entry is
5414 * before the given line (linewise is TRUE)
5415 * or before the line and column.
5416 */
5417 static int
5418qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5419{
5420 if (linewise)
5421 return qfp->qf_lnum < pos->lnum;
5422 else
5423 return (qfp->qf_lnum < pos->lnum ||
5424 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5425}
5426
5427/*
5428 * Returns TRUE if the specified quickfix entry is
5429 * on or after the given line (linewise is TRUE)
5430 * or on or after the line and column.
5431 */
5432 static int
5433qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5434{
5435 if (linewise)
5436 return qfp->qf_lnum >= pos->lnum;
5437 else
5438 return (qfp->qf_lnum > pos->lnum ||
5439 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5440}
5441
5442/*
5443 * Returns TRUE if the specified quickfix entry is
5444 * on or before the given line (linewise is TRUE)
5445 * or on or before the line and column.
5446 */
5447 static int
5448qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5449{
5450 if (linewise)
5451 return qfp->qf_lnum <= pos->lnum;
5452 else
5453 return (qfp->qf_lnum < pos->lnum ||
5454 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5455}
5456
5457/*
5458 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5459 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5460 * multiple entries on a single line as one. Otherwise returns the entry after
5461 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005462 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5463 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005464 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005465 */
5466 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005467qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005468 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005469 pos_T *pos,
5470 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005471 qfline_T *qfp,
5472 int *errornr)
5473{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005474 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005475 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005476 return qfp;
5477
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005478 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005479 while (qfp->qf_next != NULL
5480 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005481 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005482 {
5483 qfp = qfp->qf_next;
5484 ++*errornr;
5485 }
5486
5487 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005488 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005489 return NULL;
5490
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005491 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005492 qfp = qfp->qf_next;
5493 ++*errornr;
5494
5495 return qfp;
5496}
5497
5498/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005499 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5500 * If 'linewise' is TRUE, returns the entry before the specified line and
5501 * treats multiple entries on a single line as one. Otherwise returns the entry
5502 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005503 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5504 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005505 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005506 */
5507 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005508qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005509 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005510 pos_T *pos,
5511 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005512 qfline_T *qfp,
5513 int *errornr)
5514{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005515 // Find the entry just before 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_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005519 {
5520 qfp = qfp->qf_next;
5521 ++*errornr;
5522 }
5523
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005524 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005525 return NULL;
5526
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005527 if (linewise)
5528 // If multiple entries are on the same line, then use the first entry
5529 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005530
5531 return qfp;
5532}
5533
5534/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005535 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005536 * the direction 'dir'.
5537 */
5538 static qfline_T *
5539qf_find_closest_entry(
5540 qf_list_T *qfl,
5541 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005542 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005543 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005544 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005545 int *errornr)
5546{
5547 qfline_T *qfp;
5548
5549 *errornr = 0;
5550
5551 // Find the first entry in this file
5552 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5553 if (qfp == NULL)
5554 return NULL; // no entry in this file
5555
5556 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005557 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005558 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005559 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005560
5561 return qfp;
5562}
5563
5564/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005565 * Get the nth quickfix entry below the specified entry. Searches forward in
5566 * the list. If linewise is TRUE, then treat multiple entries on a single line
5567 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005568 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005569 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005570qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005571{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005572 qfline_T *entry = entry_arg;
5573
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005574 while (n-- > 0 && !got_int)
5575 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005576 int first_errornr = *errornr;
5577
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005578 if (linewise)
5579 // Treat all the entries on the same line in this file as one
5580 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005581
5582 if (entry->qf_next == NULL
5583 || entry->qf_next->qf_fnum != entry->qf_fnum)
5584 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005585 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005586 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005587 break;
5588 }
5589
5590 entry = entry->qf_next;
5591 ++*errornr;
5592 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005593}
5594
5595/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005596 * Get the nth quickfix entry above the specified entry. Searches backwards in
5597 * the list. If linewise is TRUE, then treat multiple entries on a single line
5598 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005599 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005600 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005601qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005602{
5603 while (n-- > 0 && !got_int)
5604 {
5605 if (entry->qf_prev == NULL
5606 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5607 break;
5608
5609 entry = entry->qf_prev;
5610 --*errornr;
5611
5612 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005613 if (linewise)
5614 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005615 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005616}
5617
5618/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005619 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5620 * the specified direction. Returns the error number in the quickfix list or 0
5621 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005622 */
5623 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005624qf_find_nth_adj_entry(
5625 qf_list_T *qfl,
5626 int bnr,
5627 pos_T *pos,
5628 int n,
5629 int dir,
5630 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005631{
5632 qfline_T *adj_entry;
5633 int errornr;
5634
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005635 // Find an entry closest to the specified position
5636 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005637 if (adj_entry == NULL)
5638 return 0;
5639
5640 if (--n > 0)
5641 {
5642 // Go to the n'th entry in the current buffer
5643 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005644 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005645 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005646 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005647 }
5648
5649 return errornr;
5650}
5651
5652/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005653 * Jump to a quickfix entry in the current file nearest to the current line or
5654 * current line/col.
5655 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5656 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005657 */
5658 void
5659ex_cbelow(exarg_T *eap)
5660{
5661 qf_info_T *qi;
5662 qf_list_T *qfl;
5663 int dir;
5664 int buf_has_flag;
5665 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005666 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005667
5668 if (eap->addr_count > 0 && eap->line2 <= 0)
5669 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005670 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005671 return;
5672 }
5673
5674 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005675 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5676 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005677 buf_has_flag = BUF_HAS_QF_ENTRY;
5678 else
5679 buf_has_flag = BUF_HAS_LL_ENTRY;
5680 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5681 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005682 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005683 return;
5684 }
5685
5686 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5687 return;
5688
5689 qfl = qf_get_curlist(qi);
5690 // check if the list has valid errors
5691 if (!qf_list_has_valid_entries(qfl))
5692 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005693 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005694 return;
5695 }
5696
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005697 if (eap->cmdidx == CMD_cbelow
5698 || eap->cmdidx == CMD_lbelow
5699 || eap->cmdidx == CMD_cafter
5700 || eap->cmdidx == CMD_lafter)
5701 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005702 dir = FORWARD;
5703 else
5704 dir = BACKWARD;
5705
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005706 pos = curwin->w_cursor;
5707 // A quickfix entry column number is 1 based whereas cursor column
5708 // number is 0 based. Adjust the column number.
5709 pos.col++;
5710 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5711 eap->addr_count > 0 ? eap->line2 : 0, dir,
5712 eap->cmdidx == CMD_cbelow
5713 || eap->cmdidx == CMD_lbelow
5714 || eap->cmdidx == CMD_cabove
5715 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005716
5717 if (errornr > 0)
5718 qf_jump(qi, 0, errornr, FALSE);
5719 else
5720 emsg(_(e_no_more_items));
5721}
5722
5723/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005724 * Return the autocmd name for the :cfile Ex commands
5725 */
5726 static char_u *
5727cfile_get_auname(cmdidx_T cmdidx)
5728{
5729 switch (cmdidx)
5730 {
5731 case CMD_cfile: return (char_u *)"cfile";
5732 case CMD_cgetfile: return (char_u *)"cgetfile";
5733 case CMD_caddfile: return (char_u *)"caddfile";
5734 case CMD_lfile: return (char_u *)"lfile";
5735 case CMD_lgetfile: return (char_u *)"lgetfile";
5736 case CMD_laddfile: return (char_u *)"laddfile";
5737 default: return NULL;
5738 }
5739}
5740
5741/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005742 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005743 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 */
5745 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005746ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005748 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005749 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005750 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005751 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005752 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005753 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005754
Bram Moolenaara16123a2019-03-28 20:31:07 +01005755 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005756 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5757 NULL, FALSE, curbuf))
5758 {
5759#ifdef FEAT_EVAL
5760 if (aborting())
5761 return;
5762#endif
5763 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005764
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005765 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005766#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005767 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005768 {
5769 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005770 NULL, NULL,
5771 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005772 if (browse_file == NULL)
5773 return;
5774 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5775 vim_free(browse_file);
5776 }
5777 else
5778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005780 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005781
Bram Moolenaar39665952018-08-15 20:59:48 +02005782 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005783 wp = curwin;
5784
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005785 incr_quickfix_busy();
5786
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005787 // This function is used by the :cfile, :cgetfile and :caddfile
5788 // commands.
5789 // :cfile always creates a new quickfix list and jumps to the
5790 // first error.
5791 // :cgetfile creates a new quickfix list but doesn't jump to the
5792 // first error.
5793 // :caddfile adds to an existing quickfix list. If there is no
5794 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005795 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005796 && eap->cmdidx != CMD_laddfile),
5797 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005798 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005799 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005800 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005801 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005802 {
5803 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005804 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005805 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005806 }
5807 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005808 qf_list_changed(qf_get_curlist(qi));
5809 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005810 if (au_name != NULL)
5811 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005812
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005813 // Jump to the first error for a new list and if autocmds didn't
5814 // free the list.
5815 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5816 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005817 // display the first error
5818 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005819
5820 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005821}
5822
5823/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005824 * Return the vimgrep autocmd name.
5825 */
5826 static char_u *
5827vgr_get_auname(cmdidx_T cmdidx)
5828{
5829 switch (cmdidx)
5830 {
5831 case CMD_vimgrep: return (char_u *)"vimgrep";
5832 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5833 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5834 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5835 case CMD_grep: return (char_u *)"grep";
5836 case CMD_lgrep: return (char_u *)"lgrep";
5837 case CMD_grepadd: return (char_u *)"grepadd";
5838 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5839 default: return NULL;
5840 }
5841}
5842
5843/*
5844 * Initialize the regmatch used by vimgrep for pattern "s".
5845 */
5846 static void
5847vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5848{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005849 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005850 regmatch->regprog = NULL;
5851
5852 if (s == NULL || *s == NUL)
5853 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005854 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005855 if (last_search_pat() == NULL)
5856 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005857 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005858 return;
5859 }
5860 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5861 }
5862 else
5863 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5864
5865 regmatch->rmm_ic = p_ic;
5866 regmatch->rmm_maxcol = 0;
5867}
5868
5869/*
5870 * Display a file name when vimgrep is running.
5871 */
5872 static void
5873vgr_display_fname(char_u *fname)
5874{
5875 char_u *p;
5876
5877 msg_start();
5878 p = msg_strtrunc(fname, TRUE);
5879 if (p == NULL)
5880 msg_outtrans(fname);
5881 else
5882 {
5883 msg_outtrans(p);
5884 vim_free(p);
5885 }
5886 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005887 msg_didout = FALSE; // overwrite this message
5888 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005889 msg_col = 0;
5890 out_flush();
5891}
5892
5893/*
5894 * Load a dummy buffer to search for a pattern using vimgrep.
5895 */
5896 static buf_T *
5897vgr_load_dummy_buf(
5898 char_u *fname,
5899 char_u *dirname_start,
5900 char_u *dirname_now)
5901{
5902 int save_mls;
5903#if defined(FEAT_SYN_HL)
5904 char_u *save_ei = NULL;
5905#endif
5906 buf_T *buf;
5907
5908#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005909 // Don't do Filetype autocommands to avoid loading syntax and
5910 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005911 save_ei = au_event_disable(",Filetype");
5912#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005913 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005914 save_mls = p_mls;
5915 p_mls = 0;
5916
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005917 // Load file into a buffer, so that 'fileencoding' is detected,
5918 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005919 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5920
5921 p_mls = save_mls;
5922#if defined(FEAT_SYN_HL)
5923 au_event_restore(save_ei);
5924#endif
5925
5926 return buf;
5927}
5928
5929/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005930 * Check whether a quickfix/location list is valid. Autocmds may remove or
5931 * change a quickfix list when vimgrep is running. If the list is not found,
5932 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005933 */
5934 static int
5935vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005936 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005937 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005938 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005939 char_u *title)
5940{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005941 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005942 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005943 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005944 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005945 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005946 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005947 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005948 return FALSE;
5949 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005950 else
5951 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005952 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005953 qf_new_list(qi, title);
5954 return TRUE;
5955 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005956 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005957
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005958 if (qf_restore_list(qi, qfid) == FAIL)
5959 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005960
5961 return TRUE;
5962}
5963
5964/*
5965 * Search for a pattern in all the lines in a buffer and add the matching lines
5966 * to a quickfix list.
5967 */
5968 static int
5969vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005970 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005971 char_u *fname,
5972 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005973 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005974 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005975 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005976 int duplicate_name,
5977 int flags)
5978{
5979 int found_match = FALSE;
5980 long lnum;
5981 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02005982 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005983
Bram Moolenaar1c299432018-10-28 14:36:09 +01005984 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005985 {
5986 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005987 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005988 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005989 // Regular expression match
5990 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5991 col, NULL, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005992 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005993 // Pass the buffer number so that it gets used even for a
5994 // dummy buffer, unless duplicate_name is set, then the
5995 // buffer will be wiped out below.
5996 if (qf_add_entry(qfl,
5997 NULL, // dir
5998 fname,
5999 NULL,
6000 duplicate_name ? 0 : buf->b_fnum,
6001 ml_get_buf(buf,
6002 regmatch->startpos[0].lnum + lnum, FALSE),
6003 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006004 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006005 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006006 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006007 FALSE, // vis_col
6008 NULL, // search pattern
6009 0, // nr
6010 0, // type
6011 TRUE // valid
6012 ) == QF_FAIL)
6013 {
6014 got_int = TRUE;
6015 break;
6016 }
6017 found_match = TRUE;
6018 if (--*tomatch == 0)
6019 break;
6020 if ((flags & VGR_GLOBAL) == 0
6021 || regmatch->endpos[0].lnum > 0)
6022 break;
6023 col = regmatch->endpos[0].col
6024 + (col == regmatch->endpos[0].col);
6025 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6026 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006027 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006028 }
6029 else
6030 {
6031 char_u *str = ml_get_buf(buf, lnum, FALSE);
6032 int score;
6033 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006034 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006035
6036 // Fuzzy string match
6037 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6038 {
6039 // Pass the buffer number so that it gets used even for a
6040 // dummy buffer, unless duplicate_name is set, then the
6041 // buffer will be wiped out below.
6042 if (qf_add_entry(qfl,
6043 NULL, // dir
6044 fname,
6045 NULL,
6046 duplicate_name ? 0 : buf->b_fnum,
6047 str,
6048 lnum,
thinca6864efa2021-06-19 20:45:20 +02006049 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006050 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006051 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006052 FALSE, // vis_col
6053 NULL, // search pattern
6054 0, // nr
6055 0, // type
6056 TRUE // valid
6057 ) == QF_FAIL)
6058 {
6059 got_int = TRUE;
6060 break;
6061 }
6062 found_match = TRUE;
6063 if (--*tomatch == 0)
6064 break;
6065 if ((flags & VGR_GLOBAL) == 0)
6066 break;
6067 col = matches[pat_len - 1] + col + 1;
6068 if (col > (colnr_T)STRLEN(str))
6069 break;
6070 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006071 }
6072 line_breakcheck();
6073 if (got_int)
6074 break;
6075 }
6076
6077 return found_match;
6078}
6079
6080/*
6081 * Jump to the first match and update the directory.
6082 */
6083 static void
6084vgr_jump_to_match(
6085 qf_info_T *qi,
6086 int forceit,
6087 int *redraw_for_dummy,
6088 buf_T *first_match_buf,
6089 char_u *target_dir)
6090{
6091 buf_T *buf;
6092
6093 buf = curbuf;
6094 qf_jump(qi, 0, 0, forceit);
6095 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006096 // If we jumped to another buffer redrawing will already be
6097 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006098 *redraw_for_dummy = FALSE;
6099
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006100 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006101 if (curbuf == first_match_buf && target_dir != NULL)
6102 {
6103 exarg_T ea;
6104
Bram Moolenaara80faa82020-04-12 19:37:17 +02006105 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006106 ea.arg = target_dir;
6107 ea.cmdidx = CMD_lcd;
6108 ex_cd(&ea);
6109 }
6110}
6111
6112/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006113 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006114 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006115typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006116{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006117 long tomatch; // maximum number of matches to find
6118 char_u *spat; // search pattern
6119 int flags; // search modifier
6120 char_u **fnames; // list of files to search
6121 int fcount; // number of files
6122 regmmatch_T regmatch; // compiled search pattern
6123 char_u *qf_title; // quickfix list title
6124} vgr_args_T;
6125
6126/*
6127 * Process :vimgrep command arguments. The command syntax is:
6128 *
6129 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6130 */
6131 static int
6132vgr_process_args(
6133 exarg_T *eap,
6134 vgr_args_T *args)
6135{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006136 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006137
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006138 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006139
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006140 args->regmatch.regprog = NULL;
6141 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006142
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006143 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006144 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006145 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006146 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006147
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006148 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006149 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006150 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006151 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006152 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006153 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006154 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006155
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006156 vgr_init_regmatch(&args->regmatch, args->spat);
6157 if (args->regmatch.regprog == NULL)
6158 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006159
6160 p = skipwhite(p);
6161 if (*p == NUL)
6162 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006163 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006164 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006165 }
6166
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006167 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006168 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6169 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006170 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006171 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006172 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006173 }
6174
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006175 return OK;
6176}
6177
6178/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006179 * Return TRUE if "buf" had an existing swap file, the current swap file does
6180 * not end in ".swp".
6181 */
6182 static int
6183existing_swapfile(buf_T *buf)
6184{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006185 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006186 {
6187 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6188 size_t len = STRLEN(fname);
6189
6190 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6191 }
6192 return FALSE;
6193}
6194
6195/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006196 * Search for a pattern in a list of files and populate the quickfix list with
6197 * the matches.
6198 */
6199 static int
6200vgr_process_files(
6201 win_T *wp,
6202 qf_info_T *qi,
6203 vgr_args_T *cmd_args,
6204 int *redraw_for_dummy,
6205 buf_T **first_match_buf,
6206 char_u **target_dir)
6207{
6208 int status = FAIL;
6209 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6210 time_t seconds = 0;
6211 char_u *fname;
6212 int fi;
6213 buf_T *buf;
6214 int duplicate_name = FALSE;
6215 int using_dummy;
6216 char_u *dirname_start = NULL;
6217 char_u *dirname_now = NULL;
6218 int found_match;
6219 aco_save_T aco;
6220
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006221 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6222 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006223 if (dirname_start == NULL || dirname_now == NULL)
6224 goto theend;
6225
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006226 // Remember the current directory, because a BufRead autocommand that does
6227 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006228 mch_dirname(dirname_start, MAXPATHL);
6229
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006230 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006231 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6232 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006233 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006234 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006235 if (time(NULL) > seconds)
6236 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006237 // Display the file name every second or so, show the user we are
6238 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006239 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006240 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006241 }
6242
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006243 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006244 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6245 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006246 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006247 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006248 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006249 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006250
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006251 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006252 }
6253 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006254 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006255 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006256
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006257 // Check whether the quickfix list is still valid. When loading a
6258 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006259 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006260 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006261
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006262 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006263
Bram Moolenaar81695252004-12-29 20:58:21 +00006264 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006265 {
6266 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006267 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006268 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006269 else
6270 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006271 // Try for a match in all lines of the buffer.
6272 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006273 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006274 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006275 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006276
Bram Moolenaar81695252004-12-29 20:58:21 +00006277 if (using_dummy)
6278 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006279 if (found_match && *first_match_buf == NULL)
6280 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006281 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006282 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006283 // Never keep a dummy buffer if there is another buffer
6284 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006285 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006286 buf = NULL;
6287 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006288 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006289 || buf->b_p_bh[0] == 'u' // "unload"
6290 || buf->b_p_bh[0] == 'w' // "wipe"
6291 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006292 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006293 // When no match was found we don't need to remember the
6294 // buffer, wipe it out. If there was a match and it
6295 // wasn't the first one or we won't jump there: only
6296 // unload the buffer.
6297 // Ignore 'hidden' here, because it may lead to having too
6298 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006299 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006300 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006301 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006302 buf = NULL;
6303 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006304 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006305 || (cmd_args->flags & VGR_NOJUMP)
6306 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006307 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006308 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006309 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006310 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006311 buf = NULL;
6312 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006313 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006314
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006315 if (buf != NULL)
6316 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006317 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006318 buf->b_flags &= ~BF_DUMMY;
6319
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006320 // If the buffer is still loaded we need to use the
6321 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006322 if (buf == *first_match_buf
6323 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006324 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006325 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006326
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006327 // The buffer is still loaded, the Filetype autocommands
6328 // need to be done now, in that buffer. And the modelines
6329 // need to be done (again). But not the window-local
6330 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006331 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006332#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006333 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6334 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006335#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006336 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006337 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006338 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006339 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006340 }
6341 }
6342
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006343 status = OK;
6344
6345theend:
6346 vim_free(dirname_now);
6347 vim_free(dirname_start);
6348 return status;
6349}
6350
6351/*
6352 * ":vimgrep {pattern} file(s)"
6353 * ":vimgrepadd {pattern} file(s)"
6354 * ":lvimgrep {pattern} file(s)"
6355 * ":lvimgrepadd {pattern} file(s)"
6356 */
6357 void
6358ex_vimgrep(exarg_T *eap)
6359{
6360 vgr_args_T args;
6361 qf_info_T *qi;
6362 qf_list_T *qfl;
6363 int_u save_qfid;
6364 win_T *wp = NULL;
6365 int redraw_for_dummy = FALSE;
6366 buf_T *first_match_buf = NULL;
6367 char_u *target_dir = NULL;
6368 char_u *au_name = NULL;
6369 int status;
6370
6371 au_name = vgr_get_auname(eap->cmdidx);
6372 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6373 curbuf->b_fname, TRUE, curbuf))
6374 {
6375#ifdef FEAT_EVAL
6376 if (aborting())
6377 return;
6378#endif
6379 }
6380
6381 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6382 if (qi == NULL)
6383 return;
6384
6385 if (vgr_process_args(eap, &args) == FAIL)
6386 goto theend;
6387
6388 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6389 && eap->cmdidx != CMD_vimgrepadd
6390 && eap->cmdidx != CMD_lvimgrepadd)
6391 || qf_stack_empty(qi))
6392 // make place for a new list
6393 qf_new_list(qi, args.qf_title);
6394
6395 incr_quickfix_busy();
6396
6397 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6398 &first_match_buf, &target_dir);
6399 if (status != OK)
6400 {
6401 FreeWild(args.fcount, args.fnames);
6402 decr_quickfix_busy();
6403 goto theend;
6404 }
6405
6406 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006407
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006408 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006409 qfl->qf_nonevalid = FALSE;
6410 qfl->qf_ptr = qfl->qf_start;
6411 qfl->qf_index = 1;
6412 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006413
Bram Moolenaar864293a2016-06-02 13:40:04 +02006414 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006415
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006416 // Remember the current quickfix list identifier, so that we can check for
6417 // autocommands changing the current quickfix list.
6418 save_qfid = qf_get_curlist(qi)->qf_id;
6419
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006420 if (au_name != NULL)
6421 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6422 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006423 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6424 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006425 if (!qflist_valid(wp, save_qfid)
6426 || qf_restore_list(qi, save_qfid) == FAIL)
6427 {
6428 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006429 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006430 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006431
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006432 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006433 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006435 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006436 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6437 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006438 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006439 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006440 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006441
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006442 decr_quickfix_busy();
6443
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006444 // If we loaded a dummy buffer into the current window, the autocommands
6445 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006446 if (redraw_for_dummy)
6447 {
6448#ifdef FEAT_FOLDING
6449 foldUpdateAll(curwin);
6450#else
6451 redraw_later(NOT_VALID);
6452#endif
6453 }
6454
Bram Moolenaar86b68352004-12-27 21:59:20 +00006455theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006456 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006457 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006458 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006459}
6460
6461/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006462 * Restore current working directory to "dirname_start" if they differ, taking
6463 * into account whether it is set locally or globally.
6464 */
6465 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006466restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006467{
6468 char_u *dirname_now = alloc(MAXPATHL);
6469
6470 if (NULL != dirname_now)
6471 {
6472 mch_dirname(dirname_now, MAXPATHL);
6473 if (STRCMP(dirname_start, dirname_now) != 0)
6474 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006475 // If the directory has changed, change it back by building up an
6476 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006477 exarg_T ea;
6478
Bram Moolenaara80faa82020-04-12 19:37:17 +02006479 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006480 ea.arg = dirname_start;
6481 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6482 ex_cd(&ea);
6483 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006484 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006485 }
6486}
6487
6488/*
6489 * Load file "fname" into a dummy buffer and return the buffer pointer,
6490 * placing the directory resulting from the buffer load into the
6491 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6492 * prior to calling this function. Restores directory to "dirname_start" prior
6493 * to returning, if autocmds or the 'autochdir' option have changed it.
6494 *
6495 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6496 * or wipe_dummy_buffer() later!
6497 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006498 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006499 */
6500 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006501load_dummy_buffer(
6502 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006503 char_u *dirname_start, // in: old directory
6504 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006505{
6506 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006507 bufref_T newbufref;
6508 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006509 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006510 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006511 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006513 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006514 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6515 if (newbuf == NULL)
6516 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006517 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006518
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006519 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006520 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6521
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006522 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006523 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006524 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006525 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006526 ++newbuf->b_locked;
6527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006528 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006529 aucmd_prepbuf(&aco, newbuf);
6530
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006531 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006532 (void)setfname(curbuf, fname, NULL, FALSE);
6533
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006534 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006535 check_need_swap(TRUE);
6536
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006537 // Remove the "dummy" flag, otherwise autocommands may not
6538 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006539 curbuf->b_flags &= ~BF_DUMMY;
6540
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006541 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006542 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006543 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006544 NULL, READ_NEW | READ_DUMMY);
6545 --newbuf->b_locked;
6546 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006547 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006548 && !(curbuf->b_flags & BF_NEW))
6549 {
6550 failed = FALSE;
6551 if (curbuf != newbuf)
6552 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006553 // Bloody autocommands changed the buffer! Can happen when
6554 // using netrw and editing a remote file. Use the current
6555 // buffer instead, delete the dummy one after restoring the
6556 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006557 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006558 newbuf = curbuf;
6559 }
6560 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006561
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006562 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006563 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006564 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6565 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006566
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006567 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6568 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006569 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006570 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006571
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006572 // When autocommands/'autochdir' option changed directory: go back.
6573 // Let the caller know what the resulting dir was first, in case it is
6574 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006575 mch_dirname(resulting_dir, MAXPATHL);
6576 restore_start_dir(dirname_start);
6577
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006578 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006579 return NULL;
6580 if (failed)
6581 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006582 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006583 return NULL;
6584 }
6585 return newbuf;
6586}
6587
6588/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006589 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6590 * directory to "dirname_start" prior to returning, if autocmds or the
6591 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006592 */
6593 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006594wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006595{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006596 // If any autocommand opened a window on the dummy buffer, close that
6597 // window. If we can't close them all then give up.
6598 while (buf->b_nwindows > 0)
6599 {
6600 int did_one = FALSE;
6601 win_T *wp;
6602
6603 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006604 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006605 if (wp->w_buffer == buf)
6606 {
6607 if (win_close(wp, FALSE) == OK)
6608 did_one = TRUE;
6609 break;
6610 }
6611 if (!did_one)
6612 return;
6613 }
6614
6615 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006616 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006617#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006618 cleanup_T cs;
6619
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006620 // Reset the error/interrupt/exception state here so that aborting()
6621 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6622 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006623 enter_cleanup(&cs);
6624#endif
6625
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006626 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006627
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006628#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006629 // Restore the error/interrupt/exception state if not discarded by a
6630 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006631 leave_cleanup(&cs);
6632#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006633 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006634 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006635 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006636}
6637
6638/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006639 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6640 * directory to "dirname_start" prior to returning, if autocmds or the
6641 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006642 */
6643 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006644unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006645{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006646 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006647 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006648 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006649
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006650 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006651 restore_start_dir(dirname_start);
6652 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006653}
6654
Bram Moolenaar05159a02005-02-26 23:04:13 +00006655#if defined(FEAT_EVAL) || defined(PROTO)
6656/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006657 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006658 * to 'list'. Returns OK on success.
6659 */
6660 static int
6661get_qfline_items(qfline_T *qfp, list_T *list)
6662{
6663 int bufnum;
6664 dict_T *dict;
6665 char_u buf[2];
6666
6667 // Handle entries with a non-existing buffer number.
6668 bufnum = qfp->qf_fnum;
6669 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6670 bufnum = 0;
6671
6672 if ((dict = dict_alloc()) == NULL)
6673 return FAIL;
6674 if (list_append_dict(list, dict) == FAIL)
6675 return FAIL;
6676
6677 buf[0] = qfp->qf_type;
6678 buf[1] = NUL;
6679 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006680 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6681 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6682 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6683 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6684 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6685 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006686 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6687 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6688 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6689 || dict_add_string(dict, "type", buf) == FAIL
6690 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6691 return FAIL;
6692
6693 return OK;
6694}
6695
6696/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006697 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006698 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006699 * If eidx is not 0, then return only the specified entry. Otherwise return
6700 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006701 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006702 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006703get_errorlist(
6704 qf_info_T *qi_arg,
6705 win_T *wp,
6706 int qf_idx,
6707 int eidx,
6708 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006709{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006710 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006711 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006712 qfline_T *qfp;
6713 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006714
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006715 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006716 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006717 qi = &ql_info;
6718 if (wp != NULL)
6719 {
6720 qi = GET_LOC_LIST(wp);
6721 if (qi == NULL)
6722 return FAIL;
6723 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006724 }
6725
Bram Moolenaar858ba062020-05-31 23:11:59 +02006726 if (eidx < 0)
6727 return OK;
6728
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006729 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006730 qf_idx = qi->qf_curlist;
6731
Bram Moolenaar0398e002019-03-21 21:12:49 +01006732 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006733 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006734
Bram Moolenaar0398e002019-03-21 21:12:49 +01006735 qfl = qf_get_list(qi, qf_idx);
6736 if (qf_list_empty(qfl))
6737 return FAIL;
6738
Bram Moolenaara16123a2019-03-28 20:31:07 +01006739 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006740 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006741 if (eidx > 0)
6742 {
6743 if (eidx == i)
6744 return get_qfline_items(qfp, list);
6745 }
6746 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006747 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006748 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006749
Bram Moolenaar05159a02005-02-26 23:04:13 +00006750 return OK;
6751}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006752
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006753// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006754enum {
6755 QF_GETLIST_NONE = 0x0,
6756 QF_GETLIST_TITLE = 0x1,
6757 QF_GETLIST_ITEMS = 0x2,
6758 QF_GETLIST_NR = 0x4,
6759 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006760 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006761 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006762 QF_GETLIST_IDX = 0x40,
6763 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006764 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006765 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006766 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006767 QF_GETLIST_QFTF = 0x800,
6768 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006769};
6770
6771/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006772 * Parse text from 'di' and return the quickfix list items.
6773 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006774 */
6775 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006776qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006777{
6778 int status = FAIL;
6779 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006780 char_u *errorformat = p_efm;
6781 dictitem_T *efm_di;
6782 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006783
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006784 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006785 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006786 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006787 // If errorformat is supplied then use it, otherwise use the 'efm'
6788 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006789 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6790 {
6791 if (efm_di->di_tv.v_type != VAR_STRING ||
6792 efm_di->di_tv.vval.v_string == NULL)
6793 return FAIL;
6794 errorformat = efm_di->di_tv.vval.v_string;
6795 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006796
Bram Moolenaar36538222017-09-02 19:51:44 +02006797 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006798 if (l == NULL)
6799 return FAIL;
6800
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006801 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006802 if (qi != NULL)
6803 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006804 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006805 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6806 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006807 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006808 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006809 }
6810 free(qi);
6811 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006812 dict_add_list(retdict, "items", l);
6813 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006814 }
6815
6816 return status;
6817}
6818
6819/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006820 * Return the quickfix/location list window identifier in the current tabpage.
6821 */
6822 static int
6823qf_winid(qf_info_T *qi)
6824{
6825 win_T *win;
6826
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006827 // The quickfix window can be opened even if the quickfix list is not set
6828 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006829 if (qi == NULL)
6830 return 0;
6831 win = qf_find_win(qi);
6832 if (win != NULL)
6833 return win->w_id;
6834 return 0;
6835}
6836
6837/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006838 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006839 * window. If there is no buffer associated with the list or the buffer is
6840 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006841 */
6842 static int
6843qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6844{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006845 int bufnum = 0;
6846
6847 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6848 bufnum = qi->qf_bufnr;
6849
6850 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006851}
6852
6853/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006854 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006855 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006856 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006857qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006858{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006859 int flags = QF_GETLIST_NONE;
6860
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006861 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006862 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006863 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006864 if (!loclist)
6865 // File window ID is applicable only to location list windows
6866 flags &= ~ QF_GETLIST_FILEWINID;
6867 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006868
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006869 if (dict_find(what, (char_u *)"title", -1) != NULL)
6870 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006871
Bram Moolenaara6d48492017-12-12 22:45:31 +01006872 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6873 flags |= QF_GETLIST_NR;
6874
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006875 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6876 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006877
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006878 if (dict_find(what, (char_u *)"context", -1) != NULL)
6879 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006880
Bram Moolenaara6d48492017-12-12 22:45:31 +01006881 if (dict_find(what, (char_u *)"id", -1) != NULL)
6882 flags |= QF_GETLIST_ID;
6883
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006884 if (dict_find(what, (char_u *)"items", -1) != NULL)
6885 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006886
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006887 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6888 flags |= QF_GETLIST_IDX;
6889
6890 if (dict_find(what, (char_u *)"size", -1) != NULL)
6891 flags |= QF_GETLIST_SIZE;
6892
Bram Moolenaarb254af32017-12-18 19:48:58 +01006893 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6894 flags |= QF_GETLIST_TICK;
6895
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006896 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6897 flags |= QF_GETLIST_FILEWINID;
6898
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006899 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6900 flags |= QF_GETLIST_QFBUFNR;
6901
Bram Moolenaard43906d2020-07-20 21:31:32 +02006902 if (dict_find(what, (char_u *)"quickfixtextfunc", -1) != NULL)
6903 flags |= QF_GETLIST_QFTF;
6904
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006905 return flags;
6906}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006907
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006908/*
6909 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6910 * If 'nr' and 'id' are not present in 'what' then return the current
6911 * quickfix list index.
6912 * If 'nr' is zero then return the current quickfix list index.
6913 * If 'nr' is '$' then return the last quickfix list index.
6914 * If 'id' is present then return the index of the quickfix list with that id.
6915 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6916 * Return -1, if quickfix list is not present or if the stack is empty.
6917 */
6918 static int
6919qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6920{
6921 int qf_idx;
6922 dictitem_T *di;
6923
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006924 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006925 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6926 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006927 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006928 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006929 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006930 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006931 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006932 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006933 qf_idx = di->di_tv.vval.v_number - 1;
6934 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006935 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006936 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006937 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006938 else if (di->di_tv.v_type == VAR_STRING
6939 && di->di_tv.vval.v_string != NULL
6940 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006941 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006942 qf_idx = qi->qf_listcount - 1;
6943 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006944 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006945 }
6946
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006947 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6948 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006949 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006950 if (di->di_tv.v_type == VAR_NUMBER)
6951 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006952 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006953 if (di->di_tv.vval.v_number != 0)
6954 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6955 }
6956 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006957 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006958 }
6959
6960 return qf_idx;
6961}
6962
6963/*
6964 * Return default values for quickfix list properties in retdict.
6965 */
6966 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006967qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006968{
6969 int status = OK;
6970
6971 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006972 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006973 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6974 {
6975 list_T *l = list_alloc();
6976 if (l != NULL)
6977 status = dict_add_list(retdict, "items", l);
6978 else
6979 status = FAIL;
6980 }
6981 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006982 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006983 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006984 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006985 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006986 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006987 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006988 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006989 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006990 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006991 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006992 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006993 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006994 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006995 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006996 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006997 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6998 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02006999 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7000 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007001
7002 return status;
7003}
7004
7005/*
7006 * Return the quickfix list title as 'title' in retdict
7007 */
7008 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007009qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007010{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007011 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007012}
7013
7014/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007015 * Returns the identifier of the window used to display files from a location
7016 * list. If there is no associated window, then returns 0. Useful only when
7017 * called from a location list window.
7018 */
7019 static int
7020qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7021{
7022 int winid = 0;
7023
7024 if (wp != NULL && IS_LL_WINDOW(wp))
7025 {
7026 win_T *ll_wp = qf_find_win_with_loclist(qi);
7027 if (ll_wp != NULL)
7028 winid = ll_wp->w_id;
7029 }
7030
7031 return dict_add_number(retdict, "filewinid", winid);
7032}
7033
7034/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007035 * Return the quickfix list items/entries as 'items' in retdict.
7036 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007037 */
7038 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007039qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007040{
7041 int status = OK;
7042 list_T *l = list_alloc();
7043 if (l != NULL)
7044 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007045 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007046 dict_add_list(retdict, "items", l);
7047 }
7048 else
7049 status = FAIL;
7050
7051 return status;
7052}
7053
7054/*
7055 * Return the quickfix list context (if any) as 'context' in retdict.
7056 */
7057 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007058qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007059{
7060 int status;
7061 dictitem_T *di;
7062
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007063 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007064 {
7065 di = dictitem_alloc((char_u *)"context");
7066 if (di != NULL)
7067 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007068 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007069 status = dict_add(retdict, di);
7070 if (status == FAIL)
7071 dictitem_free(di);
7072 }
7073 else
7074 status = FAIL;
7075 }
7076 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007077 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007078
7079 return status;
7080}
7081
7082/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007083 * Return the current quickfix list index as 'idx' in retdict.
7084 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007085 */
7086 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007087qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007088{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007089 if (eidx == 0)
7090 {
7091 eidx = qfl->qf_index;
7092 if (qf_list_empty(qfl))
7093 // For empty lists, current index is set to 0
7094 eidx = 0;
7095 }
7096 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007097}
7098
7099/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007100 * Return the 'quickfixtextfunc' function of a quickfix/location list
7101 */
7102 static int
7103qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7104{
7105 int status;
7106
7107 if (qfl->qftf_cb.cb_name != NULL)
7108 {
7109 typval_T tv;
7110
7111 put_callback(&qfl->qftf_cb, &tv);
7112 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7113 clear_tv(&tv);
7114 }
7115 else
7116 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7117
7118 return status;
7119}
7120
7121/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007122 * Return quickfix/location list details (title) as a
7123 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7124 * then current list is used. Otherwise the specified list is used.
7125 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007126 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007127qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7128{
7129 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007130 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007131 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007132 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007133 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007134 dictitem_T *di;
7135 int flags = QF_GETLIST_NONE;
7136
7137 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7138 return qf_get_list_from_lines(what, di, retdict);
7139
7140 if (wp != NULL)
7141 qi = GET_LOC_LIST(wp);
7142
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007143 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007144
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007145 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007146 qf_idx = qf_getprop_qfidx(qi, what);
7147
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007148 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007149 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007150 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007151
Bram Moolenaar0398e002019-03-21 21:12:49 +01007152 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007153
Bram Moolenaar858ba062020-05-31 23:11:59 +02007154 // If an entry index is specified, use that
7155 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7156 {
7157 if (di->di_tv.v_type != VAR_NUMBER)
7158 return FAIL;
7159 eidx = di->di_tv.vval.v_number;
7160 }
7161
Bram Moolenaard823fa92016-08-12 16:29:27 +02007162 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007163 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007164 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007165 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007166 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007167 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007168 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007169 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007170 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007171 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007172 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007173 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007174 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007175 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007176 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007177 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007178 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007179 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007180 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7181 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007182 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7183 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007184 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7185 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007186
Bram Moolenaard823fa92016-08-12 16:29:27 +02007187 return status;
7188}
7189
7190/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007191 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007192 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7193 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007194 */
7195 static int
7196qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007197 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007198 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007199 int first_entry,
7200 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007201{
7202 static int did_bufnr_emsg;
7203 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007204 int bufnum, valid, status, col, end_col, vcol, nr;
7205 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007206
7207 if (first_entry)
7208 did_bufnr_emsg = FALSE;
7209
Bram Moolenaar8f667172018-12-14 15:38:31 +01007210 filename = dict_get_string(d, (char_u *)"filename", TRUE);
7211 module = dict_get_string(d, (char_u *)"module", TRUE);
7212 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
7213 lnum = (int)dict_get_number(d, (char_u *)"lnum");
thinca6864efa2021-06-19 20:45:20 +02007214 end_lnum = (int)dict_get_number(d, (char_u *)"end_lnum");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007215 col = (int)dict_get_number(d, (char_u *)"col");
thinca6864efa2021-06-19 20:45:20 +02007216 end_col = (int)dict_get_number(d, (char_u *)"end_col");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007217 vcol = (int)dict_get_number(d, (char_u *)"vcol");
7218 nr = (int)dict_get_number(d, (char_u *)"nr");
7219 type = dict_get_string(d, (char_u *)"type", TRUE);
7220 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
7221 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007222 if (text == NULL)
7223 text = vim_strsave((char_u *)"");
7224
7225 valid = TRUE;
7226 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7227 valid = FALSE;
7228
7229 // Mark entries with non-existing buffer number as not valid. Give the
7230 // error message only once.
7231 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7232 {
7233 if (!did_bufnr_emsg)
7234 {
7235 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007236 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007237 }
7238 valid = FALSE;
7239 bufnum = 0;
7240 }
7241
7242 // If the 'valid' field is present it overrules the detected value.
7243 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar401f0c02020-09-05 22:37:39 +02007244 valid = (int)dict_get_bool(d, (char_u *)"valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007245
Bram Moolenaar0398e002019-03-21 21:12:49 +01007246 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007247 NULL, // dir
7248 filename,
7249 module,
7250 bufnum,
7251 text,
7252 lnum,
thinca6864efa2021-06-19 20:45:20 +02007253 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007254 col,
thinca6864efa2021-06-19 20:45:20 +02007255 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007256 vcol, // vis_col
7257 pattern, // search pattern
7258 nr,
7259 type == NULL ? NUL : *type,
7260 valid);
7261
7262 vim_free(filename);
7263 vim_free(module);
7264 vim_free(pattern);
7265 vim_free(text);
7266 vim_free(type);
7267
Bram Moolenaar9752c722018-12-22 16:49:34 +01007268 if (valid)
7269 *valid_entry = TRUE;
7270
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007271 return status;
7272}
7273
7274/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007275 * Add list of entries to quickfix/location list. Each list entry is
7276 * a dictionary with item information.
7277 */
7278 static int
7279qf_add_entries(
7280 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007281 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007282 list_T *list,
7283 char_u *title,
7284 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007285{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007286 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007287 listitem_T *li;
7288 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007289 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007290 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007291 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007292
Bram Moolenaara3921f42017-06-04 15:30:34 +02007293 if (action == ' ' || qf_idx == qi->qf_listcount)
7294 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007295 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007296 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007297 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007298 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007299 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007300 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007301 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007302 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007303 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007304 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007305 qf_free_items(qfl);
7306 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007307 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007308
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007309 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007310 {
7311 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007312 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007313
7314 d = li->li_tv.vval.v_dict;
7315 if (d == NULL)
7316 continue;
7317
Bram Moolenaar0398e002019-03-21 21:12:49 +01007318 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007319 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007320 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007321 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007322 }
7323
Bram Moolenaar9752c722018-12-22 16:49:34 +01007324 // Check if any valid error entries are added to the list.
7325 if (valid_entry)
7326 qfl->qf_nonevalid = FALSE;
7327 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007328 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007329 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007330
7331 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007332 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007333 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007334
7335 // Update the current error index if not appending to the list or if the
7336 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007337 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007338 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007339
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007340 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007341 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007342
7343 return retval;
7344}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007345
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007346/*
7347 * Get the quickfix list index from 'nr' or 'id'
7348 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007349 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007350qf_setprop_get_qfidx(
7351 qf_info_T *qi,
7352 dict_T *what,
7353 int action,
7354 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007355{
7356 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007357 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007358
Bram Moolenaard823fa92016-08-12 16:29:27 +02007359 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7360 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007361 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007362 if (di->di_tv.v_type == VAR_NUMBER)
7363 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007364 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007365 if (di->di_tv.vval.v_number != 0)
7366 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007367
Bram Moolenaar55b69262017-08-13 13:42:01 +02007368 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7369 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007370 // When creating a new list, accept qf_idx pointing to the next
7371 // non-available list and add the new list at the end of the
7372 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007373 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007374 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007375 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007376 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007377 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007378 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007379 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007380 }
7381 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007382 && di->di_tv.vval.v_string != NULL
7383 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007384 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007385 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007386 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007387 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007388 qf_idx = 0;
7389 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007390 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007391 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007392 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007393 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007394 }
7395
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007396 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007397 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007398 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007399 if (di->di_tv.v_type != VAR_NUMBER)
7400 return INVALID_QFIDX;
7401
7402 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007403 }
7404
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007405 return qf_idx;
7406}
7407
7408/*
7409 * Set the quickfix list title.
7410 */
7411 static int
7412qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7413{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007414 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007415
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007416 if (di->di_tv.v_type != VAR_STRING)
7417 return FAIL;
7418
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007419 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007420 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007421 if (qf_idx == qi->qf_curlist)
7422 qf_update_win_titlevar(qi);
7423
7424 return OK;
7425}
7426
7427/*
7428 * Set quickfix list items/entries.
7429 */
7430 static int
7431qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7432{
7433 int retval = FAIL;
7434 char_u *title_save;
7435
7436 if (di->di_tv.v_type != VAR_LIST)
7437 return FAIL;
7438
7439 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7440 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7441 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007442 vim_free(title_save);
7443
7444 return retval;
7445}
7446
7447/*
7448 * Set quickfix list items/entries from a list of lines.
7449 */
7450 static int
7451qf_setprop_items_from_lines(
7452 qf_info_T *qi,
7453 int qf_idx,
7454 dict_T *what,
7455 dictitem_T *di,
7456 int action)
7457{
7458 char_u *errorformat = p_efm;
7459 dictitem_T *efm_di;
7460 int retval = FAIL;
7461
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007462 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007463 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7464 {
7465 if (efm_di->di_tv.v_type != VAR_STRING ||
7466 efm_di->di_tv.vval.v_string == NULL)
7467 return FAIL;
7468 errorformat = efm_di->di_tv.vval.v_string;
7469 }
7470
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007471 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007472 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7473 return FAIL;
7474
7475 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007476 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007477 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007478 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007479 retval = OK;
7480
7481 return retval;
7482}
7483
7484/*
7485 * Set quickfix list context.
7486 */
7487 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007488qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007489{
7490 typval_T *ctx;
7491
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007492 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007493 ctx = alloc_tv();
7494 if (ctx != NULL)
7495 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007496 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007497
7498 return OK;
7499}
7500
7501/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007502 * Set the current index in the specified quickfix list
7503 */
7504 static int
7505qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7506{
7507 int denote = FALSE;
7508 int newidx;
7509 int old_qfidx;
7510 qfline_T *qf_ptr;
7511
7512 // If the specified index is '$', then use the last entry
7513 if (di->di_tv.v_type == VAR_STRING
7514 && di->di_tv.vval.v_string != NULL
7515 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7516 newidx = qfl->qf_count;
7517 else
7518 {
7519 // Otherwise use the specified index
7520 newidx = tv_get_number_chk(&di->di_tv, &denote);
7521 if (denote)
7522 return FAIL;
7523 }
7524
7525 if (newidx < 1) // sanity check
7526 return FAIL;
7527 if (newidx > qfl->qf_count)
7528 newidx = qfl->qf_count;
7529
7530 old_qfidx = qfl->qf_index;
7531 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7532 if (qf_ptr == NULL)
7533 return FAIL;
7534 qfl->qf_ptr = qf_ptr;
7535 qfl->qf_index = newidx;
7536
7537 // If the current list is modified and it is displayed in the quickfix
7538 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007539 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007540 qf_win_pos_update(qi, old_qfidx);
7541
7542 return OK;
7543}
7544
7545/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007546 * Set the current index in the specified quickfix list
7547 */
7548 static int
7549qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7550{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007551 callback_T cb;
7552
7553 free_callback(&qfl->qftf_cb);
7554 cb = get_callback(&di->di_tv);
7555 if (cb.cb_name != NULL && *cb.cb_name != NUL)
7556 set_callback(&qfl->qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007557
7558 return OK;
7559}
7560
7561/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007562 * Set quickfix/location list properties (title, items, context).
7563 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007564 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007565 */
7566 static int
7567qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7568{
7569 dictitem_T *di;
7570 int retval = FAIL;
7571 int qf_idx;
7572 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007573 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007574
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007575 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007576 newlist = TRUE;
7577
7578 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007579 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007580 return FAIL;
7581
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007582 if (newlist)
7583 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007584 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007585 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007586 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007587 }
7588
Bram Moolenaar0398e002019-03-21 21:12:49 +01007589 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007590 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007591 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007592 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007593 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007594 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007595 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007596 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007597 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007598 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7599 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007600 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7601 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007602
Bram Moolenaar287153c2020-11-29 14:20:27 +01007603 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007604 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007605 if (newlist)
7606 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007607
Bram Moolenaard823fa92016-08-12 16:29:27 +02007608 return retval;
7609}
7610
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007611/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007612 * Free the entire quickfix/location list stack.
7613 * If the quickfix/location list window is open, then clear it.
7614 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007615 static void
7616qf_free_stack(win_T *wp, qf_info_T *qi)
7617{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007618 win_T *qfwin = qf_find_win(qi);
7619 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007620
7621 if (qfwin != NULL)
7622 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007623 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007624 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007625 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007626 qf_update_buffer(qi, NULL);
7627 }
7628
7629 if (wp != NULL && IS_LL_WINDOW(wp))
7630 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007631 // If in the location list window, then use the non-location list
7632 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007633 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007634 if (llwin != NULL)
7635 wp = llwin;
7636 }
7637
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007638 qf_free_all(wp);
7639 if (wp == NULL)
7640 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007641 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007642 qi->qf_curlist = 0;
7643 qi->qf_listcount = 0;
7644 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007645 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007646 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007647 // If the location list window is open, then create a new empty
7648 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007649 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007650
Bram Moolenaar95946f12019-03-31 15:31:59 +02007651 if (new_ll != NULL)
7652 {
7653 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007654
Bram Moolenaar95946f12019-03-31 15:31:59 +02007655 // first free the list reference in the location list window
7656 ll_free_all(&qfwin->w_llist_ref);
7657
7658 qfwin->w_llist_ref = new_ll;
7659 if (wp != qfwin)
7660 win_set_loclist(wp, new_ll);
7661 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007662 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007663}
7664
Bram Moolenaard823fa92016-08-12 16:29:27 +02007665/*
7666 * Populate the quickfix list with the items supplied in the list
7667 * of dictionaries. "title" will be copied to w:quickfix_title.
7668 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007669 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007670 */
7671 int
7672set_errorlist(
7673 win_T *wp,
7674 list_T *list,
7675 int action,
7676 char_u *title,
7677 dict_T *what)
7678{
7679 qf_info_T *qi = &ql_info;
7680 int retval = OK;
7681
7682 if (wp != NULL)
7683 {
7684 qi = ll_get_or_alloc_list(wp);
7685 if (qi == NULL)
7686 return FAIL;
7687 }
7688
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007689 if (action == 'f')
7690 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007691 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007692 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007693 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007694 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007695
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007696 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007697 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007698 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007699 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007700 _("cannot have both a list and a \"what\" argument"));
7701 return FAIL;
7702 }
7703
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007704 incr_quickfix_busy();
7705
7706 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007707 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007708 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007709 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007710 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007711 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007712 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007713 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007714
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007715 decr_quickfix_busy();
7716
Bram Moolenaard823fa92016-08-12 16:29:27 +02007717 return retval;
7718}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007719
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007720/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007721 * Mark the quickfix context and callback function as in use for all the lists
7722 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007723 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007724 static int
7725mark_quickfix_ctx(qf_info_T *qi, int copyID)
7726{
7727 int i;
7728 int abort = FALSE;
7729 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007730 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007731
7732 for (i = 0; i < LISTCOUNT && !abort; ++i)
7733 {
7734 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007735 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7736 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007737 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7738
7739 cb = &qi->qf_lists[i].qftf_cb;
7740 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007741 }
7742
7743 return abort;
7744}
7745
7746/*
7747 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007748 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007749 */
7750 int
7751set_ref_in_quickfix(int copyID)
7752{
7753 int abort = FALSE;
7754 tabpage_T *tp;
7755 win_T *win;
7756
7757 abort = mark_quickfix_ctx(&ql_info, copyID);
7758 if (abort)
7759 return abort;
7760
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007761 abort = set_ref_in_callback(&qftf_cb, copyID);
7762 if (abort)
7763 return abort;
7764
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007765 FOR_ALL_TAB_WINDOWS(tp, win)
7766 {
7767 if (win->w_llist != NULL)
7768 {
7769 abort = mark_quickfix_ctx(win->w_llist, copyID);
7770 if (abort)
7771 return abort;
7772 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007773 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7774 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007775 // In a location list window and none of the other windows is
7776 // referring to this location list. Mark the location list
7777 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007778 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7779 if (abort)
7780 return abort;
7781 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007782 }
7783
7784 return abort;
7785}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007786#endif
7787
Bram Moolenaar81695252004-12-29 20:58:21 +00007788/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007789 * Return the autocmd name for the :cbuffer Ex commands
7790 */
7791 static char_u *
7792cbuffer_get_auname(cmdidx_T cmdidx)
7793{
7794 switch (cmdidx)
7795 {
7796 case CMD_cbuffer: return (char_u *)"cbuffer";
7797 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7798 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7799 case CMD_lbuffer: return (char_u *)"lbuffer";
7800 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7801 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7802 default: return NULL;
7803 }
7804}
7805
7806/*
7807 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7808 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7809 */
7810 static int
7811cbuffer_process_args(
7812 exarg_T *eap,
7813 buf_T **bufp,
7814 linenr_T *line1,
7815 linenr_T *line2)
7816{
7817 buf_T *buf = NULL;
7818
7819 if (*eap->arg == NUL)
7820 buf = curbuf;
7821 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7822 buf = buflist_findnr(atoi((char *)eap->arg));
7823
7824 if (buf == NULL)
7825 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007826 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007827 return FAIL;
7828 }
7829
7830 if (buf->b_ml.ml_mfp == NULL)
7831 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007832 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007833 return FAIL;
7834 }
7835
7836 if (eap->addr_count == 0)
7837 {
7838 eap->line1 = 1;
7839 eap->line2 = buf->b_ml.ml_line_count;
7840 }
7841
7842 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7843 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7844 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007845 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007846 return FAIL;
7847 }
7848
7849 *line1 = eap->line1;
7850 *line2 = eap->line2;
7851 *bufp = buf;
7852
7853 return OK;
7854}
7855
7856/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007857 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007858 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007859 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007860 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007861 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007862 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007863 */
7864 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007865ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007866{
7867 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007868 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007869 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007870 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007871 int_u save_qfid;
7872 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007873 char_u *qf_title;
7874 linenr_T line1;
7875 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007876
Bram Moolenaara16123a2019-03-28 20:31:07 +01007877 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007878 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007879 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007880 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007881#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007882 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007883 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007884#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007885 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007886
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007887 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007888 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7889 if (qi == NULL)
7890 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007891
Bram Moolenaara16123a2019-03-28 20:31:07 +01007892 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7893 return;
7894
7895 qf_title = qf_cmdtitle(*eap->cmdlinep);
7896
7897 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007898 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007899 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7900 (char *)qf_title, (char *)buf->b_sfname);
7901 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007902 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007903
7904 incr_quickfix_busy();
7905
7906 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7907 (eap->cmdidx != CMD_caddbuffer
7908 && eap->cmdidx != CMD_laddbuffer),
7909 line1, line2,
7910 qf_title, NULL);
7911 if (qf_stack_empty(qi))
7912 {
7913 decr_quickfix_busy();
7914 return;
7915 }
7916 if (res >= 0)
7917 qf_list_changed(qf_get_curlist(qi));
7918
7919 // Remember the current quickfix list identifier, so that we can
7920 // check for autocommands changing the current quickfix list.
7921 save_qfid = qf_get_curlist(qi)->qf_id;
7922 if (au_name != NULL)
7923 {
7924 buf_T *curbuf_old = curbuf;
7925
7926 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7927 TRUE, curbuf);
7928 if (curbuf != curbuf_old)
7929 // Autocommands changed buffer, don't jump now, "qi" may
7930 // be invalid.
7931 res = 0;
7932 }
7933 // Jump to the first error for a new list and if autocmds didn't
7934 // free the list.
7935 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7936 eap->cmdidx == CMD_lbuffer)
7937 && qflist_valid(wp, save_qfid))
7938 // display the first error
7939 qf_jump_first(qi, save_qfid, eap->forceit);
7940
7941 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007942}
7943
Bram Moolenaar1e015462005-09-25 22:16:38 +00007944#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007945/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007946 * Return the autocmd name for the :cexpr Ex commands.
7947 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007948 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007949cexpr_get_auname(cmdidx_T cmdidx)
7950{
7951 switch (cmdidx)
7952 {
7953 case CMD_cexpr: return (char_u *)"cexpr";
7954 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7955 case CMD_caddexpr: return (char_u *)"caddexpr";
7956 case CMD_lexpr: return (char_u *)"lexpr";
7957 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7958 case CMD_laddexpr: return (char_u *)"laddexpr";
7959 default: return NULL;
7960 }
7961}
7962
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007963 int
7964trigger_cexpr_autocmd(int cmdidx)
7965{
7966 char_u *au_name = cexpr_get_auname(cmdidx);
7967
7968 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7969 curbuf->b_fname, TRUE, curbuf))
7970 {
7971 if (aborting())
7972 return FAIL;
7973 }
7974 return OK;
7975}
7976
7977 int
7978cexpr_core(exarg_T *eap, typval_T *tv)
7979{
7980 qf_info_T *qi;
7981 win_T *wp = NULL;
7982
7983 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7984 if (qi == NULL)
7985 return FAIL;
7986
7987 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7988 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7989 {
7990 int res;
7991 int_u save_qfid;
7992 char_u *au_name = cexpr_get_auname(eap->cmdidx);
7993
7994 incr_quickfix_busy();
7995 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
7996 (eap->cmdidx != CMD_caddexpr
7997 && eap->cmdidx != CMD_laddexpr),
7998 (linenr_T)0, (linenr_T)0,
7999 qf_cmdtitle(*eap->cmdlinep), NULL);
8000 if (qf_stack_empty(qi))
8001 {
8002 decr_quickfix_busy();
8003 return FAIL;
8004 }
8005 if (res >= 0)
8006 qf_list_changed(qf_get_curlist(qi));
8007
8008 // Remember the current quickfix list identifier, so that we can
8009 // check for autocommands changing the current quickfix list.
8010 save_qfid = qf_get_curlist(qi)->qf_id;
8011 if (au_name != NULL)
8012 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8013 curbuf->b_fname, TRUE, curbuf);
8014
8015 // Jump to the first error for a new list and if autocmds didn't
8016 // free the list.
8017 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8018 && qflist_valid(wp, save_qfid))
8019 // display the first error
8020 qf_jump_first(qi, save_qfid, eap->forceit);
8021 decr_quickfix_busy();
8022 return OK;
8023 }
8024
Bram Moolenaar677658a2022-01-05 16:09:06 +00008025 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008026 return FAIL;
8027}
8028
Bram Moolenaara16123a2019-03-28 20:31:07 +01008029/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008030 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8031 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008032 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008033 */
8034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008035ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008036{
8037 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008038
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008039 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008040 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008041
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008042 // Evaluate the expression. When the result is a string or a list we can
8043 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008044 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008045 if (tv != NULL)
8046 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008047 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008048 free_tv(tv);
8049 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008050}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008051#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008052
8053/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008054 * Get the location list for ":lhelpgrep"
8055 */
8056 static qf_info_T *
8057hgr_get_ll(int *new_ll)
8058{
8059 win_T *wp;
8060 qf_info_T *qi;
8061
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008062 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008063 if (bt_help(curwin->w_buffer))
8064 wp = curwin;
8065 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008066 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008067 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008068
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008069 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008070 qi = NULL;
8071 else
8072 qi = wp->w_llist;
8073
8074 if (qi == NULL)
8075 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008076 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008077 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008078 return NULL;
8079 *new_ll = TRUE;
8080 }
8081
8082 return qi;
8083}
8084
8085/*
8086 * Search for a pattern in a help file.
8087 */
8088 static void
8089hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008090 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008091 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008092 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008093 regmatch_T *p_regmatch)
8094{
8095 FILE *fd;
8096 long lnum;
8097
8098 fd = mch_fopen((char *)fname, "r");
8099 if (fd == NULL)
8100 return;
8101
8102 lnum = 1;
8103 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8104 {
8105 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008106
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008107 // Convert a line if 'encoding' is not utf-8 and
8108 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008109 if (p_vc->vc_type != CONV_NONE
8110 && has_non_ascii(IObuff))
8111 {
8112 line = string_convert(p_vc, IObuff, NULL);
8113 if (line == NULL)
8114 line = IObuff;
8115 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008116
8117 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8118 {
8119 int l = (int)STRLEN(line);
8120
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008121 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008122 while (l > 0 && line[l - 1] <= ' ')
8123 line[--l] = NUL;
8124
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008125 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008126 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008127 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008128 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008129 0,
8130 line,
8131 lnum,
thinca6864efa2021-06-19 20:45:20 +02008132 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008133 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008134 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008135 (int)(p_regmatch->endp[0] - line)
8136 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008137 FALSE, // vis_col
8138 NULL, // search pattern
8139 0, // nr
8140 1, // type
8141 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008142 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008143 {
8144 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008145 if (line != IObuff)
8146 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008147 break;
8148 }
8149 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008150 if (line != IObuff)
8151 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008152 ++lnum;
8153 line_breakcheck();
8154 }
8155 fclose(fd);
8156}
8157
8158/*
8159 * Search for a pattern in all the help files in the doc directory under
8160 * the given directory.
8161 */
8162 static void
8163hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008164 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008165 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008166 regmatch_T *p_regmatch,
8167 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008168#ifdef FEAT_MULTI_LANG
8169 , char_u *lang
8170#endif
8171 )
8172{
8173 int fcount;
8174 char_u **fnames;
8175 int fi;
8176
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008177 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008178 add_pathsep(dirname);
8179 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8180 if (gen_expand_wildcards(1, &dirname, &fcount,
8181 &fnames, EW_FILE|EW_SILENT) == OK
8182 && fcount > 0)
8183 {
8184 for (fi = 0; fi < fcount && !got_int; ++fi)
8185 {
8186#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008187 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008188 if (lang != NULL
8189 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008190 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008191 && !(STRNICMP(lang, "en", 2) == 0
8192 && STRNICMP("txt", fnames[fi]
8193 + STRLEN(fnames[fi]) - 3, 3) == 0))
8194 continue;
8195#endif
8196
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008197 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008198 }
8199 FreeWild(fcount, fnames);
8200 }
8201}
8202
8203/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008204 * Search for a pattern in all the help files in the 'runtimepath'
8205 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008206 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008207 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008208 */
8209 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008210hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008211{
8212 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008213
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008214 vimconv_T vc;
8215
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008216 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8217 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008218 vc.vc_type = CONV_NONE;
8219 if (!enc_utf8)
8220 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008221
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008222 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008223 p = p_rtp;
8224 while (*p != NUL && !got_int)
8225 {
8226 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8227
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008228 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008229#ifdef FEAT_MULTI_LANG
8230 , lang
8231#endif
8232 );
8233 }
8234
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008235 if (vc.vc_type != CONV_NONE)
8236 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008237}
8238
8239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 * ":helpgrep {pattern}"
8241 */
8242 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008243ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 regmatch_T regmatch;
8246 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008247 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008248 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008249 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008250 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008251 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008252 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
Bram Moolenaar73633f82012-01-20 13:39:07 +01008254 switch (eap->cmdidx)
8255 {
8256 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8257 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8258 default: break;
8259 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008260 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8261 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008262 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008263#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008264 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008265 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008266#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008267 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008268
Bram Moolenaar39665952018-08-15 20:59:48 +02008269 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008270 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008271 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008272 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008273 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008274 }
8275
Bram Moolenaara16123a2019-03-28 20:31:07 +01008276 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8277 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008278 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008279 p_cpo = empty_option;
8280
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008281 incr_quickfix_busy();
8282
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008283#ifdef FEAT_MULTI_LANG
8284 // Check for a specified language
8285 lang = check_help_lang(eap->arg);
8286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8288 regmatch.rm_ic = FALSE;
8289 if (regmatch.regprog != NULL)
8290 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008291 qf_list_T *qfl;
8292
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008293 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008294 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008295 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008297 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008298
Bram Moolenaar473de612013-06-08 18:19:48 +02008299 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008301 qfl->qf_nonevalid = FALSE;
8302 qfl->qf_ptr = qfl->qf_start;
8303 qfl->qf_index = 1;
8304 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008305 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008308 if (p_cpo == empty_option)
8309 p_cpo = save_cpo;
8310 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008311 {
8312 // Darn, some plugin changed the value. If it's still empty it was
8313 // changed and restored, need to restore in the complicated way.
8314 if (*p_cpo == NUL)
8315 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008316 if (save_cpo_allocated)
8317 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008318 }
8319
8320 if (updated)
8321 // This may open a window and source scripts, do this after 'cpo' was
8322 // restored.
8323 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324
Bram Moolenaar73633f82012-01-20 13:39:07 +01008325 if (au_name != NULL)
8326 {
8327 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8328 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008329 // When adding a location list to an existing location list stack,
8330 // if the autocmd made the stack invalid, then just return.
8331 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008332 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008333 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008334 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008335 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008336 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008337
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008338 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008339 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008340 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008341 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008342 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008343
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008344 decr_quickfix_busy();
8345
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008346 if (eap->cmdidx == CMD_lhelpgrep)
8347 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008348 // If the help window is not opened or if it already points to the
8349 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008350 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008351 {
8352 if (new_qi)
8353 ll_free_all(&qi);
8354 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008355 else if (curwin->w_llist == NULL && new_qi)
8356 // current window didn't have a location list associated with it
8357 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008358 curwin->w_llist = qi;
8359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008361#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008362
8363#if defined(FEAT_EVAL) || defined(PROTO)
8364# ifdef FEAT_QUICKFIX
8365 static void
8366get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8367{
8368 if (what_arg->v_type == VAR_UNKNOWN)
8369 {
8370 if (rettv_list_alloc(rettv) == OK)
8371 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008372 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008373 }
8374 else
8375 {
8376 if (rettv_dict_alloc(rettv) == OK)
8377 if (is_qf || (wp != NULL))
8378 {
8379 if (what_arg->v_type == VAR_DICT)
8380 {
8381 dict_T *d = what_arg->vval.v_dict;
8382
8383 if (d != NULL)
8384 qf_get_properties(wp, d, rettv->vval.v_dict);
8385 }
8386 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008387 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008388 }
8389 }
8390}
8391# endif
8392
8393/*
8394 * "getloclist()" function
8395 */
8396 void
8397f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8398{
8399# ifdef FEAT_QUICKFIX
8400 win_T *wp;
8401
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008402 if (in_vim9script()
8403 && (check_for_number_arg(argvars, 0) == FAIL
8404 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8405 return;
8406
Bram Moolenaare677df82019-09-02 22:31:11 +02008407 wp = find_win_by_nr_or_id(&argvars[0]);
8408 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8409# endif
8410}
8411
8412/*
8413 * "getqflist()" function
8414 */
8415 void
8416f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8417{
8418# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008419 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8420 return;
8421
Bram Moolenaare677df82019-09-02 22:31:11 +02008422 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8423# endif
8424}
8425
8426/*
8427 * Used by "setqflist()" and "setloclist()" functions
8428 */
8429 static void
8430set_qf_ll_list(
8431 win_T *wp UNUSED,
8432 typval_T *list_arg UNUSED,
8433 typval_T *action_arg UNUSED,
8434 typval_T *what_arg UNUSED,
8435 typval_T *rettv)
8436{
8437# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008438 char_u *act;
8439 int action = 0;
8440 static int recursive = 0;
8441# endif
8442
8443 rettv->vval.v_number = -1;
8444
8445# ifdef FEAT_QUICKFIX
8446 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008447 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008448 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008449 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008450 else
8451 {
8452 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008453 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008454 int valid_dict = TRUE;
8455
8456 if (action_arg->v_type == VAR_STRING)
8457 {
8458 act = tv_get_string_chk(action_arg);
8459 if (act == NULL)
8460 return; // type error; errmsg already given
8461 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8462 act[1] == NUL)
8463 action = *act;
8464 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008465 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008466 }
8467 else if (action_arg->v_type == VAR_UNKNOWN)
8468 action = ' ';
8469 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008470 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008471
8472 if (action_arg->v_type != VAR_UNKNOWN
8473 && what_arg->v_type != VAR_UNKNOWN)
8474 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008475 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8476 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008477 else
8478 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008479 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008480 valid_dict = FALSE;
8481 }
8482 }
8483
8484 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008485 if (l != NULL && action && valid_dict
8486 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008487 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008488 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008489 rettv->vval.v_number = 0;
8490 --recursive;
8491 }
8492# endif
8493}
8494
8495/*
8496 * "setloclist()" function
8497 */
8498 void
8499f_setloclist(typval_T *argvars, typval_T *rettv)
8500{
8501 win_T *win;
8502
8503 rettv->vval.v_number = -1;
8504
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008505 if (in_vim9script()
8506 && (check_for_number_arg(argvars, 0) == FAIL
8507 || check_for_list_arg(argvars, 1) == FAIL
8508 || check_for_opt_string_arg(argvars, 2) == FAIL
8509 || (argvars[2].v_type != VAR_UNKNOWN
8510 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8511 return;
8512
Bram Moolenaare677df82019-09-02 22:31:11 +02008513 win = find_win_by_nr_or_id(&argvars[0]);
8514 if (win != NULL)
8515 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8516}
8517
8518/*
8519 * "setqflist()" function
8520 */
8521 void
8522f_setqflist(typval_T *argvars, typval_T *rettv)
8523{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008524 if (in_vim9script()
8525 && (check_for_list_arg(argvars, 0) == FAIL
8526 || check_for_opt_string_arg(argvars, 1) == FAIL
8527 || (argvars[1].v_type != VAR_UNKNOWN
8528 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8529 return;
8530
Bram Moolenaare677df82019-09-02 22:31:11 +02008531 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8532}
8533#endif