blob: 05cae9e88d69113c7d5e6c013686e718346c7c32 [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 Moolenaar3ff33112019-05-03 21:56:35 +0200190static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200192// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000193#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200194// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000195#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200196
197// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100198#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
199#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
200#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
201#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200202
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000203/*
204 * Return location list for window 'wp'
205 * For location list window, return the referenced location list
206 */
207#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
208
Bram Moolenaar95946f12019-03-31 15:31:59 +0200209// Macro to loop through all the items in a quickfix list
210// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100211#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200212 for (i = 1, qfp = qfl->qf_start; \
213 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100214 ++i, qfp = qfp->qf_next)
215
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200217 * Looking up a buffer can be slow if there are many. Remember the last one
218 * to make this a lot faster if there are multiple matches in the same file.
219 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200220static char_u *qf_last_bufname = NULL;
221static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200222
Bram Moolenaar4d170af2020-09-13 22:21:22 +0200223static char *e_current_quickfix_list_was_changed =
224 N_("E925: Current quickfix list was changed");
225static char *e_current_location_list_was_changed =
Bram Moolenaar3c097222017-12-21 20:54:49 +0100226 N_("E926: Current location list was changed");
227
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200228/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200229 * Maximum number of bytes allowed per line while reading a errorfile.
230 */
231#define LINE_MAXLEN 4096
232
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200233static struct fmtpattern
234{
235 char_u convchar;
236 char *pattern;
237} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200238 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200239 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200240 {'n', "\\d\\+"},
241 {'l', "\\d\\+"},
242 {'c', "\\d\\+"},
243 {'t', "."},
244 {'m', ".\\+"},
245 {'r', ".*"},
246 {'p', "[- .]*"},
247 {'v', "\\d\\+"},
248 {'s', ".\\+"},
249 {'o', ".\\+"}
250 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200251
252/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200254 * See fmt_pat definition above for the list of supported patterns. The
255 * pattern specifier is supplied in "efmpat". The converted pattern is stored
256 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 */
258 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200259efmpat_to_regpat(
260 char_u *efmpat,
261 char_u *regpat,
262 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200263 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100264 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265{
266 char_u *srcptr;
267
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200268 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200269 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200270 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100271 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200272 return NULL;
273 }
274 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200275 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200277 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100279 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200280 return NULL;
281 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200282 efminfo->addr[idx] = (char_u)++round;
283 *regpat++ = '\\';
284 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200285#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200286 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200287 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200288 // Also match "c:" in the file name, even when
289 // checking for a colon next: "%f:".
290 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 STRCPY(regpat, "\\%(\\a:\\)\\=");
292 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200293 }
294#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200295 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200296 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200297 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200298 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200299 // A file name may contain spaces, but this isn't
300 // in "\f". For "%f:%l:%m" there may be a ":" in
301 // the file name. Use ".\{-1,}x" instead (x is
302 // the next character), the requirement that :999:
303 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304 STRCPY(regpat, ".\\{-1,}");
305 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306 }
307 else
308 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200309 // File name followed by '\\' or '%': include as
310 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200311 STRCPY(regpat, "\\f\\+");
312 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200313 }
314 }
315 else
316 {
317 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200318 while ((*regpat = *srcptr++) != NUL)
319 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200320 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200321 *regpat++ = '\\';
322 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200323
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200324 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325}
326
327/*
328 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200329 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 */
331 static char_u *
332scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200333 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200334 char_u *efm,
335 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100336 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337{
338 char_u *efmp = *pefmp;
339
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200340 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200341 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200342 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 {
344 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200345 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 if (efmp < efm + len)
347 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200348 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200349 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200350 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200351 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200352 if (efmp == efm + len)
353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100354 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200355 return NULL;
356 }
357 }
358 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200359 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200360 *regpat++ = *++efmp;
361 *regpat++ = '\\';
362 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200363 }
364 else
365 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200366 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100367 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368 return NULL;
369 }
370
371 *pefmp = efmp;
372
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200373 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200374}
375
376/*
377 * Analyze/parse an errorformat prefix.
378 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200379 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100380efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200381{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200383 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200384 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200385 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200386 else
387 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100388 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200389 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200390 }
391
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200392 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200393}
394
395/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200396 * Converts a 'errorformat' string part in 'efm' to a regular expression
397 * pattern. The resulting regex pattern is returned in "regpat". Additional
398 * information about the 'erroformat' pattern is returned in "fmt_ptr".
399 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200400 */
401 static int
402efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200403 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200404 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200405 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200407{
408 char_u *ptr;
409 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200410 int round;
411 int idx = 0;
412
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200413 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200414 ptr = regpat;
415 *ptr++ = '^';
416 round = 0;
417 for (efmp = efm; efmp < efm + len; ++efmp)
418 {
419 if (*efmp == '%')
420 {
421 ++efmp;
422 for (idx = 0; idx < FMT_PATTERNS; ++idx)
423 if (fmt_pat[idx].convchar == *efmp)
424 break;
425 if (idx < FMT_PATTERNS)
426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100427 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200428 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200429 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200430 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200431 }
432 else if (*efmp == '*')
433 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200434 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100435 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200436 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200437 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200438 }
439 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200440 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441 else if (*efmp == '#')
442 *ptr++ = '*';
443 else if (*efmp == '>')
444 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200445 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200447 // prefix is allowed only at the beginning of the errorformat
448 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100449 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200450 if (efmp == NULL)
451 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200452 }
453 else
454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100455 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200456 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200457 }
458 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200459 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200460 {
461 if (*efmp == '\\' && efmp + 1 < efm + len)
462 ++efmp;
463 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200464 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200465 if (*efmp)
466 *ptr++ = *efmp;
467 }
468 }
469 *ptr++ = '$';
470 *ptr = NUL;
471
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200472 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200473}
474
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200475/*
476 * Free the 'errorformat' information list
477 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200478 static void
479free_efm_list(efm_T **efm_first)
480{
481 efm_T *efm_ptr;
482
483 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
484 {
485 *efm_first = efm_ptr->next;
486 vim_regfree(efm_ptr->prog);
487 vim_free(efm_ptr);
488 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100489 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200490}
491
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200492/*
493 * Compute the size of the buffer used to convert a 'errorformat' pattern into
494 * a regular expression pattern.
495 */
496 static int
497efm_regpat_bufsz(char_u *efm)
498{
499 int sz;
500 int i;
501
502 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
503 for (i = FMT_PATTERNS; i > 0; )
504 sz += (int)STRLEN(fmt_pat[--i].pattern);
505#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200506 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200507#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200508 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200509#endif
510
511 return sz;
512}
513
514/*
515 * Return the length of a 'errorformat' option part (separated by ",").
516 */
517 static int
518efm_option_part_len(char_u *efm)
519{
520 int len;
521
522 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
523 if (efm[len] == '\\' && efm[len + 1] != NUL)
524 ++len;
525
526 return len;
527}
528
529/*
530 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
531 * are parsed and converted to regular expressions. Returns information about
532 * the parsed 'errorformat' option.
533 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200534 static efm_T *
535parse_efm_option(char_u *efm)
536{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200537 efm_T *fmt_ptr = NULL;
538 efm_T *fmt_first = NULL;
539 efm_T *fmt_last = NULL;
540 char_u *fmtstr = NULL;
541 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200542 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200543
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200544 // Each part of the format string is copied and modified from errorformat
545 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200546
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200547 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200548 sz = efm_regpat_bufsz(efm);
549 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200550 goto parse_efm_error;
551
552 while (efm[0] != NUL)
553 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200554 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200555 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200556 if (fmt_ptr == NULL)
557 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200558 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200559 fmt_first = fmt_ptr;
560 else
561 fmt_last->next = fmt_ptr;
562 fmt_last = fmt_ptr;
563
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200564 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200565 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200566
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100567 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200568 goto parse_efm_error;
569 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
570 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200571 // Advance to next part
572 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200573 }
574
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200575 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100576 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200577
578 goto parse_efm_end;
579
580parse_efm_error:
581 free_efm_list(&fmt_first);
582
583parse_efm_end:
584 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200585
586 return fmt_first;
587}
588
Bram Moolenaare0d37972016-07-15 22:36:01 +0200589enum {
590 QF_FAIL = 0,
591 QF_OK = 1,
592 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200593 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200594 QF_IGNORE_LINE = 4,
595 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200596};
597
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200598/*
599 * State information used to parse lines and add entries to a quickfix/location
600 * list.
601 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200602typedef struct {
603 char_u *linebuf;
604 int linelen;
605 char_u *growbuf;
606 int growbufsiz;
607 FILE *fd;
608 typval_T *tv;
609 char_u *p_str;
610 listitem_T *p_li;
611 buf_T *buf;
612 linenr_T buflnum;
613 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100614 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200615} qfstate_T;
616
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200617/*
618 * Allocate more memory for the line buffer used for parsing lines.
619 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200620 static char_u *
621qf_grow_linebuf(qfstate_T *state, int newsz)
622{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200623 char_u *p;
624
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200625 // If the line exceeds LINE_MAXLEN exclude the last
626 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200627 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
628 if (state->growbuf == NULL)
629 {
630 state->growbuf = alloc(state->linelen + 1);
631 if (state->growbuf == NULL)
632 return NULL;
633 state->growbufsiz = state->linelen;
634 }
635 else if (state->linelen > state->growbufsiz)
636 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200637 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200638 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200639 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200640 state->growbufsiz = state->linelen;
641 }
642 return state->growbuf;
643}
644
645/*
646 * Get the next string (separated by newline) from state->p_str.
647 */
648 static int
649qf_get_next_str_line(qfstate_T *state)
650{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200651 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200652 char_u *p_str = state->p_str;
653 char_u *p;
654 int len;
655
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200656 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200657 return QF_END_OF_INPUT;
658
659 p = vim_strchr(p_str, '\n');
660 if (p != NULL)
661 len = (int)(p - p_str) + 1;
662 else
663 len = (int)STRLEN(p_str);
664
665 if (len > IOSIZE - 2)
666 {
667 state->linebuf = qf_grow_linebuf(state, len);
668 if (state->linebuf == NULL)
669 return QF_NOMEM;
670 }
671 else
672 {
673 state->linebuf = IObuff;
674 state->linelen = len;
675 }
676 vim_strncpy(state->linebuf, p_str, state->linelen);
677
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200678 // Increment using len in order to discard the rest of the
679 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200680 p_str += len;
681 state->p_str = p_str;
682
683 return QF_OK;
684}
685
686/*
687 * Get the next string from state->p_Li.
688 */
689 static int
690qf_get_next_list_line(qfstate_T *state)
691{
692 listitem_T *p_li = state->p_li;
693 int len;
694
695 while (p_li != NULL
696 && (p_li->li_tv.v_type != VAR_STRING
697 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200698 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200700 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200701 {
702 state->p_li = NULL;
703 return QF_END_OF_INPUT;
704 }
705
706 len = (int)STRLEN(p_li->li_tv.vval.v_string);
707 if (len > IOSIZE - 2)
708 {
709 state->linebuf = qf_grow_linebuf(state, len);
710 if (state->linebuf == NULL)
711 return QF_NOMEM;
712 }
713 else
714 {
715 state->linebuf = IObuff;
716 state->linelen = len;
717 }
718
719 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200721 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200722 return QF_OK;
723}
724
725/*
726 * Get the next string from state->buf.
727 */
728 static int
729qf_get_next_buf_line(qfstate_T *state)
730{
731 char_u *p_buf = NULL;
732 int len;
733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200734 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200735 if (state->buflnum > state->lnumlast)
736 return QF_END_OF_INPUT;
737
738 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
739 state->buflnum += 1;
740
741 len = (int)STRLEN(p_buf);
742 if (len > IOSIZE - 2)
743 {
744 state->linebuf = qf_grow_linebuf(state, len);
745 if (state->linebuf == NULL)
746 return QF_NOMEM;
747 }
748 else
749 {
750 state->linebuf = IObuff;
751 state->linelen = len;
752 }
753 vim_strncpy(state->linebuf, p_buf, state->linelen);
754
755 return QF_OK;
756}
757
758/*
759 * Get the next string from file state->fd.
760 */
761 static int
762qf_get_next_file_line(qfstate_T *state)
763{
764 int discard;
765 int growbuflen;
766
767 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
768 return QF_END_OF_INPUT;
769
770 discard = FALSE;
771 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200772 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200773 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200774 // The current line exceeds IObuff, continue reading using
775 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200776 if (state->growbuf == NULL)
777 {
778 state->growbufsiz = 2 * (IOSIZE - 1);
779 state->growbuf = alloc(state->growbufsiz);
780 if (state->growbuf == NULL)
781 return QF_NOMEM;
782 }
783
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200784 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200785 memcpy(state->growbuf, IObuff, IOSIZE - 1);
786 growbuflen = state->linelen;
787
788 for (;;)
789 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200790 char_u *p;
791
Bram Moolenaare0d37972016-07-15 22:36:01 +0200792 if (fgets((char *)state->growbuf + growbuflen,
793 state->growbufsiz - growbuflen, state->fd) == NULL)
794 break;
795 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
796 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 break;
799 if (state->growbufsiz == LINE_MAXLEN)
800 {
801 discard = TRUE;
802 break;
803 }
804
805 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
806 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200807 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200809 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200810 }
811
812 while (discard)
813 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200814 // The current line is longer than LINE_MAXLEN, continue
815 // reading but discard everything until EOL or EOF is
816 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200817 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
818 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200819 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200820 break;
821 }
822
823 state->linebuf = state->growbuf;
824 state->linelen = growbuflen;
825 }
826 else
827 state->linebuf = IObuff;
828
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200829 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200830 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
831 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100832 char_u *line;
833
834 line = string_convert(&state->vc, state->linebuf, &state->linelen);
835 if (line != NULL)
836 {
837 if (state->linelen < IOSIZE)
838 {
839 STRCPY(state->linebuf, line);
840 vim_free(line);
841 }
842 else
843 {
844 vim_free(state->growbuf);
845 state->linebuf = state->growbuf = line;
846 state->growbufsiz = state->linelen < LINE_MAXLEN
847 ? state->linelen : LINE_MAXLEN;
848 }
849 }
850 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100851
Bram Moolenaare0d37972016-07-15 22:36:01 +0200852 return QF_OK;
853}
854
855/*
856 * Get the next string from a file/buffer/list/string.
857 */
858 static int
859qf_get_nextline(qfstate_T *state)
860{
861 int status = QF_FAIL;
862
863 if (state->fd == NULL)
864 {
865 if (state->tv != NULL)
866 {
867 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200868 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200869 status = qf_get_next_str_line(state);
870 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200871 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200872 status = qf_get_next_list_line(state);
873 }
874 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200875 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200876 status = qf_get_next_buf_line(state);
877 }
878 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200879 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200880 status = qf_get_next_file_line(state);
881
882 if (status != QF_OK)
883 return status;
884
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200885 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200886 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200887 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888 state->linebuf[state->linelen - 1] = NUL;
889#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200890 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
891 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200892#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200893 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200894
Bram Moolenaare0d37972016-07-15 22:36:01 +0200895 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200896
897 return QF_OK;
898}
899
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200900typedef struct {
901 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200902 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200903 char_u *errmsg;
904 int errmsglen;
905 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200906 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200907 int col;
thinca6864efa2021-06-19 20:45:20 +0200908 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200909 char_u use_viscol;
910 char_u *pattern;
911 int enr;
912 int type;
913 int valid;
914} qffields_T;
915
916/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200917 * Parse the match for filename ('%f') pattern in regmatch.
918 * Return the matched value in "fields->namebuf".
919 */
920 static int
921qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
922{
923 int c;
924
925 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
926 return QF_FAIL;
927
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200928 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200929 c = *rmp->endp[midx];
930 *rmp->endp[midx] = NUL;
931 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
932 *rmp->endp[midx] = c;
933
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200934 // For separate filename patterns (%O, %P and %Q), the specified file
935 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200936 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
937 && mch_getperm(fields->namebuf) == -1)
938 return QF_FAIL;
939
940 return QF_OK;
941}
942
943/*
944 * Parse the match for error number ('%n') pattern in regmatch.
945 * Return the matched value in "fields->enr".
946 */
947 static int
948qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
949{
950 if (rmp->startp[midx] == NULL)
951 return QF_FAIL;
952 fields->enr = (int)atol((char *)rmp->startp[midx]);
953 return QF_OK;
954}
955
956/*
957 * Parse the match for line number (%l') pattern in regmatch.
958 * Return the matched value in "fields->lnum".
959 */
960 static int
961qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
962{
963 if (rmp->startp[midx] == NULL)
964 return QF_FAIL;
965 fields->lnum = atol((char *)rmp->startp[midx]);
966 return QF_OK;
967}
968
969/*
970 * Parse the match for column number ('%c') pattern in regmatch.
971 * Return the matched value in "fields->col".
972 */
973 static int
974qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
975{
976 if (rmp->startp[midx] == NULL)
977 return QF_FAIL;
978 fields->col = (int)atol((char *)rmp->startp[midx]);
979 return QF_OK;
980}
981
982/*
983 * Parse the match for error type ('%t') pattern in regmatch.
984 * Return the matched value in "fields->type".
985 */
986 static int
987qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
988{
989 if (rmp->startp[midx] == NULL)
990 return QF_FAIL;
991 fields->type = *rmp->startp[midx];
992 return QF_OK;
993}
994
995/*
Bram Moolenaarf4140482020-02-15 23:06:45 +0100996 * Copy a non-error line into the error string. Return the matched line in
997 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200998 */
999 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001000copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001001{
1002 char_u *p;
1003
1004 if (linelen >= fields->errmsglen)
1005 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001006 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001007 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1008 return QF_NOMEM;
1009 fields->errmsg = p;
1010 fields->errmsglen = linelen + 1;
1011 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001012 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001013 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001014
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001015 return QF_OK;
1016}
1017
1018/*
1019 * Parse the match for error message ('%m') pattern in regmatch.
1020 * Return the matched value in "fields->errmsg".
1021 */
1022 static int
1023qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1024{
1025 char_u *p;
1026 int len;
1027
1028 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1029 return QF_FAIL;
1030 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1031 if (len >= fields->errmsglen)
1032 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001033 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001034 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1035 return QF_NOMEM;
1036 fields->errmsg = p;
1037 fields->errmsglen = len + 1;
1038 }
1039 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1040 return QF_OK;
1041}
1042
1043/*
1044 * Parse the match for rest of a single-line file message ('%r') pattern.
1045 * Return the matched value in "tail".
1046 */
1047 static int
1048qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1049{
1050 if (rmp->startp[midx] == NULL)
1051 return QF_FAIL;
1052 *tail = rmp->startp[midx];
1053 return QF_OK;
1054}
1055
1056/*
1057 * Parse the match for the pointer line ('%p') pattern in regmatch.
1058 * Return the matched value in "fields->col".
1059 */
1060 static int
1061qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1062{
1063 char_u *match_ptr;
1064
1065 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1066 return QF_FAIL;
1067 fields->col = 0;
1068 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1069 ++match_ptr)
1070 {
1071 ++fields->col;
1072 if (*match_ptr == TAB)
1073 {
1074 fields->col += 7;
1075 fields->col -= fields->col % 8;
1076 }
1077 }
1078 ++fields->col;
1079 fields->use_viscol = TRUE;
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1085 * Return the matched value in "fields->col".
1086 */
1087 static int
1088qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 if (rmp->startp[midx] == NULL)
1091 return QF_FAIL;
1092 fields->col = (int)atol((char *)rmp->startp[midx]);
1093 fields->use_viscol = TRUE;
1094 return QF_OK;
1095}
1096
1097/*
1098 * Parse the match for the search text ('%s') pattern in regmatch.
1099 * Return the matched value in "fields->pattern".
1100 */
1101 static int
1102qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1103{
1104 int len;
1105
1106 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1107 return QF_FAIL;
1108 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1109 if (len > CMDBUFFSIZE - 5)
1110 len = CMDBUFFSIZE - 5;
1111 STRCPY(fields->pattern, "^\\V");
1112 STRNCAT(fields->pattern, rmp->startp[midx], len);
1113 fields->pattern[len + 3] = '\\';
1114 fields->pattern[len + 4] = '$';
1115 fields->pattern[len + 5] = NUL;
1116 return QF_OK;
1117}
1118
1119/*
1120 * Parse the match for the module ('%o') pattern in regmatch.
1121 * Return the matched value in "fields->module".
1122 */
1123 static int
1124qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1125{
1126 int len;
1127
1128 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1129 return QF_FAIL;
1130 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1131 if (len > CMDBUFFSIZE)
1132 len = CMDBUFFSIZE;
1133 STRNCAT(fields->module, rmp->startp[midx], len);
1134 return QF_OK;
1135}
1136
1137/*
1138 * 'errorformat' format pattern parser functions.
1139 * The '%f' and '%r' formats are parsed differently from other formats.
1140 * See qf_parse_match() for details.
1141 */
1142static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1143{
1144 NULL,
1145 qf_parse_fmt_n,
1146 qf_parse_fmt_l,
1147 qf_parse_fmt_c,
1148 qf_parse_fmt_t,
1149 qf_parse_fmt_m,
1150 NULL,
1151 qf_parse_fmt_p,
1152 qf_parse_fmt_v,
1153 qf_parse_fmt_s,
1154 qf_parse_fmt_o
1155};
1156
1157/*
1158 * Parse the error format pattern matches in "regmatch" and set the values in
1159 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1160 * match. Returns QF_OK if all the matches are successfully parsed. On
1161 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001162 */
1163 static int
1164qf_parse_match(
1165 char_u *linebuf,
1166 int linelen,
1167 efm_T *fmt_ptr,
1168 regmatch_T *regmatch,
1169 qffields_T *fields,
1170 int qf_multiline,
1171 int qf_multiscan,
1172 char_u **tail)
1173{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 int idx = fmt_ptr->prefix;
1175 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 int midx;
1177 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001178
1179 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1180 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001181 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001182 fields->type = idx;
1183 else
1184 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001185
1186 // Extract error message data from matched line.
1187 // We check for an actual submatch, because "\[" and "\]" in
1188 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 status = QF_OK;
1192 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001193 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1195 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001196 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001197 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001198 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001199 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001200 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001201 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001202 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001203 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001204 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001205 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001206
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001207 if (status != QF_OK)
1208 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001209 }
1210
1211 return QF_OK;
1212}
1213
1214/*
1215 * Parse an error line in 'linebuf' using a single error format string in
1216 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1217 * Returns QF_OK if the efm format matches completely and the fields are
1218 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1219 */
1220 static int
1221qf_parse_get_fields(
1222 char_u *linebuf,
1223 int linelen,
1224 efm_T *fmt_ptr,
1225 qffields_T *fields,
1226 int qf_multiline,
1227 int qf_multiscan,
1228 char_u **tail)
1229{
1230 regmatch_T regmatch;
1231 int status = QF_FAIL;
1232 int r;
1233
1234 if (qf_multiscan &&
1235 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1236 return QF_FAIL;
1237
1238 fields->namebuf[0] = NUL;
1239 fields->module[0] = NUL;
1240 fields->pattern[0] = NUL;
1241 if (!qf_multiscan)
1242 fields->errmsg[0] = NUL;
1243 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001244 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001245 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001246 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001247 fields->use_viscol = FALSE;
1248 fields->enr = -1;
1249 fields->type = 0;
1250 *tail = NULL;
1251
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001252 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001253 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001254 regmatch.regprog = fmt_ptr->prog;
1255 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1256 fmt_ptr->prog = regmatch.regprog;
1257 if (r)
1258 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1259 fields, qf_multiline, qf_multiscan, tail);
1260
1261 return status;
1262}
1263
1264/*
1265 * Parse directory error format prefixes (%D and %X).
1266 * Push and pop directories from the directory stack when scanning directory
1267 * names.
1268 */
1269 static int
1270qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1271{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001272 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001273 {
1274 if (*fields->namebuf == NUL)
1275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001276 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001277 return QF_FAIL;
1278 }
1279 qfl->qf_directory =
1280 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1281 if (qfl->qf_directory == NULL)
1282 return QF_FAIL;
1283 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001284 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001285 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1286
1287 return QF_OK;
1288}
1289
1290/*
1291 * Parse global file name error format prefixes (%O, %P and %Q).
1292 */
1293 static int
1294qf_parse_file_pfx(
1295 int idx,
1296 qffields_T *fields,
1297 qf_list_T *qfl,
1298 char_u *tail)
1299{
1300 fields->valid = FALSE;
1301 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1302 {
1303 if (*fields->namebuf && idx == 'P')
1304 qfl->qf_currfile =
1305 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1306 else if (idx == 'Q')
1307 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1308 *fields->namebuf = NUL;
1309 if (tail && *tail)
1310 {
1311 STRMOVE(IObuff, skipwhite(tail));
1312 qfl->qf_multiscan = TRUE;
1313 return QF_MULTISCAN;
1314 }
1315 }
1316
1317 return QF_OK;
1318}
1319
1320/*
1321 * Parse a non-error line (a line which doesn't match any of the error
1322 * format in 'efm').
1323 */
1324 static int
1325qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1326{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001327 fields->namebuf[0] = NUL; // no match found, remove file name
1328 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001329 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001330
Bram Moolenaarf4140482020-02-15 23:06:45 +01001331 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001332}
1333
1334/*
1335 * Parse multi-line error format prefixes (%C and %Z)
1336 */
1337 static int
1338qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001339 int idx,
1340 qf_list_T *qfl,
1341 qffields_T *fields)
1342{
1343 char_u *ptr;
1344 int len;
1345
1346 if (!qfl->qf_multiignore)
1347 {
1348 qfline_T *qfprev = qfl->qf_last;
1349
1350 if (qfprev == NULL)
1351 return QF_FAIL;
1352 if (*fields->errmsg && !qfl->qf_multiignore)
1353 {
1354 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001355 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001356 == NULL)
1357 return QF_FAIL;
1358 STRCPY(ptr, qfprev->qf_text);
1359 vim_free(qfprev->qf_text);
1360 qfprev->qf_text = ptr;
1361 *(ptr += len) = '\n';
1362 STRCPY(++ptr, fields->errmsg);
1363 }
1364 if (qfprev->qf_nr == -1)
1365 qfprev->qf_nr = fields->enr;
1366 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001367 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001368 qfprev->qf_type = fields->type;
1369
1370 if (!qfprev->qf_lnum)
1371 qfprev->qf_lnum = fields->lnum;
1372 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001373 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001374 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001375 qfprev->qf_viscol = fields->use_viscol;
1376 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001377 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001378 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001379 qfl->qf_directory,
1380 *fields->namebuf || qfl->qf_directory != NULL
1381 ? fields->namebuf
1382 : qfl->qf_currfile != NULL && fields->valid
1383 ? qfl->qf_currfile : 0);
1384 }
1385 if (idx == 'Z')
1386 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1387 line_breakcheck();
1388
1389 return QF_IGNORE_LINE;
1390}
1391
1392/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001393 * Parse a line and get the quickfix fields.
1394 * Return the QF_ status.
1395 */
1396 static int
1397qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001398 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001399 char_u *linebuf,
1400 int linelen,
1401 efm_T *fmt_first,
1402 qffields_T *fields)
1403{
1404 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001405 int idx = 0;
1406 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001407 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408
Bram Moolenaare333e792018-04-08 13:27:39 +02001409restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001410 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 if (fmt_start == NULL)
1412 fmt_ptr = fmt_first;
1413 else
1414 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001415 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 fmt_ptr = fmt_start;
1417 fmt_start = NULL;
1418 }
1419
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001420 // Try to match each part of 'errorformat' until we find a complete
1421 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1424 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001425 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001426 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1427 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1428 if (status == QF_NOMEM)
1429 return status;
1430 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001431 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001432 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001433 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434
1435 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1436 {
1437 if (fmt_ptr != NULL)
1438 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001439 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001440 status = qf_parse_dir_pfx(idx, fields, qfl);
1441 if (status != QF_OK)
1442 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001444
1445 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1446 if (status != QF_OK)
1447 return status;
1448
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001449 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001450 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001451 }
1452 else if (fmt_ptr != NULL)
1453 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001454 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001455 if (fmt_ptr->conthere)
1456 fmt_start = fmt_ptr;
1457
Bram Moolenaare9283662020-06-07 14:10:47 +02001458 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001460 qfl->qf_multiline = TRUE; // start of a multi-line message
1461 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001462 }
1463 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001464 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001465 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001466 if (status != QF_OK)
1467 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468 }
1469 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001470 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001471 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1472 if (status == QF_MULTISCAN)
1473 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001474 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001475 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001476 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001477 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001478 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001479 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001480 return QF_IGNORE_LINE;
1481 }
1482 }
1483
1484 return QF_OK;
1485}
1486
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001487/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001488 * Returns TRUE if the specified quickfix/location stack is empty
1489 */
1490 static int
1491qf_stack_empty(qf_info_T *qi)
1492{
1493 return qi == NULL || qi->qf_listcount <= 0;
1494}
1495
1496/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001497 * Returns TRUE if the specified quickfix/location list is empty.
1498 */
1499 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001500qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001501{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001502 return qfl == NULL || qfl->qf_count <= 0;
1503}
1504
1505/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001506 * Returns TRUE if the specified quickfix/location list is not empty and
1507 * has valid entries.
1508 */
1509 static int
1510qf_list_has_valid_entries(qf_list_T *qfl)
1511{
1512 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1513}
1514
1515/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001516 * Return a pointer to a list in the specified quickfix stack
1517 */
1518 static qf_list_T *
1519qf_get_list(qf_info_T *qi, int idx)
1520{
1521 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001522}
1523
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001524/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001525 * Allocate the fields used for parsing lines and populating a quickfix list.
1526 */
1527 static int
1528qf_alloc_fields(qffields_T *pfields)
1529{
1530 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1531 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1532 pfields->errmsglen = CMDBUFFSIZE + 1;
1533 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1534 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1535 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1536 || pfields->pattern == NULL || pfields->module == NULL)
1537 return FAIL;
1538
1539 return OK;
1540}
1541
1542/*
1543 * Free the fields used for parsing lines and populating a quickfix list.
1544 */
1545 static void
1546qf_free_fields(qffields_T *pfields)
1547{
1548 vim_free(pfields->namebuf);
1549 vim_free(pfields->module);
1550 vim_free(pfields->errmsg);
1551 vim_free(pfields->pattern);
1552}
1553
1554/*
1555 * Setup the state information used for parsing lines and populating a
1556 * quickfix list.
1557 */
1558 static int
1559qf_setup_state(
1560 qfstate_T *pstate,
1561 char_u *enc,
1562 char_u *efile,
1563 typval_T *tv,
1564 buf_T *buf,
1565 linenr_T lnumfirst,
1566 linenr_T lnumlast)
1567{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001568 pstate->vc.vc_type = CONV_NONE;
1569 if (enc != NULL && *enc != NUL)
1570 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001571
1572 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1573 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001574 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001575 return FAIL;
1576 }
1577
1578 if (tv != NULL)
1579 {
1580 if (tv->v_type == VAR_STRING)
1581 pstate->p_str = tv->vval.v_string;
1582 else if (tv->v_type == VAR_LIST)
1583 pstate->p_li = tv->vval.v_list->lv_first;
1584 pstate->tv = tv;
1585 }
1586 pstate->buf = buf;
1587 pstate->buflnum = lnumfirst;
1588 pstate->lnumlast = lnumlast;
1589
1590 return OK;
1591}
1592
1593/*
1594 * Cleanup the state information used for parsing lines and populating a
1595 * quickfix list.
1596 */
1597 static void
1598qf_cleanup_state(qfstate_T *pstate)
1599{
1600 if (pstate->fd != NULL)
1601 fclose(pstate->fd);
1602
1603 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001604 if (pstate->vc.vc_type != CONV_NONE)
1605 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001606}
1607
1608/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001609 * Process the next line from a file/buffer/list/string and add it
1610 * to the quickfix list 'qfl'.
1611 */
1612 static int
1613qf_init_process_nextline(
1614 qf_list_T *qfl,
1615 efm_T *fmt_first,
1616 qfstate_T *state,
1617 qffields_T *fields)
1618{
1619 int status;
1620
1621 // Get the next line from a file/buffer/list/string
1622 status = qf_get_nextline(state);
1623 if (status != QF_OK)
1624 return status;
1625
1626 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1627 fmt_first, fields);
1628 if (status != QF_OK)
1629 return status;
1630
1631 return qf_add_entry(qfl,
1632 qfl->qf_directory,
1633 (*fields->namebuf || qfl->qf_directory != NULL)
1634 ? fields->namebuf
1635 : ((qfl->qf_currfile != NULL && fields->valid)
1636 ? qfl->qf_currfile : (char_u *)NULL),
1637 fields->module,
1638 0,
1639 fields->errmsg,
1640 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001641 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001642 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001643 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001644 fields->use_viscol,
1645 fields->pattern,
1646 fields->enr,
1647 fields->type,
1648 fields->valid);
1649}
1650
1651/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001652 * Read the errorfile "efile" into memory, line by line, building the error
1653 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001654 * Alternative: when "efile" is NULL read errors from buffer "buf".
1655 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001656 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001657 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1658 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001659 * Return -1 for error, number of errors for success.
1660 */
1661 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001662qf_init_ext(
1663 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001664 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001665 char_u *efile,
1666 buf_T *buf,
1667 typval_T *tv,
1668 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001669 int newlist, // TRUE: start a new error list
1670 linenr_T lnumfirst, // first line number to use
1671 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001672 char_u *qf_title,
1673 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001674{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001675 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001676 qfstate_T state;
1677 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001678 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001679 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001680 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001682 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001683 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001684 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001686 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001687 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001688
Bram Moolenaara80faa82020-04-12 19:37:17 +02001689 CLEAR_FIELD(state);
1690 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001691 if ((qf_alloc_fields(&fields) == FAIL) ||
1692 (qf_setup_state(&state, enc, efile, tv, buf,
1693 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 goto qf_init_end;
1695
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001696 if (newlist || qf_idx == qi->qf_listcount)
1697 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001698 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001699 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001700 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001701 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001702 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001703 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001704 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001705 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001706 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001707 qfl = qf_get_list(qi, qf_idx);
1708 if (!qf_list_empty(qfl))
1709 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001712 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001713 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001714 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 else
1716 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001718 // If the errorformat didn't change between calls, then reuse the
1719 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001720 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1721 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001722 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001723 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001724 free_efm_list(&fmt_first);
1725
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001726 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001727 fmt_first = parse_efm_option(efm);
1728 if (fmt_first != NULL)
1729 last_efm = vim_strsave(efm);
1730 }
1731
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001732 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001735 // got_int is reset here, because it was probably set when killing the
1736 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 got_int = FALSE;
1738
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001739 // Read the lines in the error file one by one.
1740 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001741 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001743 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001744 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001745 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001746 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001747 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001748 if (status == QF_FAIL)
1749 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 line_breakcheck();
1752 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001753 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001755 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001757 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001758 qfl->qf_ptr = qfl->qf_start;
1759 qfl->qf_index = 1;
1760 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762 else
1763 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001764 qfl->qf_nonevalid = FALSE;
1765 if (qfl->qf_ptr == NULL)
1766 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001768 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001769 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001770 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001772 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001774 if (!adding)
1775 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001776 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001777 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001778 qi->qf_listcount--;
1779 if (qi->qf_curlist > 0)
1780 --qi->qf_curlist;
1781 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001782qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001783 if (qf_idx == qi->qf_curlist)
1784 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001785 qf_cleanup_state(&state);
1786 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788 return retval;
1789}
1790
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001791/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001792 * Read the errorfile "efile" into memory, line by line, building the error
1793 * list. Set the error list's title to qf_title.
1794 * Return -1 for error, number of errors for success.
1795 */
1796 int
1797qf_init(win_T *wp,
1798 char_u *efile,
1799 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001800 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001801 char_u *qf_title,
1802 char_u *enc)
1803{
1804 qf_info_T *qi = &ql_info;
1805
1806 if (wp != NULL)
1807 {
1808 qi = ll_get_or_alloc_list(wp);
1809 if (qi == NULL)
1810 return FAIL;
1811 }
1812
1813 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1814 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1815}
1816
1817/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001818 * Set the title of the specified quickfix list. Frees the previous title.
1819 * Prepends ':' to the title.
1820 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001821 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001822qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001823{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001824 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001825
Bram Moolenaarfb604092014-07-23 15:55:00 +02001826 if (title != NULL)
1827 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001828 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001829
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001830 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001831 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001832 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001833 }
1834}
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001837 * The title of a quickfix/location list is set, by default, to the command
1838 * that created the quickfix list with the ":" prefix.
1839 * Create a quickfix list title string by prepending ":" to a user command.
1840 * Returns a pointer to a static buffer with the title.
1841 */
1842 static char_u *
1843qf_cmdtitle(char_u *cmd)
1844{
1845 static char_u qftitle_str[IOSIZE];
1846
1847 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1848 return qftitle_str;
1849}
1850
1851/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001852 * Return a pointer to the current list in the specified quickfix stack
1853 */
1854 static qf_list_T *
1855qf_get_curlist(qf_info_T *qi)
1856{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001857 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001858}
1859
1860/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001861 * Prepare for adding a new quickfix list. If the current list is in the
1862 * middle of the stack, then all the following lists are freed and then
1863 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 */
1865 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001866qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
1868 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001869 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001871 // If the current entry is not the last entry, delete entries beyond
1872 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001873 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001874 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001875 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001877 // When the stack is full, remove to oldest entry
1878 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001879 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001881 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001883 qi->qf_lists[i - 1] = qi->qf_lists[i];
1884 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
1886 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001887 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001888 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001889 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001890 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001891 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001892 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893}
1894
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001895/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001896 * Queue location list stack delete request.
1897 */
1898 static void
1899locstack_queue_delreq(qf_info_T *qi)
1900{
1901 qf_delq_T *q;
1902
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001903 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001904 if (q != NULL)
1905 {
1906 q->qi = qi;
1907 q->next = qf_delq_head;
1908 qf_delq_head = q;
1909 }
1910}
1911
1912/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001913 * Return the global quickfix stack window buffer number.
1914 */
1915 int
1916qf_stack_get_bufnr(void)
1917{
1918 return ql_info.qf_bufnr;
1919}
1920
1921/*
1922 * Wipe the quickfix window buffer (if present) for the specified
1923 * quickfix/location list.
1924 */
1925 static void
1926wipe_qf_buffer(qf_info_T *qi)
1927{
1928 buf_T *qfbuf;
1929
1930 if (qi->qf_bufnr != INVALID_QFBUFNR)
1931 {
1932 qfbuf = buflist_findnr(qi->qf_bufnr);
1933 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1934 {
1935 // If the quickfix buffer is not loaded in any window, then
1936 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001937 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001938 qi->qf_bufnr = INVALID_QFBUFNR;
1939 }
1940 }
1941}
1942
1943/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001944 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001945 */
1946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001947ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001948{
1949 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001950 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001951
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001952 qi = *pqi;
1953 if (qi == NULL)
1954 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001955 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001956
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001957 // If the location list is still in use, then queue the delete request
1958 // to be processed later.
1959 if (quickfix_busy > 0)
1960 {
1961 locstack_queue_delreq(qi);
1962 return;
1963 }
1964
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001965 qi->qf_refcount--;
1966 if (qi->qf_refcount < 1)
1967 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001968 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001969 // If the quickfix window buffer is loaded, then wipe it
1970 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001971
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001972 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001973 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001974 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001975 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001976}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001977
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001978/*
1979 * Free all the quickfix/location lists in the stack.
1980 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001981 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001982qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001983{
1984 int i;
1985 qf_info_T *qi = &ql_info;
1986
1987 if (wp != NULL)
1988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001989 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990 ll_free_all(&wp->w_llist);
1991 ll_free_all(&wp->w_llist_ref);
1992 }
1993 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001994 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001995 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001996 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001997}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002000 * Delay freeing of location list stacks when the quickfix code is running.
2001 * Used to avoid problems with autocmds freeing location list stacks when the
2002 * quickfix code is still referencing the stack.
2003 * Must always call decr_quickfix_busy() exactly once after this.
2004 */
2005 static void
2006incr_quickfix_busy(void)
2007{
2008 quickfix_busy++;
2009}
2010
2011/*
2012 * Safe to free location list stacks. Process any delayed delete requests.
2013 */
2014 static void
2015decr_quickfix_busy(void)
2016{
2017 if (--quickfix_busy == 0)
2018 {
2019 // No longer referencing the location lists. Process all the pending
2020 // delete requests.
2021 while (qf_delq_head != NULL)
2022 {
2023 qf_delq_T *q = qf_delq_head;
2024
2025 qf_delq_head = q->next;
2026 ll_free_all(&q->qi);
2027 vim_free(q);
2028 }
2029 }
2030#ifdef ABORT_ON_INTERNAL_ERROR
2031 if (quickfix_busy < 0)
2032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002033 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002034 abort();
2035 }
2036#endif
2037}
2038
2039#if defined(EXITFREE) || defined(PROTO)
2040 void
2041check_quickfix_busy(void)
2042{
2043 if (quickfix_busy != 0)
2044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002045 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002046# ifdef ABORT_ON_INTERNAL_ERROR
2047 abort();
2048# endif
2049 }
2050}
2051#endif
2052
2053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002055 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 */
2057 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002058qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002059 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002060 char_u *dir, // optional directory name
2061 char_u *fname, // file name or NULL
2062 char_u *module, // module name or NULL
2063 int bufnum, // buffer number or zero
2064 char_u *mesg, // message
2065 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002066 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002067 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002068 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002069 int vis_col, // using visual column
2070 char_u *pattern, // search pattern
2071 int nr, // error number
2072 int type, // type character
2073 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002075 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002076 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002078 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002079 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002080 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002081 {
2082 buf_T *buf = buflist_findnr(bufnum);
2083
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002084 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002085 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002086 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002087 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002088 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002089 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002090 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2092 {
2093 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002094 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002097 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002099 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002100 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002101 if (pattern == NULL || *pattern == NUL)
2102 qfp->qf_pattern = NULL;
2103 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2104 {
2105 vim_free(qfp->qf_text);
2106 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002107 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002108 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002109 if (module == NULL || *module == NUL)
2110 qfp->qf_module = NULL;
2111 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2112 {
2113 vim_free(qfp->qf_text);
2114 vim_free(qfp->qf_pattern);
2115 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002116 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002119 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 type = 0;
2121 qfp->qf_type = type;
2122 qfp->qf_valid = valid;
2123
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002124 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002125 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002127 qfl->qf_start = qfp;
2128 qfl->qf_ptr = qfp;
2129 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002130 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132 else
2133 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002134 qfp->qf_prev = *lastp;
2135 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002137 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002139 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002140 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002141 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002143 qfl->qf_index = qfl->qf_count;
2144 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146
Bram Moolenaar95946f12019-03-31 15:31:59 +02002147 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148}
2149
2150/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002151 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002153 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002154qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002155{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002156 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002158 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002159 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002160 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002161 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002162 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002163 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002164 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002165 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002166}
2167
2168/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002169 * Return the location list stack for window 'wp'.
2170 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171 */
2172 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002173ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002174{
2175 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002176 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002177 return wp->w_llist_ref;
2178
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002179 // For a non-location list window, w_llist_ref should not point to a
2180 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002181 ll_free_all(&wp->w_llist_ref);
2182
2183 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002184 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002185 return wp->w_llist;
2186}
2187
2188/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002189 * Get the quickfix/location list stack to use for the specified Ex command.
2190 * For a location list command, returns the stack for the current window. If
2191 * the location list is not found, then returns NULL and prints an error
2192 * message if 'print_emsg' is TRUE.
2193 */
2194 static qf_info_T *
2195qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2196{
2197 qf_info_T *qi = &ql_info;
2198
2199 if (is_loclist_cmd(eap->cmdidx))
2200 {
2201 qi = GET_LOC_LIST(curwin);
2202 if (qi == NULL)
2203 {
2204 if (print_emsg)
2205 emsg(_(e_loclist));
2206 return NULL;
2207 }
2208 }
2209
2210 return qi;
2211}
2212
2213/*
2214 * Get the quickfix/location list stack to use for the specified Ex command.
2215 * For a location list command, returns the stack for the current window.
2216 * If the location list is not present, then allocates a new one.
2217 * Returns NULL if the allocation fails. For a location list command, sets
2218 * 'pwinp' to curwin.
2219 */
2220 static qf_info_T *
2221qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2222{
2223 qf_info_T *qi = &ql_info;
2224
2225 if (is_loclist_cmd(eap->cmdidx))
2226 {
2227 qi = ll_get_or_alloc_list(curwin);
2228 if (qi == NULL)
2229 return NULL;
2230 *pwinp = curwin;
2231 }
2232
2233 return qi;
2234}
2235
2236/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002237 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2238 */
2239 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002240copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002241{
2242 int i;
2243 qfline_T *from_qfp;
2244 qfline_T *prevp;
2245
2246 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002247 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002248 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002249 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002250 NULL,
2251 NULL,
2252 from_qfp->qf_module,
2253 0,
2254 from_qfp->qf_text,
2255 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002256 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002257 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002258 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002259 from_qfp->qf_viscol,
2260 from_qfp->qf_pattern,
2261 from_qfp->qf_nr,
2262 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002263 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002264 return FAIL;
2265
2266 // qf_add_entry() will not set the qf_num field, as the
2267 // directory and file names are not supplied. So the qf_fnum
2268 // field is copied here.
2269 prevp = to_qfl->qf_last;
2270 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2271 prevp->qf_type = from_qfp->qf_type; // error type
2272 if (from_qfl->qf_ptr == from_qfp)
2273 to_qfl->qf_ptr = prevp; // current location
2274 }
2275
2276 return OK;
2277}
2278
2279/*
2280 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2281 */
2282 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002283copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002284{
2285 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002286 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002287 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2288 to_qfl->qf_count = 0;
2289 to_qfl->qf_index = 0;
2290 to_qfl->qf_start = NULL;
2291 to_qfl->qf_last = NULL;
2292 to_qfl->qf_ptr = NULL;
2293 if (from_qfl->qf_title != NULL)
2294 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2295 else
2296 to_qfl->qf_title = NULL;
2297 if (from_qfl->qf_ctx != NULL)
2298 {
2299 to_qfl->qf_ctx = alloc_tv();
2300 if (to_qfl->qf_ctx != NULL)
2301 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2302 }
2303 else
2304 to_qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002305 if (from_qfl->qftf_cb.cb_name != NULL)
2306 copy_callback(&to_qfl->qftf_cb, &from_qfl->qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002307 else
Bram Moolenaard43906d2020-07-20 21:31:32 +02002308 to_qfl->qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002309
2310 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002311 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002312 return FAIL;
2313
2314 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2315
2316 // Assign a new ID for the location list
2317 to_qfl->qf_id = ++last_qf_id;
2318 to_qfl->qf_changedtick = 0L;
2319
2320 // When no valid entries are present in the list, qf_ptr points to
2321 // the first item in the list
2322 if (to_qfl->qf_nonevalid)
2323 {
2324 to_qfl->qf_ptr = to_qfl->qf_start;
2325 to_qfl->qf_index = 1;
2326 }
2327
2328 return OK;
2329}
2330
2331/*
2332 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002333 */
2334 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002335copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002336{
2337 qf_info_T *qi;
2338 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339
Bram Moolenaar09037502018-09-25 22:08:14 +02002340 // When copying from a location list window, copy the referenced
2341 // location list. For other windows, copy the location list for
2342 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002343 if (IS_LL_WINDOW(from))
2344 qi = from->w_llist_ref;
2345 else
2346 qi = from->w_llist;
2347
Bram Moolenaar09037502018-09-25 22:08:14 +02002348 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002349 return;
2350
Bram Moolenaar09037502018-09-25 22:08:14 +02002351 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002352 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002353 return;
2354
2355 to->w_llist->qf_listcount = qi->qf_listcount;
2356
Bram Moolenaar09037502018-09-25 22:08:14 +02002357 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002358 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002359 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002360 to->w_llist->qf_curlist = idx;
2361
Bram Moolenaar0398e002019-03-21 21:12:49 +01002362 if (copy_loclist(qf_get_list(qi, idx),
2363 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002364 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002365 qf_free_all(to);
2366 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002367 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002368 }
2369
Bram Moolenaar09037502018-09-25 22:08:14 +02002370 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002371}
2372
2373/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002374 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002375 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 */
2377 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002378qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
Bram Moolenaar82404332016-07-10 17:00:38 +02002380 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002381 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002382 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002383
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002384 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaare60acc12011-05-10 16:41:25 +02002387#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002388 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002389#endif
2390#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002391 if (directory != NULL)
2392 slash_adjust(directory);
2393 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002394#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002395 if (directory != NULL && !vim_isAbsName(fname)
2396 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2397 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002398 // Here we check if the file really exists.
2399 // This should normally be true, but if make works without
2400 // "leaving directory"-messages we might have missed a
2401 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002402 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002405 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002406 if (directory)
2407 ptr = concat_fnames(directory, fname, TRUE);
2408 else
2409 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002411 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002412 bufname = ptr;
2413 }
2414 else
2415 bufname = fname;
2416
2417 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002418 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002419 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002420 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002421 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002423 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002424 {
2425 vim_free(qf_last_bufname);
2426 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2427 if (bufname == ptr)
2428 qf_last_bufname = bufname;
2429 else
2430 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002431 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002432 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002433 if (buf == NULL)
2434 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002435
Bram Moolenaarc1542742016-07-20 21:44:37 +02002436 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002437 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002438 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439}
2440
2441/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002442 * Push dirbuf onto the directory stack and return pointer to actual dir or
2443 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 */
2445 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002446qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 struct dir_stack_T *ds_new;
2449 struct dir_stack_T *ds_ptr;
2450
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002451 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002452 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (ds_new == NULL)
2454 return NULL;
2455
2456 ds_new->next = *stackptr;
2457 *stackptr = ds_new;
2458
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002459 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if (vim_isAbsName(dirbuf)
2461 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002462 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 (*stackptr)->dirname = vim_strsave(dirbuf);
2464 else
2465 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002466 // Okay we don't have an absolute path.
2467 // dirbuf must be a subdir of one of the directories on the stack.
2468 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 ds_new = (*stackptr)->next;
2470 (*stackptr)->dirname = NULL;
2471 while (ds_new)
2472 {
2473 vim_free((*stackptr)->dirname);
2474 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2475 TRUE);
2476 if (mch_isdir((*stackptr)->dirname) == TRUE)
2477 break;
2478
2479 ds_new = ds_new->next;
2480 }
2481
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002482 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 while ((*stackptr)->next != ds_new)
2484 {
2485 ds_ptr = (*stackptr)->next;
2486 (*stackptr)->next = (*stackptr)->next->next;
2487 vim_free(ds_ptr->dirname);
2488 vim_free(ds_ptr);
2489 }
2490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002491 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (ds_new == NULL)
2493 {
2494 vim_free((*stackptr)->dirname);
2495 (*stackptr)->dirname = vim_strsave(dirbuf);
2496 }
2497 }
2498
2499 if ((*stackptr)->dirname != NULL)
2500 return (*stackptr)->dirname;
2501 else
2502 {
2503 ds_ptr = *stackptr;
2504 *stackptr = (*stackptr)->next;
2505 vim_free(ds_ptr);
2506 return NULL;
2507 }
2508}
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510/*
2511 * pop dirbuf from the directory stack and return previous directory or NULL if
2512 * stack is empty
2513 */
2514 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002515qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 struct dir_stack_T *ds_ptr;
2518
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002519 // TODO: Should we check if dirbuf is the directory on top of the stack?
2520 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002522 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (*stackptr != NULL)
2524 {
2525 ds_ptr = *stackptr;
2526 *stackptr = (*stackptr)->next;
2527 vim_free(ds_ptr->dirname);
2528 vim_free(ds_ptr);
2529 }
2530
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002531 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 return *stackptr ? (*stackptr)->dirname : NULL;
2533}
2534
2535/*
2536 * clean up directory stack
2537 */
2538 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002539qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 struct dir_stack_T *ds_ptr;
2542
2543 while ((ds_ptr = *stackptr) != NULL)
2544 {
2545 *stackptr = (*stackptr)->next;
2546 vim_free(ds_ptr->dirname);
2547 vim_free(ds_ptr);
2548 }
2549}
2550
2551/*
2552 * Check in which directory of the directory stack the given file can be
2553 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002554 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 * Cleans up intermediate directory entries.
2556 *
2557 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002558 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 * ./
2560 * ./aa
2561 * ./aa/bb
2562 * ./bb
2563 * ./bb/x.c
2564 * and make says:
2565 * making all in aa
2566 * making all in bb
2567 * x.c:9: Error
2568 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2569 * qf_guess_filepath will return NULL.
2570 */
2571 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002572qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
2574 struct dir_stack_T *ds_ptr;
2575 struct dir_stack_T *ds_tmp;
2576 char_u *fullname;
2577
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002578 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002579 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 return NULL;
2581
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002582 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 fullname = NULL;
2584 while (ds_ptr)
2585 {
2586 vim_free(fullname);
2587 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2588
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002589 // If concat_fnames failed, just go on. The worst thing that can happen
2590 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2592 break;
2593
2594 ds_ptr = ds_ptr->next;
2595 }
2596
2597 vim_free(fullname);
2598
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002599 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002600 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002602 ds_tmp = qfl->qf_dir_stack->next;
2603 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 vim_free(ds_tmp->dirname);
2605 vim_free(ds_tmp);
2606 }
2607
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002608 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609}
2610
2611/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002612 * Returns TRUE if a quickfix/location list with the given identifier exists.
2613 */
2614 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002615qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002616{
2617 qf_info_T *qi = &ql_info;
2618 int i;
2619
2620 if (wp != NULL)
2621 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002622 if (!win_valid(wp))
2623 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002624 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002625 if (qi == NULL)
2626 return FALSE;
2627 }
2628
2629 for (i = 0; i < qi->qf_listcount; ++i)
2630 if (qi->qf_lists[i].qf_id == qf_id)
2631 return TRUE;
2632
2633 return FALSE;
2634}
2635
2636/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002637 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002638 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002639 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002640 * Similar to location list.
2641 */
2642 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002643is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002644{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002645 qfline_T *qfp;
2646 int i;
2647
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002648 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002649 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2650 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002651 break;
2652
Bram Moolenaar95946f12019-03-31 15:31:59 +02002653 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002654 return FALSE;
2655
2656 return TRUE;
2657}
2658
2659/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002660 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002661 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002662 */
2663 static qfline_T *
2664get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002665 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002666 qfline_T *qf_ptr,
2667 int *qf_index,
2668 int dir)
2669{
2670 int idx;
2671 int old_qf_fnum;
2672
2673 idx = *qf_index;
2674 old_qf_fnum = qf_ptr->qf_fnum;
2675
2676 do
2677 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002678 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002679 return NULL;
2680 ++idx;
2681 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002682 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002683 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2684
2685 *qf_index = idx;
2686 return qf_ptr;
2687}
2688
2689/*
2690 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002691 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002692 */
2693 static qfline_T *
2694get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002695 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002696 qfline_T *qf_ptr,
2697 int *qf_index,
2698 int dir)
2699{
2700 int idx;
2701 int old_qf_fnum;
2702
2703 idx = *qf_index;
2704 old_qf_fnum = qf_ptr->qf_fnum;
2705
2706 do
2707 {
2708 if (idx == 1 || qf_ptr->qf_prev == NULL)
2709 return NULL;
2710 --idx;
2711 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002712 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002713 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2714
2715 *qf_index = idx;
2716 return qf_ptr;
2717}
2718
2719/*
2720 * Get the n'th (errornr) previous/next valid entry from the current entry in
2721 * the quickfix list.
2722 * dir == FORWARD or FORWARD_FILE: next valid entry
2723 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2724 */
2725 static qfline_T *
2726get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002727 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002728 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002729 int dir,
2730 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002732 qfline_T *qf_ptr = qfl->qf_ptr;
2733 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002734 qfline_T *prev_qf_ptr;
2735 int prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002736 char_u *err = e_no_more_items;
2737
2738 while (errornr--)
2739 {
2740 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002741 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002742
2743 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002744 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002745 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002746 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002747 if (qf_ptr == NULL)
2748 {
2749 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002750 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002751 if (err != NULL)
2752 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002753 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002754 return NULL;
2755 }
2756 break;
2757 }
2758
2759 err = NULL;
2760 }
2761
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002762 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002763 return qf_ptr;
2764}
2765
2766/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002767 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2768 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002769 */
2770 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002771get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002772{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002773 qfline_T *qf_ptr = qfl->qf_ptr;
2774 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002775
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002776 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002777 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2778 {
2779 --qf_idx;
2780 qf_ptr = qf_ptr->qf_prev;
2781 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002782 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002783 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2784 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002785 {
2786 ++qf_idx;
2787 qf_ptr = qf_ptr->qf_next;
2788 }
2789
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002790 *new_qfidx = qf_idx;
2791 return qf_ptr;
2792}
2793
2794/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002795 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002796 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2797 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2798 * Returns a pointer to the entry and the index of the new entry is stored in
2799 * 'new_qfidx'.
2800 */
2801 static qfline_T *
2802qf_get_entry(
2803 qf_list_T *qfl,
2804 int errornr,
2805 int dir,
2806 int *new_qfidx)
2807{
2808 qfline_T *qf_ptr = qfl->qf_ptr;
2809 int qfidx = qfl->qf_index;
2810
2811 if (dir != 0) // next/prev valid entry
2812 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2813 else if (errornr != 0) // go to specified number
2814 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2815
2816 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002817 return qf_ptr;
2818}
2819
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002820/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002821 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002822 */
2823 static win_T *
2824qf_find_help_win(void)
2825{
2826 win_T *wp;
2827
2828 FOR_ALL_WINDOWS(wp)
2829 if (bt_help(wp->w_buffer))
2830 return wp;
2831
2832 return NULL;
2833}
2834
2835/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002836 * Set the location list for the specified window to 'qi'.
2837 */
2838 static void
2839win_set_loclist(win_T *wp, qf_info_T *qi)
2840{
2841 wp->w_llist = qi;
2842 qi->qf_refcount++;
2843}
2844
2845/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002846 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2847 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002848 */
2849 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002850jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002851{
2852 win_T *wp;
2853 int flags;
2854
Bram Moolenaare1004402020-10-24 20:49:43 +02002855 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002856 wp = NULL;
2857 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002858 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002859 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2860 win_enter(wp, TRUE);
2861 else
2862 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002863 // Split off help window; put it at far top if no position
2864 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002865 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002867 && curwin->w_width < 80)
2868 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002869 // If the user asks to open a new window, then copy the location list.
2870 // Otherwise, don't copy the location list.
2871 if (IS_LL_STACK(qi) && !newwin)
2872 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002873
2874 if (win_split(0, flags) == FAIL)
2875 return FAIL;
2876
2877 *opened_window = TRUE;
2878
2879 if (curwin->w_height < p_hh)
2880 win_setheight((int)p_hh);
2881
Bram Moolenaarb2443732018-11-11 22:50:27 +01002882 // When using location list, the new window should use the supplied
2883 // location list. If the user asks to open a new window, then the new
2884 // window will get a copy of the location list.
2885 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002886 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002887 }
2888
2889 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002890 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002891
2892 return OK;
2893}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002894
2895/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002896 * Find a non-quickfix window using the given location list stack in the
2897 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002898 * Returns NULL if a matching window is not found.
2899 */
2900 static win_T *
2901qf_find_win_with_loclist(qf_info_T *ll)
2902{
2903 win_T *wp;
2904
2905 FOR_ALL_WINDOWS(wp)
2906 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2907 return wp;
2908
2909 return NULL;
2910}
2911
2912/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002913 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002914 */
2915 static win_T *
2916qf_find_win_with_normal_buf(void)
2917{
2918 win_T *wp;
2919
2920 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002921 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002922 return wp;
2923
2924 return NULL;
2925}
2926
2927/*
2928 * Go to a window in any tabpage containing the specified file. Returns TRUE
2929 * if successfully jumped to the window. Otherwise returns FALSE.
2930 */
2931 static int
2932qf_goto_tabwin_with_file(int fnum)
2933{
2934 tabpage_T *tp;
2935 win_T *wp;
2936
2937 FOR_ALL_TAB_WINDOWS(tp, wp)
2938 if (wp->w_buffer->b_fnum == fnum)
2939 {
2940 goto_tabpage_win(tp, wp);
2941 return TRUE;
2942 }
2943
2944 return FALSE;
2945}
2946
2947/*
2948 * Create a new window to show a file above the quickfix window. Called when
2949 * only the quickfix window is present.
2950 */
2951 static int
2952qf_open_new_file_win(qf_info_T *ll_ref)
2953{
2954 int flags;
2955
2956 flags = WSP_ABOVE;
2957 if (ll_ref != NULL)
2958 flags |= WSP_NEWLOC;
2959 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002960 return FAIL; // not enough room for window
2961 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002962 swb_flags = 0;
2963 RESET_BINDING(curwin);
2964 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 // The new window should use the location list from the
2966 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002967 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002968 return OK;
2969}
2970
2971/*
2972 * Go to a window that shows the right buffer. If the window is not found, go
2973 * to the window just above the location list window. This is used for opening
2974 * a file from a location window and not from a quickfix window. If some usable
2975 * window is previously found, then it is supplied in 'use_win'.
2976 */
2977 static void
2978qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2979{
2980 win_T *win = use_win;
2981
2982 if (win == NULL)
2983 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002984 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002985 FOR_ALL_WINDOWS(win)
2986 if (win->w_buffer->b_fnum == qf_fnum)
2987 break;
2988 if (win == NULL)
2989 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002990 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002991 win = curwin;
2992 do
2993 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002994 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002995 break;
2996 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002997 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002998 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002999 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003000 } while (win != curwin);
3001 }
3002 }
3003 win_goto(win);
3004
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003005 // If the location list for the window is not set, then set it
3006 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003007 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003008 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003009}
3010
3011/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003012 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3013 * not found, then go to the window just above the quickfix window. This is
3014 * used for opening a file from a quickfix window and not from a location
3015 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003016 */
3017 static void
3018qf_goto_win_with_qfl_file(int qf_fnum)
3019{
3020 win_T *win;
3021 win_T *altwin;
3022
3023 win = curwin;
3024 altwin = NULL;
3025 for (;;)
3026 {
3027 if (win->w_buffer->b_fnum == qf_fnum)
3028 break;
3029 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003030 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003031 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003032 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003033
3034 if (IS_QF_WINDOW(win))
3035 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003036 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003037 // window, unless 'switchbuf' contains 'uselast': in this case we
3038 // try to jump to the previously used window first.
3039 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3040 win = prevwin;
3041 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003042 win = altwin;
3043 else if (curwin->w_prev != NULL)
3044 win = curwin->w_prev;
3045 else
3046 win = curwin->w_next;
3047 break;
3048 }
3049
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003050 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003051 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003052 altwin = win;
3053 }
3054
3055 win_goto(win);
3056}
3057
3058/*
3059 * Find a suitable window for opening a file (qf_fnum) from the
3060 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003061 * window, jump to it. Otherwise open a new window to display the file. If
3062 * 'newwin' is TRUE, then always open a new window. This is called from either
3063 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003064 */
3065 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003066qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003067{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003068 win_T *usable_wp = NULL;
3069 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003070 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003071
Bram Moolenaarb2443732018-11-11 22:50:27 +01003072 // If opening a new window, then don't use the location list referred by
3073 // the current window. Otherwise two windows will refer to the same
3074 // location list.
3075 if (!newwin)
3076 ll_ref = curwin->w_llist_ref;
3077
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003078 if (ll_ref != NULL)
3079 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003080 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003081 usable_wp = qf_find_win_with_loclist(ll_ref);
3082 if (usable_wp != NULL)
3083 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003084 }
3085
3086 if (!usable_win)
3087 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003088 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003089 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003090 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003091 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003092 }
3093
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003094 // If no usable window is found and 'switchbuf' contains "usetab"
3095 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003096 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003097 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003098
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003099 // If there is only one window and it is the quickfix window, create a
3100 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003101 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003102 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003103 if (qf_open_new_file_win(ll_ref) != OK)
3104 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003105 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003106 }
3107 else
3108 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003109 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003110 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003111 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003112 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003113 }
3114
3115 return OK;
3116}
3117
3118/*
3119 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003120 * Returns OK if successfully edited the file, FAIL on failing to open the
3121 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3122 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003123 */
3124 static int
3125qf_jump_edit_buffer(
3126 qf_info_T *qi,
3127 qfline_T *qf_ptr,
3128 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003129 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003130 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003131{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003132 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003133 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003134 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003135 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003136 int old_qf_curlist = qi->qf_curlist;
3137 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003138
3139 if (qf_ptr->qf_type == 1)
3140 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003141 // Open help file (do_ecmd() will set b_help flag, readfile() will
3142 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003143 if (!can_abandon(curbuf, forceit))
3144 {
3145 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003146 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003147 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003148
3149 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3150 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003151 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003152 }
3153 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003154 retval = buflist_getfile(qf_ptr->qf_fnum,
3155 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003156
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003157 // If a location list, check whether the associated window is still
3158 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003159 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003160 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003161 win_T *wp = win_id2wp(prev_winid);
3162 if (wp == NULL && curwin->w_llist != qi)
3163 {
3164 emsg(_("E924: Current window was closed"));
3165 *opened_window = FALSE;
3166 return NOTDONE;
3167 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003168 }
3169
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003170 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003171 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003172 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003173 return NOTDONE;
3174 }
3175
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003176 // Check if the list was changed. The pointers may happen to be identical,
3177 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003178 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003179 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003180 || !is_qf_entry_present(qfl, qf_ptr))
3181 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003182 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003183 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003184 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003185 emsg(_(e_current_location_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003186 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003187 }
3188
3189 return retval;
3190}
3191
3192/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003193 * Go to the error line in the current file using either line/column number or
3194 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003195 */
3196 static void
3197qf_jump_goto_line(
3198 linenr_T qf_lnum,
3199 int qf_col,
3200 char_u qf_viscol,
3201 char_u *qf_pattern)
3202{
3203 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003204
3205 if (qf_pattern == NULL)
3206 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003207 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003208 i = qf_lnum;
3209 if (i > 0)
3210 {
3211 if (i > curbuf->b_ml.ml_line_count)
3212 i = curbuf->b_ml.ml_line_count;
3213 curwin->w_cursor.lnum = i;
3214 }
3215 if (qf_col > 0)
3216 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003217 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003218 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003219 coladvance(qf_col - 1);
3220 else
3221 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003222 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003223 check_cursor();
3224 }
3225 else
3226 beginline(BL_WHITE | BL_FIX);
3227 }
3228 else
3229 {
3230 pos_T save_cursor;
3231
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003232 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003233 save_cursor = curwin->w_cursor;
3234 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003235 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003236 curwin->w_cursor = save_cursor;
3237 }
3238}
3239
3240/*
3241 * Display quickfix list index and size message
3242 */
3243 static void
3244qf_jump_print_msg(
3245 qf_info_T *qi,
3246 int qf_index,
3247 qfline_T *qf_ptr,
3248 buf_T *old_curbuf,
3249 linenr_T old_lnum)
3250{
3251 linenr_T i;
3252 int len;
3253
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003254 // Update the screen before showing the message, unless the screen
3255 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003256 if (!msg_scrolled)
3257 update_topline_redraw();
3258 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003259 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003260 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3261 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003262 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003263 len = (int)STRLEN(IObuff);
3264 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3265
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003266 // Output the message. Overwrite to avoid scrolling when the 'O'
3267 // flag is present in 'shortmess'; But when not jumping, print the
3268 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003269 i = msg_scroll;
3270 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3271 msg_scroll = TRUE;
3272 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3273 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003274 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003275 msg_scroll = i;
3276}
3277
3278/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003279 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003280 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3281 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003282 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3283 * able to jump/open a window. Returns NOTDONE if a file is not associated
3284 * with the entry.
3285 */
3286 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003287qf_jump_open_window(
3288 qf_info_T *qi,
3289 qfline_T *qf_ptr,
3290 int newwin,
3291 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003292{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003293 qf_list_T *qfl = qf_get_curlist(qi);
3294 int old_changedtick = qfl->qf_changedtick;
3295 int old_qf_curlist = qi->qf_curlist;
3296 qfltype_T qfl_type = qfl->qfl_type;
3297
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003298 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003299 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3300 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003301 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003302 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003303 if (old_qf_curlist != qi->qf_curlist
3304 || old_changedtick != qfl->qf_changedtick
3305 || !is_qf_entry_present(qfl, qf_ptr))
3306 {
3307 if (qfl_type == QFLT_QUICKFIX)
3308 emsg(_(e_current_quickfix_list_was_changed));
3309 else
3310 emsg(_(e_current_location_list_was_changed));
3311 return FAIL;
3312 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003313
3314 // If currently in the quickfix window, find another window to show the
3315 // file in.
3316 if (bt_quickfix(curbuf) && !*opened_window)
3317 {
3318 // If there is no file specified, we don't know where to go.
3319 // But do advance, otherwise ":cn" gets stuck.
3320 if (qf_ptr->qf_fnum == 0)
3321 return NOTDONE;
3322
Bram Moolenaarb2443732018-11-11 22:50:27 +01003323 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3324 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003325 return FAIL;
3326 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003327 if (old_qf_curlist != qi->qf_curlist
3328 || old_changedtick != qfl->qf_changedtick
3329 || !is_qf_entry_present(qfl, qf_ptr))
3330 {
3331 if (qfl_type == QFLT_QUICKFIX)
3332 emsg(_(e_current_quickfix_list_was_changed));
3333 else
3334 emsg(_(e_current_location_list_was_changed));
3335 return FAIL;
3336 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003337
3338 return OK;
3339}
3340
3341/*
3342 * Edit a selected file from the quickfix/location list and jump to a
3343 * particular line/column, adjust the folds and display a message about the
3344 * jump.
3345 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3346 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3347 * the file.
3348 */
3349 static int
3350qf_jump_to_buffer(
3351 qf_info_T *qi,
3352 int qf_index,
3353 qfline_T *qf_ptr,
3354 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003355 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003356 int *opened_window,
3357 int openfold,
3358 int print_message)
3359{
3360 buf_T *old_curbuf;
3361 linenr_T old_lnum;
3362 int retval = OK;
3363
3364 // If there is a file name, read the wanted file if needed, and check
3365 // autowrite etc.
3366 old_curbuf = curbuf;
3367 old_lnum = curwin->w_cursor.lnum;
3368
3369 if (qf_ptr->qf_fnum != 0)
3370 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003371 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003372 opened_window);
3373 if (retval != OK)
3374 return retval;
3375 }
3376
3377 // When not switched to another buffer, still need to set pc mark
3378 if (curbuf == old_curbuf)
3379 setpcmark();
3380
3381 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3382 qf_ptr->qf_pattern);
3383
3384#ifdef FEAT_FOLDING
3385 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3386 foldOpenCursor();
3387#endif
3388 if (print_message)
3389 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3390
3391 return retval;
3392}
3393
3394/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003395 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 */
3397 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003398qf_jump(qf_info_T *qi,
3399 int dir,
3400 int errornr,
3401 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003403 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3404}
3405
3406/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003407 * Jump to a quickfix line.
3408 * If dir == 0 go to entry "errornr".
3409 * If dir == FORWARD go "errornr" valid entries forward.
3410 * If dir == BACKWARD go "errornr" valid entries backward.
3411 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3412 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3413 * else if "errornr" is zero, redisplay the same line
3414 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003415 * If 'newwin' is TRUE, then open the file in a new window.
3416 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003417 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003418qf_jump_newwin(qf_info_T *qi,
3419 int dir,
3420 int errornr,
3421 int forceit,
3422 int newwin)
3423{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003424 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003425 qfline_T *qf_ptr;
3426 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003429 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003430 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003431 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003434 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003435 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003437 if (qi == NULL)
3438 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003439
Bram Moolenaar0398e002019-03-21 21:12:49 +01003440 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003442 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 return;
3444 }
3445
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003446 incr_quickfix_busy();
3447
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003448 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003449
3450 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003452 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003454
3455 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3456 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003458 qf_ptr = old_qf_ptr;
3459 qf_index = old_qf_index;
3460 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003463 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003464 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003465 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003466 // No need to print the error message if it's visible in the error
3467 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 print_message = FALSE;
3469
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003470 prev_winid = curwin->w_id;
3471
Bram Moolenaarb2443732018-11-11 22:50:27 +01003472 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003473 if (retval == FAIL)
3474 goto failed;
3475 if (retval == NOTDONE)
3476 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003477
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003478 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003479 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003480 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003482 // Quickfix/location list is freed by an autocmd
3483 qi = NULL;
3484 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003487 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003490 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003491 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003493 // Couldn't open file, so put index back where it was. This could
3494 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 qf_ptr = old_qf_ptr;
3497 qf_index = old_qf_index;
3498 }
3499 }
3500theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003501 if (qi != NULL)
3502 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003503 qfl->qf_ptr = qf_ptr;
3504 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003505 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003506 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003508 // Restore old 'switchbuf' value, but not when an autocommand or
3509 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003510 p_swb = old_swb;
3511 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003513 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514}
3515
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003516// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003517static int qfFileAttr;
3518static int qfSepAttr;
3519static int qfLineAttr;
3520
3521/*
3522 * Display information about a single entry from the quickfix/location list.
3523 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003524 * 'cursel' will be set to TRUE for the currently selected entry in the
3525 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003526 */
3527 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003528qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003529{
3530 char_u *fname;
3531 buf_T *buf;
3532 int filter_entry;
3533
3534 fname = NULL;
3535 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3536 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3537 (char *)qfp->qf_module);
3538 else {
3539 if (qfp->qf_fnum != 0
3540 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3541 {
3542 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003543 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003544 fname = gettail(fname);
3545 }
3546 if (fname == NULL)
3547 sprintf((char *)IObuff, "%2d", qf_idx);
3548 else
3549 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3550 qf_idx, (char *)fname);
3551 }
3552
3553 // Support for filtering entries using :filter /pat/ clist
3554 // Match against the module name, file name, search pattern and
3555 // text of the entry.
3556 filter_entry = TRUE;
3557 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3558 filter_entry &= message_filtered(qfp->qf_module);
3559 if (filter_entry && fname != NULL)
3560 filter_entry &= message_filtered(fname);
3561 if (filter_entry && qfp->qf_pattern != NULL)
3562 filter_entry &= message_filtered(qfp->qf_pattern);
3563 if (filter_entry)
3564 filter_entry &= message_filtered(qfp->qf_text);
3565 if (filter_entry)
3566 return;
3567
3568 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003569 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003570
3571 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003572 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003573 if (qfp->qf_lnum == 0)
3574 IObuff[0] = NUL;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003575 else
thinca6864efa2021-06-19 20:45:20 +02003576 qf_range_text(qfp, IObuff, IOSIZE);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003577 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3578 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003579 msg_puts_attr((char *)IObuff, qfLineAttr);
3580 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003581 if (qfp->qf_pattern != NULL)
3582 {
3583 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003584 msg_puts((char *)IObuff);
3585 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003586 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003587 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003588
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003589 // Remove newlines and leading whitespace from the text. For an
3590 // unrecognized line keep the indent, the compiler may mark a word
3591 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003592 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3593 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3594 IObuff, IOSIZE);
3595 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003596 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003597}
3598
3599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003601 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 */
3603 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003604qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003606 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003607 qfline_T *qfp;
3608 int i;
3609 int idx1 = 1;
3610 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003611 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003612 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003613 int all = eap->forceit; // if not :cl!, only show
3614 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003615 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003617 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3618 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003619
Bram Moolenaar0398e002019-03-21 21:12:49 +01003620 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003622 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 return;
3624 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003625 if (*arg == '+')
3626 {
3627 ++arg;
3628 plus = TRUE;
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3631 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003632 semsg(_(e_trailing_arg), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 return;
3634 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003635 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003636 if (plus)
3637 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003638 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003639 idx2 = i + idx1;
3640 idx1 = i;
3641 }
3642 else
3643 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003644 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003645 if (idx1 < 0)
3646 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3647 if (idx2 < 0)
3648 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003651 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003652 shorten_fnames(FALSE);
3653
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003654 // Get the attributes for the different quickfix highlight items. Note
3655 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003656 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3657 if (qfFileAttr == 0)
3658 qfFileAttr = HL_ATTR(HLF_D);
3659 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3660 if (qfSepAttr == 0)
3661 qfSepAttr = HL_ATTR(HLF_D);
3662 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3663 if (qfLineAttr == 0)
3664 qfLineAttr = HL_ATTR(HLF_N);
3665
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003666 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003668 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
3670 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003671 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 ui_breakcheck();
3674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675}
3676
3677/*
3678 * Remove newlines and leading whitespace from an error message.
3679 * Put the result in "buf[bufsize]".
3680 */
3681 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003682qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 int i;
3685 char_u *p = text;
3686
3687 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3688 {
3689 if (*p == '\n')
3690 {
3691 buf[i] = ' ';
3692 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003693 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 break;
3695 }
3696 else
3697 buf[i] = *p++;
3698 }
3699 buf[i] = NUL;
3700}
3701
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003702/*
thinca6864efa2021-06-19 20:45:20 +02003703 * Range information from lnum, col, end_lnum, and end_col.
3704 * Put the result in "buf[bufsize]".
3705 */
3706 static void
3707qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3708{
3709 int len;
3710 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3711 len = (int)STRLEN(buf);
3712
3713 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3714 {
3715 vim_snprintf((char *)buf + len, bufsize - len,
3716 "-%ld", qfp->qf_end_lnum);
3717 len += (int)STRLEN(buf + len);
3718 }
3719 if (qfp->qf_col > 0)
3720 {
3721 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3722 len += (int)STRLEN(buf + len);
3723 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3724 {
3725 vim_snprintf((char *)buf + len, bufsize - len,
3726 "-%d", qfp->qf_end_col);
3727 len += (int)STRLEN(buf + len);
3728 }
3729 }
3730 buf[len] = NUL;
3731}
3732
3733/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003734 * Display information (list number, list size and the title) about a
3735 * quickfix/location list.
3736 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003737 static void
3738qf_msg(qf_info_T *qi, int which, char *lead)
3739{
3740 char *title = (char *)qi->qf_lists[which].qf_title;
3741 int count = qi->qf_lists[which].qf_count;
3742 char_u buf[IOSIZE];
3743
3744 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3745 lead,
3746 which + 1,
3747 qi->qf_listcount,
3748 count);
3749
3750 if (title != NULL)
3751 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003752 size_t len = STRLEN(buf);
3753
3754 if (len < 34)
3755 {
3756 vim_memset(buf + len, ' ', 34 - len);
3757 buf[34] = NUL;
3758 }
3759 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003760 }
3761 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003762 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003763}
3764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765/*
3766 * ":colder [count]": Up in the quickfix stack.
3767 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003768 * ":lolder [count]": Up in the location list stack.
3769 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 */
3771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003772qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003774 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 int count;
3776
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003777 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3778 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003779
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 if (eap->addr_count != 0)
3781 count = eap->line2;
3782 else
3783 count = 1;
3784 while (count--)
3785 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003786 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003788 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003790 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003791 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 else
3796 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003797 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003799 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003805 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003806 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807}
3808
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003809/*
3810 * Display the information about all the quickfix/location lists in the stack
3811 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003812 void
3813qf_history(exarg_T *eap)
3814{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003815 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003816 int i;
3817
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003818 if (eap->addr_count > 0)
3819 {
3820 if (qi == NULL)
3821 {
3822 emsg(_(e_loclist));
3823 return;
3824 }
3825
3826 // Jump to the specified quickfix list
3827 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3828 {
3829 qi->qf_curlist = eap->line2 - 1;
3830 qf_msg(qi, qi->qf_curlist, "");
3831 qf_update_buffer(qi, NULL);
3832 }
3833 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003834 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003835
3836 return;
3837 }
3838
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003839 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003840 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003841 else
3842 for (i = 0; i < qi->qf_listcount; ++i)
3843 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3844}
3845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003847 * Free all the entries in the error list "idx". Note that other information
3848 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 */
3850 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003851qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003853 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003854 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003855 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003857 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003859 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003860 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003861 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003862 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003863 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003864 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003865 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003866 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003867 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003868 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003869 // Somehow qf_count may have an incorrect value, set it to 1
3870 // to avoid crashing when it's wrong.
3871 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003872 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003873 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003874 qfl->qf_start = qfpnext;
3875 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003877
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003878 qfl->qf_index = 0;
3879 qfl->qf_start = NULL;
3880 qfl->qf_last = NULL;
3881 qfl->qf_ptr = NULL;
3882 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003883
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003884 qf_clean_dir_stack(&qfl->qf_dir_stack);
3885 qfl->qf_directory = NULL;
3886 qf_clean_dir_stack(&qfl->qf_file_stack);
3887 qfl->qf_currfile = NULL;
3888 qfl->qf_multiline = FALSE;
3889 qfl->qf_multiignore = FALSE;
3890 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891}
3892
3893/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003894 * Free error list "idx". Frees all the entries in the quickfix list,
3895 * associated context information and the title.
3896 */
3897 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003898qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003899{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003900 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003901
Bram Moolenaard23a8232018-02-10 18:45:26 +01003902 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003903 free_tv(qfl->qf_ctx);
3904 qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02003905 free_callback(&qfl->qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003906 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003907 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003908}
3909
3910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 * qf_mark_adjust: adjust marks
3912 */
3913 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003914qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003915 win_T *wp,
3916 linenr_T line1,
3917 linenr_T line2,
3918 long amount,
3919 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003921 int i;
3922 qfline_T *qfp;
3923 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003924 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003925 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003926 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaarc1542742016-07-20 21:44:37 +02003928 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003929 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003930 if (wp != NULL)
3931 {
3932 if (wp->w_llist == NULL)
3933 return;
3934 qi = wp->w_llist;
3935 }
3936
3937 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003938 {
3939 qf_list_T *qfl = qf_get_list(qi, idx);
3940
3941 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003942 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 if (qfp->qf_fnum == curbuf->b_fnum)
3944 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003945 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3947 {
3948 if (amount == MAXLNUM)
3949 qfp->qf_cleared = TRUE;
3950 else
3951 qfp->qf_lnum += amount;
3952 }
3953 else if (amount_after && qfp->qf_lnum > line2)
3954 qfp->qf_lnum += amount_after;
3955 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003956 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003957
3958 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003959 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960}
3961
3962/*
3963 * Make a nice message out of the error character and the error number:
3964 * char number message
3965 * e or E 0 " error"
3966 * w or W 0 " warning"
3967 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02003968 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 * 0 0 ""
3970 * other 0 " c"
3971 * e or E n " error n"
3972 * w or W n " warning n"
3973 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02003974 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 * 0 n " error n"
3976 * other n " c n"
3977 * 1 x "" :helpgrep
3978 */
3979 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003980qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
3982 static char_u buf[20];
3983 static char_u cc[3];
3984 char_u *p;
3985
3986 if (c == 'W' || c == 'w')
3987 p = (char_u *)" warning";
3988 else if (c == 'I' || c == 'i')
3989 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02003990 else if (c == 'N' || c == 'n')
3991 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3993 p = (char_u *)" error";
3994 else if (c == 0 || c == 1)
3995 p = (char_u *)"";
3996 else
3997 {
3998 cc[0] = ' ';
3999 cc[1] = c;
4000 cc[2] = NUL;
4001 p = cc;
4002 }
4003
4004 if (nr <= 0)
4005 return p;
4006
4007 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4008 return buf;
4009}
4010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004012 * When "split" is FALSE: Open the entry/result under the cursor.
4013 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4014 */
4015 void
4016qf_view_result(int split)
4017{
4018 qf_info_T *qi = &ql_info;
4019
4020 if (!bt_quickfix(curbuf))
4021 return;
4022
4023 if (IS_LL_WINDOW(curwin))
4024 qi = GET_LOC_LIST(curwin);
4025
Bram Moolenaar0398e002019-03-21 21:12:49 +01004026 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004027 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004028 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004029 return;
4030 }
4031
4032 if (split)
4033 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004034 // Open the selected entry in a new window
4035 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4036 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004037 return;
4038 }
4039
4040 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4041}
4042
4043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 * ":cwindow": open the quickfix window if we have errors to display,
4045 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004046 * ":lwindow": open the location list window if we have locations to display,
4047 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 */
4049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004050ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004052 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004053 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 win_T *win;
4055
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004056 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4057 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004058
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004059 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004060
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004061 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004062 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004064 // If a quickfix window is open but we have no errors to display,
4065 // close the window. If a quickfix window is not open, then open
4066 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004067 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004068 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004069 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
4071 if (win != NULL)
4072 ex_cclose(eap);
4073 }
4074 else if (win == NULL)
4075 ex_copen(eap);
4076}
4077
4078/*
4079 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004080 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004083ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004085 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004086 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004088 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4089 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004091 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004092 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (win != NULL)
4094 win_close(win, FALSE);
4095}
4096
4097/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004098 * Set "w:quickfix_title" if "qi" has a title.
4099 */
4100 static void
4101qf_set_title_var(qf_list_T *qfl)
4102{
4103 if (qfl->qf_title != NULL)
4104 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4105}
4106
4107/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004108 * Goto a quickfix or location list window (if present).
4109 * Returns OK if the window is found, FAIL otherwise.
4110 */
4111 static int
4112qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4113{
4114 win_T *win;
4115
4116 win = qf_find_win(qi);
4117 if (win == NULL)
4118 return FAIL;
4119
4120 win_goto(win);
4121 if (resize)
4122 {
4123 if (vertsplit)
4124 {
4125 if (sz != win->w_width)
4126 win_setwidth(sz);
4127 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004128 else if (sz != win->w_height && win->w_height
4129 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004130 win_setheight(sz);
4131 }
4132
4133 return OK;
4134}
4135
4136/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004137 * Set options for the buffer in the quickfix or location list window.
4138 */
4139 static void
4140qf_set_cwindow_options(void)
4141{
4142 // switch off 'swapfile'
4143 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4144 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4145 OPT_LOCAL);
4146 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4147 RESET_BINDING(curwin);
4148#ifdef FEAT_DIFF
4149 curwin->w_p_diff = FALSE;
4150#endif
4151#ifdef FEAT_FOLDING
4152 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4153 OPT_LOCAL);
4154#endif
4155}
4156
4157/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004158 * Open a new quickfix or location list window, load the quickfix buffer and
4159 * set the appropriate options for the window.
4160 * Returns FAIL if the window could not be opened.
4161 */
4162 static int
4163qf_open_new_cwindow(qf_info_T *qi, int height)
4164{
4165 buf_T *qf_buf;
4166 win_T *oldwin = curwin;
4167 tabpage_T *prevtab = curtab;
4168 int flags = 0;
4169 win_T *win;
4170
4171 qf_buf = qf_find_buf(qi);
4172
4173 // The current window becomes the previous window afterwards.
4174 win = curwin;
4175
Bram Moolenaare1004402020-10-24 20:49:43 +02004176 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004177 // Create the new quickfix window at the very bottom, except when
4178 // :belowright or :aboveleft is used.
4179 win_goto(lastwin);
4180 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004181 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004182 flags = WSP_BELOW;
4183 flags |= WSP_NEWLOC;
4184 if (win_split(height, flags) == FAIL)
4185 return FAIL; // not enough room for window
4186 RESET_BINDING(curwin);
4187
4188 if (IS_LL_STACK(qi))
4189 {
4190 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004191 // location list stack from the window 'win'.
4192 curwin->w_llist_ref = qi;
4193 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004194 }
4195
4196 if (oldwin != curwin)
4197 oldwin = NULL; // don't store info when in another window
4198 if (qf_buf != NULL)
4199 {
4200 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004201 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004202 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004203 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004204 }
4205 else
4206 {
4207 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004208 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4209 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004210 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004211
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004212 // save the number of the new buffer
4213 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004214 }
4215
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004216 // Set the options for the quickfix buffer/window (if not already done)
4217 // Do this even if the quickfix buffer was already present, as an autocmd
4218 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004219 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004220 qf_set_cwindow_options();
4221
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004222 // Only set the height when still in the same tab page and there is no
4223 // window to the side.
4224 if (curtab == prevtab && curwin->w_width == Columns)
4225 win_setheight(height);
4226 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4227 if (win_valid(win))
4228 prevwin = win;
4229
4230 return OK;
4231}
4232
4233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004235 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 */
4237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004238ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004240 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004241 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004243 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004244 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004246 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4247 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004248
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004249 incr_quickfix_busy();
4250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 if (eap->addr_count != 0)
4252 height = eap->line2;
4253 else
4254 height = QF_WINHEIGHT;
4255
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004256 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257#ifdef FEAT_GUI
4258 need_mouse_correct = TRUE;
4259#endif
4260
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004261 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004262 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004263 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004264 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004265 if (status == FAIL)
4266 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004267 {
4268 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004269 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004272 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004273 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004274 // Save the current index here, as updating the quickfix buffer may free
4275 // the quickfix list
4276 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004277
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004278 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004279 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004281 decr_quickfix_busy();
4282
4283 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 curwin->w_cursor.col = 0;
4285 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004286 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287}
4288
4289/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004290 * Move the cursor in the quickfix window to "lnum".
4291 */
4292 static void
4293qf_win_goto(win_T *win, linenr_T lnum)
4294{
4295 win_T *old_curwin = curwin;
4296
4297 curwin = win;
4298 curbuf = win->w_buffer;
4299 curwin->w_cursor.lnum = lnum;
4300 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004301 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004302 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004303 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004304 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004305 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004306 curwin = old_curwin;
4307 curbuf = curwin->w_buffer;
4308}
4309
4310/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004311 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004312 */
4313 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004314ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004315{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004316 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004317 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004318
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004319 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4320 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004321
4322 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004323 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4324 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4325}
4326
4327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 * Return the number of the current entry (line number in the quickfix
4329 * window).
4330 */
4331 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004332qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004334 qf_info_T *qi = &ql_info;
4335
4336 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004337 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004338 qi = wp->w_llist_ref;
4339
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004340 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341}
4342
4343/*
4344 * Update the cursor position in the quickfix window to the current error.
4345 * Return TRUE if there is a quickfix window.
4346 */
4347 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004348qf_win_pos_update(
4349 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004350 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
4352 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004353 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004355 // Put the cursor on the current error in the quickfix window, so that
4356 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004357 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (win != NULL
4359 && qf_index <= win->w_buffer->b_ml.ml_line_count
4360 && old_qf_index != qf_index)
4361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (qf_index > old_qf_index)
4363 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004364 win->w_redraw_top = old_qf_index;
4365 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 else
4368 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004369 win->w_redraw_top = qf_index;
4370 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004372 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 return win != NULL;
4375}
4376
4377/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004378 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004379 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004380 */
4381 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004382is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004383{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004384 // A window displaying the quickfix buffer will have the w_llist_ref field
4385 // set to NULL.
4386 // A window displaying a location list buffer will have the w_llist_ref
4387 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004388 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004389 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4390 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004391 return TRUE;
4392
4393 return FALSE;
4394}
4395
4396/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004397 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4398 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004399 */
4400 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004401qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004402{
4403 win_T *win;
4404
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004405 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004406 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004407 return win;
4408 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004409}
4410
4411/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004412 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004413 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 */
4415 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004416qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004418 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004419 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004421 if (qi->qf_bufnr != INVALID_QFBUFNR)
4422 {
4423 buf_T *qfbuf;
4424 qfbuf = buflist_findnr(qi->qf_bufnr);
4425 if (qfbuf != NULL)
4426 return qfbuf;
4427 // buffer is no longer present
4428 qi->qf_bufnr = INVALID_QFBUFNR;
4429 }
4430
Bram Moolenaar9c102382006-05-03 21:26:49 +00004431 FOR_ALL_TAB_WINDOWS(tp, win)
4432 if (is_qf_win(win, qi))
4433 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004434
Bram Moolenaar9c102382006-05-03 21:26:49 +00004435 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436}
4437
4438/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004439 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004440 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004441 */
4442 int
4443qf_process_qftf_option(void)
4444{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004445 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004446}
4447
4448/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004449 * Update the w:quickfix_title variable in the quickfix/location list window in
4450 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004451 */
4452 static void
4453qf_update_win_titlevar(qf_info_T *qi)
4454{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004455 qf_list_T *qfl = qf_get_curlist(qi);
4456 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004457 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004458 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004459
Bram Moolenaar530bed92020-12-16 21:02:56 +01004460 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004461 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004462 if (is_qf_win(win, qi))
4463 {
4464 curwin = win;
4465 qf_set_title_var(qfl);
4466 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004467 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004468 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004469}
4470
4471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 * Find the quickfix buffer. If it exists, update the contents.
4473 */
4474 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004475qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
4477 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004478 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004481 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004482 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (buf != NULL)
4484 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004485 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004486 int qf_winid = 0;
4487
4488 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004489 {
4490 if (curwin->w_llist == qi)
4491 win = curwin;
4492 else
4493 {
4494 win = qf_find_win_with_loclist(qi);
4495 if (win == NULL)
4496 return;
4497 }
4498 qf_winid = win->w_id;
4499 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004500
4501 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004502 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004503 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaard823fa92016-08-12 16:29:27 +02004505 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004506
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004507 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004508 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004509
Bram Moolenaar864293a2016-06-02 13:40:04 +02004510 if (old_last == NULL)
4511 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004512 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004513
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004514 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004515 aucmd_restbuf(&aco);
4516 }
4517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004518 // Only redraw when added lines are visible. This avoids flickering
4519 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004520 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4521 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523}
4524
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004525/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004526 * Add an error line to the quickfix buffer.
4527 */
4528 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004529qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004530 buf_T *buf, // quickfix window buffer
4531 linenr_T lnum,
4532 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004533 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004534 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004535 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004536{
4537 int len;
4538 buf_T *errbuf;
4539
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004540 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004541 // for this entry, then use it.
4542 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004543 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004544 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004545 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004546 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004547 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004548 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4549 len = (int)STRLEN(IObuff);
4550 }
4551 else if (qfp->qf_fnum != 0
4552 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4553 && errbuf->b_fname != NULL)
4554 {
4555 if (qfp->qf_type == 1) // :helpgrep
4556 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4557 else
4558 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004559 // Shorten the file name if not done already.
4560 // For optimization, do this only for the first entry in a
4561 // buffer.
4562 if (first_bufline && (errbuf->b_sfname == NULL
4563 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004564 {
4565 if (*dirname == NUL)
4566 mch_dirname(dirname, MAXPATHL);
4567 shorten_buf_fname(errbuf, dirname, FALSE);
4568 }
4569 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4570 }
4571 len = (int)STRLEN(IObuff);
4572 }
4573 else
4574 len = 0;
4575
4576 if (len < IOSIZE - 1)
4577 IObuff[len++] = '|';
4578
4579 if (qfp->qf_lnum > 0)
4580 {
thinca6864efa2021-06-19 20:45:20 +02004581 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004582 len += (int)STRLEN(IObuff + len);
4583
Bram Moolenaar858ba062020-05-31 23:11:59 +02004584 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4585 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004586 len += (int)STRLEN(IObuff + len);
4587 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004588 else if (qfp->qf_pattern != NULL)
4589 {
4590 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4591 len += (int)STRLEN(IObuff + len);
4592 }
4593 if (len < IOSIZE - 2)
4594 {
4595 IObuff[len++] = '|';
4596 IObuff[len++] = ' ';
4597 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004598
Bram Moolenaar858ba062020-05-31 23:11:59 +02004599 // Remove newlines and leading whitespace from the text.
4600 // For an unrecognized line keep the indent, the compiler may
4601 // mark a word with ^^^^.
4602 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4603 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004604 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004605
4606 if (ml_append_buf(buf, lnum, IObuff,
4607 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4608 return FAIL;
4609
4610 return OK;
4611}
4612
Bram Moolenaard43906d2020-07-20 21:31:32 +02004613/*
4614 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4615 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4616 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004617 static list_T *
4618call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4619{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004620 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004621 list_T *qftf_list = NULL;
4622
4623 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4624 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4625 // set.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004626 if (qfl->qftf_cb.cb_name != NULL)
4627 cb = &qfl->qftf_cb;
4628 if (cb != NULL && cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004629 {
4630 typval_T args[1];
4631 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004632 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004633
4634 // create the dict argument
4635 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4636 return NULL;
4637 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4638 dict_add_number(d, "winid", (long)qf_winid);
4639 dict_add_number(d, "id", (long)qfl->qf_id);
4640 dict_add_number(d, "start_idx", start_idx);
4641 dict_add_number(d, "end_idx", end_idx);
4642 ++d->dv_refcount;
4643 args[0].v_type = VAR_DICT;
4644 args[0].vval.v_dict = d;
4645
Bram Moolenaard43906d2020-07-20 21:31:32 +02004646 qftf_list = NULL;
4647 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4648 {
4649 if (rettv.v_type == VAR_LIST)
4650 {
4651 qftf_list = rettv.vval.v_list;
4652 qftf_list->lv_refcount++;
4653 }
4654 clear_tv(&rettv);
4655 }
4656 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004657 }
4658
4659 return qftf_list;
4660}
4661
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 * Fill current buffer with quickfix errors, replacing any previous contents.
4664 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004665 * If "old_last" is not NULL append the items after this one.
4666 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4667 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 */
4669 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004670qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004672 linenr_T lnum;
4673 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004674 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004675 list_T *qftf_list = NULL;
4676 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
Bram Moolenaar864293a2016-06-02 13:40:04 +02004678 if (old_last == NULL)
4679 {
4680 if (buf != curbuf)
4681 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004682 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004683 return;
4684 }
4685
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004686 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004687 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004688 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004691 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004692 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004694 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004695 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004696 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004697
4698 *dirname = NUL;
4699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004700 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004701 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004702 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004703 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004704 lnum = 0;
4705 }
4706 else
4707 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004708 if (old_last->qf_next != NULL)
4709 qfp = old_last->qf_next;
4710 else
4711 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004712 lnum = buf->b_ml.ml_line_count;
4713 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004714
4715 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4716 (long)qfl->qf_count);
4717 if (qftf_list != NULL)
4718 qftf_li = qftf_list->lv_first;
4719
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004720 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004722 char_u *qftf_str = NULL;
4723
Bram Moolenaard43906d2020-07-20 21:31:32 +02004724 // Use the text supplied by the user defined function (if any).
4725 // If the returned value is not string, then ignore the rest
4726 // of the returned values and use the default.
4727 if (qftf_li != NULL && !invalid_val)
4728 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004729 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004730 if (qftf_str == NULL)
4731 invalid_val = TRUE;
4732 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004733
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004734 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4735 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004737
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004738 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004739 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004741 if (qfp == NULL)
4742 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004743
4744 if (qftf_li != NULL)
4745 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004747
4748 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004749 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004750 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004753 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 check_lnums(TRUE);
4755
Bram Moolenaar864293a2016-06-02 13:40:04 +02004756 if (old_last == NULL)
4757 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004758 // Set the 'filetype' to "qf" each time after filling the buffer.
4759 // This resembles reading a file into a buffer, it's more logical when
4760 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004761 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004762 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4763 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004765 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004766 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004768 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004770 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004771 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004773 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004774 redraw_curbuf_later(NOT_VALID);
4775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004777 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 KeyTyped = old_KeyTyped;
4779}
4780
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004781/*
4782 * For every change made to the quickfix list, update the changed tick.
4783 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004784 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004785qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004786{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004787 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004788}
4789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004791 * Return the quickfix/location list number with the given identifier.
4792 * Returns -1 if list is not found.
4793 */
4794 static int
4795qf_id2nr(qf_info_T *qi, int_u qfid)
4796{
4797 int qf_idx;
4798
4799 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4800 if (qi->qf_lists[qf_idx].qf_id == qfid)
4801 return qf_idx;
4802 return INVALID_QFIDX;
4803}
4804
4805/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004806 * If the current list is not "save_qfid" and we can find the list with that ID
4807 * then make it the current list.
4808 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004809 * Returns OK if successfully restored the list. Returns FAIL if the list with
4810 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004811 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004812 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004813qf_restore_list(qf_info_T *qi, int_u save_qfid)
4814{
4815 int curlist;
4816
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004817 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004818 {
4819 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004820 if (curlist < 0)
4821 // list is not present
4822 return FAIL;
4823 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004824 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004825 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004826}
4827
4828/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004829 * Jump to the first entry if there is one.
4830 */
4831 static void
4832qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4833{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004834 if (qf_restore_list(qi, save_qfid) == FAIL)
4835 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004836
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004837 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004838 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004839 qf_jump(qi, 0, 0, forceit);
4840}
4841
4842/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004843 * Return TRUE when using ":vimgrep" for ":grep".
4844 */
4845 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004846grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004847{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004848 return ((cmdidx == CMD_grep
4849 || cmdidx == CMD_lgrep
4850 || cmdidx == CMD_grepadd
4851 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004852 && STRCMP("internal",
4853 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4854}
4855
4856/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004857 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004859 static char_u *
4860make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004862 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004863 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004864 case CMD_make: return (char_u *)"make";
4865 case CMD_lmake: return (char_u *)"lmake";
4866 case CMD_grep: return (char_u *)"grep";
4867 case CMD_lgrep: return (char_u *)"lgrep";
4868 case CMD_grepadd: return (char_u *)"grepadd";
4869 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4870 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872}
4873
4874/*
4875 * Return the name for the errorfile, in allocated memory.
4876 * Find a new unique name when 'makeef' contains "##".
4877 * Returns NULL for error.
4878 */
4879 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004880get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
4882 char_u *p;
4883 char_u *name;
4884 static int start = -1;
4885 static int off = 0;
4886#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004887 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#endif
4889
4890 if (*p_mef == NUL)
4891 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004892 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004894 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 return name;
4896 }
4897
4898 for (p = p_mef; *p; ++p)
4899 if (p[0] == '#' && p[1] == '#')
4900 break;
4901
4902 if (*p == NUL)
4903 return vim_strsave(p_mef);
4904
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004905 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 for (;;)
4907 {
4908 if (start == -1)
4909 start = mch_get_pid();
4910 else
4911 off += 19;
4912
Bram Moolenaar964b3742019-05-24 18:54:09 +02004913 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (name == NULL)
4915 break;
4916 STRCPY(name, p_mef);
4917 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4918 STRCAT(name, p + 2);
4919 if (mch_getperm(name) < 0
4920#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004921 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 && mch_lstat((char *)name, &sb) < 0
4923#endif
4924 )
4925 break;
4926 vim_free(name);
4927 }
4928 return name;
4929}
4930
4931/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004932 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4933 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4934 */
4935 static char_u *
4936make_get_fullcmd(char_u *makecmd, char_u *fname)
4937{
4938 char_u *cmd;
4939 unsigned len;
4940
4941 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4942 if (*p_sp != NUL)
4943 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4944 cmd = alloc(len);
4945 if (cmd == NULL)
4946 return NULL;
4947 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4948 (char *)p_shq);
4949
4950 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4951 if (*p_sp != NUL)
4952 append_redir(cmd, len, p_sp, fname);
4953
4954 // Display the fully formed command. Output a newline if there's something
4955 // else than the :make command that was typed (in which case the cursor is
4956 // in column 0).
4957 if (msg_col == 0)
4958 msg_didout = FALSE;
4959 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004960 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004961 msg_outtrans(cmd); // show what we are doing
4962
4963 return cmd;
4964}
4965
4966/*
4967 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4968 */
4969 void
4970ex_make(exarg_T *eap)
4971{
4972 char_u *fname;
4973 char_u *cmd;
4974 char_u *enc = NULL;
4975 win_T *wp = NULL;
4976 qf_info_T *qi = &ql_info;
4977 int res;
4978 char_u *au_name = NULL;
4979 int_u save_qfid;
4980
4981 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4982 if (grep_internal(eap->cmdidx))
4983 {
4984 ex_vimgrep(eap);
4985 return;
4986 }
4987
4988 au_name = make_get_auname(eap->cmdidx);
4989 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4990 curbuf->b_fname, TRUE, curbuf))
4991 {
4992#ifdef FEAT_EVAL
4993 if (aborting())
4994 return;
4995#endif
4996 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004997 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004998
4999 if (is_loclist_cmd(eap->cmdidx))
5000 wp = curwin;
5001
5002 autowrite_all();
5003 fname = get_mef_name();
5004 if (fname == NULL)
5005 return;
5006 mch_remove(fname); // in case it's not unique
5007
5008 cmd = make_get_fullcmd(eap->arg, fname);
5009 if (cmd == NULL)
5010 return;
5011
5012 // let the shell know if we are redirecting output or not
5013 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5014
5015#ifdef AMIGA
5016 out_flush();
5017 // read window status report and redraw before message
5018 (void)char_avail();
5019#endif
5020
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005021 incr_quickfix_busy();
5022
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005023 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
5024 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
5025 (eap->cmdidx != CMD_grepadd
5026 && eap->cmdidx != CMD_lgrepadd),
5027 qf_cmdtitle(*eap->cmdlinep), enc);
5028 if (wp != NULL)
5029 {
5030 qi = GET_LOC_LIST(wp);
5031 if (qi == NULL)
5032 goto cleanup;
5033 }
5034 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005035 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005036
5037 // Remember the current quickfix list identifier, so that we can
5038 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005039 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005040 if (au_name != NULL)
5041 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5042 curbuf->b_fname, TRUE, curbuf);
5043 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5044 // display the first error
5045 qf_jump_first(qi, save_qfid, FALSE);
5046
5047cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005048 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005049 mch_remove(fname);
5050 vim_free(fname);
5051 vim_free(cmd);
5052}
5053
5054/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005055 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005056 */
5057 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005058qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005059{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005060 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005061
5062 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5063 return 0;
5064 return qf_get_curlist(qi)->qf_count;
5065}
5066
5067/*
5068 * Returns the number of valid entries in the current quickfix/location list.
5069 */
5070 int
5071qf_get_valid_size(exarg_T *eap)
5072{
5073 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005074 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005075 qfline_T *qfp;
5076 int i, sz = 0;
5077 int prev_fnum = 0;
5078
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005079 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5080 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005081
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005082 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005083 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005084 {
5085 if (qfp->qf_valid)
5086 {
5087 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005088 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005089 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5090 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005091 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005092 sz++;
5093 prev_fnum = qfp->qf_fnum;
5094 }
5095 }
5096 }
5097
5098 return sz;
5099}
5100
5101/*
5102 * Returns the current index of the quickfix/location list.
5103 * Returns 0 if there is an error.
5104 */
5105 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005106qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005107{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005108 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005109
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005110 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5111 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005112
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005113 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005114}
5115
5116/*
5117 * Returns the current index in the quickfix/location list (counting only valid
5118 * entries). If no valid entries are in the list, then returns 1.
5119 */
5120 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005121qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005122{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005123 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005124 qf_list_T *qfl;
5125 qfline_T *qfp;
5126 int i, eidx = 0;
5127 int prev_fnum = 0;
5128
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005129 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5130 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005131
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005132 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005133 qfp = qfl->qf_start;
5134
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005135 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005136 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005137 return 1;
5138
5139 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5140 {
5141 if (qfp->qf_valid)
5142 {
5143 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5144 {
5145 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5146 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005147 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005148 eidx++;
5149 prev_fnum = qfp->qf_fnum;
5150 }
5151 }
5152 else
5153 eidx++;
5154 }
5155 }
5156
5157 return eidx ? eidx : 1;
5158}
5159
5160/*
5161 * Get the 'n'th valid error entry in the quickfix or location list.
5162 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5163 * For :cdo and :ldo returns the 'n'th valid error entry.
5164 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5165 */
5166 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005167qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005168{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005169 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005170 int i, eidx;
5171 int prev_fnum = 0;
5172
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005173 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005174 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005175 return 1;
5176
Bram Moolenaar95946f12019-03-31 15:31:59 +02005177 eidx = 0;
5178 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005179 {
5180 if (qfp->qf_valid)
5181 {
5182 if (fdo)
5183 {
5184 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5185 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005186 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005187 eidx++;
5188 prev_fnum = qfp->qf_fnum;
5189 }
5190 }
5191 else
5192 eidx++;
5193 }
5194
5195 if (eidx == n)
5196 break;
5197 }
5198
5199 if (i <= qfl->qf_count)
5200 return i;
5201 else
5202 return 1;
5203}
5204
5205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005207 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005208 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 */
5210 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005211ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005213 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005214 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005215
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005216 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5217 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005218
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005219 if (eap->addr_count > 0)
5220 errornr = (int)eap->line2;
5221 else
5222 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005223 switch (eap->cmdidx)
5224 {
5225 case CMD_cc: case CMD_ll:
5226 errornr = 0;
5227 break;
5228 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5229 case CMD_lfirst:
5230 errornr = 1;
5231 break;
5232 default:
5233 errornr = 32767;
5234 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005235 }
5236
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005237 // For cdo and ldo commands, jump to the nth valid error.
5238 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005239 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5240 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005241 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005242 eap->addr_count > 0 ? (int)eap->line1 : 1,
5243 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5244
5245 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246}
5247
5248/*
5249 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005250 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005251 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 */
5253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005254ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005256 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005257 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005258 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005259
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005260 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5261 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005262
Bram Moolenaar55b69262017-08-13 13:42:01 +02005263 if (eap->addr_count > 0
5264 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5265 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005266 errornr = (int)eap->line2;
5267 else
5268 errornr = 1;
5269
Bram Moolenaar39665952018-08-15 20:59:48 +02005270 // Depending on the command jump to either next or previous entry/file.
5271 switch (eap->cmdidx)
5272 {
5273 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5274 dir = FORWARD;
5275 break;
5276 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5277 case CMD_lNext:
5278 dir = BACKWARD;
5279 break;
5280 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5281 dir = FORWARD_FILE;
5282 break;
5283 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5284 dir = BACKWARD_FILE;
5285 break;
5286 default:
5287 dir = FORWARD;
5288 break;
5289 }
5290
5291 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292}
5293
5294/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005295 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5296 * The index of the entry is stored in 'errornr'.
5297 * Returns NULL if an entry is not found.
5298 */
5299 static qfline_T *
5300qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5301{
5302 qfline_T *qfp = NULL;
5303 int idx = 0;
5304
5305 // Find the first entry in this file
5306 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5307 if (qfp->qf_fnum == bnr)
5308 break;
5309
5310 *errornr = idx;
5311 return qfp;
5312}
5313
5314/*
5315 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5316 * with the error number for the first entry. Assumes the entries are sorted in
5317 * the quickfix list by line number.
5318 */
5319 static qfline_T *
5320qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5321{
5322 while (!got_int
5323 && entry->qf_prev != NULL
5324 && entry->qf_fnum == entry->qf_prev->qf_fnum
5325 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5326 {
5327 entry = entry->qf_prev;
5328 --*errornr;
5329 }
5330
5331 return entry;
5332}
5333
5334/*
5335 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5336 * with the error number for the last entry. Assumes the entries are sorted in
5337 * the quickfix list by line number.
5338 */
5339 static qfline_T *
5340qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5341{
5342 while (!got_int &&
5343 entry->qf_next != NULL
5344 && entry->qf_fnum == entry->qf_next->qf_fnum
5345 && entry->qf_lnum == entry->qf_next->qf_lnum)
5346 {
5347 entry = entry->qf_next;
5348 ++*errornr;
5349 }
5350
5351 return entry;
5352}
5353
5354/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005355 * Returns TRUE if the specified quickfix entry is
5356 * after the given line (linewise is TRUE)
5357 * or after the line and column.
5358 */
5359 static int
5360qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5361{
5362 if (linewise)
5363 return qfp->qf_lnum > pos->lnum;
5364 else
5365 return (qfp->qf_lnum > pos->lnum ||
5366 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5367}
5368
5369/*
5370 * Returns TRUE if the specified quickfix entry is
5371 * before the given line (linewise is TRUE)
5372 * or before the line and column.
5373 */
5374 static int
5375qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5376{
5377 if (linewise)
5378 return qfp->qf_lnum < pos->lnum;
5379 else
5380 return (qfp->qf_lnum < pos->lnum ||
5381 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5382}
5383
5384/*
5385 * Returns TRUE if the specified quickfix entry is
5386 * on or after the given line (linewise is TRUE)
5387 * or on or after the line and column.
5388 */
5389 static int
5390qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5391{
5392 if (linewise)
5393 return qfp->qf_lnum >= pos->lnum;
5394 else
5395 return (qfp->qf_lnum > pos->lnum ||
5396 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5397}
5398
5399/*
5400 * Returns TRUE if the specified quickfix entry is
5401 * on or before the given line (linewise is TRUE)
5402 * or on or before the line and column.
5403 */
5404 static int
5405qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5406{
5407 if (linewise)
5408 return qfp->qf_lnum <= pos->lnum;
5409 else
5410 return (qfp->qf_lnum < pos->lnum ||
5411 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5412}
5413
5414/*
5415 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5416 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5417 * multiple entries on a single line as one. Otherwise returns the entry after
5418 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005419 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5420 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005421 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005422 */
5423 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005424qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005425 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005426 pos_T *pos,
5427 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005428 qfline_T *qfp,
5429 int *errornr)
5430{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005431 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005432 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005433 return qfp;
5434
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005435 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005436 while (qfp->qf_next != NULL
5437 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005438 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005439 {
5440 qfp = qfp->qf_next;
5441 ++*errornr;
5442 }
5443
5444 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005445 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005446 return NULL;
5447
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005448 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005449 qfp = qfp->qf_next;
5450 ++*errornr;
5451
5452 return qfp;
5453}
5454
5455/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005456 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5457 * If 'linewise' is TRUE, returns the entry before the specified line and
5458 * treats multiple entries on a single line as one. Otherwise returns the entry
5459 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005460 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5461 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005462 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005463 */
5464 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005465qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005466 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005467 pos_T *pos,
5468 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005469 qfline_T *qfp,
5470 int *errornr)
5471{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005472 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005473 while (qfp->qf_next != NULL
5474 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005475 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005476 {
5477 qfp = qfp->qf_next;
5478 ++*errornr;
5479 }
5480
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005481 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005482 return NULL;
5483
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005484 if (linewise)
5485 // If multiple entries are on the same line, then use the first entry
5486 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005487
5488 return qfp;
5489}
5490
5491/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005492 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005493 * the direction 'dir'.
5494 */
5495 static qfline_T *
5496qf_find_closest_entry(
5497 qf_list_T *qfl,
5498 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005499 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005500 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005501 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005502 int *errornr)
5503{
5504 qfline_T *qfp;
5505
5506 *errornr = 0;
5507
5508 // Find the first entry in this file
5509 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5510 if (qfp == NULL)
5511 return NULL; // no entry in this file
5512
5513 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005514 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005515 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005516 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005517
5518 return qfp;
5519}
5520
5521/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005522 * Get the nth quickfix entry below the specified entry. Searches forward in
5523 * the list. If linewise is TRUE, then treat multiple entries on a single line
5524 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005525 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005526 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005527qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005528{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005529 qfline_T *entry = entry_arg;
5530
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005531 while (n-- > 0 && !got_int)
5532 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005533 int first_errornr = *errornr;
5534
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005535 if (linewise)
5536 // Treat all the entries on the same line in this file as one
5537 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005538
5539 if (entry->qf_next == NULL
5540 || entry->qf_next->qf_fnum != entry->qf_fnum)
5541 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005542 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005543 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005544 break;
5545 }
5546
5547 entry = entry->qf_next;
5548 ++*errornr;
5549 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005550}
5551
5552/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005553 * Get the nth quickfix entry above the specified entry. Searches backwards in
5554 * the list. If linewise is TRUE, then treat multiple entries on a single line
5555 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005556 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005557 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005558qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005559{
5560 while (n-- > 0 && !got_int)
5561 {
5562 if (entry->qf_prev == NULL
5563 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5564 break;
5565
5566 entry = entry->qf_prev;
5567 --*errornr;
5568
5569 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005570 if (linewise)
5571 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005572 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005573}
5574
5575/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005576 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5577 * the specified direction. Returns the error number in the quickfix list or 0
5578 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005579 */
5580 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005581qf_find_nth_adj_entry(
5582 qf_list_T *qfl,
5583 int bnr,
5584 pos_T *pos,
5585 int n,
5586 int dir,
5587 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005588{
5589 qfline_T *adj_entry;
5590 int errornr;
5591
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005592 // Find an entry closest to the specified position
5593 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005594 if (adj_entry == NULL)
5595 return 0;
5596
5597 if (--n > 0)
5598 {
5599 // Go to the n'th entry in the current buffer
5600 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005601 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005602 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005603 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005604 }
5605
5606 return errornr;
5607}
5608
5609/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005610 * Jump to a quickfix entry in the current file nearest to the current line or
5611 * current line/col.
5612 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5613 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005614 */
5615 void
5616ex_cbelow(exarg_T *eap)
5617{
5618 qf_info_T *qi;
5619 qf_list_T *qfl;
5620 int dir;
5621 int buf_has_flag;
5622 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005623 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005624
5625 if (eap->addr_count > 0 && eap->line2 <= 0)
5626 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005627 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005628 return;
5629 }
5630
5631 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005632 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5633 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005634 buf_has_flag = BUF_HAS_QF_ENTRY;
5635 else
5636 buf_has_flag = BUF_HAS_LL_ENTRY;
5637 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5638 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005639 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005640 return;
5641 }
5642
5643 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5644 return;
5645
5646 qfl = qf_get_curlist(qi);
5647 // check if the list has valid errors
5648 if (!qf_list_has_valid_entries(qfl))
5649 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005650 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005651 return;
5652 }
5653
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005654 if (eap->cmdidx == CMD_cbelow
5655 || eap->cmdidx == CMD_lbelow
5656 || eap->cmdidx == CMD_cafter
5657 || eap->cmdidx == CMD_lafter)
5658 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005659 dir = FORWARD;
5660 else
5661 dir = BACKWARD;
5662
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005663 pos = curwin->w_cursor;
5664 // A quickfix entry column number is 1 based whereas cursor column
5665 // number is 0 based. Adjust the column number.
5666 pos.col++;
5667 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5668 eap->addr_count > 0 ? eap->line2 : 0, dir,
5669 eap->cmdidx == CMD_cbelow
5670 || eap->cmdidx == CMD_lbelow
5671 || eap->cmdidx == CMD_cabove
5672 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005673
5674 if (errornr > 0)
5675 qf_jump(qi, 0, errornr, FALSE);
5676 else
5677 emsg(_(e_no_more_items));
5678}
5679
5680/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005681 * Return the autocmd name for the :cfile Ex commands
5682 */
5683 static char_u *
5684cfile_get_auname(cmdidx_T cmdidx)
5685{
5686 switch (cmdidx)
5687 {
5688 case CMD_cfile: return (char_u *)"cfile";
5689 case CMD_cgetfile: return (char_u *)"cgetfile";
5690 case CMD_caddfile: return (char_u *)"caddfile";
5691 case CMD_lfile: return (char_u *)"lfile";
5692 case CMD_lgetfile: return (char_u *)"lgetfile";
5693 case CMD_laddfile: return (char_u *)"laddfile";
5694 default: return NULL;
5695 }
5696}
5697
5698/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005699 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005700 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 */
5702 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005703ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005705 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005706 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005707 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005708 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005709 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005710 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005711
Bram Moolenaara16123a2019-03-28 20:31:07 +01005712 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005713 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5714 NULL, FALSE, curbuf))
5715 {
5716#ifdef FEAT_EVAL
5717 if (aborting())
5718 return;
5719#endif
5720 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005721
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005722 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005723#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005724 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005725 {
5726 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005727 NULL, NULL,
5728 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005729 if (browse_file == NULL)
5730 return;
5731 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5732 vim_free(browse_file);
5733 }
5734 else
5735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005737 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005738
Bram Moolenaar39665952018-08-15 20:59:48 +02005739 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005740 wp = curwin;
5741
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005742 incr_quickfix_busy();
5743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005744 // This function is used by the :cfile, :cgetfile and :caddfile
5745 // commands.
5746 // :cfile always creates a new quickfix list and jumps to the
5747 // first error.
5748 // :cgetfile creates a new quickfix list but doesn't jump to the
5749 // first error.
5750 // :caddfile adds to an existing quickfix list. If there is no
5751 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005752 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005753 && eap->cmdidx != CMD_laddfile),
5754 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005755 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005756 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005757 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005758 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005759 {
5760 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005761 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005762 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005763 }
5764 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005765 qf_list_changed(qf_get_curlist(qi));
5766 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005767 if (au_name != NULL)
5768 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005769
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005770 // Jump to the first error for a new list and if autocmds didn't
5771 // free the list.
5772 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5773 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005774 // display the first error
5775 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005776
5777 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005778}
5779
5780/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005781 * Return the vimgrep autocmd name.
5782 */
5783 static char_u *
5784vgr_get_auname(cmdidx_T cmdidx)
5785{
5786 switch (cmdidx)
5787 {
5788 case CMD_vimgrep: return (char_u *)"vimgrep";
5789 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5790 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5791 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5792 case CMD_grep: return (char_u *)"grep";
5793 case CMD_lgrep: return (char_u *)"lgrep";
5794 case CMD_grepadd: return (char_u *)"grepadd";
5795 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5796 default: return NULL;
5797 }
5798}
5799
5800/*
5801 * Initialize the regmatch used by vimgrep for pattern "s".
5802 */
5803 static void
5804vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5805{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005806 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005807 regmatch->regprog = NULL;
5808
5809 if (s == NULL || *s == NUL)
5810 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005811 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005812 if (last_search_pat() == NULL)
5813 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005814 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005815 return;
5816 }
5817 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5818 }
5819 else
5820 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5821
5822 regmatch->rmm_ic = p_ic;
5823 regmatch->rmm_maxcol = 0;
5824}
5825
5826/*
5827 * Display a file name when vimgrep is running.
5828 */
5829 static void
5830vgr_display_fname(char_u *fname)
5831{
5832 char_u *p;
5833
5834 msg_start();
5835 p = msg_strtrunc(fname, TRUE);
5836 if (p == NULL)
5837 msg_outtrans(fname);
5838 else
5839 {
5840 msg_outtrans(p);
5841 vim_free(p);
5842 }
5843 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005844 msg_didout = FALSE; // overwrite this message
5845 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005846 msg_col = 0;
5847 out_flush();
5848}
5849
5850/*
5851 * Load a dummy buffer to search for a pattern using vimgrep.
5852 */
5853 static buf_T *
5854vgr_load_dummy_buf(
5855 char_u *fname,
5856 char_u *dirname_start,
5857 char_u *dirname_now)
5858{
5859 int save_mls;
5860#if defined(FEAT_SYN_HL)
5861 char_u *save_ei = NULL;
5862#endif
5863 buf_T *buf;
5864
5865#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005866 // Don't do Filetype autocommands to avoid loading syntax and
5867 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005868 save_ei = au_event_disable(",Filetype");
5869#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005870 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005871 save_mls = p_mls;
5872 p_mls = 0;
5873
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005874 // Load file into a buffer, so that 'fileencoding' is detected,
5875 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005876 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5877
5878 p_mls = save_mls;
5879#if defined(FEAT_SYN_HL)
5880 au_event_restore(save_ei);
5881#endif
5882
5883 return buf;
5884}
5885
5886/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005887 * Check whether a quickfix/location list is valid. Autocmds may remove or
5888 * change a quickfix list when vimgrep is running. If the list is not found,
5889 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005890 */
5891 static int
5892vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005893 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005894 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005895 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005896 char_u *title)
5897{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005898 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005899 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005900 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005901 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005902 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005903 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005904 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005905 return FALSE;
5906 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005907 else
5908 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005909 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005910 qf_new_list(qi, title);
5911 return TRUE;
5912 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005913 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005914
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005915 if (qf_restore_list(qi, qfid) == FAIL)
5916 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005917
5918 return TRUE;
5919}
5920
5921/*
5922 * Search for a pattern in all the lines in a buffer and add the matching lines
5923 * to a quickfix list.
5924 */
5925 static int
5926vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005927 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005928 char_u *fname,
5929 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005930 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005931 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005932 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005933 int duplicate_name,
5934 int flags)
5935{
5936 int found_match = FALSE;
5937 long lnum;
5938 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02005939 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005940
Bram Moolenaar1c299432018-10-28 14:36:09 +01005941 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005942 {
5943 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005944 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005945 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005946 // Regular expression match
5947 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5948 col, NULL, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005949 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005950 // Pass the buffer number so that it gets used even for a
5951 // dummy buffer, unless duplicate_name is set, then the
5952 // buffer will be wiped out below.
5953 if (qf_add_entry(qfl,
5954 NULL, // dir
5955 fname,
5956 NULL,
5957 duplicate_name ? 0 : buf->b_fnum,
5958 ml_get_buf(buf,
5959 regmatch->startpos[0].lnum + lnum, FALSE),
5960 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02005961 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005962 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02005963 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005964 FALSE, // vis_col
5965 NULL, // search pattern
5966 0, // nr
5967 0, // type
5968 TRUE // valid
5969 ) == QF_FAIL)
5970 {
5971 got_int = TRUE;
5972 break;
5973 }
5974 found_match = TRUE;
5975 if (--*tomatch == 0)
5976 break;
5977 if ((flags & VGR_GLOBAL) == 0
5978 || regmatch->endpos[0].lnum > 0)
5979 break;
5980 col = regmatch->endpos[0].col
5981 + (col == regmatch->endpos[0].col);
5982 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5983 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005984 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005985 }
5986 else
5987 {
5988 char_u *str = ml_get_buf(buf, lnum, FALSE);
5989 int score;
5990 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02005991 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005992
5993 // Fuzzy string match
5994 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
5995 {
5996 // Pass the buffer number so that it gets used even for a
5997 // dummy buffer, unless duplicate_name is set, then the
5998 // buffer will be wiped out below.
5999 if (qf_add_entry(qfl,
6000 NULL, // dir
6001 fname,
6002 NULL,
6003 duplicate_name ? 0 : buf->b_fnum,
6004 str,
6005 lnum,
thinca6864efa2021-06-19 20:45:20 +02006006 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006007 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006008 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006009 FALSE, // vis_col
6010 NULL, // search pattern
6011 0, // nr
6012 0, // type
6013 TRUE // valid
6014 ) == QF_FAIL)
6015 {
6016 got_int = TRUE;
6017 break;
6018 }
6019 found_match = TRUE;
6020 if (--*tomatch == 0)
6021 break;
6022 if ((flags & VGR_GLOBAL) == 0)
6023 break;
6024 col = matches[pat_len - 1] + col + 1;
6025 if (col > (colnr_T)STRLEN(str))
6026 break;
6027 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006028 }
6029 line_breakcheck();
6030 if (got_int)
6031 break;
6032 }
6033
6034 return found_match;
6035}
6036
6037/*
6038 * Jump to the first match and update the directory.
6039 */
6040 static void
6041vgr_jump_to_match(
6042 qf_info_T *qi,
6043 int forceit,
6044 int *redraw_for_dummy,
6045 buf_T *first_match_buf,
6046 char_u *target_dir)
6047{
6048 buf_T *buf;
6049
6050 buf = curbuf;
6051 qf_jump(qi, 0, 0, forceit);
6052 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006053 // If we jumped to another buffer redrawing will already be
6054 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006055 *redraw_for_dummy = FALSE;
6056
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006057 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006058 if (curbuf == first_match_buf && target_dir != NULL)
6059 {
6060 exarg_T ea;
6061
Bram Moolenaara80faa82020-04-12 19:37:17 +02006062 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006063 ea.arg = target_dir;
6064 ea.cmdidx = CMD_lcd;
6065 ex_cd(&ea);
6066 }
6067}
6068
6069/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006070 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006071 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006072typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006073{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006074 long tomatch; // maximum number of matches to find
6075 char_u *spat; // search pattern
6076 int flags; // search modifier
6077 char_u **fnames; // list of files to search
6078 int fcount; // number of files
6079 regmmatch_T regmatch; // compiled search pattern
6080 char_u *qf_title; // quickfix list title
6081} vgr_args_T;
6082
6083/*
6084 * Process :vimgrep command arguments. The command syntax is:
6085 *
6086 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6087 */
6088 static int
6089vgr_process_args(
6090 exarg_T *eap,
6091 vgr_args_T *args)
6092{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006093 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006094
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006095 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006096
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006097 args->regmatch.regprog = NULL;
6098 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006099
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006100 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006101 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006102 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006103 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006104
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006105 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006106 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006107 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006109 emsg(_(e_invalpat));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006110 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006111 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006112
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006113 vgr_init_regmatch(&args->regmatch, args->spat);
6114 if (args->regmatch.regprog == NULL)
6115 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006116
6117 p = skipwhite(p);
6118 if (*p == NUL)
6119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006120 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006121 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006122 }
6123
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006124 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006125 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6126 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006128 emsg(_(e_nomatch));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006129 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006130 }
6131
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006132 return OK;
6133}
6134
6135/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006136 * Return TRUE if "buf" had an existing swap file, the current swap file does
6137 * not end in ".swp".
6138 */
6139 static int
6140existing_swapfile(buf_T *buf)
6141{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006142 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006143 {
6144 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6145 size_t len = STRLEN(fname);
6146
6147 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6148 }
6149 return FALSE;
6150}
6151
6152/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006153 * Search for a pattern in a list of files and populate the quickfix list with
6154 * the matches.
6155 */
6156 static int
6157vgr_process_files(
6158 win_T *wp,
6159 qf_info_T *qi,
6160 vgr_args_T *cmd_args,
6161 int *redraw_for_dummy,
6162 buf_T **first_match_buf,
6163 char_u **target_dir)
6164{
6165 int status = FAIL;
6166 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6167 time_t seconds = 0;
6168 char_u *fname;
6169 int fi;
6170 buf_T *buf;
6171 int duplicate_name = FALSE;
6172 int using_dummy;
6173 char_u *dirname_start = NULL;
6174 char_u *dirname_now = NULL;
6175 int found_match;
6176 aco_save_T aco;
6177
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006178 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6179 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006180 if (dirname_start == NULL || dirname_now == NULL)
6181 goto theend;
6182
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006183 // Remember the current directory, because a BufRead autocommand that does
6184 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006185 mch_dirname(dirname_start, MAXPATHL);
6186
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006187 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006188 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6189 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006190 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006191 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006192 if (time(NULL) > seconds)
6193 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006194 // Display the file name every second or so, show the user we are
6195 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006196 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006197 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006198 }
6199
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006200 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006201 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6202 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006203 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006204 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006205 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006206 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006207
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006208 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006209 }
6210 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006211 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006212 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006213
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006214 // Check whether the quickfix list is still valid. When loading a
6215 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006216 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006217 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006218
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006219 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006220
Bram Moolenaar81695252004-12-29 20:58:21 +00006221 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006222 {
6223 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006224 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006225 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006226 else
6227 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006228 // Try for a match in all lines of the buffer.
6229 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006230 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006231 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006232 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006233
Bram Moolenaar81695252004-12-29 20:58:21 +00006234 if (using_dummy)
6235 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006236 if (found_match && *first_match_buf == NULL)
6237 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006238 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006239 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006240 // Never keep a dummy buffer if there is another buffer
6241 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006242 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006243 buf = NULL;
6244 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006245 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006246 || buf->b_p_bh[0] == 'u' // "unload"
6247 || buf->b_p_bh[0] == 'w' // "wipe"
6248 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006249 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006250 // When no match was found we don't need to remember the
6251 // buffer, wipe it out. If there was a match and it
6252 // wasn't the first one or we won't jump there: only
6253 // unload the buffer.
6254 // Ignore 'hidden' here, because it may lead to having too
6255 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006256 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006257 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006258 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006259 buf = NULL;
6260 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006261 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006262 || (cmd_args->flags & VGR_NOJUMP)
6263 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006264 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006265 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006266 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006267 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006268 buf = NULL;
6269 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006270 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006271
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006272 if (buf != NULL)
6273 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006274 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006275 buf->b_flags &= ~BF_DUMMY;
6276
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006277 // If the buffer is still loaded we need to use the
6278 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006279 if (buf == *first_match_buf
6280 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006281 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006282 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006283
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006284 // The buffer is still loaded, the Filetype autocommands
6285 // need to be done now, in that buffer. And the modelines
6286 // need to be done (again). But not the window-local
6287 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006288 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006289#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006290 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6291 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006292#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006293 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006294 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006295 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006296 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006297 }
6298 }
6299
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006300 status = OK;
6301
6302theend:
6303 vim_free(dirname_now);
6304 vim_free(dirname_start);
6305 return status;
6306}
6307
6308/*
6309 * ":vimgrep {pattern} file(s)"
6310 * ":vimgrepadd {pattern} file(s)"
6311 * ":lvimgrep {pattern} file(s)"
6312 * ":lvimgrepadd {pattern} file(s)"
6313 */
6314 void
6315ex_vimgrep(exarg_T *eap)
6316{
6317 vgr_args_T args;
6318 qf_info_T *qi;
6319 qf_list_T *qfl;
6320 int_u save_qfid;
6321 win_T *wp = NULL;
6322 int redraw_for_dummy = FALSE;
6323 buf_T *first_match_buf = NULL;
6324 char_u *target_dir = NULL;
6325 char_u *au_name = NULL;
6326 int status;
6327
6328 au_name = vgr_get_auname(eap->cmdidx);
6329 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6330 curbuf->b_fname, TRUE, curbuf))
6331 {
6332#ifdef FEAT_EVAL
6333 if (aborting())
6334 return;
6335#endif
6336 }
6337
6338 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6339 if (qi == NULL)
6340 return;
6341
6342 if (vgr_process_args(eap, &args) == FAIL)
6343 goto theend;
6344
6345 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6346 && eap->cmdidx != CMD_vimgrepadd
6347 && eap->cmdidx != CMD_lvimgrepadd)
6348 || qf_stack_empty(qi))
6349 // make place for a new list
6350 qf_new_list(qi, args.qf_title);
6351
6352 incr_quickfix_busy();
6353
6354 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6355 &first_match_buf, &target_dir);
6356 if (status != OK)
6357 {
6358 FreeWild(args.fcount, args.fnames);
6359 decr_quickfix_busy();
6360 goto theend;
6361 }
6362
6363 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006364
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006365 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006366 qfl->qf_nonevalid = FALSE;
6367 qfl->qf_ptr = qfl->qf_start;
6368 qfl->qf_index = 1;
6369 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006370
Bram Moolenaar864293a2016-06-02 13:40:04 +02006371 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006372
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006373 // Remember the current quickfix list identifier, so that we can check for
6374 // autocommands changing the current quickfix list.
6375 save_qfid = qf_get_curlist(qi)->qf_id;
6376
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006377 if (au_name != NULL)
6378 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6379 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006380 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6381 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006382 if (!qflist_valid(wp, save_qfid)
6383 || qf_restore_list(qi, save_qfid) == FAIL)
6384 {
6385 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006386 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006387 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006388
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006389 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006390 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006391 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006392 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006393 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6394 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006395 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006396 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006397 semsg(_(e_nomatch2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006398
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006399 decr_quickfix_busy();
6400
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006401 // If we loaded a dummy buffer into the current window, the autocommands
6402 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006403 if (redraw_for_dummy)
6404 {
6405#ifdef FEAT_FOLDING
6406 foldUpdateAll(curwin);
6407#else
6408 redraw_later(NOT_VALID);
6409#endif
6410 }
6411
Bram Moolenaar86b68352004-12-27 21:59:20 +00006412theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006413 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006414 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006415 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006416}
6417
6418/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006419 * Restore current working directory to "dirname_start" if they differ, taking
6420 * into account whether it is set locally or globally.
6421 */
6422 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006423restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006424{
6425 char_u *dirname_now = alloc(MAXPATHL);
6426
6427 if (NULL != dirname_now)
6428 {
6429 mch_dirname(dirname_now, MAXPATHL);
6430 if (STRCMP(dirname_start, dirname_now) != 0)
6431 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006432 // If the directory has changed, change it back by building up an
6433 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006434 exarg_T ea;
6435
Bram Moolenaara80faa82020-04-12 19:37:17 +02006436 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006437 ea.arg = dirname_start;
6438 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6439 ex_cd(&ea);
6440 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006441 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006442 }
6443}
6444
6445/*
6446 * Load file "fname" into a dummy buffer and return the buffer pointer,
6447 * placing the directory resulting from the buffer load into the
6448 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6449 * prior to calling this function. Restores directory to "dirname_start" prior
6450 * to returning, if autocmds or the 'autochdir' option have changed it.
6451 *
6452 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6453 * or wipe_dummy_buffer() later!
6454 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006455 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006456 */
6457 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006458load_dummy_buffer(
6459 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006460 char_u *dirname_start, // in: old directory
6461 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006462{
6463 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006464 bufref_T newbufref;
6465 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006466 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006467 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006468 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006469
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006470 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006471 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6472 if (newbuf == NULL)
6473 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006474 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006475
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006476 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006477 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6478
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006479 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006480 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006481 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006482 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006483 ++newbuf->b_locked;
6484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006485 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006486 aucmd_prepbuf(&aco, newbuf);
6487
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006488 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006489 (void)setfname(curbuf, fname, NULL, FALSE);
6490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006491 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006492 check_need_swap(TRUE);
6493
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006494 // Remove the "dummy" flag, otherwise autocommands may not
6495 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006496 curbuf->b_flags &= ~BF_DUMMY;
6497
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006498 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006499 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006500 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006501 NULL, READ_NEW | READ_DUMMY);
6502 --newbuf->b_locked;
6503 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006504 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006505 && !(curbuf->b_flags & BF_NEW))
6506 {
6507 failed = FALSE;
6508 if (curbuf != newbuf)
6509 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006510 // Bloody autocommands changed the buffer! Can happen when
6511 // using netrw and editing a remote file. Use the current
6512 // buffer instead, delete the dummy one after restoring the
6513 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006514 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006515 newbuf = curbuf;
6516 }
6517 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006518
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006519 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006520 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006521 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6522 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006524 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6525 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006526 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006527 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006528
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006529 // When autocommands/'autochdir' option changed directory: go back.
6530 // Let the caller know what the resulting dir was first, in case it is
6531 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006532 mch_dirname(resulting_dir, MAXPATHL);
6533 restore_start_dir(dirname_start);
6534
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006535 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006536 return NULL;
6537 if (failed)
6538 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006539 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006540 return NULL;
6541 }
6542 return newbuf;
6543}
6544
6545/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006546 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6547 * directory to "dirname_start" prior to returning, if autocmds or the
6548 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006549 */
6550 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006551wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006552{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006553 // If any autocommand opened a window on the dummy buffer, close that
6554 // window. If we can't close them all then give up.
6555 while (buf->b_nwindows > 0)
6556 {
6557 int did_one = FALSE;
6558 win_T *wp;
6559
6560 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006561 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006562 if (wp->w_buffer == buf)
6563 {
6564 if (win_close(wp, FALSE) == OK)
6565 did_one = TRUE;
6566 break;
6567 }
6568 if (!did_one)
6569 return;
6570 }
6571
6572 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006573 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006574#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006575 cleanup_T cs;
6576
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006577 // Reset the error/interrupt/exception state here so that aborting()
6578 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6579 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006580 enter_cleanup(&cs);
6581#endif
6582
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006583 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006584
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006585#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006586 // Restore the error/interrupt/exception state if not discarded by a
6587 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006588 leave_cleanup(&cs);
6589#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006590 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006591 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006592 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006593}
6594
6595/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006596 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6597 * directory to "dirname_start" prior to returning, if autocmds or the
6598 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006599 */
6600 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006601unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006602{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006603 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006604 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006605 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006606
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006607 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006608 restore_start_dir(dirname_start);
6609 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006610}
6611
Bram Moolenaar05159a02005-02-26 23:04:13 +00006612#if defined(FEAT_EVAL) || defined(PROTO)
6613/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006614 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006615 * to 'list'. Returns OK on success.
6616 */
6617 static int
6618get_qfline_items(qfline_T *qfp, list_T *list)
6619{
6620 int bufnum;
6621 dict_T *dict;
6622 char_u buf[2];
6623
6624 // Handle entries with a non-existing buffer number.
6625 bufnum = qfp->qf_fnum;
6626 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6627 bufnum = 0;
6628
6629 if ((dict = dict_alloc()) == NULL)
6630 return FAIL;
6631 if (list_append_dict(list, dict) == FAIL)
6632 return FAIL;
6633
6634 buf[0] = qfp->qf_type;
6635 buf[1] = NUL;
6636 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006637 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6638 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6639 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6640 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6641 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6642 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006643 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6644 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6645 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6646 || dict_add_string(dict, "type", buf) == FAIL
6647 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6648 return FAIL;
6649
6650 return OK;
6651}
6652
6653/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006654 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006655 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006656 * If eidx is not 0, then return only the specified entry. Otherwise return
6657 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006658 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006659 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006660get_errorlist(
6661 qf_info_T *qi_arg,
6662 win_T *wp,
6663 int qf_idx,
6664 int eidx,
6665 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006666{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006667 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006668 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006669 qfline_T *qfp;
6670 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006671
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006672 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006673 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006674 qi = &ql_info;
6675 if (wp != NULL)
6676 {
6677 qi = GET_LOC_LIST(wp);
6678 if (qi == NULL)
6679 return FAIL;
6680 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006681 }
6682
Bram Moolenaar858ba062020-05-31 23:11:59 +02006683 if (eidx < 0)
6684 return OK;
6685
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006686 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006687 qf_idx = qi->qf_curlist;
6688
Bram Moolenaar0398e002019-03-21 21:12:49 +01006689 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006690 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006691
Bram Moolenaar0398e002019-03-21 21:12:49 +01006692 qfl = qf_get_list(qi, qf_idx);
6693 if (qf_list_empty(qfl))
6694 return FAIL;
6695
Bram Moolenaara16123a2019-03-28 20:31:07 +01006696 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006697 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006698 if (eidx > 0)
6699 {
6700 if (eidx == i)
6701 return get_qfline_items(qfp, list);
6702 }
6703 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006704 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006705 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006706
Bram Moolenaar05159a02005-02-26 23:04:13 +00006707 return OK;
6708}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006709
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006710// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006711enum {
6712 QF_GETLIST_NONE = 0x0,
6713 QF_GETLIST_TITLE = 0x1,
6714 QF_GETLIST_ITEMS = 0x2,
6715 QF_GETLIST_NR = 0x4,
6716 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006717 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006718 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006719 QF_GETLIST_IDX = 0x40,
6720 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006721 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006722 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006723 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006724 QF_GETLIST_QFTF = 0x800,
6725 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006726};
6727
6728/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006729 * Parse text from 'di' and return the quickfix list items.
6730 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006731 */
6732 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006733qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006734{
6735 int status = FAIL;
6736 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006737 char_u *errorformat = p_efm;
6738 dictitem_T *efm_di;
6739 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006740
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006741 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006742 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006744 // If errorformat is supplied then use it, otherwise use the 'efm'
6745 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006746 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6747 {
6748 if (efm_di->di_tv.v_type != VAR_STRING ||
6749 efm_di->di_tv.vval.v_string == NULL)
6750 return FAIL;
6751 errorformat = efm_di->di_tv.vval.v_string;
6752 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006753
Bram Moolenaar36538222017-09-02 19:51:44 +02006754 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006755 if (l == NULL)
6756 return FAIL;
6757
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006758 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006759 if (qi != NULL)
6760 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006761 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006762 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6763 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006764 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006765 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006766 }
6767 free(qi);
6768 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006769 dict_add_list(retdict, "items", l);
6770 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006771 }
6772
6773 return status;
6774}
6775
6776/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006777 * Return the quickfix/location list window identifier in the current tabpage.
6778 */
6779 static int
6780qf_winid(qf_info_T *qi)
6781{
6782 win_T *win;
6783
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006784 // The quickfix window can be opened even if the quickfix list is not set
6785 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006786 if (qi == NULL)
6787 return 0;
6788 win = qf_find_win(qi);
6789 if (win != NULL)
6790 return win->w_id;
6791 return 0;
6792}
6793
6794/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006795 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006796 * window. If there is no buffer associated with the list or the buffer is
6797 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006798 */
6799 static int
6800qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6801{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006802 int bufnum = 0;
6803
6804 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6805 bufnum = qi->qf_bufnr;
6806
6807 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006808}
6809
6810/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006811 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006812 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006813 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006814qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006815{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006816 int flags = QF_GETLIST_NONE;
6817
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006818 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006819 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006820 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006821 if (!loclist)
6822 // File window ID is applicable only to location list windows
6823 flags &= ~ QF_GETLIST_FILEWINID;
6824 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006825
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006826 if (dict_find(what, (char_u *)"title", -1) != NULL)
6827 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006828
Bram Moolenaara6d48492017-12-12 22:45:31 +01006829 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6830 flags |= QF_GETLIST_NR;
6831
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006832 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6833 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006834
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006835 if (dict_find(what, (char_u *)"context", -1) != NULL)
6836 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006837
Bram Moolenaara6d48492017-12-12 22:45:31 +01006838 if (dict_find(what, (char_u *)"id", -1) != NULL)
6839 flags |= QF_GETLIST_ID;
6840
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006841 if (dict_find(what, (char_u *)"items", -1) != NULL)
6842 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006843
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006844 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6845 flags |= QF_GETLIST_IDX;
6846
6847 if (dict_find(what, (char_u *)"size", -1) != NULL)
6848 flags |= QF_GETLIST_SIZE;
6849
Bram Moolenaarb254af32017-12-18 19:48:58 +01006850 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6851 flags |= QF_GETLIST_TICK;
6852
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006853 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6854 flags |= QF_GETLIST_FILEWINID;
6855
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006856 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6857 flags |= QF_GETLIST_QFBUFNR;
6858
Bram Moolenaard43906d2020-07-20 21:31:32 +02006859 if (dict_find(what, (char_u *)"quickfixtextfunc", -1) != NULL)
6860 flags |= QF_GETLIST_QFTF;
6861
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006862 return flags;
6863}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006864
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006865/*
6866 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6867 * If 'nr' and 'id' are not present in 'what' then return the current
6868 * quickfix list index.
6869 * If 'nr' is zero then return the current quickfix list index.
6870 * If 'nr' is '$' then return the last quickfix list index.
6871 * If 'id' is present then return the index of the quickfix list with that id.
6872 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6873 * Return -1, if quickfix list is not present or if the stack is empty.
6874 */
6875 static int
6876qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6877{
6878 int qf_idx;
6879 dictitem_T *di;
6880
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006881 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006882 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6883 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006884 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006885 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006886 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006887 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006888 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006889 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006890 qf_idx = di->di_tv.vval.v_number - 1;
6891 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006892 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006893 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006894 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006895 else if (di->di_tv.v_type == VAR_STRING
6896 && di->di_tv.vval.v_string != NULL
6897 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006898 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006899 qf_idx = qi->qf_listcount - 1;
6900 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006901 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006902 }
6903
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006904 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6905 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006906 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006907 if (di->di_tv.v_type == VAR_NUMBER)
6908 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006909 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006910 if (di->di_tv.vval.v_number != 0)
6911 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6912 }
6913 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006914 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006915 }
6916
6917 return qf_idx;
6918}
6919
6920/*
6921 * Return default values for quickfix list properties in retdict.
6922 */
6923 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006924qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006925{
6926 int status = OK;
6927
6928 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006929 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006930 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6931 {
6932 list_T *l = list_alloc();
6933 if (l != NULL)
6934 status = dict_add_list(retdict, "items", l);
6935 else
6936 status = FAIL;
6937 }
6938 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006939 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006940 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006941 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006942 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006943 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006944 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006945 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006946 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006947 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006948 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006949 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006950 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006951 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006952 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006953 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006954 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6955 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02006956 if ((status == OK) && (flags & QF_GETLIST_QFTF))
6957 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006958
6959 return status;
6960}
6961
6962/*
6963 * Return the quickfix list title as 'title' in retdict
6964 */
6965 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006966qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006967{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006968 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006969}
6970
6971/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006972 * Returns the identifier of the window used to display files from a location
6973 * list. If there is no associated window, then returns 0. Useful only when
6974 * called from a location list window.
6975 */
6976 static int
6977qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6978{
6979 int winid = 0;
6980
6981 if (wp != NULL && IS_LL_WINDOW(wp))
6982 {
6983 win_T *ll_wp = qf_find_win_with_loclist(qi);
6984 if (ll_wp != NULL)
6985 winid = ll_wp->w_id;
6986 }
6987
6988 return dict_add_number(retdict, "filewinid", winid);
6989}
6990
6991/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006992 * Return the quickfix list items/entries as 'items' in retdict.
6993 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006994 */
6995 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006996qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006997{
6998 int status = OK;
6999 list_T *l = list_alloc();
7000 if (l != NULL)
7001 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007002 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007003 dict_add_list(retdict, "items", l);
7004 }
7005 else
7006 status = FAIL;
7007
7008 return status;
7009}
7010
7011/*
7012 * Return the quickfix list context (if any) as 'context' in retdict.
7013 */
7014 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007015qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007016{
7017 int status;
7018 dictitem_T *di;
7019
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007020 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007021 {
7022 di = dictitem_alloc((char_u *)"context");
7023 if (di != NULL)
7024 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007025 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007026 status = dict_add(retdict, di);
7027 if (status == FAIL)
7028 dictitem_free(di);
7029 }
7030 else
7031 status = FAIL;
7032 }
7033 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007034 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007035
7036 return status;
7037}
7038
7039/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007040 * Return the current quickfix list index as 'idx' in retdict.
7041 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007042 */
7043 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007044qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007045{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007046 if (eidx == 0)
7047 {
7048 eidx = qfl->qf_index;
7049 if (qf_list_empty(qfl))
7050 // For empty lists, current index is set to 0
7051 eidx = 0;
7052 }
7053 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007054}
7055
7056/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007057 * Return the 'quickfixtextfunc' function of a quickfix/location list
7058 */
7059 static int
7060qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7061{
7062 int status;
7063
7064 if (qfl->qftf_cb.cb_name != NULL)
7065 {
7066 typval_T tv;
7067
7068 put_callback(&qfl->qftf_cb, &tv);
7069 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7070 clear_tv(&tv);
7071 }
7072 else
7073 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7074
7075 return status;
7076}
7077
7078/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007079 * Return quickfix/location list details (title) as a
7080 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7081 * then current list is used. Otherwise the specified list is used.
7082 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007083 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007084qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7085{
7086 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007087 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007088 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007089 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007090 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007091 dictitem_T *di;
7092 int flags = QF_GETLIST_NONE;
7093
7094 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7095 return qf_get_list_from_lines(what, di, retdict);
7096
7097 if (wp != NULL)
7098 qi = GET_LOC_LIST(wp);
7099
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007100 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007101
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007102 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007103 qf_idx = qf_getprop_qfidx(qi, what);
7104
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007105 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007106 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007107 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007108
Bram Moolenaar0398e002019-03-21 21:12:49 +01007109 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007110
Bram Moolenaar858ba062020-05-31 23:11:59 +02007111 // If an entry index is specified, use that
7112 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7113 {
7114 if (di->di_tv.v_type != VAR_NUMBER)
7115 return FAIL;
7116 eidx = di->di_tv.vval.v_number;
7117 }
7118
Bram Moolenaard823fa92016-08-12 16:29:27 +02007119 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007120 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007121 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007122 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007123 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007124 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007125 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007126 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007127 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007128 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007129 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007130 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007131 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007132 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007133 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007134 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007135 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007136 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007137 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7138 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007139 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7140 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007141 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7142 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007143
Bram Moolenaard823fa92016-08-12 16:29:27 +02007144 return status;
7145}
7146
7147/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007148 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007149 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7150 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007151 */
7152 static int
7153qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007154 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007155 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007156 int first_entry,
7157 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007158{
7159 static int did_bufnr_emsg;
7160 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007161 int bufnum, valid, status, col, end_col, vcol, nr;
7162 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007163
7164 if (first_entry)
7165 did_bufnr_emsg = FALSE;
7166
Bram Moolenaar8f667172018-12-14 15:38:31 +01007167 filename = dict_get_string(d, (char_u *)"filename", TRUE);
7168 module = dict_get_string(d, (char_u *)"module", TRUE);
7169 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
7170 lnum = (int)dict_get_number(d, (char_u *)"lnum");
thinca6864efa2021-06-19 20:45:20 +02007171 end_lnum = (int)dict_get_number(d, (char_u *)"end_lnum");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007172 col = (int)dict_get_number(d, (char_u *)"col");
thinca6864efa2021-06-19 20:45:20 +02007173 end_col = (int)dict_get_number(d, (char_u *)"end_col");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007174 vcol = (int)dict_get_number(d, (char_u *)"vcol");
7175 nr = (int)dict_get_number(d, (char_u *)"nr");
7176 type = dict_get_string(d, (char_u *)"type", TRUE);
7177 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
7178 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007179 if (text == NULL)
7180 text = vim_strsave((char_u *)"");
7181
7182 valid = TRUE;
7183 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7184 valid = FALSE;
7185
7186 // Mark entries with non-existing buffer number as not valid. Give the
7187 // error message only once.
7188 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7189 {
7190 if (!did_bufnr_emsg)
7191 {
7192 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007193 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007194 }
7195 valid = FALSE;
7196 bufnum = 0;
7197 }
7198
7199 // If the 'valid' field is present it overrules the detected value.
7200 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar401f0c02020-09-05 22:37:39 +02007201 valid = (int)dict_get_bool(d, (char_u *)"valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007202
Bram Moolenaar0398e002019-03-21 21:12:49 +01007203 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007204 NULL, // dir
7205 filename,
7206 module,
7207 bufnum,
7208 text,
7209 lnum,
thinca6864efa2021-06-19 20:45:20 +02007210 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007211 col,
thinca6864efa2021-06-19 20:45:20 +02007212 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007213 vcol, // vis_col
7214 pattern, // search pattern
7215 nr,
7216 type == NULL ? NUL : *type,
7217 valid);
7218
7219 vim_free(filename);
7220 vim_free(module);
7221 vim_free(pattern);
7222 vim_free(text);
7223 vim_free(type);
7224
Bram Moolenaar9752c722018-12-22 16:49:34 +01007225 if (valid)
7226 *valid_entry = TRUE;
7227
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007228 return status;
7229}
7230
7231/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007232 * Add list of entries to quickfix/location list. Each list entry is
7233 * a dictionary with item information.
7234 */
7235 static int
7236qf_add_entries(
7237 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007238 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007239 list_T *list,
7240 char_u *title,
7241 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007242{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007243 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007244 listitem_T *li;
7245 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007246 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007247 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007248 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007249
Bram Moolenaara3921f42017-06-04 15:30:34 +02007250 if (action == ' ' || qf_idx == qi->qf_listcount)
7251 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007252 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007253 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007254 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007255 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007256 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007257 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007258 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007259 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007260 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007261 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007262 qf_free_items(qfl);
7263 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007264 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007265
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007266 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007267 {
7268 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007269 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007270
7271 d = li->li_tv.vval.v_dict;
7272 if (d == NULL)
7273 continue;
7274
Bram Moolenaar0398e002019-03-21 21:12:49 +01007275 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007276 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007277 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007278 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007279 }
7280
Bram Moolenaar9752c722018-12-22 16:49:34 +01007281 // Check if any valid error entries are added to the list.
7282 if (valid_entry)
7283 qfl->qf_nonevalid = FALSE;
7284 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007285 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007286 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007287
7288 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007289 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007290 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007291
7292 // Update the current error index if not appending to the list or if the
7293 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007294 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007295 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007296
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007297 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007298 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007299
7300 return retval;
7301}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007302
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007303/*
7304 * Get the quickfix list index from 'nr' or 'id'
7305 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007306 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007307qf_setprop_get_qfidx(
7308 qf_info_T *qi,
7309 dict_T *what,
7310 int action,
7311 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007312{
7313 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007314 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007315
Bram Moolenaard823fa92016-08-12 16:29:27 +02007316 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7317 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007318 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007319 if (di->di_tv.v_type == VAR_NUMBER)
7320 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007321 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007322 if (di->di_tv.vval.v_number != 0)
7323 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007324
Bram Moolenaar55b69262017-08-13 13:42:01 +02007325 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7326 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007327 // When creating a new list, accept qf_idx pointing to the next
7328 // non-available list and add the new list at the end of the
7329 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007330 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007331 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007332 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007333 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007334 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007335 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007336 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007337 }
7338 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007339 && di->di_tv.vval.v_string != NULL
7340 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007341 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007342 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007343 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007344 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007345 qf_idx = 0;
7346 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007347 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007348 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007349 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007350 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007351 }
7352
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007353 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007354 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007355 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007356 if (di->di_tv.v_type != VAR_NUMBER)
7357 return INVALID_QFIDX;
7358
7359 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007360 }
7361
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007362 return qf_idx;
7363}
7364
7365/*
7366 * Set the quickfix list title.
7367 */
7368 static int
7369qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7370{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007371 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007372
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007373 if (di->di_tv.v_type != VAR_STRING)
7374 return FAIL;
7375
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007376 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007377 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007378 if (qf_idx == qi->qf_curlist)
7379 qf_update_win_titlevar(qi);
7380
7381 return OK;
7382}
7383
7384/*
7385 * Set quickfix list items/entries.
7386 */
7387 static int
7388qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7389{
7390 int retval = FAIL;
7391 char_u *title_save;
7392
7393 if (di->di_tv.v_type != VAR_LIST)
7394 return FAIL;
7395
7396 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7397 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7398 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007399 vim_free(title_save);
7400
7401 return retval;
7402}
7403
7404/*
7405 * Set quickfix list items/entries from a list of lines.
7406 */
7407 static int
7408qf_setprop_items_from_lines(
7409 qf_info_T *qi,
7410 int qf_idx,
7411 dict_T *what,
7412 dictitem_T *di,
7413 int action)
7414{
7415 char_u *errorformat = p_efm;
7416 dictitem_T *efm_di;
7417 int retval = FAIL;
7418
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007419 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007420 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7421 {
7422 if (efm_di->di_tv.v_type != VAR_STRING ||
7423 efm_di->di_tv.vval.v_string == NULL)
7424 return FAIL;
7425 errorformat = efm_di->di_tv.vval.v_string;
7426 }
7427
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007428 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007429 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7430 return FAIL;
7431
7432 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007433 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007434 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007435 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007436 retval = OK;
7437
7438 return retval;
7439}
7440
7441/*
7442 * Set quickfix list context.
7443 */
7444 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007445qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007446{
7447 typval_T *ctx;
7448
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007449 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007450 ctx = alloc_tv();
7451 if (ctx != NULL)
7452 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007453 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007454
7455 return OK;
7456}
7457
7458/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007459 * Set the current index in the specified quickfix list
7460 */
7461 static int
7462qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7463{
7464 int denote = FALSE;
7465 int newidx;
7466 int old_qfidx;
7467 qfline_T *qf_ptr;
7468
7469 // If the specified index is '$', then use the last entry
7470 if (di->di_tv.v_type == VAR_STRING
7471 && di->di_tv.vval.v_string != NULL
7472 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7473 newidx = qfl->qf_count;
7474 else
7475 {
7476 // Otherwise use the specified index
7477 newidx = tv_get_number_chk(&di->di_tv, &denote);
7478 if (denote)
7479 return FAIL;
7480 }
7481
7482 if (newidx < 1) // sanity check
7483 return FAIL;
7484 if (newidx > qfl->qf_count)
7485 newidx = qfl->qf_count;
7486
7487 old_qfidx = qfl->qf_index;
7488 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7489 if (qf_ptr == NULL)
7490 return FAIL;
7491 qfl->qf_ptr = qf_ptr;
7492 qfl->qf_index = newidx;
7493
7494 // If the current list is modified and it is displayed in the quickfix
7495 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007496 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007497 qf_win_pos_update(qi, old_qfidx);
7498
7499 return OK;
7500}
7501
7502/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007503 * Set the current index in the specified quickfix list
7504 */
7505 static int
7506qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7507{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007508 callback_T cb;
7509
7510 free_callback(&qfl->qftf_cb);
7511 cb = get_callback(&di->di_tv);
7512 if (cb.cb_name != NULL && *cb.cb_name != NUL)
7513 set_callback(&qfl->qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007514
7515 return OK;
7516}
7517
7518/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007519 * Set quickfix/location list properties (title, items, context).
7520 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007521 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007522 */
7523 static int
7524qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7525{
7526 dictitem_T *di;
7527 int retval = FAIL;
7528 int qf_idx;
7529 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007530 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007531
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007532 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007533 newlist = TRUE;
7534
7535 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007536 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007537 return FAIL;
7538
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007539 if (newlist)
7540 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007541 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007542 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007543 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007544 }
7545
Bram Moolenaar0398e002019-03-21 21:12:49 +01007546 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007547 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007548 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007549 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007550 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007551 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007552 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007553 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007554 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007555 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7556 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007557 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7558 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007559
Bram Moolenaar287153c2020-11-29 14:20:27 +01007560 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007561 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007562 if (newlist)
7563 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007564
Bram Moolenaard823fa92016-08-12 16:29:27 +02007565 return retval;
7566}
7567
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007568/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007569 * Free the entire quickfix/location list stack.
7570 * If the quickfix/location list window is open, then clear it.
7571 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007572 static void
7573qf_free_stack(win_T *wp, qf_info_T *qi)
7574{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007575 win_T *qfwin = qf_find_win(qi);
7576 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007577
7578 if (qfwin != NULL)
7579 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007580 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007581 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007582 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007583 qf_update_buffer(qi, NULL);
7584 }
7585
7586 if (wp != NULL && IS_LL_WINDOW(wp))
7587 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007588 // If in the location list window, then use the non-location list
7589 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007590 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007591 if (llwin != NULL)
7592 wp = llwin;
7593 }
7594
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007595 qf_free_all(wp);
7596 if (wp == NULL)
7597 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007598 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007599 qi->qf_curlist = 0;
7600 qi->qf_listcount = 0;
7601 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007602 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007603 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007604 // If the location list window is open, then create a new empty
7605 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007606 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007607
Bram Moolenaar95946f12019-03-31 15:31:59 +02007608 if (new_ll != NULL)
7609 {
7610 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007611
Bram Moolenaar95946f12019-03-31 15:31:59 +02007612 // first free the list reference in the location list window
7613 ll_free_all(&qfwin->w_llist_ref);
7614
7615 qfwin->w_llist_ref = new_ll;
7616 if (wp != qfwin)
7617 win_set_loclist(wp, new_ll);
7618 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007619 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007620}
7621
Bram Moolenaard823fa92016-08-12 16:29:27 +02007622/*
7623 * Populate the quickfix list with the items supplied in the list
7624 * of dictionaries. "title" will be copied to w:quickfix_title.
7625 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007626 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007627 */
7628 int
7629set_errorlist(
7630 win_T *wp,
7631 list_T *list,
7632 int action,
7633 char_u *title,
7634 dict_T *what)
7635{
7636 qf_info_T *qi = &ql_info;
7637 int retval = OK;
7638
7639 if (wp != NULL)
7640 {
7641 qi = ll_get_or_alloc_list(wp);
7642 if (qi == NULL)
7643 return FAIL;
7644 }
7645
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007646 if (action == 'f')
7647 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007648 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007649 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007650 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007651 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007652
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007653 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007654 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007655 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007656 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007657 _("cannot have both a list and a \"what\" argument"));
7658 return FAIL;
7659 }
7660
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007661 incr_quickfix_busy();
7662
7663 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007664 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007665 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007666 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007667 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007668 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007669 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007670 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007671
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007672 decr_quickfix_busy();
7673
Bram Moolenaard823fa92016-08-12 16:29:27 +02007674 return retval;
7675}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007676
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007677/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007678 * Mark the quickfix context and callback function as in use for all the lists
7679 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007680 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007681 static int
7682mark_quickfix_ctx(qf_info_T *qi, int copyID)
7683{
7684 int i;
7685 int abort = FALSE;
7686 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007687 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007688
7689 for (i = 0; i < LISTCOUNT && !abort; ++i)
7690 {
7691 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007692 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7693 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007694 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7695
7696 cb = &qi->qf_lists[i].qftf_cb;
7697 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007698 }
7699
7700 return abort;
7701}
7702
7703/*
7704 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007705 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007706 */
7707 int
7708set_ref_in_quickfix(int copyID)
7709{
7710 int abort = FALSE;
7711 tabpage_T *tp;
7712 win_T *win;
7713
7714 abort = mark_quickfix_ctx(&ql_info, copyID);
7715 if (abort)
7716 return abort;
7717
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007718 abort = set_ref_in_callback(&qftf_cb, copyID);
7719 if (abort)
7720 return abort;
7721
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007722 FOR_ALL_TAB_WINDOWS(tp, win)
7723 {
7724 if (win->w_llist != NULL)
7725 {
7726 abort = mark_quickfix_ctx(win->w_llist, copyID);
7727 if (abort)
7728 return abort;
7729 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007730 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7731 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007732 // In a location list window and none of the other windows is
7733 // referring to this location list. Mark the location list
7734 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007735 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7736 if (abort)
7737 return abort;
7738 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007739 }
7740
7741 return abort;
7742}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007743#endif
7744
Bram Moolenaar81695252004-12-29 20:58:21 +00007745/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007746 * Return the autocmd name for the :cbuffer Ex commands
7747 */
7748 static char_u *
7749cbuffer_get_auname(cmdidx_T cmdidx)
7750{
7751 switch (cmdidx)
7752 {
7753 case CMD_cbuffer: return (char_u *)"cbuffer";
7754 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7755 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7756 case CMD_lbuffer: return (char_u *)"lbuffer";
7757 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7758 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7759 default: return NULL;
7760 }
7761}
7762
7763/*
7764 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7765 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7766 */
7767 static int
7768cbuffer_process_args(
7769 exarg_T *eap,
7770 buf_T **bufp,
7771 linenr_T *line1,
7772 linenr_T *line2)
7773{
7774 buf_T *buf = NULL;
7775
7776 if (*eap->arg == NUL)
7777 buf = curbuf;
7778 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7779 buf = buflist_findnr(atoi((char *)eap->arg));
7780
7781 if (buf == NULL)
7782 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007783 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007784 return FAIL;
7785 }
7786
7787 if (buf->b_ml.ml_mfp == NULL)
7788 {
7789 emsg(_("E681: Buffer is not loaded"));
7790 return FAIL;
7791 }
7792
7793 if (eap->addr_count == 0)
7794 {
7795 eap->line1 = 1;
7796 eap->line2 = buf->b_ml.ml_line_count;
7797 }
7798
7799 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7800 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7801 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007802 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007803 return FAIL;
7804 }
7805
7806 *line1 = eap->line1;
7807 *line2 = eap->line2;
7808 *bufp = buf;
7809
7810 return OK;
7811}
7812
7813/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007814 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007815 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007816 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007817 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007818 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007819 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007820 */
7821 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007822ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007823{
7824 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007825 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007826 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007827 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007828 int_u save_qfid;
7829 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007830 char_u *qf_title;
7831 linenr_T line1;
7832 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007833
Bram Moolenaara16123a2019-03-28 20:31:07 +01007834 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007835 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007836 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007837 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007838#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007839 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007840 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007841#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007842 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007843
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007844 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007845 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7846 if (qi == NULL)
7847 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007848
Bram Moolenaara16123a2019-03-28 20:31:07 +01007849 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7850 return;
7851
7852 qf_title = qf_cmdtitle(*eap->cmdlinep);
7853
7854 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007855 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007856 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7857 (char *)qf_title, (char *)buf->b_sfname);
7858 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007859 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007860
7861 incr_quickfix_busy();
7862
7863 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7864 (eap->cmdidx != CMD_caddbuffer
7865 && eap->cmdidx != CMD_laddbuffer),
7866 line1, line2,
7867 qf_title, NULL);
7868 if (qf_stack_empty(qi))
7869 {
7870 decr_quickfix_busy();
7871 return;
7872 }
7873 if (res >= 0)
7874 qf_list_changed(qf_get_curlist(qi));
7875
7876 // Remember the current quickfix list identifier, so that we can
7877 // check for autocommands changing the current quickfix list.
7878 save_qfid = qf_get_curlist(qi)->qf_id;
7879 if (au_name != NULL)
7880 {
7881 buf_T *curbuf_old = curbuf;
7882
7883 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7884 TRUE, curbuf);
7885 if (curbuf != curbuf_old)
7886 // Autocommands changed buffer, don't jump now, "qi" may
7887 // be invalid.
7888 res = 0;
7889 }
7890 // Jump to the first error for a new list and if autocmds didn't
7891 // free the list.
7892 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7893 eap->cmdidx == CMD_lbuffer)
7894 && qflist_valid(wp, save_qfid))
7895 // display the first error
7896 qf_jump_first(qi, save_qfid, eap->forceit);
7897
7898 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007899}
7900
Bram Moolenaar1e015462005-09-25 22:16:38 +00007901#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007902/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007903 * Return the autocmd name for the :cexpr Ex commands.
7904 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007905 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007906cexpr_get_auname(cmdidx_T cmdidx)
7907{
7908 switch (cmdidx)
7909 {
7910 case CMD_cexpr: return (char_u *)"cexpr";
7911 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7912 case CMD_caddexpr: return (char_u *)"caddexpr";
7913 case CMD_lexpr: return (char_u *)"lexpr";
7914 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7915 case CMD_laddexpr: return (char_u *)"laddexpr";
7916 default: return NULL;
7917 }
7918}
7919
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007920 int
7921trigger_cexpr_autocmd(int cmdidx)
7922{
7923 char_u *au_name = cexpr_get_auname(cmdidx);
7924
7925 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7926 curbuf->b_fname, TRUE, curbuf))
7927 {
7928 if (aborting())
7929 return FAIL;
7930 }
7931 return OK;
7932}
7933
7934 int
7935cexpr_core(exarg_T *eap, typval_T *tv)
7936{
7937 qf_info_T *qi;
7938 win_T *wp = NULL;
7939
7940 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7941 if (qi == NULL)
7942 return FAIL;
7943
7944 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7945 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7946 {
7947 int res;
7948 int_u save_qfid;
7949 char_u *au_name = cexpr_get_auname(eap->cmdidx);
7950
7951 incr_quickfix_busy();
7952 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
7953 (eap->cmdidx != CMD_caddexpr
7954 && eap->cmdidx != CMD_laddexpr),
7955 (linenr_T)0, (linenr_T)0,
7956 qf_cmdtitle(*eap->cmdlinep), NULL);
7957 if (qf_stack_empty(qi))
7958 {
7959 decr_quickfix_busy();
7960 return FAIL;
7961 }
7962 if (res >= 0)
7963 qf_list_changed(qf_get_curlist(qi));
7964
7965 // Remember the current quickfix list identifier, so that we can
7966 // check for autocommands changing the current quickfix list.
7967 save_qfid = qf_get_curlist(qi)->qf_id;
7968 if (au_name != NULL)
7969 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7970 curbuf->b_fname, TRUE, curbuf);
7971
7972 // Jump to the first error for a new list and if autocmds didn't
7973 // free the list.
7974 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
7975 && qflist_valid(wp, save_qfid))
7976 // display the first error
7977 qf_jump_first(qi, save_qfid, eap->forceit);
7978 decr_quickfix_busy();
7979 return OK;
7980 }
7981
7982 emsg(_("E777: String or List expected"));
7983 return FAIL;
7984}
7985
Bram Moolenaara16123a2019-03-28 20:31:07 +01007986/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007987 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7988 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007989 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007990 */
7991 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007992ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007993{
7994 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007995
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007996 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007997 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007998
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007999 // Evaluate the expression. When the result is a string or a list we can
8000 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008001 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008002 if (tv != NULL)
8003 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008004 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008005 free_tv(tv);
8006 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008007}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008008#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008009
8010/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008011 * Get the location list for ":lhelpgrep"
8012 */
8013 static qf_info_T *
8014hgr_get_ll(int *new_ll)
8015{
8016 win_T *wp;
8017 qf_info_T *qi;
8018
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008019 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008020 if (bt_help(curwin->w_buffer))
8021 wp = curwin;
8022 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008023 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008024 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008025
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008026 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008027 qi = NULL;
8028 else
8029 qi = wp->w_llist;
8030
8031 if (qi == NULL)
8032 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008033 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008034 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008035 return NULL;
8036 *new_ll = TRUE;
8037 }
8038
8039 return qi;
8040}
8041
8042/*
8043 * Search for a pattern in a help file.
8044 */
8045 static void
8046hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008047 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008048 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008049 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008050 regmatch_T *p_regmatch)
8051{
8052 FILE *fd;
8053 long lnum;
8054
8055 fd = mch_fopen((char *)fname, "r");
8056 if (fd == NULL)
8057 return;
8058
8059 lnum = 1;
8060 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8061 {
8062 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008063
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008064 // Convert a line if 'encoding' is not utf-8 and
8065 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008066 if (p_vc->vc_type != CONV_NONE
8067 && has_non_ascii(IObuff))
8068 {
8069 line = string_convert(p_vc, IObuff, NULL);
8070 if (line == NULL)
8071 line = IObuff;
8072 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008073
8074 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8075 {
8076 int l = (int)STRLEN(line);
8077
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008078 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008079 while (l > 0 && line[l - 1] <= ' ')
8080 line[--l] = NUL;
8081
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008082 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008083 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008084 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008085 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008086 0,
8087 line,
8088 lnum,
thinca6864efa2021-06-19 20:45:20 +02008089 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008090 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008091 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008092 (int)(p_regmatch->endp[0] - line)
8093 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008094 FALSE, // vis_col
8095 NULL, // search pattern
8096 0, // nr
8097 1, // type
8098 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008099 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008100 {
8101 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008102 if (line != IObuff)
8103 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008104 break;
8105 }
8106 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008107 if (line != IObuff)
8108 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008109 ++lnum;
8110 line_breakcheck();
8111 }
8112 fclose(fd);
8113}
8114
8115/*
8116 * Search for a pattern in all the help files in the doc directory under
8117 * the given directory.
8118 */
8119 static void
8120hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008121 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008122 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008123 regmatch_T *p_regmatch,
8124 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008125#ifdef FEAT_MULTI_LANG
8126 , char_u *lang
8127#endif
8128 )
8129{
8130 int fcount;
8131 char_u **fnames;
8132 int fi;
8133
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008134 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008135 add_pathsep(dirname);
8136 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8137 if (gen_expand_wildcards(1, &dirname, &fcount,
8138 &fnames, EW_FILE|EW_SILENT) == OK
8139 && fcount > 0)
8140 {
8141 for (fi = 0; fi < fcount && !got_int; ++fi)
8142 {
8143#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008144 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008145 if (lang != NULL
8146 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008147 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008148 && !(STRNICMP(lang, "en", 2) == 0
8149 && STRNICMP("txt", fnames[fi]
8150 + STRLEN(fnames[fi]) - 3, 3) == 0))
8151 continue;
8152#endif
8153
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008154 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008155 }
8156 FreeWild(fcount, fnames);
8157 }
8158}
8159
8160/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008161 * Search for a pattern in all the help files in the 'runtimepath'
8162 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008163 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008164 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008165 */
8166 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008167hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008168{
8169 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008170
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008171 vimconv_T vc;
8172
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008173 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8174 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008175 vc.vc_type = CONV_NONE;
8176 if (!enc_utf8)
8177 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008178
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008179 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008180 p = p_rtp;
8181 while (*p != NUL && !got_int)
8182 {
8183 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8184
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008185 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008186#ifdef FEAT_MULTI_LANG
8187 , lang
8188#endif
8189 );
8190 }
8191
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008192 if (vc.vc_type != CONV_NONE)
8193 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008194}
8195
8196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 * ":helpgrep {pattern}"
8198 */
8199 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008200ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
8202 regmatch_T regmatch;
8203 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008204 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008205 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008206 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008207 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008208 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
Bram Moolenaar73633f82012-01-20 13:39:07 +01008210 switch (eap->cmdidx)
8211 {
8212 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8213 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8214 default: break;
8215 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008216 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8217 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008218 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008219#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008220 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008221 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008222#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008223 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008224
Bram Moolenaar39665952018-08-15 20:59:48 +02008225 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008226 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008227 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008228 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008229 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008230 }
8231
Bram Moolenaara16123a2019-03-28 20:31:07 +01008232 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8233 save_cpo = p_cpo;
8234 p_cpo = empty_option;
8235
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008236 incr_quickfix_busy();
8237
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008238#ifdef FEAT_MULTI_LANG
8239 // Check for a specified language
8240 lang = check_help_lang(eap->arg);
8241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8243 regmatch.rm_ic = FALSE;
8244 if (regmatch.regprog != NULL)
8245 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008246 qf_list_T *qfl;
8247
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008248 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008249 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008250 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008252 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008253
Bram Moolenaar473de612013-06-08 18:19:48 +02008254 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008256 qfl->qf_nonevalid = FALSE;
8257 qfl->qf_ptr = qfl->qf_start;
8258 qfl->qf_index = 1;
8259 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008260 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 }
8262
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008263 if (p_cpo == empty_option)
8264 p_cpo = save_cpo;
8265 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008266 {
8267 // Darn, some plugin changed the value. If it's still empty it was
8268 // changed and restored, need to restore in the complicated way.
8269 if (*p_cpo == NUL)
8270 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008271 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008272 }
8273
8274 if (updated)
8275 // This may open a window and source scripts, do this after 'cpo' was
8276 // restored.
8277 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278
Bram Moolenaar73633f82012-01-20 13:39:07 +01008279 if (au_name != NULL)
8280 {
8281 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8282 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008283 // When adding a location list to an existing location list stack,
8284 // if the autocmd made the stack invalid, then just return.
8285 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008286 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008287 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008288 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008289 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008290 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008291
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008292 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008293 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008294 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008295 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008296 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008297
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008298 decr_quickfix_busy();
8299
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008300 if (eap->cmdidx == CMD_lhelpgrep)
8301 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008302 // If the help window is not opened or if it already points to the
8303 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008304 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008305 {
8306 if (new_qi)
8307 ll_free_all(&qi);
8308 }
8309 else if (curwin->w_llist == NULL)
8310 curwin->w_llist = qi;
8311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008313#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008314
8315#if defined(FEAT_EVAL) || defined(PROTO)
8316# ifdef FEAT_QUICKFIX
8317 static void
8318get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8319{
8320 if (what_arg->v_type == VAR_UNKNOWN)
8321 {
8322 if (rettv_list_alloc(rettv) == OK)
8323 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008324 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008325 }
8326 else
8327 {
8328 if (rettv_dict_alloc(rettv) == OK)
8329 if (is_qf || (wp != NULL))
8330 {
8331 if (what_arg->v_type == VAR_DICT)
8332 {
8333 dict_T *d = what_arg->vval.v_dict;
8334
8335 if (d != NULL)
8336 qf_get_properties(wp, d, rettv->vval.v_dict);
8337 }
8338 else
8339 emsg(_(e_dictreq));
8340 }
8341 }
8342}
8343# endif
8344
8345/*
8346 * "getloclist()" function
8347 */
8348 void
8349f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8350{
8351# ifdef FEAT_QUICKFIX
8352 win_T *wp;
8353
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008354 if (in_vim9script()
8355 && (check_for_number_arg(argvars, 0) == FAIL
8356 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8357 return;
8358
Bram Moolenaare677df82019-09-02 22:31:11 +02008359 wp = find_win_by_nr_or_id(&argvars[0]);
8360 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8361# endif
8362}
8363
8364/*
8365 * "getqflist()" function
8366 */
8367 void
8368f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8369{
8370# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008371 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8372 return;
8373
Bram Moolenaare677df82019-09-02 22:31:11 +02008374 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8375# endif
8376}
8377
8378/*
8379 * Used by "setqflist()" and "setloclist()" functions
8380 */
8381 static void
8382set_qf_ll_list(
8383 win_T *wp UNUSED,
8384 typval_T *list_arg UNUSED,
8385 typval_T *action_arg UNUSED,
8386 typval_T *what_arg UNUSED,
8387 typval_T *rettv)
8388{
8389# ifdef FEAT_QUICKFIX
8390 static char *e_invact = N_("E927: Invalid action: '%s'");
8391 char_u *act;
8392 int action = 0;
8393 static int recursive = 0;
8394# endif
8395
8396 rettv->vval.v_number = -1;
8397
8398# ifdef FEAT_QUICKFIX
8399 if (list_arg->v_type != VAR_LIST)
8400 emsg(_(e_listreq));
8401 else if (recursive != 0)
8402 emsg(_(e_au_recursive));
8403 else
8404 {
8405 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008406 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008407 int valid_dict = TRUE;
8408
8409 if (action_arg->v_type == VAR_STRING)
8410 {
8411 act = tv_get_string_chk(action_arg);
8412 if (act == NULL)
8413 return; // type error; errmsg already given
8414 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8415 act[1] == NUL)
8416 action = *act;
8417 else
8418 semsg(_(e_invact), act);
8419 }
8420 else if (action_arg->v_type == VAR_UNKNOWN)
8421 action = ' ';
8422 else
8423 emsg(_(e_stringreq));
8424
8425 if (action_arg->v_type != VAR_UNKNOWN
8426 && what_arg->v_type != VAR_UNKNOWN)
8427 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008428 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8429 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008430 else
8431 {
8432 emsg(_(e_dictreq));
8433 valid_dict = FALSE;
8434 }
8435 }
8436
8437 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008438 if (l != NULL && action && valid_dict
8439 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008440 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008441 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008442 rettv->vval.v_number = 0;
8443 --recursive;
8444 }
8445# endif
8446}
8447
8448/*
8449 * "setloclist()" function
8450 */
8451 void
8452f_setloclist(typval_T *argvars, typval_T *rettv)
8453{
8454 win_T *win;
8455
8456 rettv->vval.v_number = -1;
8457
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008458 if (in_vim9script()
8459 && (check_for_number_arg(argvars, 0) == FAIL
8460 || check_for_list_arg(argvars, 1) == FAIL
8461 || check_for_opt_string_arg(argvars, 2) == FAIL
8462 || (argvars[2].v_type != VAR_UNKNOWN
8463 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8464 return;
8465
Bram Moolenaare677df82019-09-02 22:31:11 +02008466 win = find_win_by_nr_or_id(&argvars[0]);
8467 if (win != NULL)
8468 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8469}
8470
8471/*
8472 * "setqflist()" function
8473 */
8474 void
8475f_setqflist(typval_T *argvars, typval_T *rettv)
8476{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008477 if (in_vim9script()
8478 && (check_for_list_arg(argvars, 0) == FAIL
8479 || check_for_opt_string_arg(argvars, 1) == FAIL
8480 || (argvars[1].v_type != VAR_UNKNOWN
8481 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8482 return;
8483
Bram Moolenaare677df82019-09-02 22:31:11 +02008484 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8485}
8486#endif