blob: 49484e0602a13d8d03b59e9fa65d6e5fd015048a [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 Moolenaar00bf8cd2018-10-07 20:26:20 +02003625 // Remove newlines and leading whitespace from the text. For an
3626 // unrecognized line keep the indent, the compiler may mark a word
3627 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003628 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3629 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3630 IObuff, IOSIZE);
3631 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003632 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003633}
3634
3635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003637 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 */
3639 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003640qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003642 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003643 qfline_T *qfp;
3644 int i;
3645 int idx1 = 1;
3646 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003647 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003648 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003649 int all = eap->forceit; // if not :cl!, only show
3650 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003651 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003653 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3654 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003655
Bram Moolenaar0398e002019-03-21 21:12:49 +01003656 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003658 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return;
3660 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003661 if (*arg == '+')
3662 {
3663 ++arg;
3664 plus = TRUE;
3665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3667 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003668 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 return;
3670 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003671 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003672 if (plus)
3673 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003674 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003675 idx2 = i + idx1;
3676 idx1 = i;
3677 }
3678 else
3679 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003680 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003681 if (idx1 < 0)
3682 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3683 if (idx2 < 0)
3684 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003687 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003688 shorten_fnames(FALSE);
3689
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003690 // Get the attributes for the different quickfix highlight items. Note
3691 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003692 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3693 if (qfFileAttr == 0)
3694 qfFileAttr = HL_ATTR(HLF_D);
3695 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3696 if (qfSepAttr == 0)
3697 qfSepAttr = HL_ATTR(HLF_D);
3698 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3699 if (qfLineAttr == 0)
3700 qfLineAttr = HL_ATTR(HLF_N);
3701
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003702 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003704 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
3706 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003707 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 ui_breakcheck();
3710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711}
3712
3713/*
3714 * Remove newlines and leading whitespace from an error message.
3715 * Put the result in "buf[bufsize]".
3716 */
3717 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003718qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
3720 int i;
3721 char_u *p = text;
3722
3723 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3724 {
3725 if (*p == '\n')
3726 {
3727 buf[i] = ' ';
3728 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003729 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 break;
3731 }
3732 else
3733 buf[i] = *p++;
3734 }
3735 buf[i] = NUL;
3736}
3737
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003738/*
thinca6864efa2021-06-19 20:45:20 +02003739 * Range information from lnum, col, end_lnum, and end_col.
3740 * Put the result in "buf[bufsize]".
3741 */
3742 static void
3743qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3744{
3745 int len;
3746 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3747 len = (int)STRLEN(buf);
3748
3749 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3750 {
3751 vim_snprintf((char *)buf + len, bufsize - len,
3752 "-%ld", qfp->qf_end_lnum);
3753 len += (int)STRLEN(buf + len);
3754 }
3755 if (qfp->qf_col > 0)
3756 {
3757 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3758 len += (int)STRLEN(buf + len);
3759 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3760 {
3761 vim_snprintf((char *)buf + len, bufsize - len,
3762 "-%d", qfp->qf_end_col);
3763 len += (int)STRLEN(buf + len);
3764 }
3765 }
3766 buf[len] = NUL;
3767}
3768
3769/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003770 * Display information (list number, list size and the title) about a
3771 * quickfix/location list.
3772 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003773 static void
3774qf_msg(qf_info_T *qi, int which, char *lead)
3775{
3776 char *title = (char *)qi->qf_lists[which].qf_title;
3777 int count = qi->qf_lists[which].qf_count;
3778 char_u buf[IOSIZE];
3779
3780 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3781 lead,
3782 which + 1,
3783 qi->qf_listcount,
3784 count);
3785
3786 if (title != NULL)
3787 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003788 size_t len = STRLEN(buf);
3789
3790 if (len < 34)
3791 {
3792 vim_memset(buf + len, ' ', 34 - len);
3793 buf[34] = NUL;
3794 }
3795 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003796 }
3797 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003798 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003799}
3800
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801/*
3802 * ":colder [count]": Up in the quickfix stack.
3803 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003804 * ":lolder [count]": Up in the location list stack.
3805 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 */
3807 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003808qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003810 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int count;
3812
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003813 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3814 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 if (eap->addr_count != 0)
3817 count = eap->line2;
3818 else
3819 count = 1;
3820 while (count--)
3821 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003822 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003824 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003826 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003827 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003829 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831 else
3832 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003833 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003835 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003836 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003838 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003841 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003842 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843}
3844
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003845/*
3846 * Display the information about all the quickfix/location lists in the stack
3847 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003848 void
3849qf_history(exarg_T *eap)
3850{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003851 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003852 int i;
3853
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003854 if (eap->addr_count > 0)
3855 {
3856 if (qi == NULL)
3857 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003858 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003859 return;
3860 }
3861
3862 // Jump to the specified quickfix list
3863 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3864 {
3865 qi->qf_curlist = eap->line2 - 1;
3866 qf_msg(qi, qi->qf_curlist, "");
3867 qf_update_buffer(qi, NULL);
3868 }
3869 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003870 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003871
3872 return;
3873 }
3874
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003875 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003876 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003877 else
3878 for (i = 0; i < qi->qf_listcount; ++i)
3879 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3880}
3881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003883 * Free all the entries in the error list "idx". Note that other information
3884 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 */
3886 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003887qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003889 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003890 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003891 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003893 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003895 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003896 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003897 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003898 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003899 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003900 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003901 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003902 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003903 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003904 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003905 // Somehow qf_count may have an incorrect value, set it to 1
3906 // to avoid crashing when it's wrong.
3907 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003908 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003909 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003910 qfl->qf_start = qfpnext;
3911 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003913
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003914 qfl->qf_index = 0;
3915 qfl->qf_start = NULL;
3916 qfl->qf_last = NULL;
3917 qfl->qf_ptr = NULL;
3918 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003919
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003920 qf_clean_dir_stack(&qfl->qf_dir_stack);
3921 qfl->qf_directory = NULL;
3922 qf_clean_dir_stack(&qfl->qf_file_stack);
3923 qfl->qf_currfile = NULL;
3924 qfl->qf_multiline = FALSE;
3925 qfl->qf_multiignore = FALSE;
3926 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927}
3928
3929/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003930 * Free error list "idx". Frees all the entries in the quickfix list,
3931 * associated context information and the title.
3932 */
3933 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003934qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003935{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003936 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003937
Bram Moolenaard23a8232018-02-10 18:45:26 +01003938 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003939 free_tv(qfl->qf_ctx);
3940 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01003941 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003942 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003943 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003944}
3945
3946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 * qf_mark_adjust: adjust marks
3948 */
3949 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003950qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003951 win_T *wp,
3952 linenr_T line1,
3953 linenr_T line2,
3954 long amount,
3955 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003957 int i;
3958 qfline_T *qfp;
3959 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003960 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003961 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003962 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaarc1542742016-07-20 21:44:37 +02003964 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003965 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003966 if (wp != NULL)
3967 {
3968 if (wp->w_llist == NULL)
3969 return;
3970 qi = wp->w_llist;
3971 }
3972
3973 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003974 {
3975 qf_list_T *qfl = qf_get_list(qi, idx);
3976
3977 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003978 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (qfp->qf_fnum == curbuf->b_fnum)
3980 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003981 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3983 {
3984 if (amount == MAXLNUM)
3985 qfp->qf_cleared = TRUE;
3986 else
3987 qfp->qf_lnum += amount;
3988 }
3989 else if (amount_after && qfp->qf_lnum > line2)
3990 qfp->qf_lnum += amount_after;
3991 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003992 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003993
3994 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003995 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
3998/*
3999 * Make a nice message out of the error character and the error number:
4000 * char number message
4001 * e or E 0 " error"
4002 * w or W 0 " warning"
4003 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004004 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * 0 0 ""
4006 * other 0 " c"
4007 * e or E n " error n"
4008 * w or W n " warning n"
4009 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004010 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 * 0 n " error n"
4012 * other n " c n"
4013 * 1 x "" :helpgrep
4014 */
4015 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004016qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
4018 static char_u buf[20];
4019 static char_u cc[3];
4020 char_u *p;
4021
4022 if (c == 'W' || c == 'w')
4023 p = (char_u *)" warning";
4024 else if (c == 'I' || c == 'i')
4025 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004026 else if (c == 'N' || c == 'n')
4027 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4029 p = (char_u *)" error";
4030 else if (c == 0 || c == 1)
4031 p = (char_u *)"";
4032 else
4033 {
4034 cc[0] = ' ';
4035 cc[1] = c;
4036 cc[2] = NUL;
4037 p = cc;
4038 }
4039
4040 if (nr <= 0)
4041 return p;
4042
4043 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4044 return buf;
4045}
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004048 * When "split" is FALSE: Open the entry/result under the cursor.
4049 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4050 */
4051 void
4052qf_view_result(int split)
4053{
4054 qf_info_T *qi = &ql_info;
4055
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004056 if (IS_LL_WINDOW(curwin))
4057 qi = GET_LOC_LIST(curwin);
4058
Bram Moolenaar0398e002019-03-21 21:12:49 +01004059 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004060 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004061 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004062 return;
4063 }
4064
4065 if (split)
4066 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004067 // Open the selected entry in a new window
4068 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4069 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004070 return;
4071 }
4072
4073 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4074}
4075
4076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 * ":cwindow": open the quickfix window if we have errors to display,
4078 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004079 * ":lwindow": open the location list window if we have locations to display,
4080 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 */
4082 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004083ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004085 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004086 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 win_T *win;
4088
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004089 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4090 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004091
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004092 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004093
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004094 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004095 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004097 // If a quickfix window is open but we have no errors to display,
4098 // close the window. If a quickfix window is not open, then open
4099 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004100 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004101 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004102 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
4104 if (win != NULL)
4105 ex_cclose(eap);
4106 }
4107 else if (win == NULL)
4108 ex_copen(eap);
4109}
4110
4111/*
4112 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004113 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004116ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004118 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004119 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004121 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4122 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004124 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004125 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (win != NULL)
4127 win_close(win, FALSE);
4128}
4129
4130/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004131 * Set "w:quickfix_title" if "qi" has a title.
4132 */
4133 static void
4134qf_set_title_var(qf_list_T *qfl)
4135{
4136 if (qfl->qf_title != NULL)
4137 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4138}
4139
4140/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004141 * Goto a quickfix or location list window (if present).
4142 * Returns OK if the window is found, FAIL otherwise.
4143 */
4144 static int
4145qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4146{
4147 win_T *win;
4148
4149 win = qf_find_win(qi);
4150 if (win == NULL)
4151 return FAIL;
4152
4153 win_goto(win);
4154 if (resize)
4155 {
4156 if (vertsplit)
4157 {
4158 if (sz != win->w_width)
4159 win_setwidth(sz);
4160 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004161 else if (sz != win->w_height && win->w_height
4162 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004163 win_setheight(sz);
4164 }
4165
4166 return OK;
4167}
4168
4169/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004170 * Set options for the buffer in the quickfix or location list window.
4171 */
4172 static void
4173qf_set_cwindow_options(void)
4174{
4175 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004176 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4177 set_option_value_give_err((char_u *)"bt",
4178 0L, (char_u *)"quickfix", OPT_LOCAL);
4179 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004180 RESET_BINDING(curwin);
4181#ifdef FEAT_DIFF
4182 curwin->w_p_diff = FALSE;
4183#endif
4184#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004185 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004186 OPT_LOCAL);
4187#endif
4188}
4189
4190/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004191 * Open a new quickfix or location list window, load the quickfix buffer and
4192 * set the appropriate options for the window.
4193 * Returns FAIL if the window could not be opened.
4194 */
4195 static int
4196qf_open_new_cwindow(qf_info_T *qi, int height)
4197{
4198 buf_T *qf_buf;
4199 win_T *oldwin = curwin;
4200 tabpage_T *prevtab = curtab;
4201 int flags = 0;
4202 win_T *win;
4203
4204 qf_buf = qf_find_buf(qi);
4205
4206 // The current window becomes the previous window afterwards.
4207 win = curwin;
4208
Bram Moolenaare1004402020-10-24 20:49:43 +02004209 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004210 // Create the new quickfix window at the very bottom, except when
4211 // :belowright or :aboveleft is used.
4212 win_goto(lastwin);
4213 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004214 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004215 flags = WSP_BELOW;
4216 flags |= WSP_NEWLOC;
4217 if (win_split(height, flags) == FAIL)
4218 return FAIL; // not enough room for window
4219 RESET_BINDING(curwin);
4220
4221 if (IS_LL_STACK(qi))
4222 {
4223 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004224 // location list stack from the window 'win'.
4225 curwin->w_llist_ref = qi;
4226 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004227 }
4228
4229 if (oldwin != curwin)
4230 oldwin = NULL; // don't store info when in another window
4231 if (qf_buf != NULL)
4232 {
4233 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004234 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004235 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004236 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004237 }
4238 else
4239 {
4240 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004241 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4242 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004243 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004244
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004245 // save the number of the new buffer
4246 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004247 }
4248
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004249 // Set the options for the quickfix buffer/window (if not already done)
4250 // Do this even if the quickfix buffer was already present, as an autocmd
4251 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004252 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004253 qf_set_cwindow_options();
4254
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004255 // Only set the height when still in the same tab page and there is no
4256 // window to the side.
4257 if (curtab == prevtab && curwin->w_width == Columns)
4258 win_setheight(height);
4259 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4260 if (win_valid(win))
4261 prevwin = win;
4262
4263 return OK;
4264}
4265
4266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004268 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 */
4270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004271ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004273 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004274 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004276 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004277 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004279 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4280 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004281
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004282 incr_quickfix_busy();
4283
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (eap->addr_count != 0)
4285 height = eap->line2;
4286 else
4287 height = QF_WINHEIGHT;
4288
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004289 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290#ifdef FEAT_GUI
4291 need_mouse_correct = TRUE;
4292#endif
4293
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004294 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004295 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004296 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004297 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004298 if (status == FAIL)
4299 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004300 {
4301 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004302 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004305 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004306 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004307 // Save the current index here, as updating the quickfix buffer may free
4308 // the quickfix list
4309 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004310
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004311 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004312 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004314 decr_quickfix_busy();
4315
4316 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 curwin->w_cursor.col = 0;
4318 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004319 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320}
4321
4322/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004323 * Move the cursor in the quickfix window to "lnum".
4324 */
4325 static void
4326qf_win_goto(win_T *win, linenr_T lnum)
4327{
4328 win_T *old_curwin = curwin;
4329
4330 curwin = win;
4331 curbuf = win->w_buffer;
4332 curwin->w_cursor.lnum = lnum;
4333 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004334 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004335 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004336 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004337 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004338 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004339 curwin = old_curwin;
4340 curbuf = curwin->w_buffer;
4341}
4342
4343/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004344 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004345 */
4346 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004347ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004348{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004349 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004350 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004351
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004352 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4353 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004354
4355 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004356 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4357 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4358}
4359
4360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 * Return the number of the current entry (line number in the quickfix
4362 * window).
4363 */
4364 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004365qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004367 qf_info_T *qi = &ql_info;
4368
4369 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004370 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004371 qi = wp->w_llist_ref;
4372
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004373 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374}
4375
4376/*
4377 * Update the cursor position in the quickfix window to the current error.
4378 * Return TRUE if there is a quickfix window.
4379 */
4380 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004381qf_win_pos_update(
4382 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004383 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004386 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004388 // Put the cursor on the current error in the quickfix window, so that
4389 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004390 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 if (win != NULL
4392 && qf_index <= win->w_buffer->b_ml.ml_line_count
4393 && old_qf_index != qf_index)
4394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (qf_index > old_qf_index)
4396 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004397 win->w_redraw_top = old_qf_index;
4398 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
4400 else
4401 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004402 win->w_redraw_top = qf_index;
4403 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004405 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407 return win != NULL;
4408}
4409
4410/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004411 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004412 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004413 */
4414 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004415is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004416{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004417 // A window displaying the quickfix buffer will have the w_llist_ref field
4418 // set to NULL.
4419 // A window displaying a location list buffer will have the w_llist_ref
4420 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004421 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004422 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4423 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004424 return TRUE;
4425
4426 return FALSE;
4427}
4428
4429/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004430 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4431 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004432 */
4433 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004434qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004435{
4436 win_T *win;
4437
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004438 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004439 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004440 return win;
4441 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004442}
4443
4444/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004445 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004446 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 */
4448 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004449qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004451 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004452 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004454 if (qi->qf_bufnr != INVALID_QFBUFNR)
4455 {
4456 buf_T *qfbuf;
4457 qfbuf = buflist_findnr(qi->qf_bufnr);
4458 if (qfbuf != NULL)
4459 return qfbuf;
4460 // buffer is no longer present
4461 qi->qf_bufnr = INVALID_QFBUFNR;
4462 }
4463
Bram Moolenaar9c102382006-05-03 21:26:49 +00004464 FOR_ALL_TAB_WINDOWS(tp, win)
4465 if (is_qf_win(win, qi))
4466 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004467
Bram Moolenaar9c102382006-05-03 21:26:49 +00004468 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469}
4470
4471/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004472 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004473 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004474 */
4475 int
4476qf_process_qftf_option(void)
4477{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004478 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004479}
4480
4481/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004482 * Update the w:quickfix_title variable in the quickfix/location list window in
4483 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004484 */
4485 static void
4486qf_update_win_titlevar(qf_info_T *qi)
4487{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004488 qf_list_T *qfl = qf_get_curlist(qi);
4489 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004490 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004491 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004492
Bram Moolenaar530bed92020-12-16 21:02:56 +01004493 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004494 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004495 if (is_qf_win(win, qi))
4496 {
4497 curwin = win;
4498 qf_set_title_var(qfl);
4499 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004500 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004501 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004502}
4503
4504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 * Find the quickfix buffer. If it exists, update the contents.
4506 */
4507 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004508qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004511 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004514 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004515 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 if (buf != NULL)
4517 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004518 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004519 int qf_winid = 0;
4520
4521 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004522 {
4523 if (curwin->w_llist == qi)
4524 win = curwin;
4525 else
4526 {
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004527 // Find the file window (non-quickfix) with this location list
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004528 win = qf_find_win_with_loclist(qi);
4529 if (win == NULL)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004530 // File window is not found. Find the location list window.
4531 win = qf_find_win(qi);
4532 if (win == NULL)
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004533 return;
4534 }
4535 qf_winid = win->w_id;
4536 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004537
4538 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004539 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004540 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
Bram Moolenaard823fa92016-08-12 16:29:27 +02004542 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004543
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004544 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004545 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004546
Bram Moolenaar864293a2016-06-02 13:40:04 +02004547 if (old_last == NULL)
4548 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004549 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004550
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004551 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004552 aucmd_restbuf(&aco);
4553 }
4554
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004555 // Only redraw when added lines are visible. This avoids flickering
4556 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004557 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4558 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560}
4561
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004562/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004563 * Add an error line to the quickfix buffer.
4564 */
4565 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004566qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004567 buf_T *buf, // quickfix window buffer
4568 linenr_T lnum,
4569 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004570 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004571 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004572 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004573{
4574 int len;
4575 buf_T *errbuf;
4576
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004577 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004578 // for this entry, then use it.
4579 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004580 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004581 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004582 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004583 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004584 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004585 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4586 len = (int)STRLEN(IObuff);
4587 }
4588 else if (qfp->qf_fnum != 0
4589 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4590 && errbuf->b_fname != NULL)
4591 {
4592 if (qfp->qf_type == 1) // :helpgrep
4593 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4594 else
4595 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004596 // Shorten the file name if not done already.
4597 // For optimization, do this only for the first entry in a
4598 // buffer.
4599 if (first_bufline && (errbuf->b_sfname == NULL
4600 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004601 {
4602 if (*dirname == NUL)
4603 mch_dirname(dirname, MAXPATHL);
4604 shorten_buf_fname(errbuf, dirname, FALSE);
4605 }
4606 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4607 }
4608 len = (int)STRLEN(IObuff);
4609 }
4610 else
4611 len = 0;
4612
4613 if (len < IOSIZE - 1)
4614 IObuff[len++] = '|';
4615
4616 if (qfp->qf_lnum > 0)
4617 {
thinca6864efa2021-06-19 20:45:20 +02004618 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004619 len += (int)STRLEN(IObuff + len);
4620
Bram Moolenaar858ba062020-05-31 23:11:59 +02004621 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4622 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004623 len += (int)STRLEN(IObuff + len);
4624 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004625 else if (qfp->qf_pattern != NULL)
4626 {
4627 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4628 len += (int)STRLEN(IObuff + len);
4629 }
4630 if (len < IOSIZE - 2)
4631 {
4632 IObuff[len++] = '|';
4633 IObuff[len++] = ' ';
4634 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004635
Bram Moolenaar858ba062020-05-31 23:11:59 +02004636 // Remove newlines and leading whitespace from the text.
4637 // For an unrecognized line keep the indent, the compiler may
4638 // mark a word with ^^^^.
4639 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4640 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004641 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004642
4643 if (ml_append_buf(buf, lnum, IObuff,
4644 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4645 return FAIL;
4646
4647 return OK;
4648}
4649
Bram Moolenaard43906d2020-07-20 21:31:32 +02004650/*
4651 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4652 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4653 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004654 static list_T *
4655call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4656{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004657 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004658 list_T *qftf_list = NULL;
4659
4660 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4661 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4662 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004663 if (qfl->qf_qftf_cb.cb_name != NULL)
4664 cb = &qfl->qf_qftf_cb;
4665 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004666 {
4667 typval_T args[1];
4668 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004669 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004670
4671 // create the dict argument
4672 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4673 return NULL;
4674 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4675 dict_add_number(d, "winid", (long)qf_winid);
4676 dict_add_number(d, "id", (long)qfl->qf_id);
4677 dict_add_number(d, "start_idx", start_idx);
4678 dict_add_number(d, "end_idx", end_idx);
4679 ++d->dv_refcount;
4680 args[0].v_type = VAR_DICT;
4681 args[0].vval.v_dict = d;
4682
Bram Moolenaard43906d2020-07-20 21:31:32 +02004683 qftf_list = NULL;
4684 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4685 {
4686 if (rettv.v_type == VAR_LIST)
4687 {
4688 qftf_list = rettv.vval.v_list;
4689 qftf_list->lv_refcount++;
4690 }
4691 clear_tv(&rettv);
4692 }
4693 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004694 }
4695
4696 return qftf_list;
4697}
4698
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 * Fill current buffer with quickfix errors, replacing any previous contents.
4701 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004702 * If "old_last" is not NULL append the items after this one.
4703 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4704 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 */
4706 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004707qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004709 linenr_T lnum;
4710 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004711 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004712 list_T *qftf_list = NULL;
4713 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714
Bram Moolenaar864293a2016-06-02 13:40:04 +02004715 if (old_last == NULL)
4716 {
4717 if (buf != curbuf)
4718 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004719 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004720 return;
4721 }
4722
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004723 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004724 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004725 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004728 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004729 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004731 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004732 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004733 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004734
4735 *dirname = NUL;
4736
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004737 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004738 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004739 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004740 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004741 lnum = 0;
4742 }
4743 else
4744 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004745 if (old_last->qf_next != NULL)
4746 qfp = old_last->qf_next;
4747 else
4748 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004749 lnum = buf->b_ml.ml_line_count;
4750 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004751
4752 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4753 (long)qfl->qf_count);
4754 if (qftf_list != NULL)
4755 qftf_li = qftf_list->lv_first;
4756
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004757 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004759 char_u *qftf_str = NULL;
4760
Bram Moolenaard43906d2020-07-20 21:31:32 +02004761 // Use the text supplied by the user defined function (if any).
4762 // If the returned value is not string, then ignore the rest
4763 // of the returned values and use the default.
4764 if (qftf_li != NULL && !invalid_val)
4765 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004766 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004767 if (qftf_str == NULL)
4768 invalid_val = TRUE;
4769 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004770
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004771 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4772 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004774
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004775 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004776 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004778 if (qfp == NULL)
4779 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004780
4781 if (qftf_li != NULL)
4782 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004784
4785 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004786 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004787 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004790 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 check_lnums(TRUE);
4792
Bram Moolenaar864293a2016-06-02 13:40:04 +02004793 if (old_last == NULL)
4794 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004795 // Set the 'filetype' to "qf" each time after filling the buffer.
4796 // This resembles reading a file into a buffer, it's more logical when
4797 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004798 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004799 set_option_value_give_err((char_u *)"ft",
4800 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004801 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004803 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004804 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004806 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004808 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004809 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004810
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004811 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004812 redraw_curbuf_later(NOT_VALID);
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004815 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 KeyTyped = old_KeyTyped;
4817}
4818
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004819/*
4820 * For every change made to the quickfix list, update the changed tick.
4821 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004822 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004823qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004824{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004825 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004826}
4827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004829 * Return the quickfix/location list number with the given identifier.
4830 * Returns -1 if list is not found.
4831 */
4832 static int
4833qf_id2nr(qf_info_T *qi, int_u qfid)
4834{
4835 int qf_idx;
4836
4837 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4838 if (qi->qf_lists[qf_idx].qf_id == qfid)
4839 return qf_idx;
4840 return INVALID_QFIDX;
4841}
4842
4843/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004844 * If the current list is not "save_qfid" and we can find the list with that ID
4845 * then make it the current list.
4846 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004847 * Returns OK if successfully restored the list. Returns FAIL if the list with
4848 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004849 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004850 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004851qf_restore_list(qf_info_T *qi, int_u save_qfid)
4852{
4853 int curlist;
4854
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004855 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004856 {
4857 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004858 if (curlist < 0)
4859 // list is not present
4860 return FAIL;
4861 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004862 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004863 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004864}
4865
4866/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004867 * Jump to the first entry if there is one.
4868 */
4869 static void
4870qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4871{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004872 if (qf_restore_list(qi, save_qfid) == FAIL)
4873 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004874
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004875 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004876 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004877 qf_jump(qi, 0, 0, forceit);
4878}
4879
4880/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004881 * Return TRUE when using ":vimgrep" for ":grep".
4882 */
4883 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004884grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004885{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004886 return ((cmdidx == CMD_grep
4887 || cmdidx == CMD_lgrep
4888 || cmdidx == CMD_grepadd
4889 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004890 && STRCMP("internal",
4891 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4892}
4893
4894/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004895 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004897 static char_u *
4898make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004900 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004901 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004902 case CMD_make: return (char_u *)"make";
4903 case CMD_lmake: return (char_u *)"lmake";
4904 case CMD_grep: return (char_u *)"grep";
4905 case CMD_lgrep: return (char_u *)"lgrep";
4906 case CMD_grepadd: return (char_u *)"grepadd";
4907 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4908 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910}
4911
4912/*
4913 * Return the name for the errorfile, in allocated memory.
4914 * Find a new unique name when 'makeef' contains "##".
4915 * Returns NULL for error.
4916 */
4917 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004918get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
4920 char_u *p;
4921 char_u *name;
4922 static int start = -1;
4923 static int off = 0;
4924#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004925 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926#endif
4927
4928 if (*p_mef == NUL)
4929 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004930 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004932 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 return name;
4934 }
4935
4936 for (p = p_mef; *p; ++p)
4937 if (p[0] == '#' && p[1] == '#')
4938 break;
4939
4940 if (*p == NUL)
4941 return vim_strsave(p_mef);
4942
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004943 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 for (;;)
4945 {
4946 if (start == -1)
4947 start = mch_get_pid();
4948 else
4949 off += 19;
4950
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004951 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 if (name == NULL)
4953 break;
4954 STRCPY(name, p_mef);
4955 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4956 STRCAT(name, p + 2);
4957 if (mch_getperm(name) < 0
4958#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004959 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 && mch_lstat((char *)name, &sb) < 0
4961#endif
4962 )
4963 break;
4964 vim_free(name);
4965 }
4966 return name;
4967}
4968
4969/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004970 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4971 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4972 */
4973 static char_u *
4974make_get_fullcmd(char_u *makecmd, char_u *fname)
4975{
4976 char_u *cmd;
4977 unsigned len;
4978
4979 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4980 if (*p_sp != NUL)
4981 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004982 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004983 if (cmd == NULL)
4984 return NULL;
4985 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4986 (char *)p_shq);
4987
4988 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4989 if (*p_sp != NUL)
4990 append_redir(cmd, len, p_sp, fname);
4991
4992 // Display the fully formed command. Output a newline if there's something
4993 // else than the :make command that was typed (in which case the cursor is
4994 // in column 0).
4995 if (msg_col == 0)
4996 msg_didout = FALSE;
4997 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004998 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004999 msg_outtrans(cmd); // show what we are doing
5000
5001 return cmd;
5002}
5003
5004/*
5005 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5006 */
5007 void
5008ex_make(exarg_T *eap)
5009{
5010 char_u *fname;
5011 char_u *cmd;
5012 char_u *enc = NULL;
5013 win_T *wp = NULL;
5014 qf_info_T *qi = &ql_info;
5015 int res;
5016 char_u *au_name = NULL;
5017 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005018 char_u *errorformat = p_efm;
5019 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005020
5021 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5022 if (grep_internal(eap->cmdidx))
5023 {
5024 ex_vimgrep(eap);
5025 return;
5026 }
5027
5028 au_name = make_get_auname(eap->cmdidx);
5029 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5030 curbuf->b_fname, TRUE, curbuf))
5031 {
5032#ifdef FEAT_EVAL
5033 if (aborting())
5034 return;
5035#endif
5036 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005037 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005038
5039 if (is_loclist_cmd(eap->cmdidx))
5040 wp = curwin;
5041
5042 autowrite_all();
5043 fname = get_mef_name();
5044 if (fname == NULL)
5045 return;
5046 mch_remove(fname); // in case it's not unique
5047
5048 cmd = make_get_fullcmd(eap->arg, fname);
5049 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005050 {
5051 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005052 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005053 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005054
5055 // let the shell know if we are redirecting output or not
5056 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5057
5058#ifdef AMIGA
5059 out_flush();
5060 // read window status report and redraw before message
5061 (void)char_avail();
5062#endif
5063
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005064 incr_quickfix_busy();
5065
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005066 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5067 errorformat = p_gefm;
5068 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5069 newlist = FALSE;
5070
5071 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5072 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005073 if (wp != NULL)
5074 {
5075 qi = GET_LOC_LIST(wp);
5076 if (qi == NULL)
5077 goto cleanup;
5078 }
5079 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005080 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005081
5082 // Remember the current quickfix list identifier, so that we can
5083 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005084 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005085 if (au_name != NULL)
5086 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5087 curbuf->b_fname, TRUE, curbuf);
5088 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5089 // display the first error
5090 qf_jump_first(qi, save_qfid, FALSE);
5091
5092cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005093 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005094 mch_remove(fname);
5095 vim_free(fname);
5096 vim_free(cmd);
5097}
5098
5099/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005100 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005101 */
5102 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005103qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005104{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005105 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005106
5107 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5108 return 0;
5109 return qf_get_curlist(qi)->qf_count;
5110}
5111
5112/*
5113 * Returns the number of valid entries in the current quickfix/location list.
5114 */
5115 int
5116qf_get_valid_size(exarg_T *eap)
5117{
5118 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005119 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005120 qfline_T *qfp;
5121 int i, sz = 0;
5122 int prev_fnum = 0;
5123
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005124 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5125 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005126
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005127 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005128 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005129 {
5130 if (qfp->qf_valid)
5131 {
5132 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005133 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005134 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5135 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005136 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005137 sz++;
5138 prev_fnum = qfp->qf_fnum;
5139 }
5140 }
5141 }
5142
5143 return sz;
5144}
5145
5146/*
5147 * Returns the current index of the quickfix/location list.
5148 * Returns 0 if there is an error.
5149 */
5150 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005151qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005152{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005153 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005154
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005155 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5156 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005157
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005158 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005159}
5160
5161/*
5162 * Returns the current index in the quickfix/location list (counting only valid
5163 * entries). If no valid entries are in the list, then returns 1.
5164 */
5165 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005166qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005167{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005168 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005169 qf_list_T *qfl;
5170 qfline_T *qfp;
5171 int i, eidx = 0;
5172 int prev_fnum = 0;
5173
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005174 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5175 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005176
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005177 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005178 qfp = qfl->qf_start;
5179
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005180 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005181 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005182 return 1;
5183
5184 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5185 {
5186 if (qfp->qf_valid)
5187 {
5188 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5189 {
5190 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5191 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005192 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005193 eidx++;
5194 prev_fnum = qfp->qf_fnum;
5195 }
5196 }
5197 else
5198 eidx++;
5199 }
5200 }
5201
5202 return eidx ? eidx : 1;
5203}
5204
5205/*
5206 * Get the 'n'th valid error entry in the quickfix or location list.
5207 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5208 * For :cdo and :ldo returns the 'n'th valid error entry.
5209 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5210 */
5211 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005212qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005213{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005214 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005215 int i, eidx;
5216 int prev_fnum = 0;
5217
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005218 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005219 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005220 return 1;
5221
Bram Moolenaar95946f12019-03-31 15:31:59 +02005222 eidx = 0;
5223 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005224 {
5225 if (qfp->qf_valid)
5226 {
5227 if (fdo)
5228 {
5229 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5230 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005231 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005232 eidx++;
5233 prev_fnum = qfp->qf_fnum;
5234 }
5235 }
5236 else
5237 eidx++;
5238 }
5239
5240 if (eidx == n)
5241 break;
5242 }
5243
5244 if (i <= qfl->qf_count)
5245 return i;
5246 else
5247 return 1;
5248}
5249
5250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005252 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005253 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 */
5255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005256ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005258 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005259 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005260
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005261 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5262 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005263
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005264 if (eap->addr_count > 0)
5265 errornr = (int)eap->line2;
5266 else
5267 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005268 switch (eap->cmdidx)
5269 {
5270 case CMD_cc: case CMD_ll:
5271 errornr = 0;
5272 break;
5273 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5274 case CMD_lfirst:
5275 errornr = 1;
5276 break;
5277 default:
5278 errornr = 32767;
5279 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005280 }
5281
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005282 // For cdo and ldo commands, jump to the nth valid error.
5283 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005284 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5285 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005286 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005287 eap->addr_count > 0 ? (int)eap->line1 : 1,
5288 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5289
5290 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291}
5292
5293/*
5294 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005295 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005296 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 */
5298 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005299ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005301 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005302 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005303 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005304
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005305 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5306 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005307
Bram Moolenaar55b69262017-08-13 13:42:01 +02005308 if (eap->addr_count > 0
5309 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5310 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005311 errornr = (int)eap->line2;
5312 else
5313 errornr = 1;
5314
Bram Moolenaar39665952018-08-15 20:59:48 +02005315 // Depending on the command jump to either next or previous entry/file.
5316 switch (eap->cmdidx)
5317 {
5318 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5319 dir = FORWARD;
5320 break;
5321 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5322 case CMD_lNext:
5323 dir = BACKWARD;
5324 break;
5325 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5326 dir = FORWARD_FILE;
5327 break;
5328 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5329 dir = BACKWARD_FILE;
5330 break;
5331 default:
5332 dir = FORWARD;
5333 break;
5334 }
5335
5336 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337}
5338
5339/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005340 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5341 * The index of the entry is stored in 'errornr'.
5342 * Returns NULL if an entry is not found.
5343 */
5344 static qfline_T *
5345qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5346{
5347 qfline_T *qfp = NULL;
5348 int idx = 0;
5349
5350 // Find the first entry in this file
5351 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5352 if (qfp->qf_fnum == bnr)
5353 break;
5354
5355 *errornr = idx;
5356 return qfp;
5357}
5358
5359/*
5360 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5361 * with the error number for the first entry. Assumes the entries are sorted in
5362 * the quickfix list by line number.
5363 */
5364 static qfline_T *
5365qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5366{
5367 while (!got_int
5368 && entry->qf_prev != NULL
5369 && entry->qf_fnum == entry->qf_prev->qf_fnum
5370 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5371 {
5372 entry = entry->qf_prev;
5373 --*errornr;
5374 }
5375
5376 return entry;
5377}
5378
5379/*
5380 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5381 * with the error number for the last entry. Assumes the entries are sorted in
5382 * the quickfix list by line number.
5383 */
5384 static qfline_T *
5385qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5386{
5387 while (!got_int &&
5388 entry->qf_next != NULL
5389 && entry->qf_fnum == entry->qf_next->qf_fnum
5390 && entry->qf_lnum == entry->qf_next->qf_lnum)
5391 {
5392 entry = entry->qf_next;
5393 ++*errornr;
5394 }
5395
5396 return entry;
5397}
5398
5399/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005400 * Returns TRUE if the specified quickfix entry is
5401 * after the given line (linewise is TRUE)
5402 * or after the line and column.
5403 */
5404 static int
5405qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5406{
5407 if (linewise)
5408 return qfp->qf_lnum > pos->lnum;
5409 else
5410 return (qfp->qf_lnum > pos->lnum ||
5411 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5412}
5413
5414/*
5415 * Returns TRUE if the specified quickfix entry is
5416 * before the given line (linewise is TRUE)
5417 * or before the line and column.
5418 */
5419 static int
5420qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5421{
5422 if (linewise)
5423 return qfp->qf_lnum < pos->lnum;
5424 else
5425 return (qfp->qf_lnum < pos->lnum ||
5426 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5427}
5428
5429/*
5430 * Returns TRUE if the specified quickfix entry is
5431 * on or after the given line (linewise is TRUE)
5432 * or on or after the line and column.
5433 */
5434 static int
5435qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5436{
5437 if (linewise)
5438 return qfp->qf_lnum >= pos->lnum;
5439 else
5440 return (qfp->qf_lnum > pos->lnum ||
5441 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5442}
5443
5444/*
5445 * Returns TRUE if the specified quickfix entry is
5446 * on or before the given line (linewise is TRUE)
5447 * or on or before the line and column.
5448 */
5449 static int
5450qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5451{
5452 if (linewise)
5453 return qfp->qf_lnum <= pos->lnum;
5454 else
5455 return (qfp->qf_lnum < pos->lnum ||
5456 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5457}
5458
5459/*
5460 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5461 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5462 * multiple entries on a single line as one. Otherwise returns the entry after
5463 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005464 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5465 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005466 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005467 */
5468 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005469qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005470 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005471 pos_T *pos,
5472 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005473 qfline_T *qfp,
5474 int *errornr)
5475{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005476 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005477 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005478 return qfp;
5479
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005480 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005481 while (qfp->qf_next != NULL
5482 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005483 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005484 {
5485 qfp = qfp->qf_next;
5486 ++*errornr;
5487 }
5488
5489 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005490 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005491 return NULL;
5492
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005493 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005494 qfp = qfp->qf_next;
5495 ++*errornr;
5496
5497 return qfp;
5498}
5499
5500/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005501 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5502 * If 'linewise' is TRUE, returns the entry before the specified line and
5503 * treats multiple entries on a single line as one. Otherwise returns the entry
5504 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005505 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5506 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005507 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005508 */
5509 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005510qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005511 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005512 pos_T *pos,
5513 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005514 qfline_T *qfp,
5515 int *errornr)
5516{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005517 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005518 while (qfp->qf_next != NULL
5519 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005520 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005521 {
5522 qfp = qfp->qf_next;
5523 ++*errornr;
5524 }
5525
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005526 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005527 return NULL;
5528
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005529 if (linewise)
5530 // If multiple entries are on the same line, then use the first entry
5531 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005532
5533 return qfp;
5534}
5535
5536/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005537 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005538 * the direction 'dir'.
5539 */
5540 static qfline_T *
5541qf_find_closest_entry(
5542 qf_list_T *qfl,
5543 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005544 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005545 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005546 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005547 int *errornr)
5548{
5549 qfline_T *qfp;
5550
5551 *errornr = 0;
5552
5553 // Find the first entry in this file
5554 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5555 if (qfp == NULL)
5556 return NULL; // no entry in this file
5557
5558 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005559 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005560 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005561 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005562
5563 return qfp;
5564}
5565
5566/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005567 * Get the nth quickfix entry below the specified entry. Searches forward in
5568 * the list. If linewise is TRUE, then treat multiple entries on a single line
5569 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005570 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005571 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005572qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005573{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005574 qfline_T *entry = entry_arg;
5575
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005576 while (n-- > 0 && !got_int)
5577 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005578 int first_errornr = *errornr;
5579
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005580 if (linewise)
5581 // Treat all the entries on the same line in this file as one
5582 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005583
5584 if (entry->qf_next == NULL
5585 || entry->qf_next->qf_fnum != entry->qf_fnum)
5586 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005587 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005588 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005589 break;
5590 }
5591
5592 entry = entry->qf_next;
5593 ++*errornr;
5594 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005595}
5596
5597/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005598 * Get the nth quickfix entry above the specified entry. Searches backwards in
5599 * the list. If linewise is TRUE, then treat multiple entries on a single line
5600 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005601 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005602 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005603qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005604{
5605 while (n-- > 0 && !got_int)
5606 {
5607 if (entry->qf_prev == NULL
5608 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5609 break;
5610
5611 entry = entry->qf_prev;
5612 --*errornr;
5613
5614 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005615 if (linewise)
5616 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005617 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005618}
5619
5620/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005621 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5622 * the specified direction. Returns the error number in the quickfix list or 0
5623 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005624 */
5625 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005626qf_find_nth_adj_entry(
5627 qf_list_T *qfl,
5628 int bnr,
5629 pos_T *pos,
5630 int n,
5631 int dir,
5632 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005633{
5634 qfline_T *adj_entry;
5635 int errornr;
5636
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005637 // Find an entry closest to the specified position
5638 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005639 if (adj_entry == NULL)
5640 return 0;
5641
5642 if (--n > 0)
5643 {
5644 // Go to the n'th entry in the current buffer
5645 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005646 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005647 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005648 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005649 }
5650
5651 return errornr;
5652}
5653
5654/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005655 * Jump to a quickfix entry in the current file nearest to the current line or
5656 * current line/col.
5657 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5658 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005659 */
5660 void
5661ex_cbelow(exarg_T *eap)
5662{
5663 qf_info_T *qi;
5664 qf_list_T *qfl;
5665 int dir;
5666 int buf_has_flag;
5667 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005668 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005669
5670 if (eap->addr_count > 0 && eap->line2 <= 0)
5671 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005672 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005673 return;
5674 }
5675
5676 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005677 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5678 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005679 buf_has_flag = BUF_HAS_QF_ENTRY;
5680 else
5681 buf_has_flag = BUF_HAS_LL_ENTRY;
5682 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5683 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005684 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005685 return;
5686 }
5687
5688 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5689 return;
5690
5691 qfl = qf_get_curlist(qi);
5692 // check if the list has valid errors
5693 if (!qf_list_has_valid_entries(qfl))
5694 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005695 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005696 return;
5697 }
5698
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005699 if (eap->cmdidx == CMD_cbelow
5700 || eap->cmdidx == CMD_lbelow
5701 || eap->cmdidx == CMD_cafter
5702 || eap->cmdidx == CMD_lafter)
5703 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005704 dir = FORWARD;
5705 else
5706 dir = BACKWARD;
5707
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005708 pos = curwin->w_cursor;
5709 // A quickfix entry column number is 1 based whereas cursor column
5710 // number is 0 based. Adjust the column number.
5711 pos.col++;
5712 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5713 eap->addr_count > 0 ? eap->line2 : 0, dir,
5714 eap->cmdidx == CMD_cbelow
5715 || eap->cmdidx == CMD_lbelow
5716 || eap->cmdidx == CMD_cabove
5717 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005718
5719 if (errornr > 0)
5720 qf_jump(qi, 0, errornr, FALSE);
5721 else
5722 emsg(_(e_no_more_items));
5723}
5724
5725/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005726 * Return the autocmd name for the :cfile Ex commands
5727 */
5728 static char_u *
5729cfile_get_auname(cmdidx_T cmdidx)
5730{
5731 switch (cmdidx)
5732 {
5733 case CMD_cfile: return (char_u *)"cfile";
5734 case CMD_cgetfile: return (char_u *)"cgetfile";
5735 case CMD_caddfile: return (char_u *)"caddfile";
5736 case CMD_lfile: return (char_u *)"lfile";
5737 case CMD_lgetfile: return (char_u *)"lgetfile";
5738 case CMD_laddfile: return (char_u *)"laddfile";
5739 default: return NULL;
5740 }
5741}
5742
5743/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005744 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005745 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 */
5747 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005748ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005750 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005751 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005752 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005753 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005754 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005755 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005756
Bram Moolenaara16123a2019-03-28 20:31:07 +01005757 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005758 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5759 NULL, FALSE, curbuf))
5760 {
5761#ifdef FEAT_EVAL
5762 if (aborting())
5763 return;
5764#endif
5765 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005766
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005767 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005768#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005769 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005770 {
5771 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005772 NULL, NULL,
5773 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005774 if (browse_file == NULL)
5775 return;
5776 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5777 vim_free(browse_file);
5778 }
5779 else
5780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005782 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005783
Bram Moolenaar39665952018-08-15 20:59:48 +02005784 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005785 wp = curwin;
5786
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005787 incr_quickfix_busy();
5788
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005789 // This function is used by the :cfile, :cgetfile and :caddfile
5790 // commands.
5791 // :cfile always creates a new quickfix list and jumps to the
5792 // first error.
5793 // :cgetfile creates a new quickfix list but doesn't jump to the
5794 // first error.
5795 // :caddfile adds to an existing quickfix list. If there is no
5796 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005797 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005798 && eap->cmdidx != CMD_laddfile),
5799 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005800 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005801 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005802 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005803 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005804 {
5805 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005806 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005807 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005808 }
5809 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005810 qf_list_changed(qf_get_curlist(qi));
5811 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005812 if (au_name != NULL)
5813 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005814
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005815 // Jump to the first error for a new list and if autocmds didn't
5816 // free the list.
5817 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5818 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005819 // display the first error
5820 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005821
5822 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005823}
5824
5825/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005826 * Return the vimgrep autocmd name.
5827 */
5828 static char_u *
5829vgr_get_auname(cmdidx_T cmdidx)
5830{
5831 switch (cmdidx)
5832 {
5833 case CMD_vimgrep: return (char_u *)"vimgrep";
5834 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5835 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5836 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5837 case CMD_grep: return (char_u *)"grep";
5838 case CMD_lgrep: return (char_u *)"lgrep";
5839 case CMD_grepadd: return (char_u *)"grepadd";
5840 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5841 default: return NULL;
5842 }
5843}
5844
5845/*
5846 * Initialize the regmatch used by vimgrep for pattern "s".
5847 */
5848 static void
5849vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5850{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005851 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005852 regmatch->regprog = NULL;
5853
5854 if (s == NULL || *s == NUL)
5855 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005856 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005857 if (last_search_pat() == NULL)
5858 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005859 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005860 return;
5861 }
5862 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5863 }
5864 else
5865 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5866
5867 regmatch->rmm_ic = p_ic;
5868 regmatch->rmm_maxcol = 0;
5869}
5870
5871/*
5872 * Display a file name when vimgrep is running.
5873 */
5874 static void
5875vgr_display_fname(char_u *fname)
5876{
5877 char_u *p;
5878
5879 msg_start();
5880 p = msg_strtrunc(fname, TRUE);
5881 if (p == NULL)
5882 msg_outtrans(fname);
5883 else
5884 {
5885 msg_outtrans(p);
5886 vim_free(p);
5887 }
5888 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005889 msg_didout = FALSE; // overwrite this message
5890 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005891 msg_col = 0;
5892 out_flush();
5893}
5894
5895/*
5896 * Load a dummy buffer to search for a pattern using vimgrep.
5897 */
5898 static buf_T *
5899vgr_load_dummy_buf(
5900 char_u *fname,
5901 char_u *dirname_start,
5902 char_u *dirname_now)
5903{
5904 int save_mls;
5905#if defined(FEAT_SYN_HL)
5906 char_u *save_ei = NULL;
5907#endif
5908 buf_T *buf;
5909
5910#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005911 // Don't do Filetype autocommands to avoid loading syntax and
5912 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005913 save_ei = au_event_disable(",Filetype");
5914#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005915 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005916 save_mls = p_mls;
5917 p_mls = 0;
5918
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005919 // Load file into a buffer, so that 'fileencoding' is detected,
5920 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005921 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5922
5923 p_mls = save_mls;
5924#if defined(FEAT_SYN_HL)
5925 au_event_restore(save_ei);
5926#endif
5927
5928 return buf;
5929}
5930
5931/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005932 * Check whether a quickfix/location list is valid. Autocmds may remove or
5933 * change a quickfix list when vimgrep is running. If the list is not found,
5934 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005935 */
5936 static int
5937vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005938 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005939 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005940 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005941 char_u *title)
5942{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005943 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005944 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005945 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005946 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005947 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005948 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005949 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005950 return FALSE;
5951 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005952 else
5953 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005954 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005955 qf_new_list(qi, title);
5956 return TRUE;
5957 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005958 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005959
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005960 if (qf_restore_list(qi, qfid) == FAIL)
5961 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005962
5963 return TRUE;
5964}
5965
5966/*
5967 * Search for a pattern in all the lines in a buffer and add the matching lines
5968 * to a quickfix list.
5969 */
5970 static int
5971vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005972 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005973 char_u *fname,
5974 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005975 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005976 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005977 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005978 int duplicate_name,
5979 int flags)
5980{
5981 int found_match = FALSE;
5982 long lnum;
5983 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02005984 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005985
Bram Moolenaar1c299432018-10-28 14:36:09 +01005986 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005987 {
5988 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005989 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005990 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005991 // Regular expression match
5992 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Paul Ollis65745772022-06-05 16:55:54 +01005993 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005994 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005995 // Pass the buffer number so that it gets used even for a
5996 // dummy buffer, unless duplicate_name is set, then the
5997 // buffer will be wiped out below.
5998 if (qf_add_entry(qfl,
5999 NULL, // dir
6000 fname,
6001 NULL,
6002 duplicate_name ? 0 : buf->b_fnum,
6003 ml_get_buf(buf,
6004 regmatch->startpos[0].lnum + lnum, FALSE),
6005 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006006 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006007 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006008 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006009 FALSE, // vis_col
6010 NULL, // search pattern
6011 0, // nr
6012 0, // type
6013 TRUE // valid
6014 ) == QF_FAIL)
6015 {
6016 got_int = TRUE;
6017 break;
6018 }
6019 found_match = TRUE;
6020 if (--*tomatch == 0)
6021 break;
6022 if ((flags & VGR_GLOBAL) == 0
6023 || regmatch->endpos[0].lnum > 0)
6024 break;
6025 col = regmatch->endpos[0].col
6026 + (col == regmatch->endpos[0].col);
6027 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6028 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006029 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006030 }
6031 else
6032 {
6033 char_u *str = ml_get_buf(buf, lnum, FALSE);
6034 int score;
6035 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006036 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006037
6038 // Fuzzy string match
6039 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6040 {
6041 // Pass the buffer number so that it gets used even for a
6042 // dummy buffer, unless duplicate_name is set, then the
6043 // buffer will be wiped out below.
6044 if (qf_add_entry(qfl,
6045 NULL, // dir
6046 fname,
6047 NULL,
6048 duplicate_name ? 0 : buf->b_fnum,
6049 str,
6050 lnum,
thinca6864efa2021-06-19 20:45:20 +02006051 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006052 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006053 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006054 FALSE, // vis_col
6055 NULL, // search pattern
6056 0, // nr
6057 0, // type
6058 TRUE // valid
6059 ) == QF_FAIL)
6060 {
6061 got_int = TRUE;
6062 break;
6063 }
6064 found_match = TRUE;
6065 if (--*tomatch == 0)
6066 break;
6067 if ((flags & VGR_GLOBAL) == 0)
6068 break;
6069 col = matches[pat_len - 1] + col + 1;
6070 if (col > (colnr_T)STRLEN(str))
6071 break;
6072 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006073 }
6074 line_breakcheck();
6075 if (got_int)
6076 break;
6077 }
6078
6079 return found_match;
6080}
6081
6082/*
6083 * Jump to the first match and update the directory.
6084 */
6085 static void
6086vgr_jump_to_match(
6087 qf_info_T *qi,
6088 int forceit,
6089 int *redraw_for_dummy,
6090 buf_T *first_match_buf,
6091 char_u *target_dir)
6092{
6093 buf_T *buf;
6094
6095 buf = curbuf;
6096 qf_jump(qi, 0, 0, forceit);
6097 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006098 // If we jumped to another buffer redrawing will already be
6099 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006100 *redraw_for_dummy = FALSE;
6101
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006102 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006103 if (curbuf == first_match_buf && target_dir != NULL)
6104 {
6105 exarg_T ea;
6106
Bram Moolenaara80faa82020-04-12 19:37:17 +02006107 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006108 ea.arg = target_dir;
6109 ea.cmdidx = CMD_lcd;
6110 ex_cd(&ea);
6111 }
6112}
6113
6114/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006115 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006116 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006117typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006118{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006119 long tomatch; // maximum number of matches to find
6120 char_u *spat; // search pattern
6121 int flags; // search modifier
6122 char_u **fnames; // list of files to search
6123 int fcount; // number of files
6124 regmmatch_T regmatch; // compiled search pattern
6125 char_u *qf_title; // quickfix list title
6126} vgr_args_T;
6127
6128/*
6129 * Process :vimgrep command arguments. The command syntax is:
6130 *
6131 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6132 */
6133 static int
6134vgr_process_args(
6135 exarg_T *eap,
6136 vgr_args_T *args)
6137{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006138 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006139
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006140 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006141
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006142 args->regmatch.regprog = NULL;
6143 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006144
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006145 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006146 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006147 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006148 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006149
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006150 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006151 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006152 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006153 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006154 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006155 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006156 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006157
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006158 vgr_init_regmatch(&args->regmatch, args->spat);
6159 if (args->regmatch.regprog == NULL)
6160 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006161
6162 p = skipwhite(p);
6163 if (*p == NUL)
6164 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006165 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006166 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006167 }
6168
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006169 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006170 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6171 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006172 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006173 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006174 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006175 }
6176
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006177 return OK;
6178}
6179
6180/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006181 * Return TRUE if "buf" had an existing swap file, the current swap file does
6182 * not end in ".swp".
6183 */
6184 static int
6185existing_swapfile(buf_T *buf)
6186{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006187 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006188 {
6189 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6190 size_t len = STRLEN(fname);
6191
6192 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6193 }
6194 return FALSE;
6195}
6196
6197/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006198 * Search for a pattern in a list of files and populate the quickfix list with
6199 * the matches.
6200 */
6201 static int
6202vgr_process_files(
6203 win_T *wp,
6204 qf_info_T *qi,
6205 vgr_args_T *cmd_args,
6206 int *redraw_for_dummy,
6207 buf_T **first_match_buf,
6208 char_u **target_dir)
6209{
6210 int status = FAIL;
6211 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6212 time_t seconds = 0;
6213 char_u *fname;
6214 int fi;
6215 buf_T *buf;
6216 int duplicate_name = FALSE;
6217 int using_dummy;
6218 char_u *dirname_start = NULL;
6219 char_u *dirname_now = NULL;
6220 int found_match;
6221 aco_save_T aco;
6222
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006223 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6224 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006225 if (dirname_start == NULL || dirname_now == NULL)
6226 goto theend;
6227
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006228 // Remember the current directory, because a BufRead autocommand that does
6229 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006230 mch_dirname(dirname_start, MAXPATHL);
6231
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006232 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006233 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6234 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006235 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006236 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006237 if (time(NULL) > seconds)
6238 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006239 // Display the file name every second or so, show the user we are
6240 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006241 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006242 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006243 }
6244
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006245 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006246 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6247 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006248 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006249 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006250 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006251 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006252
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006253 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006254 }
6255 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006256 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006257 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006258
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006259 // Check whether the quickfix list is still valid. When loading a
6260 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006261 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006262 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006263
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006264 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006265
Bram Moolenaar81695252004-12-29 20:58:21 +00006266 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006267 {
6268 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006269 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006270 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006271 else
6272 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006273 // Try for a match in all lines of the buffer.
6274 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006275 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006276 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006277 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006278
Bram Moolenaar81695252004-12-29 20:58:21 +00006279 if (using_dummy)
6280 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006281 if (found_match && *first_match_buf == NULL)
6282 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006283 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006284 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006285 // Never keep a dummy buffer if there is another buffer
6286 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006287 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006288 buf = NULL;
6289 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006290 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006291 || buf->b_p_bh[0] == 'u' // "unload"
6292 || buf->b_p_bh[0] == 'w' // "wipe"
6293 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006294 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006295 // When no match was found we don't need to remember the
6296 // buffer, wipe it out. If there was a match and it
6297 // wasn't the first one or we won't jump there: only
6298 // unload the buffer.
6299 // Ignore 'hidden' here, because it may lead to having too
6300 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006301 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006302 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006303 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006304 buf = NULL;
6305 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006306 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006307 || (cmd_args->flags & VGR_NOJUMP)
6308 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006309 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006310 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006311 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006312 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006313 buf = NULL;
6314 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006315 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006316
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006317 if (buf != NULL)
6318 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006319 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006320 buf->b_flags &= ~BF_DUMMY;
6321
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006322 // If the buffer is still loaded we need to use the
6323 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006324 if (buf == *first_match_buf
6325 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006326 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006327 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006328
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006329 // The buffer is still loaded, the Filetype autocommands
6330 // need to be done now, in that buffer. And the modelines
6331 // need to be done (again). But not the window-local
6332 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006333 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006334#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006335 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6336 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006337#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006338 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006339 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006340 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006341 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006342 }
6343 }
6344
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006345 status = OK;
6346
6347theend:
6348 vim_free(dirname_now);
6349 vim_free(dirname_start);
6350 return status;
6351}
6352
6353/*
6354 * ":vimgrep {pattern} file(s)"
6355 * ":vimgrepadd {pattern} file(s)"
6356 * ":lvimgrep {pattern} file(s)"
6357 * ":lvimgrepadd {pattern} file(s)"
6358 */
6359 void
6360ex_vimgrep(exarg_T *eap)
6361{
6362 vgr_args_T args;
6363 qf_info_T *qi;
6364 qf_list_T *qfl;
6365 int_u save_qfid;
6366 win_T *wp = NULL;
6367 int redraw_for_dummy = FALSE;
6368 buf_T *first_match_buf = NULL;
6369 char_u *target_dir = NULL;
6370 char_u *au_name = NULL;
6371 int status;
6372
6373 au_name = vgr_get_auname(eap->cmdidx);
6374 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6375 curbuf->b_fname, TRUE, curbuf))
6376 {
6377#ifdef FEAT_EVAL
6378 if (aborting())
6379 return;
6380#endif
6381 }
6382
6383 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6384 if (qi == NULL)
6385 return;
6386
6387 if (vgr_process_args(eap, &args) == FAIL)
6388 goto theend;
6389
6390 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6391 && eap->cmdidx != CMD_vimgrepadd
6392 && eap->cmdidx != CMD_lvimgrepadd)
6393 || qf_stack_empty(qi))
6394 // make place for a new list
6395 qf_new_list(qi, args.qf_title);
6396
6397 incr_quickfix_busy();
6398
6399 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6400 &first_match_buf, &target_dir);
6401 if (status != OK)
6402 {
6403 FreeWild(args.fcount, args.fnames);
6404 decr_quickfix_busy();
6405 goto theend;
6406 }
6407
6408 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006409
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006410 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006411 qfl->qf_nonevalid = FALSE;
6412 qfl->qf_ptr = qfl->qf_start;
6413 qfl->qf_index = 1;
6414 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006415
Bram Moolenaar864293a2016-06-02 13:40:04 +02006416 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006417
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006418 // Remember the current quickfix list identifier, so that we can check for
6419 // autocommands changing the current quickfix list.
6420 save_qfid = qf_get_curlist(qi)->qf_id;
6421
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006422 if (au_name != NULL)
6423 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6424 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006425 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6426 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006427 if (!qflist_valid(wp, save_qfid)
6428 || qf_restore_list(qi, save_qfid) == FAIL)
6429 {
6430 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006431 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006432 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006433
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006434 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006435 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006436 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006437 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006438 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6439 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006440 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006441 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006442 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006443
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006444 decr_quickfix_busy();
6445
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006446 // If we loaded a dummy buffer into the current window, the autocommands
6447 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006448 if (redraw_for_dummy)
6449 {
6450#ifdef FEAT_FOLDING
6451 foldUpdateAll(curwin);
6452#else
6453 redraw_later(NOT_VALID);
6454#endif
6455 }
6456
Bram Moolenaar86b68352004-12-27 21:59:20 +00006457theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006458 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006459 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006460 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006461}
6462
6463/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006464 * Restore current working directory to "dirname_start" if they differ, taking
6465 * into account whether it is set locally or globally.
6466 */
6467 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006468restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006469{
6470 char_u *dirname_now = alloc(MAXPATHL);
6471
6472 if (NULL != dirname_now)
6473 {
6474 mch_dirname(dirname_now, MAXPATHL);
6475 if (STRCMP(dirname_start, dirname_now) != 0)
6476 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006477 // If the directory has changed, change it back by building up an
6478 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006479 exarg_T ea;
6480
Bram Moolenaara80faa82020-04-12 19:37:17 +02006481 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006482 ea.arg = dirname_start;
6483 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6484 ex_cd(&ea);
6485 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006486 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006487 }
6488}
6489
6490/*
6491 * Load file "fname" into a dummy buffer and return the buffer pointer,
6492 * placing the directory resulting from the buffer load into the
6493 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6494 * prior to calling this function. Restores directory to "dirname_start" prior
6495 * to returning, if autocmds or the 'autochdir' option have changed it.
6496 *
6497 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6498 * or wipe_dummy_buffer() later!
6499 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006500 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006501 */
6502 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006503load_dummy_buffer(
6504 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006505 char_u *dirname_start, // in: old directory
6506 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006507{
6508 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006509 bufref_T newbufref;
6510 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006511 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006512 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006513 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006514
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006515 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006516 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6517 if (newbuf == NULL)
6518 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006519 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006520
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006521 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006522 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006524 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006525 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006526 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006527 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006528 ++newbuf->b_locked;
6529
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006530 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006531 aucmd_prepbuf(&aco, newbuf);
6532
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006533 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006534 (void)setfname(curbuf, fname, NULL, FALSE);
6535
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006536 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006537 check_need_swap(TRUE);
6538
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006539 // Remove the "dummy" flag, otherwise autocommands may not
6540 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006541 curbuf->b_flags &= ~BF_DUMMY;
6542
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006543 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006544 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006545 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006546 NULL, READ_NEW | READ_DUMMY);
6547 --newbuf->b_locked;
6548 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006549 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006550 && !(curbuf->b_flags & BF_NEW))
6551 {
6552 failed = FALSE;
6553 if (curbuf != newbuf)
6554 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006555 // Bloody autocommands changed the buffer! Can happen when
6556 // using netrw and editing a remote file. Use the current
6557 // buffer instead, delete the dummy one after restoring the
6558 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006559 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006560 newbuf = curbuf;
6561 }
6562 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006563
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006564 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006565 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006566 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6567 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006568
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006569 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6570 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006571 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006572 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006573
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006574 // When autocommands/'autochdir' option changed directory: go back.
6575 // Let the caller know what the resulting dir was first, in case it is
6576 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006577 mch_dirname(resulting_dir, MAXPATHL);
6578 restore_start_dir(dirname_start);
6579
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006580 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006581 return NULL;
6582 if (failed)
6583 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006584 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006585 return NULL;
6586 }
6587 return newbuf;
6588}
6589
6590/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006591 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6592 * directory to "dirname_start" prior to returning, if autocmds or the
6593 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006594 */
6595 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006596wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006597{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006598 // If any autocommand opened a window on the dummy buffer, close that
6599 // window. If we can't close them all then give up.
6600 while (buf->b_nwindows > 0)
6601 {
6602 int did_one = FALSE;
6603 win_T *wp;
6604
6605 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006606 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006607 if (wp->w_buffer == buf)
6608 {
6609 if (win_close(wp, FALSE) == OK)
6610 did_one = TRUE;
6611 break;
6612 }
6613 if (!did_one)
6614 return;
6615 }
6616
6617 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006618 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006619#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006620 cleanup_T cs;
6621
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006622 // Reset the error/interrupt/exception state here so that aborting()
6623 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6624 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006625 enter_cleanup(&cs);
6626#endif
6627
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006628 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006629
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006630#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006631 // Restore the error/interrupt/exception state if not discarded by a
6632 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006633 leave_cleanup(&cs);
6634#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006635 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006636 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006637 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006638}
6639
6640/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006641 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6642 * directory to "dirname_start" prior to returning, if autocmds or the
6643 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006644 */
6645 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006646unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006647{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006648 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006649 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006650 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006651
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006652 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006653 restore_start_dir(dirname_start);
6654 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006655}
6656
Bram Moolenaar05159a02005-02-26 23:04:13 +00006657#if defined(FEAT_EVAL) || defined(PROTO)
6658/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006659 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006660 * to 'list'. Returns OK on success.
6661 */
6662 static int
6663get_qfline_items(qfline_T *qfp, list_T *list)
6664{
6665 int bufnum;
6666 dict_T *dict;
6667 char_u buf[2];
6668
6669 // Handle entries with a non-existing buffer number.
6670 bufnum = qfp->qf_fnum;
6671 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6672 bufnum = 0;
6673
6674 if ((dict = dict_alloc()) == NULL)
6675 return FAIL;
6676 if (list_append_dict(list, dict) == FAIL)
6677 return FAIL;
6678
6679 buf[0] = qfp->qf_type;
6680 buf[1] = NUL;
6681 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006682 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6683 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6684 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6685 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6686 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6687 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006688 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6689 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6690 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6691 || dict_add_string(dict, "type", buf) == FAIL
6692 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6693 return FAIL;
6694
6695 return OK;
6696}
6697
6698/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006699 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006700 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006701 * If eidx is not 0, then return only the specified entry. Otherwise return
6702 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006703 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006704 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006705get_errorlist(
6706 qf_info_T *qi_arg,
6707 win_T *wp,
6708 int qf_idx,
6709 int eidx,
6710 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006711{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006712 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006713 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006714 qfline_T *qfp;
6715 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006716
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006717 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006718 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006719 qi = &ql_info;
6720 if (wp != NULL)
6721 {
6722 qi = GET_LOC_LIST(wp);
6723 if (qi == NULL)
6724 return FAIL;
6725 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006726 }
6727
Bram Moolenaar858ba062020-05-31 23:11:59 +02006728 if (eidx < 0)
6729 return OK;
6730
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006731 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006732 qf_idx = qi->qf_curlist;
6733
Bram Moolenaar0398e002019-03-21 21:12:49 +01006734 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006735 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006736
Bram Moolenaar0398e002019-03-21 21:12:49 +01006737 qfl = qf_get_list(qi, qf_idx);
6738 if (qf_list_empty(qfl))
6739 return FAIL;
6740
Bram Moolenaara16123a2019-03-28 20:31:07 +01006741 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006742 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006743 if (eidx > 0)
6744 {
6745 if (eidx == i)
6746 return get_qfline_items(qfp, list);
6747 }
6748 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006749 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006750 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006751
Bram Moolenaar05159a02005-02-26 23:04:13 +00006752 return OK;
6753}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006754
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006755// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006756enum {
6757 QF_GETLIST_NONE = 0x0,
6758 QF_GETLIST_TITLE = 0x1,
6759 QF_GETLIST_ITEMS = 0x2,
6760 QF_GETLIST_NR = 0x4,
6761 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006762 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006763 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006764 QF_GETLIST_IDX = 0x40,
6765 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006766 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006767 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006768 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006769 QF_GETLIST_QFTF = 0x800,
6770 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006771};
6772
6773/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006774 * Parse text from 'di' and return the quickfix list items.
6775 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006776 */
6777 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006778qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006779{
6780 int status = FAIL;
6781 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006782 char_u *errorformat = p_efm;
6783 dictitem_T *efm_di;
6784 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006785
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006786 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006787 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006788 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006789 // If errorformat is supplied then use it, otherwise use the 'efm'
6790 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006791 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6792 {
6793 if (efm_di->di_tv.v_type != VAR_STRING ||
6794 efm_di->di_tv.vval.v_string == NULL)
6795 return FAIL;
6796 errorformat = efm_di->di_tv.vval.v_string;
6797 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006798
Bram Moolenaar36538222017-09-02 19:51:44 +02006799 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006800 if (l == NULL)
6801 return FAIL;
6802
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006803 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006804 if (qi != NULL)
6805 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006806 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006807 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6808 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006809 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006810 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006811 }
6812 free(qi);
6813 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006814 dict_add_list(retdict, "items", l);
6815 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006816 }
6817
6818 return status;
6819}
6820
6821/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006822 * Return the quickfix/location list window identifier in the current tabpage.
6823 */
6824 static int
6825qf_winid(qf_info_T *qi)
6826{
6827 win_T *win;
6828
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006829 // The quickfix window can be opened even if the quickfix list is not set
6830 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006831 if (qi == NULL)
6832 return 0;
6833 win = qf_find_win(qi);
6834 if (win != NULL)
6835 return win->w_id;
6836 return 0;
6837}
6838
6839/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006840 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006841 * window. If there is no buffer associated with the list or the buffer is
6842 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006843 */
6844 static int
6845qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6846{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006847 int bufnum = 0;
6848
6849 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6850 bufnum = qi->qf_bufnr;
6851
6852 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006853}
6854
6855/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006856 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006857 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006858 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006859qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006860{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006861 int flags = QF_GETLIST_NONE;
6862
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006863 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006864 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006865 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006866 if (!loclist)
6867 // File window ID is applicable only to location list windows
6868 flags &= ~ QF_GETLIST_FILEWINID;
6869 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006870
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006871 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006872 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006873
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006874 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006875 flags |= QF_GETLIST_NR;
6876
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006877 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006878 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006879
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006880 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006881 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006882
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006883 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006884 flags |= QF_GETLIST_ID;
6885
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006886 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006887 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006888
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006889 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006890 flags |= QF_GETLIST_IDX;
6891
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006892 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006893 flags |= QF_GETLIST_SIZE;
6894
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006895 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01006896 flags |= QF_GETLIST_TICK;
6897
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006898 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006899 flags |= QF_GETLIST_FILEWINID;
6900
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006901 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006902 flags |= QF_GETLIST_QFBUFNR;
6903
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006904 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02006905 flags |= QF_GETLIST_QFTF;
6906
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006907 return flags;
6908}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006909
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006910/*
6911 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6912 * If 'nr' and 'id' are not present in 'what' then return the current
6913 * quickfix list index.
6914 * If 'nr' is zero then return the current quickfix list index.
6915 * If 'nr' is '$' then return the last quickfix list index.
6916 * If 'id' is present then return the index of the quickfix list with that id.
6917 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6918 * Return -1, if quickfix list is not present or if the stack is empty.
6919 */
6920 static int
6921qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6922{
6923 int qf_idx;
6924 dictitem_T *di;
6925
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006926 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006927 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6928 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006929 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006930 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006931 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006932 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006933 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006934 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006935 qf_idx = di->di_tv.vval.v_number - 1;
6936 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006937 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006938 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006939 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006940 else if (di->di_tv.v_type == VAR_STRING
6941 && di->di_tv.vval.v_string != NULL
6942 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006943 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006944 qf_idx = qi->qf_listcount - 1;
6945 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006946 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006947 }
6948
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006949 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6950 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006951 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006952 if (di->di_tv.v_type == VAR_NUMBER)
6953 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006954 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006955 if (di->di_tv.vval.v_number != 0)
6956 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6957 }
6958 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006959 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006960 }
6961
6962 return qf_idx;
6963}
6964
6965/*
6966 * Return default values for quickfix list properties in retdict.
6967 */
6968 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006969qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006970{
6971 int status = OK;
6972
6973 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006974 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006975 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6976 {
6977 list_T *l = list_alloc();
6978 if (l != NULL)
6979 status = dict_add_list(retdict, "items", l);
6980 else
6981 status = FAIL;
6982 }
6983 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006984 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006985 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006986 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006987 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006988 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006989 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006990 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006991 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006992 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006993 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006994 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006995 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006996 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006997 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006998 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006999 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7000 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007001 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7002 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007003
7004 return status;
7005}
7006
7007/*
7008 * Return the quickfix list title as 'title' in retdict
7009 */
7010 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007011qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007012{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007013 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007014}
7015
7016/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007017 * Returns the identifier of the window used to display files from a location
7018 * list. If there is no associated window, then returns 0. Useful only when
7019 * called from a location list window.
7020 */
7021 static int
7022qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7023{
7024 int winid = 0;
7025
7026 if (wp != NULL && IS_LL_WINDOW(wp))
7027 {
7028 win_T *ll_wp = qf_find_win_with_loclist(qi);
7029 if (ll_wp != NULL)
7030 winid = ll_wp->w_id;
7031 }
7032
7033 return dict_add_number(retdict, "filewinid", winid);
7034}
7035
7036/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007037 * Return the quickfix list items/entries as 'items' in retdict.
7038 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007039 */
7040 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007041qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007042{
7043 int status = OK;
7044 list_T *l = list_alloc();
7045 if (l != NULL)
7046 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007047 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007048 dict_add_list(retdict, "items", l);
7049 }
7050 else
7051 status = FAIL;
7052
7053 return status;
7054}
7055
7056/*
7057 * Return the quickfix list context (if any) as 'context' in retdict.
7058 */
7059 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007060qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007061{
7062 int status;
7063 dictitem_T *di;
7064
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007065 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007066 {
7067 di = dictitem_alloc((char_u *)"context");
7068 if (di != NULL)
7069 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007070 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007071 status = dict_add(retdict, di);
7072 if (status == FAIL)
7073 dictitem_free(di);
7074 }
7075 else
7076 status = FAIL;
7077 }
7078 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007079 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007080
7081 return status;
7082}
7083
7084/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007085 * Return the current quickfix list index as 'idx' in retdict.
7086 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007087 */
7088 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007089qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007090{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007091 if (eidx == 0)
7092 {
7093 eidx = qfl->qf_index;
7094 if (qf_list_empty(qfl))
7095 // For empty lists, current index is set to 0
7096 eidx = 0;
7097 }
7098 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007099}
7100
7101/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007102 * Return the 'quickfixtextfunc' function of a quickfix/location list
7103 */
7104 static int
7105qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7106{
7107 int status;
7108
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007109 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007110 {
7111 typval_T tv;
7112
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007113 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007114 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7115 clear_tv(&tv);
7116 }
7117 else
7118 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7119
7120 return status;
7121}
7122
7123/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007124 * Return quickfix/location list details (title) as a
7125 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7126 * then current list is used. Otherwise the specified list is used.
7127 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007128 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007129qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7130{
7131 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007132 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007133 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007134 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007135 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007136 dictitem_T *di;
7137 int flags = QF_GETLIST_NONE;
7138
7139 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7140 return qf_get_list_from_lines(what, di, retdict);
7141
7142 if (wp != NULL)
7143 qi = GET_LOC_LIST(wp);
7144
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007145 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007146
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007147 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007148 qf_idx = qf_getprop_qfidx(qi, what);
7149
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007150 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007151 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007152 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007153
Bram Moolenaar0398e002019-03-21 21:12:49 +01007154 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007155
Bram Moolenaar858ba062020-05-31 23:11:59 +02007156 // If an entry index is specified, use that
7157 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7158 {
7159 if (di->di_tv.v_type != VAR_NUMBER)
7160 return FAIL;
7161 eidx = di->di_tv.vval.v_number;
7162 }
7163
Bram Moolenaard823fa92016-08-12 16:29:27 +02007164 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007165 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007166 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007167 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007168 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007169 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007170 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007171 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007172 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007173 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007174 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007175 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007176 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007177 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007178 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007179 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007180 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007181 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007182 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7183 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007184 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7185 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007186 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7187 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007188
Bram Moolenaard823fa92016-08-12 16:29:27 +02007189 return status;
7190}
7191
7192/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007193 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007194 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7195 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007196 */
7197 static int
7198qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007199 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007200 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007201 int first_entry,
7202 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007203{
7204 static int did_bufnr_emsg;
7205 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007206 int bufnum, valid, status, col, end_col, vcol, nr;
7207 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007208
7209 if (first_entry)
7210 did_bufnr_emsg = FALSE;
7211
Bram Moolenaard61efa52022-07-23 09:52:04 +01007212 filename = dict_get_string(d, "filename", TRUE);
7213 module = dict_get_string(d, "module", TRUE);
7214 bufnum = (int)dict_get_number(d, "bufnr");
7215 lnum = (int)dict_get_number(d, "lnum");
7216 end_lnum = (int)dict_get_number(d, "end_lnum");
7217 col = (int)dict_get_number(d, "col");
7218 end_col = (int)dict_get_number(d, "end_col");
7219 vcol = (int)dict_get_number(d, "vcol");
7220 nr = (int)dict_get_number(d, "nr");
7221 type = dict_get_string(d, "type", TRUE);
7222 pattern = dict_get_string(d, "pattern", TRUE);
7223 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007224 if (text == NULL)
7225 text = vim_strsave((char_u *)"");
7226
7227 valid = TRUE;
7228 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7229 valid = FALSE;
7230
7231 // Mark entries with non-existing buffer number as not valid. Give the
7232 // error message only once.
7233 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7234 {
7235 if (!did_bufnr_emsg)
7236 {
7237 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007238 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007239 }
7240 valid = FALSE;
7241 bufnum = 0;
7242 }
7243
7244 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007245 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007246 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007247
Bram Moolenaar0398e002019-03-21 21:12:49 +01007248 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007249 NULL, // dir
7250 filename,
7251 module,
7252 bufnum,
7253 text,
7254 lnum,
thinca6864efa2021-06-19 20:45:20 +02007255 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007256 col,
thinca6864efa2021-06-19 20:45:20 +02007257 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007258 vcol, // vis_col
7259 pattern, // search pattern
7260 nr,
7261 type == NULL ? NUL : *type,
7262 valid);
7263
7264 vim_free(filename);
7265 vim_free(module);
7266 vim_free(pattern);
7267 vim_free(text);
7268 vim_free(type);
7269
Bram Moolenaar9752c722018-12-22 16:49:34 +01007270 if (valid)
7271 *valid_entry = TRUE;
7272
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007273 return status;
7274}
7275
7276/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007277 * Add list of entries to quickfix/location list. Each list entry is
7278 * a dictionary with item information.
7279 */
7280 static int
7281qf_add_entries(
7282 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007283 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007284 list_T *list,
7285 char_u *title,
7286 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007287{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007288 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007289 listitem_T *li;
7290 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007291 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007292 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007293 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007294
Bram Moolenaara3921f42017-06-04 15:30:34 +02007295 if (action == ' ' || qf_idx == qi->qf_listcount)
7296 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007297 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007298 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007299 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007300 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007301 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007302 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007303 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007304 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007305 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007306 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007307 qf_free_items(qfl);
7308 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007309 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007310
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007311 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007312 {
7313 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007314 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007315
7316 d = li->li_tv.vval.v_dict;
7317 if (d == NULL)
7318 continue;
7319
Bram Moolenaar0398e002019-03-21 21:12:49 +01007320 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007321 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007322 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007323 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007324 }
7325
Bram Moolenaar9752c722018-12-22 16:49:34 +01007326 // Check if any valid error entries are added to the list.
7327 if (valid_entry)
7328 qfl->qf_nonevalid = FALSE;
7329 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007330 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007331 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007332
7333 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007334 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007335 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007336
7337 // Update the current error index if not appending to the list or if the
7338 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007339 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007340 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007341
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007342 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007343 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007344
7345 return retval;
7346}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007347
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007348/*
7349 * Get the quickfix list index from 'nr' or 'id'
7350 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007351 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007352qf_setprop_get_qfidx(
7353 qf_info_T *qi,
7354 dict_T *what,
7355 int action,
7356 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007357{
7358 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007359 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007360
Bram Moolenaard823fa92016-08-12 16:29:27 +02007361 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7362 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007363 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007364 if (di->di_tv.v_type == VAR_NUMBER)
7365 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007366 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007367 if (di->di_tv.vval.v_number != 0)
7368 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007369
Bram Moolenaar55b69262017-08-13 13:42:01 +02007370 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7371 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007372 // When creating a new list, accept qf_idx pointing to the next
7373 // non-available list and add the new list at the end of the
7374 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007375 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007376 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007377 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007378 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007379 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007380 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007381 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007382 }
7383 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007384 && di->di_tv.vval.v_string != NULL
7385 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007386 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007387 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007388 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007389 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007390 qf_idx = 0;
7391 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007392 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007393 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007394 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007395 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007396 }
7397
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007398 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007399 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007400 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007401 if (di->di_tv.v_type != VAR_NUMBER)
7402 return INVALID_QFIDX;
7403
7404 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007405 }
7406
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007407 return qf_idx;
7408}
7409
7410/*
7411 * Set the quickfix list title.
7412 */
7413 static int
7414qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7415{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007416 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007417
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007418 if (di->di_tv.v_type != VAR_STRING)
7419 return FAIL;
7420
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007421 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007422 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007423 if (qf_idx == qi->qf_curlist)
7424 qf_update_win_titlevar(qi);
7425
7426 return OK;
7427}
7428
7429/*
7430 * Set quickfix list items/entries.
7431 */
7432 static int
7433qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7434{
7435 int retval = FAIL;
7436 char_u *title_save;
7437
7438 if (di->di_tv.v_type != VAR_LIST)
7439 return FAIL;
7440
7441 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7442 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7443 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007444 vim_free(title_save);
7445
7446 return retval;
7447}
7448
7449/*
7450 * Set quickfix list items/entries from a list of lines.
7451 */
7452 static int
7453qf_setprop_items_from_lines(
7454 qf_info_T *qi,
7455 int qf_idx,
7456 dict_T *what,
7457 dictitem_T *di,
7458 int action)
7459{
7460 char_u *errorformat = p_efm;
7461 dictitem_T *efm_di;
7462 int retval = FAIL;
7463
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007464 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007465 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7466 {
7467 if (efm_di->di_tv.v_type != VAR_STRING ||
7468 efm_di->di_tv.vval.v_string == NULL)
7469 return FAIL;
7470 errorformat = efm_di->di_tv.vval.v_string;
7471 }
7472
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007473 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007474 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7475 return FAIL;
7476
7477 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007478 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007479 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007480 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007481 retval = OK;
7482
7483 return retval;
7484}
7485
7486/*
7487 * Set quickfix list context.
7488 */
7489 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007490qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007491{
7492 typval_T *ctx;
7493
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007494 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007495 ctx = alloc_tv();
7496 if (ctx != NULL)
7497 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007498 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007499
7500 return OK;
7501}
7502
7503/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007504 * Set the current index in the specified quickfix list
7505 */
7506 static int
7507qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7508{
7509 int denote = FALSE;
7510 int newidx;
7511 int old_qfidx;
7512 qfline_T *qf_ptr;
7513
7514 // If the specified index is '$', then use the last entry
7515 if (di->di_tv.v_type == VAR_STRING
7516 && di->di_tv.vval.v_string != NULL
7517 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7518 newidx = qfl->qf_count;
7519 else
7520 {
7521 // Otherwise use the specified index
7522 newidx = tv_get_number_chk(&di->di_tv, &denote);
7523 if (denote)
7524 return FAIL;
7525 }
7526
7527 if (newidx < 1) // sanity check
7528 return FAIL;
7529 if (newidx > qfl->qf_count)
7530 newidx = qfl->qf_count;
7531
7532 old_qfidx = qfl->qf_index;
7533 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7534 if (qf_ptr == NULL)
7535 return FAIL;
7536 qfl->qf_ptr = qf_ptr;
7537 qfl->qf_index = newidx;
7538
7539 // If the current list is modified and it is displayed in the quickfix
7540 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007541 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007542 qf_win_pos_update(qi, old_qfidx);
7543
7544 return OK;
7545}
7546
7547/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007548 * Set the current index in the specified quickfix list
7549 */
7550 static int
7551qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7552{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007553 callback_T cb;
7554
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007555 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007556 cb = get_callback(&di->di_tv);
7557 if (cb.cb_name != NULL && *cb.cb_name != NUL)
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007558 set_callback(&qfl->qf_qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007559
7560 return OK;
7561}
7562
7563/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007564 * Set quickfix/location list properties (title, items, context).
7565 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007566 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007567 */
7568 static int
7569qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7570{
7571 dictitem_T *di;
7572 int retval = FAIL;
7573 int qf_idx;
7574 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007575 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007576
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007577 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007578 newlist = TRUE;
7579
7580 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007581 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007582 return FAIL;
7583
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007584 if (newlist)
7585 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007586 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007587 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007588 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007589 }
7590
Bram Moolenaar0398e002019-03-21 21:12:49 +01007591 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007592 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007593 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007594 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007595 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007596 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007597 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007598 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007599 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007600 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7601 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007602 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7603 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007604
Bram Moolenaar287153c2020-11-29 14:20:27 +01007605 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007606 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007607 if (newlist)
7608 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007609
Bram Moolenaard823fa92016-08-12 16:29:27 +02007610 return retval;
7611}
7612
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007613/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007614 * Free the entire quickfix/location list stack.
7615 * If the quickfix/location list window is open, then clear it.
7616 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007617 static void
7618qf_free_stack(win_T *wp, qf_info_T *qi)
7619{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007620 win_T *qfwin = qf_find_win(qi);
7621 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007622
7623 if (qfwin != NULL)
7624 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007625 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007626 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007627 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007628 qf_update_buffer(qi, NULL);
7629 }
7630
7631 if (wp != NULL && IS_LL_WINDOW(wp))
7632 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007633 // If in the location list window, then use the non-location list
7634 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007635 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007636 if (llwin != NULL)
7637 wp = llwin;
7638 }
7639
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007640 qf_free_all(wp);
7641 if (wp == NULL)
7642 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007643 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007644 qi->qf_curlist = 0;
7645 qi->qf_listcount = 0;
7646 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007647 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007648 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007649 // If the location list window is open, then create a new empty
7650 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007651 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007652
Bram Moolenaar95946f12019-03-31 15:31:59 +02007653 if (new_ll != NULL)
7654 {
7655 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007656
Bram Moolenaar95946f12019-03-31 15:31:59 +02007657 // first free the list reference in the location list window
7658 ll_free_all(&qfwin->w_llist_ref);
7659
7660 qfwin->w_llist_ref = new_ll;
7661 if (wp != qfwin)
7662 win_set_loclist(wp, new_ll);
7663 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007664 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007665}
7666
Bram Moolenaard823fa92016-08-12 16:29:27 +02007667/*
7668 * Populate the quickfix list with the items supplied in the list
7669 * of dictionaries. "title" will be copied to w:quickfix_title.
7670 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007671 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007672 */
7673 int
7674set_errorlist(
7675 win_T *wp,
7676 list_T *list,
7677 int action,
7678 char_u *title,
7679 dict_T *what)
7680{
7681 qf_info_T *qi = &ql_info;
7682 int retval = OK;
7683
7684 if (wp != NULL)
7685 {
7686 qi = ll_get_or_alloc_list(wp);
7687 if (qi == NULL)
7688 return FAIL;
7689 }
7690
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007691 if (action == 'f')
7692 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007693 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007694 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007695 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007696 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007697
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007698 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007699 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007700 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007701 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007702 _("cannot have both a list and a \"what\" argument"));
7703 return FAIL;
7704 }
7705
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007706 incr_quickfix_busy();
7707
7708 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007709 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007710 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007711 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007712 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007713 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007714 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007715 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007716
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007717 decr_quickfix_busy();
7718
Bram Moolenaard823fa92016-08-12 16:29:27 +02007719 return retval;
7720}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007721
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007722/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007723 * Mark the quickfix context and callback function as in use for all the lists
7724 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007725 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007726 static int
7727mark_quickfix_ctx(qf_info_T *qi, int copyID)
7728{
7729 int i;
7730 int abort = FALSE;
7731 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007732 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007733
7734 for (i = 0; i < LISTCOUNT && !abort; ++i)
7735 {
7736 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007737 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7738 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007739 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7740
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007741 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007742 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007743 }
7744
7745 return abort;
7746}
7747
7748/*
7749 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007750 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007751 */
7752 int
7753set_ref_in_quickfix(int copyID)
7754{
7755 int abort = FALSE;
7756 tabpage_T *tp;
7757 win_T *win;
7758
7759 abort = mark_quickfix_ctx(&ql_info, copyID);
7760 if (abort)
7761 return abort;
7762
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007763 abort = set_ref_in_callback(&qftf_cb, copyID);
7764 if (abort)
7765 return abort;
7766
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007767 FOR_ALL_TAB_WINDOWS(tp, win)
7768 {
7769 if (win->w_llist != NULL)
7770 {
7771 abort = mark_quickfix_ctx(win->w_llist, copyID);
7772 if (abort)
7773 return abort;
7774 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007775 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7776 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007777 // In a location list window and none of the other windows is
7778 // referring to this location list. Mark the location list
7779 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007780 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7781 if (abort)
7782 return abort;
7783 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007784 }
7785
7786 return abort;
7787}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007788#endif
7789
Bram Moolenaar81695252004-12-29 20:58:21 +00007790/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007791 * Return the autocmd name for the :cbuffer Ex commands
7792 */
7793 static char_u *
7794cbuffer_get_auname(cmdidx_T cmdidx)
7795{
7796 switch (cmdidx)
7797 {
7798 case CMD_cbuffer: return (char_u *)"cbuffer";
7799 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7800 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7801 case CMD_lbuffer: return (char_u *)"lbuffer";
7802 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7803 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7804 default: return NULL;
7805 }
7806}
7807
7808/*
7809 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7810 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7811 */
7812 static int
7813cbuffer_process_args(
7814 exarg_T *eap,
7815 buf_T **bufp,
7816 linenr_T *line1,
7817 linenr_T *line2)
7818{
7819 buf_T *buf = NULL;
7820
7821 if (*eap->arg == NUL)
7822 buf = curbuf;
7823 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7824 buf = buflist_findnr(atoi((char *)eap->arg));
7825
7826 if (buf == NULL)
7827 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007828 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007829 return FAIL;
7830 }
7831
7832 if (buf->b_ml.ml_mfp == NULL)
7833 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007834 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007835 return FAIL;
7836 }
7837
7838 if (eap->addr_count == 0)
7839 {
7840 eap->line1 = 1;
7841 eap->line2 = buf->b_ml.ml_line_count;
7842 }
7843
7844 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7845 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7846 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007847 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007848 return FAIL;
7849 }
7850
7851 *line1 = eap->line1;
7852 *line2 = eap->line2;
7853 *bufp = buf;
7854
7855 return OK;
7856}
7857
7858/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007859 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007860 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007861 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007862 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007863 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007864 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007865 */
7866 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007867ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007868{
7869 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007870 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007871 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007872 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007873 int_u save_qfid;
7874 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007875 char_u *qf_title;
7876 linenr_T line1;
7877 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007878
Bram Moolenaara16123a2019-03-28 20:31:07 +01007879 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007880 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007881 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007882 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007883#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007884 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007885 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007886#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007887 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007888
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007889 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007890 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7891 if (qi == NULL)
7892 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007893
Bram Moolenaara16123a2019-03-28 20:31:07 +01007894 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7895 return;
7896
7897 qf_title = qf_cmdtitle(*eap->cmdlinep);
7898
7899 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007900 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007901 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7902 (char *)qf_title, (char *)buf->b_sfname);
7903 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007904 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007905
7906 incr_quickfix_busy();
7907
7908 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7909 (eap->cmdidx != CMD_caddbuffer
7910 && eap->cmdidx != CMD_laddbuffer),
7911 line1, line2,
7912 qf_title, NULL);
7913 if (qf_stack_empty(qi))
7914 {
7915 decr_quickfix_busy();
7916 return;
7917 }
7918 if (res >= 0)
7919 qf_list_changed(qf_get_curlist(qi));
7920
7921 // Remember the current quickfix list identifier, so that we can
7922 // check for autocommands changing the current quickfix list.
7923 save_qfid = qf_get_curlist(qi)->qf_id;
7924 if (au_name != NULL)
7925 {
7926 buf_T *curbuf_old = curbuf;
7927
7928 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7929 TRUE, curbuf);
7930 if (curbuf != curbuf_old)
7931 // Autocommands changed buffer, don't jump now, "qi" may
7932 // be invalid.
7933 res = 0;
7934 }
7935 // Jump to the first error for a new list and if autocmds didn't
7936 // free the list.
7937 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7938 eap->cmdidx == CMD_lbuffer)
7939 && qflist_valid(wp, save_qfid))
7940 // display the first error
7941 qf_jump_first(qi, save_qfid, eap->forceit);
7942
7943 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007944}
7945
Bram Moolenaar1e015462005-09-25 22:16:38 +00007946#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007947/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007948 * Return the autocmd name for the :cexpr Ex commands.
7949 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007950 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007951cexpr_get_auname(cmdidx_T cmdidx)
7952{
7953 switch (cmdidx)
7954 {
7955 case CMD_cexpr: return (char_u *)"cexpr";
7956 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7957 case CMD_caddexpr: return (char_u *)"caddexpr";
7958 case CMD_lexpr: return (char_u *)"lexpr";
7959 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7960 case CMD_laddexpr: return (char_u *)"laddexpr";
7961 default: return NULL;
7962 }
7963}
7964
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007965 int
7966trigger_cexpr_autocmd(int cmdidx)
7967{
7968 char_u *au_name = cexpr_get_auname(cmdidx);
7969
7970 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7971 curbuf->b_fname, TRUE, curbuf))
7972 {
7973 if (aborting())
7974 return FAIL;
7975 }
7976 return OK;
7977}
7978
7979 int
7980cexpr_core(exarg_T *eap, typval_T *tv)
7981{
7982 qf_info_T *qi;
7983 win_T *wp = NULL;
7984
7985 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7986 if (qi == NULL)
7987 return FAIL;
7988
7989 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7990 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7991 {
7992 int res;
7993 int_u save_qfid;
7994 char_u *au_name = cexpr_get_auname(eap->cmdidx);
7995
7996 incr_quickfix_busy();
7997 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
7998 (eap->cmdidx != CMD_caddexpr
7999 && eap->cmdidx != CMD_laddexpr),
8000 (linenr_T)0, (linenr_T)0,
8001 qf_cmdtitle(*eap->cmdlinep), NULL);
8002 if (qf_stack_empty(qi))
8003 {
8004 decr_quickfix_busy();
8005 return FAIL;
8006 }
8007 if (res >= 0)
8008 qf_list_changed(qf_get_curlist(qi));
8009
8010 // Remember the current quickfix list identifier, so that we can
8011 // check for autocommands changing the current quickfix list.
8012 save_qfid = qf_get_curlist(qi)->qf_id;
8013 if (au_name != NULL)
8014 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8015 curbuf->b_fname, TRUE, curbuf);
8016
8017 // Jump to the first error for a new list and if autocmds didn't
8018 // free the list.
8019 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8020 && qflist_valid(wp, save_qfid))
8021 // display the first error
8022 qf_jump_first(qi, save_qfid, eap->forceit);
8023 decr_quickfix_busy();
8024 return OK;
8025 }
8026
Bram Moolenaar677658a2022-01-05 16:09:06 +00008027 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008028 return FAIL;
8029}
8030
Bram Moolenaara16123a2019-03-28 20:31:07 +01008031/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008032 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8033 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008034 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008035 */
8036 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008037ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008038{
8039 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008040
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008041 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008042 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008043
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008044 // Evaluate the expression. When the result is a string or a list we can
8045 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008046 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008047 if (tv != NULL)
8048 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008049 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008050 free_tv(tv);
8051 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008052}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008053#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008054
8055/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008056 * Get the location list for ":lhelpgrep"
8057 */
8058 static qf_info_T *
8059hgr_get_ll(int *new_ll)
8060{
8061 win_T *wp;
8062 qf_info_T *qi;
8063
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008064 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008065 if (bt_help(curwin->w_buffer))
8066 wp = curwin;
8067 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008068 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008069 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008070
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008071 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008072 qi = NULL;
8073 else
8074 qi = wp->w_llist;
8075
8076 if (qi == NULL)
8077 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008078 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008079 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008080 return NULL;
8081 *new_ll = TRUE;
8082 }
8083
8084 return qi;
8085}
8086
8087/*
8088 * Search for a pattern in a help file.
8089 */
8090 static void
8091hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008092 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008093 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008094 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008095 regmatch_T *p_regmatch)
8096{
8097 FILE *fd;
8098 long lnum;
8099
8100 fd = mch_fopen((char *)fname, "r");
8101 if (fd == NULL)
8102 return;
8103
8104 lnum = 1;
8105 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8106 {
8107 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008108
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008109 // Convert a line if 'encoding' is not utf-8 and
8110 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008111 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008112 {
8113 line = string_convert(p_vc, IObuff, NULL);
8114 if (line == NULL)
8115 line = IObuff;
8116 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008117
8118 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8119 {
8120 int l = (int)STRLEN(line);
8121
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008122 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008123 while (l > 0 && line[l - 1] <= ' ')
8124 line[--l] = NUL;
8125
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008126 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008127 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008128 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008129 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008130 0,
8131 line,
8132 lnum,
thinca6864efa2021-06-19 20:45:20 +02008133 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008134 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008135 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008136 (int)(p_regmatch->endp[0] - line)
8137 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008138 FALSE, // vis_col
8139 NULL, // search pattern
8140 0, // nr
8141 1, // type
8142 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008143 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008144 {
8145 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008146 if (line != IObuff)
8147 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008148 break;
8149 }
8150 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008151 if (line != IObuff)
8152 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008153 ++lnum;
8154 line_breakcheck();
8155 }
8156 fclose(fd);
8157}
8158
8159/*
8160 * Search for a pattern in all the help files in the doc directory under
8161 * the given directory.
8162 */
8163 static void
8164hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008165 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008166 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008167 regmatch_T *p_regmatch,
8168 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008169#ifdef FEAT_MULTI_LANG
8170 , char_u *lang
8171#endif
8172 )
8173{
8174 int fcount;
8175 char_u **fnames;
8176 int fi;
8177
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008178 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008179 add_pathsep(dirname);
8180 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8181 if (gen_expand_wildcards(1, &dirname, &fcount,
8182 &fnames, EW_FILE|EW_SILENT) == OK
8183 && fcount > 0)
8184 {
8185 for (fi = 0; fi < fcount && !got_int; ++fi)
8186 {
8187#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008188 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008189 if (lang != NULL
8190 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008191 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008192 && !(STRNICMP(lang, "en", 2) == 0
8193 && STRNICMP("txt", fnames[fi]
8194 + STRLEN(fnames[fi]) - 3, 3) == 0))
8195 continue;
8196#endif
8197
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008198 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008199 }
8200 FreeWild(fcount, fnames);
8201 }
8202}
8203
8204/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008205 * Search for a pattern in all the help files in the 'runtimepath'
8206 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008207 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008208 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008209 */
8210 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008211hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008212{
8213 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008214
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008215 vimconv_T vc;
8216
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008217 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8218 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008219 vc.vc_type = CONV_NONE;
8220 if (!enc_utf8)
8221 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008222
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008223 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008224 p = p_rtp;
8225 while (*p != NUL && !got_int)
8226 {
8227 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8228
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008229 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008230#ifdef FEAT_MULTI_LANG
8231 , lang
8232#endif
8233 );
8234 }
8235
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008236 if (vc.vc_type != CONV_NONE)
8237 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008238}
8239
8240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 * ":helpgrep {pattern}"
8242 */
8243 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008244ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
8246 regmatch_T regmatch;
8247 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008248 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008249 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008250 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008251 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008252 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008253 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254
Bram Moolenaar73633f82012-01-20 13:39:07 +01008255 switch (eap->cmdidx)
8256 {
8257 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8258 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8259 default: break;
8260 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008261 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8262 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008263 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008264#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008265 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008266 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008267#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008268 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008269
Bram Moolenaar39665952018-08-15 20:59:48 +02008270 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008271 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008272 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008273 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008274 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008275 }
8276
Bram Moolenaara16123a2019-03-28 20:31:07 +01008277 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8278 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008279 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008280 p_cpo = empty_option;
8281
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008282 incr_quickfix_busy();
8283
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008284#ifdef FEAT_MULTI_LANG
8285 // Check for a specified language
8286 lang = check_help_lang(eap->arg);
8287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8289 regmatch.rm_ic = FALSE;
8290 if (regmatch.regprog != NULL)
8291 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008292 qf_list_T *qfl;
8293
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008294 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008295 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008296 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008298 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008299
Bram Moolenaar473de612013-06-08 18:19:48 +02008300 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008302 qfl->qf_nonevalid = FALSE;
8303 qfl->qf_ptr = qfl->qf_start;
8304 qfl->qf_index = 1;
8305 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008306 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008309 if (p_cpo == empty_option)
8310 p_cpo = save_cpo;
8311 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008312 {
8313 // Darn, some plugin changed the value. If it's still empty it was
8314 // changed and restored, need to restore in the complicated way.
8315 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008316 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008317 if (save_cpo_allocated)
8318 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008319 }
8320
8321 if (updated)
8322 // This may open a window and source scripts, do this after 'cpo' was
8323 // restored.
8324 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
Bram Moolenaar73633f82012-01-20 13:39:07 +01008326 if (au_name != NULL)
8327 {
8328 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8329 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008330 // When adding a location list to an existing location list stack,
8331 // if the autocmd made the stack invalid, then just return.
8332 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008333 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008334 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008335 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008336 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008337 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008338
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008339 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008340 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008341 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008342 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008343 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008344
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008345 decr_quickfix_busy();
8346
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008347 if (eap->cmdidx == CMD_lhelpgrep)
8348 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008349 // If the help window is not opened or if it already points to the
8350 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008351 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008352 {
8353 if (new_qi)
8354 ll_free_all(&qi);
8355 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008356 else if (curwin->w_llist == NULL && new_qi)
8357 // current window didn't have a location list associated with it
8358 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008359 curwin->w_llist = qi;
8360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008362#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008363
8364#if defined(FEAT_EVAL) || defined(PROTO)
8365# ifdef FEAT_QUICKFIX
8366 static void
8367get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8368{
8369 if (what_arg->v_type == VAR_UNKNOWN)
8370 {
8371 if (rettv_list_alloc(rettv) == OK)
8372 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008373 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008374 }
8375 else
8376 {
8377 if (rettv_dict_alloc(rettv) == OK)
8378 if (is_qf || (wp != NULL))
8379 {
8380 if (what_arg->v_type == VAR_DICT)
8381 {
8382 dict_T *d = what_arg->vval.v_dict;
8383
8384 if (d != NULL)
8385 qf_get_properties(wp, d, rettv->vval.v_dict);
8386 }
8387 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008388 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008389 }
8390 }
8391}
8392# endif
8393
8394/*
8395 * "getloclist()" function
8396 */
8397 void
8398f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8399{
8400# ifdef FEAT_QUICKFIX
8401 win_T *wp;
8402
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008403 if (in_vim9script()
8404 && (check_for_number_arg(argvars, 0) == FAIL
8405 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8406 return;
8407
Bram Moolenaare677df82019-09-02 22:31:11 +02008408 wp = find_win_by_nr_or_id(&argvars[0]);
8409 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8410# endif
8411}
8412
8413/*
8414 * "getqflist()" function
8415 */
8416 void
8417f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8418{
8419# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008420 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8421 return;
8422
Bram Moolenaare677df82019-09-02 22:31:11 +02008423 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8424# endif
8425}
8426
8427/*
8428 * Used by "setqflist()" and "setloclist()" functions
8429 */
8430 static void
8431set_qf_ll_list(
8432 win_T *wp UNUSED,
8433 typval_T *list_arg UNUSED,
8434 typval_T *action_arg UNUSED,
8435 typval_T *what_arg UNUSED,
8436 typval_T *rettv)
8437{
8438# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008439 char_u *act;
8440 int action = 0;
8441 static int recursive = 0;
8442# endif
8443
8444 rettv->vval.v_number = -1;
8445
8446# ifdef FEAT_QUICKFIX
8447 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008448 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008449 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008450 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008451 else
8452 {
8453 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008454 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008455 int valid_dict = TRUE;
8456
8457 if (action_arg->v_type == VAR_STRING)
8458 {
8459 act = tv_get_string_chk(action_arg);
8460 if (act == NULL)
8461 return; // type error; errmsg already given
8462 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8463 act[1] == NUL)
8464 action = *act;
8465 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008466 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008467 }
8468 else if (action_arg->v_type == VAR_UNKNOWN)
8469 action = ' ';
8470 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008471 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008472
8473 if (action_arg->v_type != VAR_UNKNOWN
8474 && what_arg->v_type != VAR_UNKNOWN)
8475 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008476 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8477 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008478 else
8479 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008480 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008481 valid_dict = FALSE;
8482 }
8483 }
8484
8485 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008486 if (l != NULL && action && valid_dict
8487 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008488 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008489 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008490 rettv->vval.v_number = 0;
8491 --recursive;
8492 }
8493# endif
8494}
8495
8496/*
8497 * "setloclist()" function
8498 */
8499 void
8500f_setloclist(typval_T *argvars, typval_T *rettv)
8501{
8502 win_T *win;
8503
8504 rettv->vval.v_number = -1;
8505
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008506 if (in_vim9script()
8507 && (check_for_number_arg(argvars, 0) == FAIL
8508 || check_for_list_arg(argvars, 1) == FAIL
8509 || check_for_opt_string_arg(argvars, 2) == FAIL
8510 || (argvars[2].v_type != VAR_UNKNOWN
8511 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8512 return;
8513
Bram Moolenaare677df82019-09-02 22:31:11 +02008514 win = find_win_by_nr_or_id(&argvars[0]);
8515 if (win != NULL)
8516 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8517}
8518
8519/*
8520 * "setqflist()" function
8521 */
8522 void
8523f_setqflist(typval_T *argvars, typval_T *rettv)
8524{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008525 if (in_vim9script()
8526 && (check_for_list_arg(argvars, 0) == FAIL
8527 || check_for_opt_string_arg(argvars, 1) == FAIL
8528 || (argvars[1].v_type != VAR_UNKNOWN
8529 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8530 return;
8531
Bram Moolenaare677df82019-09-02 22:31:11 +02008532 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8533}
8534#endif