blob: 132d99216fe7c296d2016347c3c01df3cf976aa4 [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
Bram Moolenaard12f5c12006-01-25 22:10:52 +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
Bram Moolenaard12f5c12006-01-25 22:10:52 +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
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100197#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 */
206#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
207
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) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200211 for (i = 1, qfp = qfl->qf_start; \
212 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100213 ++i, qfp = qfp->qf_next)
214
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);
550 if ((fmtstr = alloc(sz)) == 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
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200556 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
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 {
631 state->growbuf = alloc(state->linelen + 1);
632 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/*
688 * Get the next string from state->p_Li.
689 */
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);
780 state->growbuf = alloc(state->growbufsiz);
781 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);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001385 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001386 == NULL)
1387 return QF_FAIL;
1388 STRCPY(ptr, qfprev->qf_text);
1389 vim_free(qfprev->qf_text);
1390 qfprev->qf_text = ptr;
1391 *(ptr += len) = '\n';
1392 STRCPY(++ptr, fields->errmsg);
1393 }
1394 if (qfprev->qf_nr == -1)
1395 qfprev->qf_nr = fields->enr;
1396 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001397 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001398 qfprev->qf_type = fields->type;
1399
1400 if (!qfprev->qf_lnum)
1401 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001402 if (!qfprev->qf_end_lnum)
1403 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001404 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001405 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001406 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001407 qfprev->qf_viscol = fields->use_viscol;
1408 }
haya14busae023d492022-02-08 18:09:29 +00001409 if (!qfprev->qf_end_col)
1410 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001411 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001412 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001413 qfl->qf_directory,
1414 *fields->namebuf || qfl->qf_directory != NULL
1415 ? fields->namebuf
1416 : qfl->qf_currfile != NULL && fields->valid
1417 ? qfl->qf_currfile : 0);
1418 }
1419 if (idx == 'Z')
1420 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1421 line_breakcheck();
1422
1423 return QF_IGNORE_LINE;
1424}
1425
1426/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001427 * Parse a line and get the quickfix fields.
1428 * Return the QF_ status.
1429 */
1430 static int
1431qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001432 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001433 char_u *linebuf,
1434 int linelen,
1435 efm_T *fmt_first,
1436 qffields_T *fields)
1437{
1438 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001439 int idx = 0;
1440 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001441 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001442
Bram Moolenaare333e792018-04-08 13:27:39 +02001443restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001444 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445 if (fmt_start == NULL)
1446 fmt_ptr = fmt_first;
1447 else
1448 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001449 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001450 fmt_ptr = fmt_start;
1451 fmt_start = NULL;
1452 }
1453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001454 // Try to match each part of 'errorformat' until we find a complete
1455 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001456 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001457 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1458 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001460 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1461 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1462 if (status == QF_NOMEM)
1463 return status;
1464 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001465 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001467 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468
1469 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1470 {
1471 if (fmt_ptr != NULL)
1472 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001473 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001474 status = qf_parse_dir_pfx(idx, fields, qfl);
1475 if (status != QF_OK)
1476 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001477 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001478
1479 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1480 if (status != QF_OK)
1481 return status;
1482
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001483 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001484 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001485 }
1486 else if (fmt_ptr != NULL)
1487 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001488 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001489 if (fmt_ptr->conthere)
1490 fmt_start = fmt_ptr;
1491
Bram Moolenaare9283662020-06-07 14:10:47 +02001492 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001493 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001494 qfl->qf_multiline = TRUE; // start of a multi-line message
1495 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001496 }
1497 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001498 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001499 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001500 if (status != QF_OK)
1501 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001502 }
1503 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001504 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001505 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1506 if (status == QF_MULTISCAN)
1507 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001508 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001509 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001510 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001511 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001512 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001513 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001514 return QF_IGNORE_LINE;
1515 }
1516 }
1517
1518 return QF_OK;
1519}
1520
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001521/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001522 * Returns TRUE if the specified quickfix/location stack is empty
1523 */
1524 static int
1525qf_stack_empty(qf_info_T *qi)
1526{
1527 return qi == NULL || qi->qf_listcount <= 0;
1528}
1529
1530/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001531 * Returns TRUE if the specified quickfix/location list is empty.
1532 */
1533 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001534qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001535{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001536 return qfl == NULL || qfl->qf_count <= 0;
1537}
1538
1539/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001540 * Returns TRUE if the specified quickfix/location list is not empty and
1541 * has valid entries.
1542 */
1543 static int
1544qf_list_has_valid_entries(qf_list_T *qfl)
1545{
1546 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1547}
1548
1549/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001550 * Return a pointer to a list in the specified quickfix stack
1551 */
1552 static qf_list_T *
1553qf_get_list(qf_info_T *qi, int idx)
1554{
1555 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001556}
1557
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001558/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001559 * Allocate the fields used for parsing lines and populating a quickfix list.
1560 */
1561 static int
1562qf_alloc_fields(qffields_T *pfields)
1563{
1564 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1565 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1566 pfields->errmsglen = CMDBUFFSIZE + 1;
1567 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1568 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1569 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1570 || pfields->pattern == NULL || pfields->module == NULL)
1571 return FAIL;
1572
1573 return OK;
1574}
1575
1576/*
1577 * Free the fields used for parsing lines and populating a quickfix list.
1578 */
1579 static void
1580qf_free_fields(qffields_T *pfields)
1581{
1582 vim_free(pfields->namebuf);
1583 vim_free(pfields->module);
1584 vim_free(pfields->errmsg);
1585 vim_free(pfields->pattern);
1586}
1587
1588/*
1589 * Setup the state information used for parsing lines and populating a
1590 * quickfix list.
1591 */
1592 static int
1593qf_setup_state(
1594 qfstate_T *pstate,
1595 char_u *enc,
1596 char_u *efile,
1597 typval_T *tv,
1598 buf_T *buf,
1599 linenr_T lnumfirst,
1600 linenr_T lnumlast)
1601{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001602 pstate->vc.vc_type = CONV_NONE;
1603 if (enc != NULL && *enc != NUL)
1604 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001605
1606 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1607 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001608 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001609 return FAIL;
1610 }
1611
1612 if (tv != NULL)
1613 {
1614 if (tv->v_type == VAR_STRING)
1615 pstate->p_str = tv->vval.v_string;
1616 else if (tv->v_type == VAR_LIST)
1617 pstate->p_li = tv->vval.v_list->lv_first;
1618 pstate->tv = tv;
1619 }
1620 pstate->buf = buf;
1621 pstate->buflnum = lnumfirst;
1622 pstate->lnumlast = lnumlast;
1623
1624 return OK;
1625}
1626
1627/*
1628 * Cleanup the state information used for parsing lines and populating a
1629 * quickfix list.
1630 */
1631 static void
1632qf_cleanup_state(qfstate_T *pstate)
1633{
1634 if (pstate->fd != NULL)
1635 fclose(pstate->fd);
1636
1637 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001638 if (pstate->vc.vc_type != CONV_NONE)
1639 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001640}
1641
1642/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001643 * Process the next line from a file/buffer/list/string and add it
1644 * to the quickfix list 'qfl'.
1645 */
1646 static int
1647qf_init_process_nextline(
1648 qf_list_T *qfl,
1649 efm_T *fmt_first,
1650 qfstate_T *state,
1651 qffields_T *fields)
1652{
1653 int status;
1654
1655 // Get the next line from a file/buffer/list/string
1656 status = qf_get_nextline(state);
1657 if (status != QF_OK)
1658 return status;
1659
1660 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1661 fmt_first, fields);
1662 if (status != QF_OK)
1663 return status;
1664
1665 return qf_add_entry(qfl,
1666 qfl->qf_directory,
1667 (*fields->namebuf || qfl->qf_directory != NULL)
1668 ? fields->namebuf
1669 : ((qfl->qf_currfile != NULL && fields->valid)
1670 ? qfl->qf_currfile : (char_u *)NULL),
1671 fields->module,
1672 0,
1673 fields->errmsg,
1674 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001675 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001676 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001677 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001678 fields->use_viscol,
1679 fields->pattern,
1680 fields->enr,
1681 fields->type,
1682 fields->valid);
1683}
1684
1685/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001686 * Read the errorfile "efile" into memory, line by line, building the error
1687 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001688 * Alternative: when "efile" is NULL read errors from buffer "buf".
1689 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001690 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001691 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1692 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001693 * Return -1 for error, number of errors for success.
1694 */
1695 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001696qf_init_ext(
1697 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001698 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001699 char_u *efile,
1700 buf_T *buf,
1701 typval_T *tv,
1702 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001703 int newlist, // TRUE: start a new error list
1704 linenr_T lnumfirst, // first line number to use
1705 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001706 char_u *qf_title,
1707 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001708{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001709 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001710 qfstate_T state;
1711 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001712 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001713 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001714 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001716 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001717 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001718 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001720 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001721 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001722
Bram Moolenaara80faa82020-04-12 19:37:17 +02001723 CLEAR_FIELD(state);
1724 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001725 if ((qf_alloc_fields(&fields) == FAIL) ||
1726 (qf_setup_state(&state, enc, efile, tv, buf,
1727 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 goto qf_init_end;
1729
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001730 if (newlist || qf_idx == qi->qf_listcount)
1731 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001732 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001733 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001734 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001735 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001736 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001737 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001738 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001739 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001740 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001741 qfl = qf_get_list(qi, qf_idx);
1742 if (!qf_list_empty(qfl))
1743 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001746 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001747 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001748 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 else
1750 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001752 // If the errorformat didn't change between calls, then reuse the
1753 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001754 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1755 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001756 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001757 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001758 free_efm_list(&fmt_first);
1759
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001760 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001761 fmt_first = parse_efm_option(efm);
1762 if (fmt_first != NULL)
1763 last_efm = vim_strsave(efm);
1764 }
1765
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001766 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001769 // got_int is reset here, because it was probably set when killing the
1770 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 got_int = FALSE;
1772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001773 // Read the lines in the error file one by one.
1774 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001775 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001777 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001778 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001779 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001780 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001781 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001782 if (status == QF_FAIL)
1783 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 line_breakcheck();
1786 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001787 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001789 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001791 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001792 qfl->qf_ptr = qfl->qf_start;
1793 qfl->qf_index = 1;
1794 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
1796 else
1797 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001798 qfl->qf_nonevalid = FALSE;
1799 if (qfl->qf_ptr == NULL)
1800 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001802 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001803 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001804 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001806 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001808 if (!adding)
1809 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001810 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001811 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001812 qi->qf_listcount--;
1813 if (qi->qf_curlist > 0)
1814 --qi->qf_curlist;
1815 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001816qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001817 if (qf_idx == qi->qf_curlist)
1818 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001819 qf_cleanup_state(&state);
1820 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 return retval;
1823}
1824
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001825/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001826 * Read the errorfile "efile" into memory, line by line, building the error
1827 * list. Set the error list's title to qf_title.
1828 * Return -1 for error, number of errors for success.
1829 */
1830 int
1831qf_init(win_T *wp,
1832 char_u *efile,
1833 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001834 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001835 char_u *qf_title,
1836 char_u *enc)
1837{
1838 qf_info_T *qi = &ql_info;
1839
1840 if (wp != NULL)
1841 {
1842 qi = ll_get_or_alloc_list(wp);
1843 if (qi == NULL)
1844 return FAIL;
1845 }
1846
1847 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1848 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1849}
1850
1851/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001852 * Set the title of the specified quickfix list. Frees the previous title.
1853 * Prepends ':' to the title.
1854 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001855 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001856qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001857{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001858 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001859
Bram Moolenaarfb604092014-07-23 15:55:00 +02001860 if (title != NULL)
1861 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001862 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001863
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001864 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001865 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001866 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001867 }
1868}
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001871 * The title of a quickfix/location list is set, by default, to the command
1872 * that created the quickfix list with the ":" prefix.
1873 * Create a quickfix list title string by prepending ":" to a user command.
1874 * Returns a pointer to a static buffer with the title.
1875 */
1876 static char_u *
1877qf_cmdtitle(char_u *cmd)
1878{
1879 static char_u qftitle_str[IOSIZE];
1880
1881 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1882 return qftitle_str;
1883}
1884
1885/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001886 * Return a pointer to the current list in the specified quickfix stack
1887 */
1888 static qf_list_T *
1889qf_get_curlist(qf_info_T *qi)
1890{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001891 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001892}
1893
1894/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001895 * Prepare for adding a new quickfix list. If the current list is in the
1896 * middle of the stack, then all the following lists are freed and then
1897 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 */
1899 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001900qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001903 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001905 // If the current entry is not the last entry, delete entries beyond
1906 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001907 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001908 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001909 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001911 // When the stack is full, remove to oldest entry
1912 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001915 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001917 qi->qf_lists[i - 1] = qi->qf_lists[i];
1918 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001922 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001923 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001924 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001925 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001926 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927}
1928
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001929/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001930 * Queue location list stack delete request.
1931 */
1932 static void
1933locstack_queue_delreq(qf_info_T *qi)
1934{
1935 qf_delq_T *q;
1936
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001937 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001938 if (q != NULL)
1939 {
1940 q->qi = qi;
1941 q->next = qf_delq_head;
1942 qf_delq_head = q;
1943 }
1944}
1945
1946/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001947 * Return the global quickfix stack window buffer number.
1948 */
1949 int
1950qf_stack_get_bufnr(void)
1951{
1952 return ql_info.qf_bufnr;
1953}
1954
1955/*
1956 * Wipe the quickfix window buffer (if present) for the specified
1957 * quickfix/location list.
1958 */
1959 static void
1960wipe_qf_buffer(qf_info_T *qi)
1961{
1962 buf_T *qfbuf;
1963
1964 if (qi->qf_bufnr != INVALID_QFBUFNR)
1965 {
1966 qfbuf = buflist_findnr(qi->qf_bufnr);
1967 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1968 {
1969 // If the quickfix buffer is not loaded in any window, then
1970 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001971 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001972 qi->qf_bufnr = INVALID_QFBUFNR;
1973 }
1974 }
1975}
1976
1977/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001978 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001979 */
1980 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001981ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001982{
1983 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001984 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001985
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001986 qi = *pqi;
1987 if (qi == NULL)
1988 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001989 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001991 // If the location list is still in use, then queue the delete request
1992 // to be processed later.
1993 if (quickfix_busy > 0)
1994 {
1995 locstack_queue_delreq(qi);
1996 return;
1997 }
1998
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001999 qi->qf_refcount--;
2000 if (qi->qf_refcount < 1)
2001 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002002 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002003 // If the quickfix window buffer is loaded, then wipe it
2004 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002005
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002006 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002007 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002008 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002010}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002011
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002012/*
2013 * Free all the quickfix/location lists in the stack.
2014 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002016qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017{
2018 int i;
2019 qf_info_T *qi = &ql_info;
2020
2021 if (wp != NULL)
2022 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002023 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002024 ll_free_all(&wp->w_llist);
2025 ll_free_all(&wp->w_llist_ref);
2026 }
2027 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002028 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002029 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002030 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002031}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002032
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002034 * Delay freeing of location list stacks when the quickfix code is running.
2035 * Used to avoid problems with autocmds freeing location list stacks when the
2036 * quickfix code is still referencing the stack.
2037 * Must always call decr_quickfix_busy() exactly once after this.
2038 */
2039 static void
2040incr_quickfix_busy(void)
2041{
2042 quickfix_busy++;
2043}
2044
2045/*
2046 * Safe to free location list stacks. Process any delayed delete requests.
2047 */
2048 static void
2049decr_quickfix_busy(void)
2050{
2051 if (--quickfix_busy == 0)
2052 {
2053 // No longer referencing the location lists. Process all the pending
2054 // delete requests.
2055 while (qf_delq_head != NULL)
2056 {
2057 qf_delq_T *q = qf_delq_head;
2058
2059 qf_delq_head = q->next;
2060 ll_free_all(&q->qi);
2061 vim_free(q);
2062 }
2063 }
2064#ifdef ABORT_ON_INTERNAL_ERROR
2065 if (quickfix_busy < 0)
2066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002067 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002068 abort();
2069 }
2070#endif
2071}
2072
2073#if defined(EXITFREE) || defined(PROTO)
2074 void
2075check_quickfix_busy(void)
2076{
2077 if (quickfix_busy != 0)
2078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002080# ifdef ABORT_ON_INTERNAL_ERROR
2081 abort();
2082# endif
2083 }
2084}
2085#endif
2086
2087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002089 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 */
2091 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002092qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002093 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002094 char_u *dir, // optional directory name
2095 char_u *fname, // file name or NULL
2096 char_u *module, // module name or NULL
2097 int bufnum, // buffer number or zero
2098 char_u *mesg, // message
2099 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002100 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002101 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002102 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002103 int vis_col, // using visual column
2104 char_u *pattern, // search pattern
2105 int nr, // error number
2106 int type, // type character
2107 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002109 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002110 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002112 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002113 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002114 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002115 {
2116 buf_T *buf = buflist_findnr(bufnum);
2117
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002118 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002119 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002120 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002121 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002122 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002123 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002124 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2126 {
2127 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002128 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002131 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002133 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002134 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002135 if (pattern == NULL || *pattern == NUL)
2136 qfp->qf_pattern = NULL;
2137 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2138 {
2139 vim_free(qfp->qf_text);
2140 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002141 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002142 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002143 if (module == NULL || *module == NUL)
2144 qfp->qf_module = NULL;
2145 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2146 {
2147 vim_free(qfp->qf_text);
2148 vim_free(qfp->qf_pattern);
2149 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002150 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002153 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 type = 0;
2155 qfp->qf_type = type;
2156 qfp->qf_valid = valid;
2157
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002158 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002159 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002161 qfl->qf_start = qfp;
2162 qfl->qf_ptr = qfp;
2163 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002164 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 else
2167 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002168 qfp->qf_prev = *lastp;
2169 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002171 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002173 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002174 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002175 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002177 qfl->qf_index = qfl->qf_count;
2178 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180
Bram Moolenaar95946f12019-03-31 15:31:59 +02002181 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182}
2183
2184/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002185 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002186 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002187 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002188qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002189{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002190 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002191
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002192 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002193 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002194 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002195 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002196 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002197 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002198 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002199 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002200}
2201
2202/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002203 * Return the location list stack for window 'wp'.
2204 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002205 */
2206 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002207ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002208{
2209 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002210 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 return wp->w_llist_ref;
2212
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002213 // For a non-location list window, w_llist_ref should not point to a
2214 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 ll_free_all(&wp->w_llist_ref);
2216
2217 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002218 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002219 return wp->w_llist;
2220}
2221
2222/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002223 * Get the quickfix/location list stack to use for the specified Ex command.
2224 * For a location list command, returns the stack for the current window. If
2225 * the location list is not found, then returns NULL and prints an error
2226 * message if 'print_emsg' is TRUE.
2227 */
2228 static qf_info_T *
2229qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2230{
2231 qf_info_T *qi = &ql_info;
2232
2233 if (is_loclist_cmd(eap->cmdidx))
2234 {
2235 qi = GET_LOC_LIST(curwin);
2236 if (qi == NULL)
2237 {
2238 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002239 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002240 return NULL;
2241 }
2242 }
2243
2244 return qi;
2245}
2246
2247/*
2248 * Get the quickfix/location list stack to use for the specified Ex command.
2249 * For a location list command, returns the stack for the current window.
2250 * If the location list is not present, then allocates a new one.
2251 * Returns NULL if the allocation fails. For a location list command, sets
2252 * 'pwinp' to curwin.
2253 */
2254 static qf_info_T *
2255qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2256{
2257 qf_info_T *qi = &ql_info;
2258
2259 if (is_loclist_cmd(eap->cmdidx))
2260 {
2261 qi = ll_get_or_alloc_list(curwin);
2262 if (qi == NULL)
2263 return NULL;
2264 *pwinp = curwin;
2265 }
2266
2267 return qi;
2268}
2269
2270/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002271 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2272 */
2273 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002274copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002275{
2276 int i;
2277 qfline_T *from_qfp;
2278 qfline_T *prevp;
2279
2280 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002281 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002282 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002283 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002284 NULL,
2285 NULL,
2286 from_qfp->qf_module,
2287 0,
2288 from_qfp->qf_text,
2289 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002290 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002291 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002292 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002293 from_qfp->qf_viscol,
2294 from_qfp->qf_pattern,
2295 from_qfp->qf_nr,
2296 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002297 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002298 return FAIL;
2299
2300 // qf_add_entry() will not set the qf_num field, as the
2301 // directory and file names are not supplied. So the qf_fnum
2302 // field is copied here.
2303 prevp = to_qfl->qf_last;
2304 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2305 prevp->qf_type = from_qfp->qf_type; // error type
2306 if (from_qfl->qf_ptr == from_qfp)
2307 to_qfl->qf_ptr = prevp; // current location
2308 }
2309
2310 return OK;
2311}
2312
2313/*
2314 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2315 */
2316 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002317copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002318{
2319 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002320 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002321 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2322 to_qfl->qf_count = 0;
2323 to_qfl->qf_index = 0;
2324 to_qfl->qf_start = NULL;
2325 to_qfl->qf_last = NULL;
2326 to_qfl->qf_ptr = NULL;
2327 if (from_qfl->qf_title != NULL)
2328 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2329 else
2330 to_qfl->qf_title = NULL;
2331 if (from_qfl->qf_ctx != NULL)
2332 {
2333 to_qfl->qf_ctx = alloc_tv();
2334 if (to_qfl->qf_ctx != NULL)
2335 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2336 }
2337 else
2338 to_qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002339 if (from_qfl->qftf_cb.cb_name != NULL)
2340 copy_callback(&to_qfl->qftf_cb, &from_qfl->qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002341 else
Bram Moolenaard43906d2020-07-20 21:31:32 +02002342 to_qfl->qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002343
2344 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002345 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002346 return FAIL;
2347
2348 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2349
2350 // Assign a new ID for the location list
2351 to_qfl->qf_id = ++last_qf_id;
2352 to_qfl->qf_changedtick = 0L;
2353
2354 // When no valid entries are present in the list, qf_ptr points to
2355 // the first item in the list
2356 if (to_qfl->qf_nonevalid)
2357 {
2358 to_qfl->qf_ptr = to_qfl->qf_start;
2359 to_qfl->qf_index = 1;
2360 }
2361
2362 return OK;
2363}
2364
2365/*
2366 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 */
2368 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002369copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002370{
2371 qf_info_T *qi;
2372 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002373
Bram Moolenaar09037502018-09-25 22:08:14 +02002374 // When copying from a location list window, copy the referenced
2375 // location list. For other windows, copy the location list for
2376 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002377 if (IS_LL_WINDOW(from))
2378 qi = from->w_llist_ref;
2379 else
2380 qi = from->w_llist;
2381
Bram Moolenaar09037502018-09-25 22:08:14 +02002382 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002383 return;
2384
Bram Moolenaar09037502018-09-25 22:08:14 +02002385 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002386 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002387 return;
2388
2389 to->w_llist->qf_listcount = qi->qf_listcount;
2390
Bram Moolenaar09037502018-09-25 22:08:14 +02002391 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002392 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002393 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002394 to->w_llist->qf_curlist = idx;
2395
Bram Moolenaar0398e002019-03-21 21:12:49 +01002396 if (copy_loclist(qf_get_list(qi, idx),
2397 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002398 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002399 qf_free_all(to);
2400 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002401 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 }
2403
Bram Moolenaar09037502018-09-25 22:08:14 +02002404 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405}
2406
2407/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002408 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002409 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 */
2411 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002412qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
Bram Moolenaar82404332016-07-10 17:00:38 +02002414 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002415 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002416 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002417
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002418 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaare60acc12011-05-10 16:41:25 +02002421#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002422 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002423#endif
2424#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002425 if (directory != NULL)
2426 slash_adjust(directory);
2427 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002428#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002429 if (directory != NULL && !vim_isAbsName(fname)
2430 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2431 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002432 // Here we check if the file really exists.
2433 // This should normally be true, but if make works without
2434 // "leaving directory"-messages we might have missed a
2435 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002436 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002439 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002440 if (directory)
2441 ptr = concat_fnames(directory, fname, TRUE);
2442 else
2443 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002445 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002446 bufname = ptr;
2447 }
2448 else
2449 bufname = fname;
2450
2451 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002452 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002453 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002454 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002455 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002457 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002458 {
2459 vim_free(qf_last_bufname);
2460 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2461 if (bufname == ptr)
2462 qf_last_bufname = bufname;
2463 else
2464 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002465 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002466 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002467 if (buf == NULL)
2468 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002469
Bram Moolenaarc1542742016-07-20 21:44:37 +02002470 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002471 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002472 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473}
2474
2475/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002476 * Push dirbuf onto the directory stack and return pointer to actual dir or
2477 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
2479 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002480qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 struct dir_stack_T *ds_new;
2483 struct dir_stack_T *ds_ptr;
2484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002485 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002486 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (ds_new == NULL)
2488 return NULL;
2489
2490 ds_new->next = *stackptr;
2491 *stackptr = ds_new;
2492
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002493 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (vim_isAbsName(dirbuf)
2495 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002496 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 (*stackptr)->dirname = vim_strsave(dirbuf);
2498 else
2499 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002500 // Okay we don't have an absolute path.
2501 // dirbuf must be a subdir of one of the directories on the stack.
2502 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 ds_new = (*stackptr)->next;
2504 (*stackptr)->dirname = NULL;
2505 while (ds_new)
2506 {
2507 vim_free((*stackptr)->dirname);
2508 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2509 TRUE);
2510 if (mch_isdir((*stackptr)->dirname) == TRUE)
2511 break;
2512
2513 ds_new = ds_new->next;
2514 }
2515
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002516 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 while ((*stackptr)->next != ds_new)
2518 {
2519 ds_ptr = (*stackptr)->next;
2520 (*stackptr)->next = (*stackptr)->next->next;
2521 vim_free(ds_ptr->dirname);
2522 vim_free(ds_ptr);
2523 }
2524
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002525 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (ds_new == NULL)
2527 {
2528 vim_free((*stackptr)->dirname);
2529 (*stackptr)->dirname = vim_strsave(dirbuf);
2530 }
2531 }
2532
2533 if ((*stackptr)->dirname != NULL)
2534 return (*stackptr)->dirname;
2535 else
2536 {
2537 ds_ptr = *stackptr;
2538 *stackptr = (*stackptr)->next;
2539 vim_free(ds_ptr);
2540 return NULL;
2541 }
2542}
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544/*
2545 * pop dirbuf from the directory stack and return previous directory or NULL if
2546 * stack is empty
2547 */
2548 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002549qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 struct dir_stack_T *ds_ptr;
2552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002553 // TODO: Should we check if dirbuf is the directory on top of the stack?
2554 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002556 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (*stackptr != NULL)
2558 {
2559 ds_ptr = *stackptr;
2560 *stackptr = (*stackptr)->next;
2561 vim_free(ds_ptr->dirname);
2562 vim_free(ds_ptr);
2563 }
2564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002565 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 return *stackptr ? (*stackptr)->dirname : NULL;
2567}
2568
2569/*
2570 * clean up directory stack
2571 */
2572 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002573qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 struct dir_stack_T *ds_ptr;
2576
2577 while ((ds_ptr = *stackptr) != NULL)
2578 {
2579 *stackptr = (*stackptr)->next;
2580 vim_free(ds_ptr->dirname);
2581 vim_free(ds_ptr);
2582 }
2583}
2584
2585/*
2586 * Check in which directory of the directory stack the given file can be
2587 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002588 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 * Cleans up intermediate directory entries.
2590 *
2591 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002592 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 * ./
2594 * ./aa
2595 * ./aa/bb
2596 * ./bb
2597 * ./bb/x.c
2598 * and make says:
2599 * making all in aa
2600 * making all in bb
2601 * x.c:9: Error
2602 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2603 * qf_guess_filepath will return NULL.
2604 */
2605 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002606qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 struct dir_stack_T *ds_ptr;
2609 struct dir_stack_T *ds_tmp;
2610 char_u *fullname;
2611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002612 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002613 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 return NULL;
2615
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002616 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 fullname = NULL;
2618 while (ds_ptr)
2619 {
2620 vim_free(fullname);
2621 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2622
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002623 // If concat_fnames failed, just go on. The worst thing that can happen
2624 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2626 break;
2627
2628 ds_ptr = ds_ptr->next;
2629 }
2630
2631 vim_free(fullname);
2632
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002633 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002634 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002636 ds_tmp = qfl->qf_dir_stack->next;
2637 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 vim_free(ds_tmp->dirname);
2639 vim_free(ds_tmp);
2640 }
2641
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002642 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643}
2644
2645/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002646 * Returns TRUE if a quickfix/location list with the given identifier exists.
2647 */
2648 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002649qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002650{
2651 qf_info_T *qi = &ql_info;
2652 int i;
2653
2654 if (wp != NULL)
2655 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002656 if (!win_valid(wp))
2657 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002658 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002659 if (qi == NULL)
2660 return FALSE;
2661 }
2662
2663 for (i = 0; i < qi->qf_listcount; ++i)
2664 if (qi->qf_lists[i].qf_id == qf_id)
2665 return TRUE;
2666
2667 return FALSE;
2668}
2669
2670/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002671 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002672 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002673 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002674 * Similar to location list.
2675 */
2676 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002677is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002678{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002679 qfline_T *qfp;
2680 int i;
2681
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002682 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002683 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2684 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002685 break;
2686
Bram Moolenaar95946f12019-03-31 15:31:59 +02002687 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002688 return FALSE;
2689
2690 return TRUE;
2691}
2692
2693/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002694 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002695 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002696 */
2697 static qfline_T *
2698get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002699 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002700 qfline_T *qf_ptr,
2701 int *qf_index,
2702 int dir)
2703{
2704 int idx;
2705 int old_qf_fnum;
2706
2707 idx = *qf_index;
2708 old_qf_fnum = qf_ptr->qf_fnum;
2709
2710 do
2711 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002712 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002713 return NULL;
2714 ++idx;
2715 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002716 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002717 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2718
2719 *qf_index = idx;
2720 return qf_ptr;
2721}
2722
2723/*
2724 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002725 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726 */
2727 static qfline_T *
2728get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002729 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002730 qfline_T *qf_ptr,
2731 int *qf_index,
2732 int dir)
2733{
2734 int idx;
2735 int old_qf_fnum;
2736
2737 idx = *qf_index;
2738 old_qf_fnum = qf_ptr->qf_fnum;
2739
2740 do
2741 {
2742 if (idx == 1 || qf_ptr->qf_prev == NULL)
2743 return NULL;
2744 --idx;
2745 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002746 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002747 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2748
2749 *qf_index = idx;
2750 return qf_ptr;
2751}
2752
2753/*
2754 * Get the n'th (errornr) previous/next valid entry from the current entry in
2755 * the quickfix list.
2756 * dir == FORWARD or FORWARD_FILE: next valid entry
2757 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2758 */
2759 static qfline_T *
2760get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002761 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002762 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002763 int dir,
2764 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002765{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002766 qfline_T *qf_ptr = qfl->qf_ptr;
2767 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002768 qfline_T *prev_qf_ptr;
2769 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002770 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002771
2772 while (errornr--)
2773 {
2774 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002775 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002776
2777 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002778 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002779 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002780 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002781 if (qf_ptr == NULL)
2782 {
2783 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002784 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002785 if (err != NULL)
2786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002787 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002788 return NULL;
2789 }
2790 break;
2791 }
2792
2793 err = NULL;
2794 }
2795
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002796 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002797 return qf_ptr;
2798}
2799
2800/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002801 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2802 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002803 */
2804 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002805get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002806{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002807 qfline_T *qf_ptr = qfl->qf_ptr;
2808 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002809
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002810 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002811 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2812 {
2813 --qf_idx;
2814 qf_ptr = qf_ptr->qf_prev;
2815 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002816 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002817 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2818 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002819 {
2820 ++qf_idx;
2821 qf_ptr = qf_ptr->qf_next;
2822 }
2823
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002824 *new_qfidx = qf_idx;
2825 return qf_ptr;
2826}
2827
2828/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002829 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002830 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2831 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2832 * Returns a pointer to the entry and the index of the new entry is stored in
2833 * 'new_qfidx'.
2834 */
2835 static qfline_T *
2836qf_get_entry(
2837 qf_list_T *qfl,
2838 int errornr,
2839 int dir,
2840 int *new_qfidx)
2841{
2842 qfline_T *qf_ptr = qfl->qf_ptr;
2843 int qfidx = qfl->qf_index;
2844
2845 if (dir != 0) // next/prev valid entry
2846 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2847 else if (errornr != 0) // go to specified number
2848 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2849
2850 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002851 return qf_ptr;
2852}
2853
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002854/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002855 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002856 */
2857 static win_T *
2858qf_find_help_win(void)
2859{
2860 win_T *wp;
2861
2862 FOR_ALL_WINDOWS(wp)
2863 if (bt_help(wp->w_buffer))
2864 return wp;
2865
2866 return NULL;
2867}
2868
2869/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002870 * Set the location list for the specified window to 'qi'.
2871 */
2872 static void
2873win_set_loclist(win_T *wp, qf_info_T *qi)
2874{
2875 wp->w_llist = qi;
2876 qi->qf_refcount++;
2877}
2878
2879/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002880 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2881 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002882 */
2883 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002884jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002885{
2886 win_T *wp;
2887 int flags;
2888
Bram Moolenaare1004402020-10-24 20:49:43 +02002889 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002890 wp = NULL;
2891 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002892 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002893 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2894 win_enter(wp, TRUE);
2895 else
2896 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002897 // Split off help window; put it at far top if no position
2898 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002899 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002900 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002901 && curwin->w_width < 80)
2902 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002903 // If the user asks to open a new window, then copy the location list.
2904 // Otherwise, don't copy the location list.
2905 if (IS_LL_STACK(qi) && !newwin)
2906 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002907
2908 if (win_split(0, flags) == FAIL)
2909 return FAIL;
2910
2911 *opened_window = TRUE;
2912
2913 if (curwin->w_height < p_hh)
2914 win_setheight((int)p_hh);
2915
Bram Moolenaarb2443732018-11-11 22:50:27 +01002916 // When using location list, the new window should use the supplied
2917 // location list. If the user asks to open a new window, then the new
2918 // window will get a copy of the location list.
2919 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002920 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002921 }
2922
2923 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002924 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002925
2926 return OK;
2927}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002928
2929/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002930 * Find a non-quickfix window using the given location list stack in the
2931 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002932 * Returns NULL if a matching window is not found.
2933 */
2934 static win_T *
2935qf_find_win_with_loclist(qf_info_T *ll)
2936{
2937 win_T *wp;
2938
2939 FOR_ALL_WINDOWS(wp)
2940 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2941 return wp;
2942
2943 return NULL;
2944}
2945
2946/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002947 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002948 */
2949 static win_T *
2950qf_find_win_with_normal_buf(void)
2951{
2952 win_T *wp;
2953
2954 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002955 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002956 return wp;
2957
2958 return NULL;
2959}
2960
2961/*
2962 * Go to a window in any tabpage containing the specified file. Returns TRUE
2963 * if successfully jumped to the window. Otherwise returns FALSE.
2964 */
2965 static int
2966qf_goto_tabwin_with_file(int fnum)
2967{
2968 tabpage_T *tp;
2969 win_T *wp;
2970
2971 FOR_ALL_TAB_WINDOWS(tp, wp)
2972 if (wp->w_buffer->b_fnum == fnum)
2973 {
2974 goto_tabpage_win(tp, wp);
2975 return TRUE;
2976 }
2977
2978 return FALSE;
2979}
2980
2981/*
2982 * Create a new window to show a file above the quickfix window. Called when
2983 * only the quickfix window is present.
2984 */
2985 static int
2986qf_open_new_file_win(qf_info_T *ll_ref)
2987{
2988 int flags;
2989
2990 flags = WSP_ABOVE;
2991 if (ll_ref != NULL)
2992 flags |= WSP_NEWLOC;
2993 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002994 return FAIL; // not enough room for window
2995 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002996 swb_flags = 0;
2997 RESET_BINDING(curwin);
2998 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002999 // The new window should use the location list from the
3000 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003001 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003002 return OK;
3003}
3004
3005/*
3006 * Go to a window that shows the right buffer. If the window is not found, go
3007 * to the window just above the location list window. This is used for opening
3008 * a file from a location window and not from a quickfix window. If some usable
3009 * window is previously found, then it is supplied in 'use_win'.
3010 */
3011 static void
3012qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3013{
3014 win_T *win = use_win;
3015
3016 if (win == NULL)
3017 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003018 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003019 FOR_ALL_WINDOWS(win)
3020 if (win->w_buffer->b_fnum == qf_fnum)
3021 break;
3022 if (win == NULL)
3023 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003024 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003025 win = curwin;
3026 do
3027 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003028 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003029 break;
3030 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003031 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003032 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003033 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003034 } while (win != curwin);
3035 }
3036 }
3037 win_goto(win);
3038
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003039 // If the location list for the window is not set, then set it
3040 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003041 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003042 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003043}
3044
3045/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003046 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3047 * not found, then go to the window just above the quickfix window. This is
3048 * used for opening a file from a quickfix window and not from a location
3049 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003050 */
3051 static void
3052qf_goto_win_with_qfl_file(int qf_fnum)
3053{
3054 win_T *win;
3055 win_T *altwin;
3056
3057 win = curwin;
3058 altwin = NULL;
3059 for (;;)
3060 {
3061 if (win->w_buffer->b_fnum == qf_fnum)
3062 break;
3063 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003064 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003065 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003066 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003067
3068 if (IS_QF_WINDOW(win))
3069 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003070 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003071 // window, unless 'switchbuf' contains 'uselast': in this case we
3072 // try to jump to the previously used window first.
3073 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3074 win = prevwin;
3075 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003076 win = altwin;
3077 else if (curwin->w_prev != NULL)
3078 win = curwin->w_prev;
3079 else
3080 win = curwin->w_next;
3081 break;
3082 }
3083
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003084 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003085 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003086 altwin = win;
3087 }
3088
3089 win_goto(win);
3090}
3091
3092/*
3093 * Find a suitable window for opening a file (qf_fnum) from the
3094 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003095 * window, jump to it. Otherwise open a new window to display the file. If
3096 * 'newwin' is TRUE, then always open a new window. This is called from either
3097 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003098 */
3099 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003100qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003101{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003102 win_T *usable_wp = NULL;
3103 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003104 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003105
Bram Moolenaarb2443732018-11-11 22:50:27 +01003106 // If opening a new window, then don't use the location list referred by
3107 // the current window. Otherwise two windows will refer to the same
3108 // location list.
3109 if (!newwin)
3110 ll_ref = curwin->w_llist_ref;
3111
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003112 if (ll_ref != NULL)
3113 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003114 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003115 usable_wp = qf_find_win_with_loclist(ll_ref);
3116 if (usable_wp != NULL)
3117 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003118 }
3119
3120 if (!usable_win)
3121 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003122 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003123 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003124 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003125 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003126 }
3127
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003128 // If no usable window is found and 'switchbuf' contains "usetab"
3129 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003130 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003131 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003132
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003133 // If there is only one window and it is the quickfix window, create a
3134 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003135 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003136 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003137 if (qf_open_new_file_win(ll_ref) != OK)
3138 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003139 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003140 }
3141 else
3142 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003143 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003144 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003145 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003146 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003147 }
3148
3149 return OK;
3150}
3151
3152/*
3153 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003154 * Returns OK if successfully edited the file, FAIL on failing to open the
3155 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3156 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003157 */
3158 static int
3159qf_jump_edit_buffer(
3160 qf_info_T *qi,
3161 qfline_T *qf_ptr,
3162 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003163 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003164 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003165{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003166 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003167 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003168 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003170 int old_qf_curlist = qi->qf_curlist;
3171 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003172
3173 if (qf_ptr->qf_type == 1)
3174 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003175 // Open help file (do_ecmd() will set b_help flag, readfile() will
3176 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003177 if (!can_abandon(curbuf, forceit))
3178 {
3179 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003180 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003181 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003182
3183 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3184 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003185 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003186 }
3187 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003188 retval = buflist_getfile(qf_ptr->qf_fnum,
3189 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003190
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003191 // If a location list, check whether the associated window is still
3192 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003193 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003194 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003195 win_T *wp = win_id2wp(prev_winid);
3196 if (wp == NULL && curwin->w_llist != qi)
3197 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003198 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003199 *opened_window = FALSE;
3200 return NOTDONE;
3201 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003202 }
3203
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003204 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003205 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003206 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003207 return NOTDONE;
3208 }
3209
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003210 // Check if the list was changed. The pointers may happen to be identical,
3211 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003212 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003213 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003214 || !is_qf_entry_present(qfl, qf_ptr))
3215 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003216 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003217 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003218 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003219 emsg(_(e_current_location_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003220 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003221 }
3222
3223 return retval;
3224}
3225
3226/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003227 * Go to the error line in the current file using either line/column number or
3228 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003229 */
3230 static void
3231qf_jump_goto_line(
3232 linenr_T qf_lnum,
3233 int qf_col,
3234 char_u qf_viscol,
3235 char_u *qf_pattern)
3236{
3237 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003238
3239 if (qf_pattern == NULL)
3240 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003241 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003242 i = qf_lnum;
3243 if (i > 0)
3244 {
3245 if (i > curbuf->b_ml.ml_line_count)
3246 i = curbuf->b_ml.ml_line_count;
3247 curwin->w_cursor.lnum = i;
3248 }
3249 if (qf_col > 0)
3250 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003251 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003252 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003253 coladvance(qf_col - 1);
3254 else
3255 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003256 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003257 check_cursor();
3258 }
3259 else
3260 beginline(BL_WHITE | BL_FIX);
3261 }
3262 else
3263 {
3264 pos_T save_cursor;
3265
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003266 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003267 save_cursor = curwin->w_cursor;
3268 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003269 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003270 curwin->w_cursor = save_cursor;
3271 }
3272}
3273
3274/*
3275 * Display quickfix list index and size message
3276 */
3277 static void
3278qf_jump_print_msg(
3279 qf_info_T *qi,
3280 int qf_index,
3281 qfline_T *qf_ptr,
3282 buf_T *old_curbuf,
3283 linenr_T old_lnum)
3284{
3285 linenr_T i;
3286 int len;
3287
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003288 // Update the screen before showing the message, unless the screen
3289 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003290 if (!msg_scrolled)
3291 update_topline_redraw();
3292 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003293 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003294 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3295 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003296 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003297 len = (int)STRLEN(IObuff);
3298 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3299
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003300 // Output the message. Overwrite to avoid scrolling when the 'O'
3301 // flag is present in 'shortmess'; But when not jumping, print the
3302 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003303 i = msg_scroll;
3304 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3305 msg_scroll = TRUE;
3306 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3307 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003308 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003309 msg_scroll = i;
3310}
3311
3312/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003313 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003314 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3315 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003316 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3317 * able to jump/open a window. Returns NOTDONE if a file is not associated
3318 * with the entry.
3319 */
3320 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003321qf_jump_open_window(
3322 qf_info_T *qi,
3323 qfline_T *qf_ptr,
3324 int newwin,
3325 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003326{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003327 qf_list_T *qfl = qf_get_curlist(qi);
3328 int old_changedtick = qfl->qf_changedtick;
3329 int old_qf_curlist = qi->qf_curlist;
3330 qfltype_T qfl_type = qfl->qfl_type;
3331
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003332 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003333 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3334 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003335 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003336 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003337 if (old_qf_curlist != qi->qf_curlist
3338 || old_changedtick != qfl->qf_changedtick
3339 || !is_qf_entry_present(qfl, qf_ptr))
3340 {
3341 if (qfl_type == QFLT_QUICKFIX)
3342 emsg(_(e_current_quickfix_list_was_changed));
3343 else
3344 emsg(_(e_current_location_list_was_changed));
3345 return FAIL;
3346 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003347
3348 // If currently in the quickfix window, find another window to show the
3349 // file in.
3350 if (bt_quickfix(curbuf) && !*opened_window)
3351 {
3352 // If there is no file specified, we don't know where to go.
3353 // But do advance, otherwise ":cn" gets stuck.
3354 if (qf_ptr->qf_fnum == 0)
3355 return NOTDONE;
3356
Bram Moolenaarb2443732018-11-11 22:50:27 +01003357 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3358 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003359 return FAIL;
3360 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003361 if (old_qf_curlist != qi->qf_curlist
3362 || old_changedtick != qfl->qf_changedtick
3363 || !is_qf_entry_present(qfl, qf_ptr))
3364 {
3365 if (qfl_type == QFLT_QUICKFIX)
3366 emsg(_(e_current_quickfix_list_was_changed));
3367 else
3368 emsg(_(e_current_location_list_was_changed));
3369 return FAIL;
3370 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003371
3372 return OK;
3373}
3374
3375/*
3376 * Edit a selected file from the quickfix/location list and jump to a
3377 * particular line/column, adjust the folds and display a message about the
3378 * jump.
3379 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3380 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3381 * the file.
3382 */
3383 static int
3384qf_jump_to_buffer(
3385 qf_info_T *qi,
3386 int qf_index,
3387 qfline_T *qf_ptr,
3388 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003389 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003390 int *opened_window,
3391 int openfold,
3392 int print_message)
3393{
3394 buf_T *old_curbuf;
3395 linenr_T old_lnum;
3396 int retval = OK;
3397
3398 // If there is a file name, read the wanted file if needed, and check
3399 // autowrite etc.
3400 old_curbuf = curbuf;
3401 old_lnum = curwin->w_cursor.lnum;
3402
3403 if (qf_ptr->qf_fnum != 0)
3404 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003405 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003406 opened_window);
3407 if (retval != OK)
3408 return retval;
3409 }
3410
3411 // When not switched to another buffer, still need to set pc mark
3412 if (curbuf == old_curbuf)
3413 setpcmark();
3414
3415 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3416 qf_ptr->qf_pattern);
3417
3418#ifdef FEAT_FOLDING
3419 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3420 foldOpenCursor();
3421#endif
3422 if (print_message)
3423 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3424
3425 return retval;
3426}
3427
3428/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003429 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
3431 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003432qf_jump(qf_info_T *qi,
3433 int dir,
3434 int errornr,
3435 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003437 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3438}
3439
3440/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003441 * Jump to a quickfix line.
3442 * If dir == 0 go to entry "errornr".
3443 * If dir == FORWARD go "errornr" valid entries forward.
3444 * If dir == BACKWARD go "errornr" valid entries backward.
3445 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3446 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3447 * else if "errornr" is zero, redisplay the same line
3448 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003449 * If 'newwin' is TRUE, then open the file in a new window.
3450 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003451 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003452qf_jump_newwin(qf_info_T *qi,
3453 int dir,
3454 int errornr,
3455 int forceit,
3456 int newwin)
3457{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003458 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003459 qfline_T *qf_ptr;
3460 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003463 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003464 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003465 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003468 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003469 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003471 if (qi == NULL)
3472 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003473
Bram Moolenaar0398e002019-03-21 21:12:49 +01003474 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003476 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return;
3478 }
3479
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003480 incr_quickfix_busy();
3481
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003482 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003483
3484 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003486 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003488
3489 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3490 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003492 qf_ptr = old_qf_ptr;
3493 qf_index = old_qf_index;
3494 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003497 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003498 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003499 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003500 // No need to print the error message if it's visible in the error
3501 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 print_message = FALSE;
3503
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003504 prev_winid = curwin->w_id;
3505
Bram Moolenaarb2443732018-11-11 22:50:27 +01003506 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003507 if (retval == FAIL)
3508 goto failed;
3509 if (retval == NOTDONE)
3510 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003511
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003512 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003513 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003514 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003516 // Quickfix/location list is freed by an autocmd
3517 qi = NULL;
3518 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003521 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003524 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003525 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003527 // Couldn't open file, so put index back where it was. This could
3528 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 qf_ptr = old_qf_ptr;
3531 qf_index = old_qf_index;
3532 }
3533 }
3534theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003535 if (qi != NULL)
3536 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003537 qfl->qf_ptr = qf_ptr;
3538 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003539 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003540 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003542 // Restore old 'switchbuf' value, but not when an autocommand or
3543 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003544 p_swb = old_swb;
3545 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003547 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548}
3549
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003550// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003551static int qfFileAttr;
3552static int qfSepAttr;
3553static int qfLineAttr;
3554
3555/*
3556 * Display information about a single entry from the quickfix/location list.
3557 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003558 * 'cursel' will be set to TRUE for the currently selected entry in the
3559 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003560 */
3561 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003562qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003563{
3564 char_u *fname;
3565 buf_T *buf;
3566 int filter_entry;
3567
3568 fname = NULL;
3569 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3570 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3571 (char *)qfp->qf_module);
3572 else {
3573 if (qfp->qf_fnum != 0
3574 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3575 {
3576 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003577 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003578 fname = gettail(fname);
3579 }
3580 if (fname == NULL)
3581 sprintf((char *)IObuff, "%2d", qf_idx);
3582 else
3583 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3584 qf_idx, (char *)fname);
3585 }
3586
3587 // Support for filtering entries using :filter /pat/ clist
3588 // Match against the module name, file name, search pattern and
3589 // text of the entry.
3590 filter_entry = TRUE;
3591 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3592 filter_entry &= message_filtered(qfp->qf_module);
3593 if (filter_entry && fname != NULL)
3594 filter_entry &= message_filtered(fname);
3595 if (filter_entry && qfp->qf_pattern != NULL)
3596 filter_entry &= message_filtered(qfp->qf_pattern);
3597 if (filter_entry)
3598 filter_entry &= message_filtered(qfp->qf_text);
3599 if (filter_entry)
3600 return;
3601
3602 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003603 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003604
3605 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003606 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003607 if (qfp->qf_lnum == 0)
3608 IObuff[0] = NUL;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003609 else
thinca6864efa2021-06-19 20:45:20 +02003610 qf_range_text(qfp, IObuff, IOSIZE);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003611 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3612 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003613 msg_puts_attr((char *)IObuff, qfLineAttr);
3614 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003615 if (qfp->qf_pattern != NULL)
3616 {
3617 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003618 msg_puts((char *)IObuff);
3619 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003620 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003621 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003622
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003623 // Remove newlines and leading whitespace from the text. For an
3624 // unrecognized line keep the indent, the compiler may mark a word
3625 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003626 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3627 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3628 IObuff, IOSIZE);
3629 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003630 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003631}
3632
3633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003635 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
3637 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003638qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003640 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003641 qfline_T *qfp;
3642 int i;
3643 int idx1 = 1;
3644 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003645 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003646 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003647 int all = eap->forceit; // if not :cl!, only show
3648 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003649 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003651 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3652 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003653
Bram Moolenaar0398e002019-03-21 21:12:49 +01003654 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003656 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 return;
3658 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003659 if (*arg == '+')
3660 {
3661 ++arg;
3662 plus = TRUE;
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3665 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003666 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return;
3668 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003669 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003670 if (plus)
3671 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003672 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003673 idx2 = i + idx1;
3674 idx1 = i;
3675 }
3676 else
3677 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003678 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003679 if (idx1 < 0)
3680 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3681 if (idx2 < 0)
3682 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003685 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003686 shorten_fnames(FALSE);
3687
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003688 // Get the attributes for the different quickfix highlight items. Note
3689 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003690 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3691 if (qfFileAttr == 0)
3692 qfFileAttr = HL_ATTR(HLF_D);
3693 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3694 if (qfSepAttr == 0)
3695 qfSepAttr = HL_ATTR(HLF_D);
3696 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3697 if (qfLineAttr == 0)
3698 qfLineAttr = HL_ATTR(HLF_N);
3699
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003700 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003702 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003705 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 ui_breakcheck();
3708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709}
3710
3711/*
3712 * Remove newlines and leading whitespace from an error message.
3713 * Put the result in "buf[bufsize]".
3714 */
3715 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003716qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
3718 int i;
3719 char_u *p = text;
3720
3721 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3722 {
3723 if (*p == '\n')
3724 {
3725 buf[i] = ' ';
3726 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003727 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 break;
3729 }
3730 else
3731 buf[i] = *p++;
3732 }
3733 buf[i] = NUL;
3734}
3735
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003736/*
thinca6864efa2021-06-19 20:45:20 +02003737 * Range information from lnum, col, end_lnum, and end_col.
3738 * Put the result in "buf[bufsize]".
3739 */
3740 static void
3741qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3742{
3743 int len;
3744 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3745 len = (int)STRLEN(buf);
3746
3747 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3748 {
3749 vim_snprintf((char *)buf + len, bufsize - len,
3750 "-%ld", qfp->qf_end_lnum);
3751 len += (int)STRLEN(buf + len);
3752 }
3753 if (qfp->qf_col > 0)
3754 {
3755 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3756 len += (int)STRLEN(buf + len);
3757 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3758 {
3759 vim_snprintf((char *)buf + len, bufsize - len,
3760 "-%d", qfp->qf_end_col);
3761 len += (int)STRLEN(buf + len);
3762 }
3763 }
3764 buf[len] = NUL;
3765}
3766
3767/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003768 * Display information (list number, list size and the title) about a
3769 * quickfix/location list.
3770 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003771 static void
3772qf_msg(qf_info_T *qi, int which, char *lead)
3773{
3774 char *title = (char *)qi->qf_lists[which].qf_title;
3775 int count = qi->qf_lists[which].qf_count;
3776 char_u buf[IOSIZE];
3777
3778 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3779 lead,
3780 which + 1,
3781 qi->qf_listcount,
3782 count);
3783
3784 if (title != NULL)
3785 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003786 size_t len = STRLEN(buf);
3787
3788 if (len < 34)
3789 {
3790 vim_memset(buf + len, ' ', 34 - len);
3791 buf[34] = NUL;
3792 }
3793 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003794 }
3795 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003796 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003797}
3798
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799/*
3800 * ":colder [count]": Up in the quickfix stack.
3801 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802 * ":lolder [count]": Up in the location list stack.
3803 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 */
3805 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003806qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003808 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 int count;
3810
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003811 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3812 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (eap->addr_count != 0)
3815 count = eap->line2;
3816 else
3817 count = 1;
3818 while (count--)
3819 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003820 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003822 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003824 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003825 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003827 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829 else
3830 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003831 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003833 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003836 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003839 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003840 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841}
3842
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003843/*
3844 * Display the information about all the quickfix/location lists in the stack
3845 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003846 void
3847qf_history(exarg_T *eap)
3848{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003849 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003850 int i;
3851
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003852 if (eap->addr_count > 0)
3853 {
3854 if (qi == NULL)
3855 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003856 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003857 return;
3858 }
3859
3860 // Jump to the specified quickfix list
3861 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3862 {
3863 qi->qf_curlist = eap->line2 - 1;
3864 qf_msg(qi, qi->qf_curlist, "");
3865 qf_update_buffer(qi, NULL);
3866 }
3867 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003868 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003869
3870 return;
3871 }
3872
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003873 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003874 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003875 else
3876 for (i = 0; i < qi->qf_listcount; ++i)
3877 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3878}
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003881 * Free all the entries in the error list "idx". Note that other information
3882 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 */
3884 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003885qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003887 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003888 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003889 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003891 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003893 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003894 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003895 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003896 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003897 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003898 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003899 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003900 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003901 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003902 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003903 // Somehow qf_count may have an incorrect value, set it to 1
3904 // to avoid crashing when it's wrong.
3905 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003906 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003907 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003908 qfl->qf_start = qfpnext;
3909 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003911
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003912 qfl->qf_index = 0;
3913 qfl->qf_start = NULL;
3914 qfl->qf_last = NULL;
3915 qfl->qf_ptr = NULL;
3916 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003917
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003918 qf_clean_dir_stack(&qfl->qf_dir_stack);
3919 qfl->qf_directory = NULL;
3920 qf_clean_dir_stack(&qfl->qf_file_stack);
3921 qfl->qf_currfile = NULL;
3922 qfl->qf_multiline = FALSE;
3923 qfl->qf_multiignore = FALSE;
3924 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925}
3926
3927/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003928 * Free error list "idx". Frees all the entries in the quickfix list,
3929 * associated context information and the title.
3930 */
3931 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003932qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003933{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003934 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003935
Bram Moolenaard23a8232018-02-10 18:45:26 +01003936 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003937 free_tv(qfl->qf_ctx);
3938 qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02003939 free_callback(&qfl->qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003940 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003941 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003942}
3943
3944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 * qf_mark_adjust: adjust marks
3946 */
3947 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003948qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003949 win_T *wp,
3950 linenr_T line1,
3951 linenr_T line2,
3952 long amount,
3953 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003955 int i;
3956 qfline_T *qfp;
3957 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003958 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003959 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003960 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
Bram Moolenaarc1542742016-07-20 21:44:37 +02003962 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003963 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003964 if (wp != NULL)
3965 {
3966 if (wp->w_llist == NULL)
3967 return;
3968 qi = wp->w_llist;
3969 }
3970
3971 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003972 {
3973 qf_list_T *qfl = qf_get_list(qi, idx);
3974
3975 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003976 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (qfp->qf_fnum == curbuf->b_fnum)
3978 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003979 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3981 {
3982 if (amount == MAXLNUM)
3983 qfp->qf_cleared = TRUE;
3984 else
3985 qfp->qf_lnum += amount;
3986 }
3987 else if (amount_after && qfp->qf_lnum > line2)
3988 qfp->qf_lnum += amount_after;
3989 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003990 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003991
3992 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003993 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994}
3995
3996/*
3997 * Make a nice message out of the error character and the error number:
3998 * char number message
3999 * e or E 0 " error"
4000 * w or W 0 " warning"
4001 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004002 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 * 0 0 ""
4004 * other 0 " c"
4005 * e or E n " error n"
4006 * w or W n " warning n"
4007 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004008 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 * 0 n " error n"
4010 * other n " c n"
4011 * 1 x "" :helpgrep
4012 */
4013 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004014qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
4016 static char_u buf[20];
4017 static char_u cc[3];
4018 char_u *p;
4019
4020 if (c == 'W' || c == 'w')
4021 p = (char_u *)" warning";
4022 else if (c == 'I' || c == 'i')
4023 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004024 else if (c == 'N' || c == 'n')
4025 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4027 p = (char_u *)" error";
4028 else if (c == 0 || c == 1)
4029 p = (char_u *)"";
4030 else
4031 {
4032 cc[0] = ' ';
4033 cc[1] = c;
4034 cc[2] = NUL;
4035 p = cc;
4036 }
4037
4038 if (nr <= 0)
4039 return p;
4040
4041 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4042 return buf;
4043}
4044
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004046 * When "split" is FALSE: Open the entry/result under the cursor.
4047 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4048 */
4049 void
4050qf_view_result(int split)
4051{
4052 qf_info_T *qi = &ql_info;
4053
4054 if (!bt_quickfix(curbuf))
4055 return;
4056
4057 if (IS_LL_WINDOW(curwin))
4058 qi = GET_LOC_LIST(curwin);
4059
Bram Moolenaar0398e002019-03-21 21:12:49 +01004060 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004061 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004062 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004063 return;
4064 }
4065
4066 if (split)
4067 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004068 // Open the selected entry in a new window
4069 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4070 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004071 return;
4072 }
4073
4074 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4075}
4076
4077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * ":cwindow": open the quickfix window if we have errors to display,
4079 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004080 * ":lwindow": open the location list window if we have locations to display,
4081 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 */
4083 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004084ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004086 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004087 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 win_T *win;
4089
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004090 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4091 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004092
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004093 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004094
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004095 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004096 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004098 // If a quickfix window is open but we have no errors to display,
4099 // close the window. If a quickfix window is not open, then open
4100 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004101 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004102 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004103 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
4105 if (win != NULL)
4106 ex_cclose(eap);
4107 }
4108 else if (win == NULL)
4109 ex_copen(eap);
4110}
4111
4112/*
4113 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004114 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004117ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004119 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004120 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004122 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4123 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004125 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004126 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (win != NULL)
4128 win_close(win, FALSE);
4129}
4130
4131/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004132 * Set "w:quickfix_title" if "qi" has a title.
4133 */
4134 static void
4135qf_set_title_var(qf_list_T *qfl)
4136{
4137 if (qfl->qf_title != NULL)
4138 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4139}
4140
4141/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004142 * Goto a quickfix or location list window (if present).
4143 * Returns OK if the window is found, FAIL otherwise.
4144 */
4145 static int
4146qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4147{
4148 win_T *win;
4149
4150 win = qf_find_win(qi);
4151 if (win == NULL)
4152 return FAIL;
4153
4154 win_goto(win);
4155 if (resize)
4156 {
4157 if (vertsplit)
4158 {
4159 if (sz != win->w_width)
4160 win_setwidth(sz);
4161 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004162 else if (sz != win->w_height && win->w_height
4163 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004164 win_setheight(sz);
4165 }
4166
4167 return OK;
4168}
4169
4170/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004171 * Set options for the buffer in the quickfix or location list window.
4172 */
4173 static void
4174qf_set_cwindow_options(void)
4175{
4176 // switch off 'swapfile'
4177 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4178 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4179 OPT_LOCAL);
4180 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4181 RESET_BINDING(curwin);
4182#ifdef FEAT_DIFF
4183 curwin->w_p_diff = FALSE;
4184#endif
4185#ifdef FEAT_FOLDING
4186 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4187 OPT_LOCAL);
4188#endif
4189}
4190
4191/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004192 * Open a new quickfix or location list window, load the quickfix buffer and
4193 * set the appropriate options for the window.
4194 * Returns FAIL if the window could not be opened.
4195 */
4196 static int
4197qf_open_new_cwindow(qf_info_T *qi, int height)
4198{
4199 buf_T *qf_buf;
4200 win_T *oldwin = curwin;
4201 tabpage_T *prevtab = curtab;
4202 int flags = 0;
4203 win_T *win;
4204
4205 qf_buf = qf_find_buf(qi);
4206
4207 // The current window becomes the previous window afterwards.
4208 win = curwin;
4209
Bram Moolenaare1004402020-10-24 20:49:43 +02004210 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004211 // Create the new quickfix window at the very bottom, except when
4212 // :belowright or :aboveleft is used.
4213 win_goto(lastwin);
4214 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004215 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004216 flags = WSP_BELOW;
4217 flags |= WSP_NEWLOC;
4218 if (win_split(height, flags) == FAIL)
4219 return FAIL; // not enough room for window
4220 RESET_BINDING(curwin);
4221
4222 if (IS_LL_STACK(qi))
4223 {
4224 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004225 // location list stack from the window 'win'.
4226 curwin->w_llist_ref = qi;
4227 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004228 }
4229
4230 if (oldwin != curwin)
4231 oldwin = NULL; // don't store info when in another window
4232 if (qf_buf != NULL)
4233 {
4234 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004235 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004236 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004237 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004238 }
4239 else
4240 {
4241 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004242 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4243 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004244 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004245
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004246 // save the number of the new buffer
4247 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004248 }
4249
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004250 // Set the options for the quickfix buffer/window (if not already done)
4251 // Do this even if the quickfix buffer was already present, as an autocmd
4252 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004253 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004254 qf_set_cwindow_options();
4255
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004256 // Only set the height when still in the same tab page and there is no
4257 // window to the side.
4258 if (curtab == prevtab && curwin->w_width == Columns)
4259 win_setheight(height);
4260 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4261 if (win_valid(win))
4262 prevwin = win;
4263
4264 return OK;
4265}
4266
4267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004269 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 */
4271 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004272ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004274 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004275 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004277 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004278 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004280 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4281 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004282
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004283 incr_quickfix_busy();
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 if (eap->addr_count != 0)
4286 height = eap->line2;
4287 else
4288 height = QF_WINHEIGHT;
4289
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004290 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291#ifdef FEAT_GUI
4292 need_mouse_correct = TRUE;
4293#endif
4294
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004295 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004296 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004297 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004298 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004299 if (status == FAIL)
4300 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004301 {
4302 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004303 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004306 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004307 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004308 // Save the current index here, as updating the quickfix buffer may free
4309 // the quickfix list
4310 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004311
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004312 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004313 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004315 decr_quickfix_busy();
4316
4317 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 curwin->w_cursor.col = 0;
4319 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004320 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321}
4322
4323/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004324 * Move the cursor in the quickfix window to "lnum".
4325 */
4326 static void
4327qf_win_goto(win_T *win, linenr_T lnum)
4328{
4329 win_T *old_curwin = curwin;
4330
4331 curwin = win;
4332 curbuf = win->w_buffer;
4333 curwin->w_cursor.lnum = lnum;
4334 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004335 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004336 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004337 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004338 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004339 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004340 curwin = old_curwin;
4341 curbuf = curwin->w_buffer;
4342}
4343
4344/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004345 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004346 */
4347 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004348ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004349{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004350 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004351 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004352
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004353 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4354 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004355
4356 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004357 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4358 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4359}
4360
4361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 * Return the number of the current entry (line number in the quickfix
4363 * window).
4364 */
4365 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004366qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004368 qf_info_T *qi = &ql_info;
4369
4370 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004371 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004372 qi = wp->w_llist_ref;
4373
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004374 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375}
4376
4377/*
4378 * Update the cursor position in the quickfix window to the current error.
4379 * Return TRUE if there is a quickfix window.
4380 */
4381 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004382qf_win_pos_update(
4383 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004384 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
4386 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004387 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004389 // Put the cursor on the current error in the quickfix window, so that
4390 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004391 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 if (win != NULL
4393 && qf_index <= win->w_buffer->b_ml.ml_line_count
4394 && old_qf_index != qf_index)
4395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 if (qf_index > old_qf_index)
4397 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004398 win->w_redraw_top = old_qf_index;
4399 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
4401 else
4402 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004403 win->w_redraw_top = qf_index;
4404 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004406 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
4408 return win != NULL;
4409}
4410
4411/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004412 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004413 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004414 */
4415 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004416is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004417{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004418 // A window displaying the quickfix buffer will have the w_llist_ref field
4419 // set to NULL.
4420 // A window displaying a location list buffer will have the w_llist_ref
4421 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004422 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004423 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4424 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004425 return TRUE;
4426
4427 return FALSE;
4428}
4429
4430/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004431 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4432 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004433 */
4434 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004435qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004436{
4437 win_T *win;
4438
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004439 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004440 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004441 return win;
4442 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004443}
4444
4445/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004446 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004447 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 */
4449 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004450qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004452 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004453 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004455 if (qi->qf_bufnr != INVALID_QFBUFNR)
4456 {
4457 buf_T *qfbuf;
4458 qfbuf = buflist_findnr(qi->qf_bufnr);
4459 if (qfbuf != NULL)
4460 return qfbuf;
4461 // buffer is no longer present
4462 qi->qf_bufnr = INVALID_QFBUFNR;
4463 }
4464
Bram Moolenaar9c102382006-05-03 21:26:49 +00004465 FOR_ALL_TAB_WINDOWS(tp, win)
4466 if (is_qf_win(win, qi))
4467 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004468
Bram Moolenaar9c102382006-05-03 21:26:49 +00004469 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470}
4471
4472/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004473 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004474 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004475 */
4476 int
4477qf_process_qftf_option(void)
4478{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004479 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004480}
4481
4482/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004483 * Update the w:quickfix_title variable in the quickfix/location list window in
4484 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004485 */
4486 static void
4487qf_update_win_titlevar(qf_info_T *qi)
4488{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004489 qf_list_T *qfl = qf_get_curlist(qi);
4490 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004491 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004492 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004493
Bram Moolenaar530bed92020-12-16 21:02:56 +01004494 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004495 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004496 if (is_qf_win(win, qi))
4497 {
4498 curwin = win;
4499 qf_set_title_var(qfl);
4500 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004501 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004502 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004503}
4504
4505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 * Find the quickfix buffer. If it exists, update the contents.
4507 */
4508 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004509qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004512 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004515 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004516 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (buf != NULL)
4518 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004519 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004520 int qf_winid = 0;
4521
4522 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004523 {
4524 if (curwin->w_llist == qi)
4525 win = curwin;
4526 else
4527 {
4528 win = qf_find_win_with_loclist(qi);
4529 if (win == NULL)
4530 return;
4531 }
4532 qf_winid = win->w_id;
4533 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004534
4535 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004536 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004537 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
Bram Moolenaard823fa92016-08-12 16:29:27 +02004539 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004540
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004541 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004542 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004543
Bram Moolenaar864293a2016-06-02 13:40:04 +02004544 if (old_last == NULL)
4545 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004546 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004547
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004548 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004549 aucmd_restbuf(&aco);
4550 }
4551
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004552 // Only redraw when added lines are visible. This avoids flickering
4553 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004554 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4555 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557}
4558
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004559/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004560 * Add an error line to the quickfix buffer.
4561 */
4562 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004563qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004564 buf_T *buf, // quickfix window buffer
4565 linenr_T lnum,
4566 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004567 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004568 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004569 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004570{
4571 int len;
4572 buf_T *errbuf;
4573
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004574 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004575 // for this entry, then use it.
4576 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004577 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004578 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004579 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004580 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004581 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004582 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4583 len = (int)STRLEN(IObuff);
4584 }
4585 else if (qfp->qf_fnum != 0
4586 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4587 && errbuf->b_fname != NULL)
4588 {
4589 if (qfp->qf_type == 1) // :helpgrep
4590 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4591 else
4592 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004593 // Shorten the file name if not done already.
4594 // For optimization, do this only for the first entry in a
4595 // buffer.
4596 if (first_bufline && (errbuf->b_sfname == NULL
4597 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004598 {
4599 if (*dirname == NUL)
4600 mch_dirname(dirname, MAXPATHL);
4601 shorten_buf_fname(errbuf, dirname, FALSE);
4602 }
4603 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4604 }
4605 len = (int)STRLEN(IObuff);
4606 }
4607 else
4608 len = 0;
4609
4610 if (len < IOSIZE - 1)
4611 IObuff[len++] = '|';
4612
4613 if (qfp->qf_lnum > 0)
4614 {
thinca6864efa2021-06-19 20:45:20 +02004615 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004616 len += (int)STRLEN(IObuff + len);
4617
Bram Moolenaar858ba062020-05-31 23:11:59 +02004618 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4619 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004620 len += (int)STRLEN(IObuff + len);
4621 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004622 else if (qfp->qf_pattern != NULL)
4623 {
4624 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4625 len += (int)STRLEN(IObuff + len);
4626 }
4627 if (len < IOSIZE - 2)
4628 {
4629 IObuff[len++] = '|';
4630 IObuff[len++] = ' ';
4631 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004632
Bram Moolenaar858ba062020-05-31 23:11:59 +02004633 // Remove newlines and leading whitespace from the text.
4634 // For an unrecognized line keep the indent, the compiler may
4635 // mark a word with ^^^^.
4636 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4637 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004638 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004639
4640 if (ml_append_buf(buf, lnum, IObuff,
4641 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4642 return FAIL;
4643
4644 return OK;
4645}
4646
Bram Moolenaard43906d2020-07-20 21:31:32 +02004647/*
4648 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4649 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4650 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004651 static list_T *
4652call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4653{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004654 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004655 list_T *qftf_list = NULL;
4656
4657 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4658 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4659 // set.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004660 if (qfl->qftf_cb.cb_name != NULL)
4661 cb = &qfl->qftf_cb;
4662 if (cb != NULL && cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004663 {
4664 typval_T args[1];
4665 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004666 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004667
4668 // create the dict argument
4669 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4670 return NULL;
4671 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4672 dict_add_number(d, "winid", (long)qf_winid);
4673 dict_add_number(d, "id", (long)qfl->qf_id);
4674 dict_add_number(d, "start_idx", start_idx);
4675 dict_add_number(d, "end_idx", end_idx);
4676 ++d->dv_refcount;
4677 args[0].v_type = VAR_DICT;
4678 args[0].vval.v_dict = d;
4679
Bram Moolenaard43906d2020-07-20 21:31:32 +02004680 qftf_list = NULL;
4681 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4682 {
4683 if (rettv.v_type == VAR_LIST)
4684 {
4685 qftf_list = rettv.vval.v_list;
4686 qftf_list->lv_refcount++;
4687 }
4688 clear_tv(&rettv);
4689 }
4690 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004691 }
4692
4693 return qftf_list;
4694}
4695
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 * Fill current buffer with quickfix errors, replacing any previous contents.
4698 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004699 * If "old_last" is not NULL append the items after this one.
4700 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4701 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 */
4703 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004704qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004706 linenr_T lnum;
4707 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004708 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004709 list_T *qftf_list = NULL;
4710 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
Bram Moolenaar864293a2016-06-02 13:40:04 +02004712 if (old_last == NULL)
4713 {
4714 if (buf != curbuf)
4715 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004716 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004717 return;
4718 }
4719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004720 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004721 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004722 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004725 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004726 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004728 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004729 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004730 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004731
4732 *dirname = NUL;
4733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004734 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004735 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004736 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004737 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004738 lnum = 0;
4739 }
4740 else
4741 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004742 if (old_last->qf_next != NULL)
4743 qfp = old_last->qf_next;
4744 else
4745 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004746 lnum = buf->b_ml.ml_line_count;
4747 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004748
4749 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4750 (long)qfl->qf_count);
4751 if (qftf_list != NULL)
4752 qftf_li = qftf_list->lv_first;
4753
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004754 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004756 char_u *qftf_str = NULL;
4757
Bram Moolenaard43906d2020-07-20 21:31:32 +02004758 // Use the text supplied by the user defined function (if any).
4759 // If the returned value is not string, then ignore the rest
4760 // of the returned values and use the default.
4761 if (qftf_li != NULL && !invalid_val)
4762 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004763 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004764 if (qftf_str == NULL)
4765 invalid_val = TRUE;
4766 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004767
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004768 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4769 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004771
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004772 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004773 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004775 if (qfp == NULL)
4776 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004777
4778 if (qftf_li != NULL)
4779 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004781
4782 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004783 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004784 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004787 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 check_lnums(TRUE);
4789
Bram Moolenaar864293a2016-06-02 13:40:04 +02004790 if (old_last == NULL)
4791 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004792 // Set the 'filetype' to "qf" each time after filling the buffer.
4793 // This resembles reading a file into a buffer, it's more logical when
4794 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004795 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004796 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4797 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004799 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004800 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004802 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004804 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004805 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004806
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004807 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004808 redraw_curbuf_later(NOT_VALID);
4809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004811 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 KeyTyped = old_KeyTyped;
4813}
4814
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004815/*
4816 * For every change made to the quickfix list, update the changed tick.
4817 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004818 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004819qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004820{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004821 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004822}
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004825 * Return the quickfix/location list number with the given identifier.
4826 * Returns -1 if list is not found.
4827 */
4828 static int
4829qf_id2nr(qf_info_T *qi, int_u qfid)
4830{
4831 int qf_idx;
4832
4833 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4834 if (qi->qf_lists[qf_idx].qf_id == qfid)
4835 return qf_idx;
4836 return INVALID_QFIDX;
4837}
4838
4839/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004840 * If the current list is not "save_qfid" and we can find the list with that ID
4841 * then make it the current list.
4842 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004843 * Returns OK if successfully restored the list. Returns FAIL if the list with
4844 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004845 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004846 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004847qf_restore_list(qf_info_T *qi, int_u save_qfid)
4848{
4849 int curlist;
4850
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004851 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004852 {
4853 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004854 if (curlist < 0)
4855 // list is not present
4856 return FAIL;
4857 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004858 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004859 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004860}
4861
4862/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004863 * Jump to the first entry if there is one.
4864 */
4865 static void
4866qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4867{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004868 if (qf_restore_list(qi, save_qfid) == FAIL)
4869 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004870
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004871 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004872 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004873 qf_jump(qi, 0, 0, forceit);
4874}
4875
4876/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004877 * Return TRUE when using ":vimgrep" for ":grep".
4878 */
4879 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004880grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004881{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004882 return ((cmdidx == CMD_grep
4883 || cmdidx == CMD_lgrep
4884 || cmdidx == CMD_grepadd
4885 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004886 && STRCMP("internal",
4887 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4888}
4889
4890/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004891 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004893 static char_u *
4894make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004896 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004897 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004898 case CMD_make: return (char_u *)"make";
4899 case CMD_lmake: return (char_u *)"lmake";
4900 case CMD_grep: return (char_u *)"grep";
4901 case CMD_lgrep: return (char_u *)"lgrep";
4902 case CMD_grepadd: return (char_u *)"grepadd";
4903 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4904 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906}
4907
4908/*
4909 * Return the name for the errorfile, in allocated memory.
4910 * Find a new unique name when 'makeef' contains "##".
4911 * Returns NULL for error.
4912 */
4913 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004914get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915{
4916 char_u *p;
4917 char_u *name;
4918 static int start = -1;
4919 static int off = 0;
4920#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004921 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922#endif
4923
4924 if (*p_mef == NUL)
4925 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004926 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004928 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 return name;
4930 }
4931
4932 for (p = p_mef; *p; ++p)
4933 if (p[0] == '#' && p[1] == '#')
4934 break;
4935
4936 if (*p == NUL)
4937 return vim_strsave(p_mef);
4938
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004939 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 for (;;)
4941 {
4942 if (start == -1)
4943 start = mch_get_pid();
4944 else
4945 off += 19;
4946
Bram Moolenaar964b3742019-05-24 18:54:09 +02004947 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (name == NULL)
4949 break;
4950 STRCPY(name, p_mef);
4951 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4952 STRCAT(name, p + 2);
4953 if (mch_getperm(name) < 0
4954#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004955 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 && mch_lstat((char *)name, &sb) < 0
4957#endif
4958 )
4959 break;
4960 vim_free(name);
4961 }
4962 return name;
4963}
4964
4965/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004966 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4967 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4968 */
4969 static char_u *
4970make_get_fullcmd(char_u *makecmd, char_u *fname)
4971{
4972 char_u *cmd;
4973 unsigned len;
4974
4975 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4976 if (*p_sp != NUL)
4977 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4978 cmd = alloc(len);
4979 if (cmd == NULL)
4980 return NULL;
4981 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4982 (char *)p_shq);
4983
4984 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4985 if (*p_sp != NUL)
4986 append_redir(cmd, len, p_sp, fname);
4987
4988 // Display the fully formed command. Output a newline if there's something
4989 // else than the :make command that was typed (in which case the cursor is
4990 // in column 0).
4991 if (msg_col == 0)
4992 msg_didout = FALSE;
4993 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004994 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004995 msg_outtrans(cmd); // show what we are doing
4996
4997 return cmd;
4998}
4999
5000/*
5001 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5002 */
5003 void
5004ex_make(exarg_T *eap)
5005{
5006 char_u *fname;
5007 char_u *cmd;
5008 char_u *enc = NULL;
5009 win_T *wp = NULL;
5010 qf_info_T *qi = &ql_info;
5011 int res;
5012 char_u *au_name = NULL;
5013 int_u save_qfid;
5014
5015 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5016 if (grep_internal(eap->cmdidx))
5017 {
5018 ex_vimgrep(eap);
5019 return;
5020 }
5021
5022 au_name = make_get_auname(eap->cmdidx);
5023 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5024 curbuf->b_fname, TRUE, curbuf))
5025 {
5026#ifdef FEAT_EVAL
5027 if (aborting())
5028 return;
5029#endif
5030 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005031 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005032
5033 if (is_loclist_cmd(eap->cmdidx))
5034 wp = curwin;
5035
5036 autowrite_all();
5037 fname = get_mef_name();
5038 if (fname == NULL)
5039 return;
5040 mch_remove(fname); // in case it's not unique
5041
5042 cmd = make_get_fullcmd(eap->arg, fname);
5043 if (cmd == NULL)
5044 return;
5045
5046 // let the shell know if we are redirecting output or not
5047 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5048
5049#ifdef AMIGA
5050 out_flush();
5051 // read window status report and redraw before message
5052 (void)char_avail();
5053#endif
5054
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005055 incr_quickfix_busy();
5056
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005057 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
5058 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
5059 (eap->cmdidx != CMD_grepadd
5060 && eap->cmdidx != CMD_lgrepadd),
5061 qf_cmdtitle(*eap->cmdlinep), enc);
5062 if (wp != NULL)
5063 {
5064 qi = GET_LOC_LIST(wp);
5065 if (qi == NULL)
5066 goto cleanup;
5067 }
5068 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005069 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005070
5071 // Remember the current quickfix list identifier, so that we can
5072 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005073 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005074 if (au_name != NULL)
5075 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5076 curbuf->b_fname, TRUE, curbuf);
5077 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5078 // display the first error
5079 qf_jump_first(qi, save_qfid, FALSE);
5080
5081cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005082 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005083 mch_remove(fname);
5084 vim_free(fname);
5085 vim_free(cmd);
5086}
5087
5088/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005089 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005090 */
5091 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005092qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005093{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005094 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005095
5096 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5097 return 0;
5098 return qf_get_curlist(qi)->qf_count;
5099}
5100
5101/*
5102 * Returns the number of valid entries in the current quickfix/location list.
5103 */
5104 int
5105qf_get_valid_size(exarg_T *eap)
5106{
5107 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005108 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005109 qfline_T *qfp;
5110 int i, sz = 0;
5111 int prev_fnum = 0;
5112
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005113 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5114 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005115
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005116 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005117 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005118 {
5119 if (qfp->qf_valid)
5120 {
5121 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005122 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005123 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5124 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005125 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005126 sz++;
5127 prev_fnum = qfp->qf_fnum;
5128 }
5129 }
5130 }
5131
5132 return sz;
5133}
5134
5135/*
5136 * Returns the current index of the quickfix/location list.
5137 * Returns 0 if there is an error.
5138 */
5139 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005140qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005141{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005142 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005143
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005144 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5145 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005146
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005147 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005148}
5149
5150/*
5151 * Returns the current index in the quickfix/location list (counting only valid
5152 * entries). If no valid entries are in the list, then returns 1.
5153 */
5154 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005155qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005156{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005157 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005158 qf_list_T *qfl;
5159 qfline_T *qfp;
5160 int i, eidx = 0;
5161 int prev_fnum = 0;
5162
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005163 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5164 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005165
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005166 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005167 qfp = qfl->qf_start;
5168
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005169 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005170 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005171 return 1;
5172
5173 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5174 {
5175 if (qfp->qf_valid)
5176 {
5177 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5178 {
5179 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5180 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005181 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005182 eidx++;
5183 prev_fnum = qfp->qf_fnum;
5184 }
5185 }
5186 else
5187 eidx++;
5188 }
5189 }
5190
5191 return eidx ? eidx : 1;
5192}
5193
5194/*
5195 * Get the 'n'th valid error entry in the quickfix or location list.
5196 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5197 * For :cdo and :ldo returns the 'n'th valid error entry.
5198 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5199 */
5200 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005201qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005202{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005203 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005204 int i, eidx;
5205 int prev_fnum = 0;
5206
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005207 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005208 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005209 return 1;
5210
Bram Moolenaar95946f12019-03-31 15:31:59 +02005211 eidx = 0;
5212 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005213 {
5214 if (qfp->qf_valid)
5215 {
5216 if (fdo)
5217 {
5218 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5219 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005220 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005221 eidx++;
5222 prev_fnum = qfp->qf_fnum;
5223 }
5224 }
5225 else
5226 eidx++;
5227 }
5228
5229 if (eidx == n)
5230 break;
5231 }
5232
5233 if (i <= qfl->qf_count)
5234 return i;
5235 else
5236 return 1;
5237}
5238
5239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005241 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005242 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 */
5244 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005245ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005247 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005248 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005249
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005250 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5251 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005252
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005253 if (eap->addr_count > 0)
5254 errornr = (int)eap->line2;
5255 else
5256 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005257 switch (eap->cmdidx)
5258 {
5259 case CMD_cc: case CMD_ll:
5260 errornr = 0;
5261 break;
5262 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5263 case CMD_lfirst:
5264 errornr = 1;
5265 break;
5266 default:
5267 errornr = 32767;
5268 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005269 }
5270
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005271 // For cdo and ldo commands, jump to the nth valid error.
5272 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005273 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5274 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005275 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005276 eap->addr_count > 0 ? (int)eap->line1 : 1,
5277 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5278
5279 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280}
5281
5282/*
5283 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005284 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005285 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 */
5287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005288ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005290 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005291 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005292 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005293
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005294 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5295 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005296
Bram Moolenaar55b69262017-08-13 13:42:01 +02005297 if (eap->addr_count > 0
5298 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5299 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005300 errornr = (int)eap->line2;
5301 else
5302 errornr = 1;
5303
Bram Moolenaar39665952018-08-15 20:59:48 +02005304 // Depending on the command jump to either next or previous entry/file.
5305 switch (eap->cmdidx)
5306 {
5307 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5308 dir = FORWARD;
5309 break;
5310 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5311 case CMD_lNext:
5312 dir = BACKWARD;
5313 break;
5314 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5315 dir = FORWARD_FILE;
5316 break;
5317 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5318 dir = BACKWARD_FILE;
5319 break;
5320 default:
5321 dir = FORWARD;
5322 break;
5323 }
5324
5325 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326}
5327
5328/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005329 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5330 * The index of the entry is stored in 'errornr'.
5331 * Returns NULL if an entry is not found.
5332 */
5333 static qfline_T *
5334qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5335{
5336 qfline_T *qfp = NULL;
5337 int idx = 0;
5338
5339 // Find the first entry in this file
5340 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5341 if (qfp->qf_fnum == bnr)
5342 break;
5343
5344 *errornr = idx;
5345 return qfp;
5346}
5347
5348/*
5349 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5350 * with the error number for the first entry. Assumes the entries are sorted in
5351 * the quickfix list by line number.
5352 */
5353 static qfline_T *
5354qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5355{
5356 while (!got_int
5357 && entry->qf_prev != NULL
5358 && entry->qf_fnum == entry->qf_prev->qf_fnum
5359 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5360 {
5361 entry = entry->qf_prev;
5362 --*errornr;
5363 }
5364
5365 return entry;
5366}
5367
5368/*
5369 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5370 * with the error number for the last entry. Assumes the entries are sorted in
5371 * the quickfix list by line number.
5372 */
5373 static qfline_T *
5374qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5375{
5376 while (!got_int &&
5377 entry->qf_next != NULL
5378 && entry->qf_fnum == entry->qf_next->qf_fnum
5379 && entry->qf_lnum == entry->qf_next->qf_lnum)
5380 {
5381 entry = entry->qf_next;
5382 ++*errornr;
5383 }
5384
5385 return entry;
5386}
5387
5388/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005389 * Returns TRUE if the specified quickfix entry is
5390 * after the given line (linewise is TRUE)
5391 * or after the line and column.
5392 */
5393 static int
5394qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5395{
5396 if (linewise)
5397 return qfp->qf_lnum > pos->lnum;
5398 else
5399 return (qfp->qf_lnum > pos->lnum ||
5400 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5401}
5402
5403/*
5404 * Returns TRUE if the specified quickfix entry is
5405 * before the given line (linewise is TRUE)
5406 * or before the line and column.
5407 */
5408 static int
5409qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5410{
5411 if (linewise)
5412 return qfp->qf_lnum < pos->lnum;
5413 else
5414 return (qfp->qf_lnum < pos->lnum ||
5415 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5416}
5417
5418/*
5419 * Returns TRUE if the specified quickfix entry is
5420 * on or after the given line (linewise is TRUE)
5421 * or on or after the line and column.
5422 */
5423 static int
5424qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5425{
5426 if (linewise)
5427 return qfp->qf_lnum >= pos->lnum;
5428 else
5429 return (qfp->qf_lnum > pos->lnum ||
5430 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5431}
5432
5433/*
5434 * Returns TRUE if the specified quickfix entry is
5435 * on or before the given line (linewise is TRUE)
5436 * or on or before the line and column.
5437 */
5438 static int
5439qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5440{
5441 if (linewise)
5442 return qfp->qf_lnum <= pos->lnum;
5443 else
5444 return (qfp->qf_lnum < pos->lnum ||
5445 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5446}
5447
5448/*
5449 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5450 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5451 * multiple entries on a single line as one. Otherwise returns the entry after
5452 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005453 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5454 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005455 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005456 */
5457 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005458qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005459 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005460 pos_T *pos,
5461 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005462 qfline_T *qfp,
5463 int *errornr)
5464{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005465 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005466 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005467 return qfp;
5468
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005469 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005470 while (qfp->qf_next != NULL
5471 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005472 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005473 {
5474 qfp = qfp->qf_next;
5475 ++*errornr;
5476 }
5477
5478 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005479 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005480 return NULL;
5481
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005482 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005483 qfp = qfp->qf_next;
5484 ++*errornr;
5485
5486 return qfp;
5487}
5488
5489/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005490 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5491 * If 'linewise' is TRUE, returns the entry before the specified line and
5492 * treats multiple entries on a single line as one. Otherwise returns the entry
5493 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005494 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5495 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005496 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005497 */
5498 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005499qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005500 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005501 pos_T *pos,
5502 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005503 qfline_T *qfp,
5504 int *errornr)
5505{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005506 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005507 while (qfp->qf_next != NULL
5508 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005509 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005510 {
5511 qfp = qfp->qf_next;
5512 ++*errornr;
5513 }
5514
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005515 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005516 return NULL;
5517
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005518 if (linewise)
5519 // If multiple entries are on the same line, then use the first entry
5520 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005521
5522 return qfp;
5523}
5524
5525/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005526 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005527 * the direction 'dir'.
5528 */
5529 static qfline_T *
5530qf_find_closest_entry(
5531 qf_list_T *qfl,
5532 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005533 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005534 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005535 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005536 int *errornr)
5537{
5538 qfline_T *qfp;
5539
5540 *errornr = 0;
5541
5542 // Find the first entry in this file
5543 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5544 if (qfp == NULL)
5545 return NULL; // no entry in this file
5546
5547 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005548 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005549 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005550 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005551
5552 return qfp;
5553}
5554
5555/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005556 * Get the nth quickfix entry below the specified entry. Searches forward in
5557 * the list. If linewise is TRUE, then treat multiple entries on a single line
5558 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005559 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005560 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005561qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005562{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005563 qfline_T *entry = entry_arg;
5564
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005565 while (n-- > 0 && !got_int)
5566 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005567 int first_errornr = *errornr;
5568
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005569 if (linewise)
5570 // Treat all the entries on the same line in this file as one
5571 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005572
5573 if (entry->qf_next == NULL
5574 || entry->qf_next->qf_fnum != entry->qf_fnum)
5575 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005576 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005577 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005578 break;
5579 }
5580
5581 entry = entry->qf_next;
5582 ++*errornr;
5583 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005584}
5585
5586/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005587 * Get the nth quickfix entry above the specified entry. Searches backwards in
5588 * the list. If linewise is TRUE, then treat multiple entries on a single line
5589 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005590 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005591 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005592qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005593{
5594 while (n-- > 0 && !got_int)
5595 {
5596 if (entry->qf_prev == NULL
5597 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5598 break;
5599
5600 entry = entry->qf_prev;
5601 --*errornr;
5602
5603 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005604 if (linewise)
5605 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005606 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005607}
5608
5609/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005610 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5611 * the specified direction. Returns the error number in the quickfix list or 0
5612 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005613 */
5614 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005615qf_find_nth_adj_entry(
5616 qf_list_T *qfl,
5617 int bnr,
5618 pos_T *pos,
5619 int n,
5620 int dir,
5621 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005622{
5623 qfline_T *adj_entry;
5624 int errornr;
5625
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005626 // Find an entry closest to the specified position
5627 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005628 if (adj_entry == NULL)
5629 return 0;
5630
5631 if (--n > 0)
5632 {
5633 // Go to the n'th entry in the current buffer
5634 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005635 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005636 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005637 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005638 }
5639
5640 return errornr;
5641}
5642
5643/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005644 * Jump to a quickfix entry in the current file nearest to the current line or
5645 * current line/col.
5646 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5647 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005648 */
5649 void
5650ex_cbelow(exarg_T *eap)
5651{
5652 qf_info_T *qi;
5653 qf_list_T *qfl;
5654 int dir;
5655 int buf_has_flag;
5656 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005657 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005658
5659 if (eap->addr_count > 0 && eap->line2 <= 0)
5660 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005661 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005662 return;
5663 }
5664
5665 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005666 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5667 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005668 buf_has_flag = BUF_HAS_QF_ENTRY;
5669 else
5670 buf_has_flag = BUF_HAS_LL_ENTRY;
5671 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5672 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005673 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005674 return;
5675 }
5676
5677 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5678 return;
5679
5680 qfl = qf_get_curlist(qi);
5681 // check if the list has valid errors
5682 if (!qf_list_has_valid_entries(qfl))
5683 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005684 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005685 return;
5686 }
5687
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005688 if (eap->cmdidx == CMD_cbelow
5689 || eap->cmdidx == CMD_lbelow
5690 || eap->cmdidx == CMD_cafter
5691 || eap->cmdidx == CMD_lafter)
5692 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005693 dir = FORWARD;
5694 else
5695 dir = BACKWARD;
5696
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005697 pos = curwin->w_cursor;
5698 // A quickfix entry column number is 1 based whereas cursor column
5699 // number is 0 based. Adjust the column number.
5700 pos.col++;
5701 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5702 eap->addr_count > 0 ? eap->line2 : 0, dir,
5703 eap->cmdidx == CMD_cbelow
5704 || eap->cmdidx == CMD_lbelow
5705 || eap->cmdidx == CMD_cabove
5706 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005707
5708 if (errornr > 0)
5709 qf_jump(qi, 0, errornr, FALSE);
5710 else
5711 emsg(_(e_no_more_items));
5712}
5713
5714/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005715 * Return the autocmd name for the :cfile Ex commands
5716 */
5717 static char_u *
5718cfile_get_auname(cmdidx_T cmdidx)
5719{
5720 switch (cmdidx)
5721 {
5722 case CMD_cfile: return (char_u *)"cfile";
5723 case CMD_cgetfile: return (char_u *)"cgetfile";
5724 case CMD_caddfile: return (char_u *)"caddfile";
5725 case CMD_lfile: return (char_u *)"lfile";
5726 case CMD_lgetfile: return (char_u *)"lgetfile";
5727 case CMD_laddfile: return (char_u *)"laddfile";
5728 default: return NULL;
5729 }
5730}
5731
5732/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005733 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005734 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 */
5736 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005737ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005739 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005740 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005741 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005742 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005743 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005744 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005745
Bram Moolenaara16123a2019-03-28 20:31:07 +01005746 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005747 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5748 NULL, FALSE, curbuf))
5749 {
5750#ifdef FEAT_EVAL
5751 if (aborting())
5752 return;
5753#endif
5754 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005755
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005756 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005757#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005758 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005759 {
5760 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005761 NULL, NULL,
5762 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005763 if (browse_file == NULL)
5764 return;
5765 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5766 vim_free(browse_file);
5767 }
5768 else
5769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005771 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005772
Bram Moolenaar39665952018-08-15 20:59:48 +02005773 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005774 wp = curwin;
5775
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005776 incr_quickfix_busy();
5777
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005778 // This function is used by the :cfile, :cgetfile and :caddfile
5779 // commands.
5780 // :cfile always creates a new quickfix list and jumps to the
5781 // first error.
5782 // :cgetfile creates a new quickfix list but doesn't jump to the
5783 // first error.
5784 // :caddfile adds to an existing quickfix list. If there is no
5785 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005786 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005787 && eap->cmdidx != CMD_laddfile),
5788 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005789 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005790 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005791 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005792 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005793 {
5794 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005795 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005796 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005797 }
5798 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005799 qf_list_changed(qf_get_curlist(qi));
5800 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005801 if (au_name != NULL)
5802 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005803
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005804 // Jump to the first error for a new list and if autocmds didn't
5805 // free the list.
5806 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5807 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005808 // display the first error
5809 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005810
5811 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005812}
5813
5814/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005815 * Return the vimgrep autocmd name.
5816 */
5817 static char_u *
5818vgr_get_auname(cmdidx_T cmdidx)
5819{
5820 switch (cmdidx)
5821 {
5822 case CMD_vimgrep: return (char_u *)"vimgrep";
5823 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5824 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5825 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5826 case CMD_grep: return (char_u *)"grep";
5827 case CMD_lgrep: return (char_u *)"lgrep";
5828 case CMD_grepadd: return (char_u *)"grepadd";
5829 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5830 default: return NULL;
5831 }
5832}
5833
5834/*
5835 * Initialize the regmatch used by vimgrep for pattern "s".
5836 */
5837 static void
5838vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5839{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005840 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005841 regmatch->regprog = NULL;
5842
5843 if (s == NULL || *s == NUL)
5844 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005845 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005846 if (last_search_pat() == NULL)
5847 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005848 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005849 return;
5850 }
5851 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5852 }
5853 else
5854 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5855
5856 regmatch->rmm_ic = p_ic;
5857 regmatch->rmm_maxcol = 0;
5858}
5859
5860/*
5861 * Display a file name when vimgrep is running.
5862 */
5863 static void
5864vgr_display_fname(char_u *fname)
5865{
5866 char_u *p;
5867
5868 msg_start();
5869 p = msg_strtrunc(fname, TRUE);
5870 if (p == NULL)
5871 msg_outtrans(fname);
5872 else
5873 {
5874 msg_outtrans(p);
5875 vim_free(p);
5876 }
5877 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005878 msg_didout = FALSE; // overwrite this message
5879 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005880 msg_col = 0;
5881 out_flush();
5882}
5883
5884/*
5885 * Load a dummy buffer to search for a pattern using vimgrep.
5886 */
5887 static buf_T *
5888vgr_load_dummy_buf(
5889 char_u *fname,
5890 char_u *dirname_start,
5891 char_u *dirname_now)
5892{
5893 int save_mls;
5894#if defined(FEAT_SYN_HL)
5895 char_u *save_ei = NULL;
5896#endif
5897 buf_T *buf;
5898
5899#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005900 // Don't do Filetype autocommands to avoid loading syntax and
5901 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005902 save_ei = au_event_disable(",Filetype");
5903#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005904 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005905 save_mls = p_mls;
5906 p_mls = 0;
5907
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005908 // Load file into a buffer, so that 'fileencoding' is detected,
5909 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005910 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5911
5912 p_mls = save_mls;
5913#if defined(FEAT_SYN_HL)
5914 au_event_restore(save_ei);
5915#endif
5916
5917 return buf;
5918}
5919
5920/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005921 * Check whether a quickfix/location list is valid. Autocmds may remove or
5922 * change a quickfix list when vimgrep is running. If the list is not found,
5923 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005924 */
5925 static int
5926vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005927 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005928 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005929 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005930 char_u *title)
5931{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005932 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005933 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005934 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005935 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005936 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005937 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005938 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005939 return FALSE;
5940 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005941 else
5942 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005943 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005944 qf_new_list(qi, title);
5945 return TRUE;
5946 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005947 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005948
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005949 if (qf_restore_list(qi, qfid) == FAIL)
5950 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005951
5952 return TRUE;
5953}
5954
5955/*
5956 * Search for a pattern in all the lines in a buffer and add the matching lines
5957 * to a quickfix list.
5958 */
5959 static int
5960vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005961 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005962 char_u *fname,
5963 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005964 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005965 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005966 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005967 int duplicate_name,
5968 int flags)
5969{
5970 int found_match = FALSE;
5971 long lnum;
5972 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02005973 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005974
Bram Moolenaar1c299432018-10-28 14:36:09 +01005975 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005976 {
5977 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005978 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005979 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005980 // Regular expression match
5981 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5982 col, NULL, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005983 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005984 // Pass the buffer number so that it gets used even for a
5985 // dummy buffer, unless duplicate_name is set, then the
5986 // buffer will be wiped out below.
5987 if (qf_add_entry(qfl,
5988 NULL, // dir
5989 fname,
5990 NULL,
5991 duplicate_name ? 0 : buf->b_fnum,
5992 ml_get_buf(buf,
5993 regmatch->startpos[0].lnum + lnum, FALSE),
5994 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02005995 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005996 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02005997 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005998 FALSE, // vis_col
5999 NULL, // search pattern
6000 0, // nr
6001 0, // type
6002 TRUE // valid
6003 ) == QF_FAIL)
6004 {
6005 got_int = TRUE;
6006 break;
6007 }
6008 found_match = TRUE;
6009 if (--*tomatch == 0)
6010 break;
6011 if ((flags & VGR_GLOBAL) == 0
6012 || regmatch->endpos[0].lnum > 0)
6013 break;
6014 col = regmatch->endpos[0].col
6015 + (col == regmatch->endpos[0].col);
6016 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6017 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006018 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006019 }
6020 else
6021 {
6022 char_u *str = ml_get_buf(buf, lnum, FALSE);
6023 int score;
6024 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006025 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006026
6027 // Fuzzy string match
6028 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6029 {
6030 // Pass the buffer number so that it gets used even for a
6031 // dummy buffer, unless duplicate_name is set, then the
6032 // buffer will be wiped out below.
6033 if (qf_add_entry(qfl,
6034 NULL, // dir
6035 fname,
6036 NULL,
6037 duplicate_name ? 0 : buf->b_fnum,
6038 str,
6039 lnum,
thinca6864efa2021-06-19 20:45:20 +02006040 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006041 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006042 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006043 FALSE, // vis_col
6044 NULL, // search pattern
6045 0, // nr
6046 0, // type
6047 TRUE // valid
6048 ) == QF_FAIL)
6049 {
6050 got_int = TRUE;
6051 break;
6052 }
6053 found_match = TRUE;
6054 if (--*tomatch == 0)
6055 break;
6056 if ((flags & VGR_GLOBAL) == 0)
6057 break;
6058 col = matches[pat_len - 1] + col + 1;
6059 if (col > (colnr_T)STRLEN(str))
6060 break;
6061 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006062 }
6063 line_breakcheck();
6064 if (got_int)
6065 break;
6066 }
6067
6068 return found_match;
6069}
6070
6071/*
6072 * Jump to the first match and update the directory.
6073 */
6074 static void
6075vgr_jump_to_match(
6076 qf_info_T *qi,
6077 int forceit,
6078 int *redraw_for_dummy,
6079 buf_T *first_match_buf,
6080 char_u *target_dir)
6081{
6082 buf_T *buf;
6083
6084 buf = curbuf;
6085 qf_jump(qi, 0, 0, forceit);
6086 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006087 // If we jumped to another buffer redrawing will already be
6088 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006089 *redraw_for_dummy = FALSE;
6090
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006091 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006092 if (curbuf == first_match_buf && target_dir != NULL)
6093 {
6094 exarg_T ea;
6095
Bram Moolenaara80faa82020-04-12 19:37:17 +02006096 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006097 ea.arg = target_dir;
6098 ea.cmdidx = CMD_lcd;
6099 ex_cd(&ea);
6100 }
6101}
6102
6103/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006104 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006105 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006106typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006107{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006108 long tomatch; // maximum number of matches to find
6109 char_u *spat; // search pattern
6110 int flags; // search modifier
6111 char_u **fnames; // list of files to search
6112 int fcount; // number of files
6113 regmmatch_T regmatch; // compiled search pattern
6114 char_u *qf_title; // quickfix list title
6115} vgr_args_T;
6116
6117/*
6118 * Process :vimgrep command arguments. The command syntax is:
6119 *
6120 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6121 */
6122 static int
6123vgr_process_args(
6124 exarg_T *eap,
6125 vgr_args_T *args)
6126{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006127 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006128
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006129 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006130
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006131 args->regmatch.regprog = NULL;
6132 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006133
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006134 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006135 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006136 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006137 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006138
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006139 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006140 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006141 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006142 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006143 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006144 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006145 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006146
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006147 vgr_init_regmatch(&args->regmatch, args->spat);
6148 if (args->regmatch.regprog == NULL)
6149 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006150
6151 p = skipwhite(p);
6152 if (*p == NUL)
6153 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006154 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006155 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006156 }
6157
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006158 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006159 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6160 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006161 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006162 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006163 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006164 }
6165
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006166 return OK;
6167}
6168
6169/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006170 * Return TRUE if "buf" had an existing swap file, the current swap file does
6171 * not end in ".swp".
6172 */
6173 static int
6174existing_swapfile(buf_T *buf)
6175{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006176 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006177 {
6178 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6179 size_t len = STRLEN(fname);
6180
6181 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6182 }
6183 return FALSE;
6184}
6185
6186/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006187 * Search for a pattern in a list of files and populate the quickfix list with
6188 * the matches.
6189 */
6190 static int
6191vgr_process_files(
6192 win_T *wp,
6193 qf_info_T *qi,
6194 vgr_args_T *cmd_args,
6195 int *redraw_for_dummy,
6196 buf_T **first_match_buf,
6197 char_u **target_dir)
6198{
6199 int status = FAIL;
6200 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6201 time_t seconds = 0;
6202 char_u *fname;
6203 int fi;
6204 buf_T *buf;
6205 int duplicate_name = FALSE;
6206 int using_dummy;
6207 char_u *dirname_start = NULL;
6208 char_u *dirname_now = NULL;
6209 int found_match;
6210 aco_save_T aco;
6211
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006212 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6213 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006214 if (dirname_start == NULL || dirname_now == NULL)
6215 goto theend;
6216
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006217 // Remember the current directory, because a BufRead autocommand that does
6218 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006219 mch_dirname(dirname_start, MAXPATHL);
6220
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006221 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006222 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6223 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006224 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006225 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006226 if (time(NULL) > seconds)
6227 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006228 // Display the file name every second or so, show the user we are
6229 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006230 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006231 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006232 }
6233
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006234 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006235 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6236 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006237 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006238 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006239 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006240 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006241
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006242 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006243 }
6244 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006245 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006246 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006247
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006248 // Check whether the quickfix list is still valid. When loading a
6249 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006250 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006251 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006252
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006253 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006254
Bram Moolenaar81695252004-12-29 20:58:21 +00006255 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006256 {
6257 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006258 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006259 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006260 else
6261 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006262 // Try for a match in all lines of the buffer.
6263 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006264 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006265 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006266 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006267
Bram Moolenaar81695252004-12-29 20:58:21 +00006268 if (using_dummy)
6269 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006270 if (found_match && *first_match_buf == NULL)
6271 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006272 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006273 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006274 // Never keep a dummy buffer if there is another buffer
6275 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006276 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006277 buf = NULL;
6278 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006279 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006280 || buf->b_p_bh[0] == 'u' // "unload"
6281 || buf->b_p_bh[0] == 'w' // "wipe"
6282 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006283 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006284 // When no match was found we don't need to remember the
6285 // buffer, wipe it out. If there was a match and it
6286 // wasn't the first one or we won't jump there: only
6287 // unload the buffer.
6288 // Ignore 'hidden' here, because it may lead to having too
6289 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006290 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006291 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006292 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006293 buf = NULL;
6294 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006295 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006296 || (cmd_args->flags & VGR_NOJUMP)
6297 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006298 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006299 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006300 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006301 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006302 buf = NULL;
6303 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006304 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006305
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006306 if (buf != NULL)
6307 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006308 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006309 buf->b_flags &= ~BF_DUMMY;
6310
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006311 // If the buffer is still loaded we need to use the
6312 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006313 if (buf == *first_match_buf
6314 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006315 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006316 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006317
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006318 // The buffer is still loaded, the Filetype autocommands
6319 // need to be done now, in that buffer. And the modelines
6320 // need to be done (again). But not the window-local
6321 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006322 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006323#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006324 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6325 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006326#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006327 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006328 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006329 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006330 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006331 }
6332 }
6333
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006334 status = OK;
6335
6336theend:
6337 vim_free(dirname_now);
6338 vim_free(dirname_start);
6339 return status;
6340}
6341
6342/*
6343 * ":vimgrep {pattern} file(s)"
6344 * ":vimgrepadd {pattern} file(s)"
6345 * ":lvimgrep {pattern} file(s)"
6346 * ":lvimgrepadd {pattern} file(s)"
6347 */
6348 void
6349ex_vimgrep(exarg_T *eap)
6350{
6351 vgr_args_T args;
6352 qf_info_T *qi;
6353 qf_list_T *qfl;
6354 int_u save_qfid;
6355 win_T *wp = NULL;
6356 int redraw_for_dummy = FALSE;
6357 buf_T *first_match_buf = NULL;
6358 char_u *target_dir = NULL;
6359 char_u *au_name = NULL;
6360 int status;
6361
6362 au_name = vgr_get_auname(eap->cmdidx);
6363 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6364 curbuf->b_fname, TRUE, curbuf))
6365 {
6366#ifdef FEAT_EVAL
6367 if (aborting())
6368 return;
6369#endif
6370 }
6371
6372 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6373 if (qi == NULL)
6374 return;
6375
6376 if (vgr_process_args(eap, &args) == FAIL)
6377 goto theend;
6378
6379 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6380 && eap->cmdidx != CMD_vimgrepadd
6381 && eap->cmdidx != CMD_lvimgrepadd)
6382 || qf_stack_empty(qi))
6383 // make place for a new list
6384 qf_new_list(qi, args.qf_title);
6385
6386 incr_quickfix_busy();
6387
6388 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6389 &first_match_buf, &target_dir);
6390 if (status != OK)
6391 {
6392 FreeWild(args.fcount, args.fnames);
6393 decr_quickfix_busy();
6394 goto theend;
6395 }
6396
6397 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006398
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006399 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006400 qfl->qf_nonevalid = FALSE;
6401 qfl->qf_ptr = qfl->qf_start;
6402 qfl->qf_index = 1;
6403 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006404
Bram Moolenaar864293a2016-06-02 13:40:04 +02006405 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006406
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006407 // Remember the current quickfix list identifier, so that we can check for
6408 // autocommands changing the current quickfix list.
6409 save_qfid = qf_get_curlist(qi)->qf_id;
6410
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006411 if (au_name != NULL)
6412 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6413 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006414 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6415 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006416 if (!qflist_valid(wp, save_qfid)
6417 || qf_restore_list(qi, save_qfid) == FAIL)
6418 {
6419 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006420 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006421 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006422
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006423 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006424 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006426 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006427 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6428 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006429 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006430 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006431 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006432
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006433 decr_quickfix_busy();
6434
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006435 // If we loaded a dummy buffer into the current window, the autocommands
6436 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006437 if (redraw_for_dummy)
6438 {
6439#ifdef FEAT_FOLDING
6440 foldUpdateAll(curwin);
6441#else
6442 redraw_later(NOT_VALID);
6443#endif
6444 }
6445
Bram Moolenaar86b68352004-12-27 21:59:20 +00006446theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006447 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006448 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006449 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006450}
6451
6452/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006453 * Restore current working directory to "dirname_start" if they differ, taking
6454 * into account whether it is set locally or globally.
6455 */
6456 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006457restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006458{
6459 char_u *dirname_now = alloc(MAXPATHL);
6460
6461 if (NULL != dirname_now)
6462 {
6463 mch_dirname(dirname_now, MAXPATHL);
6464 if (STRCMP(dirname_start, dirname_now) != 0)
6465 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006466 // If the directory has changed, change it back by building up an
6467 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006468 exarg_T ea;
6469
Bram Moolenaara80faa82020-04-12 19:37:17 +02006470 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006471 ea.arg = dirname_start;
6472 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6473 ex_cd(&ea);
6474 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006475 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006476 }
6477}
6478
6479/*
6480 * Load file "fname" into a dummy buffer and return the buffer pointer,
6481 * placing the directory resulting from the buffer load into the
6482 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6483 * prior to calling this function. Restores directory to "dirname_start" prior
6484 * to returning, if autocmds or the 'autochdir' option have changed it.
6485 *
6486 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6487 * or wipe_dummy_buffer() later!
6488 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006489 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006490 */
6491 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006492load_dummy_buffer(
6493 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006494 char_u *dirname_start, // in: old directory
6495 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006496{
6497 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006498 bufref_T newbufref;
6499 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006500 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006501 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006502 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006503
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006504 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006505 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6506 if (newbuf == NULL)
6507 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006508 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006509
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006510 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006511 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006513 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006514 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006515 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006516 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006517 ++newbuf->b_locked;
6518
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006519 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006520 aucmd_prepbuf(&aco, newbuf);
6521
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006522 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006523 (void)setfname(curbuf, fname, NULL, FALSE);
6524
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006525 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006526 check_need_swap(TRUE);
6527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006528 // Remove the "dummy" flag, otherwise autocommands may not
6529 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006530 curbuf->b_flags &= ~BF_DUMMY;
6531
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006532 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006533 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006534 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006535 NULL, READ_NEW | READ_DUMMY);
6536 --newbuf->b_locked;
6537 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006538 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006539 && !(curbuf->b_flags & BF_NEW))
6540 {
6541 failed = FALSE;
6542 if (curbuf != newbuf)
6543 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006544 // Bloody autocommands changed the buffer! Can happen when
6545 // using netrw and editing a remote file. Use the current
6546 // buffer instead, delete the dummy one after restoring the
6547 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006548 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006549 newbuf = curbuf;
6550 }
6551 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006553 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006554 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006555 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6556 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006558 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6559 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006560 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006561 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006562
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006563 // When autocommands/'autochdir' option changed directory: go back.
6564 // Let the caller know what the resulting dir was first, in case it is
6565 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006566 mch_dirname(resulting_dir, MAXPATHL);
6567 restore_start_dir(dirname_start);
6568
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006569 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006570 return NULL;
6571 if (failed)
6572 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006573 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006574 return NULL;
6575 }
6576 return newbuf;
6577}
6578
6579/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006580 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6581 * directory to "dirname_start" prior to returning, if autocmds or the
6582 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006583 */
6584 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006585wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006586{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006587 // If any autocommand opened a window on the dummy buffer, close that
6588 // window. If we can't close them all then give up.
6589 while (buf->b_nwindows > 0)
6590 {
6591 int did_one = FALSE;
6592 win_T *wp;
6593
6594 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006595 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006596 if (wp->w_buffer == buf)
6597 {
6598 if (win_close(wp, FALSE) == OK)
6599 did_one = TRUE;
6600 break;
6601 }
6602 if (!did_one)
6603 return;
6604 }
6605
6606 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006607 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006608#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006609 cleanup_T cs;
6610
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006611 // Reset the error/interrupt/exception state here so that aborting()
6612 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6613 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006614 enter_cleanup(&cs);
6615#endif
6616
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006617 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006618
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006619#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006620 // Restore the error/interrupt/exception state if not discarded by a
6621 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006622 leave_cleanup(&cs);
6623#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006624 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006625 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006626 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006627}
6628
6629/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006630 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6631 * directory to "dirname_start" prior to returning, if autocmds or the
6632 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006633 */
6634 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006635unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006636{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006637 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006638 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006639 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006640
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006641 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006642 restore_start_dir(dirname_start);
6643 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006644}
6645
Bram Moolenaar05159a02005-02-26 23:04:13 +00006646#if defined(FEAT_EVAL) || defined(PROTO)
6647/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006648 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006649 * to 'list'. Returns OK on success.
6650 */
6651 static int
6652get_qfline_items(qfline_T *qfp, list_T *list)
6653{
6654 int bufnum;
6655 dict_T *dict;
6656 char_u buf[2];
6657
6658 // Handle entries with a non-existing buffer number.
6659 bufnum = qfp->qf_fnum;
6660 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6661 bufnum = 0;
6662
6663 if ((dict = dict_alloc()) == NULL)
6664 return FAIL;
6665 if (list_append_dict(list, dict) == FAIL)
6666 return FAIL;
6667
6668 buf[0] = qfp->qf_type;
6669 buf[1] = NUL;
6670 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006671 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6672 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6673 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6674 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6675 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6676 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006677 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6678 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6679 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6680 || dict_add_string(dict, "type", buf) == FAIL
6681 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6682 return FAIL;
6683
6684 return OK;
6685}
6686
6687/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006688 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006689 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006690 * If eidx is not 0, then return only the specified entry. Otherwise return
6691 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006692 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006693 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006694get_errorlist(
6695 qf_info_T *qi_arg,
6696 win_T *wp,
6697 int qf_idx,
6698 int eidx,
6699 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006700{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006701 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006702 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006703 qfline_T *qfp;
6704 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006705
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006706 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006707 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006708 qi = &ql_info;
6709 if (wp != NULL)
6710 {
6711 qi = GET_LOC_LIST(wp);
6712 if (qi == NULL)
6713 return FAIL;
6714 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006715 }
6716
Bram Moolenaar858ba062020-05-31 23:11:59 +02006717 if (eidx < 0)
6718 return OK;
6719
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006720 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006721 qf_idx = qi->qf_curlist;
6722
Bram Moolenaar0398e002019-03-21 21:12:49 +01006723 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006724 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006725
Bram Moolenaar0398e002019-03-21 21:12:49 +01006726 qfl = qf_get_list(qi, qf_idx);
6727 if (qf_list_empty(qfl))
6728 return FAIL;
6729
Bram Moolenaara16123a2019-03-28 20:31:07 +01006730 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006731 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006732 if (eidx > 0)
6733 {
6734 if (eidx == i)
6735 return get_qfline_items(qfp, list);
6736 }
6737 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006738 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006739 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006740
Bram Moolenaar05159a02005-02-26 23:04:13 +00006741 return OK;
6742}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006744// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006745enum {
6746 QF_GETLIST_NONE = 0x0,
6747 QF_GETLIST_TITLE = 0x1,
6748 QF_GETLIST_ITEMS = 0x2,
6749 QF_GETLIST_NR = 0x4,
6750 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006751 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006752 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006753 QF_GETLIST_IDX = 0x40,
6754 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006755 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006756 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006757 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006758 QF_GETLIST_QFTF = 0x800,
6759 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006760};
6761
6762/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006763 * Parse text from 'di' and return the quickfix list items.
6764 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006765 */
6766 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006767qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006768{
6769 int status = FAIL;
6770 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006771 char_u *errorformat = p_efm;
6772 dictitem_T *efm_di;
6773 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006774
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006775 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006776 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006777 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006778 // If errorformat is supplied then use it, otherwise use the 'efm'
6779 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006780 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6781 {
6782 if (efm_di->di_tv.v_type != VAR_STRING ||
6783 efm_di->di_tv.vval.v_string == NULL)
6784 return FAIL;
6785 errorformat = efm_di->di_tv.vval.v_string;
6786 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006787
Bram Moolenaar36538222017-09-02 19:51:44 +02006788 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006789 if (l == NULL)
6790 return FAIL;
6791
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006792 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006793 if (qi != NULL)
6794 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006795 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006796 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6797 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006798 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006799 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006800 }
6801 free(qi);
6802 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006803 dict_add_list(retdict, "items", l);
6804 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006805 }
6806
6807 return status;
6808}
6809
6810/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006811 * Return the quickfix/location list window identifier in the current tabpage.
6812 */
6813 static int
6814qf_winid(qf_info_T *qi)
6815{
6816 win_T *win;
6817
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006818 // The quickfix window can be opened even if the quickfix list is not set
6819 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006820 if (qi == NULL)
6821 return 0;
6822 win = qf_find_win(qi);
6823 if (win != NULL)
6824 return win->w_id;
6825 return 0;
6826}
6827
6828/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006829 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006830 * window. If there is no buffer associated with the list or the buffer is
6831 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006832 */
6833 static int
6834qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6835{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006836 int bufnum = 0;
6837
6838 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6839 bufnum = qi->qf_bufnr;
6840
6841 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006842}
6843
6844/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006845 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006846 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006847 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006848qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006849{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006850 int flags = QF_GETLIST_NONE;
6851
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006852 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006853 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006854 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006855 if (!loclist)
6856 // File window ID is applicable only to location list windows
6857 flags &= ~ QF_GETLIST_FILEWINID;
6858 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006859
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006860 if (dict_find(what, (char_u *)"title", -1) != NULL)
6861 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006862
Bram Moolenaara6d48492017-12-12 22:45:31 +01006863 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6864 flags |= QF_GETLIST_NR;
6865
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006866 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6867 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006868
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006869 if (dict_find(what, (char_u *)"context", -1) != NULL)
6870 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006871
Bram Moolenaara6d48492017-12-12 22:45:31 +01006872 if (dict_find(what, (char_u *)"id", -1) != NULL)
6873 flags |= QF_GETLIST_ID;
6874
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006875 if (dict_find(what, (char_u *)"items", -1) != NULL)
6876 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006877
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006878 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6879 flags |= QF_GETLIST_IDX;
6880
6881 if (dict_find(what, (char_u *)"size", -1) != NULL)
6882 flags |= QF_GETLIST_SIZE;
6883
Bram Moolenaarb254af32017-12-18 19:48:58 +01006884 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6885 flags |= QF_GETLIST_TICK;
6886
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006887 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6888 flags |= QF_GETLIST_FILEWINID;
6889
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006890 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6891 flags |= QF_GETLIST_QFBUFNR;
6892
Bram Moolenaard43906d2020-07-20 21:31:32 +02006893 if (dict_find(what, (char_u *)"quickfixtextfunc", -1) != NULL)
6894 flags |= QF_GETLIST_QFTF;
6895
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006896 return flags;
6897}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006898
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006899/*
6900 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6901 * If 'nr' and 'id' are not present in 'what' then return the current
6902 * quickfix list index.
6903 * If 'nr' is zero then return the current quickfix list index.
6904 * If 'nr' is '$' then return the last quickfix list index.
6905 * If 'id' is present then return the index of the quickfix list with that id.
6906 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6907 * Return -1, if quickfix list is not present or if the stack is empty.
6908 */
6909 static int
6910qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6911{
6912 int qf_idx;
6913 dictitem_T *di;
6914
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006915 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006916 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6917 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006918 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006919 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006920 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006921 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006922 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006923 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006924 qf_idx = di->di_tv.vval.v_number - 1;
6925 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006926 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006927 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006928 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006929 else if (di->di_tv.v_type == VAR_STRING
6930 && di->di_tv.vval.v_string != NULL
6931 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006932 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006933 qf_idx = qi->qf_listcount - 1;
6934 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006935 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006936 }
6937
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006938 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6939 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006940 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006941 if (di->di_tv.v_type == VAR_NUMBER)
6942 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006943 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006944 if (di->di_tv.vval.v_number != 0)
6945 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6946 }
6947 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006948 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006949 }
6950
6951 return qf_idx;
6952}
6953
6954/*
6955 * Return default values for quickfix list properties in retdict.
6956 */
6957 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006958qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006959{
6960 int status = OK;
6961
6962 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006963 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006964 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6965 {
6966 list_T *l = list_alloc();
6967 if (l != NULL)
6968 status = dict_add_list(retdict, "items", l);
6969 else
6970 status = FAIL;
6971 }
6972 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006973 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006974 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006975 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006976 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006977 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006978 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006979 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006980 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006981 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006982 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006983 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006984 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006985 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006986 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006987 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006988 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6989 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02006990 if ((status == OK) && (flags & QF_GETLIST_QFTF))
6991 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006992
6993 return status;
6994}
6995
6996/*
6997 * Return the quickfix list title as 'title' in retdict
6998 */
6999 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007000qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007001{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007002 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007003}
7004
7005/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007006 * Returns the identifier of the window used to display files from a location
7007 * list. If there is no associated window, then returns 0. Useful only when
7008 * called from a location list window.
7009 */
7010 static int
7011qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7012{
7013 int winid = 0;
7014
7015 if (wp != NULL && IS_LL_WINDOW(wp))
7016 {
7017 win_T *ll_wp = qf_find_win_with_loclist(qi);
7018 if (ll_wp != NULL)
7019 winid = ll_wp->w_id;
7020 }
7021
7022 return dict_add_number(retdict, "filewinid", winid);
7023}
7024
7025/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007026 * Return the quickfix list items/entries as 'items' in retdict.
7027 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007028 */
7029 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007030qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007031{
7032 int status = OK;
7033 list_T *l = list_alloc();
7034 if (l != NULL)
7035 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007036 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007037 dict_add_list(retdict, "items", l);
7038 }
7039 else
7040 status = FAIL;
7041
7042 return status;
7043}
7044
7045/*
7046 * Return the quickfix list context (if any) as 'context' in retdict.
7047 */
7048 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007049qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007050{
7051 int status;
7052 dictitem_T *di;
7053
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007054 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007055 {
7056 di = dictitem_alloc((char_u *)"context");
7057 if (di != NULL)
7058 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007059 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007060 status = dict_add(retdict, di);
7061 if (status == FAIL)
7062 dictitem_free(di);
7063 }
7064 else
7065 status = FAIL;
7066 }
7067 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007068 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007069
7070 return status;
7071}
7072
7073/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007074 * Return the current quickfix list index as 'idx' in retdict.
7075 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007076 */
7077 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007078qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007079{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007080 if (eidx == 0)
7081 {
7082 eidx = qfl->qf_index;
7083 if (qf_list_empty(qfl))
7084 // For empty lists, current index is set to 0
7085 eidx = 0;
7086 }
7087 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007088}
7089
7090/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007091 * Return the 'quickfixtextfunc' function of a quickfix/location list
7092 */
7093 static int
7094qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7095{
7096 int status;
7097
7098 if (qfl->qftf_cb.cb_name != NULL)
7099 {
7100 typval_T tv;
7101
7102 put_callback(&qfl->qftf_cb, &tv);
7103 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7104 clear_tv(&tv);
7105 }
7106 else
7107 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7108
7109 return status;
7110}
7111
7112/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007113 * Return quickfix/location list details (title) as a
7114 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7115 * then current list is used. Otherwise the specified list is used.
7116 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007117 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007118qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7119{
7120 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007121 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007122 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007123 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007124 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007125 dictitem_T *di;
7126 int flags = QF_GETLIST_NONE;
7127
7128 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7129 return qf_get_list_from_lines(what, di, retdict);
7130
7131 if (wp != NULL)
7132 qi = GET_LOC_LIST(wp);
7133
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007134 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007135
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007136 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007137 qf_idx = qf_getprop_qfidx(qi, what);
7138
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007139 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007140 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007141 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007142
Bram Moolenaar0398e002019-03-21 21:12:49 +01007143 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007144
Bram Moolenaar858ba062020-05-31 23:11:59 +02007145 // If an entry index is specified, use that
7146 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7147 {
7148 if (di->di_tv.v_type != VAR_NUMBER)
7149 return FAIL;
7150 eidx = di->di_tv.vval.v_number;
7151 }
7152
Bram Moolenaard823fa92016-08-12 16:29:27 +02007153 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007154 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007155 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007156 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007157 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007158 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007159 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007160 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007161 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007162 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007163 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007164 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007165 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007166 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007167 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007168 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007169 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007170 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007171 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7172 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007173 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7174 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007175 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7176 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007177
Bram Moolenaard823fa92016-08-12 16:29:27 +02007178 return status;
7179}
7180
7181/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007182 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007183 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7184 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007185 */
7186 static int
7187qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007188 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007189 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007190 int first_entry,
7191 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007192{
7193 static int did_bufnr_emsg;
7194 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007195 int bufnum, valid, status, col, end_col, vcol, nr;
7196 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007197
7198 if (first_entry)
7199 did_bufnr_emsg = FALSE;
7200
Bram Moolenaar8f667172018-12-14 15:38:31 +01007201 filename = dict_get_string(d, (char_u *)"filename", TRUE);
7202 module = dict_get_string(d, (char_u *)"module", TRUE);
7203 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
7204 lnum = (int)dict_get_number(d, (char_u *)"lnum");
thinca6864efa2021-06-19 20:45:20 +02007205 end_lnum = (int)dict_get_number(d, (char_u *)"end_lnum");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007206 col = (int)dict_get_number(d, (char_u *)"col");
thinca6864efa2021-06-19 20:45:20 +02007207 end_col = (int)dict_get_number(d, (char_u *)"end_col");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007208 vcol = (int)dict_get_number(d, (char_u *)"vcol");
7209 nr = (int)dict_get_number(d, (char_u *)"nr");
7210 type = dict_get_string(d, (char_u *)"type", TRUE);
7211 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
7212 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007213 if (text == NULL)
7214 text = vim_strsave((char_u *)"");
7215
7216 valid = TRUE;
7217 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7218 valid = FALSE;
7219
7220 // Mark entries with non-existing buffer number as not valid. Give the
7221 // error message only once.
7222 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7223 {
7224 if (!did_bufnr_emsg)
7225 {
7226 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007227 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007228 }
7229 valid = FALSE;
7230 bufnum = 0;
7231 }
7232
7233 // If the 'valid' field is present it overrules the detected value.
7234 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar401f0c02020-09-05 22:37:39 +02007235 valid = (int)dict_get_bool(d, (char_u *)"valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007236
Bram Moolenaar0398e002019-03-21 21:12:49 +01007237 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007238 NULL, // dir
7239 filename,
7240 module,
7241 bufnum,
7242 text,
7243 lnum,
thinca6864efa2021-06-19 20:45:20 +02007244 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007245 col,
thinca6864efa2021-06-19 20:45:20 +02007246 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007247 vcol, // vis_col
7248 pattern, // search pattern
7249 nr,
7250 type == NULL ? NUL : *type,
7251 valid);
7252
7253 vim_free(filename);
7254 vim_free(module);
7255 vim_free(pattern);
7256 vim_free(text);
7257 vim_free(type);
7258
Bram Moolenaar9752c722018-12-22 16:49:34 +01007259 if (valid)
7260 *valid_entry = TRUE;
7261
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007262 return status;
7263}
7264
7265/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007266 * Add list of entries to quickfix/location list. Each list entry is
7267 * a dictionary with item information.
7268 */
7269 static int
7270qf_add_entries(
7271 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007272 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007273 list_T *list,
7274 char_u *title,
7275 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007276{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007277 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007278 listitem_T *li;
7279 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007280 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007281 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007282 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007283
Bram Moolenaara3921f42017-06-04 15:30:34 +02007284 if (action == ' ' || qf_idx == qi->qf_listcount)
7285 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007286 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007287 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007288 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007289 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007290 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007291 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007292 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007293 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007294 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007295 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007296 qf_free_items(qfl);
7297 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007298 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007299
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007300 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007301 {
7302 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007303 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007304
7305 d = li->li_tv.vval.v_dict;
7306 if (d == NULL)
7307 continue;
7308
Bram Moolenaar0398e002019-03-21 21:12:49 +01007309 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007310 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007311 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007312 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007313 }
7314
Bram Moolenaar9752c722018-12-22 16:49:34 +01007315 // Check if any valid error entries are added to the list.
7316 if (valid_entry)
7317 qfl->qf_nonevalid = FALSE;
7318 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007319 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007320 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007321
7322 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007323 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007324 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007325
7326 // Update the current error index if not appending to the list or if the
7327 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007328 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007329 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007330
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007331 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007332 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007333
7334 return retval;
7335}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007336
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007337/*
7338 * Get the quickfix list index from 'nr' or 'id'
7339 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007340 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007341qf_setprop_get_qfidx(
7342 qf_info_T *qi,
7343 dict_T *what,
7344 int action,
7345 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007346{
7347 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007348 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007349
Bram Moolenaard823fa92016-08-12 16:29:27 +02007350 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7351 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007352 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007353 if (di->di_tv.v_type == VAR_NUMBER)
7354 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007355 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007356 if (di->di_tv.vval.v_number != 0)
7357 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007358
Bram Moolenaar55b69262017-08-13 13:42:01 +02007359 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7360 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007361 // When creating a new list, accept qf_idx pointing to the next
7362 // non-available list and add the new list at the end of the
7363 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007364 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007365 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007366 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007367 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007368 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007369 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007370 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007371 }
7372 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007373 && di->di_tv.vval.v_string != NULL
7374 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007375 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007376 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007377 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007378 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007379 qf_idx = 0;
7380 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007381 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007382 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007383 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007384 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007385 }
7386
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007387 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007388 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007389 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007390 if (di->di_tv.v_type != VAR_NUMBER)
7391 return INVALID_QFIDX;
7392
7393 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007394 }
7395
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007396 return qf_idx;
7397}
7398
7399/*
7400 * Set the quickfix list title.
7401 */
7402 static int
7403qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7404{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007405 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007406
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007407 if (di->di_tv.v_type != VAR_STRING)
7408 return FAIL;
7409
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007410 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007411 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007412 if (qf_idx == qi->qf_curlist)
7413 qf_update_win_titlevar(qi);
7414
7415 return OK;
7416}
7417
7418/*
7419 * Set quickfix list items/entries.
7420 */
7421 static int
7422qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7423{
7424 int retval = FAIL;
7425 char_u *title_save;
7426
7427 if (di->di_tv.v_type != VAR_LIST)
7428 return FAIL;
7429
7430 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7431 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7432 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007433 vim_free(title_save);
7434
7435 return retval;
7436}
7437
7438/*
7439 * Set quickfix list items/entries from a list of lines.
7440 */
7441 static int
7442qf_setprop_items_from_lines(
7443 qf_info_T *qi,
7444 int qf_idx,
7445 dict_T *what,
7446 dictitem_T *di,
7447 int action)
7448{
7449 char_u *errorformat = p_efm;
7450 dictitem_T *efm_di;
7451 int retval = FAIL;
7452
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007453 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007454 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7455 {
7456 if (efm_di->di_tv.v_type != VAR_STRING ||
7457 efm_di->di_tv.vval.v_string == NULL)
7458 return FAIL;
7459 errorformat = efm_di->di_tv.vval.v_string;
7460 }
7461
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007462 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007463 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7464 return FAIL;
7465
7466 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007467 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007468 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007469 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007470 retval = OK;
7471
7472 return retval;
7473}
7474
7475/*
7476 * Set quickfix list context.
7477 */
7478 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007479qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007480{
7481 typval_T *ctx;
7482
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007483 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007484 ctx = alloc_tv();
7485 if (ctx != NULL)
7486 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007487 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007488
7489 return OK;
7490}
7491
7492/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007493 * Set the current index in the specified quickfix list
7494 */
7495 static int
7496qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7497{
7498 int denote = FALSE;
7499 int newidx;
7500 int old_qfidx;
7501 qfline_T *qf_ptr;
7502
7503 // If the specified index is '$', then use the last entry
7504 if (di->di_tv.v_type == VAR_STRING
7505 && di->di_tv.vval.v_string != NULL
7506 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7507 newidx = qfl->qf_count;
7508 else
7509 {
7510 // Otherwise use the specified index
7511 newidx = tv_get_number_chk(&di->di_tv, &denote);
7512 if (denote)
7513 return FAIL;
7514 }
7515
7516 if (newidx < 1) // sanity check
7517 return FAIL;
7518 if (newidx > qfl->qf_count)
7519 newidx = qfl->qf_count;
7520
7521 old_qfidx = qfl->qf_index;
7522 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7523 if (qf_ptr == NULL)
7524 return FAIL;
7525 qfl->qf_ptr = qf_ptr;
7526 qfl->qf_index = newidx;
7527
7528 // If the current list is modified and it is displayed in the quickfix
7529 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007530 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007531 qf_win_pos_update(qi, old_qfidx);
7532
7533 return OK;
7534}
7535
7536/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007537 * Set the current index in the specified quickfix list
7538 */
7539 static int
7540qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7541{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007542 callback_T cb;
7543
7544 free_callback(&qfl->qftf_cb);
7545 cb = get_callback(&di->di_tv);
7546 if (cb.cb_name != NULL && *cb.cb_name != NUL)
7547 set_callback(&qfl->qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007548
7549 return OK;
7550}
7551
7552/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007553 * Set quickfix/location list properties (title, items, context).
7554 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007555 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007556 */
7557 static int
7558qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7559{
7560 dictitem_T *di;
7561 int retval = FAIL;
7562 int qf_idx;
7563 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007564 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007565
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007566 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007567 newlist = TRUE;
7568
7569 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007570 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007571 return FAIL;
7572
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007573 if (newlist)
7574 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007575 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007576 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007577 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007578 }
7579
Bram Moolenaar0398e002019-03-21 21:12:49 +01007580 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007581 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007582 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007583 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007584 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007585 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007586 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007587 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007588 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007589 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7590 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007591 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7592 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007593
Bram Moolenaar287153c2020-11-29 14:20:27 +01007594 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007595 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007596 if (newlist)
7597 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007598
Bram Moolenaard823fa92016-08-12 16:29:27 +02007599 return retval;
7600}
7601
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007602/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007603 * Free the entire quickfix/location list stack.
7604 * If the quickfix/location list window is open, then clear it.
7605 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007606 static void
7607qf_free_stack(win_T *wp, qf_info_T *qi)
7608{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007609 win_T *qfwin = qf_find_win(qi);
7610 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007611
7612 if (qfwin != NULL)
7613 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007614 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007615 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007616 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007617 qf_update_buffer(qi, NULL);
7618 }
7619
7620 if (wp != NULL && IS_LL_WINDOW(wp))
7621 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007622 // If in the location list window, then use the non-location list
7623 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007624 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007625 if (llwin != NULL)
7626 wp = llwin;
7627 }
7628
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007629 qf_free_all(wp);
7630 if (wp == NULL)
7631 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007632 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007633 qi->qf_curlist = 0;
7634 qi->qf_listcount = 0;
7635 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007636 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007637 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007638 // If the location list window is open, then create a new empty
7639 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007640 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007641
Bram Moolenaar95946f12019-03-31 15:31:59 +02007642 if (new_ll != NULL)
7643 {
7644 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007645
Bram Moolenaar95946f12019-03-31 15:31:59 +02007646 // first free the list reference in the location list window
7647 ll_free_all(&qfwin->w_llist_ref);
7648
7649 qfwin->w_llist_ref = new_ll;
7650 if (wp != qfwin)
7651 win_set_loclist(wp, new_ll);
7652 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007653 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007654}
7655
Bram Moolenaard823fa92016-08-12 16:29:27 +02007656/*
7657 * Populate the quickfix list with the items supplied in the list
7658 * of dictionaries. "title" will be copied to w:quickfix_title.
7659 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007660 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007661 */
7662 int
7663set_errorlist(
7664 win_T *wp,
7665 list_T *list,
7666 int action,
7667 char_u *title,
7668 dict_T *what)
7669{
7670 qf_info_T *qi = &ql_info;
7671 int retval = OK;
7672
7673 if (wp != NULL)
7674 {
7675 qi = ll_get_or_alloc_list(wp);
7676 if (qi == NULL)
7677 return FAIL;
7678 }
7679
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007680 if (action == 'f')
7681 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007682 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007683 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007684 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007685 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007686
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007687 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007688 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007689 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007690 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007691 _("cannot have both a list and a \"what\" argument"));
7692 return FAIL;
7693 }
7694
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007695 incr_quickfix_busy();
7696
7697 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007698 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007699 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007700 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007701 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007702 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007703 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007704 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007705
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007706 decr_quickfix_busy();
7707
Bram Moolenaard823fa92016-08-12 16:29:27 +02007708 return retval;
7709}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007710
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007711/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007712 * Mark the quickfix context and callback function as in use for all the lists
7713 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007714 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007715 static int
7716mark_quickfix_ctx(qf_info_T *qi, int copyID)
7717{
7718 int i;
7719 int abort = FALSE;
7720 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007721 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007722
7723 for (i = 0; i < LISTCOUNT && !abort; ++i)
7724 {
7725 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007726 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7727 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007728 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7729
7730 cb = &qi->qf_lists[i].qftf_cb;
7731 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007732 }
7733
7734 return abort;
7735}
7736
7737/*
7738 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007739 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007740 */
7741 int
7742set_ref_in_quickfix(int copyID)
7743{
7744 int abort = FALSE;
7745 tabpage_T *tp;
7746 win_T *win;
7747
7748 abort = mark_quickfix_ctx(&ql_info, copyID);
7749 if (abort)
7750 return abort;
7751
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007752 abort = set_ref_in_callback(&qftf_cb, copyID);
7753 if (abort)
7754 return abort;
7755
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007756 FOR_ALL_TAB_WINDOWS(tp, win)
7757 {
7758 if (win->w_llist != NULL)
7759 {
7760 abort = mark_quickfix_ctx(win->w_llist, copyID);
7761 if (abort)
7762 return abort;
7763 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007764 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7765 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007766 // In a location list window and none of the other windows is
7767 // referring to this location list. Mark the location list
7768 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007769 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7770 if (abort)
7771 return abort;
7772 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007773 }
7774
7775 return abort;
7776}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007777#endif
7778
Bram Moolenaar81695252004-12-29 20:58:21 +00007779/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007780 * Return the autocmd name for the :cbuffer Ex commands
7781 */
7782 static char_u *
7783cbuffer_get_auname(cmdidx_T cmdidx)
7784{
7785 switch (cmdidx)
7786 {
7787 case CMD_cbuffer: return (char_u *)"cbuffer";
7788 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7789 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7790 case CMD_lbuffer: return (char_u *)"lbuffer";
7791 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7792 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7793 default: return NULL;
7794 }
7795}
7796
7797/*
7798 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7799 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7800 */
7801 static int
7802cbuffer_process_args(
7803 exarg_T *eap,
7804 buf_T **bufp,
7805 linenr_T *line1,
7806 linenr_T *line2)
7807{
7808 buf_T *buf = NULL;
7809
7810 if (*eap->arg == NUL)
7811 buf = curbuf;
7812 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7813 buf = buflist_findnr(atoi((char *)eap->arg));
7814
7815 if (buf == NULL)
7816 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007817 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007818 return FAIL;
7819 }
7820
7821 if (buf->b_ml.ml_mfp == NULL)
7822 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007823 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007824 return FAIL;
7825 }
7826
7827 if (eap->addr_count == 0)
7828 {
7829 eap->line1 = 1;
7830 eap->line2 = buf->b_ml.ml_line_count;
7831 }
7832
7833 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7834 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7835 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007836 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007837 return FAIL;
7838 }
7839
7840 *line1 = eap->line1;
7841 *line2 = eap->line2;
7842 *bufp = buf;
7843
7844 return OK;
7845}
7846
7847/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007848 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007849 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007850 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007851 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007852 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007853 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007854 */
7855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007856ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007857{
7858 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007859 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007860 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007861 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007862 int_u save_qfid;
7863 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007864 char_u *qf_title;
7865 linenr_T line1;
7866 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007867
Bram Moolenaara16123a2019-03-28 20:31:07 +01007868 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007869 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007870 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007871 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007872#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007873 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007874 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007875#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007876 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007877
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007878 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007879 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7880 if (qi == NULL)
7881 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007882
Bram Moolenaara16123a2019-03-28 20:31:07 +01007883 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7884 return;
7885
7886 qf_title = qf_cmdtitle(*eap->cmdlinep);
7887
7888 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007889 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007890 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7891 (char *)qf_title, (char *)buf->b_sfname);
7892 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007893 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007894
7895 incr_quickfix_busy();
7896
7897 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7898 (eap->cmdidx != CMD_caddbuffer
7899 && eap->cmdidx != CMD_laddbuffer),
7900 line1, line2,
7901 qf_title, NULL);
7902 if (qf_stack_empty(qi))
7903 {
7904 decr_quickfix_busy();
7905 return;
7906 }
7907 if (res >= 0)
7908 qf_list_changed(qf_get_curlist(qi));
7909
7910 // Remember the current quickfix list identifier, so that we can
7911 // check for autocommands changing the current quickfix list.
7912 save_qfid = qf_get_curlist(qi)->qf_id;
7913 if (au_name != NULL)
7914 {
7915 buf_T *curbuf_old = curbuf;
7916
7917 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7918 TRUE, curbuf);
7919 if (curbuf != curbuf_old)
7920 // Autocommands changed buffer, don't jump now, "qi" may
7921 // be invalid.
7922 res = 0;
7923 }
7924 // Jump to the first error for a new list and if autocmds didn't
7925 // free the list.
7926 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7927 eap->cmdidx == CMD_lbuffer)
7928 && qflist_valid(wp, save_qfid))
7929 // display the first error
7930 qf_jump_first(qi, save_qfid, eap->forceit);
7931
7932 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007933}
7934
Bram Moolenaar1e015462005-09-25 22:16:38 +00007935#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007936/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007937 * Return the autocmd name for the :cexpr Ex commands.
7938 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007939 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007940cexpr_get_auname(cmdidx_T cmdidx)
7941{
7942 switch (cmdidx)
7943 {
7944 case CMD_cexpr: return (char_u *)"cexpr";
7945 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7946 case CMD_caddexpr: return (char_u *)"caddexpr";
7947 case CMD_lexpr: return (char_u *)"lexpr";
7948 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7949 case CMD_laddexpr: return (char_u *)"laddexpr";
7950 default: return NULL;
7951 }
7952}
7953
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007954 int
7955trigger_cexpr_autocmd(int cmdidx)
7956{
7957 char_u *au_name = cexpr_get_auname(cmdidx);
7958
7959 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7960 curbuf->b_fname, TRUE, curbuf))
7961 {
7962 if (aborting())
7963 return FAIL;
7964 }
7965 return OK;
7966}
7967
7968 int
7969cexpr_core(exarg_T *eap, typval_T *tv)
7970{
7971 qf_info_T *qi;
7972 win_T *wp = NULL;
7973
7974 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7975 if (qi == NULL)
7976 return FAIL;
7977
7978 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7979 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7980 {
7981 int res;
7982 int_u save_qfid;
7983 char_u *au_name = cexpr_get_auname(eap->cmdidx);
7984
7985 incr_quickfix_busy();
7986 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
7987 (eap->cmdidx != CMD_caddexpr
7988 && eap->cmdidx != CMD_laddexpr),
7989 (linenr_T)0, (linenr_T)0,
7990 qf_cmdtitle(*eap->cmdlinep), NULL);
7991 if (qf_stack_empty(qi))
7992 {
7993 decr_quickfix_busy();
7994 return FAIL;
7995 }
7996 if (res >= 0)
7997 qf_list_changed(qf_get_curlist(qi));
7998
7999 // Remember the current quickfix list identifier, so that we can
8000 // check for autocommands changing the current quickfix list.
8001 save_qfid = qf_get_curlist(qi)->qf_id;
8002 if (au_name != NULL)
8003 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8004 curbuf->b_fname, TRUE, curbuf);
8005
8006 // Jump to the first error for a new list and if autocmds didn't
8007 // free the list.
8008 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8009 && qflist_valid(wp, save_qfid))
8010 // display the first error
8011 qf_jump_first(qi, save_qfid, eap->forceit);
8012 decr_quickfix_busy();
8013 return OK;
8014 }
8015
Bram Moolenaar677658a2022-01-05 16:09:06 +00008016 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008017 return FAIL;
8018}
8019
Bram Moolenaara16123a2019-03-28 20:31:07 +01008020/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008021 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8022 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008023 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008024 */
8025 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008026ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008027{
8028 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008029
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008030 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008031 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008032
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008033 // Evaluate the expression. When the result is a string or a list we can
8034 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008035 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008036 if (tv != NULL)
8037 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008038 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008039 free_tv(tv);
8040 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008041}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008042#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008043
8044/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008045 * Get the location list for ":lhelpgrep"
8046 */
8047 static qf_info_T *
8048hgr_get_ll(int *new_ll)
8049{
8050 win_T *wp;
8051 qf_info_T *qi;
8052
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008053 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008054 if (bt_help(curwin->w_buffer))
8055 wp = curwin;
8056 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008057 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008058 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008059
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008060 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008061 qi = NULL;
8062 else
8063 qi = wp->w_llist;
8064
8065 if (qi == NULL)
8066 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008067 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008068 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008069 return NULL;
8070 *new_ll = TRUE;
8071 }
8072
8073 return qi;
8074}
8075
8076/*
8077 * Search for a pattern in a help file.
8078 */
8079 static void
8080hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008081 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008082 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008083 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008084 regmatch_T *p_regmatch)
8085{
8086 FILE *fd;
8087 long lnum;
8088
8089 fd = mch_fopen((char *)fname, "r");
8090 if (fd == NULL)
8091 return;
8092
8093 lnum = 1;
8094 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8095 {
8096 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008097
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008098 // Convert a line if 'encoding' is not utf-8 and
8099 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008100 if (p_vc->vc_type != CONV_NONE
8101 && has_non_ascii(IObuff))
8102 {
8103 line = string_convert(p_vc, IObuff, NULL);
8104 if (line == NULL)
8105 line = IObuff;
8106 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008107
8108 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8109 {
8110 int l = (int)STRLEN(line);
8111
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008112 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008113 while (l > 0 && line[l - 1] <= ' ')
8114 line[--l] = NUL;
8115
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008116 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008117 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008118 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008119 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008120 0,
8121 line,
8122 lnum,
thinca6864efa2021-06-19 20:45:20 +02008123 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008124 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008125 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008126 (int)(p_regmatch->endp[0] - line)
8127 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008128 FALSE, // vis_col
8129 NULL, // search pattern
8130 0, // nr
8131 1, // type
8132 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008133 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008134 {
8135 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008136 if (line != IObuff)
8137 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008138 break;
8139 }
8140 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008141 if (line != IObuff)
8142 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008143 ++lnum;
8144 line_breakcheck();
8145 }
8146 fclose(fd);
8147}
8148
8149/*
8150 * Search for a pattern in all the help files in the doc directory under
8151 * the given directory.
8152 */
8153 static void
8154hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008155 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008156 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008157 regmatch_T *p_regmatch,
8158 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008159#ifdef FEAT_MULTI_LANG
8160 , char_u *lang
8161#endif
8162 )
8163{
8164 int fcount;
8165 char_u **fnames;
8166 int fi;
8167
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008168 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008169 add_pathsep(dirname);
8170 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8171 if (gen_expand_wildcards(1, &dirname, &fcount,
8172 &fnames, EW_FILE|EW_SILENT) == OK
8173 && fcount > 0)
8174 {
8175 for (fi = 0; fi < fcount && !got_int; ++fi)
8176 {
8177#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008178 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008179 if (lang != NULL
8180 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008181 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008182 && !(STRNICMP(lang, "en", 2) == 0
8183 && STRNICMP("txt", fnames[fi]
8184 + STRLEN(fnames[fi]) - 3, 3) == 0))
8185 continue;
8186#endif
8187
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008188 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008189 }
8190 FreeWild(fcount, fnames);
8191 }
8192}
8193
8194/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008195 * Search for a pattern in all the help files in the 'runtimepath'
8196 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008197 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008198 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008199 */
8200 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008201hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008202{
8203 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008204
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008205 vimconv_T vc;
8206
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008207 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8208 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008209 vc.vc_type = CONV_NONE;
8210 if (!enc_utf8)
8211 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008212
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008213 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008214 p = p_rtp;
8215 while (*p != NUL && !got_int)
8216 {
8217 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8218
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008219 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008220#ifdef FEAT_MULTI_LANG
8221 , lang
8222#endif
8223 );
8224 }
8225
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008226 if (vc.vc_type != CONV_NONE)
8227 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008228}
8229
8230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 * ":helpgrep {pattern}"
8232 */
8233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008234ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 regmatch_T regmatch;
8237 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008238 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008239 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008240 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008241 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008242 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243
Bram Moolenaar73633f82012-01-20 13:39:07 +01008244 switch (eap->cmdidx)
8245 {
8246 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8247 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8248 default: break;
8249 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008250 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8251 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008252 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008253#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008254 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008255 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008256#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008257 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008258
Bram Moolenaar39665952018-08-15 20:59:48 +02008259 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008260 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008261 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008262 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008263 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008264 }
8265
Bram Moolenaara16123a2019-03-28 20:31:07 +01008266 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8267 save_cpo = p_cpo;
8268 p_cpo = empty_option;
8269
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008270 incr_quickfix_busy();
8271
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008272#ifdef FEAT_MULTI_LANG
8273 // Check for a specified language
8274 lang = check_help_lang(eap->arg);
8275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8277 regmatch.rm_ic = FALSE;
8278 if (regmatch.regprog != NULL)
8279 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008280 qf_list_T *qfl;
8281
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008282 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008283 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008284 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008286 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008287
Bram Moolenaar473de612013-06-08 18:19:48 +02008288 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008290 qfl->qf_nonevalid = FALSE;
8291 qfl->qf_ptr = qfl->qf_start;
8292 qfl->qf_index = 1;
8293 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008294 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
8296
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008297 if (p_cpo == empty_option)
8298 p_cpo = save_cpo;
8299 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008300 {
8301 // Darn, some plugin changed the value. If it's still empty it was
8302 // changed and restored, need to restore in the complicated way.
8303 if (*p_cpo == NUL)
8304 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008305 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008306 }
8307
8308 if (updated)
8309 // This may open a window and source scripts, do this after 'cpo' was
8310 // restored.
8311 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312
Bram Moolenaar73633f82012-01-20 13:39:07 +01008313 if (au_name != NULL)
8314 {
8315 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8316 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008317 // When adding a location list to an existing location list stack,
8318 // if the autocmd made the stack invalid, then just return.
8319 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008320 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008321 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008322 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008323 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008324 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008325
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008326 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008327 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008328 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008329 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008330 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008331
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008332 decr_quickfix_busy();
8333
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008334 if (eap->cmdidx == CMD_lhelpgrep)
8335 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008336 // If the help window is not opened or if it already points to the
8337 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008338 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008339 {
8340 if (new_qi)
8341 ll_free_all(&qi);
8342 }
8343 else if (curwin->w_llist == NULL)
8344 curwin->w_llist = qi;
8345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008347#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008348
8349#if defined(FEAT_EVAL) || defined(PROTO)
8350# ifdef FEAT_QUICKFIX
8351 static void
8352get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8353{
8354 if (what_arg->v_type == VAR_UNKNOWN)
8355 {
8356 if (rettv_list_alloc(rettv) == OK)
8357 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008358 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008359 }
8360 else
8361 {
8362 if (rettv_dict_alloc(rettv) == OK)
8363 if (is_qf || (wp != NULL))
8364 {
8365 if (what_arg->v_type == VAR_DICT)
8366 {
8367 dict_T *d = what_arg->vval.v_dict;
8368
8369 if (d != NULL)
8370 qf_get_properties(wp, d, rettv->vval.v_dict);
8371 }
8372 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008373 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008374 }
8375 }
8376}
8377# endif
8378
8379/*
8380 * "getloclist()" function
8381 */
8382 void
8383f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8384{
8385# ifdef FEAT_QUICKFIX
8386 win_T *wp;
8387
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008388 if (in_vim9script()
8389 && (check_for_number_arg(argvars, 0) == FAIL
8390 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8391 return;
8392
Bram Moolenaare677df82019-09-02 22:31:11 +02008393 wp = find_win_by_nr_or_id(&argvars[0]);
8394 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8395# endif
8396}
8397
8398/*
8399 * "getqflist()" function
8400 */
8401 void
8402f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8403{
8404# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008405 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8406 return;
8407
Bram Moolenaare677df82019-09-02 22:31:11 +02008408 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8409# endif
8410}
8411
8412/*
8413 * Used by "setqflist()" and "setloclist()" functions
8414 */
8415 static void
8416set_qf_ll_list(
8417 win_T *wp UNUSED,
8418 typval_T *list_arg UNUSED,
8419 typval_T *action_arg UNUSED,
8420 typval_T *what_arg UNUSED,
8421 typval_T *rettv)
8422{
8423# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008424 char_u *act;
8425 int action = 0;
8426 static int recursive = 0;
8427# endif
8428
8429 rettv->vval.v_number = -1;
8430
8431# ifdef FEAT_QUICKFIX
8432 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008433 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008434 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008435 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008436 else
8437 {
8438 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008439 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008440 int valid_dict = TRUE;
8441
8442 if (action_arg->v_type == VAR_STRING)
8443 {
8444 act = tv_get_string_chk(action_arg);
8445 if (act == NULL)
8446 return; // type error; errmsg already given
8447 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8448 act[1] == NUL)
8449 action = *act;
8450 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008451 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008452 }
8453 else if (action_arg->v_type == VAR_UNKNOWN)
8454 action = ' ';
8455 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008456 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008457
8458 if (action_arg->v_type != VAR_UNKNOWN
8459 && what_arg->v_type != VAR_UNKNOWN)
8460 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008461 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8462 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008463 else
8464 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008465 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008466 valid_dict = FALSE;
8467 }
8468 }
8469
8470 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008471 if (l != NULL && action && valid_dict
8472 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008473 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008474 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008475 rettv->vval.v_number = 0;
8476 --recursive;
8477 }
8478# endif
8479}
8480
8481/*
8482 * "setloclist()" function
8483 */
8484 void
8485f_setloclist(typval_T *argvars, typval_T *rettv)
8486{
8487 win_T *win;
8488
8489 rettv->vval.v_number = -1;
8490
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008491 if (in_vim9script()
8492 && (check_for_number_arg(argvars, 0) == FAIL
8493 || check_for_list_arg(argvars, 1) == FAIL
8494 || check_for_opt_string_arg(argvars, 2) == FAIL
8495 || (argvars[2].v_type != VAR_UNKNOWN
8496 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8497 return;
8498
Bram Moolenaare677df82019-09-02 22:31:11 +02008499 win = find_win_by_nr_or_id(&argvars[0]);
8500 if (win != NULL)
8501 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8502}
8503
8504/*
8505 * "setqflist()" function
8506 */
8507 void
8508f_setqflist(typval_T *argvars, typval_T *rettv)
8509{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008510 if (in_vim9script()
8511 && (check_for_list_arg(argvars, 0) == FAIL
8512 || check_for_opt_string_arg(argvars, 1) == FAIL
8513 || (argvars[1].v_type != VAR_UNKNOWN
8514 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8515 return;
8516
Bram Moolenaare677df82019-09-02 22:31:11 +02008517 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8518}
8519#endif