blob: 3643f2740392f1cdc9c4fc69bb843d188e8560a3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
thinca6864efa2021-06-19 20:45:20 +020033 linenr_T qf_end_lnum; // line number when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020034 int qf_fnum; // file number for the line
35 int qf_col; // column where the error occurred
thinca6864efa2021-06-19 20:45:20 +020036 int qf_end_col; // column when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020037 int qf_nr; // error number
38 char_u *qf_module; // module name for this error
39 char_u *qf_pattern; // search pattern for the error
40 char_u *qf_text; // description of the error
thinca6864efa2021-06-19 20:45:20 +020041 char_u qf_viscol; // set to TRUE if qf_col and qf_end_col is
42 // screen column
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020043 char_u qf_cleared; // set to TRUE if line has been deleted
44 char_u qf_type; // type of the error (mostly 'E'); 1 for
45 // :helpgrep
46 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000047};
48
49/*
50 * There is a stack of error lists.
51 */
52#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020053#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010054#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020056/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010057 * Quickfix list type.
58 */
59typedef enum
60{
61 QFLT_QUICKFIX, // Quickfix list - global list
62 QFLT_LOCATION, // Location list - per window list
63 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
64} qfltype_T;
65
66/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 * Quickfix/Location list definition
68 * Contains a list of entries (qfline_T). qf_start points to the first entry
69 * and qf_last points to the last entry. qf_count contains the list size.
70 *
71 * Usually the list contains one or more entries. But an empty list can be
72 * created using setqflist()/setloclist() with a title and/or user context
73 * information and entries can be added later using setqflist()/setloclist().
74 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000075typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020077 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010078 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020079 qfline_T *qf_start; // pointer to the first error
80 qfline_T *qf_last; // pointer to the last error
81 qfline_T *qf_ptr; // pointer to the current error
82 int qf_count; // number of errors (0 means empty list)
83 int qf_index; // current index in the error list
84 int qf_nonevalid; // TRUE if not a single valid entry found
85 char_u *qf_title; // title derived from the command that created
86 // the error list or set by setqflist
87 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaard43906d2020-07-20 21:31:32 +020088 callback_T qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020089
90 struct dir_stack_T *qf_dir_stack;
91 char_u *qf_directory;
92 struct dir_stack_T *qf_file_stack;
93 char_u *qf_currfile;
94 int qf_multiline;
95 int qf_multiignore;
96 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010097 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000098} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar6a8958d2017-06-22 21:33:20 +0200100/*
101 * Quickfix/Location list stack definition
102 * Contains a list of quickfix/location lists (qf_list_T)
103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000104struct qf_info_S
105{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 // Count of references to this list. Used only for location lists.
107 // When a location list window reference this list, qf_refcount
108 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
109 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200111 int qf_listcount; // current number of lists
112 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100114 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100115 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000116};
117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200118static qf_info_T ql_info; // global quickfix list
119static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200121#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
124 * Structure used to hold the info of one part of 'errorformat'
125 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000126typedef struct efm_S efm_T;
127struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200129 regprog_T *prog; // pre-formatted part of 'errorformat'
130 efm_T *next; // pointer to next (NULL if last)
131 char_u addr[FMT_PATTERNS]; // indices of used % patterns
132 char_u prefix; // prefix of this format line:
133 // 'D' enter directory
134 // 'X' leave directory
135 // 'A' start of multi-line message
136 // 'E' error message
137 // 'W' warning message
138 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200139 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200140 // 'C' continuation line
141 // 'Z' end of multi-line message
142 // 'G' general, unspecific message
143 // 'P' push file (partial) message
144 // 'Q' pop/quit file (partial) message
145 // 'O' overread (partial) message
146 char_u flags; // additional flags given in prefix
147 // '-' do not include this line
148 // '+' include whole line in message
149 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150};
151
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200152// List of location lists to be deleted.
153// Used to delay the deletion of locations lists by autocmds.
154typedef struct qf_delq_S
155{
156 struct qf_delq_S *next;
157 qf_info_T *qi;
158} qf_delq_T;
159static qf_delq_T *qf_delq_head = NULL;
160
161// Counter to prevent autocmds from freeing up location lists when they are
162// still being used.
163static int quickfix_busy = 0;
164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200165static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100166
Bram Moolenaard43906d2020-07-20 21:31:32 +0200167// callback function for 'quickfixtextfunc'
168static callback_T qftf_cb;
169
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_new_list(qf_info_T *qi, char_u *qf_title);
thinca6864efa2021-06-19 20:45:20 +0200171static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, long end_lnum, int col, int end_col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200172static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100174static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200175static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200177static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200178static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
thinca6864efa2021-06-19 20:45:20 +0200180static void qf_range_text(qfline_T *qfp, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100181static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static win_T *qf_find_win(qf_info_T *qi);
183static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200184static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200185static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100186static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
187static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
188static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
189static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200191// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200193// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200195
196// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100197#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
198#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
199#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
200#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200201
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000202/*
203 * Return location list for window 'wp'
204 * For location list window, return the referenced location list
205 */
206#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
207
Bram Moolenaar95946f12019-03-31 15:31:59 +0200208// Macro to loop through all the items in a quickfix list
209// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100210#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200211 for (i = 1, qfp = qfl->qf_start; \
212 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100213 ++i, qfp = qfp->qf_next)
214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200216 * Looking up a buffer can be slow if there are many. Remember the last one
217 * to make this a lot faster if there are multiple matches in the same file.
218 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200219static char_u *qf_last_bufname = NULL;
220static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200221
222/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200223 * Maximum number of bytes allowed per line while reading a errorfile.
224 */
225#define LINE_MAXLEN 4096
226
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200227static struct fmtpattern
228{
229 char_u convchar;
230 char *pattern;
231} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200232 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200233 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200234 {'n', "\\d\\+"},
235 {'l', "\\d\\+"},
236 {'c', "\\d\\+"},
237 {'t', "."},
238 {'m', ".\\+"},
239 {'r', ".*"},
240 {'p', "[- .]*"},
241 {'v', "\\d\\+"},
242 {'s', ".\\+"},
243 {'o', ".\\+"}
244 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200245
246/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200247 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200248 * See fmt_pat definition above for the list of supported patterns. The
249 * pattern specifier is supplied in "efmpat". The converted pattern is stored
250 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200251 */
252 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200253efmpat_to_regpat(
254 char_u *efmpat,
255 char_u *regpat,
256 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100258 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200259{
260 char_u *srcptr;
261
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200262 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200263 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200264 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000265 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266 return NULL;
267 }
268 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200271 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200272 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000273 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200274 return NULL;
275 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 efminfo->addr[idx] = (char_u)++round;
277 *regpat++ = '\\';
278 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200282 // Also match "c:" in the file name, even when
283 // checking for a colon next: "%f:".
284 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200285 STRCPY(regpat, "\\%(\\a:\\)\\=");
286 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200287 }
288#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200289 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200290 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200293 // A file name may contain spaces, but this isn't
294 // in "\f". For "%f:%l:%m" there may be a ":" in
295 // the file name. Use ".\{-1,}x" instead (x is
296 // the next character), the requirement that :999:
297 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 STRCPY(regpat, ".\\{-1,}");
299 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200300 }
301 else
302 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200303 // File name followed by '\\' or '%': include as
304 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 STRCPY(regpat, "\\f\\+");
306 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 }
308 }
309 else
310 {
311 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 while ((*regpat = *srcptr++) != NUL)
313 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200315 *regpat++ = '\\';
316 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200317
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200318 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319}
320
321/*
322 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200323 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324 */
325 static char_u *
326scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200327 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200328 char_u *efm,
329 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100330 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331{
332 char_u *efmp = *pefmp;
333
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200336 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 {
338 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200339 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 if (efmp < efm + len)
341 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200342 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200344 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200345 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 if (efmp == efm + len)
347 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000348 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200349 return NULL;
350 }
351 }
352 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200353 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200354 *regpat++ = *++efmp;
355 *regpat++ = '\\';
356 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200357 }
358 else
359 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200360 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000361 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362 return NULL;
363 }
364
365 *pefmp = efmp;
366
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200367 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368}
369
370/*
371 * Analyze/parse an errorformat prefix.
372 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200373 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100374efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200376 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200377 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200378 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200379 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200380 else
381 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000382 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200383 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200384 }
385
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200386 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200387}
388
389/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 * Converts a 'errorformat' string part in 'efm' to a regular expression
391 * pattern. The resulting regex pattern is returned in "regpat". Additional
392 * information about the 'erroformat' pattern is returned in "fmt_ptr".
393 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200394 */
395 static int
396efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200397 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200398 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200399 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100400 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200401{
402 char_u *ptr;
403 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200404 int round;
405 int idx = 0;
406
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200407 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200408 ptr = regpat;
409 *ptr++ = '^';
410 round = 0;
411 for (efmp = efm; efmp < efm + len; ++efmp)
412 {
413 if (*efmp == '%')
414 {
415 ++efmp;
416 for (idx = 0; idx < FMT_PATTERNS; ++idx)
417 if (fmt_pat[idx].convchar == *efmp)
418 break;
419 if (idx < FMT_PATTERNS)
420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100421 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200422 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200423 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200424 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200425 }
426 else if (*efmp == '*')
427 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100429 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200430 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200431 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200432 }
433 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200434 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200435 else if (*efmp == '#')
436 *ptr++ = '*';
437 else if (*efmp == '>')
438 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200439 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200440 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200441 // prefix is allowed only at the beginning of the errorformat
442 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100443 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200444 if (efmp == NULL)
445 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 }
447 else
448 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000449 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200450 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200451 }
452 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200453 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200454 {
455 if (*efmp == '\\' && efmp + 1 < efm + len)
456 ++efmp;
457 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200458 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200459 if (*efmp)
460 *ptr++ = *efmp;
461 }
462 }
463 *ptr++ = '$';
464 *ptr = NUL;
465
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200466 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200467}
468
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200469/*
470 * Free the 'errorformat' information list
471 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200472 static void
473free_efm_list(efm_T **efm_first)
474{
475 efm_T *efm_ptr;
476
477 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
478 {
479 *efm_first = efm_ptr->next;
480 vim_regfree(efm_ptr->prog);
481 vim_free(efm_ptr);
482 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100483 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200484}
485
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200486/*
487 * Compute the size of the buffer used to convert a 'errorformat' pattern into
488 * a regular expression pattern.
489 */
490 static int
491efm_regpat_bufsz(char_u *efm)
492{
493 int sz;
494 int i;
495
496 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
497 for (i = FMT_PATTERNS; i > 0; )
498 sz += (int)STRLEN(fmt_pat[--i].pattern);
499#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200500 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200501#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200502 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200503#endif
504
505 return sz;
506}
507
508/*
509 * Return the length of a 'errorformat' option part (separated by ",").
510 */
511 static int
512efm_option_part_len(char_u *efm)
513{
514 int len;
515
516 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
517 if (efm[len] == '\\' && efm[len + 1] != NUL)
518 ++len;
519
520 return len;
521}
522
523/*
524 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
525 * are parsed and converted to regular expressions. Returns information about
526 * the parsed 'errorformat' option.
527 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200528 static efm_T *
529parse_efm_option(char_u *efm)
530{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200531 efm_T *fmt_ptr = NULL;
532 efm_T *fmt_first = NULL;
533 efm_T *fmt_last = NULL;
534 char_u *fmtstr = NULL;
535 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200536 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200537
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200538 // Each part of the format string is copied and modified from errorformat
539 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200540
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200541 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200542 sz = efm_regpat_bufsz(efm);
543 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544 goto parse_efm_error;
545
546 while (efm[0] != NUL)
547 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200548 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200549 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200550 if (fmt_ptr == NULL)
551 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200552 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200553 fmt_first = fmt_ptr;
554 else
555 fmt_last->next = fmt_ptr;
556 fmt_last = fmt_ptr;
557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200558 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200559 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200560
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100561 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200562 goto parse_efm_error;
563 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
564 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200565 // Advance to next part
566 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567 }
568
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200569 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000570 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200571
572 goto parse_efm_end;
573
574parse_efm_error:
575 free_efm_list(&fmt_first);
576
577parse_efm_end:
578 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200579
580 return fmt_first;
581}
582
Bram Moolenaare0d37972016-07-15 22:36:01 +0200583enum {
584 QF_FAIL = 0,
585 QF_OK = 1,
586 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200587 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200588 QF_IGNORE_LINE = 4,
589 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200590};
591
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200592/*
593 * State information used to parse lines and add entries to a quickfix/location
594 * list.
595 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200596typedef struct {
597 char_u *linebuf;
598 int linelen;
599 char_u *growbuf;
600 int growbufsiz;
601 FILE *fd;
602 typval_T *tv;
603 char_u *p_str;
604 listitem_T *p_li;
605 buf_T *buf;
606 linenr_T buflnum;
607 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100608 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200609} qfstate_T;
610
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200611/*
612 * Allocate more memory for the line buffer used for parsing lines.
613 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200614 static char_u *
615qf_grow_linebuf(qfstate_T *state, int newsz)
616{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200617 char_u *p;
618
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200619 // If the line exceeds LINE_MAXLEN exclude the last
620 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200621 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
622 if (state->growbuf == NULL)
623 {
624 state->growbuf = alloc(state->linelen + 1);
625 if (state->growbuf == NULL)
626 return NULL;
627 state->growbufsiz = state->linelen;
628 }
629 else if (state->linelen > state->growbufsiz)
630 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200631 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200632 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200633 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200634 state->growbufsiz = state->linelen;
635 }
636 return state->growbuf;
637}
638
639/*
640 * Get the next string (separated by newline) from state->p_str.
641 */
642 static int
643qf_get_next_str_line(qfstate_T *state)
644{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200645 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200646 char_u *p_str = state->p_str;
647 char_u *p;
648 int len;
649
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200650 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200651 return QF_END_OF_INPUT;
652
653 p = vim_strchr(p_str, '\n');
654 if (p != NULL)
655 len = (int)(p - p_str) + 1;
656 else
657 len = (int)STRLEN(p_str);
658
659 if (len > IOSIZE - 2)
660 {
661 state->linebuf = qf_grow_linebuf(state, len);
662 if (state->linebuf == NULL)
663 return QF_NOMEM;
664 }
665 else
666 {
667 state->linebuf = IObuff;
668 state->linelen = len;
669 }
670 vim_strncpy(state->linebuf, p_str, state->linelen);
671
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200672 // Increment using len in order to discard the rest of the
673 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200674 p_str += len;
675 state->p_str = p_str;
676
677 return QF_OK;
678}
679
680/*
681 * Get the next string from state->p_Li.
682 */
683 static int
684qf_get_next_list_line(qfstate_T *state)
685{
686 listitem_T *p_li = state->p_li;
687 int len;
688
689 while (p_li != NULL
690 && (p_li->li_tv.v_type != VAR_STRING
691 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200692 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200693
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200694 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200695 {
696 state->p_li = NULL;
697 return QF_END_OF_INPUT;
698 }
699
700 len = (int)STRLEN(p_li->li_tv.vval.v_string);
701 if (len > IOSIZE - 2)
702 {
703 state->linebuf = qf_grow_linebuf(state, len);
704 if (state->linebuf == NULL)
705 return QF_NOMEM;
706 }
707 else
708 {
709 state->linebuf = IObuff;
710 state->linelen = len;
711 }
712
713 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
714
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200715 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200716 return QF_OK;
717}
718
719/*
720 * Get the next string from state->buf.
721 */
722 static int
723qf_get_next_buf_line(qfstate_T *state)
724{
725 char_u *p_buf = NULL;
726 int len;
727
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200728 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200729 if (state->buflnum > state->lnumlast)
730 return QF_END_OF_INPUT;
731
732 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
733 state->buflnum += 1;
734
735 len = (int)STRLEN(p_buf);
736 if (len > IOSIZE - 2)
737 {
738 state->linebuf = qf_grow_linebuf(state, len);
739 if (state->linebuf == NULL)
740 return QF_NOMEM;
741 }
742 else
743 {
744 state->linebuf = IObuff;
745 state->linelen = len;
746 }
747 vim_strncpy(state->linebuf, p_buf, state->linelen);
748
749 return QF_OK;
750}
751
752/*
753 * Get the next string from file state->fd.
754 */
755 static int
756qf_get_next_file_line(qfstate_T *state)
757{
758 int discard;
759 int growbuflen;
760
761 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
762 return QF_END_OF_INPUT;
763
764 discard = FALSE;
765 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200766 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200767 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200768 // The current line exceeds IObuff, continue reading using
769 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200770 if (state->growbuf == NULL)
771 {
772 state->growbufsiz = 2 * (IOSIZE - 1);
773 state->growbuf = alloc(state->growbufsiz);
774 if (state->growbuf == NULL)
775 return QF_NOMEM;
776 }
777
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200778 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200779 memcpy(state->growbuf, IObuff, IOSIZE - 1);
780 growbuflen = state->linelen;
781
782 for (;;)
783 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200784 char_u *p;
785
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 if (fgets((char *)state->growbuf + growbuflen,
787 state->growbufsiz - growbuflen, state->fd) == NULL)
788 break;
789 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
790 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200791 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200792 break;
793 if (state->growbufsiz == LINE_MAXLEN)
794 {
795 discard = TRUE;
796 break;
797 }
798
799 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
800 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200801 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200803 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804 }
805
806 while (discard)
807 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200808 // The current line is longer than LINE_MAXLEN, continue
809 // reading but discard everything until EOL or EOF is
810 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200811 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
812 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200813 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200814 break;
815 }
816
817 state->linebuf = state->growbuf;
818 state->linelen = growbuflen;
819 }
820 else
821 state->linebuf = IObuff;
822
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200823 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200824 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
825 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100826 char_u *line;
827
828 line = string_convert(&state->vc, state->linebuf, &state->linelen);
829 if (line != NULL)
830 {
831 if (state->linelen < IOSIZE)
832 {
833 STRCPY(state->linebuf, line);
834 vim_free(line);
835 }
836 else
837 {
838 vim_free(state->growbuf);
839 state->linebuf = state->growbuf = line;
840 state->growbufsiz = state->linelen < LINE_MAXLEN
841 ? state->linelen : LINE_MAXLEN;
842 }
843 }
844 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100845
Bram Moolenaare0d37972016-07-15 22:36:01 +0200846 return QF_OK;
847}
848
849/*
850 * Get the next string from a file/buffer/list/string.
851 */
852 static int
853qf_get_nextline(qfstate_T *state)
854{
855 int status = QF_FAIL;
856
857 if (state->fd == NULL)
858 {
859 if (state->tv != NULL)
860 {
861 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200862 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200863 status = qf_get_next_str_line(state);
864 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200865 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200866 status = qf_get_next_list_line(state);
867 }
868 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200869 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200870 status = qf_get_next_buf_line(state);
871 }
872 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 status = qf_get_next_file_line(state);
875
876 if (status != QF_OK)
877 return status;
878
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200879 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200880 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200881 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882 state->linebuf[state->linelen - 1] = NUL;
883#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200884 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
885 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200886#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200887 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888
Bram Moolenaare0d37972016-07-15 22:36:01 +0200889 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200890
891 return QF_OK;
892}
893
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200894typedef struct {
895 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200896 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200897 char_u *errmsg;
898 int errmsglen;
899 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200900 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200901 int col;
thinca6864efa2021-06-19 20:45:20 +0200902 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200903 char_u use_viscol;
904 char_u *pattern;
905 int enr;
906 int type;
907 int valid;
908} qffields_T;
909
910/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200911 * Parse the match for filename ('%f') pattern in regmatch.
912 * Return the matched value in "fields->namebuf".
913 */
914 static int
915qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
916{
917 int c;
918
919 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
920 return QF_FAIL;
921
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200922 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200923 c = *rmp->endp[midx];
924 *rmp->endp[midx] = NUL;
925 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
926 *rmp->endp[midx] = c;
927
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200928 // For separate filename patterns (%O, %P and %Q), the specified file
929 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200930 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
931 && mch_getperm(fields->namebuf) == -1)
932 return QF_FAIL;
933
934 return QF_OK;
935}
936
937/*
938 * Parse the match for error number ('%n') pattern in regmatch.
939 * Return the matched value in "fields->enr".
940 */
941 static int
942qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
943{
944 if (rmp->startp[midx] == NULL)
945 return QF_FAIL;
946 fields->enr = (int)atol((char *)rmp->startp[midx]);
947 return QF_OK;
948}
949
950/*
951 * Parse the match for line number (%l') pattern in regmatch.
952 * Return the matched value in "fields->lnum".
953 */
954 static int
955qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
956{
957 if (rmp->startp[midx] == NULL)
958 return QF_FAIL;
959 fields->lnum = atol((char *)rmp->startp[midx]);
960 return QF_OK;
961}
962
963/*
964 * Parse the match for column number ('%c') pattern in regmatch.
965 * Return the matched value in "fields->col".
966 */
967 static int
968qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
969{
970 if (rmp->startp[midx] == NULL)
971 return QF_FAIL;
972 fields->col = (int)atol((char *)rmp->startp[midx]);
973 return QF_OK;
974}
975
976/*
977 * Parse the match for error type ('%t') pattern in regmatch.
978 * Return the matched value in "fields->type".
979 */
980 static int
981qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
982{
983 if (rmp->startp[midx] == NULL)
984 return QF_FAIL;
985 fields->type = *rmp->startp[midx];
986 return QF_OK;
987}
988
989/*
Bram Moolenaarf4140482020-02-15 23:06:45 +0100990 * Copy a non-error line into the error string. Return the matched line in
991 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200992 */
993 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +0100994copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200995{
996 char_u *p;
997
998 if (linelen >= fields->errmsglen)
999 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001000 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001001 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1002 return QF_NOMEM;
1003 fields->errmsg = p;
1004 fields->errmsglen = linelen + 1;
1005 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001006 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001007 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001008
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001009 return QF_OK;
1010}
1011
1012/*
1013 * Parse the match for error message ('%m') pattern in regmatch.
1014 * Return the matched value in "fields->errmsg".
1015 */
1016 static int
1017qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1018{
1019 char_u *p;
1020 int len;
1021
1022 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1023 return QF_FAIL;
1024 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1025 if (len >= fields->errmsglen)
1026 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001027 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001028 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1029 return QF_NOMEM;
1030 fields->errmsg = p;
1031 fields->errmsglen = len + 1;
1032 }
1033 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1034 return QF_OK;
1035}
1036
1037/*
1038 * Parse the match for rest of a single-line file message ('%r') pattern.
1039 * Return the matched value in "tail".
1040 */
1041 static int
1042qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1043{
1044 if (rmp->startp[midx] == NULL)
1045 return QF_FAIL;
1046 *tail = rmp->startp[midx];
1047 return QF_OK;
1048}
1049
1050/*
1051 * Parse the match for the pointer line ('%p') pattern in regmatch.
1052 * Return the matched value in "fields->col".
1053 */
1054 static int
1055qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1056{
1057 char_u *match_ptr;
1058
1059 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1060 return QF_FAIL;
1061 fields->col = 0;
1062 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1063 ++match_ptr)
1064 {
1065 ++fields->col;
1066 if (*match_ptr == TAB)
1067 {
1068 fields->col += 7;
1069 fields->col -= fields->col % 8;
1070 }
1071 }
1072 ++fields->col;
1073 fields->use_viscol = TRUE;
1074 return QF_OK;
1075}
1076
1077/*
1078 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1079 * Return the matched value in "fields->col".
1080 */
1081 static int
1082qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1083{
1084 if (rmp->startp[midx] == NULL)
1085 return QF_FAIL;
1086 fields->col = (int)atol((char *)rmp->startp[midx]);
1087 fields->use_viscol = TRUE;
1088 return QF_OK;
1089}
1090
1091/*
1092 * Parse the match for the search text ('%s') pattern in regmatch.
1093 * Return the matched value in "fields->pattern".
1094 */
1095 static int
1096qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1097{
1098 int len;
1099
1100 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1101 return QF_FAIL;
1102 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1103 if (len > CMDBUFFSIZE - 5)
1104 len = CMDBUFFSIZE - 5;
1105 STRCPY(fields->pattern, "^\\V");
1106 STRNCAT(fields->pattern, rmp->startp[midx], len);
1107 fields->pattern[len + 3] = '\\';
1108 fields->pattern[len + 4] = '$';
1109 fields->pattern[len + 5] = NUL;
1110 return QF_OK;
1111}
1112
1113/*
1114 * Parse the match for the module ('%o') pattern in regmatch.
1115 * Return the matched value in "fields->module".
1116 */
1117 static int
1118qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1119{
1120 int len;
1121
1122 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1123 return QF_FAIL;
1124 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1125 if (len > CMDBUFFSIZE)
1126 len = CMDBUFFSIZE;
1127 STRNCAT(fields->module, rmp->startp[midx], len);
1128 return QF_OK;
1129}
1130
1131/*
1132 * 'errorformat' format pattern parser functions.
1133 * The '%f' and '%r' formats are parsed differently from other formats.
1134 * See qf_parse_match() for details.
1135 */
1136static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1137{
1138 NULL,
1139 qf_parse_fmt_n,
1140 qf_parse_fmt_l,
1141 qf_parse_fmt_c,
1142 qf_parse_fmt_t,
1143 qf_parse_fmt_m,
1144 NULL,
1145 qf_parse_fmt_p,
1146 qf_parse_fmt_v,
1147 qf_parse_fmt_s,
1148 qf_parse_fmt_o
1149};
1150
1151/*
1152 * Parse the error format pattern matches in "regmatch" and set the values in
1153 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1154 * match. Returns QF_OK if all the matches are successfully parsed. On
1155 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001156 */
1157 static int
1158qf_parse_match(
1159 char_u *linebuf,
1160 int linelen,
1161 efm_T *fmt_ptr,
1162 regmatch_T *regmatch,
1163 qffields_T *fields,
1164 int qf_multiline,
1165 int qf_multiscan,
1166 char_u **tail)
1167{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001168 int idx = fmt_ptr->prefix;
1169 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001170 int midx;
1171 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001172
1173 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1174 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001175 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001176 fields->type = idx;
1177 else
1178 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001179
1180 // Extract error message data from matched line.
1181 // We check for an actual submatch, because "\[" and "\]" in
1182 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001183 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001184 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001185 status = QF_OK;
1186 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001187 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1189 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001191 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001192 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001193 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001195 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001196 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001197 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001198 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001199 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001200
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001201 if (status != QF_OK)
1202 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001203 }
1204
1205 return QF_OK;
1206}
1207
1208/*
1209 * Parse an error line in 'linebuf' using a single error format string in
1210 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1211 * Returns QF_OK if the efm format matches completely and the fields are
1212 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1213 */
1214 static int
1215qf_parse_get_fields(
1216 char_u *linebuf,
1217 int linelen,
1218 efm_T *fmt_ptr,
1219 qffields_T *fields,
1220 int qf_multiline,
1221 int qf_multiscan,
1222 char_u **tail)
1223{
1224 regmatch_T regmatch;
1225 int status = QF_FAIL;
1226 int r;
1227
1228 if (qf_multiscan &&
1229 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1230 return QF_FAIL;
1231
1232 fields->namebuf[0] = NUL;
1233 fields->module[0] = NUL;
1234 fields->pattern[0] = NUL;
1235 if (!qf_multiscan)
1236 fields->errmsg[0] = NUL;
1237 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001238 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001239 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001240 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001241 fields->use_viscol = FALSE;
1242 fields->enr = -1;
1243 fields->type = 0;
1244 *tail = NULL;
1245
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001246 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001247 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001248 regmatch.regprog = fmt_ptr->prog;
1249 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1250 fmt_ptr->prog = regmatch.regprog;
1251 if (r)
1252 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1253 fields, qf_multiline, qf_multiscan, tail);
1254
1255 return status;
1256}
1257
1258/*
1259 * Parse directory error format prefixes (%D and %X).
1260 * Push and pop directories from the directory stack when scanning directory
1261 * names.
1262 */
1263 static int
1264qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1265{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001266 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001267 {
1268 if (*fields->namebuf == NUL)
1269 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001270 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001271 return QF_FAIL;
1272 }
1273 qfl->qf_directory =
1274 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1275 if (qfl->qf_directory == NULL)
1276 return QF_FAIL;
1277 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001278 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001279 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1280
1281 return QF_OK;
1282}
1283
1284/*
1285 * Parse global file name error format prefixes (%O, %P and %Q).
1286 */
1287 static int
1288qf_parse_file_pfx(
1289 int idx,
1290 qffields_T *fields,
1291 qf_list_T *qfl,
1292 char_u *tail)
1293{
1294 fields->valid = FALSE;
1295 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1296 {
1297 if (*fields->namebuf && idx == 'P')
1298 qfl->qf_currfile =
1299 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1300 else if (idx == 'Q')
1301 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1302 *fields->namebuf = NUL;
1303 if (tail && *tail)
1304 {
1305 STRMOVE(IObuff, skipwhite(tail));
1306 qfl->qf_multiscan = TRUE;
1307 return QF_MULTISCAN;
1308 }
1309 }
1310
1311 return QF_OK;
1312}
1313
1314/*
1315 * Parse a non-error line (a line which doesn't match any of the error
1316 * format in 'efm').
1317 */
1318 static int
1319qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1320{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001321 fields->namebuf[0] = NUL; // no match found, remove file name
1322 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001323 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001324
Bram Moolenaarf4140482020-02-15 23:06:45 +01001325 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001326}
1327
1328/*
1329 * Parse multi-line error format prefixes (%C and %Z)
1330 */
1331 static int
1332qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001333 int idx,
1334 qf_list_T *qfl,
1335 qffields_T *fields)
1336{
1337 char_u *ptr;
1338 int len;
1339
1340 if (!qfl->qf_multiignore)
1341 {
1342 qfline_T *qfprev = qfl->qf_last;
1343
1344 if (qfprev == NULL)
1345 return QF_FAIL;
1346 if (*fields->errmsg && !qfl->qf_multiignore)
1347 {
1348 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001349 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001350 == NULL)
1351 return QF_FAIL;
1352 STRCPY(ptr, qfprev->qf_text);
1353 vim_free(qfprev->qf_text);
1354 qfprev->qf_text = ptr;
1355 *(ptr += len) = '\n';
1356 STRCPY(++ptr, fields->errmsg);
1357 }
1358 if (qfprev->qf_nr == -1)
1359 qfprev->qf_nr = fields->enr;
1360 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001361 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001362 qfprev->qf_type = fields->type;
1363
1364 if (!qfprev->qf_lnum)
1365 qfprev->qf_lnum = fields->lnum;
1366 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001367 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001368 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001369 qfprev->qf_viscol = fields->use_viscol;
1370 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001371 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001372 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001373 qfl->qf_directory,
1374 *fields->namebuf || qfl->qf_directory != NULL
1375 ? fields->namebuf
1376 : qfl->qf_currfile != NULL && fields->valid
1377 ? qfl->qf_currfile : 0);
1378 }
1379 if (idx == 'Z')
1380 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1381 line_breakcheck();
1382
1383 return QF_IGNORE_LINE;
1384}
1385
1386/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001387 * Parse a line and get the quickfix fields.
1388 * Return the QF_ status.
1389 */
1390 static int
1391qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001392 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001393 char_u *linebuf,
1394 int linelen,
1395 efm_T *fmt_first,
1396 qffields_T *fields)
1397{
1398 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001399 int idx = 0;
1400 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001401 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001402
Bram Moolenaare333e792018-04-08 13:27:39 +02001403restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001404 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001405 if (fmt_start == NULL)
1406 fmt_ptr = fmt_first;
1407 else
1408 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001409 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001410 fmt_ptr = fmt_start;
1411 fmt_start = NULL;
1412 }
1413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001414 // Try to match each part of 'errorformat' until we find a complete
1415 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1418 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001419 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001420 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1421 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1422 if (status == QF_NOMEM)
1423 return status;
1424 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001425 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001426 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001427 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001428
1429 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1430 {
1431 if (fmt_ptr != NULL)
1432 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001433 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001434 status = qf_parse_dir_pfx(idx, fields, qfl);
1435 if (status != QF_OK)
1436 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001437 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001438
1439 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1440 if (status != QF_OK)
1441 return status;
1442
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001444 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445 }
1446 else if (fmt_ptr != NULL)
1447 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001448 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001449 if (fmt_ptr->conthere)
1450 fmt_start = fmt_ptr;
1451
Bram Moolenaare9283662020-06-07 14:10:47 +02001452 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001453 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001454 qfl->qf_multiline = TRUE; // start of a multi-line message
1455 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001456 }
1457 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001458 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001459 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001460 if (status != QF_OK)
1461 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001462 }
1463 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001464 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001465 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1466 if (status == QF_MULTISCAN)
1467 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001469 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001470 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001471 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001472 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001473 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001474 return QF_IGNORE_LINE;
1475 }
1476 }
1477
1478 return QF_OK;
1479}
1480
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001481/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001482 * Returns TRUE if the specified quickfix/location stack is empty
1483 */
1484 static int
1485qf_stack_empty(qf_info_T *qi)
1486{
1487 return qi == NULL || qi->qf_listcount <= 0;
1488}
1489
1490/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001491 * Returns TRUE if the specified quickfix/location list is empty.
1492 */
1493 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001494qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001495{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001496 return qfl == NULL || qfl->qf_count <= 0;
1497}
1498
1499/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001500 * Returns TRUE if the specified quickfix/location list is not empty and
1501 * has valid entries.
1502 */
1503 static int
1504qf_list_has_valid_entries(qf_list_T *qfl)
1505{
1506 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1507}
1508
1509/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001510 * Return a pointer to a list in the specified quickfix stack
1511 */
1512 static qf_list_T *
1513qf_get_list(qf_info_T *qi, int idx)
1514{
1515 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001516}
1517
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001518/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001519 * Allocate the fields used for parsing lines and populating a quickfix list.
1520 */
1521 static int
1522qf_alloc_fields(qffields_T *pfields)
1523{
1524 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1525 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1526 pfields->errmsglen = CMDBUFFSIZE + 1;
1527 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1528 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1529 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1530 || pfields->pattern == NULL || pfields->module == NULL)
1531 return FAIL;
1532
1533 return OK;
1534}
1535
1536/*
1537 * Free the fields used for parsing lines and populating a quickfix list.
1538 */
1539 static void
1540qf_free_fields(qffields_T *pfields)
1541{
1542 vim_free(pfields->namebuf);
1543 vim_free(pfields->module);
1544 vim_free(pfields->errmsg);
1545 vim_free(pfields->pattern);
1546}
1547
1548/*
1549 * Setup the state information used for parsing lines and populating a
1550 * quickfix list.
1551 */
1552 static int
1553qf_setup_state(
1554 qfstate_T *pstate,
1555 char_u *enc,
1556 char_u *efile,
1557 typval_T *tv,
1558 buf_T *buf,
1559 linenr_T lnumfirst,
1560 linenr_T lnumlast)
1561{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001562 pstate->vc.vc_type = CONV_NONE;
1563 if (enc != NULL && *enc != NUL)
1564 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001565
1566 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1567 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001568 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001569 return FAIL;
1570 }
1571
1572 if (tv != NULL)
1573 {
1574 if (tv->v_type == VAR_STRING)
1575 pstate->p_str = tv->vval.v_string;
1576 else if (tv->v_type == VAR_LIST)
1577 pstate->p_li = tv->vval.v_list->lv_first;
1578 pstate->tv = tv;
1579 }
1580 pstate->buf = buf;
1581 pstate->buflnum = lnumfirst;
1582 pstate->lnumlast = lnumlast;
1583
1584 return OK;
1585}
1586
1587/*
1588 * Cleanup the state information used for parsing lines and populating a
1589 * quickfix list.
1590 */
1591 static void
1592qf_cleanup_state(qfstate_T *pstate)
1593{
1594 if (pstate->fd != NULL)
1595 fclose(pstate->fd);
1596
1597 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001598 if (pstate->vc.vc_type != CONV_NONE)
1599 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001600}
1601
1602/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001603 * Process the next line from a file/buffer/list/string and add it
1604 * to the quickfix list 'qfl'.
1605 */
1606 static int
1607qf_init_process_nextline(
1608 qf_list_T *qfl,
1609 efm_T *fmt_first,
1610 qfstate_T *state,
1611 qffields_T *fields)
1612{
1613 int status;
1614
1615 // Get the next line from a file/buffer/list/string
1616 status = qf_get_nextline(state);
1617 if (status != QF_OK)
1618 return status;
1619
1620 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1621 fmt_first, fields);
1622 if (status != QF_OK)
1623 return status;
1624
1625 return qf_add_entry(qfl,
1626 qfl->qf_directory,
1627 (*fields->namebuf || qfl->qf_directory != NULL)
1628 ? fields->namebuf
1629 : ((qfl->qf_currfile != NULL && fields->valid)
1630 ? qfl->qf_currfile : (char_u *)NULL),
1631 fields->module,
1632 0,
1633 fields->errmsg,
1634 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001635 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001636 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001637 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001638 fields->use_viscol,
1639 fields->pattern,
1640 fields->enr,
1641 fields->type,
1642 fields->valid);
1643}
1644
1645/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001646 * Read the errorfile "efile" into memory, line by line, building the error
1647 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001648 * Alternative: when "efile" is NULL read errors from buffer "buf".
1649 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001650 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001651 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1652 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001653 * Return -1 for error, number of errors for success.
1654 */
1655 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001656qf_init_ext(
1657 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001658 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001659 char_u *efile,
1660 buf_T *buf,
1661 typval_T *tv,
1662 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001663 int newlist, // TRUE: start a new error list
1664 linenr_T lnumfirst, // first line number to use
1665 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001666 char_u *qf_title,
1667 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001668{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001669 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001670 qfstate_T state;
1671 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001672 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001673 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001674 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001676 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001677 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001678 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001680 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001681 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001682
Bram Moolenaara80faa82020-04-12 19:37:17 +02001683 CLEAR_FIELD(state);
1684 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001685 if ((qf_alloc_fields(&fields) == FAIL) ||
1686 (qf_setup_state(&state, enc, efile, tv, buf,
1687 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 goto qf_init_end;
1689
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001690 if (newlist || qf_idx == qi->qf_listcount)
1691 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001692 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001693 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001694 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001695 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001696 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001697 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001698 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001699 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001700 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001701 qfl = qf_get_list(qi, qf_idx);
1702 if (!qf_list_empty(qfl))
1703 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001706 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001707 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001708 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 else
1710 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001712 // If the errorformat didn't change between calls, then reuse the
1713 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001714 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1715 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001716 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001717 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001718 free_efm_list(&fmt_first);
1719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001720 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001721 fmt_first = parse_efm_option(efm);
1722 if (fmt_first != NULL)
1723 last_efm = vim_strsave(efm);
1724 }
1725
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001726 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001729 // got_int is reset here, because it was probably set when killing the
1730 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 got_int = FALSE;
1732
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001733 // Read the lines in the error file one by one.
1734 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001735 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001737 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001738 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001739 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001740 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001741 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001742 if (status == QF_FAIL)
1743 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 line_breakcheck();
1746 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001747 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001749 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001751 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001752 qfl->qf_ptr = qfl->qf_start;
1753 qfl->qf_index = 1;
1754 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756 else
1757 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001758 qfl->qf_nonevalid = FALSE;
1759 if (qfl->qf_ptr == NULL)
1760 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001762 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001763 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001764 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001766 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001768 if (!adding)
1769 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001770 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001771 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001772 qi->qf_listcount--;
1773 if (qi->qf_curlist > 0)
1774 --qi->qf_curlist;
1775 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001776qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001777 if (qf_idx == qi->qf_curlist)
1778 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001779 qf_cleanup_state(&state);
1780 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 return retval;
1783}
1784
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001785/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001786 * Read the errorfile "efile" into memory, line by line, building the error
1787 * list. Set the error list's title to qf_title.
1788 * Return -1 for error, number of errors for success.
1789 */
1790 int
1791qf_init(win_T *wp,
1792 char_u *efile,
1793 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001794 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001795 char_u *qf_title,
1796 char_u *enc)
1797{
1798 qf_info_T *qi = &ql_info;
1799
1800 if (wp != NULL)
1801 {
1802 qi = ll_get_or_alloc_list(wp);
1803 if (qi == NULL)
1804 return FAIL;
1805 }
1806
1807 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1808 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1809}
1810
1811/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001812 * Set the title of the specified quickfix list. Frees the previous title.
1813 * Prepends ':' to the title.
1814 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001815 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001816qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001817{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001818 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001819
Bram Moolenaarfb604092014-07-23 15:55:00 +02001820 if (title != NULL)
1821 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001822 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001823
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001824 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001825 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001826 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001827 }
1828}
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001831 * The title of a quickfix/location list is set, by default, to the command
1832 * that created the quickfix list with the ":" prefix.
1833 * Create a quickfix list title string by prepending ":" to a user command.
1834 * Returns a pointer to a static buffer with the title.
1835 */
1836 static char_u *
1837qf_cmdtitle(char_u *cmd)
1838{
1839 static char_u qftitle_str[IOSIZE];
1840
1841 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1842 return qftitle_str;
1843}
1844
1845/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001846 * Return a pointer to the current list in the specified quickfix stack
1847 */
1848 static qf_list_T *
1849qf_get_curlist(qf_info_T *qi)
1850{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001851 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001852}
1853
1854/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001855 * Prepare for adding a new quickfix list. If the current list is in the
1856 * middle of the stack, then all the following lists are freed and then
1857 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 */
1859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001860qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001863 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001865 // If the current entry is not the last entry, delete entries beyond
1866 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001867 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001869 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001871 // When the stack is full, remove to oldest entry
1872 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001875 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877 qi->qf_lists[i - 1] = qi->qf_lists[i];
1878 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001881 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001882 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001883 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001884 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001885 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001886 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887}
1888
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001889/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001890 * Queue location list stack delete request.
1891 */
1892 static void
1893locstack_queue_delreq(qf_info_T *qi)
1894{
1895 qf_delq_T *q;
1896
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001897 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001898 if (q != NULL)
1899 {
1900 q->qi = qi;
1901 q->next = qf_delq_head;
1902 qf_delq_head = q;
1903 }
1904}
1905
1906/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001907 * Return the global quickfix stack window buffer number.
1908 */
1909 int
1910qf_stack_get_bufnr(void)
1911{
1912 return ql_info.qf_bufnr;
1913}
1914
1915/*
1916 * Wipe the quickfix window buffer (if present) for the specified
1917 * quickfix/location list.
1918 */
1919 static void
1920wipe_qf_buffer(qf_info_T *qi)
1921{
1922 buf_T *qfbuf;
1923
1924 if (qi->qf_bufnr != INVALID_QFBUFNR)
1925 {
1926 qfbuf = buflist_findnr(qi->qf_bufnr);
1927 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1928 {
1929 // If the quickfix buffer is not loaded in any window, then
1930 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001931 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001932 qi->qf_bufnr = INVALID_QFBUFNR;
1933 }
1934 }
1935}
1936
1937/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001938 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939 */
1940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001941ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001942{
1943 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001944 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001945
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946 qi = *pqi;
1947 if (qi == NULL)
1948 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001949 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001950
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001951 // If the location list is still in use, then queue the delete request
1952 // to be processed later.
1953 if (quickfix_busy > 0)
1954 {
1955 locstack_queue_delreq(qi);
1956 return;
1957 }
1958
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001959 qi->qf_refcount--;
1960 if (qi->qf_refcount < 1)
1961 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001962 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001963 // If the quickfix window buffer is loaded, then wipe it
1964 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001965
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001966 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001967 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001968 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001970}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001971
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001972/*
1973 * Free all the quickfix/location lists in the stack.
1974 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001975 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001976qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001977{
1978 int i;
1979 qf_info_T *qi = &ql_info;
1980
1981 if (wp != NULL)
1982 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001983 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001984 ll_free_all(&wp->w_llist);
1985 ll_free_all(&wp->w_llist_ref);
1986 }
1987 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001988 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001989 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001990 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001991}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001992
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001994 * Delay freeing of location list stacks when the quickfix code is running.
1995 * Used to avoid problems with autocmds freeing location list stacks when the
1996 * quickfix code is still referencing the stack.
1997 * Must always call decr_quickfix_busy() exactly once after this.
1998 */
1999 static void
2000incr_quickfix_busy(void)
2001{
2002 quickfix_busy++;
2003}
2004
2005/*
2006 * Safe to free location list stacks. Process any delayed delete requests.
2007 */
2008 static void
2009decr_quickfix_busy(void)
2010{
2011 if (--quickfix_busy == 0)
2012 {
2013 // No longer referencing the location lists. Process all the pending
2014 // delete requests.
2015 while (qf_delq_head != NULL)
2016 {
2017 qf_delq_T *q = qf_delq_head;
2018
2019 qf_delq_head = q->next;
2020 ll_free_all(&q->qi);
2021 vim_free(q);
2022 }
2023 }
2024#ifdef ABORT_ON_INTERNAL_ERROR
2025 if (quickfix_busy < 0)
2026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002027 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002028 abort();
2029 }
2030#endif
2031}
2032
2033#if defined(EXITFREE) || defined(PROTO)
2034 void
2035check_quickfix_busy(void)
2036{
2037 if (quickfix_busy != 0)
2038 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002039 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002040# ifdef ABORT_ON_INTERNAL_ERROR
2041 abort();
2042# endif
2043 }
2044}
2045#endif
2046
2047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002049 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 */
2051 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002052qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002053 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002054 char_u *dir, // optional directory name
2055 char_u *fname, // file name or NULL
2056 char_u *module, // module name or NULL
2057 int bufnum, // buffer number or zero
2058 char_u *mesg, // message
2059 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002060 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002061 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002062 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002063 int vis_col, // using visual column
2064 char_u *pattern, // search pattern
2065 int nr, // error number
2066 int type, // type character
2067 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002069 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002070 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002072 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002073 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002074 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002075 {
2076 buf_T *buf = buflist_findnr(bufnum);
2077
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002078 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002079 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002080 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002081 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002082 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002083 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002084 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2086 {
2087 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002088 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002091 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002093 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002094 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002095 if (pattern == NULL || *pattern == NUL)
2096 qfp->qf_pattern = NULL;
2097 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2098 {
2099 vim_free(qfp->qf_text);
2100 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002101 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002102 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002103 if (module == NULL || *module == NUL)
2104 qfp->qf_module = NULL;
2105 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2106 {
2107 vim_free(qfp->qf_text);
2108 vim_free(qfp->qf_pattern);
2109 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002110 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002113 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 type = 0;
2115 qfp->qf_type = type;
2116 qfp->qf_valid = valid;
2117
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002118 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002119 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002121 qfl->qf_start = qfp;
2122 qfl->qf_ptr = qfp;
2123 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002124 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126 else
2127 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002128 qfp->qf_prev = *lastp;
2129 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002131 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002133 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002134 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002135 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002137 qfl->qf_index = qfl->qf_count;
2138 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
2140
Bram Moolenaar95946f12019-03-31 15:31:59 +02002141 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142}
2143
2144/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002145 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002146 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002147 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002148qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002150 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002151
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002152 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002153 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002154 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002155 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002156 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002157 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002158 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002159 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160}
2161
2162/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002163 * Return the location list stack for window 'wp'.
2164 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002165 */
2166 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002167ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002168{
2169 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002170 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171 return wp->w_llist_ref;
2172
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002173 // For a non-location list window, w_llist_ref should not point to a
2174 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002175 ll_free_all(&wp->w_llist_ref);
2176
2177 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002178 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002179 return wp->w_llist;
2180}
2181
2182/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002183 * Get the quickfix/location list stack to use for the specified Ex command.
2184 * For a location list command, returns the stack for the current window. If
2185 * the location list is not found, then returns NULL and prints an error
2186 * message if 'print_emsg' is TRUE.
2187 */
2188 static qf_info_T *
2189qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2190{
2191 qf_info_T *qi = &ql_info;
2192
2193 if (is_loclist_cmd(eap->cmdidx))
2194 {
2195 qi = GET_LOC_LIST(curwin);
2196 if (qi == NULL)
2197 {
2198 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002199 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002200 return NULL;
2201 }
2202 }
2203
2204 return qi;
2205}
2206
2207/*
2208 * Get the quickfix/location list stack to use for the specified Ex command.
2209 * For a location list command, returns the stack for the current window.
2210 * If the location list is not present, then allocates a new one.
2211 * Returns NULL if the allocation fails. For a location list command, sets
2212 * 'pwinp' to curwin.
2213 */
2214 static qf_info_T *
2215qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2216{
2217 qf_info_T *qi = &ql_info;
2218
2219 if (is_loclist_cmd(eap->cmdidx))
2220 {
2221 qi = ll_get_or_alloc_list(curwin);
2222 if (qi == NULL)
2223 return NULL;
2224 *pwinp = curwin;
2225 }
2226
2227 return qi;
2228}
2229
2230/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002231 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2232 */
2233 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002234copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002235{
2236 int i;
2237 qfline_T *from_qfp;
2238 qfline_T *prevp;
2239
2240 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002241 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002242 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002243 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002244 NULL,
2245 NULL,
2246 from_qfp->qf_module,
2247 0,
2248 from_qfp->qf_text,
2249 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002250 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002251 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002252 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002253 from_qfp->qf_viscol,
2254 from_qfp->qf_pattern,
2255 from_qfp->qf_nr,
2256 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002257 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002258 return FAIL;
2259
2260 // qf_add_entry() will not set the qf_num field, as the
2261 // directory and file names are not supplied. So the qf_fnum
2262 // field is copied here.
2263 prevp = to_qfl->qf_last;
2264 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2265 prevp->qf_type = from_qfp->qf_type; // error type
2266 if (from_qfl->qf_ptr == from_qfp)
2267 to_qfl->qf_ptr = prevp; // current location
2268 }
2269
2270 return OK;
2271}
2272
2273/*
2274 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2275 */
2276 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002277copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002278{
2279 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002280 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002281 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2282 to_qfl->qf_count = 0;
2283 to_qfl->qf_index = 0;
2284 to_qfl->qf_start = NULL;
2285 to_qfl->qf_last = NULL;
2286 to_qfl->qf_ptr = NULL;
2287 if (from_qfl->qf_title != NULL)
2288 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2289 else
2290 to_qfl->qf_title = NULL;
2291 if (from_qfl->qf_ctx != NULL)
2292 {
2293 to_qfl->qf_ctx = alloc_tv();
2294 if (to_qfl->qf_ctx != NULL)
2295 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2296 }
2297 else
2298 to_qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002299 if (from_qfl->qftf_cb.cb_name != NULL)
2300 copy_callback(&to_qfl->qftf_cb, &from_qfl->qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002301 else
Bram Moolenaard43906d2020-07-20 21:31:32 +02002302 to_qfl->qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002303
2304 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002305 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002306 return FAIL;
2307
2308 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2309
2310 // Assign a new ID for the location list
2311 to_qfl->qf_id = ++last_qf_id;
2312 to_qfl->qf_changedtick = 0L;
2313
2314 // When no valid entries are present in the list, qf_ptr points to
2315 // the first item in the list
2316 if (to_qfl->qf_nonevalid)
2317 {
2318 to_qfl->qf_ptr = to_qfl->qf_start;
2319 to_qfl->qf_index = 1;
2320 }
2321
2322 return OK;
2323}
2324
2325/*
2326 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002327 */
2328 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002329copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002330{
2331 qf_info_T *qi;
2332 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002333
Bram Moolenaar09037502018-09-25 22:08:14 +02002334 // When copying from a location list window, copy the referenced
2335 // location list. For other windows, copy the location list for
2336 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 if (IS_LL_WINDOW(from))
2338 qi = from->w_llist_ref;
2339 else
2340 qi = from->w_llist;
2341
Bram Moolenaar09037502018-09-25 22:08:14 +02002342 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002343 return;
2344
Bram Moolenaar09037502018-09-25 22:08:14 +02002345 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002346 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347 return;
2348
2349 to->w_llist->qf_listcount = qi->qf_listcount;
2350
Bram Moolenaar09037502018-09-25 22:08:14 +02002351 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002352 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002353 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002354 to->w_llist->qf_curlist = idx;
2355
Bram Moolenaar0398e002019-03-21 21:12:49 +01002356 if (copy_loclist(qf_get_list(qi, idx),
2357 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002358 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002359 qf_free_all(to);
2360 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002361 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002362 }
2363
Bram Moolenaar09037502018-09-25 22:08:14 +02002364 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002365}
2366
2367/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002368 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002369 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 */
2371 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002372qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
Bram Moolenaar82404332016-07-10 17:00:38 +02002374 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002375 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002376 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002377
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002378 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
Bram Moolenaare60acc12011-05-10 16:41:25 +02002381#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002382 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002383#endif
2384#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002385 if (directory != NULL)
2386 slash_adjust(directory);
2387 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002388#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002389 if (directory != NULL && !vim_isAbsName(fname)
2390 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2391 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002392 // Here we check if the file really exists.
2393 // This should normally be true, but if make works without
2394 // "leaving directory"-messages we might have missed a
2395 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002396 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002399 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002400 if (directory)
2401 ptr = concat_fnames(directory, fname, TRUE);
2402 else
2403 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002405 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002406 bufname = ptr;
2407 }
2408 else
2409 bufname = fname;
2410
2411 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002412 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002413 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002414 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002415 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002417 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002418 {
2419 vim_free(qf_last_bufname);
2420 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2421 if (bufname == ptr)
2422 qf_last_bufname = bufname;
2423 else
2424 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002425 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002426 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002427 if (buf == NULL)
2428 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002429
Bram Moolenaarc1542742016-07-20 21:44:37 +02002430 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002431 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002432 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433}
2434
2435/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002436 * Push dirbuf onto the directory stack and return pointer to actual dir or
2437 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 */
2439 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002440qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 struct dir_stack_T *ds_new;
2443 struct dir_stack_T *ds_ptr;
2444
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002445 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002446 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 if (ds_new == NULL)
2448 return NULL;
2449
2450 ds_new->next = *stackptr;
2451 *stackptr = ds_new;
2452
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002453 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 if (vim_isAbsName(dirbuf)
2455 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002456 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 (*stackptr)->dirname = vim_strsave(dirbuf);
2458 else
2459 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002460 // Okay we don't have an absolute path.
2461 // dirbuf must be a subdir of one of the directories on the stack.
2462 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 ds_new = (*stackptr)->next;
2464 (*stackptr)->dirname = NULL;
2465 while (ds_new)
2466 {
2467 vim_free((*stackptr)->dirname);
2468 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2469 TRUE);
2470 if (mch_isdir((*stackptr)->dirname) == TRUE)
2471 break;
2472
2473 ds_new = ds_new->next;
2474 }
2475
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002476 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 while ((*stackptr)->next != ds_new)
2478 {
2479 ds_ptr = (*stackptr)->next;
2480 (*stackptr)->next = (*stackptr)->next->next;
2481 vim_free(ds_ptr->dirname);
2482 vim_free(ds_ptr);
2483 }
2484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002485 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (ds_new == NULL)
2487 {
2488 vim_free((*stackptr)->dirname);
2489 (*stackptr)->dirname = vim_strsave(dirbuf);
2490 }
2491 }
2492
2493 if ((*stackptr)->dirname != NULL)
2494 return (*stackptr)->dirname;
2495 else
2496 {
2497 ds_ptr = *stackptr;
2498 *stackptr = (*stackptr)->next;
2499 vim_free(ds_ptr);
2500 return NULL;
2501 }
2502}
2503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504/*
2505 * pop dirbuf from the directory stack and return previous directory or NULL if
2506 * stack is empty
2507 */
2508 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002509qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 struct dir_stack_T *ds_ptr;
2512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002513 // TODO: Should we check if dirbuf is the directory on top of the stack?
2514 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002516 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (*stackptr != NULL)
2518 {
2519 ds_ptr = *stackptr;
2520 *stackptr = (*stackptr)->next;
2521 vim_free(ds_ptr->dirname);
2522 vim_free(ds_ptr);
2523 }
2524
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002525 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 return *stackptr ? (*stackptr)->dirname : NULL;
2527}
2528
2529/*
2530 * clean up directory stack
2531 */
2532 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002533qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 struct dir_stack_T *ds_ptr;
2536
2537 while ((ds_ptr = *stackptr) != NULL)
2538 {
2539 *stackptr = (*stackptr)->next;
2540 vim_free(ds_ptr->dirname);
2541 vim_free(ds_ptr);
2542 }
2543}
2544
2545/*
2546 * Check in which directory of the directory stack the given file can be
2547 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002548 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 * Cleans up intermediate directory entries.
2550 *
2551 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002552 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 * ./
2554 * ./aa
2555 * ./aa/bb
2556 * ./bb
2557 * ./bb/x.c
2558 * and make says:
2559 * making all in aa
2560 * making all in bb
2561 * x.c:9: Error
2562 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2563 * qf_guess_filepath will return NULL.
2564 */
2565 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002566qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 struct dir_stack_T *ds_ptr;
2569 struct dir_stack_T *ds_tmp;
2570 char_u *fullname;
2571
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002572 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002573 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 return NULL;
2575
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002576 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 fullname = NULL;
2578 while (ds_ptr)
2579 {
2580 vim_free(fullname);
2581 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2582
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002583 // If concat_fnames failed, just go on. The worst thing that can happen
2584 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2586 break;
2587
2588 ds_ptr = ds_ptr->next;
2589 }
2590
2591 vim_free(fullname);
2592
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002593 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002594 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002596 ds_tmp = qfl->qf_dir_stack->next;
2597 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 vim_free(ds_tmp->dirname);
2599 vim_free(ds_tmp);
2600 }
2601
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002602 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603}
2604
2605/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002606 * Returns TRUE if a quickfix/location list with the given identifier exists.
2607 */
2608 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002609qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002610{
2611 qf_info_T *qi = &ql_info;
2612 int i;
2613
2614 if (wp != NULL)
2615 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002616 if (!win_valid(wp))
2617 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002618 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002619 if (qi == NULL)
2620 return FALSE;
2621 }
2622
2623 for (i = 0; i < qi->qf_listcount; ++i)
2624 if (qi->qf_lists[i].qf_id == qf_id)
2625 return TRUE;
2626
2627 return FALSE;
2628}
2629
2630/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002631 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002632 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002633 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002634 * Similar to location list.
2635 */
2636 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002637is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002638{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002639 qfline_T *qfp;
2640 int i;
2641
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002642 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002643 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2644 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002645 break;
2646
Bram Moolenaar95946f12019-03-31 15:31:59 +02002647 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002648 return FALSE;
2649
2650 return TRUE;
2651}
2652
2653/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002655 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002656 */
2657 static qfline_T *
2658get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002659 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002660 qfline_T *qf_ptr,
2661 int *qf_index,
2662 int dir)
2663{
2664 int idx;
2665 int old_qf_fnum;
2666
2667 idx = *qf_index;
2668 old_qf_fnum = qf_ptr->qf_fnum;
2669
2670 do
2671 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002672 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002673 return NULL;
2674 ++idx;
2675 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002676 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002677 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2678
2679 *qf_index = idx;
2680 return qf_ptr;
2681}
2682
2683/*
2684 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002685 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002686 */
2687 static qfline_T *
2688get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002689 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002690 qfline_T *qf_ptr,
2691 int *qf_index,
2692 int dir)
2693{
2694 int idx;
2695 int old_qf_fnum;
2696
2697 idx = *qf_index;
2698 old_qf_fnum = qf_ptr->qf_fnum;
2699
2700 do
2701 {
2702 if (idx == 1 || qf_ptr->qf_prev == NULL)
2703 return NULL;
2704 --idx;
2705 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002706 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002707 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2708
2709 *qf_index = idx;
2710 return qf_ptr;
2711}
2712
2713/*
2714 * Get the n'th (errornr) previous/next valid entry from the current entry in
2715 * the quickfix list.
2716 * dir == FORWARD or FORWARD_FILE: next valid entry
2717 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2718 */
2719 static qfline_T *
2720get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002721 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002722 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002723 int dir,
2724 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002725{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002726 qfline_T *qf_ptr = qfl->qf_ptr;
2727 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002728 qfline_T *prev_qf_ptr;
2729 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002730 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731
2732 while (errornr--)
2733 {
2734 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002735 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002736
2737 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002738 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002739 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002740 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002741 if (qf_ptr == NULL)
2742 {
2743 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002744 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002745 if (err != NULL)
2746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002747 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748 return NULL;
2749 }
2750 break;
2751 }
2752
2753 err = NULL;
2754 }
2755
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002756 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002757 return qf_ptr;
2758}
2759
2760/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002761 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2762 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002763 */
2764 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002765get_nth_entry(qf_list_T *qfl, int errornr, 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
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002770 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002771 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2772 {
2773 --qf_idx;
2774 qf_ptr = qf_ptr->qf_prev;
2775 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002776 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002777 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2778 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002779 {
2780 ++qf_idx;
2781 qf_ptr = qf_ptr->qf_next;
2782 }
2783
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002784 *new_qfidx = qf_idx;
2785 return qf_ptr;
2786}
2787
2788/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002789 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002790 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2791 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2792 * Returns a pointer to the entry and the index of the new entry is stored in
2793 * 'new_qfidx'.
2794 */
2795 static qfline_T *
2796qf_get_entry(
2797 qf_list_T *qfl,
2798 int errornr,
2799 int dir,
2800 int *new_qfidx)
2801{
2802 qfline_T *qf_ptr = qfl->qf_ptr;
2803 int qfidx = qfl->qf_index;
2804
2805 if (dir != 0) // next/prev valid entry
2806 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2807 else if (errornr != 0) // go to specified number
2808 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2809
2810 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002811 return qf_ptr;
2812}
2813
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002814/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002815 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002816 */
2817 static win_T *
2818qf_find_help_win(void)
2819{
2820 win_T *wp;
2821
2822 FOR_ALL_WINDOWS(wp)
2823 if (bt_help(wp->w_buffer))
2824 return wp;
2825
2826 return NULL;
2827}
2828
2829/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002830 * Set the location list for the specified window to 'qi'.
2831 */
2832 static void
2833win_set_loclist(win_T *wp, qf_info_T *qi)
2834{
2835 wp->w_llist = qi;
2836 qi->qf_refcount++;
2837}
2838
2839/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002840 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2841 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002842 */
2843 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002844jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002845{
2846 win_T *wp;
2847 int flags;
2848
Bram Moolenaare1004402020-10-24 20:49:43 +02002849 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002850 wp = NULL;
2851 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002852 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002853 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2854 win_enter(wp, TRUE);
2855 else
2856 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002857 // Split off help window; put it at far top if no position
2858 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002859 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002860 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002861 && curwin->w_width < 80)
2862 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002863 // If the user asks to open a new window, then copy the location list.
2864 // Otherwise, don't copy the location list.
2865 if (IS_LL_STACK(qi) && !newwin)
2866 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002867
2868 if (win_split(0, flags) == FAIL)
2869 return FAIL;
2870
2871 *opened_window = TRUE;
2872
2873 if (curwin->w_height < p_hh)
2874 win_setheight((int)p_hh);
2875
Bram Moolenaarb2443732018-11-11 22:50:27 +01002876 // When using location list, the new window should use the supplied
2877 // location list. If the user asks to open a new window, then the new
2878 // window will get a copy of the location list.
2879 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002880 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002881 }
2882
2883 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002884 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002885
2886 return OK;
2887}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002888
2889/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002890 * Find a non-quickfix window using the given location list stack in the
2891 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002892 * Returns NULL if a matching window is not found.
2893 */
2894 static win_T *
2895qf_find_win_with_loclist(qf_info_T *ll)
2896{
2897 win_T *wp;
2898
2899 FOR_ALL_WINDOWS(wp)
2900 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2901 return wp;
2902
2903 return NULL;
2904}
2905
2906/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002907 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002908 */
2909 static win_T *
2910qf_find_win_with_normal_buf(void)
2911{
2912 win_T *wp;
2913
2914 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002915 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002916 return wp;
2917
2918 return NULL;
2919}
2920
2921/*
2922 * Go to a window in any tabpage containing the specified file. Returns TRUE
2923 * if successfully jumped to the window. Otherwise returns FALSE.
2924 */
2925 static int
2926qf_goto_tabwin_with_file(int fnum)
2927{
2928 tabpage_T *tp;
2929 win_T *wp;
2930
2931 FOR_ALL_TAB_WINDOWS(tp, wp)
2932 if (wp->w_buffer->b_fnum == fnum)
2933 {
2934 goto_tabpage_win(tp, wp);
2935 return TRUE;
2936 }
2937
2938 return FALSE;
2939}
2940
2941/*
2942 * Create a new window to show a file above the quickfix window. Called when
2943 * only the quickfix window is present.
2944 */
2945 static int
2946qf_open_new_file_win(qf_info_T *ll_ref)
2947{
2948 int flags;
2949
2950 flags = WSP_ABOVE;
2951 if (ll_ref != NULL)
2952 flags |= WSP_NEWLOC;
2953 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002954 return FAIL; // not enough room for window
2955 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002956 swb_flags = 0;
2957 RESET_BINDING(curwin);
2958 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002959 // The new window should use the location list from the
2960 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002961 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002962 return OK;
2963}
2964
2965/*
2966 * Go to a window that shows the right buffer. If the window is not found, go
2967 * to the window just above the location list window. This is used for opening
2968 * a file from a location window and not from a quickfix window. If some usable
2969 * window is previously found, then it is supplied in 'use_win'.
2970 */
2971 static void
2972qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2973{
2974 win_T *win = use_win;
2975
2976 if (win == NULL)
2977 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002978 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002979 FOR_ALL_WINDOWS(win)
2980 if (win->w_buffer->b_fnum == qf_fnum)
2981 break;
2982 if (win == NULL)
2983 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002984 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002985 win = curwin;
2986 do
2987 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002988 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002989 break;
2990 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002991 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002992 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002993 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002994 } while (win != curwin);
2995 }
2996 }
2997 win_goto(win);
2998
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002999 // If the location list for the window is not set, then set it
3000 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003001 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003002 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003003}
3004
3005/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003006 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3007 * not found, then go to the window just above the quickfix window. This is
3008 * used for opening a file from a quickfix window and not from a location
3009 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003010 */
3011 static void
3012qf_goto_win_with_qfl_file(int qf_fnum)
3013{
3014 win_T *win;
3015 win_T *altwin;
3016
3017 win = curwin;
3018 altwin = NULL;
3019 for (;;)
3020 {
3021 if (win->w_buffer->b_fnum == qf_fnum)
3022 break;
3023 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003024 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003025 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003026 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003027
3028 if (IS_QF_WINDOW(win))
3029 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003030 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003031 // window, unless 'switchbuf' contains 'uselast': in this case we
3032 // try to jump to the previously used window first.
3033 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3034 win = prevwin;
3035 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003036 win = altwin;
3037 else if (curwin->w_prev != NULL)
3038 win = curwin->w_prev;
3039 else
3040 win = curwin->w_next;
3041 break;
3042 }
3043
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003044 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003045 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003046 altwin = win;
3047 }
3048
3049 win_goto(win);
3050}
3051
3052/*
3053 * Find a suitable window for opening a file (qf_fnum) from the
3054 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003055 * window, jump to it. Otherwise open a new window to display the file. If
3056 * 'newwin' is TRUE, then always open a new window. This is called from either
3057 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003058 */
3059 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003060qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003061{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003062 win_T *usable_wp = NULL;
3063 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003064 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003065
Bram Moolenaarb2443732018-11-11 22:50:27 +01003066 // If opening a new window, then don't use the location list referred by
3067 // the current window. Otherwise two windows will refer to the same
3068 // location list.
3069 if (!newwin)
3070 ll_ref = curwin->w_llist_ref;
3071
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003072 if (ll_ref != NULL)
3073 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003074 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003075 usable_wp = qf_find_win_with_loclist(ll_ref);
3076 if (usable_wp != NULL)
3077 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003078 }
3079
3080 if (!usable_win)
3081 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003082 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003083 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003084 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003085 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003086 }
3087
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003088 // If no usable window is found and 'switchbuf' contains "usetab"
3089 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003090 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003091 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003092
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003093 // If there is only one window and it is the quickfix window, create a
3094 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003095 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003096 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003097 if (qf_open_new_file_win(ll_ref) != OK)
3098 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003099 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003100 }
3101 else
3102 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003103 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003104 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003105 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003106 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003107 }
3108
3109 return OK;
3110}
3111
3112/*
3113 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003114 * Returns OK if successfully edited the file, FAIL on failing to open the
3115 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3116 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003117 */
3118 static int
3119qf_jump_edit_buffer(
3120 qf_info_T *qi,
3121 qfline_T *qf_ptr,
3122 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003123 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003124 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003125{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003126 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003127 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003128 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003129 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003130 int old_qf_curlist = qi->qf_curlist;
3131 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003132
3133 if (qf_ptr->qf_type == 1)
3134 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003135 // Open help file (do_ecmd() will set b_help flag, readfile() will
3136 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003137 if (!can_abandon(curbuf, forceit))
3138 {
3139 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003140 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003141 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003142
3143 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3144 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003145 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003146 }
3147 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003148 retval = buflist_getfile(qf_ptr->qf_fnum,
3149 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003150
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003151 // If a location list, check whether the associated window is still
3152 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003153 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003154 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003155 win_T *wp = win_id2wp(prev_winid);
3156 if (wp == NULL && curwin->w_llist != qi)
3157 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003158 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003159 *opened_window = FALSE;
3160 return NOTDONE;
3161 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003162 }
3163
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003164 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003165 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003166 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003167 return NOTDONE;
3168 }
3169
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003170 // Check if the list was changed. The pointers may happen to be identical,
3171 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003172 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003173 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003174 || !is_qf_entry_present(qfl, qf_ptr))
3175 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003176 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003177 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003178 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003179 emsg(_(e_current_location_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003180 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003181 }
3182
3183 return retval;
3184}
3185
3186/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003187 * Go to the error line in the current file using either line/column number or
3188 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003189 */
3190 static void
3191qf_jump_goto_line(
3192 linenr_T qf_lnum,
3193 int qf_col,
3194 char_u qf_viscol,
3195 char_u *qf_pattern)
3196{
3197 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003198
3199 if (qf_pattern == NULL)
3200 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003201 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003202 i = qf_lnum;
3203 if (i > 0)
3204 {
3205 if (i > curbuf->b_ml.ml_line_count)
3206 i = curbuf->b_ml.ml_line_count;
3207 curwin->w_cursor.lnum = i;
3208 }
3209 if (qf_col > 0)
3210 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003211 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003212 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003213 coladvance(qf_col - 1);
3214 else
3215 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003216 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003217 check_cursor();
3218 }
3219 else
3220 beginline(BL_WHITE | BL_FIX);
3221 }
3222 else
3223 {
3224 pos_T save_cursor;
3225
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003226 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003227 save_cursor = curwin->w_cursor;
3228 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003229 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003230 curwin->w_cursor = save_cursor;
3231 }
3232}
3233
3234/*
3235 * Display quickfix list index and size message
3236 */
3237 static void
3238qf_jump_print_msg(
3239 qf_info_T *qi,
3240 int qf_index,
3241 qfline_T *qf_ptr,
3242 buf_T *old_curbuf,
3243 linenr_T old_lnum)
3244{
3245 linenr_T i;
3246 int len;
3247
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003248 // Update the screen before showing the message, unless the screen
3249 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003250 if (!msg_scrolled)
3251 update_topline_redraw();
3252 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003253 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003254 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3255 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003256 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003257 len = (int)STRLEN(IObuff);
3258 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3259
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003260 // Output the message. Overwrite to avoid scrolling when the 'O'
3261 // flag is present in 'shortmess'; But when not jumping, print the
3262 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003263 i = msg_scroll;
3264 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3265 msg_scroll = TRUE;
3266 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3267 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003268 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003269 msg_scroll = i;
3270}
3271
3272/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003273 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003274 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3275 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003276 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3277 * able to jump/open a window. Returns NOTDONE if a file is not associated
3278 * with the entry.
3279 */
3280 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003281qf_jump_open_window(
3282 qf_info_T *qi,
3283 qfline_T *qf_ptr,
3284 int newwin,
3285 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003286{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003287 qf_list_T *qfl = qf_get_curlist(qi);
3288 int old_changedtick = qfl->qf_changedtick;
3289 int old_qf_curlist = qi->qf_curlist;
3290 qfltype_T qfl_type = qfl->qfl_type;
3291
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003292 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003293 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3294 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003295 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003296 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003297 if (old_qf_curlist != qi->qf_curlist
3298 || old_changedtick != qfl->qf_changedtick
3299 || !is_qf_entry_present(qfl, qf_ptr))
3300 {
3301 if (qfl_type == QFLT_QUICKFIX)
3302 emsg(_(e_current_quickfix_list_was_changed));
3303 else
3304 emsg(_(e_current_location_list_was_changed));
3305 return FAIL;
3306 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003307
3308 // If currently in the quickfix window, find another window to show the
3309 // file in.
3310 if (bt_quickfix(curbuf) && !*opened_window)
3311 {
3312 // If there is no file specified, we don't know where to go.
3313 // But do advance, otherwise ":cn" gets stuck.
3314 if (qf_ptr->qf_fnum == 0)
3315 return NOTDONE;
3316
Bram Moolenaarb2443732018-11-11 22:50:27 +01003317 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3318 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003319 return FAIL;
3320 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003321 if (old_qf_curlist != qi->qf_curlist
3322 || old_changedtick != qfl->qf_changedtick
3323 || !is_qf_entry_present(qfl, qf_ptr))
3324 {
3325 if (qfl_type == QFLT_QUICKFIX)
3326 emsg(_(e_current_quickfix_list_was_changed));
3327 else
3328 emsg(_(e_current_location_list_was_changed));
3329 return FAIL;
3330 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003331
3332 return OK;
3333}
3334
3335/*
3336 * Edit a selected file from the quickfix/location list and jump to a
3337 * particular line/column, adjust the folds and display a message about the
3338 * jump.
3339 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3340 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3341 * the file.
3342 */
3343 static int
3344qf_jump_to_buffer(
3345 qf_info_T *qi,
3346 int qf_index,
3347 qfline_T *qf_ptr,
3348 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003349 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003350 int *opened_window,
3351 int openfold,
3352 int print_message)
3353{
3354 buf_T *old_curbuf;
3355 linenr_T old_lnum;
3356 int retval = OK;
3357
3358 // If there is a file name, read the wanted file if needed, and check
3359 // autowrite etc.
3360 old_curbuf = curbuf;
3361 old_lnum = curwin->w_cursor.lnum;
3362
3363 if (qf_ptr->qf_fnum != 0)
3364 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003365 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003366 opened_window);
3367 if (retval != OK)
3368 return retval;
3369 }
3370
3371 // When not switched to another buffer, still need to set pc mark
3372 if (curbuf == old_curbuf)
3373 setpcmark();
3374
3375 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3376 qf_ptr->qf_pattern);
3377
3378#ifdef FEAT_FOLDING
3379 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3380 foldOpenCursor();
3381#endif
3382 if (print_message)
3383 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3384
3385 return retval;
3386}
3387
3388/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003389 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 */
3391 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003392qf_jump(qf_info_T *qi,
3393 int dir,
3394 int errornr,
3395 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003397 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3398}
3399
3400/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003401 * Jump to a quickfix line.
3402 * If dir == 0 go to entry "errornr".
3403 * If dir == FORWARD go "errornr" valid entries forward.
3404 * If dir == BACKWARD go "errornr" valid entries backward.
3405 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3406 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3407 * else if "errornr" is zero, redisplay the same line
3408 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003409 * If 'newwin' is TRUE, then open the file in a new window.
3410 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003411 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003412qf_jump_newwin(qf_info_T *qi,
3413 int dir,
3414 int errornr,
3415 int forceit,
3416 int newwin)
3417{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003418 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003419 qfline_T *qf_ptr;
3420 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003423 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003424 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003425 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003428 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003429 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003431 if (qi == NULL)
3432 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003433
Bram Moolenaar0398e002019-03-21 21:12:49 +01003434 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003436 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return;
3438 }
3439
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003440 incr_quickfix_busy();
3441
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003442 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003443
3444 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003446 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003448
3449 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3450 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003452 qf_ptr = old_qf_ptr;
3453 qf_index = old_qf_index;
3454 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003457 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003458 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003459 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003460 // No need to print the error message if it's visible in the error
3461 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 print_message = FALSE;
3463
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003464 prev_winid = curwin->w_id;
3465
Bram Moolenaarb2443732018-11-11 22:50:27 +01003466 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003467 if (retval == FAIL)
3468 goto failed;
3469 if (retval == NOTDONE)
3470 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003471
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003472 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003473 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003474 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003476 // Quickfix/location list is freed by an autocmd
3477 qi = NULL;
3478 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003481 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003484 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003485 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003487 // Couldn't open file, so put index back where it was. This could
3488 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 qf_ptr = old_qf_ptr;
3491 qf_index = old_qf_index;
3492 }
3493 }
3494theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003495 if (qi != NULL)
3496 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003497 qfl->qf_ptr = qf_ptr;
3498 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003499 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003500 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003502 // Restore old 'switchbuf' value, but not when an autocommand or
3503 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003504 p_swb = old_swb;
3505 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003507 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508}
3509
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003510// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003511static int qfFileAttr;
3512static int qfSepAttr;
3513static int qfLineAttr;
3514
3515/*
3516 * Display information about a single entry from the quickfix/location list.
3517 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003518 * 'cursel' will be set to TRUE for the currently selected entry in the
3519 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003520 */
3521 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003522qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003523{
3524 char_u *fname;
3525 buf_T *buf;
3526 int filter_entry;
3527
3528 fname = NULL;
3529 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3530 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3531 (char *)qfp->qf_module);
3532 else {
3533 if (qfp->qf_fnum != 0
3534 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3535 {
3536 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003537 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003538 fname = gettail(fname);
3539 }
3540 if (fname == NULL)
3541 sprintf((char *)IObuff, "%2d", qf_idx);
3542 else
3543 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3544 qf_idx, (char *)fname);
3545 }
3546
3547 // Support for filtering entries using :filter /pat/ clist
3548 // Match against the module name, file name, search pattern and
3549 // text of the entry.
3550 filter_entry = TRUE;
3551 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3552 filter_entry &= message_filtered(qfp->qf_module);
3553 if (filter_entry && fname != NULL)
3554 filter_entry &= message_filtered(fname);
3555 if (filter_entry && qfp->qf_pattern != NULL)
3556 filter_entry &= message_filtered(qfp->qf_pattern);
3557 if (filter_entry)
3558 filter_entry &= message_filtered(qfp->qf_text);
3559 if (filter_entry)
3560 return;
3561
3562 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003563 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003564
3565 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003566 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003567 if (qfp->qf_lnum == 0)
3568 IObuff[0] = NUL;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003569 else
thinca6864efa2021-06-19 20:45:20 +02003570 qf_range_text(qfp, IObuff, IOSIZE);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003571 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3572 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003573 msg_puts_attr((char *)IObuff, qfLineAttr);
3574 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003575 if (qfp->qf_pattern != NULL)
3576 {
3577 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003578 msg_puts((char *)IObuff);
3579 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003580 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003581 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003582
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003583 // Remove newlines and leading whitespace from the text. For an
3584 // unrecognized line keep the indent, the compiler may mark a word
3585 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003586 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3587 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3588 IObuff, IOSIZE);
3589 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003590 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003591}
3592
3593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003595 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 */
3597 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003598qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003600 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003601 qfline_T *qfp;
3602 int i;
3603 int idx1 = 1;
3604 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003605 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003606 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003607 int all = eap->forceit; // if not :cl!, only show
3608 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003609 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003611 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3612 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003613
Bram Moolenaar0398e002019-03-21 21:12:49 +01003614 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003616 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 return;
3618 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003619 if (*arg == '+')
3620 {
3621 ++arg;
3622 plus = TRUE;
3623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3625 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003626 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return;
3628 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003629 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003630 if (plus)
3631 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003632 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003633 idx2 = i + idx1;
3634 idx1 = i;
3635 }
3636 else
3637 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003638 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003639 if (idx1 < 0)
3640 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3641 if (idx2 < 0)
3642 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003645 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003646 shorten_fnames(FALSE);
3647
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003648 // Get the attributes for the different quickfix highlight items. Note
3649 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003650 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3651 if (qfFileAttr == 0)
3652 qfFileAttr = HL_ATTR(HLF_D);
3653 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3654 if (qfSepAttr == 0)
3655 qfSepAttr = HL_ATTR(HLF_D);
3656 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3657 if (qfLineAttr == 0)
3658 qfLineAttr = HL_ATTR(HLF_N);
3659
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003660 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003662 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003665 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 ui_breakcheck();
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669}
3670
3671/*
3672 * Remove newlines and leading whitespace from an error message.
3673 * Put the result in "buf[bufsize]".
3674 */
3675 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003676qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
3678 int i;
3679 char_u *p = text;
3680
3681 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3682 {
3683 if (*p == '\n')
3684 {
3685 buf[i] = ' ';
3686 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003687 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 break;
3689 }
3690 else
3691 buf[i] = *p++;
3692 }
3693 buf[i] = NUL;
3694}
3695
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003696/*
thinca6864efa2021-06-19 20:45:20 +02003697 * Range information from lnum, col, end_lnum, and end_col.
3698 * Put the result in "buf[bufsize]".
3699 */
3700 static void
3701qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3702{
3703 int len;
3704 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3705 len = (int)STRLEN(buf);
3706
3707 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3708 {
3709 vim_snprintf((char *)buf + len, bufsize - len,
3710 "-%ld", qfp->qf_end_lnum);
3711 len += (int)STRLEN(buf + len);
3712 }
3713 if (qfp->qf_col > 0)
3714 {
3715 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3716 len += (int)STRLEN(buf + len);
3717 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3718 {
3719 vim_snprintf((char *)buf + len, bufsize - len,
3720 "-%d", qfp->qf_end_col);
3721 len += (int)STRLEN(buf + len);
3722 }
3723 }
3724 buf[len] = NUL;
3725}
3726
3727/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003728 * Display information (list number, list size and the title) about a
3729 * quickfix/location list.
3730 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003731 static void
3732qf_msg(qf_info_T *qi, int which, char *lead)
3733{
3734 char *title = (char *)qi->qf_lists[which].qf_title;
3735 int count = qi->qf_lists[which].qf_count;
3736 char_u buf[IOSIZE];
3737
3738 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3739 lead,
3740 which + 1,
3741 qi->qf_listcount,
3742 count);
3743
3744 if (title != NULL)
3745 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003746 size_t len = STRLEN(buf);
3747
3748 if (len < 34)
3749 {
3750 vim_memset(buf + len, ' ', 34 - len);
3751 buf[34] = NUL;
3752 }
3753 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003754 }
3755 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003756 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003757}
3758
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759/*
3760 * ":colder [count]": Up in the quickfix stack.
3761 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003762 * ":lolder [count]": Up in the location list stack.
3763 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 */
3765 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003766qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003768 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 int count;
3770
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003771 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3772 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (eap->addr_count != 0)
3775 count = eap->line2;
3776 else
3777 count = 1;
3778 while (count--)
3779 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003780 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003782 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003784 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003785 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003787 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 else
3790 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003791 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003793 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003796 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003799 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003800 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801}
3802
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003803/*
3804 * Display the information about all the quickfix/location lists in the stack
3805 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003806 void
3807qf_history(exarg_T *eap)
3808{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003809 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003810 int i;
3811
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003812 if (eap->addr_count > 0)
3813 {
3814 if (qi == NULL)
3815 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003816 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003817 return;
3818 }
3819
3820 // Jump to the specified quickfix list
3821 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3822 {
3823 qi->qf_curlist = eap->line2 - 1;
3824 qf_msg(qi, qi->qf_curlist, "");
3825 qf_update_buffer(qi, NULL);
3826 }
3827 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003828 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003829
3830 return;
3831 }
3832
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003833 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003834 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003835 else
3836 for (i = 0; i < qi->qf_listcount; ++i)
3837 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3838}
3839
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003841 * Free all the entries in the error list "idx". Note that other information
3842 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 */
3844 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003845qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003847 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003848 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003849 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003851 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003853 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003854 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003855 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003856 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003857 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003858 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003859 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003860 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003861 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003862 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003863 // Somehow qf_count may have an incorrect value, set it to 1
3864 // to avoid crashing when it's wrong.
3865 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003866 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003867 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003868 qfl->qf_start = qfpnext;
3869 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003871
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003872 qfl->qf_index = 0;
3873 qfl->qf_start = NULL;
3874 qfl->qf_last = NULL;
3875 qfl->qf_ptr = NULL;
3876 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003877
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003878 qf_clean_dir_stack(&qfl->qf_dir_stack);
3879 qfl->qf_directory = NULL;
3880 qf_clean_dir_stack(&qfl->qf_file_stack);
3881 qfl->qf_currfile = NULL;
3882 qfl->qf_multiline = FALSE;
3883 qfl->qf_multiignore = FALSE;
3884 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885}
3886
3887/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003888 * Free error list "idx". Frees all the entries in the quickfix list,
3889 * associated context information and the title.
3890 */
3891 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003892qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003893{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003894 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003895
Bram Moolenaard23a8232018-02-10 18:45:26 +01003896 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003897 free_tv(qfl->qf_ctx);
3898 qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02003899 free_callback(&qfl->qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003900 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003901 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003902}
3903
3904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 * qf_mark_adjust: adjust marks
3906 */
3907 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003908qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003909 win_T *wp,
3910 linenr_T line1,
3911 linenr_T line2,
3912 long amount,
3913 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003915 int i;
3916 qfline_T *qfp;
3917 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003918 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003919 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003920 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
Bram Moolenaarc1542742016-07-20 21:44:37 +02003922 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003923 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003924 if (wp != NULL)
3925 {
3926 if (wp->w_llist == NULL)
3927 return;
3928 qi = wp->w_llist;
3929 }
3930
3931 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003932 {
3933 qf_list_T *qfl = qf_get_list(qi, idx);
3934
3935 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003936 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (qfp->qf_fnum == curbuf->b_fnum)
3938 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003939 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3941 {
3942 if (amount == MAXLNUM)
3943 qfp->qf_cleared = TRUE;
3944 else
3945 qfp->qf_lnum += amount;
3946 }
3947 else if (amount_after && qfp->qf_lnum > line2)
3948 qfp->qf_lnum += amount_after;
3949 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003950 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003951
3952 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003953 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954}
3955
3956/*
3957 * Make a nice message out of the error character and the error number:
3958 * char number message
3959 * e or E 0 " error"
3960 * w or W 0 " warning"
3961 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02003962 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 * 0 0 ""
3964 * other 0 " c"
3965 * e or E n " error n"
3966 * w or W n " warning n"
3967 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02003968 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 * 0 n " error n"
3970 * other n " c n"
3971 * 1 x "" :helpgrep
3972 */
3973 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003974qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
3976 static char_u buf[20];
3977 static char_u cc[3];
3978 char_u *p;
3979
3980 if (c == 'W' || c == 'w')
3981 p = (char_u *)" warning";
3982 else if (c == 'I' || c == 'i')
3983 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02003984 else if (c == 'N' || c == 'n')
3985 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3987 p = (char_u *)" error";
3988 else if (c == 0 || c == 1)
3989 p = (char_u *)"";
3990 else
3991 {
3992 cc[0] = ' ';
3993 cc[1] = c;
3994 cc[2] = NUL;
3995 p = cc;
3996 }
3997
3998 if (nr <= 0)
3999 return p;
4000
4001 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4002 return buf;
4003}
4004
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004006 * When "split" is FALSE: Open the entry/result under the cursor.
4007 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4008 */
4009 void
4010qf_view_result(int split)
4011{
4012 qf_info_T *qi = &ql_info;
4013
4014 if (!bt_quickfix(curbuf))
4015 return;
4016
4017 if (IS_LL_WINDOW(curwin))
4018 qi = GET_LOC_LIST(curwin);
4019
Bram Moolenaar0398e002019-03-21 21:12:49 +01004020 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004021 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004022 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004023 return;
4024 }
4025
4026 if (split)
4027 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004028 // Open the selected entry in a new window
4029 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4030 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004031 return;
4032 }
4033
4034 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4035}
4036
4037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 * ":cwindow": open the quickfix window if we have errors to display,
4039 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004040 * ":lwindow": open the location list window if we have locations to display,
4041 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 */
4043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004044ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004046 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004047 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 win_T *win;
4049
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004050 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4051 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004052
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004053 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004054
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004055 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004056 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004058 // If a quickfix window is open but we have no errors to display,
4059 // close the window. If a quickfix window is not open, then open
4060 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004061 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004062 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004063 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
4065 if (win != NULL)
4066 ex_cclose(eap);
4067 }
4068 else if (win == NULL)
4069 ex_copen(eap);
4070}
4071
4072/*
4073 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004074 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004077ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004079 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004080 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004082 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4083 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004085 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004086 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 if (win != NULL)
4088 win_close(win, FALSE);
4089}
4090
4091/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004092 * Set "w:quickfix_title" if "qi" has a title.
4093 */
4094 static void
4095qf_set_title_var(qf_list_T *qfl)
4096{
4097 if (qfl->qf_title != NULL)
4098 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4099}
4100
4101/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004102 * Goto a quickfix or location list window (if present).
4103 * Returns OK if the window is found, FAIL otherwise.
4104 */
4105 static int
4106qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4107{
4108 win_T *win;
4109
4110 win = qf_find_win(qi);
4111 if (win == NULL)
4112 return FAIL;
4113
4114 win_goto(win);
4115 if (resize)
4116 {
4117 if (vertsplit)
4118 {
4119 if (sz != win->w_width)
4120 win_setwidth(sz);
4121 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004122 else if (sz != win->w_height && win->w_height
4123 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004124 win_setheight(sz);
4125 }
4126
4127 return OK;
4128}
4129
4130/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004131 * Set options for the buffer in the quickfix or location list window.
4132 */
4133 static void
4134qf_set_cwindow_options(void)
4135{
4136 // switch off 'swapfile'
4137 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4138 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4139 OPT_LOCAL);
4140 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4141 RESET_BINDING(curwin);
4142#ifdef FEAT_DIFF
4143 curwin->w_p_diff = FALSE;
4144#endif
4145#ifdef FEAT_FOLDING
4146 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4147 OPT_LOCAL);
4148#endif
4149}
4150
4151/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004152 * Open a new quickfix or location list window, load the quickfix buffer and
4153 * set the appropriate options for the window.
4154 * Returns FAIL if the window could not be opened.
4155 */
4156 static int
4157qf_open_new_cwindow(qf_info_T *qi, int height)
4158{
4159 buf_T *qf_buf;
4160 win_T *oldwin = curwin;
4161 tabpage_T *prevtab = curtab;
4162 int flags = 0;
4163 win_T *win;
4164
4165 qf_buf = qf_find_buf(qi);
4166
4167 // The current window becomes the previous window afterwards.
4168 win = curwin;
4169
Bram Moolenaare1004402020-10-24 20:49:43 +02004170 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004171 // Create the new quickfix window at the very bottom, except when
4172 // :belowright or :aboveleft is used.
4173 win_goto(lastwin);
4174 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004175 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004176 flags = WSP_BELOW;
4177 flags |= WSP_NEWLOC;
4178 if (win_split(height, flags) == FAIL)
4179 return FAIL; // not enough room for window
4180 RESET_BINDING(curwin);
4181
4182 if (IS_LL_STACK(qi))
4183 {
4184 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004185 // location list stack from the window 'win'.
4186 curwin->w_llist_ref = qi;
4187 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004188 }
4189
4190 if (oldwin != curwin)
4191 oldwin = NULL; // don't store info when in another window
4192 if (qf_buf != NULL)
4193 {
4194 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004195 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004196 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004197 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004198 }
4199 else
4200 {
4201 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004202 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4203 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004204 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004205
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004206 // save the number of the new buffer
4207 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004208 }
4209
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004210 // Set the options for the quickfix buffer/window (if not already done)
4211 // Do this even if the quickfix buffer was already present, as an autocmd
4212 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004213 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004214 qf_set_cwindow_options();
4215
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004216 // Only set the height when still in the same tab page and there is no
4217 // window to the side.
4218 if (curtab == prevtab && curwin->w_width == Columns)
4219 win_setheight(height);
4220 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4221 if (win_valid(win))
4222 prevwin = win;
4223
4224 return OK;
4225}
4226
4227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004229 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 */
4231 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004232ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004234 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004235 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004237 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004238 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004240 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4241 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004242
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004243 incr_quickfix_busy();
4244
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (eap->addr_count != 0)
4246 height = eap->line2;
4247 else
4248 height = QF_WINHEIGHT;
4249
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004250 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#ifdef FEAT_GUI
4252 need_mouse_correct = TRUE;
4253#endif
4254
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004255 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004256 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004257 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004258 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004259 if (status == FAIL)
4260 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004261 {
4262 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004263 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004266 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004267 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004268 // Save the current index here, as updating the quickfix buffer may free
4269 // the quickfix list
4270 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004271
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004272 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004273 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004275 decr_quickfix_busy();
4276
4277 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 curwin->w_cursor.col = 0;
4279 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004280 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281}
4282
4283/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004284 * Move the cursor in the quickfix window to "lnum".
4285 */
4286 static void
4287qf_win_goto(win_T *win, linenr_T lnum)
4288{
4289 win_T *old_curwin = curwin;
4290
4291 curwin = win;
4292 curbuf = win->w_buffer;
4293 curwin->w_cursor.lnum = lnum;
4294 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004295 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004296 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004297 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004298 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004299 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004300 curwin = old_curwin;
4301 curbuf = curwin->w_buffer;
4302}
4303
4304/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004305 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004306 */
4307 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004308ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004309{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004310 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004311 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004312
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004313 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4314 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004315
4316 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004317 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4318 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4319}
4320
4321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 * Return the number of the current entry (line number in the quickfix
4323 * window).
4324 */
4325 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004326qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004328 qf_info_T *qi = &ql_info;
4329
4330 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004331 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004332 qi = wp->w_llist_ref;
4333
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004334 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335}
4336
4337/*
4338 * Update the cursor position in the quickfix window to the current error.
4339 * Return TRUE if there is a quickfix window.
4340 */
4341 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004342qf_win_pos_update(
4343 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004344 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345{
4346 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004347 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004349 // Put the cursor on the current error in the quickfix window, so that
4350 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004351 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if (win != NULL
4353 && qf_index <= win->w_buffer->b_ml.ml_line_count
4354 && old_qf_index != qf_index)
4355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (qf_index > old_qf_index)
4357 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004358 win->w_redraw_top = old_qf_index;
4359 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 else
4362 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004363 win->w_redraw_top = qf_index;
4364 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004366 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 return win != NULL;
4369}
4370
4371/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004372 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004373 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004374 */
4375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004376is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004377{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004378 // A window displaying the quickfix buffer will have the w_llist_ref field
4379 // set to NULL.
4380 // A window displaying a location list buffer will have the w_llist_ref
4381 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004382 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004383 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4384 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004385 return TRUE;
4386
4387 return FALSE;
4388}
4389
4390/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004391 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4392 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004393 */
4394 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004395qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004396{
4397 win_T *win;
4398
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004399 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004400 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004401 return win;
4402 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004403}
4404
4405/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004406 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004407 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 */
4409 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004410qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004412 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004413 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004415 if (qi->qf_bufnr != INVALID_QFBUFNR)
4416 {
4417 buf_T *qfbuf;
4418 qfbuf = buflist_findnr(qi->qf_bufnr);
4419 if (qfbuf != NULL)
4420 return qfbuf;
4421 // buffer is no longer present
4422 qi->qf_bufnr = INVALID_QFBUFNR;
4423 }
4424
Bram Moolenaar9c102382006-05-03 21:26:49 +00004425 FOR_ALL_TAB_WINDOWS(tp, win)
4426 if (is_qf_win(win, qi))
4427 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004428
Bram Moolenaar9c102382006-05-03 21:26:49 +00004429 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430}
4431
4432/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004433 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004434 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004435 */
4436 int
4437qf_process_qftf_option(void)
4438{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004439 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004440}
4441
4442/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004443 * Update the w:quickfix_title variable in the quickfix/location list window in
4444 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004445 */
4446 static void
4447qf_update_win_titlevar(qf_info_T *qi)
4448{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004449 qf_list_T *qfl = qf_get_curlist(qi);
4450 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004451 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004452 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004453
Bram Moolenaar530bed92020-12-16 21:02:56 +01004454 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004455 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004456 if (is_qf_win(win, qi))
4457 {
4458 curwin = win;
4459 qf_set_title_var(qfl);
4460 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004461 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004462 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004463}
4464
4465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 * Find the quickfix buffer. If it exists, update the contents.
4467 */
4468 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004469qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
4471 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004472 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004475 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004476 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (buf != NULL)
4478 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004479 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004480 int qf_winid = 0;
4481
4482 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004483 {
4484 if (curwin->w_llist == qi)
4485 win = curwin;
4486 else
4487 {
4488 win = qf_find_win_with_loclist(qi);
4489 if (win == NULL)
4490 return;
4491 }
4492 qf_winid = win->w_id;
4493 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004494
4495 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004496 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004497 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Bram Moolenaard823fa92016-08-12 16:29:27 +02004499 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004500
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004501 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004502 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004503
Bram Moolenaar864293a2016-06-02 13:40:04 +02004504 if (old_last == NULL)
4505 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004506 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004507
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004508 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004509 aucmd_restbuf(&aco);
4510 }
4511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004512 // Only redraw when added lines are visible. This avoids flickering
4513 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004514 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4515 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517}
4518
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004519/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004520 * Add an error line to the quickfix buffer.
4521 */
4522 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004523qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004524 buf_T *buf, // quickfix window buffer
4525 linenr_T lnum,
4526 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004527 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004528 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004529 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004530{
4531 int len;
4532 buf_T *errbuf;
4533
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004534 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004535 // for this entry, then use it.
4536 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004537 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004538 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004539 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004540 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004541 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004542 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4543 len = (int)STRLEN(IObuff);
4544 }
4545 else if (qfp->qf_fnum != 0
4546 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4547 && errbuf->b_fname != NULL)
4548 {
4549 if (qfp->qf_type == 1) // :helpgrep
4550 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4551 else
4552 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004553 // Shorten the file name if not done already.
4554 // For optimization, do this only for the first entry in a
4555 // buffer.
4556 if (first_bufline && (errbuf->b_sfname == NULL
4557 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004558 {
4559 if (*dirname == NUL)
4560 mch_dirname(dirname, MAXPATHL);
4561 shorten_buf_fname(errbuf, dirname, FALSE);
4562 }
4563 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4564 }
4565 len = (int)STRLEN(IObuff);
4566 }
4567 else
4568 len = 0;
4569
4570 if (len < IOSIZE - 1)
4571 IObuff[len++] = '|';
4572
4573 if (qfp->qf_lnum > 0)
4574 {
thinca6864efa2021-06-19 20:45:20 +02004575 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004576 len += (int)STRLEN(IObuff + len);
4577
Bram Moolenaar858ba062020-05-31 23:11:59 +02004578 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4579 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004580 len += (int)STRLEN(IObuff + len);
4581 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004582 else if (qfp->qf_pattern != NULL)
4583 {
4584 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4585 len += (int)STRLEN(IObuff + len);
4586 }
4587 if (len < IOSIZE - 2)
4588 {
4589 IObuff[len++] = '|';
4590 IObuff[len++] = ' ';
4591 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004592
Bram Moolenaar858ba062020-05-31 23:11:59 +02004593 // Remove newlines and leading whitespace from the text.
4594 // For an unrecognized line keep the indent, the compiler may
4595 // mark a word with ^^^^.
4596 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4597 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004598 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004599
4600 if (ml_append_buf(buf, lnum, IObuff,
4601 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4602 return FAIL;
4603
4604 return OK;
4605}
4606
Bram Moolenaard43906d2020-07-20 21:31:32 +02004607/*
4608 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4609 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4610 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004611 static list_T *
4612call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4613{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004614 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004615 list_T *qftf_list = NULL;
4616
4617 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4618 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4619 // set.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004620 if (qfl->qftf_cb.cb_name != NULL)
4621 cb = &qfl->qftf_cb;
4622 if (cb != NULL && cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004623 {
4624 typval_T args[1];
4625 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004626 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004627
4628 // create the dict argument
4629 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4630 return NULL;
4631 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4632 dict_add_number(d, "winid", (long)qf_winid);
4633 dict_add_number(d, "id", (long)qfl->qf_id);
4634 dict_add_number(d, "start_idx", start_idx);
4635 dict_add_number(d, "end_idx", end_idx);
4636 ++d->dv_refcount;
4637 args[0].v_type = VAR_DICT;
4638 args[0].vval.v_dict = d;
4639
Bram Moolenaard43906d2020-07-20 21:31:32 +02004640 qftf_list = NULL;
4641 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4642 {
4643 if (rettv.v_type == VAR_LIST)
4644 {
4645 qftf_list = rettv.vval.v_list;
4646 qftf_list->lv_refcount++;
4647 }
4648 clear_tv(&rettv);
4649 }
4650 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004651 }
4652
4653 return qftf_list;
4654}
4655
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 * Fill current buffer with quickfix errors, replacing any previous contents.
4658 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004659 * If "old_last" is not NULL append the items after this one.
4660 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4661 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 */
4663 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004664qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004666 linenr_T lnum;
4667 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004668 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004669 list_T *qftf_list = NULL;
4670 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaar864293a2016-06-02 13:40:04 +02004672 if (old_last == NULL)
4673 {
4674 if (buf != curbuf)
4675 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004676 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004677 return;
4678 }
4679
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004680 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004681 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004682 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004685 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004686 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004688 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004689 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004690 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004691
4692 *dirname = NUL;
4693
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004694 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004695 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004696 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004697 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004698 lnum = 0;
4699 }
4700 else
4701 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004702 if (old_last->qf_next != NULL)
4703 qfp = old_last->qf_next;
4704 else
4705 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004706 lnum = buf->b_ml.ml_line_count;
4707 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004708
4709 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4710 (long)qfl->qf_count);
4711 if (qftf_list != NULL)
4712 qftf_li = qftf_list->lv_first;
4713
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004714 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004716 char_u *qftf_str = NULL;
4717
Bram Moolenaard43906d2020-07-20 21:31:32 +02004718 // Use the text supplied by the user defined function (if any).
4719 // If the returned value is not string, then ignore the rest
4720 // of the returned values and use the default.
4721 if (qftf_li != NULL && !invalid_val)
4722 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004723 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004724 if (qftf_str == NULL)
4725 invalid_val = TRUE;
4726 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004727
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004728 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4729 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004731
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004732 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004733 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004735 if (qfp == NULL)
4736 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004737
4738 if (qftf_li != NULL)
4739 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004741
4742 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004743 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004744 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004747 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 check_lnums(TRUE);
4749
Bram Moolenaar864293a2016-06-02 13:40:04 +02004750 if (old_last == NULL)
4751 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004752 // Set the 'filetype' to "qf" each time after filling the buffer.
4753 // This resembles reading a file into a buffer, it's more logical when
4754 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004755 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004756 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4757 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004759 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004760 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004762 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004764 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004765 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004766
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004767 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004768 redraw_curbuf_later(NOT_VALID);
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004771 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 KeyTyped = old_KeyTyped;
4773}
4774
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004775/*
4776 * For every change made to the quickfix list, update the changed tick.
4777 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004778 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004779qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004780{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004781 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004782}
4783
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004785 * Return the quickfix/location list number with the given identifier.
4786 * Returns -1 if list is not found.
4787 */
4788 static int
4789qf_id2nr(qf_info_T *qi, int_u qfid)
4790{
4791 int qf_idx;
4792
4793 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4794 if (qi->qf_lists[qf_idx].qf_id == qfid)
4795 return qf_idx;
4796 return INVALID_QFIDX;
4797}
4798
4799/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004800 * If the current list is not "save_qfid" and we can find the list with that ID
4801 * then make it the current list.
4802 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004803 * Returns OK if successfully restored the list. Returns FAIL if the list with
4804 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004805 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004806 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004807qf_restore_list(qf_info_T *qi, int_u save_qfid)
4808{
4809 int curlist;
4810
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004811 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004812 {
4813 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004814 if (curlist < 0)
4815 // list is not present
4816 return FAIL;
4817 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004818 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004819 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004820}
4821
4822/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004823 * Jump to the first entry if there is one.
4824 */
4825 static void
4826qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4827{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004828 if (qf_restore_list(qi, save_qfid) == FAIL)
4829 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004830
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004831 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004832 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004833 qf_jump(qi, 0, 0, forceit);
4834}
4835
4836/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004837 * Return TRUE when using ":vimgrep" for ":grep".
4838 */
4839 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004840grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004841{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004842 return ((cmdidx == CMD_grep
4843 || cmdidx == CMD_lgrep
4844 || cmdidx == CMD_grepadd
4845 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004846 && STRCMP("internal",
4847 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4848}
4849
4850/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004851 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004853 static char_u *
4854make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004856 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004857 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004858 case CMD_make: return (char_u *)"make";
4859 case CMD_lmake: return (char_u *)"lmake";
4860 case CMD_grep: return (char_u *)"grep";
4861 case CMD_lgrep: return (char_u *)"lgrep";
4862 case CMD_grepadd: return (char_u *)"grepadd";
4863 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4864 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866}
4867
4868/*
4869 * Return the name for the errorfile, in allocated memory.
4870 * Find a new unique name when 'makeef' contains "##".
4871 * Returns NULL for error.
4872 */
4873 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004874get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
4876 char_u *p;
4877 char_u *name;
4878 static int start = -1;
4879 static int off = 0;
4880#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004881 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#endif
4883
4884 if (*p_mef == NUL)
4885 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004886 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004888 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return name;
4890 }
4891
4892 for (p = p_mef; *p; ++p)
4893 if (p[0] == '#' && p[1] == '#')
4894 break;
4895
4896 if (*p == NUL)
4897 return vim_strsave(p_mef);
4898
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004899 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 for (;;)
4901 {
4902 if (start == -1)
4903 start = mch_get_pid();
4904 else
4905 off += 19;
4906
Bram Moolenaar964b3742019-05-24 18:54:09 +02004907 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (name == NULL)
4909 break;
4910 STRCPY(name, p_mef);
4911 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4912 STRCAT(name, p + 2);
4913 if (mch_getperm(name) < 0
4914#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004915 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 && mch_lstat((char *)name, &sb) < 0
4917#endif
4918 )
4919 break;
4920 vim_free(name);
4921 }
4922 return name;
4923}
4924
4925/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004926 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4927 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4928 */
4929 static char_u *
4930make_get_fullcmd(char_u *makecmd, char_u *fname)
4931{
4932 char_u *cmd;
4933 unsigned len;
4934
4935 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4936 if (*p_sp != NUL)
4937 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4938 cmd = alloc(len);
4939 if (cmd == NULL)
4940 return NULL;
4941 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4942 (char *)p_shq);
4943
4944 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4945 if (*p_sp != NUL)
4946 append_redir(cmd, len, p_sp, fname);
4947
4948 // Display the fully formed command. Output a newline if there's something
4949 // else than the :make command that was typed (in which case the cursor is
4950 // in column 0).
4951 if (msg_col == 0)
4952 msg_didout = FALSE;
4953 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004954 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004955 msg_outtrans(cmd); // show what we are doing
4956
4957 return cmd;
4958}
4959
4960/*
4961 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4962 */
4963 void
4964ex_make(exarg_T *eap)
4965{
4966 char_u *fname;
4967 char_u *cmd;
4968 char_u *enc = NULL;
4969 win_T *wp = NULL;
4970 qf_info_T *qi = &ql_info;
4971 int res;
4972 char_u *au_name = NULL;
4973 int_u save_qfid;
4974
4975 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4976 if (grep_internal(eap->cmdidx))
4977 {
4978 ex_vimgrep(eap);
4979 return;
4980 }
4981
4982 au_name = make_get_auname(eap->cmdidx);
4983 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4984 curbuf->b_fname, TRUE, curbuf))
4985 {
4986#ifdef FEAT_EVAL
4987 if (aborting())
4988 return;
4989#endif
4990 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004991 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004992
4993 if (is_loclist_cmd(eap->cmdidx))
4994 wp = curwin;
4995
4996 autowrite_all();
4997 fname = get_mef_name();
4998 if (fname == NULL)
4999 return;
5000 mch_remove(fname); // in case it's not unique
5001
5002 cmd = make_get_fullcmd(eap->arg, fname);
5003 if (cmd == NULL)
5004 return;
5005
5006 // let the shell know if we are redirecting output or not
5007 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5008
5009#ifdef AMIGA
5010 out_flush();
5011 // read window status report and redraw before message
5012 (void)char_avail();
5013#endif
5014
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005015 incr_quickfix_busy();
5016
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005017 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
5018 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
5019 (eap->cmdidx != CMD_grepadd
5020 && eap->cmdidx != CMD_lgrepadd),
5021 qf_cmdtitle(*eap->cmdlinep), enc);
5022 if (wp != NULL)
5023 {
5024 qi = GET_LOC_LIST(wp);
5025 if (qi == NULL)
5026 goto cleanup;
5027 }
5028 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005029 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005030
5031 // Remember the current quickfix list identifier, so that we can
5032 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005033 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005034 if (au_name != NULL)
5035 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5036 curbuf->b_fname, TRUE, curbuf);
5037 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5038 // display the first error
5039 qf_jump_first(qi, save_qfid, FALSE);
5040
5041cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005042 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005043 mch_remove(fname);
5044 vim_free(fname);
5045 vim_free(cmd);
5046}
5047
5048/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005049 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005050 */
5051 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005052qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005053{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005054 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005055
5056 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5057 return 0;
5058 return qf_get_curlist(qi)->qf_count;
5059}
5060
5061/*
5062 * Returns the number of valid entries in the current quickfix/location list.
5063 */
5064 int
5065qf_get_valid_size(exarg_T *eap)
5066{
5067 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005068 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005069 qfline_T *qfp;
5070 int i, sz = 0;
5071 int prev_fnum = 0;
5072
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005073 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5074 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005075
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005076 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005077 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005078 {
5079 if (qfp->qf_valid)
5080 {
5081 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005082 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005083 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5084 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005085 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005086 sz++;
5087 prev_fnum = qfp->qf_fnum;
5088 }
5089 }
5090 }
5091
5092 return sz;
5093}
5094
5095/*
5096 * Returns the current index of the quickfix/location list.
5097 * Returns 0 if there is an error.
5098 */
5099 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005100qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005101{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005102 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005103
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005104 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5105 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005106
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005107 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005108}
5109
5110/*
5111 * Returns the current index in the quickfix/location list (counting only valid
5112 * entries). If no valid entries are in the list, then returns 1.
5113 */
5114 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005115qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005116{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005117 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005118 qf_list_T *qfl;
5119 qfline_T *qfp;
5120 int i, eidx = 0;
5121 int prev_fnum = 0;
5122
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005123 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5124 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005125
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005126 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005127 qfp = qfl->qf_start;
5128
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005129 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005130 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005131 return 1;
5132
5133 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5134 {
5135 if (qfp->qf_valid)
5136 {
5137 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5138 {
5139 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5140 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005141 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005142 eidx++;
5143 prev_fnum = qfp->qf_fnum;
5144 }
5145 }
5146 else
5147 eidx++;
5148 }
5149 }
5150
5151 return eidx ? eidx : 1;
5152}
5153
5154/*
5155 * Get the 'n'th valid error entry in the quickfix or location list.
5156 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5157 * For :cdo and :ldo returns the 'n'th valid error entry.
5158 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5159 */
5160 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005161qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005162{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005163 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005164 int i, eidx;
5165 int prev_fnum = 0;
5166
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005167 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005168 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005169 return 1;
5170
Bram Moolenaar95946f12019-03-31 15:31:59 +02005171 eidx = 0;
5172 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005173 {
5174 if (qfp->qf_valid)
5175 {
5176 if (fdo)
5177 {
5178 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5179 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005180 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005181 eidx++;
5182 prev_fnum = qfp->qf_fnum;
5183 }
5184 }
5185 else
5186 eidx++;
5187 }
5188
5189 if (eidx == n)
5190 break;
5191 }
5192
5193 if (i <= qfl->qf_count)
5194 return i;
5195 else
5196 return 1;
5197}
5198
5199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005201 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005202 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 */
5204 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005205ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005207 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005208 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005209
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005210 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5211 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005212
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005213 if (eap->addr_count > 0)
5214 errornr = (int)eap->line2;
5215 else
5216 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005217 switch (eap->cmdidx)
5218 {
5219 case CMD_cc: case CMD_ll:
5220 errornr = 0;
5221 break;
5222 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5223 case CMD_lfirst:
5224 errornr = 1;
5225 break;
5226 default:
5227 errornr = 32767;
5228 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005229 }
5230
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005231 // For cdo and ldo commands, jump to the nth valid error.
5232 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005233 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5234 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005235 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005236 eap->addr_count > 0 ? (int)eap->line1 : 1,
5237 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5238
5239 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240}
5241
5242/*
5243 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005244 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005245 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 */
5247 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005248ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005250 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005251 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005252 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005253
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005254 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5255 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005256
Bram Moolenaar55b69262017-08-13 13:42:01 +02005257 if (eap->addr_count > 0
5258 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5259 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005260 errornr = (int)eap->line2;
5261 else
5262 errornr = 1;
5263
Bram Moolenaar39665952018-08-15 20:59:48 +02005264 // Depending on the command jump to either next or previous entry/file.
5265 switch (eap->cmdidx)
5266 {
5267 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5268 dir = FORWARD;
5269 break;
5270 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5271 case CMD_lNext:
5272 dir = BACKWARD;
5273 break;
5274 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5275 dir = FORWARD_FILE;
5276 break;
5277 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5278 dir = BACKWARD_FILE;
5279 break;
5280 default:
5281 dir = FORWARD;
5282 break;
5283 }
5284
5285 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286}
5287
5288/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005289 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5290 * The index of the entry is stored in 'errornr'.
5291 * Returns NULL if an entry is not found.
5292 */
5293 static qfline_T *
5294qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5295{
5296 qfline_T *qfp = NULL;
5297 int idx = 0;
5298
5299 // Find the first entry in this file
5300 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5301 if (qfp->qf_fnum == bnr)
5302 break;
5303
5304 *errornr = idx;
5305 return qfp;
5306}
5307
5308/*
5309 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5310 * with the error number for the first entry. Assumes the entries are sorted in
5311 * the quickfix list by line number.
5312 */
5313 static qfline_T *
5314qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5315{
5316 while (!got_int
5317 && entry->qf_prev != NULL
5318 && entry->qf_fnum == entry->qf_prev->qf_fnum
5319 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5320 {
5321 entry = entry->qf_prev;
5322 --*errornr;
5323 }
5324
5325 return entry;
5326}
5327
5328/*
5329 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5330 * with the error number for the last entry. Assumes the entries are sorted in
5331 * the quickfix list by line number.
5332 */
5333 static qfline_T *
5334qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5335{
5336 while (!got_int &&
5337 entry->qf_next != NULL
5338 && entry->qf_fnum == entry->qf_next->qf_fnum
5339 && entry->qf_lnum == entry->qf_next->qf_lnum)
5340 {
5341 entry = entry->qf_next;
5342 ++*errornr;
5343 }
5344
5345 return entry;
5346}
5347
5348/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005349 * Returns TRUE if the specified quickfix entry is
5350 * after the given line (linewise is TRUE)
5351 * or after the line and column.
5352 */
5353 static int
5354qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5355{
5356 if (linewise)
5357 return qfp->qf_lnum > pos->lnum;
5358 else
5359 return (qfp->qf_lnum > pos->lnum ||
5360 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5361}
5362
5363/*
5364 * Returns TRUE if the specified quickfix entry is
5365 * before the given line (linewise is TRUE)
5366 * or before the line and column.
5367 */
5368 static int
5369qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5370{
5371 if (linewise)
5372 return qfp->qf_lnum < pos->lnum;
5373 else
5374 return (qfp->qf_lnum < pos->lnum ||
5375 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5376}
5377
5378/*
5379 * Returns TRUE if the specified quickfix entry is
5380 * on or after the given line (linewise is TRUE)
5381 * or on or after the line and column.
5382 */
5383 static int
5384qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5385{
5386 if (linewise)
5387 return qfp->qf_lnum >= pos->lnum;
5388 else
5389 return (qfp->qf_lnum > pos->lnum ||
5390 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5391}
5392
5393/*
5394 * Returns TRUE if the specified quickfix entry is
5395 * on or before the given line (linewise is TRUE)
5396 * or on or before the line and column.
5397 */
5398 static int
5399qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5400{
5401 if (linewise)
5402 return qfp->qf_lnum <= pos->lnum;
5403 else
5404 return (qfp->qf_lnum < pos->lnum ||
5405 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5406}
5407
5408/*
5409 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5410 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5411 * multiple entries on a single line as one. Otherwise returns the entry after
5412 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005413 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5414 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005415 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005416 */
5417 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005418qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005419 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005420 pos_T *pos,
5421 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005422 qfline_T *qfp,
5423 int *errornr)
5424{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005425 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005426 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005427 return qfp;
5428
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005429 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005430 while (qfp->qf_next != NULL
5431 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005432 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005433 {
5434 qfp = qfp->qf_next;
5435 ++*errornr;
5436 }
5437
5438 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005439 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005440 return NULL;
5441
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005442 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005443 qfp = qfp->qf_next;
5444 ++*errornr;
5445
5446 return qfp;
5447}
5448
5449/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005450 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5451 * If 'linewise' is TRUE, returns the entry before the specified line and
5452 * treats multiple entries on a single line as one. Otherwise returns the entry
5453 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005454 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5455 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005456 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005457 */
5458 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005459qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005460 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005461 pos_T *pos,
5462 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005463 qfline_T *qfp,
5464 int *errornr)
5465{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005466 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005467 while (qfp->qf_next != NULL
5468 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005469 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005470 {
5471 qfp = qfp->qf_next;
5472 ++*errornr;
5473 }
5474
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005475 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005476 return NULL;
5477
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005478 if (linewise)
5479 // If multiple entries are on the same line, then use the first entry
5480 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005481
5482 return qfp;
5483}
5484
5485/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005486 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005487 * the direction 'dir'.
5488 */
5489 static qfline_T *
5490qf_find_closest_entry(
5491 qf_list_T *qfl,
5492 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005493 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005494 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005495 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005496 int *errornr)
5497{
5498 qfline_T *qfp;
5499
5500 *errornr = 0;
5501
5502 // Find the first entry in this file
5503 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5504 if (qfp == NULL)
5505 return NULL; // no entry in this file
5506
5507 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005508 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005509 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005510 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005511
5512 return qfp;
5513}
5514
5515/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005516 * Get the nth quickfix entry below the specified entry. Searches forward in
5517 * the list. If linewise is TRUE, then treat multiple entries on a single line
5518 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005519 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005520 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005521qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005522{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005523 qfline_T *entry = entry_arg;
5524
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005525 while (n-- > 0 && !got_int)
5526 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005527 int first_errornr = *errornr;
5528
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005529 if (linewise)
5530 // Treat all the entries on the same line in this file as one
5531 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005532
5533 if (entry->qf_next == NULL
5534 || entry->qf_next->qf_fnum != entry->qf_fnum)
5535 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005536 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005537 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005538 break;
5539 }
5540
5541 entry = entry->qf_next;
5542 ++*errornr;
5543 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005544}
5545
5546/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005547 * Get the nth quickfix entry above the specified entry. Searches backwards in
5548 * the list. If linewise is TRUE, then treat multiple entries on a single line
5549 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005550 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005551 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005552qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005553{
5554 while (n-- > 0 && !got_int)
5555 {
5556 if (entry->qf_prev == NULL
5557 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5558 break;
5559
5560 entry = entry->qf_prev;
5561 --*errornr;
5562
5563 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005564 if (linewise)
5565 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005566 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005567}
5568
5569/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005570 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5571 * the specified direction. Returns the error number in the quickfix list or 0
5572 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005573 */
5574 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005575qf_find_nth_adj_entry(
5576 qf_list_T *qfl,
5577 int bnr,
5578 pos_T *pos,
5579 int n,
5580 int dir,
5581 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005582{
5583 qfline_T *adj_entry;
5584 int errornr;
5585
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005586 // Find an entry closest to the specified position
5587 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005588 if (adj_entry == NULL)
5589 return 0;
5590
5591 if (--n > 0)
5592 {
5593 // Go to the n'th entry in the current buffer
5594 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005595 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005596 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005597 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005598 }
5599
5600 return errornr;
5601}
5602
5603/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005604 * Jump to a quickfix entry in the current file nearest to the current line or
5605 * current line/col.
5606 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5607 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005608 */
5609 void
5610ex_cbelow(exarg_T *eap)
5611{
5612 qf_info_T *qi;
5613 qf_list_T *qfl;
5614 int dir;
5615 int buf_has_flag;
5616 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005617 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005618
5619 if (eap->addr_count > 0 && eap->line2 <= 0)
5620 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005621 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005622 return;
5623 }
5624
5625 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005626 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5627 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005628 buf_has_flag = BUF_HAS_QF_ENTRY;
5629 else
5630 buf_has_flag = BUF_HAS_LL_ENTRY;
5631 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5632 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005633 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005634 return;
5635 }
5636
5637 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5638 return;
5639
5640 qfl = qf_get_curlist(qi);
5641 // check if the list has valid errors
5642 if (!qf_list_has_valid_entries(qfl))
5643 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005644 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005645 return;
5646 }
5647
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005648 if (eap->cmdidx == CMD_cbelow
5649 || eap->cmdidx == CMD_lbelow
5650 || eap->cmdidx == CMD_cafter
5651 || eap->cmdidx == CMD_lafter)
5652 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005653 dir = FORWARD;
5654 else
5655 dir = BACKWARD;
5656
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005657 pos = curwin->w_cursor;
5658 // A quickfix entry column number is 1 based whereas cursor column
5659 // number is 0 based. Adjust the column number.
5660 pos.col++;
5661 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5662 eap->addr_count > 0 ? eap->line2 : 0, dir,
5663 eap->cmdidx == CMD_cbelow
5664 || eap->cmdidx == CMD_lbelow
5665 || eap->cmdidx == CMD_cabove
5666 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005667
5668 if (errornr > 0)
5669 qf_jump(qi, 0, errornr, FALSE);
5670 else
5671 emsg(_(e_no_more_items));
5672}
5673
5674/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005675 * Return the autocmd name for the :cfile Ex commands
5676 */
5677 static char_u *
5678cfile_get_auname(cmdidx_T cmdidx)
5679{
5680 switch (cmdidx)
5681 {
5682 case CMD_cfile: return (char_u *)"cfile";
5683 case CMD_cgetfile: return (char_u *)"cgetfile";
5684 case CMD_caddfile: return (char_u *)"caddfile";
5685 case CMD_lfile: return (char_u *)"lfile";
5686 case CMD_lgetfile: return (char_u *)"lgetfile";
5687 case CMD_laddfile: return (char_u *)"laddfile";
5688 default: return NULL;
5689 }
5690}
5691
5692/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005693 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005694 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 */
5696 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005697ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005699 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005700 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005701 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005702 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005703 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005704 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005705
Bram Moolenaara16123a2019-03-28 20:31:07 +01005706 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005707 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5708 NULL, FALSE, curbuf))
5709 {
5710#ifdef FEAT_EVAL
5711 if (aborting())
5712 return;
5713#endif
5714 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005715
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005716 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005717#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005718 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005719 {
5720 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005721 NULL, NULL,
5722 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005723 if (browse_file == NULL)
5724 return;
5725 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5726 vim_free(browse_file);
5727 }
5728 else
5729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005731 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005732
Bram Moolenaar39665952018-08-15 20:59:48 +02005733 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005734 wp = curwin;
5735
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005736 incr_quickfix_busy();
5737
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005738 // This function is used by the :cfile, :cgetfile and :caddfile
5739 // commands.
5740 // :cfile always creates a new quickfix list and jumps to the
5741 // first error.
5742 // :cgetfile creates a new quickfix list but doesn't jump to the
5743 // first error.
5744 // :caddfile adds to an existing quickfix list. If there is no
5745 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005746 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005747 && eap->cmdidx != CMD_laddfile),
5748 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005749 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005750 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005751 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005752 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005753 {
5754 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005755 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005756 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005757 }
5758 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005759 qf_list_changed(qf_get_curlist(qi));
5760 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005761 if (au_name != NULL)
5762 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005763
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005764 // Jump to the first error for a new list and if autocmds didn't
5765 // free the list.
5766 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5767 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005768 // display the first error
5769 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005770
5771 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005772}
5773
5774/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005775 * Return the vimgrep autocmd name.
5776 */
5777 static char_u *
5778vgr_get_auname(cmdidx_T cmdidx)
5779{
5780 switch (cmdidx)
5781 {
5782 case CMD_vimgrep: return (char_u *)"vimgrep";
5783 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5784 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5785 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5786 case CMD_grep: return (char_u *)"grep";
5787 case CMD_lgrep: return (char_u *)"lgrep";
5788 case CMD_grepadd: return (char_u *)"grepadd";
5789 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5790 default: return NULL;
5791 }
5792}
5793
5794/*
5795 * Initialize the regmatch used by vimgrep for pattern "s".
5796 */
5797 static void
5798vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5799{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005800 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005801 regmatch->regprog = NULL;
5802
5803 if (s == NULL || *s == NUL)
5804 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005805 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005806 if (last_search_pat() == NULL)
5807 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005808 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005809 return;
5810 }
5811 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5812 }
5813 else
5814 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5815
5816 regmatch->rmm_ic = p_ic;
5817 regmatch->rmm_maxcol = 0;
5818}
5819
5820/*
5821 * Display a file name when vimgrep is running.
5822 */
5823 static void
5824vgr_display_fname(char_u *fname)
5825{
5826 char_u *p;
5827
5828 msg_start();
5829 p = msg_strtrunc(fname, TRUE);
5830 if (p == NULL)
5831 msg_outtrans(fname);
5832 else
5833 {
5834 msg_outtrans(p);
5835 vim_free(p);
5836 }
5837 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005838 msg_didout = FALSE; // overwrite this message
5839 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005840 msg_col = 0;
5841 out_flush();
5842}
5843
5844/*
5845 * Load a dummy buffer to search for a pattern using vimgrep.
5846 */
5847 static buf_T *
5848vgr_load_dummy_buf(
5849 char_u *fname,
5850 char_u *dirname_start,
5851 char_u *dirname_now)
5852{
5853 int save_mls;
5854#if defined(FEAT_SYN_HL)
5855 char_u *save_ei = NULL;
5856#endif
5857 buf_T *buf;
5858
5859#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005860 // Don't do Filetype autocommands to avoid loading syntax and
5861 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005862 save_ei = au_event_disable(",Filetype");
5863#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005864 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005865 save_mls = p_mls;
5866 p_mls = 0;
5867
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005868 // Load file into a buffer, so that 'fileencoding' is detected,
5869 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005870 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5871
5872 p_mls = save_mls;
5873#if defined(FEAT_SYN_HL)
5874 au_event_restore(save_ei);
5875#endif
5876
5877 return buf;
5878}
5879
5880/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005881 * Check whether a quickfix/location list is valid. Autocmds may remove or
5882 * change a quickfix list when vimgrep is running. If the list is not found,
5883 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005884 */
5885 static int
5886vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005887 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005888 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005889 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005890 char_u *title)
5891{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005892 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005893 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005894 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005895 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005896 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005897 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005898 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005899 return FALSE;
5900 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005901 else
5902 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005903 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005904 qf_new_list(qi, title);
5905 return TRUE;
5906 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005907 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005908
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005909 if (qf_restore_list(qi, qfid) == FAIL)
5910 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005911
5912 return TRUE;
5913}
5914
5915/*
5916 * Search for a pattern in all the lines in a buffer and add the matching lines
5917 * to a quickfix list.
5918 */
5919 static int
5920vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005921 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005922 char_u *fname,
5923 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005924 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005925 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005926 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005927 int duplicate_name,
5928 int flags)
5929{
5930 int found_match = FALSE;
5931 long lnum;
5932 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02005933 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005934
Bram Moolenaar1c299432018-10-28 14:36:09 +01005935 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005936 {
5937 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005938 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005939 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005940 // Regular expression match
5941 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5942 col, NULL, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005943 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005944 // Pass the buffer number so that it gets used even for a
5945 // dummy buffer, unless duplicate_name is set, then the
5946 // buffer will be wiped out below.
5947 if (qf_add_entry(qfl,
5948 NULL, // dir
5949 fname,
5950 NULL,
5951 duplicate_name ? 0 : buf->b_fnum,
5952 ml_get_buf(buf,
5953 regmatch->startpos[0].lnum + lnum, FALSE),
5954 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02005955 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005956 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02005957 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005958 FALSE, // vis_col
5959 NULL, // search pattern
5960 0, // nr
5961 0, // type
5962 TRUE // valid
5963 ) == QF_FAIL)
5964 {
5965 got_int = TRUE;
5966 break;
5967 }
5968 found_match = TRUE;
5969 if (--*tomatch == 0)
5970 break;
5971 if ((flags & VGR_GLOBAL) == 0
5972 || regmatch->endpos[0].lnum > 0)
5973 break;
5974 col = regmatch->endpos[0].col
5975 + (col == regmatch->endpos[0].col);
5976 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5977 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005978 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005979 }
5980 else
5981 {
5982 char_u *str = ml_get_buf(buf, lnum, FALSE);
5983 int score;
5984 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02005985 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005986
5987 // Fuzzy string match
5988 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
5989 {
5990 // Pass the buffer number so that it gets used even for a
5991 // dummy buffer, unless duplicate_name is set, then the
5992 // buffer will be wiped out below.
5993 if (qf_add_entry(qfl,
5994 NULL, // dir
5995 fname,
5996 NULL,
5997 duplicate_name ? 0 : buf->b_fnum,
5998 str,
5999 lnum,
thinca6864efa2021-06-19 20:45:20 +02006000 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006001 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006002 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006003 FALSE, // vis_col
6004 NULL, // search pattern
6005 0, // nr
6006 0, // type
6007 TRUE // valid
6008 ) == QF_FAIL)
6009 {
6010 got_int = TRUE;
6011 break;
6012 }
6013 found_match = TRUE;
6014 if (--*tomatch == 0)
6015 break;
6016 if ((flags & VGR_GLOBAL) == 0)
6017 break;
6018 col = matches[pat_len - 1] + col + 1;
6019 if (col > (colnr_T)STRLEN(str))
6020 break;
6021 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006022 }
6023 line_breakcheck();
6024 if (got_int)
6025 break;
6026 }
6027
6028 return found_match;
6029}
6030
6031/*
6032 * Jump to the first match and update the directory.
6033 */
6034 static void
6035vgr_jump_to_match(
6036 qf_info_T *qi,
6037 int forceit,
6038 int *redraw_for_dummy,
6039 buf_T *first_match_buf,
6040 char_u *target_dir)
6041{
6042 buf_T *buf;
6043
6044 buf = curbuf;
6045 qf_jump(qi, 0, 0, forceit);
6046 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006047 // If we jumped to another buffer redrawing will already be
6048 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006049 *redraw_for_dummy = FALSE;
6050
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006051 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006052 if (curbuf == first_match_buf && target_dir != NULL)
6053 {
6054 exarg_T ea;
6055
Bram Moolenaara80faa82020-04-12 19:37:17 +02006056 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006057 ea.arg = target_dir;
6058 ea.cmdidx = CMD_lcd;
6059 ex_cd(&ea);
6060 }
6061}
6062
6063/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006064 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006065 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006066typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006067{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006068 long tomatch; // maximum number of matches to find
6069 char_u *spat; // search pattern
6070 int flags; // search modifier
6071 char_u **fnames; // list of files to search
6072 int fcount; // number of files
6073 regmmatch_T regmatch; // compiled search pattern
6074 char_u *qf_title; // quickfix list title
6075} vgr_args_T;
6076
6077/*
6078 * Process :vimgrep command arguments. The command syntax is:
6079 *
6080 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6081 */
6082 static int
6083vgr_process_args(
6084 exarg_T *eap,
6085 vgr_args_T *args)
6086{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006087 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006088
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006089 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006090
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006091 args->regmatch.regprog = NULL;
6092 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006093
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006094 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006095 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006096 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006097 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006098
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006099 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006100 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006101 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006102 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006103 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006104 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006105 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006106
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006107 vgr_init_regmatch(&args->regmatch, args->spat);
6108 if (args->regmatch.regprog == NULL)
6109 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006110
6111 p = skipwhite(p);
6112 if (*p == NUL)
6113 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006114 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006115 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006116 }
6117
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006118 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006119 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6120 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006121 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006122 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006123 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006124 }
6125
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006126 return OK;
6127}
6128
6129/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006130 * Return TRUE if "buf" had an existing swap file, the current swap file does
6131 * not end in ".swp".
6132 */
6133 static int
6134existing_swapfile(buf_T *buf)
6135{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006136 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006137 {
6138 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6139 size_t len = STRLEN(fname);
6140
6141 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6142 }
6143 return FALSE;
6144}
6145
6146/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006147 * Search for a pattern in a list of files and populate the quickfix list with
6148 * the matches.
6149 */
6150 static int
6151vgr_process_files(
6152 win_T *wp,
6153 qf_info_T *qi,
6154 vgr_args_T *cmd_args,
6155 int *redraw_for_dummy,
6156 buf_T **first_match_buf,
6157 char_u **target_dir)
6158{
6159 int status = FAIL;
6160 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6161 time_t seconds = 0;
6162 char_u *fname;
6163 int fi;
6164 buf_T *buf;
6165 int duplicate_name = FALSE;
6166 int using_dummy;
6167 char_u *dirname_start = NULL;
6168 char_u *dirname_now = NULL;
6169 int found_match;
6170 aco_save_T aco;
6171
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006172 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6173 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006174 if (dirname_start == NULL || dirname_now == NULL)
6175 goto theend;
6176
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006177 // Remember the current directory, because a BufRead autocommand that does
6178 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006179 mch_dirname(dirname_start, MAXPATHL);
6180
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006181 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006182 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6183 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006184 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006185 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006186 if (time(NULL) > seconds)
6187 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006188 // Display the file name every second or so, show the user we are
6189 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006190 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006191 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006192 }
6193
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006194 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006195 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6196 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006197 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006198 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006199 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006200 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006201
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006202 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006203 }
6204 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006205 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006206 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006207
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006208 // Check whether the quickfix list is still valid. When loading a
6209 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006210 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006211 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006212
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006213 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006214
Bram Moolenaar81695252004-12-29 20:58:21 +00006215 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006216 {
6217 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006218 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006219 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006220 else
6221 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006222 // Try for a match in all lines of the buffer.
6223 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006224 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006225 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006226 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006227
Bram Moolenaar81695252004-12-29 20:58:21 +00006228 if (using_dummy)
6229 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006230 if (found_match && *first_match_buf == NULL)
6231 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006232 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006233 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006234 // Never keep a dummy buffer if there is another buffer
6235 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006236 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006237 buf = NULL;
6238 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006239 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006240 || buf->b_p_bh[0] == 'u' // "unload"
6241 || buf->b_p_bh[0] == 'w' // "wipe"
6242 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006243 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006244 // When no match was found we don't need to remember the
6245 // buffer, wipe it out. If there was a match and it
6246 // wasn't the first one or we won't jump there: only
6247 // unload the buffer.
6248 // Ignore 'hidden' here, because it may lead to having too
6249 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006250 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006251 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006252 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006253 buf = NULL;
6254 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006255 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006256 || (cmd_args->flags & VGR_NOJUMP)
6257 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006258 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006259 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006260 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006261 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006262 buf = NULL;
6263 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006264 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006265
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006266 if (buf != NULL)
6267 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006268 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006269 buf->b_flags &= ~BF_DUMMY;
6270
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006271 // If the buffer is still loaded we need to use the
6272 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006273 if (buf == *first_match_buf
6274 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006275 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006276 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006277
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006278 // The buffer is still loaded, the Filetype autocommands
6279 // need to be done now, in that buffer. And the modelines
6280 // need to be done (again). But not the window-local
6281 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006282 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006283#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006284 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6285 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006286#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006287 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006288 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006289 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006290 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006291 }
6292 }
6293
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006294 status = OK;
6295
6296theend:
6297 vim_free(dirname_now);
6298 vim_free(dirname_start);
6299 return status;
6300}
6301
6302/*
6303 * ":vimgrep {pattern} file(s)"
6304 * ":vimgrepadd {pattern} file(s)"
6305 * ":lvimgrep {pattern} file(s)"
6306 * ":lvimgrepadd {pattern} file(s)"
6307 */
6308 void
6309ex_vimgrep(exarg_T *eap)
6310{
6311 vgr_args_T args;
6312 qf_info_T *qi;
6313 qf_list_T *qfl;
6314 int_u save_qfid;
6315 win_T *wp = NULL;
6316 int redraw_for_dummy = FALSE;
6317 buf_T *first_match_buf = NULL;
6318 char_u *target_dir = NULL;
6319 char_u *au_name = NULL;
6320 int status;
6321
6322 au_name = vgr_get_auname(eap->cmdidx);
6323 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6324 curbuf->b_fname, TRUE, curbuf))
6325 {
6326#ifdef FEAT_EVAL
6327 if (aborting())
6328 return;
6329#endif
6330 }
6331
6332 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6333 if (qi == NULL)
6334 return;
6335
6336 if (vgr_process_args(eap, &args) == FAIL)
6337 goto theend;
6338
6339 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6340 && eap->cmdidx != CMD_vimgrepadd
6341 && eap->cmdidx != CMD_lvimgrepadd)
6342 || qf_stack_empty(qi))
6343 // make place for a new list
6344 qf_new_list(qi, args.qf_title);
6345
6346 incr_quickfix_busy();
6347
6348 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6349 &first_match_buf, &target_dir);
6350 if (status != OK)
6351 {
6352 FreeWild(args.fcount, args.fnames);
6353 decr_quickfix_busy();
6354 goto theend;
6355 }
6356
6357 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006358
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006359 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006360 qfl->qf_nonevalid = FALSE;
6361 qfl->qf_ptr = qfl->qf_start;
6362 qfl->qf_index = 1;
6363 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006364
Bram Moolenaar864293a2016-06-02 13:40:04 +02006365 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006366
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006367 // Remember the current quickfix list identifier, so that we can check for
6368 // autocommands changing the current quickfix list.
6369 save_qfid = qf_get_curlist(qi)->qf_id;
6370
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006371 if (au_name != NULL)
6372 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6373 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006374 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6375 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006376 if (!qflist_valid(wp, save_qfid)
6377 || qf_restore_list(qi, save_qfid) == FAIL)
6378 {
6379 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006380 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006381 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006382
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006383 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006384 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006385 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006386 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006387 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6388 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006389 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006390 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006391 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006392
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006393 decr_quickfix_busy();
6394
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006395 // If we loaded a dummy buffer into the current window, the autocommands
6396 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006397 if (redraw_for_dummy)
6398 {
6399#ifdef FEAT_FOLDING
6400 foldUpdateAll(curwin);
6401#else
6402 redraw_later(NOT_VALID);
6403#endif
6404 }
6405
Bram Moolenaar86b68352004-12-27 21:59:20 +00006406theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006407 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006408 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006409 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006410}
6411
6412/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006413 * Restore current working directory to "dirname_start" if they differ, taking
6414 * into account whether it is set locally or globally.
6415 */
6416 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006417restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006418{
6419 char_u *dirname_now = alloc(MAXPATHL);
6420
6421 if (NULL != dirname_now)
6422 {
6423 mch_dirname(dirname_now, MAXPATHL);
6424 if (STRCMP(dirname_start, dirname_now) != 0)
6425 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006426 // If the directory has changed, change it back by building up an
6427 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006428 exarg_T ea;
6429
Bram Moolenaara80faa82020-04-12 19:37:17 +02006430 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006431 ea.arg = dirname_start;
6432 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6433 ex_cd(&ea);
6434 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006435 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006436 }
6437}
6438
6439/*
6440 * Load file "fname" into a dummy buffer and return the buffer pointer,
6441 * placing the directory resulting from the buffer load into the
6442 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6443 * prior to calling this function. Restores directory to "dirname_start" prior
6444 * to returning, if autocmds or the 'autochdir' option have changed it.
6445 *
6446 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6447 * or wipe_dummy_buffer() later!
6448 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006449 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006450 */
6451 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006452load_dummy_buffer(
6453 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006454 char_u *dirname_start, // in: old directory
6455 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006456{
6457 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006458 bufref_T newbufref;
6459 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006460 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006461 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006462 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006463
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006464 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006465 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6466 if (newbuf == NULL)
6467 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006468 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006469
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006470 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006471 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6472
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006473 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006474 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006475 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006476 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006477 ++newbuf->b_locked;
6478
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006479 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006480 aucmd_prepbuf(&aco, newbuf);
6481
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006482 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006483 (void)setfname(curbuf, fname, NULL, FALSE);
6484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006485 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006486 check_need_swap(TRUE);
6487
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006488 // Remove the "dummy" flag, otherwise autocommands may not
6489 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006490 curbuf->b_flags &= ~BF_DUMMY;
6491
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006492 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006493 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006494 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006495 NULL, READ_NEW | READ_DUMMY);
6496 --newbuf->b_locked;
6497 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006498 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006499 && !(curbuf->b_flags & BF_NEW))
6500 {
6501 failed = FALSE;
6502 if (curbuf != newbuf)
6503 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006504 // Bloody autocommands changed the buffer! Can happen when
6505 // using netrw and editing a remote file. Use the current
6506 // buffer instead, delete the dummy one after restoring the
6507 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006508 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006509 newbuf = curbuf;
6510 }
6511 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006513 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006514 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006515 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6516 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006518 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6519 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006520 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006521 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006522
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006523 // When autocommands/'autochdir' option changed directory: go back.
6524 // Let the caller know what the resulting dir was first, in case it is
6525 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006526 mch_dirname(resulting_dir, MAXPATHL);
6527 restore_start_dir(dirname_start);
6528
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006529 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006530 return NULL;
6531 if (failed)
6532 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006533 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006534 return NULL;
6535 }
6536 return newbuf;
6537}
6538
6539/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006540 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6541 * directory to "dirname_start" prior to returning, if autocmds or the
6542 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006543 */
6544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006545wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006546{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006547 // If any autocommand opened a window on the dummy buffer, close that
6548 // window. If we can't close them all then give up.
6549 while (buf->b_nwindows > 0)
6550 {
6551 int did_one = FALSE;
6552 win_T *wp;
6553
6554 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006555 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006556 if (wp->w_buffer == buf)
6557 {
6558 if (win_close(wp, FALSE) == OK)
6559 did_one = TRUE;
6560 break;
6561 }
6562 if (!did_one)
6563 return;
6564 }
6565
6566 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006567 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006568#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006569 cleanup_T cs;
6570
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006571 // Reset the error/interrupt/exception state here so that aborting()
6572 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6573 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006574 enter_cleanup(&cs);
6575#endif
6576
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006577 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006578
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006579#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006580 // Restore the error/interrupt/exception state if not discarded by a
6581 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006582 leave_cleanup(&cs);
6583#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006584 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006585 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006586 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006587}
6588
6589/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006590 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6591 * directory to "dirname_start" prior to returning, if autocmds or the
6592 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006593 */
6594 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006595unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006596{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006597 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006598 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006599 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006600
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006601 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006602 restore_start_dir(dirname_start);
6603 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006604}
6605
Bram Moolenaar05159a02005-02-26 23:04:13 +00006606#if defined(FEAT_EVAL) || defined(PROTO)
6607/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006608 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006609 * to 'list'. Returns OK on success.
6610 */
6611 static int
6612get_qfline_items(qfline_T *qfp, list_T *list)
6613{
6614 int bufnum;
6615 dict_T *dict;
6616 char_u buf[2];
6617
6618 // Handle entries with a non-existing buffer number.
6619 bufnum = qfp->qf_fnum;
6620 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6621 bufnum = 0;
6622
6623 if ((dict = dict_alloc()) == NULL)
6624 return FAIL;
6625 if (list_append_dict(list, dict) == FAIL)
6626 return FAIL;
6627
6628 buf[0] = qfp->qf_type;
6629 buf[1] = NUL;
6630 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006631 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6632 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6633 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6634 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6635 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6636 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006637 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6638 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6639 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6640 || dict_add_string(dict, "type", buf) == FAIL
6641 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6642 return FAIL;
6643
6644 return OK;
6645}
6646
6647/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006648 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006649 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006650 * If eidx is not 0, then return only the specified entry. Otherwise return
6651 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006652 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006653 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006654get_errorlist(
6655 qf_info_T *qi_arg,
6656 win_T *wp,
6657 int qf_idx,
6658 int eidx,
6659 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006660{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006661 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006662 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006663 qfline_T *qfp;
6664 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006665
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006666 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006667 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006668 qi = &ql_info;
6669 if (wp != NULL)
6670 {
6671 qi = GET_LOC_LIST(wp);
6672 if (qi == NULL)
6673 return FAIL;
6674 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006675 }
6676
Bram Moolenaar858ba062020-05-31 23:11:59 +02006677 if (eidx < 0)
6678 return OK;
6679
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006680 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006681 qf_idx = qi->qf_curlist;
6682
Bram Moolenaar0398e002019-03-21 21:12:49 +01006683 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006684 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006685
Bram Moolenaar0398e002019-03-21 21:12:49 +01006686 qfl = qf_get_list(qi, qf_idx);
6687 if (qf_list_empty(qfl))
6688 return FAIL;
6689
Bram Moolenaara16123a2019-03-28 20:31:07 +01006690 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006691 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006692 if (eidx > 0)
6693 {
6694 if (eidx == i)
6695 return get_qfline_items(qfp, list);
6696 }
6697 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006698 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006699 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006700
Bram Moolenaar05159a02005-02-26 23:04:13 +00006701 return OK;
6702}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006703
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006704// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006705enum {
6706 QF_GETLIST_NONE = 0x0,
6707 QF_GETLIST_TITLE = 0x1,
6708 QF_GETLIST_ITEMS = 0x2,
6709 QF_GETLIST_NR = 0x4,
6710 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006711 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006712 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006713 QF_GETLIST_IDX = 0x40,
6714 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006715 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006716 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006717 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006718 QF_GETLIST_QFTF = 0x800,
6719 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006720};
6721
6722/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006723 * Parse text from 'di' and return the quickfix list items.
6724 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006725 */
6726 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006727qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006728{
6729 int status = FAIL;
6730 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006731 char_u *errorformat = p_efm;
6732 dictitem_T *efm_di;
6733 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006735 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006736 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006737 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006738 // If errorformat is supplied then use it, otherwise use the 'efm'
6739 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006740 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6741 {
6742 if (efm_di->di_tv.v_type != VAR_STRING ||
6743 efm_di->di_tv.vval.v_string == NULL)
6744 return FAIL;
6745 errorformat = efm_di->di_tv.vval.v_string;
6746 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006747
Bram Moolenaar36538222017-09-02 19:51:44 +02006748 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006749 if (l == NULL)
6750 return FAIL;
6751
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006752 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006753 if (qi != NULL)
6754 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006755 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006756 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6757 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006758 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006759 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006760 }
6761 free(qi);
6762 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006763 dict_add_list(retdict, "items", l);
6764 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006765 }
6766
6767 return status;
6768}
6769
6770/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006771 * Return the quickfix/location list window identifier in the current tabpage.
6772 */
6773 static int
6774qf_winid(qf_info_T *qi)
6775{
6776 win_T *win;
6777
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006778 // The quickfix window can be opened even if the quickfix list is not set
6779 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006780 if (qi == NULL)
6781 return 0;
6782 win = qf_find_win(qi);
6783 if (win != NULL)
6784 return win->w_id;
6785 return 0;
6786}
6787
6788/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006789 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006790 * window. If there is no buffer associated with the list or the buffer is
6791 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006792 */
6793 static int
6794qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6795{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006796 int bufnum = 0;
6797
6798 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6799 bufnum = qi->qf_bufnr;
6800
6801 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006802}
6803
6804/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006805 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006806 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006807 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006808qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006809{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006810 int flags = QF_GETLIST_NONE;
6811
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006812 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006813 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006814 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006815 if (!loclist)
6816 // File window ID is applicable only to location list windows
6817 flags &= ~ QF_GETLIST_FILEWINID;
6818 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006819
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006820 if (dict_find(what, (char_u *)"title", -1) != NULL)
6821 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006822
Bram Moolenaara6d48492017-12-12 22:45:31 +01006823 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6824 flags |= QF_GETLIST_NR;
6825
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006826 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6827 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006828
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006829 if (dict_find(what, (char_u *)"context", -1) != NULL)
6830 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006831
Bram Moolenaara6d48492017-12-12 22:45:31 +01006832 if (dict_find(what, (char_u *)"id", -1) != NULL)
6833 flags |= QF_GETLIST_ID;
6834
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006835 if (dict_find(what, (char_u *)"items", -1) != NULL)
6836 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006837
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006838 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6839 flags |= QF_GETLIST_IDX;
6840
6841 if (dict_find(what, (char_u *)"size", -1) != NULL)
6842 flags |= QF_GETLIST_SIZE;
6843
Bram Moolenaarb254af32017-12-18 19:48:58 +01006844 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6845 flags |= QF_GETLIST_TICK;
6846
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006847 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6848 flags |= QF_GETLIST_FILEWINID;
6849
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006850 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6851 flags |= QF_GETLIST_QFBUFNR;
6852
Bram Moolenaard43906d2020-07-20 21:31:32 +02006853 if (dict_find(what, (char_u *)"quickfixtextfunc", -1) != NULL)
6854 flags |= QF_GETLIST_QFTF;
6855
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006856 return flags;
6857}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006858
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006859/*
6860 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6861 * If 'nr' and 'id' are not present in 'what' then return the current
6862 * quickfix list index.
6863 * If 'nr' is zero then return the current quickfix list index.
6864 * If 'nr' is '$' then return the last quickfix list index.
6865 * If 'id' is present then return the index of the quickfix list with that id.
6866 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6867 * Return -1, if quickfix list is not present or if the stack is empty.
6868 */
6869 static int
6870qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6871{
6872 int qf_idx;
6873 dictitem_T *di;
6874
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006875 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006876 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6877 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006878 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006879 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006880 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006881 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006882 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006883 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006884 qf_idx = di->di_tv.vval.v_number - 1;
6885 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006886 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006887 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006888 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006889 else if (di->di_tv.v_type == VAR_STRING
6890 && di->di_tv.vval.v_string != NULL
6891 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006892 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006893 qf_idx = qi->qf_listcount - 1;
6894 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006895 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006896 }
6897
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006898 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6899 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006900 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006901 if (di->di_tv.v_type == VAR_NUMBER)
6902 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006903 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006904 if (di->di_tv.vval.v_number != 0)
6905 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6906 }
6907 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006908 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006909 }
6910
6911 return qf_idx;
6912}
6913
6914/*
6915 * Return default values for quickfix list properties in retdict.
6916 */
6917 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006918qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006919{
6920 int status = OK;
6921
6922 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006923 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006924 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6925 {
6926 list_T *l = list_alloc();
6927 if (l != NULL)
6928 status = dict_add_list(retdict, "items", l);
6929 else
6930 status = FAIL;
6931 }
6932 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006933 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006934 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006935 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006936 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006937 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006938 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006939 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006940 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006941 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006942 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006943 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006944 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006945 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006946 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006947 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006948 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6949 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02006950 if ((status == OK) && (flags & QF_GETLIST_QFTF))
6951 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006952
6953 return status;
6954}
6955
6956/*
6957 * Return the quickfix list title as 'title' in retdict
6958 */
6959 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006960qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006961{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006962 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006963}
6964
6965/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006966 * Returns the identifier of the window used to display files from a location
6967 * list. If there is no associated window, then returns 0. Useful only when
6968 * called from a location list window.
6969 */
6970 static int
6971qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6972{
6973 int winid = 0;
6974
6975 if (wp != NULL && IS_LL_WINDOW(wp))
6976 {
6977 win_T *ll_wp = qf_find_win_with_loclist(qi);
6978 if (ll_wp != NULL)
6979 winid = ll_wp->w_id;
6980 }
6981
6982 return dict_add_number(retdict, "filewinid", winid);
6983}
6984
6985/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006986 * Return the quickfix list items/entries as 'items' in retdict.
6987 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006988 */
6989 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006990qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006991{
6992 int status = OK;
6993 list_T *l = list_alloc();
6994 if (l != NULL)
6995 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006996 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006997 dict_add_list(retdict, "items", l);
6998 }
6999 else
7000 status = FAIL;
7001
7002 return status;
7003}
7004
7005/*
7006 * Return the quickfix list context (if any) as 'context' in retdict.
7007 */
7008 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007009qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007010{
7011 int status;
7012 dictitem_T *di;
7013
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007014 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007015 {
7016 di = dictitem_alloc((char_u *)"context");
7017 if (di != NULL)
7018 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007019 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007020 status = dict_add(retdict, di);
7021 if (status == FAIL)
7022 dictitem_free(di);
7023 }
7024 else
7025 status = FAIL;
7026 }
7027 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007028 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007029
7030 return status;
7031}
7032
7033/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007034 * Return the current quickfix list index as 'idx' in retdict.
7035 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007036 */
7037 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007038qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007039{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007040 if (eidx == 0)
7041 {
7042 eidx = qfl->qf_index;
7043 if (qf_list_empty(qfl))
7044 // For empty lists, current index is set to 0
7045 eidx = 0;
7046 }
7047 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007048}
7049
7050/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007051 * Return the 'quickfixtextfunc' function of a quickfix/location list
7052 */
7053 static int
7054qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7055{
7056 int status;
7057
7058 if (qfl->qftf_cb.cb_name != NULL)
7059 {
7060 typval_T tv;
7061
7062 put_callback(&qfl->qftf_cb, &tv);
7063 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7064 clear_tv(&tv);
7065 }
7066 else
7067 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7068
7069 return status;
7070}
7071
7072/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007073 * Return quickfix/location list details (title) as a
7074 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7075 * then current list is used. Otherwise the specified list is used.
7076 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007077 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007078qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7079{
7080 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007081 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007082 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007083 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007084 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007085 dictitem_T *di;
7086 int flags = QF_GETLIST_NONE;
7087
7088 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7089 return qf_get_list_from_lines(what, di, retdict);
7090
7091 if (wp != NULL)
7092 qi = GET_LOC_LIST(wp);
7093
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007094 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007095
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007096 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007097 qf_idx = qf_getprop_qfidx(qi, what);
7098
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007099 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007100 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007101 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007102
Bram Moolenaar0398e002019-03-21 21:12:49 +01007103 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007104
Bram Moolenaar858ba062020-05-31 23:11:59 +02007105 // If an entry index is specified, use that
7106 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7107 {
7108 if (di->di_tv.v_type != VAR_NUMBER)
7109 return FAIL;
7110 eidx = di->di_tv.vval.v_number;
7111 }
7112
Bram Moolenaard823fa92016-08-12 16:29:27 +02007113 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007114 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007115 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007116 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007117 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007118 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007119 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007120 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007121 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007122 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007123 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007124 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007125 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007126 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007127 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007128 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007129 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007130 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007131 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7132 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007133 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7134 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007135 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7136 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007137
Bram Moolenaard823fa92016-08-12 16:29:27 +02007138 return status;
7139}
7140
7141/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007142 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007143 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7144 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007145 */
7146 static int
7147qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007148 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007149 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007150 int first_entry,
7151 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007152{
7153 static int did_bufnr_emsg;
7154 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007155 int bufnum, valid, status, col, end_col, vcol, nr;
7156 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007157
7158 if (first_entry)
7159 did_bufnr_emsg = FALSE;
7160
Bram Moolenaar8f667172018-12-14 15:38:31 +01007161 filename = dict_get_string(d, (char_u *)"filename", TRUE);
7162 module = dict_get_string(d, (char_u *)"module", TRUE);
7163 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
7164 lnum = (int)dict_get_number(d, (char_u *)"lnum");
thinca6864efa2021-06-19 20:45:20 +02007165 end_lnum = (int)dict_get_number(d, (char_u *)"end_lnum");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007166 col = (int)dict_get_number(d, (char_u *)"col");
thinca6864efa2021-06-19 20:45:20 +02007167 end_col = (int)dict_get_number(d, (char_u *)"end_col");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007168 vcol = (int)dict_get_number(d, (char_u *)"vcol");
7169 nr = (int)dict_get_number(d, (char_u *)"nr");
7170 type = dict_get_string(d, (char_u *)"type", TRUE);
7171 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
7172 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007173 if (text == NULL)
7174 text = vim_strsave((char_u *)"");
7175
7176 valid = TRUE;
7177 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7178 valid = FALSE;
7179
7180 // Mark entries with non-existing buffer number as not valid. Give the
7181 // error message only once.
7182 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7183 {
7184 if (!did_bufnr_emsg)
7185 {
7186 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007187 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007188 }
7189 valid = FALSE;
7190 bufnum = 0;
7191 }
7192
7193 // If the 'valid' field is present it overrules the detected value.
7194 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar401f0c02020-09-05 22:37:39 +02007195 valid = (int)dict_get_bool(d, (char_u *)"valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007196
Bram Moolenaar0398e002019-03-21 21:12:49 +01007197 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007198 NULL, // dir
7199 filename,
7200 module,
7201 bufnum,
7202 text,
7203 lnum,
thinca6864efa2021-06-19 20:45:20 +02007204 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007205 col,
thinca6864efa2021-06-19 20:45:20 +02007206 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007207 vcol, // vis_col
7208 pattern, // search pattern
7209 nr,
7210 type == NULL ? NUL : *type,
7211 valid);
7212
7213 vim_free(filename);
7214 vim_free(module);
7215 vim_free(pattern);
7216 vim_free(text);
7217 vim_free(type);
7218
Bram Moolenaar9752c722018-12-22 16:49:34 +01007219 if (valid)
7220 *valid_entry = TRUE;
7221
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007222 return status;
7223}
7224
7225/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007226 * Add list of entries to quickfix/location list. Each list entry is
7227 * a dictionary with item information.
7228 */
7229 static int
7230qf_add_entries(
7231 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007232 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007233 list_T *list,
7234 char_u *title,
7235 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007236{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007237 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007238 listitem_T *li;
7239 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007240 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007241 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007242 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007243
Bram Moolenaara3921f42017-06-04 15:30:34 +02007244 if (action == ' ' || qf_idx == qi->qf_listcount)
7245 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007246 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007247 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007248 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007249 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007250 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007251 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007252 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007253 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007254 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007255 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007256 qf_free_items(qfl);
7257 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007258 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007259
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007260 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007261 {
7262 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007263 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007264
7265 d = li->li_tv.vval.v_dict;
7266 if (d == NULL)
7267 continue;
7268
Bram Moolenaar0398e002019-03-21 21:12:49 +01007269 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007270 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007271 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007272 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007273 }
7274
Bram Moolenaar9752c722018-12-22 16:49:34 +01007275 // Check if any valid error entries are added to the list.
7276 if (valid_entry)
7277 qfl->qf_nonevalid = FALSE;
7278 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007279 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007280 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007281
7282 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007283 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007284 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007285
7286 // Update the current error index if not appending to the list or if the
7287 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007288 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007289 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007290
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007291 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007292 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007293
7294 return retval;
7295}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007296
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007297/*
7298 * Get the quickfix list index from 'nr' or 'id'
7299 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007300 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007301qf_setprop_get_qfidx(
7302 qf_info_T *qi,
7303 dict_T *what,
7304 int action,
7305 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007306{
7307 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007308 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007309
Bram Moolenaard823fa92016-08-12 16:29:27 +02007310 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7311 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007312 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007313 if (di->di_tv.v_type == VAR_NUMBER)
7314 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007315 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007316 if (di->di_tv.vval.v_number != 0)
7317 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007318
Bram Moolenaar55b69262017-08-13 13:42:01 +02007319 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7320 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007321 // When creating a new list, accept qf_idx pointing to the next
7322 // non-available list and add the new list at the end of the
7323 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007324 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007325 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007326 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007327 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007328 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007329 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007330 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007331 }
7332 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007333 && di->di_tv.vval.v_string != NULL
7334 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007335 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007336 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007337 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007338 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007339 qf_idx = 0;
7340 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007341 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007342 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007343 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007344 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007345 }
7346
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007347 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007348 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007349 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007350 if (di->di_tv.v_type != VAR_NUMBER)
7351 return INVALID_QFIDX;
7352
7353 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007354 }
7355
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007356 return qf_idx;
7357}
7358
7359/*
7360 * Set the quickfix list title.
7361 */
7362 static int
7363qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7364{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007365 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007366
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007367 if (di->di_tv.v_type != VAR_STRING)
7368 return FAIL;
7369
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007370 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007371 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007372 if (qf_idx == qi->qf_curlist)
7373 qf_update_win_titlevar(qi);
7374
7375 return OK;
7376}
7377
7378/*
7379 * Set quickfix list items/entries.
7380 */
7381 static int
7382qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7383{
7384 int retval = FAIL;
7385 char_u *title_save;
7386
7387 if (di->di_tv.v_type != VAR_LIST)
7388 return FAIL;
7389
7390 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7391 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7392 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007393 vim_free(title_save);
7394
7395 return retval;
7396}
7397
7398/*
7399 * Set quickfix list items/entries from a list of lines.
7400 */
7401 static int
7402qf_setprop_items_from_lines(
7403 qf_info_T *qi,
7404 int qf_idx,
7405 dict_T *what,
7406 dictitem_T *di,
7407 int action)
7408{
7409 char_u *errorformat = p_efm;
7410 dictitem_T *efm_di;
7411 int retval = FAIL;
7412
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007413 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007414 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7415 {
7416 if (efm_di->di_tv.v_type != VAR_STRING ||
7417 efm_di->di_tv.vval.v_string == NULL)
7418 return FAIL;
7419 errorformat = efm_di->di_tv.vval.v_string;
7420 }
7421
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007422 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007423 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7424 return FAIL;
7425
7426 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007427 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007428 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007429 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007430 retval = OK;
7431
7432 return retval;
7433}
7434
7435/*
7436 * Set quickfix list context.
7437 */
7438 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007439qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007440{
7441 typval_T *ctx;
7442
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007443 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007444 ctx = alloc_tv();
7445 if (ctx != NULL)
7446 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007447 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007448
7449 return OK;
7450}
7451
7452/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007453 * Set the current index in the specified quickfix list
7454 */
7455 static int
7456qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7457{
7458 int denote = FALSE;
7459 int newidx;
7460 int old_qfidx;
7461 qfline_T *qf_ptr;
7462
7463 // If the specified index is '$', then use the last entry
7464 if (di->di_tv.v_type == VAR_STRING
7465 && di->di_tv.vval.v_string != NULL
7466 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7467 newidx = qfl->qf_count;
7468 else
7469 {
7470 // Otherwise use the specified index
7471 newidx = tv_get_number_chk(&di->di_tv, &denote);
7472 if (denote)
7473 return FAIL;
7474 }
7475
7476 if (newidx < 1) // sanity check
7477 return FAIL;
7478 if (newidx > qfl->qf_count)
7479 newidx = qfl->qf_count;
7480
7481 old_qfidx = qfl->qf_index;
7482 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7483 if (qf_ptr == NULL)
7484 return FAIL;
7485 qfl->qf_ptr = qf_ptr;
7486 qfl->qf_index = newidx;
7487
7488 // If the current list is modified and it is displayed in the quickfix
7489 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007490 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007491 qf_win_pos_update(qi, old_qfidx);
7492
7493 return OK;
7494}
7495
7496/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007497 * Set the current index in the specified quickfix list
7498 */
7499 static int
7500qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7501{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007502 callback_T cb;
7503
7504 free_callback(&qfl->qftf_cb);
7505 cb = get_callback(&di->di_tv);
7506 if (cb.cb_name != NULL && *cb.cb_name != NUL)
7507 set_callback(&qfl->qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007508
7509 return OK;
7510}
7511
7512/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007513 * Set quickfix/location list properties (title, items, context).
7514 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007515 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007516 */
7517 static int
7518qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7519{
7520 dictitem_T *di;
7521 int retval = FAIL;
7522 int qf_idx;
7523 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007524 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007525
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007526 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007527 newlist = TRUE;
7528
7529 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007530 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007531 return FAIL;
7532
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007533 if (newlist)
7534 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007535 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007536 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007537 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007538 }
7539
Bram Moolenaar0398e002019-03-21 21:12:49 +01007540 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007541 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007542 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007543 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007544 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007545 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007546 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007547 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007548 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007549 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7550 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007551 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7552 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007553
Bram Moolenaar287153c2020-11-29 14:20:27 +01007554 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007555 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007556 if (newlist)
7557 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007558
Bram Moolenaard823fa92016-08-12 16:29:27 +02007559 return retval;
7560}
7561
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007562/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007563 * Free the entire quickfix/location list stack.
7564 * If the quickfix/location list window is open, then clear it.
7565 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007566 static void
7567qf_free_stack(win_T *wp, qf_info_T *qi)
7568{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007569 win_T *qfwin = qf_find_win(qi);
7570 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007571
7572 if (qfwin != NULL)
7573 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007574 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007575 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007576 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007577 qf_update_buffer(qi, NULL);
7578 }
7579
7580 if (wp != NULL && IS_LL_WINDOW(wp))
7581 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007582 // If in the location list window, then use the non-location list
7583 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007584 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007585 if (llwin != NULL)
7586 wp = llwin;
7587 }
7588
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007589 qf_free_all(wp);
7590 if (wp == NULL)
7591 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007592 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007593 qi->qf_curlist = 0;
7594 qi->qf_listcount = 0;
7595 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007596 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007597 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007598 // If the location list window is open, then create a new empty
7599 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007600 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007601
Bram Moolenaar95946f12019-03-31 15:31:59 +02007602 if (new_ll != NULL)
7603 {
7604 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007605
Bram Moolenaar95946f12019-03-31 15:31:59 +02007606 // first free the list reference in the location list window
7607 ll_free_all(&qfwin->w_llist_ref);
7608
7609 qfwin->w_llist_ref = new_ll;
7610 if (wp != qfwin)
7611 win_set_loclist(wp, new_ll);
7612 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007613 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007614}
7615
Bram Moolenaard823fa92016-08-12 16:29:27 +02007616/*
7617 * Populate the quickfix list with the items supplied in the list
7618 * of dictionaries. "title" will be copied to w:quickfix_title.
7619 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007620 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007621 */
7622 int
7623set_errorlist(
7624 win_T *wp,
7625 list_T *list,
7626 int action,
7627 char_u *title,
7628 dict_T *what)
7629{
7630 qf_info_T *qi = &ql_info;
7631 int retval = OK;
7632
7633 if (wp != NULL)
7634 {
7635 qi = ll_get_or_alloc_list(wp);
7636 if (qi == NULL)
7637 return FAIL;
7638 }
7639
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007640 if (action == 'f')
7641 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007642 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007643 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007644 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007645 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007646
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007647 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007648 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007649 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007650 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007651 _("cannot have both a list and a \"what\" argument"));
7652 return FAIL;
7653 }
7654
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007655 incr_quickfix_busy();
7656
7657 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007658 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007659 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007660 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007661 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007662 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007663 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007664 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007665
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007666 decr_quickfix_busy();
7667
Bram Moolenaard823fa92016-08-12 16:29:27 +02007668 return retval;
7669}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007670
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007671/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007672 * Mark the quickfix context and callback function as in use for all the lists
7673 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007674 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007675 static int
7676mark_quickfix_ctx(qf_info_T *qi, int copyID)
7677{
7678 int i;
7679 int abort = FALSE;
7680 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007681 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007682
7683 for (i = 0; i < LISTCOUNT && !abort; ++i)
7684 {
7685 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007686 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7687 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007688 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7689
7690 cb = &qi->qf_lists[i].qftf_cb;
7691 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007692 }
7693
7694 return abort;
7695}
7696
7697/*
7698 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007699 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007700 */
7701 int
7702set_ref_in_quickfix(int copyID)
7703{
7704 int abort = FALSE;
7705 tabpage_T *tp;
7706 win_T *win;
7707
7708 abort = mark_quickfix_ctx(&ql_info, copyID);
7709 if (abort)
7710 return abort;
7711
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007712 abort = set_ref_in_callback(&qftf_cb, copyID);
7713 if (abort)
7714 return abort;
7715
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007716 FOR_ALL_TAB_WINDOWS(tp, win)
7717 {
7718 if (win->w_llist != NULL)
7719 {
7720 abort = mark_quickfix_ctx(win->w_llist, copyID);
7721 if (abort)
7722 return abort;
7723 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007724 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7725 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007726 // In a location list window and none of the other windows is
7727 // referring to this location list. Mark the location list
7728 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007729 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7730 if (abort)
7731 return abort;
7732 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007733 }
7734
7735 return abort;
7736}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007737#endif
7738
Bram Moolenaar81695252004-12-29 20:58:21 +00007739/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007740 * Return the autocmd name for the :cbuffer Ex commands
7741 */
7742 static char_u *
7743cbuffer_get_auname(cmdidx_T cmdidx)
7744{
7745 switch (cmdidx)
7746 {
7747 case CMD_cbuffer: return (char_u *)"cbuffer";
7748 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7749 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7750 case CMD_lbuffer: return (char_u *)"lbuffer";
7751 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7752 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7753 default: return NULL;
7754 }
7755}
7756
7757/*
7758 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7759 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7760 */
7761 static int
7762cbuffer_process_args(
7763 exarg_T *eap,
7764 buf_T **bufp,
7765 linenr_T *line1,
7766 linenr_T *line2)
7767{
7768 buf_T *buf = NULL;
7769
7770 if (*eap->arg == NUL)
7771 buf = curbuf;
7772 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7773 buf = buflist_findnr(atoi((char *)eap->arg));
7774
7775 if (buf == NULL)
7776 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007777 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007778 return FAIL;
7779 }
7780
7781 if (buf->b_ml.ml_mfp == NULL)
7782 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007783 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007784 return FAIL;
7785 }
7786
7787 if (eap->addr_count == 0)
7788 {
7789 eap->line1 = 1;
7790 eap->line2 = buf->b_ml.ml_line_count;
7791 }
7792
7793 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7794 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7795 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007796 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007797 return FAIL;
7798 }
7799
7800 *line1 = eap->line1;
7801 *line2 = eap->line2;
7802 *bufp = buf;
7803
7804 return OK;
7805}
7806
7807/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007808 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007809 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007810 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007811 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007812 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007813 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007814 */
7815 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007816ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007817{
7818 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007819 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007820 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007821 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007822 int_u save_qfid;
7823 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007824 char_u *qf_title;
7825 linenr_T line1;
7826 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007827
Bram Moolenaara16123a2019-03-28 20:31:07 +01007828 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007829 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007830 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007831 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007832#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007833 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007834 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007835#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007836 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007837
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007838 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007839 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7840 if (qi == NULL)
7841 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007842
Bram Moolenaara16123a2019-03-28 20:31:07 +01007843 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7844 return;
7845
7846 qf_title = qf_cmdtitle(*eap->cmdlinep);
7847
7848 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007849 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007850 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7851 (char *)qf_title, (char *)buf->b_sfname);
7852 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007853 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007854
7855 incr_quickfix_busy();
7856
7857 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7858 (eap->cmdidx != CMD_caddbuffer
7859 && eap->cmdidx != CMD_laddbuffer),
7860 line1, line2,
7861 qf_title, NULL);
7862 if (qf_stack_empty(qi))
7863 {
7864 decr_quickfix_busy();
7865 return;
7866 }
7867 if (res >= 0)
7868 qf_list_changed(qf_get_curlist(qi));
7869
7870 // Remember the current quickfix list identifier, so that we can
7871 // check for autocommands changing the current quickfix list.
7872 save_qfid = qf_get_curlist(qi)->qf_id;
7873 if (au_name != NULL)
7874 {
7875 buf_T *curbuf_old = curbuf;
7876
7877 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7878 TRUE, curbuf);
7879 if (curbuf != curbuf_old)
7880 // Autocommands changed buffer, don't jump now, "qi" may
7881 // be invalid.
7882 res = 0;
7883 }
7884 // Jump to the first error for a new list and if autocmds didn't
7885 // free the list.
7886 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7887 eap->cmdidx == CMD_lbuffer)
7888 && qflist_valid(wp, save_qfid))
7889 // display the first error
7890 qf_jump_first(qi, save_qfid, eap->forceit);
7891
7892 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007893}
7894
Bram Moolenaar1e015462005-09-25 22:16:38 +00007895#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007896/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007897 * Return the autocmd name for the :cexpr Ex commands.
7898 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007899 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007900cexpr_get_auname(cmdidx_T cmdidx)
7901{
7902 switch (cmdidx)
7903 {
7904 case CMD_cexpr: return (char_u *)"cexpr";
7905 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7906 case CMD_caddexpr: return (char_u *)"caddexpr";
7907 case CMD_lexpr: return (char_u *)"lexpr";
7908 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7909 case CMD_laddexpr: return (char_u *)"laddexpr";
7910 default: return NULL;
7911 }
7912}
7913
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007914 int
7915trigger_cexpr_autocmd(int cmdidx)
7916{
7917 char_u *au_name = cexpr_get_auname(cmdidx);
7918
7919 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7920 curbuf->b_fname, TRUE, curbuf))
7921 {
7922 if (aborting())
7923 return FAIL;
7924 }
7925 return OK;
7926}
7927
7928 int
7929cexpr_core(exarg_T *eap, typval_T *tv)
7930{
7931 qf_info_T *qi;
7932 win_T *wp = NULL;
7933
7934 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7935 if (qi == NULL)
7936 return FAIL;
7937
7938 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7939 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7940 {
7941 int res;
7942 int_u save_qfid;
7943 char_u *au_name = cexpr_get_auname(eap->cmdidx);
7944
7945 incr_quickfix_busy();
7946 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
7947 (eap->cmdidx != CMD_caddexpr
7948 && eap->cmdidx != CMD_laddexpr),
7949 (linenr_T)0, (linenr_T)0,
7950 qf_cmdtitle(*eap->cmdlinep), NULL);
7951 if (qf_stack_empty(qi))
7952 {
7953 decr_quickfix_busy();
7954 return FAIL;
7955 }
7956 if (res >= 0)
7957 qf_list_changed(qf_get_curlist(qi));
7958
7959 // Remember the current quickfix list identifier, so that we can
7960 // check for autocommands changing the current quickfix list.
7961 save_qfid = qf_get_curlist(qi)->qf_id;
7962 if (au_name != NULL)
7963 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7964 curbuf->b_fname, TRUE, curbuf);
7965
7966 // Jump to the first error for a new list and if autocmds didn't
7967 // free the list.
7968 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
7969 && qflist_valid(wp, save_qfid))
7970 // display the first error
7971 qf_jump_first(qi, save_qfid, eap->forceit);
7972 decr_quickfix_busy();
7973 return OK;
7974 }
7975
Bram Moolenaar677658a2022-01-05 16:09:06 +00007976 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007977 return FAIL;
7978}
7979
Bram Moolenaara16123a2019-03-28 20:31:07 +01007980/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007981 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7982 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007983 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007984 */
7985 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007986ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007987{
7988 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007989
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007990 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007991 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007992
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007993 // Evaluate the expression. When the result is a string or a list we can
7994 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007995 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007996 if (tv != NULL)
7997 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007998 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007999 free_tv(tv);
8000 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008001}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008002#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008003
8004/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008005 * Get the location list for ":lhelpgrep"
8006 */
8007 static qf_info_T *
8008hgr_get_ll(int *new_ll)
8009{
8010 win_T *wp;
8011 qf_info_T *qi;
8012
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008013 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008014 if (bt_help(curwin->w_buffer))
8015 wp = curwin;
8016 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008017 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008018 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008019
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008020 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008021 qi = NULL;
8022 else
8023 qi = wp->w_llist;
8024
8025 if (qi == NULL)
8026 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008027 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008028 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008029 return NULL;
8030 *new_ll = TRUE;
8031 }
8032
8033 return qi;
8034}
8035
8036/*
8037 * Search for a pattern in a help file.
8038 */
8039 static void
8040hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008041 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008042 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008043 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008044 regmatch_T *p_regmatch)
8045{
8046 FILE *fd;
8047 long lnum;
8048
8049 fd = mch_fopen((char *)fname, "r");
8050 if (fd == NULL)
8051 return;
8052
8053 lnum = 1;
8054 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8055 {
8056 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008057
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008058 // Convert a line if 'encoding' is not utf-8 and
8059 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008060 if (p_vc->vc_type != CONV_NONE
8061 && has_non_ascii(IObuff))
8062 {
8063 line = string_convert(p_vc, IObuff, NULL);
8064 if (line == NULL)
8065 line = IObuff;
8066 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008067
8068 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8069 {
8070 int l = (int)STRLEN(line);
8071
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008072 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008073 while (l > 0 && line[l - 1] <= ' ')
8074 line[--l] = NUL;
8075
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008076 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008077 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008078 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008079 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008080 0,
8081 line,
8082 lnum,
thinca6864efa2021-06-19 20:45:20 +02008083 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008084 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008085 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008086 (int)(p_regmatch->endp[0] - line)
8087 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008088 FALSE, // vis_col
8089 NULL, // search pattern
8090 0, // nr
8091 1, // type
8092 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008093 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008094 {
8095 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008096 if (line != IObuff)
8097 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008098 break;
8099 }
8100 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008101 if (line != IObuff)
8102 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008103 ++lnum;
8104 line_breakcheck();
8105 }
8106 fclose(fd);
8107}
8108
8109/*
8110 * Search for a pattern in all the help files in the doc directory under
8111 * the given directory.
8112 */
8113 static void
8114hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008115 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008116 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008117 regmatch_T *p_regmatch,
8118 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008119#ifdef FEAT_MULTI_LANG
8120 , char_u *lang
8121#endif
8122 )
8123{
8124 int fcount;
8125 char_u **fnames;
8126 int fi;
8127
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008128 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008129 add_pathsep(dirname);
8130 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8131 if (gen_expand_wildcards(1, &dirname, &fcount,
8132 &fnames, EW_FILE|EW_SILENT) == OK
8133 && fcount > 0)
8134 {
8135 for (fi = 0; fi < fcount && !got_int; ++fi)
8136 {
8137#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008138 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008139 if (lang != NULL
8140 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008141 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008142 && !(STRNICMP(lang, "en", 2) == 0
8143 && STRNICMP("txt", fnames[fi]
8144 + STRLEN(fnames[fi]) - 3, 3) == 0))
8145 continue;
8146#endif
8147
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008148 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008149 }
8150 FreeWild(fcount, fnames);
8151 }
8152}
8153
8154/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008155 * Search for a pattern in all the help files in the 'runtimepath'
8156 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008157 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008158 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008159 */
8160 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008161hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008162{
8163 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008164
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008165 vimconv_T vc;
8166
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008167 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8168 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008169 vc.vc_type = CONV_NONE;
8170 if (!enc_utf8)
8171 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008172
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008173 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008174 p = p_rtp;
8175 while (*p != NUL && !got_int)
8176 {
8177 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8178
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008179 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008180#ifdef FEAT_MULTI_LANG
8181 , lang
8182#endif
8183 );
8184 }
8185
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008186 if (vc.vc_type != CONV_NONE)
8187 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008188}
8189
8190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 * ":helpgrep {pattern}"
8192 */
8193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008194ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 regmatch_T regmatch;
8197 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008198 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008199 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008200 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008201 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008202 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
Bram Moolenaar73633f82012-01-20 13:39:07 +01008204 switch (eap->cmdidx)
8205 {
8206 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8207 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8208 default: break;
8209 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008210 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8211 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008212 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008213#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008214 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008215 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008216#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008217 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008218
Bram Moolenaar39665952018-08-15 20:59:48 +02008219 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008220 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008221 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008222 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008223 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008224 }
8225
Bram Moolenaara16123a2019-03-28 20:31:07 +01008226 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8227 save_cpo = p_cpo;
8228 p_cpo = empty_option;
8229
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008230 incr_quickfix_busy();
8231
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008232#ifdef FEAT_MULTI_LANG
8233 // Check for a specified language
8234 lang = check_help_lang(eap->arg);
8235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8237 regmatch.rm_ic = FALSE;
8238 if (regmatch.regprog != NULL)
8239 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008240 qf_list_T *qfl;
8241
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008242 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008243 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008244 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008246 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008247
Bram Moolenaar473de612013-06-08 18:19:48 +02008248 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008250 qfl->qf_nonevalid = FALSE;
8251 qfl->qf_ptr = qfl->qf_start;
8252 qfl->qf_index = 1;
8253 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008254 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 }
8256
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008257 if (p_cpo == empty_option)
8258 p_cpo = save_cpo;
8259 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008260 {
8261 // Darn, some plugin changed the value. If it's still empty it was
8262 // changed and restored, need to restore in the complicated way.
8263 if (*p_cpo == NUL)
8264 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008265 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008266 }
8267
8268 if (updated)
8269 // This may open a window and source scripts, do this after 'cpo' was
8270 // restored.
8271 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
Bram Moolenaar73633f82012-01-20 13:39:07 +01008273 if (au_name != NULL)
8274 {
8275 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8276 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008277 // When adding a location list to an existing location list stack,
8278 // if the autocmd made the stack invalid, then just return.
8279 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008280 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008281 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008282 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008283 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008284 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008285
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008286 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008287 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008288 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008289 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008290 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008291
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008292 decr_quickfix_busy();
8293
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008294 if (eap->cmdidx == CMD_lhelpgrep)
8295 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008296 // If the help window is not opened or if it already points to the
8297 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008298 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008299 {
8300 if (new_qi)
8301 ll_free_all(&qi);
8302 }
8303 else if (curwin->w_llist == NULL)
8304 curwin->w_llist = qi;
8305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008307#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008308
8309#if defined(FEAT_EVAL) || defined(PROTO)
8310# ifdef FEAT_QUICKFIX
8311 static void
8312get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8313{
8314 if (what_arg->v_type == VAR_UNKNOWN)
8315 {
8316 if (rettv_list_alloc(rettv) == OK)
8317 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008318 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008319 }
8320 else
8321 {
8322 if (rettv_dict_alloc(rettv) == OK)
8323 if (is_qf || (wp != NULL))
8324 {
8325 if (what_arg->v_type == VAR_DICT)
8326 {
8327 dict_T *d = what_arg->vval.v_dict;
8328
8329 if (d != NULL)
8330 qf_get_properties(wp, d, rettv->vval.v_dict);
8331 }
8332 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008333 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008334 }
8335 }
8336}
8337# endif
8338
8339/*
8340 * "getloclist()" function
8341 */
8342 void
8343f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8344{
8345# ifdef FEAT_QUICKFIX
8346 win_T *wp;
8347
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008348 if (in_vim9script()
8349 && (check_for_number_arg(argvars, 0) == FAIL
8350 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8351 return;
8352
Bram Moolenaare677df82019-09-02 22:31:11 +02008353 wp = find_win_by_nr_or_id(&argvars[0]);
8354 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8355# endif
8356}
8357
8358/*
8359 * "getqflist()" function
8360 */
8361 void
8362f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8363{
8364# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008365 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8366 return;
8367
Bram Moolenaare677df82019-09-02 22:31:11 +02008368 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8369# endif
8370}
8371
8372/*
8373 * Used by "setqflist()" and "setloclist()" functions
8374 */
8375 static void
8376set_qf_ll_list(
8377 win_T *wp UNUSED,
8378 typval_T *list_arg UNUSED,
8379 typval_T *action_arg UNUSED,
8380 typval_T *what_arg UNUSED,
8381 typval_T *rettv)
8382{
8383# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008384 char_u *act;
8385 int action = 0;
8386 static int recursive = 0;
8387# endif
8388
8389 rettv->vval.v_number = -1;
8390
8391# ifdef FEAT_QUICKFIX
8392 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008393 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008394 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008395 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008396 else
8397 {
8398 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008399 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008400 int valid_dict = TRUE;
8401
8402 if (action_arg->v_type == VAR_STRING)
8403 {
8404 act = tv_get_string_chk(action_arg);
8405 if (act == NULL)
8406 return; // type error; errmsg already given
8407 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8408 act[1] == NUL)
8409 action = *act;
8410 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008411 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008412 }
8413 else if (action_arg->v_type == VAR_UNKNOWN)
8414 action = ' ';
8415 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008416 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008417
8418 if (action_arg->v_type != VAR_UNKNOWN
8419 && what_arg->v_type != VAR_UNKNOWN)
8420 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008421 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8422 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008423 else
8424 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008425 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008426 valid_dict = FALSE;
8427 }
8428 }
8429
8430 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008431 if (l != NULL && action && valid_dict
8432 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008433 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008434 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008435 rettv->vval.v_number = 0;
8436 --recursive;
8437 }
8438# endif
8439}
8440
8441/*
8442 * "setloclist()" function
8443 */
8444 void
8445f_setloclist(typval_T *argvars, typval_T *rettv)
8446{
8447 win_T *win;
8448
8449 rettv->vval.v_number = -1;
8450
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008451 if (in_vim9script()
8452 && (check_for_number_arg(argvars, 0) == FAIL
8453 || check_for_list_arg(argvars, 1) == FAIL
8454 || check_for_opt_string_arg(argvars, 2) == FAIL
8455 || (argvars[2].v_type != VAR_UNKNOWN
8456 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8457 return;
8458
Bram Moolenaare677df82019-09-02 22:31:11 +02008459 win = find_win_by_nr_or_id(&argvars[0]);
8460 if (win != NULL)
8461 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8462}
8463
8464/*
8465 * "setqflist()" function
8466 */
8467 void
8468f_setqflist(typval_T *argvars, typval_T *rettv)
8469{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008470 if (in_vim9script()
8471 && (check_for_list_arg(argvars, 0) == FAIL
8472 || check_for_opt_string_arg(argvars, 1) == FAIL
8473 || (argvars[1].v_type != VAR_UNKNOWN
8474 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8475 return;
8476
Bram Moolenaare677df82019-09-02 22:31:11 +02008477 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8478}
8479#endif