blob: b9b908cc064e8421e6fa46d3c827c383a69447d0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
thinca6864efa2021-06-19 20:45:20 +020033 linenr_T qf_end_lnum; // line number when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020034 int qf_fnum; // file number for the line
35 int qf_col; // column where the error occurred
thinca6864efa2021-06-19 20:45:20 +020036 int qf_end_col; // column when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020037 int qf_nr; // error number
38 char_u *qf_module; // module name for this error
39 char_u *qf_pattern; // search pattern for the error
40 char_u *qf_text; // description of the error
thinca6864efa2021-06-19 20:45:20 +020041 char_u qf_viscol; // set to TRUE if qf_col and qf_end_col is
42 // screen column
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020043 char_u qf_cleared; // set to TRUE if line has been deleted
44 char_u qf_type; // type of the error (mostly 'E'); 1 for
45 // :helpgrep
46 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000047};
48
49/*
50 * There is a stack of error lists.
51 */
52#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020053#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010054#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020056/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010057 * Quickfix list type.
58 */
59typedef enum
60{
61 QFLT_QUICKFIX, // Quickfix list - global list
62 QFLT_LOCATION, // Location list - per window list
63 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
64} qfltype_T;
65
66/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 * Quickfix/Location list definition
68 * Contains a list of entries (qfline_T). qf_start points to the first entry
69 * and qf_last points to the last entry. qf_count contains the list size.
70 *
71 * Usually the list contains one or more entries. But an empty list can be
72 * created using setqflist()/setloclist() with a title and/or user context
73 * information and entries can be added later using setqflist()/setloclist().
74 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000075typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020077 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010078 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020079 qfline_T *qf_start; // pointer to the first error
80 qfline_T *qf_last; // pointer to the last error
81 qfline_T *qf_ptr; // pointer to the current error
82 int qf_count; // number of errors (0 means empty list)
83 int qf_index; // current index in the error list
84 int qf_nonevalid; // TRUE if not a single valid entry found
85 char_u *qf_title; // title derived from the command that created
86 // the error list or set by setqflist
87 typval_T *qf_ctx; // context set by setqflist/setloclist
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +010088 callback_T qf_qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020089
90 struct dir_stack_T *qf_dir_stack;
91 char_u *qf_directory;
92 struct dir_stack_T *qf_file_stack;
93 char_u *qf_currfile;
94 int qf_multiline;
95 int qf_multiignore;
96 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010097 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000098} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar6a8958d2017-06-22 21:33:20 +0200100/*
101 * Quickfix/Location list stack definition
102 * Contains a list of quickfix/location lists (qf_list_T)
103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000104struct qf_info_S
105{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 // Count of references to this list. Used only for location lists.
107 // When a location list window reference this list, qf_refcount
108 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
109 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200111 int qf_listcount; // current number of lists
112 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100114 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100115 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000116};
117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200118static qf_info_T ql_info; // global quickfix list
119static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
haya14busae023d492022-02-08 18:09:29 +0000121#define FMT_PATTERNS 13 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
124 * Structure used to hold the info of one part of 'errorformat'
125 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000126typedef struct efm_S efm_T;
127struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200129 regprog_T *prog; // pre-formatted part of 'errorformat'
130 efm_T *next; // pointer to next (NULL if last)
131 char_u addr[FMT_PATTERNS]; // indices of used % patterns
132 char_u prefix; // prefix of this format line:
133 // 'D' enter directory
134 // 'X' leave directory
135 // 'A' start of multi-line message
136 // 'E' error message
137 // 'W' warning message
138 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200139 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200140 // 'C' continuation line
141 // 'Z' end of multi-line message
142 // 'G' general, unspecific message
143 // 'P' push file (partial) message
144 // 'Q' pop/quit file (partial) message
145 // 'O' overread (partial) message
146 char_u flags; // additional flags given in prefix
147 // '-' do not include this line
148 // '+' include whole line in message
149 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150};
151
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200152// List of location lists to be deleted.
153// Used to delay the deletion of locations lists by autocmds.
154typedef struct qf_delq_S
155{
156 struct qf_delq_S *next;
157 qf_info_T *qi;
158} qf_delq_T;
159static qf_delq_T *qf_delq_head = NULL;
160
161// Counter to prevent autocmds from freeing up location lists when they are
162// still being used.
163static int quickfix_busy = 0;
164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200165static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100166
Bram Moolenaard43906d2020-07-20 21:31:32 +0200167// callback function for 'quickfixtextfunc'
168static callback_T qftf_cb;
169
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_new_list(qf_info_T *qi, char_u *qf_title);
thinca6864efa2021-06-19 20:45:20 +0200171static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, long end_lnum, int col, int end_col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200172static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100174static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200175static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200177static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200178static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
thinca6864efa2021-06-19 20:45:20 +0200180static void qf_range_text(qfline_T *qfp, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100181static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static win_T *qf_find_win(qf_info_T *qi);
183static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200184static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200185static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100186static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
187static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
188static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
189static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200191// Quickfix window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000192#define IS_QF_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200193// Location list window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000194#define IS_LL_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200195
196// Quickfix and location list stack check helper macros
kylo252ae6f1d82022-02-16 19:24:07 +0000197#define IS_QF_STACK(qi) ((qi)->qfl_type == QFLT_QUICKFIX)
198#define IS_LL_STACK(qi) ((qi)->qfl_type == QFLT_LOCATION)
199#define IS_QF_LIST(qfl) ((qfl)->qfl_type == QFLT_QUICKFIX)
200#define IS_LL_LIST(qfl) ((qfl)->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200201
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000202/*
203 * Return location list for window 'wp'
204 * For location list window, return the referenced location list
205 */
kylo252ae6f1d82022-02-16 19:24:07 +0000206#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? (wp)->w_llist_ref : (wp)->w_llist)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000207
Bram Moolenaar95946f12019-03-31 15:31:59 +0200208// Macro to loop through all the items in a quickfix list
209// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100210#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
kylo252ae6f1d82022-02-16 19:24:07 +0000211 for ((i) = 1, (qfp) = (qfl)->qf_start; \
212 !got_int && (i) <= (qfl)->qf_count && (qfp) != NULL; \
213 ++(i), (qfp) = (qfp)->qf_next)
Bram Moolenaara16123a2019-03-28 20:31:07 +0100214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200216 * Looking up a buffer can be slow if there are many. Remember the last one
217 * to make this a lot faster if there are multiple matches in the same file.
218 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200219static char_u *qf_last_bufname = NULL;
220static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200221
222/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200223 * Maximum number of bytes allowed per line while reading a errorfile.
224 */
225#define LINE_MAXLEN 4096
226
haya14busae023d492022-02-08 18:09:29 +0000227/*
228 * Patterns used. Keep in sync with qf_parse_fmt[].
229 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200230static struct fmtpattern
231{
232 char_u convchar;
233 char *pattern;
234} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200235 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200236 {'f', ".\\+"}, // only used when at end
haya14busae023d492022-02-08 18:09:29 +0000237 {'n', "\\d\\+"}, // 1
238 {'l', "\\d\\+"}, // 2
239 {'e', "\\d\\+"}, // 3
240 {'c', "\\d\\+"}, // 4
241 {'k', "\\d\\+"}, // 5
242 {'t', "."}, // 6
243#define FMT_PATTERN_M 7
244 {'m', ".\\+"}, // 7
245#define FMT_PATTERN_R 8
246 {'r', ".*"}, // 8
247 {'p', "[- .]*"}, // 9
248 {'v', "\\d\\+"}, // 10
249 {'s', ".\\+"}, // 11
250 {'o', ".\\+"} // 12
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200251 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200252
253/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 * See fmt_pat definition above for the list of supported patterns. The
256 * pattern specifier is supplied in "efmpat". The converted pattern is stored
257 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 */
259 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260efmpat_to_regpat(
261 char_u *efmpat,
262 char_u *regpat,
263 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200264 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100265 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266{
267 char_u *srcptr;
268
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200271 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000272 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200273 return NULL;
274 }
haya14busae023d492022-02-08 18:09:29 +0000275 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000277 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000280 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 return NULL;
282 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283 efminfo->addr[idx] = (char_u)++round;
284 *regpat++ = '\\';
285 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200288 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200289 // Also match "c:" in the file name, even when
290 // checking for a colon next: "%f:".
291 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 STRCPY(regpat, "\\%(\\a:\\)\\=");
293 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294 }
295#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200300 // A file name may contain spaces, but this isn't
301 // in "\f". For "%f:%l:%m" there may be a ":" in
302 // the file name. Use ".\{-1,}x" instead (x is
303 // the next character), the requirement that :999:
304 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 STRCPY(regpat, ".\\{-1,}");
306 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 }
308 else
309 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200310 // File name followed by '\\' or '%': include as
311 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 STRCPY(regpat, "\\f\\+");
313 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314 }
315 }
316 else
317 {
318 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 while ((*regpat = *srcptr++) != NUL)
320 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200322 *regpat++ = '\\';
323 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200325 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326}
327
328/*
329 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 */
332 static char_u *
333scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 char_u *efm,
336 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100337 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338{
339 char_u *efmp = *pefmp;
340
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200343 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 {
345 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200346 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347 if (efmp < efm + len)
348 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200349 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200350 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200352 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353 if (efmp == efm + len)
354 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000355 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 return NULL;
357 }
358 }
359 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200360 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 *regpat++ = *++efmp;
362 *regpat++ = '\\';
363 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 }
365 else
366 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200367 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000368 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369 return NULL;
370 }
371
372 *pefmp = efmp;
373
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375}
376
377/*
378 * Analyze/parse an errorformat prefix.
379 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100381efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200383 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200385 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200386 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200387 else
388 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000389 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 }
392
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394}
395
396/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200397 * Converts a 'errorformat' string part in 'efm' to a regular expression
398 * pattern. The resulting regex pattern is returned in "regpat". Additional
399 * information about the 'erroformat' pattern is returned in "fmt_ptr".
400 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200401 */
402 static int
403efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200404 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200405 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200406 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100407 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200408{
409 char_u *ptr;
410 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200411 int round;
412 int idx = 0;
413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200414 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200415 ptr = regpat;
416 *ptr++ = '^';
417 round = 0;
418 for (efmp = efm; efmp < efm + len; ++efmp)
419 {
420 if (*efmp == '%')
421 {
422 ++efmp;
423 for (idx = 0; idx < FMT_PATTERNS; ++idx)
424 if (fmt_pat[idx].convchar == *efmp)
425 break;
426 if (idx < FMT_PATTERNS)
427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100428 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200429 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200430 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200431 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200432 }
433 else if (*efmp == '*')
434 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200435 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100436 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200437 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200438 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200439 }
440 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200441 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 else if (*efmp == '#')
443 *ptr++ = '*';
444 else if (*efmp == '>')
445 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200446 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200448 // prefix is allowed only at the beginning of the errorformat
449 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100450 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200451 if (efmp == NULL)
452 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 }
454 else
455 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000456 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200457 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458 }
459 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200460 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200461 {
462 if (*efmp == '\\' && efmp + 1 < efm + len)
463 ++efmp;
464 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200465 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200466 if (*efmp)
467 *ptr++ = *efmp;
468 }
469 }
470 *ptr++ = '$';
471 *ptr = NUL;
472
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200473 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200474}
475
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200476/*
477 * Free the 'errorformat' information list
478 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200479 static void
480free_efm_list(efm_T **efm_first)
481{
482 efm_T *efm_ptr;
483
484 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
485 {
486 *efm_first = efm_ptr->next;
487 vim_regfree(efm_ptr->prog);
488 vim_free(efm_ptr);
489 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100490 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200491}
492
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200493/*
494 * Compute the size of the buffer used to convert a 'errorformat' pattern into
495 * a regular expression pattern.
496 */
497 static int
498efm_regpat_bufsz(char_u *efm)
499{
500 int sz;
501 int i;
502
503 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
504 for (i = FMT_PATTERNS; i > 0; )
505 sz += (int)STRLEN(fmt_pat[--i].pattern);
506#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200507 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200508#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200509 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200510#endif
511
512 return sz;
513}
514
515/*
516 * Return the length of a 'errorformat' option part (separated by ",").
517 */
518 static int
519efm_option_part_len(char_u *efm)
520{
521 int len;
522
523 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
524 if (efm[len] == '\\' && efm[len + 1] != NUL)
525 ++len;
526
527 return len;
528}
529
530/*
531 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
532 * are parsed and converted to regular expressions. Returns information about
533 * the parsed 'errorformat' option.
534 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200535 static efm_T *
536parse_efm_option(char_u *efm)
537{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200538 efm_T *fmt_ptr = NULL;
539 efm_T *fmt_first = NULL;
540 efm_T *fmt_last = NULL;
541 char_u *fmtstr = NULL;
542 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200543 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200545 // Each part of the format string is copied and modified from errorformat
546 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200547
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200548 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200549 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000550 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 goto parse_efm_error;
552
553 while (efm[0] != NUL)
554 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200555 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000556 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200557 if (fmt_ptr == NULL)
558 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200559 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200560 fmt_first = fmt_ptr;
561 else
562 fmt_last->next = fmt_ptr;
563 fmt_last = fmt_ptr;
564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200565 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200566 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100568 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200569 goto parse_efm_error;
570 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
571 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200572 // Advance to next part
573 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200574 }
575
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200576 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000577 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200578
579 goto parse_efm_end;
580
581parse_efm_error:
582 free_efm_list(&fmt_first);
583
584parse_efm_end:
585 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200586
587 return fmt_first;
588}
589
Bram Moolenaare0d37972016-07-15 22:36:01 +0200590enum {
591 QF_FAIL = 0,
592 QF_OK = 1,
593 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200594 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200595 QF_IGNORE_LINE = 4,
596 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200597};
598
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200599/*
600 * State information used to parse lines and add entries to a quickfix/location
601 * list.
602 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200603typedef struct {
604 char_u *linebuf;
605 int linelen;
606 char_u *growbuf;
607 int growbufsiz;
608 FILE *fd;
609 typval_T *tv;
610 char_u *p_str;
611 listitem_T *p_li;
612 buf_T *buf;
613 linenr_T buflnum;
614 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100615 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616} qfstate_T;
617
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200618/*
619 * Allocate more memory for the line buffer used for parsing lines.
620 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200621 static char_u *
622qf_grow_linebuf(qfstate_T *state, int newsz)
623{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200624 char_u *p;
625
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200626 // If the line exceeds LINE_MAXLEN exclude the last
627 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
629 if (state->growbuf == NULL)
630 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000631 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200632 if (state->growbuf == NULL)
633 return NULL;
634 state->growbufsiz = state->linelen;
635 }
636 else if (state->linelen > state->growbufsiz)
637 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200638 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200639 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200640 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200641 state->growbufsiz = state->linelen;
642 }
643 return state->growbuf;
644}
645
646/*
647 * Get the next string (separated by newline) from state->p_str.
648 */
649 static int
650qf_get_next_str_line(qfstate_T *state)
651{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200652 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200653 char_u *p_str = state->p_str;
654 char_u *p;
655 int len;
656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200657 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200658 return QF_END_OF_INPUT;
659
660 p = vim_strchr(p_str, '\n');
661 if (p != NULL)
662 len = (int)(p - p_str) + 1;
663 else
664 len = (int)STRLEN(p_str);
665
666 if (len > IOSIZE - 2)
667 {
668 state->linebuf = qf_grow_linebuf(state, len);
669 if (state->linebuf == NULL)
670 return QF_NOMEM;
671 }
672 else
673 {
674 state->linebuf = IObuff;
675 state->linelen = len;
676 }
677 vim_strncpy(state->linebuf, p_str, state->linelen);
678
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200679 // Increment using len in order to discard the rest of the
680 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 p_str += len;
682 state->p_str = p_str;
683
684 return QF_OK;
685}
686
687/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000688 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200689 */
690 static int
691qf_get_next_list_line(qfstate_T *state)
692{
693 listitem_T *p_li = state->p_li;
694 int len;
695
696 while (p_li != NULL
697 && (p_li->li_tv.v_type != VAR_STRING
698 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200699 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200700
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200701 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200702 {
703 state->p_li = NULL;
704 return QF_END_OF_INPUT;
705 }
706
707 len = (int)STRLEN(p_li->li_tv.vval.v_string);
708 if (len > IOSIZE - 2)
709 {
710 state->linebuf = qf_grow_linebuf(state, len);
711 if (state->linebuf == NULL)
712 return QF_NOMEM;
713 }
714 else
715 {
716 state->linebuf = IObuff;
717 state->linelen = len;
718 }
719
720 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200722 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200723 return QF_OK;
724}
725
726/*
727 * Get the next string from state->buf.
728 */
729 static int
730qf_get_next_buf_line(qfstate_T *state)
731{
732 char_u *p_buf = NULL;
733 int len;
734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200735 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200736 if (state->buflnum > state->lnumlast)
737 return QF_END_OF_INPUT;
738
739 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
740 state->buflnum += 1;
741
742 len = (int)STRLEN(p_buf);
743 if (len > IOSIZE - 2)
744 {
745 state->linebuf = qf_grow_linebuf(state, len);
746 if (state->linebuf == NULL)
747 return QF_NOMEM;
748 }
749 else
750 {
751 state->linebuf = IObuff;
752 state->linelen = len;
753 }
754 vim_strncpy(state->linebuf, p_buf, state->linelen);
755
756 return QF_OK;
757}
758
759/*
760 * Get the next string from file state->fd.
761 */
762 static int
763qf_get_next_file_line(qfstate_T *state)
764{
765 int discard;
766 int growbuflen;
767
768 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
769 return QF_END_OF_INPUT;
770
771 discard = FALSE;
772 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200773 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200774 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200775 // The current line exceeds IObuff, continue reading using
776 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200777 if (state->growbuf == NULL)
778 {
779 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000780 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200781 if (state->growbuf == NULL)
782 return QF_NOMEM;
783 }
784
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200785 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 memcpy(state->growbuf, IObuff, IOSIZE - 1);
787 growbuflen = state->linelen;
788
789 for (;;)
790 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200791 char_u *p;
792
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793 if (fgets((char *)state->growbuf + growbuflen,
794 state->growbufsiz - growbuflen, state->fd) == NULL)
795 break;
796 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
797 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200798 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 break;
800 if (state->growbufsiz == LINE_MAXLEN)
801 {
802 discard = TRUE;
803 break;
804 }
805
806 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
807 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200808 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200809 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200810 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200811 }
812
813 while (discard)
814 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200815 // The current line is longer than LINE_MAXLEN, continue
816 // reading but discard everything until EOL or EOF is
817 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200818 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
819 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200820 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200821 break;
822 }
823
824 state->linebuf = state->growbuf;
825 state->linelen = growbuflen;
826 }
827 else
828 state->linebuf = IObuff;
829
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200830 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200831 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
832 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100833 char_u *line;
834
835 line = string_convert(&state->vc, state->linebuf, &state->linelen);
836 if (line != NULL)
837 {
838 if (state->linelen < IOSIZE)
839 {
840 STRCPY(state->linebuf, line);
841 vim_free(line);
842 }
843 else
844 {
845 vim_free(state->growbuf);
846 state->linebuf = state->growbuf = line;
847 state->growbufsiz = state->linelen < LINE_MAXLEN
848 ? state->linelen : LINE_MAXLEN;
849 }
850 }
851 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100852
Bram Moolenaare0d37972016-07-15 22:36:01 +0200853 return QF_OK;
854}
855
856/*
857 * Get the next string from a file/buffer/list/string.
858 */
859 static int
860qf_get_nextline(qfstate_T *state)
861{
862 int status = QF_FAIL;
863
864 if (state->fd == NULL)
865 {
866 if (state->tv != NULL)
867 {
868 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200869 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200870 status = qf_get_next_str_line(state);
871 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200872 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200873 status = qf_get_next_list_line(state);
874 }
875 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200876 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200877 status = qf_get_next_buf_line(state);
878 }
879 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200880 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200881 status = qf_get_next_file_line(state);
882
883 if (status != QF_OK)
884 return status;
885
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200886 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200887 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200888 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200889 state->linebuf[state->linelen - 1] = NUL;
890#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200891 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
892 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200893#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200894 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200895
Bram Moolenaare0d37972016-07-15 22:36:01 +0200896 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200897
898 return QF_OK;
899}
900
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200901typedef struct {
902 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200903 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200904 char_u *errmsg;
905 int errmsglen;
906 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200907 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200908 int col;
thinca6864efa2021-06-19 20:45:20 +0200909 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200910 char_u use_viscol;
911 char_u *pattern;
912 int enr;
913 int type;
914 int valid;
915} qffields_T;
916
917/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200918 * Parse the match for filename ('%f') pattern in regmatch.
919 * Return the matched value in "fields->namebuf".
920 */
921 static int
922qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
923{
924 int c;
925
926 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
927 return QF_FAIL;
928
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200929 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200930 c = *rmp->endp[midx];
931 *rmp->endp[midx] = NUL;
932 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
933 *rmp->endp[midx] = c;
934
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200935 // For separate filename patterns (%O, %P and %Q), the specified file
936 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200937 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
938 && mch_getperm(fields->namebuf) == -1)
939 return QF_FAIL;
940
941 return QF_OK;
942}
943
944/*
945 * Parse the match for error number ('%n') pattern in regmatch.
946 * Return the matched value in "fields->enr".
947 */
948 static int
949qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
950{
951 if (rmp->startp[midx] == NULL)
952 return QF_FAIL;
953 fields->enr = (int)atol((char *)rmp->startp[midx]);
954 return QF_OK;
955}
956
957/*
haya14busae023d492022-02-08 18:09:29 +0000958 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200959 * Return the matched value in "fields->lnum".
960 */
961 static int
962qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
963{
964 if (rmp->startp[midx] == NULL)
965 return QF_FAIL;
966 fields->lnum = atol((char *)rmp->startp[midx]);
967 return QF_OK;
968}
969
970/*
haya14busae023d492022-02-08 18:09:29 +0000971 * Parse the match for end line number ('%e') pattern in regmatch.
972 * Return the matched value in "fields->end_lnum".
973 */
974 static int
975qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
976{
977 if (rmp->startp[midx] == NULL)
978 return QF_FAIL;
979 fields->end_lnum = atol((char *)rmp->startp[midx]);
980 return QF_OK;
981}
982
983/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200984 * Parse the match for column number ('%c') pattern in regmatch.
985 * Return the matched value in "fields->col".
986 */
987 static int
988qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
989{
990 if (rmp->startp[midx] == NULL)
991 return QF_FAIL;
992 fields->col = (int)atol((char *)rmp->startp[midx]);
993 return QF_OK;
994}
995
996/*
haya14busae023d492022-02-08 18:09:29 +0000997 * Parse the match for end column number ('%k') pattern in regmatch.
998 * Return the matched value in "fields->end_col".
999 */
1000 static int
1001qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1002{
1003 if (rmp->startp[midx] == NULL)
1004 return QF_FAIL;
1005 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1006 return QF_OK;
1007}
1008
1009/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001010 * Parse the match for error type ('%t') pattern in regmatch.
1011 * Return the matched value in "fields->type".
1012 */
1013 static int
1014qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1015{
1016 if (rmp->startp[midx] == NULL)
1017 return QF_FAIL;
1018 fields->type = *rmp->startp[midx];
1019 return QF_OK;
1020}
1021
1022/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001023 * Copy a non-error line into the error string. Return the matched line in
1024 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001025 */
1026 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001027copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001028{
1029 char_u *p;
1030
1031 if (linelen >= fields->errmsglen)
1032 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001033 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001034 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1035 return QF_NOMEM;
1036 fields->errmsg = p;
1037 fields->errmsglen = linelen + 1;
1038 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001039 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001040 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001041
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001042 return QF_OK;
1043}
1044
1045/*
1046 * Parse the match for error message ('%m') pattern in regmatch.
1047 * Return the matched value in "fields->errmsg".
1048 */
1049 static int
1050qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1051{
1052 char_u *p;
1053 int len;
1054
1055 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1056 return QF_FAIL;
1057 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1058 if (len >= fields->errmsglen)
1059 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001060 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001061 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1062 return QF_NOMEM;
1063 fields->errmsg = p;
1064 fields->errmsglen = len + 1;
1065 }
1066 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1067 return QF_OK;
1068}
1069
1070/*
1071 * Parse the match for rest of a single-line file message ('%r') pattern.
1072 * Return the matched value in "tail".
1073 */
1074 static int
1075qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1076{
1077 if (rmp->startp[midx] == NULL)
1078 return QF_FAIL;
1079 *tail = rmp->startp[midx];
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the pointer line ('%p') pattern in regmatch.
1085 * Return the matched value in "fields->col".
1086 */
1087 static int
1088qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 char_u *match_ptr;
1091
1092 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1093 return QF_FAIL;
1094 fields->col = 0;
1095 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1096 ++match_ptr)
1097 {
1098 ++fields->col;
1099 if (*match_ptr == TAB)
1100 {
1101 fields->col += 7;
1102 fields->col -= fields->col % 8;
1103 }
1104 }
1105 ++fields->col;
1106 fields->use_viscol = TRUE;
1107 return QF_OK;
1108}
1109
1110/*
1111 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1112 * Return the matched value in "fields->col".
1113 */
1114 static int
1115qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1116{
1117 if (rmp->startp[midx] == NULL)
1118 return QF_FAIL;
1119 fields->col = (int)atol((char *)rmp->startp[midx]);
1120 fields->use_viscol = TRUE;
1121 return QF_OK;
1122}
1123
1124/*
1125 * Parse the match for the search text ('%s') pattern in regmatch.
1126 * Return the matched value in "fields->pattern".
1127 */
1128 static int
1129qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1130{
1131 int len;
1132
1133 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1134 return QF_FAIL;
1135 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1136 if (len > CMDBUFFSIZE - 5)
1137 len = CMDBUFFSIZE - 5;
1138 STRCPY(fields->pattern, "^\\V");
1139 STRNCAT(fields->pattern, rmp->startp[midx], len);
1140 fields->pattern[len + 3] = '\\';
1141 fields->pattern[len + 4] = '$';
1142 fields->pattern[len + 5] = NUL;
1143 return QF_OK;
1144}
1145
1146/*
1147 * Parse the match for the module ('%o') pattern in regmatch.
1148 * Return the matched value in "fields->module".
1149 */
1150 static int
1151qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1152{
1153 int len;
1154
1155 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1156 return QF_FAIL;
1157 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1158 if (len > CMDBUFFSIZE)
1159 len = CMDBUFFSIZE;
1160 STRNCAT(fields->module, rmp->startp[midx], len);
1161 return QF_OK;
1162}
1163
1164/*
1165 * 'errorformat' format pattern parser functions.
1166 * The '%f' and '%r' formats are parsed differently from other formats.
1167 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001168 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001169 */
1170static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1171{
haya14busae023d492022-02-08 18:09:29 +00001172 NULL, // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001173 qf_parse_fmt_n,
1174 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001175 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001177 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 qf_parse_fmt_t,
1179 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001180 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 qf_parse_fmt_p,
1182 qf_parse_fmt_v,
1183 qf_parse_fmt_s,
1184 qf_parse_fmt_o
1185};
1186
1187/*
1188 * Parse the error format pattern matches in "regmatch" and set the values in
1189 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1190 * match. Returns QF_OK if all the matches are successfully parsed. On
1191 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001192 */
1193 static int
1194qf_parse_match(
1195 char_u *linebuf,
1196 int linelen,
1197 efm_T *fmt_ptr,
1198 regmatch_T *regmatch,
1199 qffields_T *fields,
1200 int qf_multiline,
1201 int qf_multiscan,
1202 char_u **tail)
1203{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001204 int idx = fmt_ptr->prefix;
1205 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001206 int midx;
1207 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001208
1209 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1210 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001211 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001212 fields->type = idx;
1213 else
1214 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001215
1216 // Extract error message data from matched line.
1217 // We check for an actual submatch, because "\[" and "\]" in
1218 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001219 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001220 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001221 status = QF_OK;
1222 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001223 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001224 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001225 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001227 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001228 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001229 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001230 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001231 }
haya14busae023d492022-02-08 18:09:29 +00001232 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001233 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001234 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001235 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001236
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001237 if (status != QF_OK)
1238 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001239 }
1240
1241 return QF_OK;
1242}
1243
1244/*
1245 * Parse an error line in 'linebuf' using a single error format string in
1246 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1247 * Returns QF_OK if the efm format matches completely and the fields are
1248 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1249 */
1250 static int
1251qf_parse_get_fields(
1252 char_u *linebuf,
1253 int linelen,
1254 efm_T *fmt_ptr,
1255 qffields_T *fields,
1256 int qf_multiline,
1257 int qf_multiscan,
1258 char_u **tail)
1259{
1260 regmatch_T regmatch;
1261 int status = QF_FAIL;
1262 int r;
1263
1264 if (qf_multiscan &&
1265 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1266 return QF_FAIL;
1267
1268 fields->namebuf[0] = NUL;
1269 fields->module[0] = NUL;
1270 fields->pattern[0] = NUL;
1271 if (!qf_multiscan)
1272 fields->errmsg[0] = NUL;
1273 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001274 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001275 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001276 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001277 fields->use_viscol = FALSE;
1278 fields->enr = -1;
1279 fields->type = 0;
1280 *tail = NULL;
1281
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001282 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001283 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001284 regmatch.regprog = fmt_ptr->prog;
1285 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1286 fmt_ptr->prog = regmatch.regprog;
1287 if (r)
1288 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1289 fields, qf_multiline, qf_multiscan, tail);
1290
1291 return status;
1292}
1293
1294/*
1295 * Parse directory error format prefixes (%D and %X).
1296 * Push and pop directories from the directory stack when scanning directory
1297 * names.
1298 */
1299 static int
1300qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1301{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001302 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001303 {
1304 if (*fields->namebuf == NUL)
1305 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001306 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001307 return QF_FAIL;
1308 }
1309 qfl->qf_directory =
1310 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1311 if (qfl->qf_directory == NULL)
1312 return QF_FAIL;
1313 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001314 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1316
1317 return QF_OK;
1318}
1319
1320/*
1321 * Parse global file name error format prefixes (%O, %P and %Q).
1322 */
1323 static int
1324qf_parse_file_pfx(
1325 int idx,
1326 qffields_T *fields,
1327 qf_list_T *qfl,
1328 char_u *tail)
1329{
1330 fields->valid = FALSE;
1331 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1332 {
1333 if (*fields->namebuf && idx == 'P')
1334 qfl->qf_currfile =
1335 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1336 else if (idx == 'Q')
1337 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1338 *fields->namebuf = NUL;
1339 if (tail && *tail)
1340 {
1341 STRMOVE(IObuff, skipwhite(tail));
1342 qfl->qf_multiscan = TRUE;
1343 return QF_MULTISCAN;
1344 }
1345 }
1346
1347 return QF_OK;
1348}
1349
1350/*
1351 * Parse a non-error line (a line which doesn't match any of the error
1352 * format in 'efm').
1353 */
1354 static int
1355qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1356{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001357 fields->namebuf[0] = NUL; // no match found, remove file name
1358 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001359 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001360
Bram Moolenaarf4140482020-02-15 23:06:45 +01001361 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001362}
1363
1364/*
1365 * Parse multi-line error format prefixes (%C and %Z)
1366 */
1367 static int
1368qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001369 int idx,
1370 qf_list_T *qfl,
1371 qffields_T *fields)
1372{
1373 char_u *ptr;
1374 int len;
1375
1376 if (!qfl->qf_multiignore)
1377 {
1378 qfline_T *qfprev = qfl->qf_last;
1379
1380 if (qfprev == NULL)
1381 return QF_FAIL;
1382 if (*fields->errmsg && !qfl->qf_multiignore)
1383 {
1384 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001385 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1386 aid_qf_multiline_pfx);
1387 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001388 return QF_FAIL;
1389 STRCPY(ptr, qfprev->qf_text);
1390 vim_free(qfprev->qf_text);
1391 qfprev->qf_text = ptr;
1392 *(ptr += len) = '\n';
1393 STRCPY(++ptr, fields->errmsg);
1394 }
1395 if (qfprev->qf_nr == -1)
1396 qfprev->qf_nr = fields->enr;
1397 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001398 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001399 qfprev->qf_type = fields->type;
1400
1401 if (!qfprev->qf_lnum)
1402 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001403 if (!qfprev->qf_end_lnum)
1404 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001405 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001406 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001407 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001408 qfprev->qf_viscol = fields->use_viscol;
1409 }
haya14busae023d492022-02-08 18:09:29 +00001410 if (!qfprev->qf_end_col)
1411 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001412 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001413 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001414 qfl->qf_directory,
1415 *fields->namebuf || qfl->qf_directory != NULL
1416 ? fields->namebuf
1417 : qfl->qf_currfile != NULL && fields->valid
1418 ? qfl->qf_currfile : 0);
1419 }
1420 if (idx == 'Z')
1421 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1422 line_breakcheck();
1423
1424 return QF_IGNORE_LINE;
1425}
1426
1427/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001428 * Parse a line and get the quickfix fields.
1429 * Return the QF_ status.
1430 */
1431 static int
1432qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001433 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 char_u *linebuf,
1435 int linelen,
1436 efm_T *fmt_first,
1437 qffields_T *fields)
1438{
1439 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001440 int idx = 0;
1441 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001442 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443
Bram Moolenaare333e792018-04-08 13:27:39 +02001444restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001445 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446 if (fmt_start == NULL)
1447 fmt_ptr = fmt_first;
1448 else
1449 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001450 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001451 fmt_ptr = fmt_start;
1452 fmt_start = NULL;
1453 }
1454
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001455 // Try to match each part of 'errorformat' until we find a complete
1456 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001457 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1459 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001461 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1462 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1463 if (status == QF_NOMEM)
1464 return status;
1465 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001468 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001469
1470 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1471 {
1472 if (fmt_ptr != NULL)
1473 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001474 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001475 status = qf_parse_dir_pfx(idx, fields, qfl);
1476 if (status != QF_OK)
1477 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001478 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001479
1480 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1481 if (status != QF_OK)
1482 return status;
1483
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001484 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001485 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001486 }
1487 else if (fmt_ptr != NULL)
1488 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001489 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001490 if (fmt_ptr->conthere)
1491 fmt_start = fmt_ptr;
1492
Bram Moolenaare9283662020-06-07 14:10:47 +02001493 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001494 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001495 qfl->qf_multiline = TRUE; // start of a multi-line message
1496 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001497 }
1498 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001499 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001500 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001501 if (status != QF_OK)
1502 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001503 }
1504 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001505 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001506 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1507 if (status == QF_MULTISCAN)
1508 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001509 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001510 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001511 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001512 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001513 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001514 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001515 return QF_IGNORE_LINE;
1516 }
1517 }
1518
1519 return QF_OK;
1520}
1521
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001522/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001523 * Returns TRUE if the specified quickfix/location stack is empty
1524 */
1525 static int
1526qf_stack_empty(qf_info_T *qi)
1527{
1528 return qi == NULL || qi->qf_listcount <= 0;
1529}
1530
1531/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001532 * Returns TRUE if the specified quickfix/location list is empty.
1533 */
1534 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001535qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001536{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001537 return qfl == NULL || qfl->qf_count <= 0;
1538}
1539
1540/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001541 * Returns TRUE if the specified quickfix/location list is not empty and
1542 * has valid entries.
1543 */
1544 static int
1545qf_list_has_valid_entries(qf_list_T *qfl)
1546{
1547 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1548}
1549
1550/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001551 * Return a pointer to a list in the specified quickfix stack
1552 */
1553 static qf_list_T *
1554qf_get_list(qf_info_T *qi, int idx)
1555{
1556 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001557}
1558
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001559/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001560 * Allocate the fields used for parsing lines and populating a quickfix list.
1561 */
1562 static int
1563qf_alloc_fields(qffields_T *pfields)
1564{
1565 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1566 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1567 pfields->errmsglen = CMDBUFFSIZE + 1;
1568 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1569 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1570 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1571 || pfields->pattern == NULL || pfields->module == NULL)
1572 return FAIL;
1573
1574 return OK;
1575}
1576
1577/*
1578 * Free the fields used for parsing lines and populating a quickfix list.
1579 */
1580 static void
1581qf_free_fields(qffields_T *pfields)
1582{
1583 vim_free(pfields->namebuf);
1584 vim_free(pfields->module);
1585 vim_free(pfields->errmsg);
1586 vim_free(pfields->pattern);
1587}
1588
1589/*
1590 * Setup the state information used for parsing lines and populating a
1591 * quickfix list.
1592 */
1593 static int
1594qf_setup_state(
1595 qfstate_T *pstate,
1596 char_u *enc,
1597 char_u *efile,
1598 typval_T *tv,
1599 buf_T *buf,
1600 linenr_T lnumfirst,
1601 linenr_T lnumlast)
1602{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001603 pstate->vc.vc_type = CONV_NONE;
1604 if (enc != NULL && *enc != NUL)
1605 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001606
1607 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1608 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001609 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001610 return FAIL;
1611 }
1612
1613 if (tv != NULL)
1614 {
1615 if (tv->v_type == VAR_STRING)
1616 pstate->p_str = tv->vval.v_string;
1617 else if (tv->v_type == VAR_LIST)
1618 pstate->p_li = tv->vval.v_list->lv_first;
1619 pstate->tv = tv;
1620 }
1621 pstate->buf = buf;
1622 pstate->buflnum = lnumfirst;
1623 pstate->lnumlast = lnumlast;
1624
1625 return OK;
1626}
1627
1628/*
1629 * Cleanup the state information used for parsing lines and populating a
1630 * quickfix list.
1631 */
1632 static void
1633qf_cleanup_state(qfstate_T *pstate)
1634{
1635 if (pstate->fd != NULL)
1636 fclose(pstate->fd);
1637
1638 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001639 if (pstate->vc.vc_type != CONV_NONE)
1640 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001641}
1642
1643/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001644 * Process the next line from a file/buffer/list/string and add it
1645 * to the quickfix list 'qfl'.
1646 */
1647 static int
1648qf_init_process_nextline(
1649 qf_list_T *qfl,
1650 efm_T *fmt_first,
1651 qfstate_T *state,
1652 qffields_T *fields)
1653{
1654 int status;
1655
1656 // Get the next line from a file/buffer/list/string
1657 status = qf_get_nextline(state);
1658 if (status != QF_OK)
1659 return status;
1660
1661 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1662 fmt_first, fields);
1663 if (status != QF_OK)
1664 return status;
1665
1666 return qf_add_entry(qfl,
1667 qfl->qf_directory,
1668 (*fields->namebuf || qfl->qf_directory != NULL)
1669 ? fields->namebuf
1670 : ((qfl->qf_currfile != NULL && fields->valid)
1671 ? qfl->qf_currfile : (char_u *)NULL),
1672 fields->module,
1673 0,
1674 fields->errmsg,
1675 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001676 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001677 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001678 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001679 fields->use_viscol,
1680 fields->pattern,
1681 fields->enr,
1682 fields->type,
1683 fields->valid);
1684}
1685
1686/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001687 * Read the errorfile "efile" into memory, line by line, building the error
1688 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001689 * Alternative: when "efile" is NULL read errors from buffer "buf".
1690 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001691 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001692 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1693 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001694 * Return -1 for error, number of errors for success.
1695 */
1696 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001697qf_init_ext(
1698 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001699 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001700 char_u *efile,
1701 buf_T *buf,
1702 typval_T *tv,
1703 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001704 int newlist, // TRUE: start a new error list
1705 linenr_T lnumfirst, // first line number to use
1706 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001707 char_u *qf_title,
1708 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001709{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001710 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001711 qfstate_T state;
1712 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001713 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001714 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001715 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001717 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001718 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001719 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001721 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001722 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001723
Bram Moolenaara80faa82020-04-12 19:37:17 +02001724 CLEAR_FIELD(state);
1725 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001726 if ((qf_alloc_fields(&fields) == FAIL) ||
1727 (qf_setup_state(&state, enc, efile, tv, buf,
1728 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 goto qf_init_end;
1730
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001731 if (newlist || qf_idx == qi->qf_listcount)
1732 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001733 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001734 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001735 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001736 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001738 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001739 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001740 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001741 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001742 qfl = qf_get_list(qi, qf_idx);
1743 if (!qf_list_empty(qfl))
1744 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001747 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001748 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001749 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 else
1751 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001753 // If the errorformat didn't change between calls, then reuse the
1754 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001755 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1756 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001757 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001758 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001759 free_efm_list(&fmt_first);
1760
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001761 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001762 fmt_first = parse_efm_option(efm);
1763 if (fmt_first != NULL)
1764 last_efm = vim_strsave(efm);
1765 }
1766
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001767 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001770 // got_int is reset here, because it was probably set when killing the
1771 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 got_int = FALSE;
1773
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001774 // Read the lines in the error file one by one.
1775 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001776 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001778 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001779 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001780 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001781 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001782 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001783 if (status == QF_FAIL)
1784 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 line_breakcheck();
1787 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001788 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001790 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001792 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001793 qfl->qf_ptr = qfl->qf_start;
1794 qfl->qf_index = 1;
1795 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
1797 else
1798 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001799 qfl->qf_nonevalid = FALSE;
1800 if (qfl->qf_ptr == NULL)
1801 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001803 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001804 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001805 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001807 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001809 if (!adding)
1810 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001811 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001812 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001813 qi->qf_listcount--;
1814 if (qi->qf_curlist > 0)
1815 --qi->qf_curlist;
1816 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001817qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001818 if (qf_idx == qi->qf_curlist)
1819 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001820 qf_cleanup_state(&state);
1821 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823 return retval;
1824}
1825
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001826/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001827 * Read the errorfile "efile" into memory, line by line, building the error
1828 * list. Set the error list's title to qf_title.
1829 * Return -1 for error, number of errors for success.
1830 */
1831 int
1832qf_init(win_T *wp,
1833 char_u *efile,
1834 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001835 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001836 char_u *qf_title,
1837 char_u *enc)
1838{
1839 qf_info_T *qi = &ql_info;
1840
1841 if (wp != NULL)
1842 {
1843 qi = ll_get_or_alloc_list(wp);
1844 if (qi == NULL)
1845 return FAIL;
1846 }
1847
1848 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1849 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1850}
1851
1852/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001853 * Set the title of the specified quickfix list. Frees the previous title.
1854 * Prepends ':' to the title.
1855 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001856 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001857qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001858{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001859 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001860
Bram Moolenaarfb604092014-07-23 15:55:00 +02001861 if (title != NULL)
1862 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001863 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001864
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001865 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001866 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001867 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001868 }
1869}
1870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001872 * The title of a quickfix/location list is set, by default, to the command
1873 * that created the quickfix list with the ":" prefix.
1874 * Create a quickfix list title string by prepending ":" to a user command.
1875 * Returns a pointer to a static buffer with the title.
1876 */
1877 static char_u *
1878qf_cmdtitle(char_u *cmd)
1879{
1880 static char_u qftitle_str[IOSIZE];
1881
1882 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1883 return qftitle_str;
1884}
1885
1886/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001887 * Return a pointer to the current list in the specified quickfix stack
1888 */
1889 static qf_list_T *
1890qf_get_curlist(qf_info_T *qi)
1891{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001892 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001893}
1894
1895/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001896 * Prepare for adding a new quickfix list. If the current list is in the
1897 * middle of the stack, then all the following lists are freed and then
1898 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 */
1900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001901qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
1903 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001904 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001906 // If the current entry is not the last entry, delete entries beyond
1907 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001908 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001909 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001910 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001912 // When the stack is full, remove to oldest entry
1913 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001914 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001916 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001918 qi->qf_lists[i - 1] = qi->qf_lists[i];
1919 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001922 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001923 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001924 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001925 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001926 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001927 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928}
1929
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001930/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001931 * Queue location list stack delete request.
1932 */
1933 static void
1934locstack_queue_delreq(qf_info_T *qi)
1935{
1936 qf_delq_T *q;
1937
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001938 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001939 if (q != NULL)
1940 {
1941 q->qi = qi;
1942 q->next = qf_delq_head;
1943 qf_delq_head = q;
1944 }
1945}
1946
1947/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001948 * Return the global quickfix stack window buffer number.
1949 */
1950 int
1951qf_stack_get_bufnr(void)
1952{
1953 return ql_info.qf_bufnr;
1954}
1955
1956/*
1957 * Wipe the quickfix window buffer (if present) for the specified
1958 * quickfix/location list.
1959 */
1960 static void
1961wipe_qf_buffer(qf_info_T *qi)
1962{
1963 buf_T *qfbuf;
1964
1965 if (qi->qf_bufnr != INVALID_QFBUFNR)
1966 {
1967 qfbuf = buflist_findnr(qi->qf_bufnr);
1968 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1969 {
1970 // If the quickfix buffer is not loaded in any window, then
1971 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001972 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001973 qi->qf_bufnr = INVALID_QFBUFNR;
1974 }
1975 }
1976}
1977
1978/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001979 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 */
1981 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001982ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001983{
1984 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001985 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001986
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001987 qi = *pqi;
1988 if (qi == NULL)
1989 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001990 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001991
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001992 // If the location list is still in use, then queue the delete request
1993 // to be processed later.
1994 if (quickfix_busy > 0)
1995 {
1996 locstack_queue_delreq(qi);
1997 return;
1998 }
1999
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002000 qi->qf_refcount--;
2001 if (qi->qf_refcount < 1)
2002 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002003 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002004 // If the quickfix window buffer is loaded, then wipe it
2005 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002006
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002007 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002008 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002009 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002010 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002011}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002012
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002013/*
2014 * Free all the quickfix/location lists in the stack.
2015 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002016 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002017qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002018{
2019 int i;
2020 qf_info_T *qi = &ql_info;
2021
2022 if (wp != NULL)
2023 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002024 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025 ll_free_all(&wp->w_llist);
2026 ll_free_all(&wp->w_llist_ref);
2027 }
2028 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002029 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002031 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002032}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002033
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002035 * Delay freeing of location list stacks when the quickfix code is running.
2036 * Used to avoid problems with autocmds freeing location list stacks when the
2037 * quickfix code is still referencing the stack.
2038 * Must always call decr_quickfix_busy() exactly once after this.
2039 */
2040 static void
2041incr_quickfix_busy(void)
2042{
2043 quickfix_busy++;
2044}
2045
2046/*
2047 * Safe to free location list stacks. Process any delayed delete requests.
2048 */
2049 static void
2050decr_quickfix_busy(void)
2051{
2052 if (--quickfix_busy == 0)
2053 {
2054 // No longer referencing the location lists. Process all the pending
2055 // delete requests.
2056 while (qf_delq_head != NULL)
2057 {
2058 qf_delq_T *q = qf_delq_head;
2059
2060 qf_delq_head = q->next;
2061 ll_free_all(&q->qi);
2062 vim_free(q);
2063 }
2064 }
2065#ifdef ABORT_ON_INTERNAL_ERROR
2066 if (quickfix_busy < 0)
2067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002068 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002069 abort();
2070 }
2071#endif
2072}
2073
2074#if defined(EXITFREE) || defined(PROTO)
2075 void
2076check_quickfix_busy(void)
2077{
2078 if (quickfix_busy != 0)
2079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002081# ifdef ABORT_ON_INTERNAL_ERROR
2082 abort();
2083# endif
2084 }
2085}
2086#endif
2087
2088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002090 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 */
2092 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002093qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002094 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002095 char_u *dir, // optional directory name
2096 char_u *fname, // file name or NULL
2097 char_u *module, // module name or NULL
2098 int bufnum, // buffer number or zero
2099 char_u *mesg, // message
2100 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002101 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002102 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002103 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002104 int vis_col, // using visual column
2105 char_u *pattern, // search pattern
2106 int nr, // error number
2107 int type, // type character
2108 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002110 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002111 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002113 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002114 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002115 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002116 {
2117 buf_T *buf = buflist_findnr(bufnum);
2118
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002119 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002120 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002121 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002122 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002123 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002124 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002125 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2127 {
2128 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002129 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002132 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002134 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002135 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002136 if (pattern == NULL || *pattern == NUL)
2137 qfp->qf_pattern = NULL;
2138 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2139 {
2140 vim_free(qfp->qf_text);
2141 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002142 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002143 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002144 if (module == NULL || *module == NUL)
2145 qfp->qf_module = NULL;
2146 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2147 {
2148 vim_free(qfp->qf_text);
2149 vim_free(qfp->qf_pattern);
2150 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002151 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002154 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 type = 0;
2156 qfp->qf_type = type;
2157 qfp->qf_valid = valid;
2158
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002159 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002160 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002162 qfl->qf_start = qfp;
2163 qfl->qf_ptr = qfp;
2164 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002165 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167 else
2168 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002169 qfp->qf_prev = *lastp;
2170 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002172 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002174 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002175 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002176 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002178 qfl->qf_index = qfl->qf_count;
2179 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
2181
Bram Moolenaar95946f12019-03-31 15:31:59 +02002182 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183}
2184
2185/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002186 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002187 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002188 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002189qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002191 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002192
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002193 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002194 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002195 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002196 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002197 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002198 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002199 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002200 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002201}
2202
2203/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002204 * Return the location list stack for window 'wp'.
2205 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002206 */
2207 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002208ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002209{
2210 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002211 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002212 return wp->w_llist_ref;
2213
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002214 // For a non-location list window, w_llist_ref should not point to a
2215 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002216 ll_free_all(&wp->w_llist_ref);
2217
2218 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002219 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220 return wp->w_llist;
2221}
2222
2223/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002224 * Get the quickfix/location list stack to use for the specified Ex command.
2225 * For a location list command, returns the stack for the current window. If
2226 * the location list is not found, then returns NULL and prints an error
2227 * message if 'print_emsg' is TRUE.
2228 */
2229 static qf_info_T *
2230qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2231{
2232 qf_info_T *qi = &ql_info;
2233
2234 if (is_loclist_cmd(eap->cmdidx))
2235 {
2236 qi = GET_LOC_LIST(curwin);
2237 if (qi == NULL)
2238 {
2239 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002240 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002241 return NULL;
2242 }
2243 }
2244
2245 return qi;
2246}
2247
2248/*
2249 * Get the quickfix/location list stack to use for the specified Ex command.
2250 * For a location list command, returns the stack for the current window.
2251 * If the location list is not present, then allocates a new one.
2252 * Returns NULL if the allocation fails. For a location list command, sets
2253 * 'pwinp' to curwin.
2254 */
2255 static qf_info_T *
2256qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2257{
2258 qf_info_T *qi = &ql_info;
2259
2260 if (is_loclist_cmd(eap->cmdidx))
2261 {
2262 qi = ll_get_or_alloc_list(curwin);
2263 if (qi == NULL)
2264 return NULL;
2265 *pwinp = curwin;
2266 }
2267
2268 return qi;
2269}
2270
2271/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002272 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2273 */
2274 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002275copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002276{
2277 int i;
2278 qfline_T *from_qfp;
2279 qfline_T *prevp;
2280
2281 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002282 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002283 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002284 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002285 NULL,
2286 NULL,
2287 from_qfp->qf_module,
2288 0,
2289 from_qfp->qf_text,
2290 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002291 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002292 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002293 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002294 from_qfp->qf_viscol,
2295 from_qfp->qf_pattern,
2296 from_qfp->qf_nr,
2297 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002298 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002299 return FAIL;
2300
2301 // qf_add_entry() will not set the qf_num field, as the
2302 // directory and file names are not supplied. So the qf_fnum
2303 // field is copied here.
2304 prevp = to_qfl->qf_last;
2305 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2306 prevp->qf_type = from_qfp->qf_type; // error type
2307 if (from_qfl->qf_ptr == from_qfp)
2308 to_qfl->qf_ptr = prevp; // current location
2309 }
2310
2311 return OK;
2312}
2313
2314/*
2315 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2316 */
2317 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002318copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002319{
2320 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002321 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002322 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2323 to_qfl->qf_count = 0;
2324 to_qfl->qf_index = 0;
2325 to_qfl->qf_start = NULL;
2326 to_qfl->qf_last = NULL;
2327 to_qfl->qf_ptr = NULL;
2328 if (from_qfl->qf_title != NULL)
2329 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2330 else
2331 to_qfl->qf_title = NULL;
2332 if (from_qfl->qf_ctx != NULL)
2333 {
2334 to_qfl->qf_ctx = alloc_tv();
2335 if (to_qfl->qf_ctx != NULL)
2336 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2337 }
2338 else
2339 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002340 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2341 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002342 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002343 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002344
2345 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002346 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002347 return FAIL;
2348
2349 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2350
2351 // Assign a new ID for the location list
2352 to_qfl->qf_id = ++last_qf_id;
2353 to_qfl->qf_changedtick = 0L;
2354
2355 // When no valid entries are present in the list, qf_ptr points to
2356 // the first item in the list
2357 if (to_qfl->qf_nonevalid)
2358 {
2359 to_qfl->qf_ptr = to_qfl->qf_start;
2360 to_qfl->qf_index = 1;
2361 }
2362
2363 return OK;
2364}
2365
2366/*
2367 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002368 */
2369 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002370copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002371{
2372 qf_info_T *qi;
2373 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002374
Bram Moolenaar09037502018-09-25 22:08:14 +02002375 // When copying from a location list window, copy the referenced
2376 // location list. For other windows, copy the location list for
2377 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002378 if (IS_LL_WINDOW(from))
2379 qi = from->w_llist_ref;
2380 else
2381 qi = from->w_llist;
2382
Bram Moolenaar09037502018-09-25 22:08:14 +02002383 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002384 return;
2385
Bram Moolenaar09037502018-09-25 22:08:14 +02002386 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002387 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002388 return;
2389
2390 to->w_llist->qf_listcount = qi->qf_listcount;
2391
Bram Moolenaar09037502018-09-25 22:08:14 +02002392 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002393 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002394 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002395 to->w_llist->qf_curlist = idx;
2396
Bram Moolenaar0398e002019-03-21 21:12:49 +01002397 if (copy_loclist(qf_get_list(qi, idx),
2398 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002399 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002400 qf_free_all(to);
2401 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002402 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002403 }
2404
Bram Moolenaar09037502018-09-25 22:08:14 +02002405 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002406}
2407
2408/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002409 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002410 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 */
2412 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002413qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
Bram Moolenaar82404332016-07-10 17:00:38 +02002415 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002416 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002417 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002418
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002419 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
Bram Moolenaare60acc12011-05-10 16:41:25 +02002422#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002423 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002424#endif
2425#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002426 if (directory != NULL)
2427 slash_adjust(directory);
2428 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002429#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002430 if (directory != NULL && !vim_isAbsName(fname)
2431 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2432 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002433 // Here we check if the file really exists.
2434 // This should normally be true, but if make works without
2435 // "leaving directory"-messages we might have missed a
2436 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002437 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002440 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002441 if (directory)
2442 ptr = concat_fnames(directory, fname, TRUE);
2443 else
2444 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002446 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002447 bufname = ptr;
2448 }
2449 else
2450 bufname = fname;
2451
2452 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002453 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002454 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002455 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002456 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002458 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002459 {
2460 vim_free(qf_last_bufname);
2461 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2462 if (bufname == ptr)
2463 qf_last_bufname = bufname;
2464 else
2465 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002466 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002467 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002468 if (buf == NULL)
2469 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002470
Bram Moolenaarc1542742016-07-20 21:44:37 +02002471 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002472 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002473 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474}
2475
2476/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002477 * Push dirbuf onto the directory stack and return pointer to actual dir or
2478 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 */
2480 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002481qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 struct dir_stack_T *ds_new;
2484 struct dir_stack_T *ds_ptr;
2485
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002486 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002487 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (ds_new == NULL)
2489 return NULL;
2490
2491 ds_new->next = *stackptr;
2492 *stackptr = ds_new;
2493
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002494 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (vim_isAbsName(dirbuf)
2496 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002497 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 (*stackptr)->dirname = vim_strsave(dirbuf);
2499 else
2500 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002501 // Okay we don't have an absolute path.
2502 // dirbuf must be a subdir of one of the directories on the stack.
2503 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 ds_new = (*stackptr)->next;
2505 (*stackptr)->dirname = NULL;
2506 while (ds_new)
2507 {
2508 vim_free((*stackptr)->dirname);
2509 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2510 TRUE);
2511 if (mch_isdir((*stackptr)->dirname) == TRUE)
2512 break;
2513
2514 ds_new = ds_new->next;
2515 }
2516
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002517 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 while ((*stackptr)->next != ds_new)
2519 {
2520 ds_ptr = (*stackptr)->next;
2521 (*stackptr)->next = (*stackptr)->next->next;
2522 vim_free(ds_ptr->dirname);
2523 vim_free(ds_ptr);
2524 }
2525
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002526 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (ds_new == NULL)
2528 {
2529 vim_free((*stackptr)->dirname);
2530 (*stackptr)->dirname = vim_strsave(dirbuf);
2531 }
2532 }
2533
2534 if ((*stackptr)->dirname != NULL)
2535 return (*stackptr)->dirname;
2536 else
2537 {
2538 ds_ptr = *stackptr;
2539 *stackptr = (*stackptr)->next;
2540 vim_free(ds_ptr);
2541 return NULL;
2542 }
2543}
2544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545/*
2546 * pop dirbuf from the directory stack and return previous directory or NULL if
2547 * stack is empty
2548 */
2549 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002550qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 struct dir_stack_T *ds_ptr;
2553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002554 // TODO: Should we check if dirbuf is the directory on top of the stack?
2555 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002557 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (*stackptr != NULL)
2559 {
2560 ds_ptr = *stackptr;
2561 *stackptr = (*stackptr)->next;
2562 vim_free(ds_ptr->dirname);
2563 vim_free(ds_ptr);
2564 }
2565
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002566 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 return *stackptr ? (*stackptr)->dirname : NULL;
2568}
2569
2570/*
2571 * clean up directory stack
2572 */
2573 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002574qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 struct dir_stack_T *ds_ptr;
2577
2578 while ((ds_ptr = *stackptr) != NULL)
2579 {
2580 *stackptr = (*stackptr)->next;
2581 vim_free(ds_ptr->dirname);
2582 vim_free(ds_ptr);
2583 }
2584}
2585
2586/*
2587 * Check in which directory of the directory stack the given file can be
2588 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002589 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 * Cleans up intermediate directory entries.
2591 *
2592 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002593 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * ./
2595 * ./aa
2596 * ./aa/bb
2597 * ./bb
2598 * ./bb/x.c
2599 * and make says:
2600 * making all in aa
2601 * making all in bb
2602 * x.c:9: Error
2603 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2604 * qf_guess_filepath will return NULL.
2605 */
2606 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002607qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 struct dir_stack_T *ds_ptr;
2610 struct dir_stack_T *ds_tmp;
2611 char_u *fullname;
2612
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002613 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002614 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 return NULL;
2616
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002617 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 fullname = NULL;
2619 while (ds_ptr)
2620 {
2621 vim_free(fullname);
2622 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2623
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002624 // If concat_fnames failed, just go on. The worst thing that can happen
2625 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2627 break;
2628
2629 ds_ptr = ds_ptr->next;
2630 }
2631
2632 vim_free(fullname);
2633
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002634 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002635 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002637 ds_tmp = qfl->qf_dir_stack->next;
2638 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 vim_free(ds_tmp->dirname);
2640 vim_free(ds_tmp);
2641 }
2642
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002643 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644}
2645
2646/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002647 * Returns TRUE if a quickfix/location list with the given identifier exists.
2648 */
2649 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002650qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002651{
2652 qf_info_T *qi = &ql_info;
2653 int i;
2654
2655 if (wp != NULL)
2656 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002657 if (!win_valid(wp))
2658 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002659 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002660 if (qi == NULL)
2661 return FALSE;
2662 }
2663
2664 for (i = 0; i < qi->qf_listcount; ++i)
2665 if (qi->qf_lists[i].qf_id == qf_id)
2666 return TRUE;
2667
2668 return FALSE;
2669}
2670
2671/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002672 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002673 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002674 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002675 * Similar to location list.
2676 */
2677 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002678is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002679{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002680 qfline_T *qfp;
2681 int i;
2682
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002683 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002684 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2685 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002686 break;
2687
Bram Moolenaar95946f12019-03-31 15:31:59 +02002688 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002689 return FALSE;
2690
2691 return TRUE;
2692}
2693
2694/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002695 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002696 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002697 */
2698 static qfline_T *
2699get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002700 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002701 qfline_T *qf_ptr,
2702 int *qf_index,
2703 int dir)
2704{
2705 int idx;
2706 int old_qf_fnum;
2707
2708 idx = *qf_index;
2709 old_qf_fnum = qf_ptr->qf_fnum;
2710
2711 do
2712 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002713 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002714 return NULL;
2715 ++idx;
2716 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002717 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002718 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2719
2720 *qf_index = idx;
2721 return qf_ptr;
2722}
2723
2724/*
2725 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002726 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002727 */
2728 static qfline_T *
2729get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002730 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731 qfline_T *qf_ptr,
2732 int *qf_index,
2733 int dir)
2734{
2735 int idx;
2736 int old_qf_fnum;
2737
2738 idx = *qf_index;
2739 old_qf_fnum = qf_ptr->qf_fnum;
2740
2741 do
2742 {
2743 if (idx == 1 || qf_ptr->qf_prev == NULL)
2744 return NULL;
2745 --idx;
2746 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002747 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2749
2750 *qf_index = idx;
2751 return qf_ptr;
2752}
2753
2754/*
2755 * Get the n'th (errornr) previous/next valid entry from the current entry in
2756 * the quickfix list.
2757 * dir == FORWARD or FORWARD_FILE: next valid entry
2758 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2759 */
2760 static qfline_T *
2761get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002762 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002763 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002764 int dir,
2765 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002766{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002767 qfline_T *qf_ptr = qfl->qf_ptr;
2768 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002769 qfline_T *prev_qf_ptr;
2770 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002771 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002772
2773 while (errornr--)
2774 {
2775 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002776 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002777
2778 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002779 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002780 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002781 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002782 if (qf_ptr == NULL)
2783 {
2784 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002785 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002786 if (err != NULL)
2787 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002788 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002789 return NULL;
2790 }
2791 break;
2792 }
2793
2794 err = NULL;
2795 }
2796
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002797 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002798 return qf_ptr;
2799}
2800
2801/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002802 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2803 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002804 */
2805 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002806get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002807{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002808 qfline_T *qf_ptr = qfl->qf_ptr;
2809 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002810
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002811 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002812 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2813 {
2814 --qf_idx;
2815 qf_ptr = qf_ptr->qf_prev;
2816 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002817 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002818 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2819 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002820 {
2821 ++qf_idx;
2822 qf_ptr = qf_ptr->qf_next;
2823 }
2824
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002825 *new_qfidx = qf_idx;
2826 return qf_ptr;
2827}
2828
2829/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002830 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002831 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2832 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2833 * Returns a pointer to the entry and the index of the new entry is stored in
2834 * 'new_qfidx'.
2835 */
2836 static qfline_T *
2837qf_get_entry(
2838 qf_list_T *qfl,
2839 int errornr,
2840 int dir,
2841 int *new_qfidx)
2842{
2843 qfline_T *qf_ptr = qfl->qf_ptr;
2844 int qfidx = qfl->qf_index;
2845
2846 if (dir != 0) // next/prev valid entry
2847 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2848 else if (errornr != 0) // go to specified number
2849 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2850
2851 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002852 return qf_ptr;
2853}
2854
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002855/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002856 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002857 */
2858 static win_T *
2859qf_find_help_win(void)
2860{
2861 win_T *wp;
2862
2863 FOR_ALL_WINDOWS(wp)
2864 if (bt_help(wp->w_buffer))
2865 return wp;
2866
2867 return NULL;
2868}
2869
2870/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002871 * Set the location list for the specified window to 'qi'.
2872 */
2873 static void
2874win_set_loclist(win_T *wp, qf_info_T *qi)
2875{
2876 wp->w_llist = qi;
2877 qi->qf_refcount++;
2878}
2879
2880/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002881 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2882 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002883 */
2884 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002885jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002886{
2887 win_T *wp;
2888 int flags;
2889
Bram Moolenaare1004402020-10-24 20:49:43 +02002890 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002891 wp = NULL;
2892 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002893 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002894 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2895 win_enter(wp, TRUE);
2896 else
2897 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002898 // Split off help window; put it at far top if no position
2899 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002900 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002901 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002902 && curwin->w_width < 80)
2903 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002904 // If the user asks to open a new window, then copy the location list.
2905 // Otherwise, don't copy the location list.
2906 if (IS_LL_STACK(qi) && !newwin)
2907 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002908
2909 if (win_split(0, flags) == FAIL)
2910 return FAIL;
2911
2912 *opened_window = TRUE;
2913
2914 if (curwin->w_height < p_hh)
2915 win_setheight((int)p_hh);
2916
Bram Moolenaarb2443732018-11-11 22:50:27 +01002917 // When using location list, the new window should use the supplied
2918 // location list. If the user asks to open a new window, then the new
2919 // window will get a copy of the location list.
2920 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002921 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002922 }
2923
2924 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002925 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002926
2927 return OK;
2928}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002929
2930/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002931 * Find a non-quickfix window using the given location list stack in the
2932 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002933 * Returns NULL if a matching window is not found.
2934 */
2935 static win_T *
2936qf_find_win_with_loclist(qf_info_T *ll)
2937{
2938 win_T *wp;
2939
2940 FOR_ALL_WINDOWS(wp)
2941 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2942 return wp;
2943
2944 return NULL;
2945}
2946
2947/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002948 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002949 */
2950 static win_T *
2951qf_find_win_with_normal_buf(void)
2952{
2953 win_T *wp;
2954
2955 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002956 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 return wp;
2958
2959 return NULL;
2960}
2961
2962/*
2963 * Go to a window in any tabpage containing the specified file. Returns TRUE
2964 * if successfully jumped to the window. Otherwise returns FALSE.
2965 */
2966 static int
2967qf_goto_tabwin_with_file(int fnum)
2968{
2969 tabpage_T *tp;
2970 win_T *wp;
2971
2972 FOR_ALL_TAB_WINDOWS(tp, wp)
2973 if (wp->w_buffer->b_fnum == fnum)
2974 {
2975 goto_tabpage_win(tp, wp);
2976 return TRUE;
2977 }
2978
2979 return FALSE;
2980}
2981
2982/*
2983 * Create a new window to show a file above the quickfix window. Called when
2984 * only the quickfix window is present.
2985 */
2986 static int
2987qf_open_new_file_win(qf_info_T *ll_ref)
2988{
2989 int flags;
2990
2991 flags = WSP_ABOVE;
2992 if (ll_ref != NULL)
2993 flags |= WSP_NEWLOC;
2994 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002995 return FAIL; // not enough room for window
2996 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002997 swb_flags = 0;
2998 RESET_BINDING(curwin);
2999 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003000 // The new window should use the location list from the
3001 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003002 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003003 return OK;
3004}
3005
3006/*
3007 * Go to a window that shows the right buffer. If the window is not found, go
3008 * to the window just above the location list window. This is used for opening
3009 * a file from a location window and not from a quickfix window. If some usable
3010 * window is previously found, then it is supplied in 'use_win'.
3011 */
3012 static void
3013qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3014{
3015 win_T *win = use_win;
3016
3017 if (win == NULL)
3018 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003019 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003020 FOR_ALL_WINDOWS(win)
3021 if (win->w_buffer->b_fnum == qf_fnum)
3022 break;
3023 if (win == NULL)
3024 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003025 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003026 win = curwin;
3027 do
3028 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003029 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003030 break;
3031 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003032 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003033 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003034 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003035 } while (win != curwin);
3036 }
3037 }
3038 win_goto(win);
3039
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003040 // If the location list for the window is not set, then set it
3041 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003042 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003043 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003044}
3045
3046/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003047 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3048 * not found, then go to the window just above the quickfix window. This is
3049 * used for opening a file from a quickfix window and not from a location
3050 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003051 */
3052 static void
3053qf_goto_win_with_qfl_file(int qf_fnum)
3054{
3055 win_T *win;
3056 win_T *altwin;
3057
3058 win = curwin;
3059 altwin = NULL;
3060 for (;;)
3061 {
3062 if (win->w_buffer->b_fnum == qf_fnum)
3063 break;
3064 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003065 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003066 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003067 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003068
3069 if (IS_QF_WINDOW(win))
3070 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003071 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003072 // window, unless 'switchbuf' contains 'uselast': in this case we
3073 // try to jump to the previously used window first.
3074 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3075 win = prevwin;
3076 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003077 win = altwin;
3078 else if (curwin->w_prev != NULL)
3079 win = curwin->w_prev;
3080 else
3081 win = curwin->w_next;
3082 break;
3083 }
3084
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003085 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003086 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003087 altwin = win;
3088 }
3089
3090 win_goto(win);
3091}
3092
3093/*
3094 * Find a suitable window for opening a file (qf_fnum) from the
3095 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003096 * window, jump to it. Otherwise open a new window to display the file. If
3097 * 'newwin' is TRUE, then always open a new window. This is called from either
3098 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003099 */
3100 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003101qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003102{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003103 win_T *usable_wp = NULL;
3104 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003105 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003106
Bram Moolenaarb2443732018-11-11 22:50:27 +01003107 // If opening a new window, then don't use the location list referred by
3108 // the current window. Otherwise two windows will refer to the same
3109 // location list.
3110 if (!newwin)
3111 ll_ref = curwin->w_llist_ref;
3112
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003113 if (ll_ref != NULL)
3114 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003115 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003116 usable_wp = qf_find_win_with_loclist(ll_ref);
3117 if (usable_wp != NULL)
3118 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003119 }
3120
3121 if (!usable_win)
3122 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003123 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003124 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003125 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003126 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003127 }
3128
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003129 // If no usable window is found and 'switchbuf' contains "usetab"
3130 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003131 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003132 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003133
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003134 // If there is only one window and it is the quickfix window, create a
3135 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003136 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003137 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003138 if (qf_open_new_file_win(ll_ref) != OK)
3139 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003140 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003141 }
3142 else
3143 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003144 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003145 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003146 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003147 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003148 }
3149
3150 return OK;
3151}
3152
3153/*
3154 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003155 * Returns OK if successfully edited the file, FAIL on failing to open the
3156 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3157 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003158 */
3159 static int
3160qf_jump_edit_buffer(
3161 qf_info_T *qi,
3162 qfline_T *qf_ptr,
3163 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003164 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003165 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003166{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003167 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003168 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003169 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003170 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003171 int old_qf_curlist = qi->qf_curlist;
3172 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003173
3174 if (qf_ptr->qf_type == 1)
3175 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003176 // Open help file (do_ecmd() will set b_help flag, readfile() will
3177 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003178 if (!can_abandon(curbuf, forceit))
3179 {
3180 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003181 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003183
3184 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3185 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003186 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003187 }
3188 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003189 retval = buflist_getfile(qf_ptr->qf_fnum,
3190 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003191
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003192 // If a location list, check whether the associated window is still
3193 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003194 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003195 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003196 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003197
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003198 if (wp == NULL && curwin->w_llist != qi)
3199 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003200 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003201 *opened_window = FALSE;
3202 return NOTDONE;
3203 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003204 }
3205
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003206 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003207 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003208 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003209 return NOTDONE;
3210 }
3211
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003212 // Check if the list was changed. The pointers may happen to be identical,
3213 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003214 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003215 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003216 || !is_qf_entry_present(qfl, qf_ptr))
3217 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003218 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003219 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003220 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003221 emsg(_(e_current_location_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003222 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003223 }
3224
3225 return retval;
3226}
3227
3228/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003229 * Go to the error line in the current file using either line/column number or
3230 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003231 */
3232 static void
3233qf_jump_goto_line(
3234 linenr_T qf_lnum,
3235 int qf_col,
3236 char_u qf_viscol,
3237 char_u *qf_pattern)
3238{
3239 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003240
3241 if (qf_pattern == NULL)
3242 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003243 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003244 i = qf_lnum;
3245 if (i > 0)
3246 {
3247 if (i > curbuf->b_ml.ml_line_count)
3248 i = curbuf->b_ml.ml_line_count;
3249 curwin->w_cursor.lnum = i;
3250 }
3251 if (qf_col > 0)
3252 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003253 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003254 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003255 coladvance(qf_col - 1);
3256 else
3257 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003258 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003259 check_cursor();
3260 }
3261 else
3262 beginline(BL_WHITE | BL_FIX);
3263 }
3264 else
3265 {
3266 pos_T save_cursor;
3267
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003268 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003269 save_cursor = curwin->w_cursor;
3270 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003271 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003272 curwin->w_cursor = save_cursor;
3273 }
3274}
3275
3276/*
3277 * Display quickfix list index and size message
3278 */
3279 static void
3280qf_jump_print_msg(
3281 qf_info_T *qi,
3282 int qf_index,
3283 qfline_T *qf_ptr,
3284 buf_T *old_curbuf,
3285 linenr_T old_lnum)
3286{
3287 linenr_T i;
3288 int len;
3289
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003290 // Update the screen before showing the message, unless the screen
3291 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003292 if (!msg_scrolled)
3293 update_topline_redraw();
3294 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003295 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003296 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3297 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003298 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003299 len = (int)STRLEN(IObuff);
3300 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3301
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003302 // Output the message. Overwrite to avoid scrolling when the 'O'
3303 // flag is present in 'shortmess'; But when not jumping, print the
3304 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003305 i = msg_scroll;
3306 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3307 msg_scroll = TRUE;
3308 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3309 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003310 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003311 msg_scroll = i;
3312}
3313
3314/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003315 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003316 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3317 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003318 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3319 * able to jump/open a window. Returns NOTDONE if a file is not associated
3320 * with the entry.
3321 */
3322 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003323qf_jump_open_window(
3324 qf_info_T *qi,
3325 qfline_T *qf_ptr,
3326 int newwin,
3327 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003328{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003329 qf_list_T *qfl = qf_get_curlist(qi);
3330 int old_changedtick = qfl->qf_changedtick;
3331 int old_qf_curlist = qi->qf_curlist;
3332 qfltype_T qfl_type = qfl->qfl_type;
3333
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003334 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003335 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3336 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003337 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003338 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003339 if (old_qf_curlist != qi->qf_curlist
3340 || old_changedtick != qfl->qf_changedtick
3341 || !is_qf_entry_present(qfl, qf_ptr))
3342 {
3343 if (qfl_type == QFLT_QUICKFIX)
3344 emsg(_(e_current_quickfix_list_was_changed));
3345 else
3346 emsg(_(e_current_location_list_was_changed));
3347 return FAIL;
3348 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003349
3350 // If currently in the quickfix window, find another window to show the
3351 // file in.
3352 if (bt_quickfix(curbuf) && !*opened_window)
3353 {
3354 // If there is no file specified, we don't know where to go.
3355 // But do advance, otherwise ":cn" gets stuck.
3356 if (qf_ptr->qf_fnum == 0)
3357 return NOTDONE;
3358
Bram Moolenaarb2443732018-11-11 22:50:27 +01003359 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3360 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003361 return FAIL;
3362 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003363 if (old_qf_curlist != qi->qf_curlist
3364 || old_changedtick != qfl->qf_changedtick
3365 || !is_qf_entry_present(qfl, qf_ptr))
3366 {
3367 if (qfl_type == QFLT_QUICKFIX)
3368 emsg(_(e_current_quickfix_list_was_changed));
3369 else
3370 emsg(_(e_current_location_list_was_changed));
3371 return FAIL;
3372 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003373
3374 return OK;
3375}
3376
3377/*
3378 * Edit a selected file from the quickfix/location list and jump to a
3379 * particular line/column, adjust the folds and display a message about the
3380 * jump.
3381 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3382 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3383 * the file.
3384 */
3385 static int
3386qf_jump_to_buffer(
3387 qf_info_T *qi,
3388 int qf_index,
3389 qfline_T *qf_ptr,
3390 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003391 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003392 int *opened_window,
3393 int openfold,
3394 int print_message)
3395{
3396 buf_T *old_curbuf;
3397 linenr_T old_lnum;
3398 int retval = OK;
3399
3400 // If there is a file name, read the wanted file if needed, and check
3401 // autowrite etc.
3402 old_curbuf = curbuf;
3403 old_lnum = curwin->w_cursor.lnum;
3404
3405 if (qf_ptr->qf_fnum != 0)
3406 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003407 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003408 opened_window);
3409 if (retval != OK)
3410 return retval;
3411 }
3412
3413 // When not switched to another buffer, still need to set pc mark
3414 if (curbuf == old_curbuf)
3415 setpcmark();
3416
3417 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3418 qf_ptr->qf_pattern);
3419
3420#ifdef FEAT_FOLDING
3421 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3422 foldOpenCursor();
3423#endif
3424 if (print_message)
3425 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3426
3427 return retval;
3428}
3429
3430/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003431 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003434qf_jump(qf_info_T *qi,
3435 int dir,
3436 int errornr,
3437 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003439 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3440}
3441
3442/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003443 * Jump to a quickfix line.
3444 * If dir == 0 go to entry "errornr".
3445 * If dir == FORWARD go "errornr" valid entries forward.
3446 * If dir == BACKWARD go "errornr" valid entries backward.
3447 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3448 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3449 * else if "errornr" is zero, redisplay the same line
3450 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003451 * If 'newwin' is TRUE, then open the file in a new window.
3452 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003453 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003454qf_jump_newwin(qf_info_T *qi,
3455 int dir,
3456 int errornr,
3457 int forceit,
3458 int newwin)
3459{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003460 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003461 qfline_T *qf_ptr;
3462 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003465 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003466 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003467 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003470 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003471 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003473 if (qi == NULL)
3474 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003475
Bram Moolenaar0398e002019-03-21 21:12:49 +01003476 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003478 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 return;
3480 }
3481
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003482 incr_quickfix_busy();
3483
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003484 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003485
3486 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003488 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003490
3491 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3492 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003494 qf_ptr = old_qf_ptr;
3495 qf_index = old_qf_index;
3496 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003499 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003500 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003501 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003502 // No need to print the error message if it's visible in the error
3503 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 print_message = FALSE;
3505
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003506 prev_winid = curwin->w_id;
3507
Bram Moolenaarb2443732018-11-11 22:50:27 +01003508 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003509 if (retval == FAIL)
3510 goto failed;
3511 if (retval == NOTDONE)
3512 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003513
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003514 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003515 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003516 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003518 // Quickfix/location list is freed by an autocmd
3519 qi = NULL;
3520 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003523 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003526 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003527 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003529 // Couldn't open file, so put index back where it was. This could
3530 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 qf_ptr = old_qf_ptr;
3533 qf_index = old_qf_index;
3534 }
3535 }
3536theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003537 if (qi != NULL)
3538 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003539 qfl->qf_ptr = qf_ptr;
3540 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003541 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003542 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003544 // Restore old 'switchbuf' value, but not when an autocommand or
3545 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003546 p_swb = old_swb;
3547 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003549 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550}
3551
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003552// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003553static int qfFileAttr;
3554static int qfSepAttr;
3555static int qfLineAttr;
3556
3557/*
3558 * Display information about a single entry from the quickfix/location list.
3559 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003560 * 'cursel' will be set to TRUE for the currently selected entry in the
3561 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003562 */
3563 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003564qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003565{
3566 char_u *fname;
3567 buf_T *buf;
3568 int filter_entry;
3569
3570 fname = NULL;
3571 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3572 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3573 (char *)qfp->qf_module);
3574 else {
3575 if (qfp->qf_fnum != 0
3576 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3577 {
3578 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003579 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003580 fname = gettail(fname);
3581 }
3582 if (fname == NULL)
3583 sprintf((char *)IObuff, "%2d", qf_idx);
3584 else
3585 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3586 qf_idx, (char *)fname);
3587 }
3588
3589 // Support for filtering entries using :filter /pat/ clist
3590 // Match against the module name, file name, search pattern and
3591 // text of the entry.
3592 filter_entry = TRUE;
3593 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3594 filter_entry &= message_filtered(qfp->qf_module);
3595 if (filter_entry && fname != NULL)
3596 filter_entry &= message_filtered(fname);
3597 if (filter_entry && qfp->qf_pattern != NULL)
3598 filter_entry &= message_filtered(qfp->qf_pattern);
3599 if (filter_entry)
3600 filter_entry &= message_filtered(qfp->qf_text);
3601 if (filter_entry)
3602 return;
3603
3604 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003605 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003606
3607 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003608 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003609 if (qfp->qf_lnum == 0)
3610 IObuff[0] = NUL;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003611 else
thinca6864efa2021-06-19 20:45:20 +02003612 qf_range_text(qfp, IObuff, IOSIZE);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003613 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3614 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003615 msg_puts_attr((char *)IObuff, qfLineAttr);
3616 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003617 if (qfp->qf_pattern != NULL)
3618 {
3619 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003620 msg_puts((char *)IObuff);
3621 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003622 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003623 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003624
Bram Moolenaar5f30e262022-07-28 11:56:01 +01003625 {
3626 char_u *tbuf = IObuff;
3627 size_t tbuflen = IOSIZE;
3628 size_t len = STRLEN(qfp->qf_text) + 3;
3629
3630 if (len > IOSIZE)
3631 {
3632 tbuf = alloc(len);
3633 if (tbuf != NULL)
3634 tbuflen = len;
3635 else
3636 tbuf = IObuff;
3637 }
3638
3639 // Remove newlines and leading whitespace from the text. For an
3640 // unrecognized line keep the indent, the compiler may mark a word
3641 // with ^^^^.
3642 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3643 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3644 tbuf, tbuflen);
3645 msg_prt_line(tbuf, FALSE);
3646
3647 if (tbuf != IObuff)
3648 vim_free(tbuf);
3649 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003650 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003651}
3652
3653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003655 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 */
3657 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003658qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003660 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003661 qfline_T *qfp;
3662 int i;
3663 int idx1 = 1;
3664 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003665 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003666 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003667 int all = eap->forceit; // if not :cl!, only show
3668 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003669 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003671 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3672 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003673
Bram Moolenaar0398e002019-03-21 21:12:49 +01003674 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003676 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return;
3678 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003679 if (*arg == '+')
3680 {
3681 ++arg;
3682 plus = TRUE;
3683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3685 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003686 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return;
3688 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003689 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003690 if (plus)
3691 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003692 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003693 idx2 = i + idx1;
3694 idx1 = i;
3695 }
3696 else
3697 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003698 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003699 if (idx1 < 0)
3700 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3701 if (idx2 < 0)
3702 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003705 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003706 shorten_fnames(FALSE);
3707
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003708 // Get the attributes for the different quickfix highlight items. Note
3709 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003710 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3711 if (qfFileAttr == 0)
3712 qfFileAttr = HL_ATTR(HLF_D);
3713 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3714 if (qfSepAttr == 0)
3715 qfSepAttr = HL_ATTR(HLF_D);
3716 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3717 if (qfLineAttr == 0)
3718 qfLineAttr = HL_ATTR(HLF_N);
3719
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003720 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003722 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
3724 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003725 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 ui_breakcheck();
3728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729}
3730
3731/*
3732 * Remove newlines and leading whitespace from an error message.
3733 * Put the result in "buf[bufsize]".
3734 */
3735 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003736qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
3738 int i;
3739 char_u *p = text;
3740
3741 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3742 {
3743 if (*p == '\n')
3744 {
3745 buf[i] = ' ';
3746 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003747 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 break;
3749 }
3750 else
3751 buf[i] = *p++;
3752 }
3753 buf[i] = NUL;
3754}
3755
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003756/*
thinca6864efa2021-06-19 20:45:20 +02003757 * Range information from lnum, col, end_lnum, and end_col.
3758 * Put the result in "buf[bufsize]".
3759 */
3760 static void
3761qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3762{
3763 int len;
3764 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3765 len = (int)STRLEN(buf);
3766
3767 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3768 {
3769 vim_snprintf((char *)buf + len, bufsize - len,
3770 "-%ld", qfp->qf_end_lnum);
3771 len += (int)STRLEN(buf + len);
3772 }
3773 if (qfp->qf_col > 0)
3774 {
3775 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3776 len += (int)STRLEN(buf + len);
3777 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3778 {
3779 vim_snprintf((char *)buf + len, bufsize - len,
3780 "-%d", qfp->qf_end_col);
3781 len += (int)STRLEN(buf + len);
3782 }
3783 }
3784 buf[len] = NUL;
3785}
3786
3787/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003788 * Display information (list number, list size and the title) about a
3789 * quickfix/location list.
3790 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003791 static void
3792qf_msg(qf_info_T *qi, int which, char *lead)
3793{
3794 char *title = (char *)qi->qf_lists[which].qf_title;
3795 int count = qi->qf_lists[which].qf_count;
3796 char_u buf[IOSIZE];
3797
3798 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3799 lead,
3800 which + 1,
3801 qi->qf_listcount,
3802 count);
3803
3804 if (title != NULL)
3805 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003806 size_t len = STRLEN(buf);
3807
3808 if (len < 34)
3809 {
3810 vim_memset(buf + len, ' ', 34 - len);
3811 buf[34] = NUL;
3812 }
3813 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003814 }
3815 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003816 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003817}
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819/*
3820 * ":colder [count]": Up in the quickfix stack.
3821 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003822 * ":lolder [count]": Up in the location list stack.
3823 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 */
3825 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003826qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003828 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int count;
3830
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003831 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3832 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (eap->addr_count != 0)
3835 count = eap->line2;
3836 else
3837 count = 1;
3838 while (count--)
3839 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003840 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003842 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003844 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003847 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 else
3850 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003853 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003859 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003860 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861}
3862
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003863/*
3864 * Display the information about all the quickfix/location lists in the stack
3865 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003866 void
3867qf_history(exarg_T *eap)
3868{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003869 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003870 int i;
3871
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003872 if (eap->addr_count > 0)
3873 {
3874 if (qi == NULL)
3875 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003876 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003877 return;
3878 }
3879
3880 // Jump to the specified quickfix list
3881 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3882 {
3883 qi->qf_curlist = eap->line2 - 1;
3884 qf_msg(qi, qi->qf_curlist, "");
3885 qf_update_buffer(qi, NULL);
3886 }
3887 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003888 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003889
3890 return;
3891 }
3892
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003893 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003894 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003895 else
3896 for (i = 0; i < qi->qf_listcount; ++i)
3897 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3898}
3899
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003901 * Free all the entries in the error list "idx". Note that other information
3902 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 */
3904 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003905qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003907 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003908 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003909 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003911 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003913 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003914 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003915 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003916 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003917 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003918 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003919 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003920 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003921 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003922 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003923 // Somehow qf_count may have an incorrect value, set it to 1
3924 // to avoid crashing when it's wrong.
3925 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003926 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003927 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003928 qfl->qf_start = qfpnext;
3929 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003931
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003932 qfl->qf_index = 0;
3933 qfl->qf_start = NULL;
3934 qfl->qf_last = NULL;
3935 qfl->qf_ptr = NULL;
3936 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003937
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003938 qf_clean_dir_stack(&qfl->qf_dir_stack);
3939 qfl->qf_directory = NULL;
3940 qf_clean_dir_stack(&qfl->qf_file_stack);
3941 qfl->qf_currfile = NULL;
3942 qfl->qf_multiline = FALSE;
3943 qfl->qf_multiignore = FALSE;
3944 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945}
3946
3947/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003948 * Free error list "idx". Frees all the entries in the quickfix list,
3949 * associated context information and the title.
3950 */
3951 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003952qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003953{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003954 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003955
Bram Moolenaard23a8232018-02-10 18:45:26 +01003956 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003957 free_tv(qfl->qf_ctx);
3958 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01003959 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003960 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003961 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003962}
3963
3964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 * qf_mark_adjust: adjust marks
3966 */
3967 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003968qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003969 win_T *wp,
3970 linenr_T line1,
3971 linenr_T line2,
3972 long amount,
3973 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003975 int i;
3976 qfline_T *qfp;
3977 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003978 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003979 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003980 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
Bram Moolenaarc1542742016-07-20 21:44:37 +02003982 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003983 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003984 if (wp != NULL)
3985 {
3986 if (wp->w_llist == NULL)
3987 return;
3988 qi = wp->w_llist;
3989 }
3990
3991 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003992 {
3993 qf_list_T *qfl = qf_get_list(qi, idx);
3994
3995 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003996 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 if (qfp->qf_fnum == curbuf->b_fnum)
3998 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003999 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4001 {
4002 if (amount == MAXLNUM)
4003 qfp->qf_cleared = TRUE;
4004 else
4005 qfp->qf_lnum += amount;
4006 }
4007 else if (amount_after && qfp->qf_lnum > line2)
4008 qfp->qf_lnum += amount_after;
4009 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004010 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004011
4012 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004013 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014}
4015
4016/*
4017 * Make a nice message out of the error character and the error number:
4018 * char number message
4019 * e or E 0 " error"
4020 * w or W 0 " warning"
4021 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004022 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 * 0 0 ""
4024 * other 0 " c"
4025 * e or E n " error n"
4026 * w or W n " warning n"
4027 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004028 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 * 0 n " error n"
4030 * other n " c n"
4031 * 1 x "" :helpgrep
4032 */
4033 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004034qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036 static char_u buf[20];
4037 static char_u cc[3];
4038 char_u *p;
4039
4040 if (c == 'W' || c == 'w')
4041 p = (char_u *)" warning";
4042 else if (c == 'I' || c == 'i')
4043 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004044 else if (c == 'N' || c == 'n')
4045 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4047 p = (char_u *)" error";
4048 else if (c == 0 || c == 1)
4049 p = (char_u *)"";
4050 else
4051 {
4052 cc[0] = ' ';
4053 cc[1] = c;
4054 cc[2] = NUL;
4055 p = cc;
4056 }
4057
4058 if (nr <= 0)
4059 return p;
4060
4061 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4062 return buf;
4063}
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004066 * When "split" is FALSE: Open the entry/result under the cursor.
4067 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4068 */
4069 void
4070qf_view_result(int split)
4071{
4072 qf_info_T *qi = &ql_info;
4073
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004074 if (IS_LL_WINDOW(curwin))
4075 qi = GET_LOC_LIST(curwin);
4076
Bram Moolenaar0398e002019-03-21 21:12:49 +01004077 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004078 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004079 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004080 return;
4081 }
4082
4083 if (split)
4084 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004085 // Open the selected entry in a new window
4086 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4087 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004088 return;
4089 }
4090
4091 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4092}
4093
4094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 * ":cwindow": open the quickfix window if we have errors to display,
4096 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004097 * ":lwindow": open the location list window if we have locations to display,
4098 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 */
4100 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004101ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004103 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004104 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 win_T *win;
4106
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004107 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4108 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004109
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004110 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004111
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004112 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004113 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004115 // If a quickfix window is open but we have no errors to display,
4116 // close the window. If a quickfix window is not open, then open
4117 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004118 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004119 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004120 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 if (win != NULL)
4123 ex_cclose(eap);
4124 }
4125 else if (win == NULL)
4126 ex_copen(eap);
4127}
4128
4129/*
4130 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004131 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004134ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004136 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004137 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004139 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4140 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004142 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004143 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 if (win != NULL)
4145 win_close(win, FALSE);
4146}
4147
4148/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004149 * Set "w:quickfix_title" if "qi" has a title.
4150 */
4151 static void
4152qf_set_title_var(qf_list_T *qfl)
4153{
4154 if (qfl->qf_title != NULL)
4155 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4156}
4157
4158/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004159 * Goto a quickfix or location list window (if present).
4160 * Returns OK if the window is found, FAIL otherwise.
4161 */
4162 static int
4163qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4164{
4165 win_T *win;
4166
4167 win = qf_find_win(qi);
4168 if (win == NULL)
4169 return FAIL;
4170
4171 win_goto(win);
4172 if (resize)
4173 {
4174 if (vertsplit)
4175 {
4176 if (sz != win->w_width)
4177 win_setwidth(sz);
4178 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004179 else if (sz != win->w_height && win->w_height
4180 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004181 win_setheight(sz);
4182 }
4183
4184 return OK;
4185}
4186
4187/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004188 * Set options for the buffer in the quickfix or location list window.
4189 */
4190 static void
4191qf_set_cwindow_options(void)
4192{
4193 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004194 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4195 set_option_value_give_err((char_u *)"bt",
4196 0L, (char_u *)"quickfix", OPT_LOCAL);
4197 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004198 RESET_BINDING(curwin);
4199#ifdef FEAT_DIFF
4200 curwin->w_p_diff = FALSE;
4201#endif
4202#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004203 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004204 OPT_LOCAL);
4205#endif
4206}
4207
4208/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004209 * Open a new quickfix or location list window, load the quickfix buffer and
4210 * set the appropriate options for the window.
4211 * Returns FAIL if the window could not be opened.
4212 */
4213 static int
4214qf_open_new_cwindow(qf_info_T *qi, int height)
4215{
4216 buf_T *qf_buf;
4217 win_T *oldwin = curwin;
4218 tabpage_T *prevtab = curtab;
4219 int flags = 0;
4220 win_T *win;
4221
4222 qf_buf = qf_find_buf(qi);
4223
4224 // The current window becomes the previous window afterwards.
4225 win = curwin;
4226
Bram Moolenaare1004402020-10-24 20:49:43 +02004227 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004228 // Create the new quickfix window at the very bottom, except when
4229 // :belowright or :aboveleft is used.
4230 win_goto(lastwin);
4231 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004232 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004233 flags = WSP_BELOW;
4234 flags |= WSP_NEWLOC;
4235 if (win_split(height, flags) == FAIL)
4236 return FAIL; // not enough room for window
4237 RESET_BINDING(curwin);
4238
4239 if (IS_LL_STACK(qi))
4240 {
4241 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004242 // location list stack from the window 'win'.
4243 curwin->w_llist_ref = qi;
4244 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004245 }
4246
4247 if (oldwin != curwin)
4248 oldwin = NULL; // don't store info when in another window
4249 if (qf_buf != NULL)
4250 {
4251 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004252 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004253 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004254 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004255 }
4256 else
4257 {
4258 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004259 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4260 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004261 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004262
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004263 // save the number of the new buffer
4264 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004265 }
4266
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004267 // Set the options for the quickfix buffer/window (if not already done)
4268 // Do this even if the quickfix buffer was already present, as an autocmd
4269 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004270 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004271 qf_set_cwindow_options();
4272
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004273 // Only set the height when still in the same tab page and there is no
4274 // window to the side.
4275 if (curtab == prevtab && curwin->w_width == Columns)
4276 win_setheight(height);
4277 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4278 if (win_valid(win))
4279 prevwin = win;
4280
4281 return OK;
4282}
4283
4284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004286 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 */
4288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004289ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004291 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004292 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004294 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004295 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004297 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4298 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004299
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004300 incr_quickfix_busy();
4301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if (eap->addr_count != 0)
4303 height = eap->line2;
4304 else
4305 height = QF_WINHEIGHT;
4306
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004307 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#ifdef FEAT_GUI
4309 need_mouse_correct = TRUE;
4310#endif
4311
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004312 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004313 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004314 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004315 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004316 if (status == FAIL)
4317 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004318 {
4319 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004320 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004323 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004324 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004325 // Save the current index here, as updating the quickfix buffer may free
4326 // the quickfix list
4327 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004328
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004329 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004330 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004332 decr_quickfix_busy();
4333
4334 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 curwin->w_cursor.col = 0;
4336 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004337 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338}
4339
4340/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004341 * Move the cursor in the quickfix window to "lnum".
4342 */
4343 static void
4344qf_win_goto(win_T *win, linenr_T lnum)
4345{
4346 win_T *old_curwin = curwin;
4347
4348 curwin = win;
4349 curbuf = win->w_buffer;
4350 curwin->w_cursor.lnum = lnum;
4351 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004352 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004353 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004354 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004355 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004356 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004357 curwin = old_curwin;
4358 curbuf = curwin->w_buffer;
4359}
4360
4361/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004362 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004363 */
4364 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004365ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004366{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004367 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004368 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004369
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004370 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4371 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004372
4373 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004374 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4375 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4376}
4377
4378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 * Return the number of the current entry (line number in the quickfix
4380 * window).
4381 */
4382 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004383qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004385 qf_info_T *qi = &ql_info;
4386
4387 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004388 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004389 qi = wp->w_llist_ref;
4390
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004391 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392}
4393
4394/*
4395 * Update the cursor position in the quickfix window to the current error.
4396 * Return TRUE if there is a quickfix window.
4397 */
4398 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004399qf_win_pos_update(
4400 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004401 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402{
4403 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004404 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004406 // Put the cursor on the current error in the quickfix window, so that
4407 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004408 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 if (win != NULL
4410 && qf_index <= win->w_buffer->b_ml.ml_line_count
4411 && old_qf_index != qf_index)
4412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 if (qf_index > old_qf_index)
4414 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004415 win->w_redraw_top = old_qf_index;
4416 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 else
4419 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004420 win->w_redraw_top = qf_index;
4421 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004423 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 return win != NULL;
4426}
4427
4428/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004429 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004430 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004431 */
4432 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004433is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004434{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004435 // A window displaying the quickfix buffer will have the w_llist_ref field
4436 // set to NULL.
4437 // A window displaying a location list buffer will have the w_llist_ref
4438 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004439 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004440 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4441 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004442 return TRUE;
4443
4444 return FALSE;
4445}
4446
4447/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004448 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4449 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004450 */
4451 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004452qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004453{
4454 win_T *win;
4455
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004456 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004457 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004458 return win;
4459 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004460}
4461
4462/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004463 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004464 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 */
4466 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004467qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004469 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004470 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004472 if (qi->qf_bufnr != INVALID_QFBUFNR)
4473 {
4474 buf_T *qfbuf;
4475 qfbuf = buflist_findnr(qi->qf_bufnr);
4476 if (qfbuf != NULL)
4477 return qfbuf;
4478 // buffer is no longer present
4479 qi->qf_bufnr = INVALID_QFBUFNR;
4480 }
4481
Bram Moolenaar9c102382006-05-03 21:26:49 +00004482 FOR_ALL_TAB_WINDOWS(tp, win)
4483 if (is_qf_win(win, qi))
4484 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004485
Bram Moolenaar9c102382006-05-03 21:26:49 +00004486 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487}
4488
4489/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004490 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004491 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004492 */
4493 int
4494qf_process_qftf_option(void)
4495{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004496 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004497}
4498
4499/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004500 * Update the w:quickfix_title variable in the quickfix/location list window in
4501 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004502 */
4503 static void
4504qf_update_win_titlevar(qf_info_T *qi)
4505{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004506 qf_list_T *qfl = qf_get_curlist(qi);
4507 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004508 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004509 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004510
Bram Moolenaar530bed92020-12-16 21:02:56 +01004511 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004512 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004513 if (is_qf_win(win, qi))
4514 {
4515 curwin = win;
4516 qf_set_title_var(qfl);
4517 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004518 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004519 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004520}
4521
4522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 * Find the quickfix buffer. If it exists, update the contents.
4524 */
4525 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004526qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
4528 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004529 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004532 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004533 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (buf != NULL)
4535 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004536 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004537 int qf_winid = 0;
4538
4539 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004540 {
4541 if (curwin->w_llist == qi)
4542 win = curwin;
4543 else
4544 {
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004545 // Find the file window (non-quickfix) with this location list
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004546 win = qf_find_win_with_loclist(qi);
4547 if (win == NULL)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004548 // File window is not found. Find the location list window.
4549 win = qf_find_win(qi);
4550 if (win == NULL)
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004551 return;
4552 }
4553 qf_winid = win->w_id;
4554 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004555
4556 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004557 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004558 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
Bram Moolenaard823fa92016-08-12 16:29:27 +02004560 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004561
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004562 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004563 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004564
Bram Moolenaar864293a2016-06-02 13:40:04 +02004565 if (old_last == NULL)
4566 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004567 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004568
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004569 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004570 aucmd_restbuf(&aco);
4571 }
4572
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004573 // Only redraw when added lines are visible. This avoids flickering
4574 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004575 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4576 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578}
4579
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004580/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004581 * Add an error line to the quickfix buffer.
4582 */
4583 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004584qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004585 buf_T *buf, // quickfix window buffer
4586 linenr_T lnum,
4587 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004588 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004589 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004590 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004591{
4592 int len;
4593 buf_T *errbuf;
4594
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004595 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004596 // for this entry, then use it.
4597 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004598 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004599 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004600 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004601 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004602 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004603 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4604 len = (int)STRLEN(IObuff);
4605 }
4606 else if (qfp->qf_fnum != 0
4607 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4608 && errbuf->b_fname != NULL)
4609 {
4610 if (qfp->qf_type == 1) // :helpgrep
4611 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4612 else
4613 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004614 // Shorten the file name if not done already.
4615 // For optimization, do this only for the first entry in a
4616 // buffer.
4617 if (first_bufline && (errbuf->b_sfname == NULL
4618 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004619 {
4620 if (*dirname == NUL)
4621 mch_dirname(dirname, MAXPATHL);
4622 shorten_buf_fname(errbuf, dirname, FALSE);
4623 }
4624 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4625 }
4626 len = (int)STRLEN(IObuff);
4627 }
4628 else
4629 len = 0;
4630
4631 if (len < IOSIZE - 1)
4632 IObuff[len++] = '|';
4633
4634 if (qfp->qf_lnum > 0)
4635 {
thinca6864efa2021-06-19 20:45:20 +02004636 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004637 len += (int)STRLEN(IObuff + len);
4638
Bram Moolenaar858ba062020-05-31 23:11:59 +02004639 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4640 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004641 len += (int)STRLEN(IObuff + len);
4642 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004643 else if (qfp->qf_pattern != NULL)
4644 {
4645 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4646 len += (int)STRLEN(IObuff + len);
4647 }
4648 if (len < IOSIZE - 2)
4649 {
4650 IObuff[len++] = '|';
4651 IObuff[len++] = ' ';
4652 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004653
Bram Moolenaar858ba062020-05-31 23:11:59 +02004654 // Remove newlines and leading whitespace from the text.
4655 // For an unrecognized line keep the indent, the compiler may
4656 // mark a word with ^^^^.
4657 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4658 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004659 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004660
4661 if (ml_append_buf(buf, lnum, IObuff,
4662 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4663 return FAIL;
4664
4665 return OK;
4666}
4667
Bram Moolenaard43906d2020-07-20 21:31:32 +02004668/*
4669 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4670 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4671 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004672 static list_T *
4673call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4674{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004675 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004676 list_T *qftf_list = NULL;
4677
4678 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4679 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4680 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004681 if (qfl->qf_qftf_cb.cb_name != NULL)
4682 cb = &qfl->qf_qftf_cb;
4683 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004684 {
4685 typval_T args[1];
4686 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004687 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004688
4689 // create the dict argument
4690 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4691 return NULL;
4692 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4693 dict_add_number(d, "winid", (long)qf_winid);
4694 dict_add_number(d, "id", (long)qfl->qf_id);
4695 dict_add_number(d, "start_idx", start_idx);
4696 dict_add_number(d, "end_idx", end_idx);
4697 ++d->dv_refcount;
4698 args[0].v_type = VAR_DICT;
4699 args[0].vval.v_dict = d;
4700
Bram Moolenaard43906d2020-07-20 21:31:32 +02004701 qftf_list = NULL;
4702 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4703 {
4704 if (rettv.v_type == VAR_LIST)
4705 {
4706 qftf_list = rettv.vval.v_list;
4707 qftf_list->lv_refcount++;
4708 }
4709 clear_tv(&rettv);
4710 }
4711 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004712 }
4713
4714 return qftf_list;
4715}
4716
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 * Fill current buffer with quickfix errors, replacing any previous contents.
4719 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004720 * If "old_last" is not NULL append the items after this one.
4721 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4722 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 */
4724 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004725qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004727 linenr_T lnum;
4728 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004729 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004730 list_T *qftf_list = NULL;
4731 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732
Bram Moolenaar864293a2016-06-02 13:40:04 +02004733 if (old_last == NULL)
4734 {
4735 if (buf != curbuf)
4736 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004737 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004738 return;
4739 }
4740
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004741 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004742 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004743 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004746 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004747 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004749 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004750 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004751 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004752
4753 *dirname = NUL;
4754
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004755 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004756 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004757 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004758 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004759 lnum = 0;
4760 }
4761 else
4762 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004763 if (old_last->qf_next != NULL)
4764 qfp = old_last->qf_next;
4765 else
4766 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004767 lnum = buf->b_ml.ml_line_count;
4768 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004769
4770 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4771 (long)qfl->qf_count);
4772 if (qftf_list != NULL)
4773 qftf_li = qftf_list->lv_first;
4774
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004775 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004777 char_u *qftf_str = NULL;
4778
Bram Moolenaard43906d2020-07-20 21:31:32 +02004779 // Use the text supplied by the user defined function (if any).
4780 // If the returned value is not string, then ignore the rest
4781 // of the returned values and use the default.
4782 if (qftf_li != NULL && !invalid_val)
4783 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004784 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004785 if (qftf_str == NULL)
4786 invalid_val = TRUE;
4787 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004788
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004789 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4790 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004792
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004793 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004794 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004796 if (qfp == NULL)
4797 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004798
4799 if (qftf_li != NULL)
4800 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004802
4803 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004804 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004805 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004808 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 check_lnums(TRUE);
4810
Bram Moolenaar864293a2016-06-02 13:40:04 +02004811 if (old_last == NULL)
4812 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004813 // Set the 'filetype' to "qf" each time after filling the buffer.
4814 // This resembles reading a file into a buffer, it's more logical when
4815 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004816 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004817 set_option_value_give_err((char_u *)"ft",
4818 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004819 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004821 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004822 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004824 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004826 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004827 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004828
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004829 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004830 redraw_curbuf_later(NOT_VALID);
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004833 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 KeyTyped = old_KeyTyped;
4835}
4836
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004837/*
4838 * For every change made to the quickfix list, update the changed tick.
4839 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004840 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004841qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004842{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004843 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004844}
4845
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004847 * Return the quickfix/location list number with the given identifier.
4848 * Returns -1 if list is not found.
4849 */
4850 static int
4851qf_id2nr(qf_info_T *qi, int_u qfid)
4852{
4853 int qf_idx;
4854
4855 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4856 if (qi->qf_lists[qf_idx].qf_id == qfid)
4857 return qf_idx;
4858 return INVALID_QFIDX;
4859}
4860
4861/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004862 * If the current list is not "save_qfid" and we can find the list with that ID
4863 * then make it the current list.
4864 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004865 * Returns OK if successfully restored the list. Returns FAIL if the list with
4866 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004867 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004868 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004869qf_restore_list(qf_info_T *qi, int_u save_qfid)
4870{
4871 int curlist;
4872
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004873 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004874 {
4875 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004876 if (curlist < 0)
4877 // list is not present
4878 return FAIL;
4879 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004880 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004881 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004882}
4883
4884/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004885 * Jump to the first entry if there is one.
4886 */
4887 static void
4888qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4889{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004890 if (qf_restore_list(qi, save_qfid) == FAIL)
4891 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004892
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004893 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004894 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004895 qf_jump(qi, 0, 0, forceit);
4896}
4897
4898/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004899 * Return TRUE when using ":vimgrep" for ":grep".
4900 */
4901 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004902grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004903{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004904 return ((cmdidx == CMD_grep
4905 || cmdidx == CMD_lgrep
4906 || cmdidx == CMD_grepadd
4907 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004908 && STRCMP("internal",
4909 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4910}
4911
4912/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004913 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004915 static char_u *
4916make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004918 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004919 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004920 case CMD_make: return (char_u *)"make";
4921 case CMD_lmake: return (char_u *)"lmake";
4922 case CMD_grep: return (char_u *)"grep";
4923 case CMD_lgrep: return (char_u *)"lgrep";
4924 case CMD_grepadd: return (char_u *)"grepadd";
4925 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4926 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928}
4929
4930/*
4931 * Return the name for the errorfile, in allocated memory.
4932 * Find a new unique name when 'makeef' contains "##".
4933 * Returns NULL for error.
4934 */
4935 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004936get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
4938 char_u *p;
4939 char_u *name;
4940 static int start = -1;
4941 static int off = 0;
4942#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004943 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#endif
4945
4946 if (*p_mef == NUL)
4947 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004948 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004950 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 return name;
4952 }
4953
4954 for (p = p_mef; *p; ++p)
4955 if (p[0] == '#' && p[1] == '#')
4956 break;
4957
4958 if (*p == NUL)
4959 return vim_strsave(p_mef);
4960
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004961 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 for (;;)
4963 {
4964 if (start == -1)
4965 start = mch_get_pid();
4966 else
4967 off += 19;
4968
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004969 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (name == NULL)
4971 break;
4972 STRCPY(name, p_mef);
4973 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4974 STRCAT(name, p + 2);
4975 if (mch_getperm(name) < 0
4976#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004977 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 && mch_lstat((char *)name, &sb) < 0
4979#endif
4980 )
4981 break;
4982 vim_free(name);
4983 }
4984 return name;
4985}
4986
4987/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004988 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4989 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4990 */
4991 static char_u *
4992make_get_fullcmd(char_u *makecmd, char_u *fname)
4993{
4994 char_u *cmd;
4995 unsigned len;
4996
4997 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4998 if (*p_sp != NUL)
4999 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005000 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005001 if (cmd == NULL)
5002 return NULL;
5003 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5004 (char *)p_shq);
5005
5006 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5007 if (*p_sp != NUL)
5008 append_redir(cmd, len, p_sp, fname);
5009
5010 // Display the fully formed command. Output a newline if there's something
5011 // else than the :make command that was typed (in which case the cursor is
5012 // in column 0).
5013 if (msg_col == 0)
5014 msg_didout = FALSE;
5015 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005016 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005017 msg_outtrans(cmd); // show what we are doing
5018
5019 return cmd;
5020}
5021
5022/*
5023 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5024 */
5025 void
5026ex_make(exarg_T *eap)
5027{
5028 char_u *fname;
5029 char_u *cmd;
5030 char_u *enc = NULL;
5031 win_T *wp = NULL;
5032 qf_info_T *qi = &ql_info;
5033 int res;
5034 char_u *au_name = NULL;
5035 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005036 char_u *errorformat = p_efm;
5037 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005038
5039 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5040 if (grep_internal(eap->cmdidx))
5041 {
5042 ex_vimgrep(eap);
5043 return;
5044 }
5045
5046 au_name = make_get_auname(eap->cmdidx);
5047 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5048 curbuf->b_fname, TRUE, curbuf))
5049 {
5050#ifdef FEAT_EVAL
5051 if (aborting())
5052 return;
5053#endif
5054 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005055 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005056
5057 if (is_loclist_cmd(eap->cmdidx))
5058 wp = curwin;
5059
5060 autowrite_all();
5061 fname = get_mef_name();
5062 if (fname == NULL)
5063 return;
5064 mch_remove(fname); // in case it's not unique
5065
5066 cmd = make_get_fullcmd(eap->arg, fname);
5067 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005068 {
5069 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005070 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005071 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005072
5073 // let the shell know if we are redirecting output or not
5074 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5075
5076#ifdef AMIGA
5077 out_flush();
5078 // read window status report and redraw before message
5079 (void)char_avail();
5080#endif
5081
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005082 incr_quickfix_busy();
5083
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005084 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5085 errorformat = p_gefm;
5086 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5087 newlist = FALSE;
5088
5089 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5090 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005091 if (wp != NULL)
5092 {
5093 qi = GET_LOC_LIST(wp);
5094 if (qi == NULL)
5095 goto cleanup;
5096 }
5097 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005098 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005099
5100 // Remember the current quickfix list identifier, so that we can
5101 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005102 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005103 if (au_name != NULL)
5104 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5105 curbuf->b_fname, TRUE, curbuf);
5106 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5107 // display the first error
5108 qf_jump_first(qi, save_qfid, FALSE);
5109
5110cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005111 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005112 mch_remove(fname);
5113 vim_free(fname);
5114 vim_free(cmd);
5115}
5116
5117/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005118 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005119 */
5120 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005121qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005122{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005123 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005124
5125 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5126 return 0;
5127 return qf_get_curlist(qi)->qf_count;
5128}
5129
5130/*
5131 * Returns the number of valid entries in the current quickfix/location list.
5132 */
5133 int
5134qf_get_valid_size(exarg_T *eap)
5135{
5136 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005137 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005138 qfline_T *qfp;
5139 int i, sz = 0;
5140 int prev_fnum = 0;
5141
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005142 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5143 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005144
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005145 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005146 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005147 {
5148 if (qfp->qf_valid)
5149 {
5150 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005151 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005152 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5153 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005154 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005155 sz++;
5156 prev_fnum = qfp->qf_fnum;
5157 }
5158 }
5159 }
5160
5161 return sz;
5162}
5163
5164/*
5165 * Returns the current index of the quickfix/location list.
5166 * Returns 0 if there is an error.
5167 */
5168 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005169qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005170{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005171 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005172
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005173 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5174 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005175
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005176 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005177}
5178
5179/*
5180 * Returns the current index in the quickfix/location list (counting only valid
5181 * entries). If no valid entries are in the list, then returns 1.
5182 */
5183 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005184qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005185{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005186 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005187 qf_list_T *qfl;
5188 qfline_T *qfp;
5189 int i, eidx = 0;
5190 int prev_fnum = 0;
5191
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005192 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5193 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005194
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005195 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005196 qfp = qfl->qf_start;
5197
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005198 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005199 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005200 return 1;
5201
5202 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5203 {
5204 if (qfp->qf_valid)
5205 {
5206 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5207 {
5208 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5209 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005210 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005211 eidx++;
5212 prev_fnum = qfp->qf_fnum;
5213 }
5214 }
5215 else
5216 eidx++;
5217 }
5218 }
5219
5220 return eidx ? eidx : 1;
5221}
5222
5223/*
5224 * Get the 'n'th valid error entry in the quickfix or location list.
5225 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5226 * For :cdo and :ldo returns the 'n'th valid error entry.
5227 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5228 */
5229 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005230qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005231{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005232 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005233 int i, eidx;
5234 int prev_fnum = 0;
5235
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005236 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005237 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005238 return 1;
5239
Bram Moolenaar95946f12019-03-31 15:31:59 +02005240 eidx = 0;
5241 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005242 {
5243 if (qfp->qf_valid)
5244 {
5245 if (fdo)
5246 {
5247 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5248 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005249 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005250 eidx++;
5251 prev_fnum = qfp->qf_fnum;
5252 }
5253 }
5254 else
5255 eidx++;
5256 }
5257
5258 if (eidx == n)
5259 break;
5260 }
5261
5262 if (i <= qfl->qf_count)
5263 return i;
5264 else
5265 return 1;
5266}
5267
5268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005270 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005271 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 */
5273 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005274ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005276 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005277 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005278
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005279 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5280 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005281
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005282 if (eap->addr_count > 0)
5283 errornr = (int)eap->line2;
5284 else
5285 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005286 switch (eap->cmdidx)
5287 {
5288 case CMD_cc: case CMD_ll:
5289 errornr = 0;
5290 break;
5291 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5292 case CMD_lfirst:
5293 errornr = 1;
5294 break;
5295 default:
5296 errornr = 32767;
5297 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005298 }
5299
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005300 // For cdo and ldo commands, jump to the nth valid error.
5301 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005302 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5303 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005304 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005305 eap->addr_count > 0 ? (int)eap->line1 : 1,
5306 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5307
5308 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309}
5310
5311/*
5312 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005313 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005314 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 */
5316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005317ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005319 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005320 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005321 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005322
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005323 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5324 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005325
Bram Moolenaar55b69262017-08-13 13:42:01 +02005326 if (eap->addr_count > 0
5327 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5328 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005329 errornr = (int)eap->line2;
5330 else
5331 errornr = 1;
5332
Bram Moolenaar39665952018-08-15 20:59:48 +02005333 // Depending on the command jump to either next or previous entry/file.
5334 switch (eap->cmdidx)
5335 {
5336 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5337 dir = FORWARD;
5338 break;
5339 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5340 case CMD_lNext:
5341 dir = BACKWARD;
5342 break;
5343 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5344 dir = FORWARD_FILE;
5345 break;
5346 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5347 dir = BACKWARD_FILE;
5348 break;
5349 default:
5350 dir = FORWARD;
5351 break;
5352 }
5353
5354 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355}
5356
5357/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005358 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5359 * The index of the entry is stored in 'errornr'.
5360 * Returns NULL if an entry is not found.
5361 */
5362 static qfline_T *
5363qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5364{
5365 qfline_T *qfp = NULL;
5366 int idx = 0;
5367
5368 // Find the first entry in this file
5369 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5370 if (qfp->qf_fnum == bnr)
5371 break;
5372
5373 *errornr = idx;
5374 return qfp;
5375}
5376
5377/*
5378 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5379 * with the error number for the first entry. Assumes the entries are sorted in
5380 * the quickfix list by line number.
5381 */
5382 static qfline_T *
5383qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5384{
5385 while (!got_int
5386 && entry->qf_prev != NULL
5387 && entry->qf_fnum == entry->qf_prev->qf_fnum
5388 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5389 {
5390 entry = entry->qf_prev;
5391 --*errornr;
5392 }
5393
5394 return entry;
5395}
5396
5397/*
5398 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5399 * with the error number for the last entry. Assumes the entries are sorted in
5400 * the quickfix list by line number.
5401 */
5402 static qfline_T *
5403qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5404{
5405 while (!got_int &&
5406 entry->qf_next != NULL
5407 && entry->qf_fnum == entry->qf_next->qf_fnum
5408 && entry->qf_lnum == entry->qf_next->qf_lnum)
5409 {
5410 entry = entry->qf_next;
5411 ++*errornr;
5412 }
5413
5414 return entry;
5415}
5416
5417/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005418 * Returns TRUE if the specified quickfix entry is
5419 * after the given line (linewise is TRUE)
5420 * or after the line and column.
5421 */
5422 static int
5423qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5424{
5425 if (linewise)
5426 return qfp->qf_lnum > pos->lnum;
5427 else
5428 return (qfp->qf_lnum > pos->lnum ||
5429 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5430}
5431
5432/*
5433 * Returns TRUE if the specified quickfix entry is
5434 * before the given line (linewise is TRUE)
5435 * or before the line and column.
5436 */
5437 static int
5438qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5439{
5440 if (linewise)
5441 return qfp->qf_lnum < pos->lnum;
5442 else
5443 return (qfp->qf_lnum < pos->lnum ||
5444 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5445}
5446
5447/*
5448 * Returns TRUE if the specified quickfix entry is
5449 * on or after the given line (linewise is TRUE)
5450 * or on or after the line and column.
5451 */
5452 static int
5453qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5454{
5455 if (linewise)
5456 return qfp->qf_lnum >= pos->lnum;
5457 else
5458 return (qfp->qf_lnum > pos->lnum ||
5459 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5460}
5461
5462/*
5463 * Returns TRUE if the specified quickfix entry is
5464 * on or before the given line (linewise is TRUE)
5465 * or on or before the line and column.
5466 */
5467 static int
5468qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5469{
5470 if (linewise)
5471 return qfp->qf_lnum <= pos->lnum;
5472 else
5473 return (qfp->qf_lnum < pos->lnum ||
5474 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5475}
5476
5477/*
5478 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5479 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5480 * multiple entries on a single line as one. Otherwise returns the entry after
5481 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005482 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5483 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005484 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005485 */
5486 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005487qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005488 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005489 pos_T *pos,
5490 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005491 qfline_T *qfp,
5492 int *errornr)
5493{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005494 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005495 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005496 return qfp;
5497
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005498 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005499 while (qfp->qf_next != NULL
5500 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005501 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005502 {
5503 qfp = qfp->qf_next;
5504 ++*errornr;
5505 }
5506
5507 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005508 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005509 return NULL;
5510
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005511 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005512 qfp = qfp->qf_next;
5513 ++*errornr;
5514
5515 return qfp;
5516}
5517
5518/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005519 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5520 * If 'linewise' is TRUE, returns the entry before the specified line and
5521 * treats multiple entries on a single line as one. Otherwise returns the entry
5522 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005523 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5524 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005525 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005526 */
5527 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005528qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005529 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005530 pos_T *pos,
5531 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005532 qfline_T *qfp,
5533 int *errornr)
5534{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005535 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005536 while (qfp->qf_next != NULL
5537 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005538 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005539 {
5540 qfp = qfp->qf_next;
5541 ++*errornr;
5542 }
5543
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005544 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005545 return NULL;
5546
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005547 if (linewise)
5548 // If multiple entries are on the same line, then use the first entry
5549 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005550
5551 return qfp;
5552}
5553
5554/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005555 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005556 * the direction 'dir'.
5557 */
5558 static qfline_T *
5559qf_find_closest_entry(
5560 qf_list_T *qfl,
5561 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005562 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005563 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005564 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005565 int *errornr)
5566{
5567 qfline_T *qfp;
5568
5569 *errornr = 0;
5570
5571 // Find the first entry in this file
5572 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5573 if (qfp == NULL)
5574 return NULL; // no entry in this file
5575
5576 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005577 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005578 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005579 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005580
5581 return qfp;
5582}
5583
5584/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005585 * Get the nth quickfix entry below the specified entry. Searches forward in
5586 * the list. If linewise is TRUE, then treat multiple entries on a single line
5587 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005588 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005589 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005590qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005591{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005592 qfline_T *entry = entry_arg;
5593
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005594 while (n-- > 0 && !got_int)
5595 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005596 int first_errornr = *errornr;
5597
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005598 if (linewise)
5599 // Treat all the entries on the same line in this file as one
5600 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005601
5602 if (entry->qf_next == NULL
5603 || entry->qf_next->qf_fnum != entry->qf_fnum)
5604 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005605 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005606 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005607 break;
5608 }
5609
5610 entry = entry->qf_next;
5611 ++*errornr;
5612 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005613}
5614
5615/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005616 * Get the nth quickfix entry above the specified entry. Searches backwards in
5617 * the list. If linewise is TRUE, then treat multiple entries on a single line
5618 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005619 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005620 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005621qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005622{
5623 while (n-- > 0 && !got_int)
5624 {
5625 if (entry->qf_prev == NULL
5626 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5627 break;
5628
5629 entry = entry->qf_prev;
5630 --*errornr;
5631
5632 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005633 if (linewise)
5634 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005635 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005636}
5637
5638/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005639 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5640 * the specified direction. Returns the error number in the quickfix list or 0
5641 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005642 */
5643 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005644qf_find_nth_adj_entry(
5645 qf_list_T *qfl,
5646 int bnr,
5647 pos_T *pos,
5648 int n,
5649 int dir,
5650 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005651{
5652 qfline_T *adj_entry;
5653 int errornr;
5654
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005655 // Find an entry closest to the specified position
5656 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005657 if (adj_entry == NULL)
5658 return 0;
5659
5660 if (--n > 0)
5661 {
5662 // Go to the n'th entry in the current buffer
5663 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005664 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005665 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005666 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005667 }
5668
5669 return errornr;
5670}
5671
5672/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005673 * Jump to a quickfix entry in the current file nearest to the current line or
5674 * current line/col.
5675 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5676 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005677 */
5678 void
5679ex_cbelow(exarg_T *eap)
5680{
5681 qf_info_T *qi;
5682 qf_list_T *qfl;
5683 int dir;
5684 int buf_has_flag;
5685 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005686 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005687
5688 if (eap->addr_count > 0 && eap->line2 <= 0)
5689 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005690 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005691 return;
5692 }
5693
5694 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005695 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5696 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005697 buf_has_flag = BUF_HAS_QF_ENTRY;
5698 else
5699 buf_has_flag = BUF_HAS_LL_ENTRY;
5700 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5701 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005702 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005703 return;
5704 }
5705
5706 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5707 return;
5708
5709 qfl = qf_get_curlist(qi);
5710 // check if the list has valid errors
5711 if (!qf_list_has_valid_entries(qfl))
5712 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005713 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005714 return;
5715 }
5716
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005717 if (eap->cmdidx == CMD_cbelow
5718 || eap->cmdidx == CMD_lbelow
5719 || eap->cmdidx == CMD_cafter
5720 || eap->cmdidx == CMD_lafter)
5721 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005722 dir = FORWARD;
5723 else
5724 dir = BACKWARD;
5725
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005726 pos = curwin->w_cursor;
5727 // A quickfix entry column number is 1 based whereas cursor column
5728 // number is 0 based. Adjust the column number.
5729 pos.col++;
5730 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5731 eap->addr_count > 0 ? eap->line2 : 0, dir,
5732 eap->cmdidx == CMD_cbelow
5733 || eap->cmdidx == CMD_lbelow
5734 || eap->cmdidx == CMD_cabove
5735 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005736
5737 if (errornr > 0)
5738 qf_jump(qi, 0, errornr, FALSE);
5739 else
5740 emsg(_(e_no_more_items));
5741}
5742
5743/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005744 * Return the autocmd name for the :cfile Ex commands
5745 */
5746 static char_u *
5747cfile_get_auname(cmdidx_T cmdidx)
5748{
5749 switch (cmdidx)
5750 {
5751 case CMD_cfile: return (char_u *)"cfile";
5752 case CMD_cgetfile: return (char_u *)"cgetfile";
5753 case CMD_caddfile: return (char_u *)"caddfile";
5754 case CMD_lfile: return (char_u *)"lfile";
5755 case CMD_lgetfile: return (char_u *)"lgetfile";
5756 case CMD_laddfile: return (char_u *)"laddfile";
5757 default: return NULL;
5758 }
5759}
5760
5761/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005762 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005763 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 */
5765 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005766ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005768 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005769 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005770 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005771 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005772 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005773 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005774
Bram Moolenaara16123a2019-03-28 20:31:07 +01005775 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005776 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5777 NULL, FALSE, curbuf))
5778 {
5779#ifdef FEAT_EVAL
5780 if (aborting())
5781 return;
5782#endif
5783 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005784
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005785 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005786#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005787 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005788 {
5789 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005790 NULL, NULL,
5791 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005792 if (browse_file == NULL)
5793 return;
5794 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5795 vim_free(browse_file);
5796 }
5797 else
5798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005800 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005801
Bram Moolenaar39665952018-08-15 20:59:48 +02005802 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005803 wp = curwin;
5804
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005805 incr_quickfix_busy();
5806
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005807 // This function is used by the :cfile, :cgetfile and :caddfile
5808 // commands.
5809 // :cfile always creates a new quickfix list and jumps to the
5810 // first error.
5811 // :cgetfile creates a new quickfix list but doesn't jump to the
5812 // first error.
5813 // :caddfile adds to an existing quickfix list. If there is no
5814 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005815 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005816 && eap->cmdidx != CMD_laddfile),
5817 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005818 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005819 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005820 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005821 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005822 {
5823 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005824 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005825 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005826 }
5827 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005828 qf_list_changed(qf_get_curlist(qi));
5829 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005830 if (au_name != NULL)
5831 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005832
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005833 // Jump to the first error for a new list and if autocmds didn't
5834 // free the list.
5835 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5836 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005837 // display the first error
5838 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005839
5840 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005841}
5842
5843/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005844 * Return the vimgrep autocmd name.
5845 */
5846 static char_u *
5847vgr_get_auname(cmdidx_T cmdidx)
5848{
5849 switch (cmdidx)
5850 {
5851 case CMD_vimgrep: return (char_u *)"vimgrep";
5852 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5853 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5854 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5855 case CMD_grep: return (char_u *)"grep";
5856 case CMD_lgrep: return (char_u *)"lgrep";
5857 case CMD_grepadd: return (char_u *)"grepadd";
5858 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5859 default: return NULL;
5860 }
5861}
5862
5863/*
5864 * Initialize the regmatch used by vimgrep for pattern "s".
5865 */
5866 static void
5867vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5868{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005869 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005870 regmatch->regprog = NULL;
5871
5872 if (s == NULL || *s == NUL)
5873 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005874 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005875 if (last_search_pat() == NULL)
5876 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005877 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005878 return;
5879 }
5880 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5881 }
5882 else
5883 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5884
5885 regmatch->rmm_ic = p_ic;
5886 regmatch->rmm_maxcol = 0;
5887}
5888
5889/*
5890 * Display a file name when vimgrep is running.
5891 */
5892 static void
5893vgr_display_fname(char_u *fname)
5894{
5895 char_u *p;
5896
5897 msg_start();
5898 p = msg_strtrunc(fname, TRUE);
5899 if (p == NULL)
5900 msg_outtrans(fname);
5901 else
5902 {
5903 msg_outtrans(p);
5904 vim_free(p);
5905 }
5906 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005907 msg_didout = FALSE; // overwrite this message
5908 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005909 msg_col = 0;
5910 out_flush();
5911}
5912
5913/*
5914 * Load a dummy buffer to search for a pattern using vimgrep.
5915 */
5916 static buf_T *
5917vgr_load_dummy_buf(
5918 char_u *fname,
5919 char_u *dirname_start,
5920 char_u *dirname_now)
5921{
5922 int save_mls;
5923#if defined(FEAT_SYN_HL)
5924 char_u *save_ei = NULL;
5925#endif
5926 buf_T *buf;
5927
5928#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005929 // Don't do Filetype autocommands to avoid loading syntax and
5930 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005931 save_ei = au_event_disable(",Filetype");
5932#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005933 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005934 save_mls = p_mls;
5935 p_mls = 0;
5936
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005937 // Load file into a buffer, so that 'fileencoding' is detected,
5938 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005939 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5940
5941 p_mls = save_mls;
5942#if defined(FEAT_SYN_HL)
5943 au_event_restore(save_ei);
5944#endif
5945
5946 return buf;
5947}
5948
5949/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005950 * Check whether a quickfix/location list is valid. Autocmds may remove or
5951 * change a quickfix list when vimgrep is running. If the list is not found,
5952 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005953 */
5954 static int
5955vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005956 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005957 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005958 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005959 char_u *title)
5960{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005961 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005962 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005963 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005964 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005965 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005966 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005967 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005968 return FALSE;
5969 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005970 else
5971 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005972 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005973 qf_new_list(qi, title);
5974 return TRUE;
5975 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005976 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005977
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005978 if (qf_restore_list(qi, qfid) == FAIL)
5979 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005980
5981 return TRUE;
5982}
5983
5984/*
5985 * Search for a pattern in all the lines in a buffer and add the matching lines
5986 * to a quickfix list.
5987 */
5988 static int
5989vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005990 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005991 char_u *fname,
5992 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005993 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005994 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005995 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005996 int duplicate_name,
5997 int flags)
5998{
5999 int found_match = FALSE;
6000 long lnum;
6001 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006002 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006003
Bram Moolenaar1c299432018-10-28 14:36:09 +01006004 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006005 {
6006 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006007 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006008 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006009 // Regular expression match
6010 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Paul Ollis65745772022-06-05 16:55:54 +01006011 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006012 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006013 // Pass the buffer number so that it gets used even for a
6014 // dummy buffer, unless duplicate_name is set, then the
6015 // buffer will be wiped out below.
6016 if (qf_add_entry(qfl,
6017 NULL, // dir
6018 fname,
6019 NULL,
6020 duplicate_name ? 0 : buf->b_fnum,
6021 ml_get_buf(buf,
6022 regmatch->startpos[0].lnum + lnum, FALSE),
6023 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006024 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006025 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006026 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006027 FALSE, // vis_col
6028 NULL, // search pattern
6029 0, // nr
6030 0, // type
6031 TRUE // valid
6032 ) == QF_FAIL)
6033 {
6034 got_int = TRUE;
6035 break;
6036 }
6037 found_match = TRUE;
6038 if (--*tomatch == 0)
6039 break;
6040 if ((flags & VGR_GLOBAL) == 0
6041 || regmatch->endpos[0].lnum > 0)
6042 break;
6043 col = regmatch->endpos[0].col
6044 + (col == regmatch->endpos[0].col);
6045 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6046 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006047 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006048 }
6049 else
6050 {
6051 char_u *str = ml_get_buf(buf, lnum, FALSE);
6052 int score;
6053 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006054 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006055
6056 // Fuzzy string match
6057 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6058 {
6059 // Pass the buffer number so that it gets used even for a
6060 // dummy buffer, unless duplicate_name is set, then the
6061 // buffer will be wiped out below.
6062 if (qf_add_entry(qfl,
6063 NULL, // dir
6064 fname,
6065 NULL,
6066 duplicate_name ? 0 : buf->b_fnum,
6067 str,
6068 lnum,
thinca6864efa2021-06-19 20:45:20 +02006069 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006070 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006071 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006072 FALSE, // vis_col
6073 NULL, // search pattern
6074 0, // nr
6075 0, // type
6076 TRUE // valid
6077 ) == QF_FAIL)
6078 {
6079 got_int = TRUE;
6080 break;
6081 }
6082 found_match = TRUE;
6083 if (--*tomatch == 0)
6084 break;
6085 if ((flags & VGR_GLOBAL) == 0)
6086 break;
6087 col = matches[pat_len - 1] + col + 1;
6088 if (col > (colnr_T)STRLEN(str))
6089 break;
6090 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006091 }
6092 line_breakcheck();
6093 if (got_int)
6094 break;
6095 }
6096
6097 return found_match;
6098}
6099
6100/*
6101 * Jump to the first match and update the directory.
6102 */
6103 static void
6104vgr_jump_to_match(
6105 qf_info_T *qi,
6106 int forceit,
6107 int *redraw_for_dummy,
6108 buf_T *first_match_buf,
6109 char_u *target_dir)
6110{
6111 buf_T *buf;
6112
6113 buf = curbuf;
6114 qf_jump(qi, 0, 0, forceit);
6115 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006116 // If we jumped to another buffer redrawing will already be
6117 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006118 *redraw_for_dummy = FALSE;
6119
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006120 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006121 if (curbuf == first_match_buf && target_dir != NULL)
6122 {
6123 exarg_T ea;
6124
Bram Moolenaara80faa82020-04-12 19:37:17 +02006125 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006126 ea.arg = target_dir;
6127 ea.cmdidx = CMD_lcd;
6128 ex_cd(&ea);
6129 }
6130}
6131
6132/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006133 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006134 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006135typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006136{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006137 long tomatch; // maximum number of matches to find
6138 char_u *spat; // search pattern
6139 int flags; // search modifier
6140 char_u **fnames; // list of files to search
6141 int fcount; // number of files
6142 regmmatch_T regmatch; // compiled search pattern
6143 char_u *qf_title; // quickfix list title
6144} vgr_args_T;
6145
6146/*
6147 * Process :vimgrep command arguments. The command syntax is:
6148 *
6149 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6150 */
6151 static int
6152vgr_process_args(
6153 exarg_T *eap,
6154 vgr_args_T *args)
6155{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006156 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006157
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006158 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006159
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006160 args->regmatch.regprog = NULL;
6161 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006162
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006163 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006164 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006165 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006166 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006167
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006168 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006169 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006170 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006171 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006172 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006173 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006174 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006175
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006176 vgr_init_regmatch(&args->regmatch, args->spat);
6177 if (args->regmatch.regprog == NULL)
6178 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006179
6180 p = skipwhite(p);
6181 if (*p == NUL)
6182 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006183 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006184 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006185 }
6186
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006187 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006188 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6189 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006190 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006191 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006192 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006193 }
6194
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006195 return OK;
6196}
6197
6198/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006199 * Return TRUE if "buf" had an existing swap file, the current swap file does
6200 * not end in ".swp".
6201 */
6202 static int
6203existing_swapfile(buf_T *buf)
6204{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006205 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006206 {
6207 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6208 size_t len = STRLEN(fname);
6209
6210 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6211 }
6212 return FALSE;
6213}
6214
6215/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006216 * Search for a pattern in a list of files and populate the quickfix list with
6217 * the matches.
6218 */
6219 static int
6220vgr_process_files(
6221 win_T *wp,
6222 qf_info_T *qi,
6223 vgr_args_T *cmd_args,
6224 int *redraw_for_dummy,
6225 buf_T **first_match_buf,
6226 char_u **target_dir)
6227{
6228 int status = FAIL;
6229 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6230 time_t seconds = 0;
6231 char_u *fname;
6232 int fi;
6233 buf_T *buf;
6234 int duplicate_name = FALSE;
6235 int using_dummy;
6236 char_u *dirname_start = NULL;
6237 char_u *dirname_now = NULL;
6238 int found_match;
6239 aco_save_T aco;
6240
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006241 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6242 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006243 if (dirname_start == NULL || dirname_now == NULL)
6244 goto theend;
6245
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006246 // Remember the current directory, because a BufRead autocommand that does
6247 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006248 mch_dirname(dirname_start, MAXPATHL);
6249
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006250 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006251 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6252 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006253 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006254 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006255 if (time(NULL) > seconds)
6256 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006257 // Display the file name every second or so, show the user we are
6258 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006259 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006260 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006261 }
6262
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006263 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006264 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6265 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006266 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006267 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006268 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006269 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006270
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006271 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006272 }
6273 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006274 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006275 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006276
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006277 // Check whether the quickfix list is still valid. When loading a
6278 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006279 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006280 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006281
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006282 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006283
Bram Moolenaar81695252004-12-29 20:58:21 +00006284 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006285 {
6286 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006287 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006288 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006289 else
6290 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006291 // Try for a match in all lines of the buffer.
6292 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006293 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006294 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006295 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006296
Bram Moolenaar81695252004-12-29 20:58:21 +00006297 if (using_dummy)
6298 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006299 if (found_match && *first_match_buf == NULL)
6300 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006301 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006302 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006303 // Never keep a dummy buffer if there is another buffer
6304 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006305 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006306 buf = NULL;
6307 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006308 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006309 || buf->b_p_bh[0] == 'u' // "unload"
6310 || buf->b_p_bh[0] == 'w' // "wipe"
6311 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006312 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006313 // When no match was found we don't need to remember the
6314 // buffer, wipe it out. If there was a match and it
6315 // wasn't the first one or we won't jump there: only
6316 // unload the buffer.
6317 // Ignore 'hidden' here, because it may lead to having too
6318 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006319 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006320 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006321 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006322 buf = NULL;
6323 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006324 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006325 || (cmd_args->flags & VGR_NOJUMP)
6326 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006327 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006328 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006329 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006330 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006331 buf = NULL;
6332 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006333 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006334
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006335 if (buf != NULL)
6336 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006337 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006338 buf->b_flags &= ~BF_DUMMY;
6339
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006340 // If the buffer is still loaded we need to use the
6341 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006342 if (buf == *first_match_buf
6343 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006344 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006345 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006346
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006347 // The buffer is still loaded, the Filetype autocommands
6348 // need to be done now, in that buffer. And the modelines
6349 // need to be done (again). But not the window-local
6350 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006351 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006352#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006353 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6354 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006355#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006356 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006357 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006358 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006359 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006360 }
6361 }
6362
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006363 status = OK;
6364
6365theend:
6366 vim_free(dirname_now);
6367 vim_free(dirname_start);
6368 return status;
6369}
6370
6371/*
6372 * ":vimgrep {pattern} file(s)"
6373 * ":vimgrepadd {pattern} file(s)"
6374 * ":lvimgrep {pattern} file(s)"
6375 * ":lvimgrepadd {pattern} file(s)"
6376 */
6377 void
6378ex_vimgrep(exarg_T *eap)
6379{
6380 vgr_args_T args;
6381 qf_info_T *qi;
6382 qf_list_T *qfl;
6383 int_u save_qfid;
6384 win_T *wp = NULL;
6385 int redraw_for_dummy = FALSE;
6386 buf_T *first_match_buf = NULL;
6387 char_u *target_dir = NULL;
6388 char_u *au_name = NULL;
6389 int status;
6390
6391 au_name = vgr_get_auname(eap->cmdidx);
6392 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6393 curbuf->b_fname, TRUE, curbuf))
6394 {
6395#ifdef FEAT_EVAL
6396 if (aborting())
6397 return;
6398#endif
6399 }
6400
6401 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6402 if (qi == NULL)
6403 return;
6404
6405 if (vgr_process_args(eap, &args) == FAIL)
6406 goto theend;
6407
6408 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6409 && eap->cmdidx != CMD_vimgrepadd
6410 && eap->cmdidx != CMD_lvimgrepadd)
6411 || qf_stack_empty(qi))
6412 // make place for a new list
6413 qf_new_list(qi, args.qf_title);
6414
6415 incr_quickfix_busy();
6416
6417 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6418 &first_match_buf, &target_dir);
6419 if (status != OK)
6420 {
6421 FreeWild(args.fcount, args.fnames);
6422 decr_quickfix_busy();
6423 goto theend;
6424 }
6425
6426 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006427
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006428 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006429 qfl->qf_nonevalid = FALSE;
6430 qfl->qf_ptr = qfl->qf_start;
6431 qfl->qf_index = 1;
6432 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006433
Bram Moolenaar864293a2016-06-02 13:40:04 +02006434 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006435
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006436 // Remember the current quickfix list identifier, so that we can check for
6437 // autocommands changing the current quickfix list.
6438 save_qfid = qf_get_curlist(qi)->qf_id;
6439
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006440 if (au_name != NULL)
6441 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6442 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006443 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6444 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006445 if (!qflist_valid(wp, save_qfid)
6446 || qf_restore_list(qi, save_qfid) == FAIL)
6447 {
6448 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006449 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006450 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006451
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006452 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006453 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006454 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006455 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006456 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6457 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006458 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006459 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006460 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006461
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006462 decr_quickfix_busy();
6463
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006464 // If we loaded a dummy buffer into the current window, the autocommands
6465 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006466 if (redraw_for_dummy)
6467 {
6468#ifdef FEAT_FOLDING
6469 foldUpdateAll(curwin);
6470#else
6471 redraw_later(NOT_VALID);
6472#endif
6473 }
6474
Bram Moolenaar86b68352004-12-27 21:59:20 +00006475theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006476 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006477 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006478 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006479}
6480
6481/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006482 * Restore current working directory to "dirname_start" if they differ, taking
6483 * into account whether it is set locally or globally.
6484 */
6485 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006486restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006487{
6488 char_u *dirname_now = alloc(MAXPATHL);
6489
6490 if (NULL != dirname_now)
6491 {
6492 mch_dirname(dirname_now, MAXPATHL);
6493 if (STRCMP(dirname_start, dirname_now) != 0)
6494 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006495 // If the directory has changed, change it back by building up an
6496 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006497 exarg_T ea;
6498
Bram Moolenaara80faa82020-04-12 19:37:17 +02006499 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006500 ea.arg = dirname_start;
6501 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6502 ex_cd(&ea);
6503 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006504 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006505 }
6506}
6507
6508/*
6509 * Load file "fname" into a dummy buffer and return the buffer pointer,
6510 * placing the directory resulting from the buffer load into the
6511 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6512 * prior to calling this function. Restores directory to "dirname_start" prior
6513 * to returning, if autocmds or the 'autochdir' option have changed it.
6514 *
6515 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6516 * or wipe_dummy_buffer() later!
6517 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006518 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006519 */
6520 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006521load_dummy_buffer(
6522 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006523 char_u *dirname_start, // in: old directory
6524 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006525{
6526 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006527 bufref_T newbufref;
6528 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006529 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006530 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006531 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006532
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006533 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006534 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6535 if (newbuf == NULL)
6536 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006537 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006538
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006539 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006540 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6541
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006542 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006543 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006544 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006545 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006546 ++newbuf->b_locked;
6547
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006548 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006549 aucmd_prepbuf(&aco, newbuf);
6550
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006551 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006552 (void)setfname(curbuf, fname, NULL, FALSE);
6553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006554 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006555 check_need_swap(TRUE);
6556
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006557 // Remove the "dummy" flag, otherwise autocommands may not
6558 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006559 curbuf->b_flags &= ~BF_DUMMY;
6560
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006561 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006562 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006563 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006564 NULL, READ_NEW | READ_DUMMY);
6565 --newbuf->b_locked;
6566 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006567 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006568 && !(curbuf->b_flags & BF_NEW))
6569 {
6570 failed = FALSE;
6571 if (curbuf != newbuf)
6572 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006573 // Bloody autocommands changed the buffer! Can happen when
6574 // using netrw and editing a remote file. Use the current
6575 // buffer instead, delete the dummy one after restoring the
6576 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006577 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006578 newbuf = curbuf;
6579 }
6580 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006581
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006582 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006583 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006584 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6585 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006586
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006587 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6588 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006589 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006590 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006591
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006592 // When autocommands/'autochdir' option changed directory: go back.
6593 // Let the caller know what the resulting dir was first, in case it is
6594 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006595 mch_dirname(resulting_dir, MAXPATHL);
6596 restore_start_dir(dirname_start);
6597
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006598 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006599 return NULL;
6600 if (failed)
6601 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006602 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006603 return NULL;
6604 }
6605 return newbuf;
6606}
6607
6608/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006609 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6610 * directory to "dirname_start" prior to returning, if autocmds or the
6611 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006612 */
6613 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006614wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006615{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006616 // If any autocommand opened a window on the dummy buffer, close that
6617 // window. If we can't close them all then give up.
6618 while (buf->b_nwindows > 0)
6619 {
6620 int did_one = FALSE;
6621 win_T *wp;
6622
6623 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006624 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006625 if (wp->w_buffer == buf)
6626 {
6627 if (win_close(wp, FALSE) == OK)
6628 did_one = TRUE;
6629 break;
6630 }
6631 if (!did_one)
6632 return;
6633 }
6634
6635 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006636 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006637#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006638 cleanup_T cs;
6639
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006640 // Reset the error/interrupt/exception state here so that aborting()
6641 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6642 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006643 enter_cleanup(&cs);
6644#endif
6645
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006646 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006647
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006648#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006649 // Restore the error/interrupt/exception state if not discarded by a
6650 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006651 leave_cleanup(&cs);
6652#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006653 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006654 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006655 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006656}
6657
6658/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006659 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6660 * directory to "dirname_start" prior to returning, if autocmds or the
6661 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006662 */
6663 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006664unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006665{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006666 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006667 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006668 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006669
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006670 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006671 restore_start_dir(dirname_start);
6672 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006673}
6674
Bram Moolenaar05159a02005-02-26 23:04:13 +00006675#if defined(FEAT_EVAL) || defined(PROTO)
6676/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006677 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006678 * to 'list'. Returns OK on success.
6679 */
6680 static int
6681get_qfline_items(qfline_T *qfp, list_T *list)
6682{
6683 int bufnum;
6684 dict_T *dict;
6685 char_u buf[2];
6686
6687 // Handle entries with a non-existing buffer number.
6688 bufnum = qfp->qf_fnum;
6689 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6690 bufnum = 0;
6691
6692 if ((dict = dict_alloc()) == NULL)
6693 return FAIL;
6694 if (list_append_dict(list, dict) == FAIL)
6695 return FAIL;
6696
6697 buf[0] = qfp->qf_type;
6698 buf[1] = NUL;
6699 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006700 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6701 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6702 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6703 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6704 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6705 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006706 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6707 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6708 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6709 || dict_add_string(dict, "type", buf) == FAIL
6710 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6711 return FAIL;
6712
6713 return OK;
6714}
6715
6716/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006717 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006718 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006719 * If eidx is not 0, then return only the specified entry. Otherwise return
6720 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006721 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006722 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006723get_errorlist(
6724 qf_info_T *qi_arg,
6725 win_T *wp,
6726 int qf_idx,
6727 int eidx,
6728 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006729{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006730 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006731 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006732 qfline_T *qfp;
6733 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006734
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006735 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006736 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006737 qi = &ql_info;
6738 if (wp != NULL)
6739 {
6740 qi = GET_LOC_LIST(wp);
6741 if (qi == NULL)
6742 return FAIL;
6743 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006744 }
6745
Bram Moolenaar858ba062020-05-31 23:11:59 +02006746 if (eidx < 0)
6747 return OK;
6748
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006749 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006750 qf_idx = qi->qf_curlist;
6751
Bram Moolenaar0398e002019-03-21 21:12:49 +01006752 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006753 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006754
Bram Moolenaar0398e002019-03-21 21:12:49 +01006755 qfl = qf_get_list(qi, qf_idx);
6756 if (qf_list_empty(qfl))
6757 return FAIL;
6758
Bram Moolenaara16123a2019-03-28 20:31:07 +01006759 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006760 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006761 if (eidx > 0)
6762 {
6763 if (eidx == i)
6764 return get_qfline_items(qfp, list);
6765 }
6766 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006767 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006768 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006769
Bram Moolenaar05159a02005-02-26 23:04:13 +00006770 return OK;
6771}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006773// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006774enum {
6775 QF_GETLIST_NONE = 0x0,
6776 QF_GETLIST_TITLE = 0x1,
6777 QF_GETLIST_ITEMS = 0x2,
6778 QF_GETLIST_NR = 0x4,
6779 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006780 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006781 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006782 QF_GETLIST_IDX = 0x40,
6783 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006784 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006785 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006786 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006787 QF_GETLIST_QFTF = 0x800,
6788 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006789};
6790
6791/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006792 * Parse text from 'di' and return the quickfix list items.
6793 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006794 */
6795 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006796qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006797{
6798 int status = FAIL;
6799 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006800 char_u *errorformat = p_efm;
6801 dictitem_T *efm_di;
6802 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006803
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006804 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006805 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006806 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006807 // If errorformat is supplied then use it, otherwise use the 'efm'
6808 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006809 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6810 {
6811 if (efm_di->di_tv.v_type != VAR_STRING ||
6812 efm_di->di_tv.vval.v_string == NULL)
6813 return FAIL;
6814 errorformat = efm_di->di_tv.vval.v_string;
6815 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006816
Bram Moolenaar36538222017-09-02 19:51:44 +02006817 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006818 if (l == NULL)
6819 return FAIL;
6820
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006821 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006822 if (qi != NULL)
6823 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006824 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006825 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6826 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006827 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006828 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006829 }
6830 free(qi);
6831 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006832 dict_add_list(retdict, "items", l);
6833 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006834 }
6835
6836 return status;
6837}
6838
6839/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006840 * Return the quickfix/location list window identifier in the current tabpage.
6841 */
6842 static int
6843qf_winid(qf_info_T *qi)
6844{
6845 win_T *win;
6846
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006847 // The quickfix window can be opened even if the quickfix list is not set
6848 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006849 if (qi == NULL)
6850 return 0;
6851 win = qf_find_win(qi);
6852 if (win != NULL)
6853 return win->w_id;
6854 return 0;
6855}
6856
6857/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006858 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006859 * window. If there is no buffer associated with the list or the buffer is
6860 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006861 */
6862 static int
6863qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6864{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006865 int bufnum = 0;
6866
6867 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6868 bufnum = qi->qf_bufnr;
6869
6870 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006871}
6872
6873/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006874 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006875 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006876 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006877qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006878{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006879 int flags = QF_GETLIST_NONE;
6880
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006881 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006882 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006883 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006884 if (!loclist)
6885 // File window ID is applicable only to location list windows
6886 flags &= ~ QF_GETLIST_FILEWINID;
6887 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006888
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006889 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006890 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006891
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006892 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006893 flags |= QF_GETLIST_NR;
6894
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006895 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006896 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006897
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006898 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006899 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006900
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006901 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006902 flags |= QF_GETLIST_ID;
6903
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006904 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006905 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006906
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006907 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006908 flags |= QF_GETLIST_IDX;
6909
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006910 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006911 flags |= QF_GETLIST_SIZE;
6912
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006913 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01006914 flags |= QF_GETLIST_TICK;
6915
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006916 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006917 flags |= QF_GETLIST_FILEWINID;
6918
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006919 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006920 flags |= QF_GETLIST_QFBUFNR;
6921
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006922 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02006923 flags |= QF_GETLIST_QFTF;
6924
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006925 return flags;
6926}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006927
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006928/*
6929 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6930 * If 'nr' and 'id' are not present in 'what' then return the current
6931 * quickfix list index.
6932 * If 'nr' is zero then return the current quickfix list index.
6933 * If 'nr' is '$' then return the last quickfix list index.
6934 * If 'id' is present then return the index of the quickfix list with that id.
6935 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6936 * Return -1, if quickfix list is not present or if the stack is empty.
6937 */
6938 static int
6939qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6940{
6941 int qf_idx;
6942 dictitem_T *di;
6943
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006944 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006945 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6946 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006947 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006948 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006949 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006950 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006951 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006952 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006953 qf_idx = di->di_tv.vval.v_number - 1;
6954 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006955 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006956 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006957 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006958 else if (di->di_tv.v_type == VAR_STRING
6959 && di->di_tv.vval.v_string != NULL
6960 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006961 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006962 qf_idx = qi->qf_listcount - 1;
6963 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006964 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006965 }
6966
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006967 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6968 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006969 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006970 if (di->di_tv.v_type == VAR_NUMBER)
6971 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006972 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006973 if (di->di_tv.vval.v_number != 0)
6974 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6975 }
6976 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006977 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006978 }
6979
6980 return qf_idx;
6981}
6982
6983/*
6984 * Return default values for quickfix list properties in retdict.
6985 */
6986 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006987qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006988{
6989 int status = OK;
6990
6991 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006992 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006993 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6994 {
6995 list_T *l = list_alloc();
6996 if (l != NULL)
6997 status = dict_add_list(retdict, "items", l);
6998 else
6999 status = FAIL;
7000 }
7001 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007002 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007003 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007004 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007005 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007006 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007007 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007008 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007009 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007010 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007011 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007012 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007013 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007014 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007015 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007016 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007017 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7018 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007019 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7020 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007021
7022 return status;
7023}
7024
7025/*
7026 * Return the quickfix list title as 'title' in retdict
7027 */
7028 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007029qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007030{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007031 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007032}
7033
7034/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007035 * Returns the identifier of the window used to display files from a location
7036 * list. If there is no associated window, then returns 0. Useful only when
7037 * called from a location list window.
7038 */
7039 static int
7040qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7041{
7042 int winid = 0;
7043
7044 if (wp != NULL && IS_LL_WINDOW(wp))
7045 {
7046 win_T *ll_wp = qf_find_win_with_loclist(qi);
7047 if (ll_wp != NULL)
7048 winid = ll_wp->w_id;
7049 }
7050
7051 return dict_add_number(retdict, "filewinid", winid);
7052}
7053
7054/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007055 * Return the quickfix list items/entries as 'items' in retdict.
7056 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007057 */
7058 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007059qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007060{
7061 int status = OK;
7062 list_T *l = list_alloc();
7063 if (l != NULL)
7064 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007065 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007066 dict_add_list(retdict, "items", l);
7067 }
7068 else
7069 status = FAIL;
7070
7071 return status;
7072}
7073
7074/*
7075 * Return the quickfix list context (if any) as 'context' in retdict.
7076 */
7077 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007078qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007079{
7080 int status;
7081 dictitem_T *di;
7082
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007083 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007084 {
7085 di = dictitem_alloc((char_u *)"context");
7086 if (di != NULL)
7087 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007088 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007089 status = dict_add(retdict, di);
7090 if (status == FAIL)
7091 dictitem_free(di);
7092 }
7093 else
7094 status = FAIL;
7095 }
7096 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007097 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007098
7099 return status;
7100}
7101
7102/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007103 * Return the current quickfix list index as 'idx' in retdict.
7104 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007105 */
7106 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007107qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007108{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007109 if (eidx == 0)
7110 {
7111 eidx = qfl->qf_index;
7112 if (qf_list_empty(qfl))
7113 // For empty lists, current index is set to 0
7114 eidx = 0;
7115 }
7116 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007117}
7118
7119/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007120 * Return the 'quickfixtextfunc' function of a quickfix/location list
7121 */
7122 static int
7123qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7124{
7125 int status;
7126
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007127 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007128 {
7129 typval_T tv;
7130
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007131 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007132 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7133 clear_tv(&tv);
7134 }
7135 else
7136 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7137
7138 return status;
7139}
7140
7141/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007142 * Return quickfix/location list details (title) as a
7143 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7144 * then current list is used. Otherwise the specified list is used.
7145 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007146 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007147qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7148{
7149 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007150 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007151 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007152 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007153 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007154 dictitem_T *di;
7155 int flags = QF_GETLIST_NONE;
7156
7157 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7158 return qf_get_list_from_lines(what, di, retdict);
7159
7160 if (wp != NULL)
7161 qi = GET_LOC_LIST(wp);
7162
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007163 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007164
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007165 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007166 qf_idx = qf_getprop_qfidx(qi, what);
7167
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007168 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007169 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007170 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007171
Bram Moolenaar0398e002019-03-21 21:12:49 +01007172 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007173
Bram Moolenaar858ba062020-05-31 23:11:59 +02007174 // If an entry index is specified, use that
7175 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7176 {
7177 if (di->di_tv.v_type != VAR_NUMBER)
7178 return FAIL;
7179 eidx = di->di_tv.vval.v_number;
7180 }
7181
Bram Moolenaard823fa92016-08-12 16:29:27 +02007182 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007183 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007184 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007185 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007186 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007187 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007188 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007189 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007190 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007191 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007192 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007193 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007194 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007195 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007196 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007197 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007198 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007199 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007200 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7201 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007202 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7203 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007204 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7205 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007206
Bram Moolenaard823fa92016-08-12 16:29:27 +02007207 return status;
7208}
7209
7210/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007211 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007212 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7213 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007214 */
7215 static int
7216qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007217 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007218 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007219 int first_entry,
7220 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007221{
7222 static int did_bufnr_emsg;
7223 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007224 int bufnum, valid, status, col, end_col, vcol, nr;
7225 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007226
7227 if (first_entry)
7228 did_bufnr_emsg = FALSE;
7229
Bram Moolenaard61efa52022-07-23 09:52:04 +01007230 filename = dict_get_string(d, "filename", TRUE);
7231 module = dict_get_string(d, "module", TRUE);
7232 bufnum = (int)dict_get_number(d, "bufnr");
7233 lnum = (int)dict_get_number(d, "lnum");
7234 end_lnum = (int)dict_get_number(d, "end_lnum");
7235 col = (int)dict_get_number(d, "col");
7236 end_col = (int)dict_get_number(d, "end_col");
7237 vcol = (int)dict_get_number(d, "vcol");
7238 nr = (int)dict_get_number(d, "nr");
7239 type = dict_get_string(d, "type", TRUE);
7240 pattern = dict_get_string(d, "pattern", TRUE);
7241 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007242 if (text == NULL)
7243 text = vim_strsave((char_u *)"");
7244
7245 valid = TRUE;
7246 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7247 valid = FALSE;
7248
7249 // Mark entries with non-existing buffer number as not valid. Give the
7250 // error message only once.
7251 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7252 {
7253 if (!did_bufnr_emsg)
7254 {
7255 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007256 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007257 }
7258 valid = FALSE;
7259 bufnum = 0;
7260 }
7261
7262 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007263 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007264 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007265
Bram Moolenaar0398e002019-03-21 21:12:49 +01007266 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007267 NULL, // dir
7268 filename,
7269 module,
7270 bufnum,
7271 text,
7272 lnum,
thinca6864efa2021-06-19 20:45:20 +02007273 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007274 col,
thinca6864efa2021-06-19 20:45:20 +02007275 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007276 vcol, // vis_col
7277 pattern, // search pattern
7278 nr,
7279 type == NULL ? NUL : *type,
7280 valid);
7281
7282 vim_free(filename);
7283 vim_free(module);
7284 vim_free(pattern);
7285 vim_free(text);
7286 vim_free(type);
7287
Bram Moolenaar9752c722018-12-22 16:49:34 +01007288 if (valid)
7289 *valid_entry = TRUE;
7290
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007291 return status;
7292}
7293
7294/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007295 * Add list of entries to quickfix/location list. Each list entry is
7296 * a dictionary with item information.
7297 */
7298 static int
7299qf_add_entries(
7300 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007301 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007302 list_T *list,
7303 char_u *title,
7304 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007305{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007306 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007307 listitem_T *li;
7308 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007309 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007310 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007311 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007312
Bram Moolenaara3921f42017-06-04 15:30:34 +02007313 if (action == ' ' || qf_idx == qi->qf_listcount)
7314 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007315 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007316 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007317 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007318 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007319 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007320 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007321 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007322 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007323 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007324 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007325 qf_free_items(qfl);
7326 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007327 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007328
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007329 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007330 {
7331 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007332 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007333
7334 d = li->li_tv.vval.v_dict;
7335 if (d == NULL)
7336 continue;
7337
Bram Moolenaar0398e002019-03-21 21:12:49 +01007338 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007339 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007340 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007341 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007342 }
7343
Bram Moolenaar9752c722018-12-22 16:49:34 +01007344 // Check if any valid error entries are added to the list.
7345 if (valid_entry)
7346 qfl->qf_nonevalid = FALSE;
7347 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007348 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007349 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007350
7351 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007352 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007353 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007354
7355 // Update the current error index if not appending to the list or if the
7356 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007357 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007358 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007359
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007360 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007361 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007362
7363 return retval;
7364}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007365
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007366/*
7367 * Get the quickfix list index from 'nr' or 'id'
7368 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007369 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007370qf_setprop_get_qfidx(
7371 qf_info_T *qi,
7372 dict_T *what,
7373 int action,
7374 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007375{
7376 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007377 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007378
Bram Moolenaard823fa92016-08-12 16:29:27 +02007379 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7380 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007381 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007382 if (di->di_tv.v_type == VAR_NUMBER)
7383 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007384 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007385 if (di->di_tv.vval.v_number != 0)
7386 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007387
Bram Moolenaar55b69262017-08-13 13:42:01 +02007388 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7389 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007390 // When creating a new list, accept qf_idx pointing to the next
7391 // non-available list and add the new list at the end of the
7392 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007393 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007394 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007395 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007396 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007397 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007398 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007399 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007400 }
7401 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007402 && di->di_tv.vval.v_string != NULL
7403 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007404 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007405 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007406 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007407 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007408 qf_idx = 0;
7409 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007410 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007411 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007412 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007413 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007414 }
7415
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007416 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007417 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007418 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007419 if (di->di_tv.v_type != VAR_NUMBER)
7420 return INVALID_QFIDX;
7421
7422 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007423 }
7424
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007425 return qf_idx;
7426}
7427
7428/*
7429 * Set the quickfix list title.
7430 */
7431 static int
7432qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7433{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007434 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007435
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007436 if (di->di_tv.v_type != VAR_STRING)
7437 return FAIL;
7438
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007439 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007440 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007441 if (qf_idx == qi->qf_curlist)
7442 qf_update_win_titlevar(qi);
7443
7444 return OK;
7445}
7446
7447/*
7448 * Set quickfix list items/entries.
7449 */
7450 static int
7451qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7452{
7453 int retval = FAIL;
7454 char_u *title_save;
7455
7456 if (di->di_tv.v_type != VAR_LIST)
7457 return FAIL;
7458
7459 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7460 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7461 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007462 vim_free(title_save);
7463
7464 return retval;
7465}
7466
7467/*
7468 * Set quickfix list items/entries from a list of lines.
7469 */
7470 static int
7471qf_setprop_items_from_lines(
7472 qf_info_T *qi,
7473 int qf_idx,
7474 dict_T *what,
7475 dictitem_T *di,
7476 int action)
7477{
7478 char_u *errorformat = p_efm;
7479 dictitem_T *efm_di;
7480 int retval = FAIL;
7481
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007482 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007483 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7484 {
7485 if (efm_di->di_tv.v_type != VAR_STRING ||
7486 efm_di->di_tv.vval.v_string == NULL)
7487 return FAIL;
7488 errorformat = efm_di->di_tv.vval.v_string;
7489 }
7490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007491 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007492 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7493 return FAIL;
7494
7495 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007496 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007497 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007498 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007499 retval = OK;
7500
7501 return retval;
7502}
7503
7504/*
7505 * Set quickfix list context.
7506 */
7507 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007508qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007509{
7510 typval_T *ctx;
7511
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007512 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007513 ctx = alloc_tv();
7514 if (ctx != NULL)
7515 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007516 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007517
7518 return OK;
7519}
7520
7521/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007522 * Set the current index in the specified quickfix list
7523 */
7524 static int
7525qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7526{
7527 int denote = FALSE;
7528 int newidx;
7529 int old_qfidx;
7530 qfline_T *qf_ptr;
7531
7532 // If the specified index is '$', then use the last entry
7533 if (di->di_tv.v_type == VAR_STRING
7534 && di->di_tv.vval.v_string != NULL
7535 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7536 newidx = qfl->qf_count;
7537 else
7538 {
7539 // Otherwise use the specified index
7540 newidx = tv_get_number_chk(&di->di_tv, &denote);
7541 if (denote)
7542 return FAIL;
7543 }
7544
7545 if (newidx < 1) // sanity check
7546 return FAIL;
7547 if (newidx > qfl->qf_count)
7548 newidx = qfl->qf_count;
7549
7550 old_qfidx = qfl->qf_index;
7551 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7552 if (qf_ptr == NULL)
7553 return FAIL;
7554 qfl->qf_ptr = qf_ptr;
7555 qfl->qf_index = newidx;
7556
7557 // If the current list is modified and it is displayed in the quickfix
7558 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007559 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007560 qf_win_pos_update(qi, old_qfidx);
7561
7562 return OK;
7563}
7564
7565/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007566 * Set the current index in the specified quickfix list
7567 */
7568 static int
7569qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7570{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007571 callback_T cb;
7572
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007573 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007574 cb = get_callback(&di->di_tv);
7575 if (cb.cb_name != NULL && *cb.cb_name != NUL)
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007576 set_callback(&qfl->qf_qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007577
7578 return OK;
7579}
7580
7581/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007582 * Set quickfix/location list properties (title, items, context).
7583 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007584 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007585 */
7586 static int
7587qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7588{
7589 dictitem_T *di;
7590 int retval = FAIL;
7591 int qf_idx;
7592 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007593 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007594
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007595 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007596 newlist = TRUE;
7597
7598 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007599 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007600 return FAIL;
7601
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007602 if (newlist)
7603 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007604 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007605 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007606 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007607 }
7608
Bram Moolenaar0398e002019-03-21 21:12:49 +01007609 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007610 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007611 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007612 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007613 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007614 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007615 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007616 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007617 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007618 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7619 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007620 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7621 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007622
Bram Moolenaar287153c2020-11-29 14:20:27 +01007623 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007624 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007625 if (newlist)
7626 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007627
Bram Moolenaard823fa92016-08-12 16:29:27 +02007628 return retval;
7629}
7630
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007631/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007632 * Free the entire quickfix/location list stack.
7633 * If the quickfix/location list window is open, then clear it.
7634 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007635 static void
7636qf_free_stack(win_T *wp, qf_info_T *qi)
7637{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007638 win_T *qfwin = qf_find_win(qi);
7639 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007640
7641 if (qfwin != NULL)
7642 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007643 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007644 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007645 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007646 qf_update_buffer(qi, NULL);
7647 }
7648
7649 if (wp != NULL && IS_LL_WINDOW(wp))
7650 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007651 // If in the location list window, then use the non-location list
7652 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007653 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007654 if (llwin != NULL)
7655 wp = llwin;
7656 }
7657
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007658 qf_free_all(wp);
7659 if (wp == NULL)
7660 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007661 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007662 qi->qf_curlist = 0;
7663 qi->qf_listcount = 0;
7664 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007665 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007666 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007667 // If the location list window is open, then create a new empty
7668 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007669 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007670
Bram Moolenaar95946f12019-03-31 15:31:59 +02007671 if (new_ll != NULL)
7672 {
7673 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007674
Bram Moolenaar95946f12019-03-31 15:31:59 +02007675 // first free the list reference in the location list window
7676 ll_free_all(&qfwin->w_llist_ref);
7677
7678 qfwin->w_llist_ref = new_ll;
7679 if (wp != qfwin)
7680 win_set_loclist(wp, new_ll);
7681 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007682 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007683}
7684
Bram Moolenaard823fa92016-08-12 16:29:27 +02007685/*
7686 * Populate the quickfix list with the items supplied in the list
7687 * of dictionaries. "title" will be copied to w:quickfix_title.
7688 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007689 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007690 */
7691 int
7692set_errorlist(
7693 win_T *wp,
7694 list_T *list,
7695 int action,
7696 char_u *title,
7697 dict_T *what)
7698{
7699 qf_info_T *qi = &ql_info;
7700 int retval = OK;
7701
7702 if (wp != NULL)
7703 {
7704 qi = ll_get_or_alloc_list(wp);
7705 if (qi == NULL)
7706 return FAIL;
7707 }
7708
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007709 if (action == 'f')
7710 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007711 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007712 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007713 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007714 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007715
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007716 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007717 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007718 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007719 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007720 _("cannot have both a list and a \"what\" argument"));
7721 return FAIL;
7722 }
7723
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007724 incr_quickfix_busy();
7725
7726 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007727 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007728 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007729 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007730 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007731 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007732 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007733 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007734
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007735 decr_quickfix_busy();
7736
Bram Moolenaard823fa92016-08-12 16:29:27 +02007737 return retval;
7738}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007739
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007740/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007741 * Mark the quickfix context and callback function as in use for all the lists
7742 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007743 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007744 static int
7745mark_quickfix_ctx(qf_info_T *qi, int copyID)
7746{
7747 int i;
7748 int abort = FALSE;
7749 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007750 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007751
7752 for (i = 0; i < LISTCOUNT && !abort; ++i)
7753 {
7754 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007755 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7756 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007757 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7758
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007759 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007760 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007761 }
7762
7763 return abort;
7764}
7765
7766/*
7767 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007768 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007769 */
7770 int
7771set_ref_in_quickfix(int copyID)
7772{
7773 int abort = FALSE;
7774 tabpage_T *tp;
7775 win_T *win;
7776
7777 abort = mark_quickfix_ctx(&ql_info, copyID);
7778 if (abort)
7779 return abort;
7780
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007781 abort = set_ref_in_callback(&qftf_cb, copyID);
7782 if (abort)
7783 return abort;
7784
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007785 FOR_ALL_TAB_WINDOWS(tp, win)
7786 {
7787 if (win->w_llist != NULL)
7788 {
7789 abort = mark_quickfix_ctx(win->w_llist, copyID);
7790 if (abort)
7791 return abort;
7792 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007793 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7794 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007795 // In a location list window and none of the other windows is
7796 // referring to this location list. Mark the location list
7797 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007798 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7799 if (abort)
7800 return abort;
7801 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007802 }
7803
7804 return abort;
7805}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007806#endif
7807
Bram Moolenaar81695252004-12-29 20:58:21 +00007808/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007809 * Return the autocmd name for the :cbuffer Ex commands
7810 */
7811 static char_u *
7812cbuffer_get_auname(cmdidx_T cmdidx)
7813{
7814 switch (cmdidx)
7815 {
7816 case CMD_cbuffer: return (char_u *)"cbuffer";
7817 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7818 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7819 case CMD_lbuffer: return (char_u *)"lbuffer";
7820 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7821 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7822 default: return NULL;
7823 }
7824}
7825
7826/*
7827 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7828 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7829 */
7830 static int
7831cbuffer_process_args(
7832 exarg_T *eap,
7833 buf_T **bufp,
7834 linenr_T *line1,
7835 linenr_T *line2)
7836{
7837 buf_T *buf = NULL;
7838
7839 if (*eap->arg == NUL)
7840 buf = curbuf;
7841 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7842 buf = buflist_findnr(atoi((char *)eap->arg));
7843
7844 if (buf == NULL)
7845 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007846 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007847 return FAIL;
7848 }
7849
7850 if (buf->b_ml.ml_mfp == NULL)
7851 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007852 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007853 return FAIL;
7854 }
7855
7856 if (eap->addr_count == 0)
7857 {
7858 eap->line1 = 1;
7859 eap->line2 = buf->b_ml.ml_line_count;
7860 }
7861
7862 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7863 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7864 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007865 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007866 return FAIL;
7867 }
7868
7869 *line1 = eap->line1;
7870 *line2 = eap->line2;
7871 *bufp = buf;
7872
7873 return OK;
7874}
7875
7876/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007877 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007878 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007879 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007880 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007881 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007882 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007883 */
7884 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007885ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007886{
7887 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007888 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007889 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007890 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007891 int_u save_qfid;
7892 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007893 char_u *qf_title;
7894 linenr_T line1;
7895 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007896
Bram Moolenaara16123a2019-03-28 20:31:07 +01007897 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007898 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007899 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007900 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007901#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007902 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007903 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007904#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007905 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007906
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007907 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007908 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7909 if (qi == NULL)
7910 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007911
Bram Moolenaara16123a2019-03-28 20:31:07 +01007912 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7913 return;
7914
7915 qf_title = qf_cmdtitle(*eap->cmdlinep);
7916
7917 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007918 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007919 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7920 (char *)qf_title, (char *)buf->b_sfname);
7921 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007922 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007923
7924 incr_quickfix_busy();
7925
7926 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7927 (eap->cmdidx != CMD_caddbuffer
7928 && eap->cmdidx != CMD_laddbuffer),
7929 line1, line2,
7930 qf_title, NULL);
7931 if (qf_stack_empty(qi))
7932 {
7933 decr_quickfix_busy();
7934 return;
7935 }
7936 if (res >= 0)
7937 qf_list_changed(qf_get_curlist(qi));
7938
7939 // Remember the current quickfix list identifier, so that we can
7940 // check for autocommands changing the current quickfix list.
7941 save_qfid = qf_get_curlist(qi)->qf_id;
7942 if (au_name != NULL)
7943 {
7944 buf_T *curbuf_old = curbuf;
7945
7946 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7947 TRUE, curbuf);
7948 if (curbuf != curbuf_old)
7949 // Autocommands changed buffer, don't jump now, "qi" may
7950 // be invalid.
7951 res = 0;
7952 }
7953 // Jump to the first error for a new list and if autocmds didn't
7954 // free the list.
7955 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7956 eap->cmdidx == CMD_lbuffer)
7957 && qflist_valid(wp, save_qfid))
7958 // display the first error
7959 qf_jump_first(qi, save_qfid, eap->forceit);
7960
7961 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007962}
7963
Bram Moolenaar1e015462005-09-25 22:16:38 +00007964#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007965/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007966 * Return the autocmd name for the :cexpr Ex commands.
7967 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007968 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007969cexpr_get_auname(cmdidx_T cmdidx)
7970{
7971 switch (cmdidx)
7972 {
7973 case CMD_cexpr: return (char_u *)"cexpr";
7974 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7975 case CMD_caddexpr: return (char_u *)"caddexpr";
7976 case CMD_lexpr: return (char_u *)"lexpr";
7977 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7978 case CMD_laddexpr: return (char_u *)"laddexpr";
7979 default: return NULL;
7980 }
7981}
7982
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007983 int
7984trigger_cexpr_autocmd(int cmdidx)
7985{
7986 char_u *au_name = cexpr_get_auname(cmdidx);
7987
7988 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7989 curbuf->b_fname, TRUE, curbuf))
7990 {
7991 if (aborting())
7992 return FAIL;
7993 }
7994 return OK;
7995}
7996
7997 int
7998cexpr_core(exarg_T *eap, typval_T *tv)
7999{
8000 qf_info_T *qi;
8001 win_T *wp = NULL;
8002
8003 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8004 if (qi == NULL)
8005 return FAIL;
8006
8007 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8008 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8009 {
8010 int res;
8011 int_u save_qfid;
8012 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8013
8014 incr_quickfix_busy();
8015 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8016 (eap->cmdidx != CMD_caddexpr
8017 && eap->cmdidx != CMD_laddexpr),
8018 (linenr_T)0, (linenr_T)0,
8019 qf_cmdtitle(*eap->cmdlinep), NULL);
8020 if (qf_stack_empty(qi))
8021 {
8022 decr_quickfix_busy();
8023 return FAIL;
8024 }
8025 if (res >= 0)
8026 qf_list_changed(qf_get_curlist(qi));
8027
8028 // Remember the current quickfix list identifier, so that we can
8029 // check for autocommands changing the current quickfix list.
8030 save_qfid = qf_get_curlist(qi)->qf_id;
8031 if (au_name != NULL)
8032 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8033 curbuf->b_fname, TRUE, curbuf);
8034
8035 // Jump to the first error for a new list and if autocmds didn't
8036 // free the list.
8037 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8038 && qflist_valid(wp, save_qfid))
8039 // display the first error
8040 qf_jump_first(qi, save_qfid, eap->forceit);
8041 decr_quickfix_busy();
8042 return OK;
8043 }
8044
Bram Moolenaar677658a2022-01-05 16:09:06 +00008045 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008046 return FAIL;
8047}
8048
Bram Moolenaara16123a2019-03-28 20:31:07 +01008049/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008050 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8051 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008052 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008053 */
8054 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008055ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008056{
8057 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008058
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008059 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008060 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008061
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008062 // Evaluate the expression. When the result is a string or a list we can
8063 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008064 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008065 if (tv != NULL)
8066 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008067 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008068 free_tv(tv);
8069 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008070}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008071#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008072
8073/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008074 * Get the location list for ":lhelpgrep"
8075 */
8076 static qf_info_T *
8077hgr_get_ll(int *new_ll)
8078{
8079 win_T *wp;
8080 qf_info_T *qi;
8081
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008082 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008083 if (bt_help(curwin->w_buffer))
8084 wp = curwin;
8085 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008086 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008087 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008088
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008089 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008090 qi = NULL;
8091 else
8092 qi = wp->w_llist;
8093
8094 if (qi == NULL)
8095 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008096 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008097 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008098 return NULL;
8099 *new_ll = TRUE;
8100 }
8101
8102 return qi;
8103}
8104
8105/*
8106 * Search for a pattern in a help file.
8107 */
8108 static void
8109hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008110 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008111 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008112 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008113 regmatch_T *p_regmatch)
8114{
8115 FILE *fd;
8116 long lnum;
8117
8118 fd = mch_fopen((char *)fname, "r");
8119 if (fd == NULL)
8120 return;
8121
8122 lnum = 1;
8123 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8124 {
8125 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008126
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008127 // Convert a line if 'encoding' is not utf-8 and
8128 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008129 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008130 {
8131 line = string_convert(p_vc, IObuff, NULL);
8132 if (line == NULL)
8133 line = IObuff;
8134 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008135
8136 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8137 {
8138 int l = (int)STRLEN(line);
8139
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008140 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008141 while (l > 0 && line[l - 1] <= ' ')
8142 line[--l] = NUL;
8143
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008144 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008145 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008146 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008147 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008148 0,
8149 line,
8150 lnum,
thinca6864efa2021-06-19 20:45:20 +02008151 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008152 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008153 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008154 (int)(p_regmatch->endp[0] - line)
8155 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008156 FALSE, // vis_col
8157 NULL, // search pattern
8158 0, // nr
8159 1, // type
8160 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008161 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008162 {
8163 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008164 if (line != IObuff)
8165 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008166 break;
8167 }
8168 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008169 if (line != IObuff)
8170 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008171 ++lnum;
8172 line_breakcheck();
8173 }
8174 fclose(fd);
8175}
8176
8177/*
8178 * Search for a pattern in all the help files in the doc directory under
8179 * the given directory.
8180 */
8181 static void
8182hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008183 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008184 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008185 regmatch_T *p_regmatch,
8186 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008187#ifdef FEAT_MULTI_LANG
8188 , char_u *lang
8189#endif
8190 )
8191{
8192 int fcount;
8193 char_u **fnames;
8194 int fi;
8195
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008196 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008197 add_pathsep(dirname);
8198 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8199 if (gen_expand_wildcards(1, &dirname, &fcount,
8200 &fnames, EW_FILE|EW_SILENT) == OK
8201 && fcount > 0)
8202 {
8203 for (fi = 0; fi < fcount && !got_int; ++fi)
8204 {
8205#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008206 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008207 if (lang != NULL
8208 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008209 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008210 && !(STRNICMP(lang, "en", 2) == 0
8211 && STRNICMP("txt", fnames[fi]
8212 + STRLEN(fnames[fi]) - 3, 3) == 0))
8213 continue;
8214#endif
8215
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008216 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008217 }
8218 FreeWild(fcount, fnames);
8219 }
8220}
8221
8222/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008223 * Search for a pattern in all the help files in the 'runtimepath'
8224 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008225 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008226 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008227 */
8228 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008229hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008230{
8231 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008232
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008233 vimconv_T vc;
8234
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008235 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8236 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008237 vc.vc_type = CONV_NONE;
8238 if (!enc_utf8)
8239 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008240
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008241 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008242 p = p_rtp;
8243 while (*p != NUL && !got_int)
8244 {
8245 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8246
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008247 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008248#ifdef FEAT_MULTI_LANG
8249 , lang
8250#endif
8251 );
8252 }
8253
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008254 if (vc.vc_type != CONV_NONE)
8255 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008256}
8257
8258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 * ":helpgrep {pattern}"
8260 */
8261 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008262ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
8264 regmatch_T regmatch;
8265 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008266 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008267 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008268 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008269 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008270 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008271 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
Bram Moolenaar73633f82012-01-20 13:39:07 +01008273 switch (eap->cmdidx)
8274 {
8275 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8276 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8277 default: break;
8278 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008279 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8280 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008281 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008282#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008283 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008284 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008285#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008286 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008287
Bram Moolenaar39665952018-08-15 20:59:48 +02008288 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008289 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008290 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008291 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008292 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008293 }
8294
Bram Moolenaara16123a2019-03-28 20:31:07 +01008295 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8296 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008297 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008298 p_cpo = empty_option;
8299
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008300 incr_quickfix_busy();
8301
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008302#ifdef FEAT_MULTI_LANG
8303 // Check for a specified language
8304 lang = check_help_lang(eap->arg);
8305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8307 regmatch.rm_ic = FALSE;
8308 if (regmatch.regprog != NULL)
8309 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008310 qf_list_T *qfl;
8311
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008312 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008313 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008314 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008316 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008317
Bram Moolenaar473de612013-06-08 18:19:48 +02008318 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008320 qfl->qf_nonevalid = FALSE;
8321 qfl->qf_ptr = qfl->qf_start;
8322 qfl->qf_index = 1;
8323 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008324 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 }
8326
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008327 if (p_cpo == empty_option)
8328 p_cpo = save_cpo;
8329 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008330 {
8331 // Darn, some plugin changed the value. If it's still empty it was
8332 // changed and restored, need to restore in the complicated way.
8333 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008334 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008335 if (save_cpo_allocated)
8336 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008337 }
8338
8339 if (updated)
8340 // This may open a window and source scripts, do this after 'cpo' was
8341 // restored.
8342 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343
Bram Moolenaar73633f82012-01-20 13:39:07 +01008344 if (au_name != NULL)
8345 {
8346 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8347 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008348 // When adding a location list to an existing location list stack,
8349 // if the autocmd made the stack invalid, then just return.
8350 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008351 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008352 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008353 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008354 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008355 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008356
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008357 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008358 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008359 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008360 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008361 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008362
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008363 decr_quickfix_busy();
8364
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008365 if (eap->cmdidx == CMD_lhelpgrep)
8366 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008367 // If the help window is not opened or if it already points to the
8368 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008369 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008370 {
8371 if (new_qi)
8372 ll_free_all(&qi);
8373 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008374 else if (curwin->w_llist == NULL && new_qi)
8375 // current window didn't have a location list associated with it
8376 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008377 curwin->w_llist = qi;
8378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008380#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008381
8382#if defined(FEAT_EVAL) || defined(PROTO)
8383# ifdef FEAT_QUICKFIX
8384 static void
8385get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8386{
8387 if (what_arg->v_type == VAR_UNKNOWN)
8388 {
8389 if (rettv_list_alloc(rettv) == OK)
8390 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008391 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008392 }
8393 else
8394 {
8395 if (rettv_dict_alloc(rettv) == OK)
8396 if (is_qf || (wp != NULL))
8397 {
8398 if (what_arg->v_type == VAR_DICT)
8399 {
8400 dict_T *d = what_arg->vval.v_dict;
8401
8402 if (d != NULL)
8403 qf_get_properties(wp, d, rettv->vval.v_dict);
8404 }
8405 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008406 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008407 }
8408 }
8409}
8410# endif
8411
8412/*
8413 * "getloclist()" function
8414 */
8415 void
8416f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8417{
8418# ifdef FEAT_QUICKFIX
8419 win_T *wp;
8420
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008421 if (in_vim9script()
8422 && (check_for_number_arg(argvars, 0) == FAIL
8423 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8424 return;
8425
Bram Moolenaare677df82019-09-02 22:31:11 +02008426 wp = find_win_by_nr_or_id(&argvars[0]);
8427 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8428# endif
8429}
8430
8431/*
8432 * "getqflist()" function
8433 */
8434 void
8435f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8436{
8437# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008438 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8439 return;
8440
Bram Moolenaare677df82019-09-02 22:31:11 +02008441 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8442# endif
8443}
8444
8445/*
8446 * Used by "setqflist()" and "setloclist()" functions
8447 */
8448 static void
8449set_qf_ll_list(
8450 win_T *wp UNUSED,
8451 typval_T *list_arg UNUSED,
8452 typval_T *action_arg UNUSED,
8453 typval_T *what_arg UNUSED,
8454 typval_T *rettv)
8455{
8456# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008457 char_u *act;
8458 int action = 0;
8459 static int recursive = 0;
8460# endif
8461
8462 rettv->vval.v_number = -1;
8463
8464# ifdef FEAT_QUICKFIX
8465 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008466 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008467 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008468 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008469 else
8470 {
8471 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008472 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008473 int valid_dict = TRUE;
8474
8475 if (action_arg->v_type == VAR_STRING)
8476 {
8477 act = tv_get_string_chk(action_arg);
8478 if (act == NULL)
8479 return; // type error; errmsg already given
8480 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8481 act[1] == NUL)
8482 action = *act;
8483 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008484 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008485 }
8486 else if (action_arg->v_type == VAR_UNKNOWN)
8487 action = ' ';
8488 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008489 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008490
8491 if (action_arg->v_type != VAR_UNKNOWN
8492 && what_arg->v_type != VAR_UNKNOWN)
8493 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008494 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8495 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008496 else
8497 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008498 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008499 valid_dict = FALSE;
8500 }
8501 }
8502
8503 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008504 if (l != NULL && action && valid_dict
8505 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008506 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008507 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008508 rettv->vval.v_number = 0;
8509 --recursive;
8510 }
8511# endif
8512}
8513
8514/*
8515 * "setloclist()" function
8516 */
8517 void
8518f_setloclist(typval_T *argvars, typval_T *rettv)
8519{
8520 win_T *win;
8521
8522 rettv->vval.v_number = -1;
8523
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008524 if (in_vim9script()
8525 && (check_for_number_arg(argvars, 0) == FAIL
8526 || check_for_list_arg(argvars, 1) == FAIL
8527 || check_for_opt_string_arg(argvars, 2) == FAIL
8528 || (argvars[2].v_type != VAR_UNKNOWN
8529 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8530 return;
8531
Bram Moolenaare677df82019-09-02 22:31:11 +02008532 win = find_win_by_nr_or_id(&argvars[0]);
8533 if (win != NULL)
8534 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8535}
8536
8537/*
8538 * "setqflist()" function
8539 */
8540 void
8541f_setqflist(typval_T *argvars, typval_T *rettv)
8542{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008543 if (in_vim9script()
8544 && (check_for_list_arg(argvars, 0) == FAIL
8545 || check_for_opt_string_arg(argvars, 1) == FAIL
8546 || (argvars[1].v_type != VAR_UNKNOWN
8547 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8548 return;
8549
Bram Moolenaare677df82019-09-02 22:31:11 +02008550 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8551}
8552#endif