blob: f954a16bfd564095905c87e06e32cac5ba95ee10 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
thinca6864efa2021-06-19 20:45:20 +020033 linenr_T qf_end_lnum; // line number when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020034 int qf_fnum; // file number for the line
35 int qf_col; // column where the error occurred
thinca6864efa2021-06-19 20:45:20 +020036 int qf_end_col; // column when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020037 int qf_nr; // error number
38 char_u *qf_module; // module name for this error
39 char_u *qf_pattern; // search pattern for the error
40 char_u *qf_text; // description of the error
thinca6864efa2021-06-19 20:45:20 +020041 char_u qf_viscol; // set to TRUE if qf_col and qf_end_col is
42 // screen column
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020043 char_u qf_cleared; // set to TRUE if line has been deleted
44 char_u qf_type; // type of the error (mostly 'E'); 1 for
45 // :helpgrep
46 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000047};
48
49/*
50 * There is a stack of error lists.
51 */
52#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020053#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010054#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020056/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010057 * Quickfix list type.
58 */
59typedef enum
60{
61 QFLT_QUICKFIX, // Quickfix list - global list
62 QFLT_LOCATION, // Location list - per window list
63 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
64} qfltype_T;
65
66/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 * Quickfix/Location list definition
68 * Contains a list of entries (qfline_T). qf_start points to the first entry
69 * and qf_last points to the last entry. qf_count contains the list size.
70 *
71 * Usually the list contains one or more entries. But an empty list can be
72 * created using setqflist()/setloclist() with a title and/or user context
73 * information and entries can be added later using setqflist()/setloclist().
74 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000075typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020077 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010078 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020079 qfline_T *qf_start; // pointer to the first error
80 qfline_T *qf_last; // pointer to the last error
81 qfline_T *qf_ptr; // pointer to the current error
82 int qf_count; // number of errors (0 means empty list)
83 int qf_index; // current index in the error list
84 int qf_nonevalid; // TRUE if not a single valid entry found
85 char_u *qf_title; // title derived from the command that created
86 // the error list or set by setqflist
87 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaard43906d2020-07-20 21:31:32 +020088 callback_T qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020089
90 struct dir_stack_T *qf_dir_stack;
91 char_u *qf_directory;
92 struct dir_stack_T *qf_file_stack;
93 char_u *qf_currfile;
94 int qf_multiline;
95 int qf_multiignore;
96 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010097 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000098} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar6a8958d2017-06-22 21:33:20 +0200100/*
101 * Quickfix/Location list stack definition
102 * Contains a list of quickfix/location lists (qf_list_T)
103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000104struct qf_info_S
105{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 // Count of references to this list. Used only for location lists.
107 // When a location list window reference this list, qf_refcount
108 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
109 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200111 int qf_listcount; // current number of lists
112 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100114 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100115 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000116};
117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200118static qf_info_T ql_info; // global quickfix list
119static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
haya14busae023d492022-02-08 18:09:29 +0000121#define FMT_PATTERNS 13 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
124 * Structure used to hold the info of one part of 'errorformat'
125 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000126typedef struct efm_S efm_T;
127struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200129 regprog_T *prog; // pre-formatted part of 'errorformat'
130 efm_T *next; // pointer to next (NULL if last)
131 char_u addr[FMT_PATTERNS]; // indices of used % patterns
132 char_u prefix; // prefix of this format line:
133 // 'D' enter directory
134 // 'X' leave directory
135 // 'A' start of multi-line message
136 // 'E' error message
137 // 'W' warning message
138 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200139 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200140 // 'C' continuation line
141 // 'Z' end of multi-line message
142 // 'G' general, unspecific message
143 // 'P' push file (partial) message
144 // 'Q' pop/quit file (partial) message
145 // 'O' overread (partial) message
146 char_u flags; // additional flags given in prefix
147 // '-' do not include this line
148 // '+' include whole line in message
149 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150};
151
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200152// List of location lists to be deleted.
153// Used to delay the deletion of locations lists by autocmds.
154typedef struct qf_delq_S
155{
156 struct qf_delq_S *next;
157 qf_info_T *qi;
158} qf_delq_T;
159static qf_delq_T *qf_delq_head = NULL;
160
161// Counter to prevent autocmds from freeing up location lists when they are
162// still being used.
163static int quickfix_busy = 0;
164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200165static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100166
Bram Moolenaard43906d2020-07-20 21:31:32 +0200167// callback function for 'quickfixtextfunc'
168static callback_T qftf_cb;
169
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_new_list(qf_info_T *qi, char_u *qf_title);
thinca6864efa2021-06-19 20:45:20 +0200171static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, long end_lnum, int col, int end_col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200172static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100174static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200175static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200177static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200178static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
thinca6864efa2021-06-19 20:45:20 +0200180static void qf_range_text(qfline_T *qfp, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100181static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static win_T *qf_find_win(qf_info_T *qi);
183static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200184static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200185static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100186static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
187static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
188static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
189static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200191// Quickfix window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000192#define IS_QF_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200193// Location list window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000194#define IS_LL_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200195
196// Quickfix and location list stack check helper macros
kylo252ae6f1d82022-02-16 19:24:07 +0000197#define IS_QF_STACK(qi) ((qi)->qfl_type == QFLT_QUICKFIX)
198#define IS_LL_STACK(qi) ((qi)->qfl_type == QFLT_LOCATION)
199#define IS_QF_LIST(qfl) ((qfl)->qfl_type == QFLT_QUICKFIX)
200#define IS_LL_LIST(qfl) ((qfl)->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200201
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000202/*
203 * Return location list for window 'wp'
204 * For location list window, return the referenced location list
205 */
kylo252ae6f1d82022-02-16 19:24:07 +0000206#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? (wp)->w_llist_ref : (wp)->w_llist)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000207
Bram Moolenaar95946f12019-03-31 15:31:59 +0200208// Macro to loop through all the items in a quickfix list
209// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100210#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
kylo252ae6f1d82022-02-16 19:24:07 +0000211 for ((i) = 1, (qfp) = (qfl)->qf_start; \
212 !got_int && (i) <= (qfl)->qf_count && (qfp) != NULL; \
213 ++(i), (qfp) = (qfp)->qf_next)
Bram Moolenaara16123a2019-03-28 20:31:07 +0100214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200216 * Looking up a buffer can be slow if there are many. Remember the last one
217 * to make this a lot faster if there are multiple matches in the same file.
218 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200219static char_u *qf_last_bufname = NULL;
220static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200221
222/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200223 * Maximum number of bytes allowed per line while reading a errorfile.
224 */
225#define LINE_MAXLEN 4096
226
haya14busae023d492022-02-08 18:09:29 +0000227/*
228 * Patterns used. Keep in sync with qf_parse_fmt[].
229 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200230static struct fmtpattern
231{
232 char_u convchar;
233 char *pattern;
234} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200235 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200236 {'f', ".\\+"}, // only used when at end
haya14busae023d492022-02-08 18:09:29 +0000237 {'n', "\\d\\+"}, // 1
238 {'l', "\\d\\+"}, // 2
239 {'e', "\\d\\+"}, // 3
240 {'c', "\\d\\+"}, // 4
241 {'k', "\\d\\+"}, // 5
242 {'t', "."}, // 6
243#define FMT_PATTERN_M 7
244 {'m', ".\\+"}, // 7
245#define FMT_PATTERN_R 8
246 {'r', ".*"}, // 8
247 {'p', "[- .]*"}, // 9
248 {'v', "\\d\\+"}, // 10
249 {'s', ".\\+"}, // 11
250 {'o', ".\\+"} // 12
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200251 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200252
253/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 * See fmt_pat definition above for the list of supported patterns. The
256 * pattern specifier is supplied in "efmpat". The converted pattern is stored
257 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 */
259 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260efmpat_to_regpat(
261 char_u *efmpat,
262 char_u *regpat,
263 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200264 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100265 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266{
267 char_u *srcptr;
268
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200271 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000272 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200273 return NULL;
274 }
haya14busae023d492022-02-08 18:09:29 +0000275 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000277 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000280 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 return NULL;
282 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283 efminfo->addr[idx] = (char_u)++round;
284 *regpat++ = '\\';
285 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200288 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200289 // Also match "c:" in the file name, even when
290 // checking for a colon next: "%f:".
291 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 STRCPY(regpat, "\\%(\\a:\\)\\=");
293 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294 }
295#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200300 // A file name may contain spaces, but this isn't
301 // in "\f". For "%f:%l:%m" there may be a ":" in
302 // the file name. Use ".\{-1,}x" instead (x is
303 // the next character), the requirement that :999:
304 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 STRCPY(regpat, ".\\{-1,}");
306 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 }
308 else
309 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200310 // File name followed by '\\' or '%': include as
311 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 STRCPY(regpat, "\\f\\+");
313 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314 }
315 }
316 else
317 {
318 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 while ((*regpat = *srcptr++) != NUL)
320 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200322 *regpat++ = '\\';
323 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200325 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326}
327
328/*
329 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 */
332 static char_u *
333scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 char_u *efm,
336 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100337 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338{
339 char_u *efmp = *pefmp;
340
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200343 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 {
345 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200346 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347 if (efmp < efm + len)
348 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200349 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200350 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200352 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353 if (efmp == efm + len)
354 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000355 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 return NULL;
357 }
358 }
359 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200360 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 *regpat++ = *++efmp;
362 *regpat++ = '\\';
363 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 }
365 else
366 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200367 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000368 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369 return NULL;
370 }
371
372 *pefmp = efmp;
373
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375}
376
377/*
378 * Analyze/parse an errorformat prefix.
379 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100381efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200383 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200385 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200386 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200387 else
388 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000389 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 }
392
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394}
395
396/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200397 * Converts a 'errorformat' string part in 'efm' to a regular expression
398 * pattern. The resulting regex pattern is returned in "regpat". Additional
399 * information about the 'erroformat' pattern is returned in "fmt_ptr".
400 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200401 */
402 static int
403efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200404 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200405 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200406 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100407 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200408{
409 char_u *ptr;
410 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200411 int round;
412 int idx = 0;
413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200414 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200415 ptr = regpat;
416 *ptr++ = '^';
417 round = 0;
418 for (efmp = efm; efmp < efm + len; ++efmp)
419 {
420 if (*efmp == '%')
421 {
422 ++efmp;
423 for (idx = 0; idx < FMT_PATTERNS; ++idx)
424 if (fmt_pat[idx].convchar == *efmp)
425 break;
426 if (idx < FMT_PATTERNS)
427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100428 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200429 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200430 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200431 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200432 }
433 else if (*efmp == '*')
434 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200435 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100436 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200437 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200438 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200439 }
440 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200441 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 else if (*efmp == '#')
443 *ptr++ = '*';
444 else if (*efmp == '>')
445 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200446 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200448 // prefix is allowed only at the beginning of the errorformat
449 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100450 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200451 if (efmp == NULL)
452 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 }
454 else
455 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000456 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200457 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458 }
459 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200460 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200461 {
462 if (*efmp == '\\' && efmp + 1 < efm + len)
463 ++efmp;
464 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200465 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200466 if (*efmp)
467 *ptr++ = *efmp;
468 }
469 }
470 *ptr++ = '$';
471 *ptr = NUL;
472
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200473 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200474}
475
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200476/*
477 * Free the 'errorformat' information list
478 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200479 static void
480free_efm_list(efm_T **efm_first)
481{
482 efm_T *efm_ptr;
483
484 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
485 {
486 *efm_first = efm_ptr->next;
487 vim_regfree(efm_ptr->prog);
488 vim_free(efm_ptr);
489 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100490 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200491}
492
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200493/*
494 * Compute the size of the buffer used to convert a 'errorformat' pattern into
495 * a regular expression pattern.
496 */
497 static int
498efm_regpat_bufsz(char_u *efm)
499{
500 int sz;
501 int i;
502
503 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
504 for (i = FMT_PATTERNS; i > 0; )
505 sz += (int)STRLEN(fmt_pat[--i].pattern);
506#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200507 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200508#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200509 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200510#endif
511
512 return sz;
513}
514
515/*
516 * Return the length of a 'errorformat' option part (separated by ",").
517 */
518 static int
519efm_option_part_len(char_u *efm)
520{
521 int len;
522
523 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
524 if (efm[len] == '\\' && efm[len + 1] != NUL)
525 ++len;
526
527 return len;
528}
529
530/*
531 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
532 * are parsed and converted to regular expressions. Returns information about
533 * the parsed 'errorformat' option.
534 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200535 static efm_T *
536parse_efm_option(char_u *efm)
537{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200538 efm_T *fmt_ptr = NULL;
539 efm_T *fmt_first = NULL;
540 efm_T *fmt_last = NULL;
541 char_u *fmtstr = NULL;
542 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200543 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200545 // Each part of the format string is copied and modified from errorformat
546 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200547
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200548 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200549 sz = efm_regpat_bufsz(efm);
550 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 goto parse_efm_error;
552
553 while (efm[0] != NUL)
554 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200555 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200556 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200557 if (fmt_ptr == NULL)
558 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200559 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200560 fmt_first = fmt_ptr;
561 else
562 fmt_last->next = fmt_ptr;
563 fmt_last = fmt_ptr;
564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200565 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200566 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100568 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200569 goto parse_efm_error;
570 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
571 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200572 // Advance to next part
573 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200574 }
575
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200576 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000577 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200578
579 goto parse_efm_end;
580
581parse_efm_error:
582 free_efm_list(&fmt_first);
583
584parse_efm_end:
585 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200586
587 return fmt_first;
588}
589
Bram Moolenaare0d37972016-07-15 22:36:01 +0200590enum {
591 QF_FAIL = 0,
592 QF_OK = 1,
593 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200594 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200595 QF_IGNORE_LINE = 4,
596 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200597};
598
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200599/*
600 * State information used to parse lines and add entries to a quickfix/location
601 * list.
602 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200603typedef struct {
604 char_u *linebuf;
605 int linelen;
606 char_u *growbuf;
607 int growbufsiz;
608 FILE *fd;
609 typval_T *tv;
610 char_u *p_str;
611 listitem_T *p_li;
612 buf_T *buf;
613 linenr_T buflnum;
614 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100615 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616} qfstate_T;
617
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200618/*
619 * Allocate more memory for the line buffer used for parsing lines.
620 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200621 static char_u *
622qf_grow_linebuf(qfstate_T *state, int newsz)
623{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200624 char_u *p;
625
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200626 // If the line exceeds LINE_MAXLEN exclude the last
627 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
629 if (state->growbuf == NULL)
630 {
631 state->growbuf = alloc(state->linelen + 1);
632 if (state->growbuf == NULL)
633 return NULL;
634 state->growbufsiz = state->linelen;
635 }
636 else if (state->linelen > state->growbufsiz)
637 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200638 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200639 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200640 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200641 state->growbufsiz = state->linelen;
642 }
643 return state->growbuf;
644}
645
646/*
647 * Get the next string (separated by newline) from state->p_str.
648 */
649 static int
650qf_get_next_str_line(qfstate_T *state)
651{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200652 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200653 char_u *p_str = state->p_str;
654 char_u *p;
655 int len;
656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200657 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200658 return QF_END_OF_INPUT;
659
660 p = vim_strchr(p_str, '\n');
661 if (p != NULL)
662 len = (int)(p - p_str) + 1;
663 else
664 len = (int)STRLEN(p_str);
665
666 if (len > IOSIZE - 2)
667 {
668 state->linebuf = qf_grow_linebuf(state, len);
669 if (state->linebuf == NULL)
670 return QF_NOMEM;
671 }
672 else
673 {
674 state->linebuf = IObuff;
675 state->linelen = len;
676 }
677 vim_strncpy(state->linebuf, p_str, state->linelen);
678
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200679 // Increment using len in order to discard the rest of the
680 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 p_str += len;
682 state->p_str = p_str;
683
684 return QF_OK;
685}
686
687/*
688 * Get the next string from state->p_Li.
689 */
690 static int
691qf_get_next_list_line(qfstate_T *state)
692{
693 listitem_T *p_li = state->p_li;
694 int len;
695
696 while (p_li != NULL
697 && (p_li->li_tv.v_type != VAR_STRING
698 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200699 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200700
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200701 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200702 {
703 state->p_li = NULL;
704 return QF_END_OF_INPUT;
705 }
706
707 len = (int)STRLEN(p_li->li_tv.vval.v_string);
708 if (len > IOSIZE - 2)
709 {
710 state->linebuf = qf_grow_linebuf(state, len);
711 if (state->linebuf == NULL)
712 return QF_NOMEM;
713 }
714 else
715 {
716 state->linebuf = IObuff;
717 state->linelen = len;
718 }
719
720 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200722 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200723 return QF_OK;
724}
725
726/*
727 * Get the next string from state->buf.
728 */
729 static int
730qf_get_next_buf_line(qfstate_T *state)
731{
732 char_u *p_buf = NULL;
733 int len;
734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200735 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200736 if (state->buflnum > state->lnumlast)
737 return QF_END_OF_INPUT;
738
739 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
740 state->buflnum += 1;
741
742 len = (int)STRLEN(p_buf);
743 if (len > IOSIZE - 2)
744 {
745 state->linebuf = qf_grow_linebuf(state, len);
746 if (state->linebuf == NULL)
747 return QF_NOMEM;
748 }
749 else
750 {
751 state->linebuf = IObuff;
752 state->linelen = len;
753 }
754 vim_strncpy(state->linebuf, p_buf, state->linelen);
755
756 return QF_OK;
757}
758
759/*
760 * Get the next string from file state->fd.
761 */
762 static int
763qf_get_next_file_line(qfstate_T *state)
764{
765 int discard;
766 int growbuflen;
767
768 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
769 return QF_END_OF_INPUT;
770
771 discard = FALSE;
772 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200773 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200774 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200775 // The current line exceeds IObuff, continue reading using
776 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200777 if (state->growbuf == NULL)
778 {
779 state->growbufsiz = 2 * (IOSIZE - 1);
780 state->growbuf = alloc(state->growbufsiz);
781 if (state->growbuf == NULL)
782 return QF_NOMEM;
783 }
784
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200785 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 memcpy(state->growbuf, IObuff, IOSIZE - 1);
787 growbuflen = state->linelen;
788
789 for (;;)
790 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200791 char_u *p;
792
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793 if (fgets((char *)state->growbuf + growbuflen,
794 state->growbufsiz - growbuflen, state->fd) == NULL)
795 break;
796 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
797 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200798 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 break;
800 if (state->growbufsiz == LINE_MAXLEN)
801 {
802 discard = TRUE;
803 break;
804 }
805
806 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
807 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200808 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200809 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200810 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200811 }
812
813 while (discard)
814 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200815 // The current line is longer than LINE_MAXLEN, continue
816 // reading but discard everything until EOL or EOF is
817 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200818 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
819 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200820 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200821 break;
822 }
823
824 state->linebuf = state->growbuf;
825 state->linelen = growbuflen;
826 }
827 else
828 state->linebuf = IObuff;
829
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200830 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200831 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
832 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100833 char_u *line;
834
835 line = string_convert(&state->vc, state->linebuf, &state->linelen);
836 if (line != NULL)
837 {
838 if (state->linelen < IOSIZE)
839 {
840 STRCPY(state->linebuf, line);
841 vim_free(line);
842 }
843 else
844 {
845 vim_free(state->growbuf);
846 state->linebuf = state->growbuf = line;
847 state->growbufsiz = state->linelen < LINE_MAXLEN
848 ? state->linelen : LINE_MAXLEN;
849 }
850 }
851 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100852
Bram Moolenaare0d37972016-07-15 22:36:01 +0200853 return QF_OK;
854}
855
856/*
857 * Get the next string from a file/buffer/list/string.
858 */
859 static int
860qf_get_nextline(qfstate_T *state)
861{
862 int status = QF_FAIL;
863
864 if (state->fd == NULL)
865 {
866 if (state->tv != NULL)
867 {
868 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200869 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200870 status = qf_get_next_str_line(state);
871 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200872 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200873 status = qf_get_next_list_line(state);
874 }
875 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200876 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200877 status = qf_get_next_buf_line(state);
878 }
879 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200880 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200881 status = qf_get_next_file_line(state);
882
883 if (status != QF_OK)
884 return status;
885
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200886 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200887 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200888 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200889 state->linebuf[state->linelen - 1] = NUL;
890#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200891 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
892 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200893#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200894 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200895
Bram Moolenaare0d37972016-07-15 22:36:01 +0200896 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200897
898 return QF_OK;
899}
900
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200901typedef struct {
902 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200903 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200904 char_u *errmsg;
905 int errmsglen;
906 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200907 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200908 int col;
thinca6864efa2021-06-19 20:45:20 +0200909 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200910 char_u use_viscol;
911 char_u *pattern;
912 int enr;
913 int type;
914 int valid;
915} qffields_T;
916
917/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200918 * Parse the match for filename ('%f') pattern in regmatch.
919 * Return the matched value in "fields->namebuf".
920 */
921 static int
922qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
923{
924 int c;
925
926 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
927 return QF_FAIL;
928
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200929 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200930 c = *rmp->endp[midx];
931 *rmp->endp[midx] = NUL;
932 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
933 *rmp->endp[midx] = c;
934
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200935 // For separate filename patterns (%O, %P and %Q), the specified file
936 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200937 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
938 && mch_getperm(fields->namebuf) == -1)
939 return QF_FAIL;
940
941 return QF_OK;
942}
943
944/*
945 * Parse the match for error number ('%n') pattern in regmatch.
946 * Return the matched value in "fields->enr".
947 */
948 static int
949qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
950{
951 if (rmp->startp[midx] == NULL)
952 return QF_FAIL;
953 fields->enr = (int)atol((char *)rmp->startp[midx]);
954 return QF_OK;
955}
956
957/*
haya14busae023d492022-02-08 18:09:29 +0000958 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200959 * Return the matched value in "fields->lnum".
960 */
961 static int
962qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
963{
964 if (rmp->startp[midx] == NULL)
965 return QF_FAIL;
966 fields->lnum = atol((char *)rmp->startp[midx]);
967 return QF_OK;
968}
969
970/*
haya14busae023d492022-02-08 18:09:29 +0000971 * Parse the match for end line number ('%e') pattern in regmatch.
972 * Return the matched value in "fields->end_lnum".
973 */
974 static int
975qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
976{
977 if (rmp->startp[midx] == NULL)
978 return QF_FAIL;
979 fields->end_lnum = atol((char *)rmp->startp[midx]);
980 return QF_OK;
981}
982
983/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200984 * Parse the match for column number ('%c') pattern in regmatch.
985 * Return the matched value in "fields->col".
986 */
987 static int
988qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
989{
990 if (rmp->startp[midx] == NULL)
991 return QF_FAIL;
992 fields->col = (int)atol((char *)rmp->startp[midx]);
993 return QF_OK;
994}
995
996/*
haya14busae023d492022-02-08 18:09:29 +0000997 * Parse the match for end column number ('%k') pattern in regmatch.
998 * Return the matched value in "fields->end_col".
999 */
1000 static int
1001qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1002{
1003 if (rmp->startp[midx] == NULL)
1004 return QF_FAIL;
1005 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1006 return QF_OK;
1007}
1008
1009/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001010 * Parse the match for error type ('%t') pattern in regmatch.
1011 * Return the matched value in "fields->type".
1012 */
1013 static int
1014qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1015{
1016 if (rmp->startp[midx] == NULL)
1017 return QF_FAIL;
1018 fields->type = *rmp->startp[midx];
1019 return QF_OK;
1020}
1021
1022/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001023 * Copy a non-error line into the error string. Return the matched line in
1024 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001025 */
1026 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001027copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001028{
1029 char_u *p;
1030
1031 if (linelen >= fields->errmsglen)
1032 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001033 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001034 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1035 return QF_NOMEM;
1036 fields->errmsg = p;
1037 fields->errmsglen = linelen + 1;
1038 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001039 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001040 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001041
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001042 return QF_OK;
1043}
1044
1045/*
1046 * Parse the match for error message ('%m') pattern in regmatch.
1047 * Return the matched value in "fields->errmsg".
1048 */
1049 static int
1050qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1051{
1052 char_u *p;
1053 int len;
1054
1055 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1056 return QF_FAIL;
1057 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1058 if (len >= fields->errmsglen)
1059 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001060 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001061 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1062 return QF_NOMEM;
1063 fields->errmsg = p;
1064 fields->errmsglen = len + 1;
1065 }
1066 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1067 return QF_OK;
1068}
1069
1070/*
1071 * Parse the match for rest of a single-line file message ('%r') pattern.
1072 * Return the matched value in "tail".
1073 */
1074 static int
1075qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1076{
1077 if (rmp->startp[midx] == NULL)
1078 return QF_FAIL;
1079 *tail = rmp->startp[midx];
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the pointer line ('%p') pattern in regmatch.
1085 * Return the matched value in "fields->col".
1086 */
1087 static int
1088qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 char_u *match_ptr;
1091
1092 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1093 return QF_FAIL;
1094 fields->col = 0;
1095 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1096 ++match_ptr)
1097 {
1098 ++fields->col;
1099 if (*match_ptr == TAB)
1100 {
1101 fields->col += 7;
1102 fields->col -= fields->col % 8;
1103 }
1104 }
1105 ++fields->col;
1106 fields->use_viscol = TRUE;
1107 return QF_OK;
1108}
1109
1110/*
1111 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1112 * Return the matched value in "fields->col".
1113 */
1114 static int
1115qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1116{
1117 if (rmp->startp[midx] == NULL)
1118 return QF_FAIL;
1119 fields->col = (int)atol((char *)rmp->startp[midx]);
1120 fields->use_viscol = TRUE;
1121 return QF_OK;
1122}
1123
1124/*
1125 * Parse the match for the search text ('%s') pattern in regmatch.
1126 * Return the matched value in "fields->pattern".
1127 */
1128 static int
1129qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1130{
1131 int len;
1132
1133 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1134 return QF_FAIL;
1135 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1136 if (len > CMDBUFFSIZE - 5)
1137 len = CMDBUFFSIZE - 5;
1138 STRCPY(fields->pattern, "^\\V");
1139 STRNCAT(fields->pattern, rmp->startp[midx], len);
1140 fields->pattern[len + 3] = '\\';
1141 fields->pattern[len + 4] = '$';
1142 fields->pattern[len + 5] = NUL;
1143 return QF_OK;
1144}
1145
1146/*
1147 * Parse the match for the module ('%o') pattern in regmatch.
1148 * Return the matched value in "fields->module".
1149 */
1150 static int
1151qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1152{
1153 int len;
1154
1155 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1156 return QF_FAIL;
1157 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1158 if (len > CMDBUFFSIZE)
1159 len = CMDBUFFSIZE;
1160 STRNCAT(fields->module, rmp->startp[midx], len);
1161 return QF_OK;
1162}
1163
1164/*
1165 * 'errorformat' format pattern parser functions.
1166 * The '%f' and '%r' formats are parsed differently from other formats.
1167 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001168 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001169 */
1170static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1171{
haya14busae023d492022-02-08 18:09:29 +00001172 NULL, // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001173 qf_parse_fmt_n,
1174 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001175 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001177 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 qf_parse_fmt_t,
1179 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001180 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 qf_parse_fmt_p,
1182 qf_parse_fmt_v,
1183 qf_parse_fmt_s,
1184 qf_parse_fmt_o
1185};
1186
1187/*
1188 * Parse the error format pattern matches in "regmatch" and set the values in
1189 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1190 * match. Returns QF_OK if all the matches are successfully parsed. On
1191 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001192 */
1193 static int
1194qf_parse_match(
1195 char_u *linebuf,
1196 int linelen,
1197 efm_T *fmt_ptr,
1198 regmatch_T *regmatch,
1199 qffields_T *fields,
1200 int qf_multiline,
1201 int qf_multiscan,
1202 char_u **tail)
1203{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001204 int idx = fmt_ptr->prefix;
1205 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001206 int midx;
1207 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001208
1209 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1210 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001211 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001212 fields->type = idx;
1213 else
1214 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001215
1216 // Extract error message data from matched line.
1217 // We check for an actual submatch, because "\[" and "\]" in
1218 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001219 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001220 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001221 status = QF_OK;
1222 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001223 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001224 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001225 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001227 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001228 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001229 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001230 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001231 }
haya14busae023d492022-02-08 18:09:29 +00001232 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001233 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001234 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001235 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001236
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001237 if (status != QF_OK)
1238 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001239 }
1240
1241 return QF_OK;
1242}
1243
1244/*
1245 * Parse an error line in 'linebuf' using a single error format string in
1246 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1247 * Returns QF_OK if the efm format matches completely and the fields are
1248 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1249 */
1250 static int
1251qf_parse_get_fields(
1252 char_u *linebuf,
1253 int linelen,
1254 efm_T *fmt_ptr,
1255 qffields_T *fields,
1256 int qf_multiline,
1257 int qf_multiscan,
1258 char_u **tail)
1259{
1260 regmatch_T regmatch;
1261 int status = QF_FAIL;
1262 int r;
1263
1264 if (qf_multiscan &&
1265 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1266 return QF_FAIL;
1267
1268 fields->namebuf[0] = NUL;
1269 fields->module[0] = NUL;
1270 fields->pattern[0] = NUL;
1271 if (!qf_multiscan)
1272 fields->errmsg[0] = NUL;
1273 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001274 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001275 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001276 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001277 fields->use_viscol = FALSE;
1278 fields->enr = -1;
1279 fields->type = 0;
1280 *tail = NULL;
1281
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001282 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001283 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001284 regmatch.regprog = fmt_ptr->prog;
1285 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1286 fmt_ptr->prog = regmatch.regprog;
1287 if (r)
1288 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1289 fields, qf_multiline, qf_multiscan, tail);
1290
1291 return status;
1292}
1293
1294/*
1295 * Parse directory error format prefixes (%D and %X).
1296 * Push and pop directories from the directory stack when scanning directory
1297 * names.
1298 */
1299 static int
1300qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1301{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001302 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001303 {
1304 if (*fields->namebuf == NUL)
1305 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001306 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001307 return QF_FAIL;
1308 }
1309 qfl->qf_directory =
1310 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1311 if (qfl->qf_directory == NULL)
1312 return QF_FAIL;
1313 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001314 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1316
1317 return QF_OK;
1318}
1319
1320/*
1321 * Parse global file name error format prefixes (%O, %P and %Q).
1322 */
1323 static int
1324qf_parse_file_pfx(
1325 int idx,
1326 qffields_T *fields,
1327 qf_list_T *qfl,
1328 char_u *tail)
1329{
1330 fields->valid = FALSE;
1331 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1332 {
1333 if (*fields->namebuf && idx == 'P')
1334 qfl->qf_currfile =
1335 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1336 else if (idx == 'Q')
1337 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1338 *fields->namebuf = NUL;
1339 if (tail && *tail)
1340 {
1341 STRMOVE(IObuff, skipwhite(tail));
1342 qfl->qf_multiscan = TRUE;
1343 return QF_MULTISCAN;
1344 }
1345 }
1346
1347 return QF_OK;
1348}
1349
1350/*
1351 * Parse a non-error line (a line which doesn't match any of the error
1352 * format in 'efm').
1353 */
1354 static int
1355qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1356{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001357 fields->namebuf[0] = NUL; // no match found, remove file name
1358 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001359 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001360
Bram Moolenaarf4140482020-02-15 23:06:45 +01001361 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001362}
1363
1364/*
1365 * Parse multi-line error format prefixes (%C and %Z)
1366 */
1367 static int
1368qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001369 int idx,
1370 qf_list_T *qfl,
1371 qffields_T *fields)
1372{
1373 char_u *ptr;
1374 int len;
1375
1376 if (!qfl->qf_multiignore)
1377 {
1378 qfline_T *qfprev = qfl->qf_last;
1379
1380 if (qfprev == NULL)
1381 return QF_FAIL;
1382 if (*fields->errmsg && !qfl->qf_multiignore)
1383 {
1384 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001385 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001386 == NULL)
1387 return QF_FAIL;
1388 STRCPY(ptr, qfprev->qf_text);
1389 vim_free(qfprev->qf_text);
1390 qfprev->qf_text = ptr;
1391 *(ptr += len) = '\n';
1392 STRCPY(++ptr, fields->errmsg);
1393 }
1394 if (qfprev->qf_nr == -1)
1395 qfprev->qf_nr = fields->enr;
1396 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001397 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001398 qfprev->qf_type = fields->type;
1399
1400 if (!qfprev->qf_lnum)
1401 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001402 if (!qfprev->qf_end_lnum)
1403 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001404 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001405 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001406 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001407 qfprev->qf_viscol = fields->use_viscol;
1408 }
haya14busae023d492022-02-08 18:09:29 +00001409 if (!qfprev->qf_end_col)
1410 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001411 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001412 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001413 qfl->qf_directory,
1414 *fields->namebuf || qfl->qf_directory != NULL
1415 ? fields->namebuf
1416 : qfl->qf_currfile != NULL && fields->valid
1417 ? qfl->qf_currfile : 0);
1418 }
1419 if (idx == 'Z')
1420 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1421 line_breakcheck();
1422
1423 return QF_IGNORE_LINE;
1424}
1425
1426/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001427 * Parse a line and get the quickfix fields.
1428 * Return the QF_ status.
1429 */
1430 static int
1431qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001432 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001433 char_u *linebuf,
1434 int linelen,
1435 efm_T *fmt_first,
1436 qffields_T *fields)
1437{
1438 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001439 int idx = 0;
1440 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001441 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001442
Bram Moolenaare333e792018-04-08 13:27:39 +02001443restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001444 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445 if (fmt_start == NULL)
1446 fmt_ptr = fmt_first;
1447 else
1448 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001449 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001450 fmt_ptr = fmt_start;
1451 fmt_start = NULL;
1452 }
1453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001454 // Try to match each part of 'errorformat' until we find a complete
1455 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001456 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001457 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1458 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001460 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1461 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1462 if (status == QF_NOMEM)
1463 return status;
1464 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001465 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001467 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468
1469 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1470 {
1471 if (fmt_ptr != NULL)
1472 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001473 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001474 status = qf_parse_dir_pfx(idx, fields, qfl);
1475 if (status != QF_OK)
1476 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001477 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001478
1479 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1480 if (status != QF_OK)
1481 return status;
1482
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001483 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001484 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001485 }
1486 else if (fmt_ptr != NULL)
1487 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001488 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001489 if (fmt_ptr->conthere)
1490 fmt_start = fmt_ptr;
1491
Bram Moolenaare9283662020-06-07 14:10:47 +02001492 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001493 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001494 qfl->qf_multiline = TRUE; // start of a multi-line message
1495 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001496 }
1497 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001498 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001499 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001500 if (status != QF_OK)
1501 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001502 }
1503 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001504 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001505 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1506 if (status == QF_MULTISCAN)
1507 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001508 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001509 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001510 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001511 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001512 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001513 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001514 return QF_IGNORE_LINE;
1515 }
1516 }
1517
1518 return QF_OK;
1519}
1520
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001521/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001522 * Returns TRUE if the specified quickfix/location stack is empty
1523 */
1524 static int
1525qf_stack_empty(qf_info_T *qi)
1526{
1527 return qi == NULL || qi->qf_listcount <= 0;
1528}
1529
1530/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001531 * Returns TRUE if the specified quickfix/location list is empty.
1532 */
1533 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001534qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001535{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001536 return qfl == NULL || qfl->qf_count <= 0;
1537}
1538
1539/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001540 * Returns TRUE if the specified quickfix/location list is not empty and
1541 * has valid entries.
1542 */
1543 static int
1544qf_list_has_valid_entries(qf_list_T *qfl)
1545{
1546 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1547}
1548
1549/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001550 * Return a pointer to a list in the specified quickfix stack
1551 */
1552 static qf_list_T *
1553qf_get_list(qf_info_T *qi, int idx)
1554{
1555 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001556}
1557
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001558/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001559 * Allocate the fields used for parsing lines and populating a quickfix list.
1560 */
1561 static int
1562qf_alloc_fields(qffields_T *pfields)
1563{
1564 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1565 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1566 pfields->errmsglen = CMDBUFFSIZE + 1;
1567 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1568 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1569 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1570 || pfields->pattern == NULL || pfields->module == NULL)
1571 return FAIL;
1572
1573 return OK;
1574}
1575
1576/*
1577 * Free the fields used for parsing lines and populating a quickfix list.
1578 */
1579 static void
1580qf_free_fields(qffields_T *pfields)
1581{
1582 vim_free(pfields->namebuf);
1583 vim_free(pfields->module);
1584 vim_free(pfields->errmsg);
1585 vim_free(pfields->pattern);
1586}
1587
1588/*
1589 * Setup the state information used for parsing lines and populating a
1590 * quickfix list.
1591 */
1592 static int
1593qf_setup_state(
1594 qfstate_T *pstate,
1595 char_u *enc,
1596 char_u *efile,
1597 typval_T *tv,
1598 buf_T *buf,
1599 linenr_T lnumfirst,
1600 linenr_T lnumlast)
1601{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001602 pstate->vc.vc_type = CONV_NONE;
1603 if (enc != NULL && *enc != NUL)
1604 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001605
1606 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1607 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001608 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001609 return FAIL;
1610 }
1611
1612 if (tv != NULL)
1613 {
1614 if (tv->v_type == VAR_STRING)
1615 pstate->p_str = tv->vval.v_string;
1616 else if (tv->v_type == VAR_LIST)
1617 pstate->p_li = tv->vval.v_list->lv_first;
1618 pstate->tv = tv;
1619 }
1620 pstate->buf = buf;
1621 pstate->buflnum = lnumfirst;
1622 pstate->lnumlast = lnumlast;
1623
1624 return OK;
1625}
1626
1627/*
1628 * Cleanup the state information used for parsing lines and populating a
1629 * quickfix list.
1630 */
1631 static void
1632qf_cleanup_state(qfstate_T *pstate)
1633{
1634 if (pstate->fd != NULL)
1635 fclose(pstate->fd);
1636
1637 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001638 if (pstate->vc.vc_type != CONV_NONE)
1639 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001640}
1641
1642/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001643 * Process the next line from a file/buffer/list/string and add it
1644 * to the quickfix list 'qfl'.
1645 */
1646 static int
1647qf_init_process_nextline(
1648 qf_list_T *qfl,
1649 efm_T *fmt_first,
1650 qfstate_T *state,
1651 qffields_T *fields)
1652{
1653 int status;
1654
1655 // Get the next line from a file/buffer/list/string
1656 status = qf_get_nextline(state);
1657 if (status != QF_OK)
1658 return status;
1659
1660 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1661 fmt_first, fields);
1662 if (status != QF_OK)
1663 return status;
1664
1665 return qf_add_entry(qfl,
1666 qfl->qf_directory,
1667 (*fields->namebuf || qfl->qf_directory != NULL)
1668 ? fields->namebuf
1669 : ((qfl->qf_currfile != NULL && fields->valid)
1670 ? qfl->qf_currfile : (char_u *)NULL),
1671 fields->module,
1672 0,
1673 fields->errmsg,
1674 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001675 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001676 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001677 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001678 fields->use_viscol,
1679 fields->pattern,
1680 fields->enr,
1681 fields->type,
1682 fields->valid);
1683}
1684
1685/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001686 * Read the errorfile "efile" into memory, line by line, building the error
1687 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001688 * Alternative: when "efile" is NULL read errors from buffer "buf".
1689 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001690 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001691 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1692 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001693 * Return -1 for error, number of errors for success.
1694 */
1695 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001696qf_init_ext(
1697 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001698 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001699 char_u *efile,
1700 buf_T *buf,
1701 typval_T *tv,
1702 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001703 int newlist, // TRUE: start a new error list
1704 linenr_T lnumfirst, // first line number to use
1705 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001706 char_u *qf_title,
1707 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001708{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001709 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001710 qfstate_T state;
1711 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001712 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001713 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001714 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001716 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001717 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001718 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001720 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001721 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001722
Bram Moolenaara80faa82020-04-12 19:37:17 +02001723 CLEAR_FIELD(state);
1724 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001725 if ((qf_alloc_fields(&fields) == FAIL) ||
1726 (qf_setup_state(&state, enc, efile, tv, buf,
1727 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 goto qf_init_end;
1729
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001730 if (newlist || qf_idx == qi->qf_listcount)
1731 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001732 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001733 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001734 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001735 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001736 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001737 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001738 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001739 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001740 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001741 qfl = qf_get_list(qi, qf_idx);
1742 if (!qf_list_empty(qfl))
1743 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001746 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001747 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001748 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 else
1750 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001752 // If the errorformat didn't change between calls, then reuse the
1753 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001754 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1755 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001756 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001757 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001758 free_efm_list(&fmt_first);
1759
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001760 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001761 fmt_first = parse_efm_option(efm);
1762 if (fmt_first != NULL)
1763 last_efm = vim_strsave(efm);
1764 }
1765
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001766 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001769 // got_int is reset here, because it was probably set when killing the
1770 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 got_int = FALSE;
1772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001773 // Read the lines in the error file one by one.
1774 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001775 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001777 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001778 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001779 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001780 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001781 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001782 if (status == QF_FAIL)
1783 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 line_breakcheck();
1786 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001787 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001789 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001791 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001792 qfl->qf_ptr = qfl->qf_start;
1793 qfl->qf_index = 1;
1794 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
1796 else
1797 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001798 qfl->qf_nonevalid = FALSE;
1799 if (qfl->qf_ptr == NULL)
1800 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001802 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001803 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001804 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001806 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001808 if (!adding)
1809 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001810 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001811 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001812 qi->qf_listcount--;
1813 if (qi->qf_curlist > 0)
1814 --qi->qf_curlist;
1815 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001816qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001817 if (qf_idx == qi->qf_curlist)
1818 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001819 qf_cleanup_state(&state);
1820 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 return retval;
1823}
1824
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001825/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001826 * Read the errorfile "efile" into memory, line by line, building the error
1827 * list. Set the error list's title to qf_title.
1828 * Return -1 for error, number of errors for success.
1829 */
1830 int
1831qf_init(win_T *wp,
1832 char_u *efile,
1833 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001834 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001835 char_u *qf_title,
1836 char_u *enc)
1837{
1838 qf_info_T *qi = &ql_info;
1839
1840 if (wp != NULL)
1841 {
1842 qi = ll_get_or_alloc_list(wp);
1843 if (qi == NULL)
1844 return FAIL;
1845 }
1846
1847 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1848 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1849}
1850
1851/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001852 * Set the title of the specified quickfix list. Frees the previous title.
1853 * Prepends ':' to the title.
1854 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001855 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001856qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001857{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001858 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001859
Bram Moolenaarfb604092014-07-23 15:55:00 +02001860 if (title != NULL)
1861 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001862 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001863
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001864 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001865 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001866 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001867 }
1868}
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001871 * The title of a quickfix/location list is set, by default, to the command
1872 * that created the quickfix list with the ":" prefix.
1873 * Create a quickfix list title string by prepending ":" to a user command.
1874 * Returns a pointer to a static buffer with the title.
1875 */
1876 static char_u *
1877qf_cmdtitle(char_u *cmd)
1878{
1879 static char_u qftitle_str[IOSIZE];
1880
1881 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1882 return qftitle_str;
1883}
1884
1885/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001886 * Return a pointer to the current list in the specified quickfix stack
1887 */
1888 static qf_list_T *
1889qf_get_curlist(qf_info_T *qi)
1890{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001891 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001892}
1893
1894/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001895 * Prepare for adding a new quickfix list. If the current list is in the
1896 * middle of the stack, then all the following lists are freed and then
1897 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 */
1899 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001900qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001903 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001905 // If the current entry is not the last entry, delete entries beyond
1906 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001907 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001908 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001909 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001911 // When the stack is full, remove to oldest entry
1912 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001915 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001917 qi->qf_lists[i - 1] = qi->qf_lists[i];
1918 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001922 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001923 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001924 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001925 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001926 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927}
1928
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001929/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001930 * Queue location list stack delete request.
1931 */
1932 static void
1933locstack_queue_delreq(qf_info_T *qi)
1934{
1935 qf_delq_T *q;
1936
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001937 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001938 if (q != NULL)
1939 {
1940 q->qi = qi;
1941 q->next = qf_delq_head;
1942 qf_delq_head = q;
1943 }
1944}
1945
1946/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001947 * Return the global quickfix stack window buffer number.
1948 */
1949 int
1950qf_stack_get_bufnr(void)
1951{
1952 return ql_info.qf_bufnr;
1953}
1954
1955/*
1956 * Wipe the quickfix window buffer (if present) for the specified
1957 * quickfix/location list.
1958 */
1959 static void
1960wipe_qf_buffer(qf_info_T *qi)
1961{
1962 buf_T *qfbuf;
1963
1964 if (qi->qf_bufnr != INVALID_QFBUFNR)
1965 {
1966 qfbuf = buflist_findnr(qi->qf_bufnr);
1967 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1968 {
1969 // If the quickfix buffer is not loaded in any window, then
1970 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001971 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001972 qi->qf_bufnr = INVALID_QFBUFNR;
1973 }
1974 }
1975}
1976
1977/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001978 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001979 */
1980 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001981ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001982{
1983 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001984 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001985
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001986 qi = *pqi;
1987 if (qi == NULL)
1988 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001989 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001991 // If the location list is still in use, then queue the delete request
1992 // to be processed later.
1993 if (quickfix_busy > 0)
1994 {
1995 locstack_queue_delreq(qi);
1996 return;
1997 }
1998
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001999 qi->qf_refcount--;
2000 if (qi->qf_refcount < 1)
2001 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002002 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002003 // If the quickfix window buffer is loaded, then wipe it
2004 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002005
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002006 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002007 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002008 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002010}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002011
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002012/*
2013 * Free all the quickfix/location lists in the stack.
2014 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002016qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017{
2018 int i;
2019 qf_info_T *qi = &ql_info;
2020
2021 if (wp != NULL)
2022 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002023 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002024 ll_free_all(&wp->w_llist);
2025 ll_free_all(&wp->w_llist_ref);
2026 }
2027 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002028 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002029 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002030 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002031}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002032
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002034 * Delay freeing of location list stacks when the quickfix code is running.
2035 * Used to avoid problems with autocmds freeing location list stacks when the
2036 * quickfix code is still referencing the stack.
2037 * Must always call decr_quickfix_busy() exactly once after this.
2038 */
2039 static void
2040incr_quickfix_busy(void)
2041{
2042 quickfix_busy++;
2043}
2044
2045/*
2046 * Safe to free location list stacks. Process any delayed delete requests.
2047 */
2048 static void
2049decr_quickfix_busy(void)
2050{
2051 if (--quickfix_busy == 0)
2052 {
2053 // No longer referencing the location lists. Process all the pending
2054 // delete requests.
2055 while (qf_delq_head != NULL)
2056 {
2057 qf_delq_T *q = qf_delq_head;
2058
2059 qf_delq_head = q->next;
2060 ll_free_all(&q->qi);
2061 vim_free(q);
2062 }
2063 }
2064#ifdef ABORT_ON_INTERNAL_ERROR
2065 if (quickfix_busy < 0)
2066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002067 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002068 abort();
2069 }
2070#endif
2071}
2072
2073#if defined(EXITFREE) || defined(PROTO)
2074 void
2075check_quickfix_busy(void)
2076{
2077 if (quickfix_busy != 0)
2078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002080# ifdef ABORT_ON_INTERNAL_ERROR
2081 abort();
2082# endif
2083 }
2084}
2085#endif
2086
2087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002089 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 */
2091 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002092qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002093 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002094 char_u *dir, // optional directory name
2095 char_u *fname, // file name or NULL
2096 char_u *module, // module name or NULL
2097 int bufnum, // buffer number or zero
2098 char_u *mesg, // message
2099 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002100 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002101 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002102 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002103 int vis_col, // using visual column
2104 char_u *pattern, // search pattern
2105 int nr, // error number
2106 int type, // type character
2107 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002109 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002110 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002112 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002113 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002114 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002115 {
2116 buf_T *buf = buflist_findnr(bufnum);
2117
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002118 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002119 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002120 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002121 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002122 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002123 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002124 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2126 {
2127 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002128 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002131 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002133 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002134 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002135 if (pattern == NULL || *pattern == NUL)
2136 qfp->qf_pattern = NULL;
2137 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2138 {
2139 vim_free(qfp->qf_text);
2140 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002141 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002142 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002143 if (module == NULL || *module == NUL)
2144 qfp->qf_module = NULL;
2145 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2146 {
2147 vim_free(qfp->qf_text);
2148 vim_free(qfp->qf_pattern);
2149 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002150 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002153 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 type = 0;
2155 qfp->qf_type = type;
2156 qfp->qf_valid = valid;
2157
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002158 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002159 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002161 qfl->qf_start = qfp;
2162 qfl->qf_ptr = qfp;
2163 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002164 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 else
2167 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002168 qfp->qf_prev = *lastp;
2169 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002171 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002173 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002174 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002175 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002177 qfl->qf_index = qfl->qf_count;
2178 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180
Bram Moolenaar95946f12019-03-31 15:31:59 +02002181 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182}
2183
2184/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002185 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002186 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002187 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002188qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002189{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002190 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002191
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002192 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002193 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002194 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002195 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002196 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002197 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002198 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002199 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002200}
2201
2202/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002203 * Return the location list stack for window 'wp'.
2204 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002205 */
2206 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002207ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002208{
2209 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002210 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 return wp->w_llist_ref;
2212
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002213 // For a non-location list window, w_llist_ref should not point to a
2214 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 ll_free_all(&wp->w_llist_ref);
2216
2217 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002218 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002219 return wp->w_llist;
2220}
2221
2222/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002223 * Get the quickfix/location list stack to use for the specified Ex command.
2224 * For a location list command, returns the stack for the current window. If
2225 * the location list is not found, then returns NULL and prints an error
2226 * message if 'print_emsg' is TRUE.
2227 */
2228 static qf_info_T *
2229qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2230{
2231 qf_info_T *qi = &ql_info;
2232
2233 if (is_loclist_cmd(eap->cmdidx))
2234 {
2235 qi = GET_LOC_LIST(curwin);
2236 if (qi == NULL)
2237 {
2238 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002239 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002240 return NULL;
2241 }
2242 }
2243
2244 return qi;
2245}
2246
2247/*
2248 * Get the quickfix/location list stack to use for the specified Ex command.
2249 * For a location list command, returns the stack for the current window.
2250 * If the location list is not present, then allocates a new one.
2251 * Returns NULL if the allocation fails. For a location list command, sets
2252 * 'pwinp' to curwin.
2253 */
2254 static qf_info_T *
2255qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2256{
2257 qf_info_T *qi = &ql_info;
2258
2259 if (is_loclist_cmd(eap->cmdidx))
2260 {
2261 qi = ll_get_or_alloc_list(curwin);
2262 if (qi == NULL)
2263 return NULL;
2264 *pwinp = curwin;
2265 }
2266
2267 return qi;
2268}
2269
2270/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002271 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2272 */
2273 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002274copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002275{
2276 int i;
2277 qfline_T *from_qfp;
2278 qfline_T *prevp;
2279
2280 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002281 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002282 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002283 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002284 NULL,
2285 NULL,
2286 from_qfp->qf_module,
2287 0,
2288 from_qfp->qf_text,
2289 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002290 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002291 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002292 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002293 from_qfp->qf_viscol,
2294 from_qfp->qf_pattern,
2295 from_qfp->qf_nr,
2296 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002297 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002298 return FAIL;
2299
2300 // qf_add_entry() will not set the qf_num field, as the
2301 // directory and file names are not supplied. So the qf_fnum
2302 // field is copied here.
2303 prevp = to_qfl->qf_last;
2304 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2305 prevp->qf_type = from_qfp->qf_type; // error type
2306 if (from_qfl->qf_ptr == from_qfp)
2307 to_qfl->qf_ptr = prevp; // current location
2308 }
2309
2310 return OK;
2311}
2312
2313/*
2314 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2315 */
2316 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002317copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002318{
2319 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002320 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002321 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2322 to_qfl->qf_count = 0;
2323 to_qfl->qf_index = 0;
2324 to_qfl->qf_start = NULL;
2325 to_qfl->qf_last = NULL;
2326 to_qfl->qf_ptr = NULL;
2327 if (from_qfl->qf_title != NULL)
2328 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2329 else
2330 to_qfl->qf_title = NULL;
2331 if (from_qfl->qf_ctx != NULL)
2332 {
2333 to_qfl->qf_ctx = alloc_tv();
2334 if (to_qfl->qf_ctx != NULL)
2335 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2336 }
2337 else
2338 to_qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002339 if (from_qfl->qftf_cb.cb_name != NULL)
2340 copy_callback(&to_qfl->qftf_cb, &from_qfl->qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002341 else
Bram Moolenaard43906d2020-07-20 21:31:32 +02002342 to_qfl->qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002343
2344 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002345 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002346 return FAIL;
2347
2348 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2349
2350 // Assign a new ID for the location list
2351 to_qfl->qf_id = ++last_qf_id;
2352 to_qfl->qf_changedtick = 0L;
2353
2354 // When no valid entries are present in the list, qf_ptr points to
2355 // the first item in the list
2356 if (to_qfl->qf_nonevalid)
2357 {
2358 to_qfl->qf_ptr = to_qfl->qf_start;
2359 to_qfl->qf_index = 1;
2360 }
2361
2362 return OK;
2363}
2364
2365/*
2366 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 */
2368 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002369copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002370{
2371 qf_info_T *qi;
2372 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002373
Bram Moolenaar09037502018-09-25 22:08:14 +02002374 // When copying from a location list window, copy the referenced
2375 // location list. For other windows, copy the location list for
2376 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002377 if (IS_LL_WINDOW(from))
2378 qi = from->w_llist_ref;
2379 else
2380 qi = from->w_llist;
2381
Bram Moolenaar09037502018-09-25 22:08:14 +02002382 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002383 return;
2384
Bram Moolenaar09037502018-09-25 22:08:14 +02002385 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002386 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002387 return;
2388
2389 to->w_llist->qf_listcount = qi->qf_listcount;
2390
Bram Moolenaar09037502018-09-25 22:08:14 +02002391 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002392 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002393 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002394 to->w_llist->qf_curlist = idx;
2395
Bram Moolenaar0398e002019-03-21 21:12:49 +01002396 if (copy_loclist(qf_get_list(qi, idx),
2397 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002398 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002399 qf_free_all(to);
2400 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002401 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 }
2403
Bram Moolenaar09037502018-09-25 22:08:14 +02002404 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405}
2406
2407/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002408 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002409 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 */
2411 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002412qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
Bram Moolenaar82404332016-07-10 17:00:38 +02002414 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002415 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002416 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002417
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002418 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaare60acc12011-05-10 16:41:25 +02002421#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002422 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002423#endif
2424#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002425 if (directory != NULL)
2426 slash_adjust(directory);
2427 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002428#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002429 if (directory != NULL && !vim_isAbsName(fname)
2430 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2431 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002432 // Here we check if the file really exists.
2433 // This should normally be true, but if make works without
2434 // "leaving directory"-messages we might have missed a
2435 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002436 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002439 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002440 if (directory)
2441 ptr = concat_fnames(directory, fname, TRUE);
2442 else
2443 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002445 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002446 bufname = ptr;
2447 }
2448 else
2449 bufname = fname;
2450
2451 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002452 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002453 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002454 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002455 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002457 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002458 {
2459 vim_free(qf_last_bufname);
2460 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2461 if (bufname == ptr)
2462 qf_last_bufname = bufname;
2463 else
2464 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002465 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002466 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002467 if (buf == NULL)
2468 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002469
Bram Moolenaarc1542742016-07-20 21:44:37 +02002470 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002471 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002472 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473}
2474
2475/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002476 * Push dirbuf onto the directory stack and return pointer to actual dir or
2477 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
2479 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002480qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 struct dir_stack_T *ds_new;
2483 struct dir_stack_T *ds_ptr;
2484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002485 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002486 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (ds_new == NULL)
2488 return NULL;
2489
2490 ds_new->next = *stackptr;
2491 *stackptr = ds_new;
2492
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002493 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (vim_isAbsName(dirbuf)
2495 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002496 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 (*stackptr)->dirname = vim_strsave(dirbuf);
2498 else
2499 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002500 // Okay we don't have an absolute path.
2501 // dirbuf must be a subdir of one of the directories on the stack.
2502 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 ds_new = (*stackptr)->next;
2504 (*stackptr)->dirname = NULL;
2505 while (ds_new)
2506 {
2507 vim_free((*stackptr)->dirname);
2508 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2509 TRUE);
2510 if (mch_isdir((*stackptr)->dirname) == TRUE)
2511 break;
2512
2513 ds_new = ds_new->next;
2514 }
2515
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002516 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 while ((*stackptr)->next != ds_new)
2518 {
2519 ds_ptr = (*stackptr)->next;
2520 (*stackptr)->next = (*stackptr)->next->next;
2521 vim_free(ds_ptr->dirname);
2522 vim_free(ds_ptr);
2523 }
2524
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002525 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (ds_new == NULL)
2527 {
2528 vim_free((*stackptr)->dirname);
2529 (*stackptr)->dirname = vim_strsave(dirbuf);
2530 }
2531 }
2532
2533 if ((*stackptr)->dirname != NULL)
2534 return (*stackptr)->dirname;
2535 else
2536 {
2537 ds_ptr = *stackptr;
2538 *stackptr = (*stackptr)->next;
2539 vim_free(ds_ptr);
2540 return NULL;
2541 }
2542}
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544/*
2545 * pop dirbuf from the directory stack and return previous directory or NULL if
2546 * stack is empty
2547 */
2548 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002549qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 struct dir_stack_T *ds_ptr;
2552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002553 // TODO: Should we check if dirbuf is the directory on top of the stack?
2554 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002556 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (*stackptr != NULL)
2558 {
2559 ds_ptr = *stackptr;
2560 *stackptr = (*stackptr)->next;
2561 vim_free(ds_ptr->dirname);
2562 vim_free(ds_ptr);
2563 }
2564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002565 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 return *stackptr ? (*stackptr)->dirname : NULL;
2567}
2568
2569/*
2570 * clean up directory stack
2571 */
2572 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002573qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 struct dir_stack_T *ds_ptr;
2576
2577 while ((ds_ptr = *stackptr) != NULL)
2578 {
2579 *stackptr = (*stackptr)->next;
2580 vim_free(ds_ptr->dirname);
2581 vim_free(ds_ptr);
2582 }
2583}
2584
2585/*
2586 * Check in which directory of the directory stack the given file can be
2587 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002588 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 * Cleans up intermediate directory entries.
2590 *
2591 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002592 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 * ./
2594 * ./aa
2595 * ./aa/bb
2596 * ./bb
2597 * ./bb/x.c
2598 * and make says:
2599 * making all in aa
2600 * making all in bb
2601 * x.c:9: Error
2602 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2603 * qf_guess_filepath will return NULL.
2604 */
2605 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002606qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 struct dir_stack_T *ds_ptr;
2609 struct dir_stack_T *ds_tmp;
2610 char_u *fullname;
2611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002612 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002613 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 return NULL;
2615
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002616 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 fullname = NULL;
2618 while (ds_ptr)
2619 {
2620 vim_free(fullname);
2621 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2622
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002623 // If concat_fnames failed, just go on. The worst thing that can happen
2624 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2626 break;
2627
2628 ds_ptr = ds_ptr->next;
2629 }
2630
2631 vim_free(fullname);
2632
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002633 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002634 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002636 ds_tmp = qfl->qf_dir_stack->next;
2637 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 vim_free(ds_tmp->dirname);
2639 vim_free(ds_tmp);
2640 }
2641
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002642 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643}
2644
2645/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002646 * Returns TRUE if a quickfix/location list with the given identifier exists.
2647 */
2648 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002649qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002650{
2651 qf_info_T *qi = &ql_info;
2652 int i;
2653
2654 if (wp != NULL)
2655 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002656 if (!win_valid(wp))
2657 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002658 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002659 if (qi == NULL)
2660 return FALSE;
2661 }
2662
2663 for (i = 0; i < qi->qf_listcount; ++i)
2664 if (qi->qf_lists[i].qf_id == qf_id)
2665 return TRUE;
2666
2667 return FALSE;
2668}
2669
2670/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002671 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002672 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002673 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002674 * Similar to location list.
2675 */
2676 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002677is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002678{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002679 qfline_T *qfp;
2680 int i;
2681
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002682 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002683 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2684 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002685 break;
2686
Bram Moolenaar95946f12019-03-31 15:31:59 +02002687 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002688 return FALSE;
2689
2690 return TRUE;
2691}
2692
2693/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002694 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002695 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002696 */
2697 static qfline_T *
2698get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002699 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002700 qfline_T *qf_ptr,
2701 int *qf_index,
2702 int dir)
2703{
2704 int idx;
2705 int old_qf_fnum;
2706
2707 idx = *qf_index;
2708 old_qf_fnum = qf_ptr->qf_fnum;
2709
2710 do
2711 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002712 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002713 return NULL;
2714 ++idx;
2715 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002716 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002717 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2718
2719 *qf_index = idx;
2720 return qf_ptr;
2721}
2722
2723/*
2724 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002725 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726 */
2727 static qfline_T *
2728get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002729 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002730 qfline_T *qf_ptr,
2731 int *qf_index,
2732 int dir)
2733{
2734 int idx;
2735 int old_qf_fnum;
2736
2737 idx = *qf_index;
2738 old_qf_fnum = qf_ptr->qf_fnum;
2739
2740 do
2741 {
2742 if (idx == 1 || qf_ptr->qf_prev == NULL)
2743 return NULL;
2744 --idx;
2745 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002746 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002747 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2748
2749 *qf_index = idx;
2750 return qf_ptr;
2751}
2752
2753/*
2754 * Get the n'th (errornr) previous/next valid entry from the current entry in
2755 * the quickfix list.
2756 * dir == FORWARD or FORWARD_FILE: next valid entry
2757 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2758 */
2759 static qfline_T *
2760get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002761 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002762 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002763 int dir,
2764 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002765{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002766 qfline_T *qf_ptr = qfl->qf_ptr;
2767 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002768 qfline_T *prev_qf_ptr;
2769 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002770 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002771
2772 while (errornr--)
2773 {
2774 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002775 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002776
2777 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002778 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002779 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002780 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002781 if (qf_ptr == NULL)
2782 {
2783 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002784 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002785 if (err != NULL)
2786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002787 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002788 return NULL;
2789 }
2790 break;
2791 }
2792
2793 err = NULL;
2794 }
2795
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002796 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002797 return qf_ptr;
2798}
2799
2800/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002801 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2802 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002803 */
2804 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002805get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002806{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002807 qfline_T *qf_ptr = qfl->qf_ptr;
2808 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002809
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002810 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002811 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2812 {
2813 --qf_idx;
2814 qf_ptr = qf_ptr->qf_prev;
2815 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002816 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002817 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2818 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002819 {
2820 ++qf_idx;
2821 qf_ptr = qf_ptr->qf_next;
2822 }
2823
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002824 *new_qfidx = qf_idx;
2825 return qf_ptr;
2826}
2827
2828/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002829 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002830 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2831 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2832 * Returns a pointer to the entry and the index of the new entry is stored in
2833 * 'new_qfidx'.
2834 */
2835 static qfline_T *
2836qf_get_entry(
2837 qf_list_T *qfl,
2838 int errornr,
2839 int dir,
2840 int *new_qfidx)
2841{
2842 qfline_T *qf_ptr = qfl->qf_ptr;
2843 int qfidx = qfl->qf_index;
2844
2845 if (dir != 0) // next/prev valid entry
2846 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2847 else if (errornr != 0) // go to specified number
2848 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2849
2850 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002851 return qf_ptr;
2852}
2853
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002854/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002855 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002856 */
2857 static win_T *
2858qf_find_help_win(void)
2859{
2860 win_T *wp;
2861
2862 FOR_ALL_WINDOWS(wp)
2863 if (bt_help(wp->w_buffer))
2864 return wp;
2865
2866 return NULL;
2867}
2868
2869/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002870 * Set the location list for the specified window to 'qi'.
2871 */
2872 static void
2873win_set_loclist(win_T *wp, qf_info_T *qi)
2874{
2875 wp->w_llist = qi;
2876 qi->qf_refcount++;
2877}
2878
2879/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002880 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2881 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002882 */
2883 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002884jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002885{
2886 win_T *wp;
2887 int flags;
2888
Bram Moolenaare1004402020-10-24 20:49:43 +02002889 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002890 wp = NULL;
2891 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002892 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002893 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2894 win_enter(wp, TRUE);
2895 else
2896 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002897 // Split off help window; put it at far top if no position
2898 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002899 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002900 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002901 && curwin->w_width < 80)
2902 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002903 // If the user asks to open a new window, then copy the location list.
2904 // Otherwise, don't copy the location list.
2905 if (IS_LL_STACK(qi) && !newwin)
2906 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002907
2908 if (win_split(0, flags) == FAIL)
2909 return FAIL;
2910
2911 *opened_window = TRUE;
2912
2913 if (curwin->w_height < p_hh)
2914 win_setheight((int)p_hh);
2915
Bram Moolenaarb2443732018-11-11 22:50:27 +01002916 // When using location list, the new window should use the supplied
2917 // location list. If the user asks to open a new window, then the new
2918 // window will get a copy of the location list.
2919 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002920 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002921 }
2922
2923 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002924 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002925
2926 return OK;
2927}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002928
2929/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002930 * Find a non-quickfix window using the given location list stack in the
2931 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002932 * Returns NULL if a matching window is not found.
2933 */
2934 static win_T *
2935qf_find_win_with_loclist(qf_info_T *ll)
2936{
2937 win_T *wp;
2938
2939 FOR_ALL_WINDOWS(wp)
2940 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2941 return wp;
2942
2943 return NULL;
2944}
2945
2946/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002947 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002948 */
2949 static win_T *
2950qf_find_win_with_normal_buf(void)
2951{
2952 win_T *wp;
2953
2954 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002955 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002956 return wp;
2957
2958 return NULL;
2959}
2960
2961/*
2962 * Go to a window in any tabpage containing the specified file. Returns TRUE
2963 * if successfully jumped to the window. Otherwise returns FALSE.
2964 */
2965 static int
2966qf_goto_tabwin_with_file(int fnum)
2967{
2968 tabpage_T *tp;
2969 win_T *wp;
2970
2971 FOR_ALL_TAB_WINDOWS(tp, wp)
2972 if (wp->w_buffer->b_fnum == fnum)
2973 {
2974 goto_tabpage_win(tp, wp);
2975 return TRUE;
2976 }
2977
2978 return FALSE;
2979}
2980
2981/*
2982 * Create a new window to show a file above the quickfix window. Called when
2983 * only the quickfix window is present.
2984 */
2985 static int
2986qf_open_new_file_win(qf_info_T *ll_ref)
2987{
2988 int flags;
2989
2990 flags = WSP_ABOVE;
2991 if (ll_ref != NULL)
2992 flags |= WSP_NEWLOC;
2993 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002994 return FAIL; // not enough room for window
2995 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002996 swb_flags = 0;
2997 RESET_BINDING(curwin);
2998 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002999 // The new window should use the location list from the
3000 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003001 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003002 return OK;
3003}
3004
3005/*
3006 * Go to a window that shows the right buffer. If the window is not found, go
3007 * to the window just above the location list window. This is used for opening
3008 * a file from a location window and not from a quickfix window. If some usable
3009 * window is previously found, then it is supplied in 'use_win'.
3010 */
3011 static void
3012qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3013{
3014 win_T *win = use_win;
3015
3016 if (win == NULL)
3017 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003018 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003019 FOR_ALL_WINDOWS(win)
3020 if (win->w_buffer->b_fnum == qf_fnum)
3021 break;
3022 if (win == NULL)
3023 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003024 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003025 win = curwin;
3026 do
3027 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003028 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003029 break;
3030 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003031 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003032 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003033 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003034 } while (win != curwin);
3035 }
3036 }
3037 win_goto(win);
3038
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003039 // If the location list for the window is not set, then set it
3040 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003041 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003042 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003043}
3044
3045/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003046 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3047 * not found, then go to the window just above the quickfix window. This is
3048 * used for opening a file from a quickfix window and not from a location
3049 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003050 */
3051 static void
3052qf_goto_win_with_qfl_file(int qf_fnum)
3053{
3054 win_T *win;
3055 win_T *altwin;
3056
3057 win = curwin;
3058 altwin = NULL;
3059 for (;;)
3060 {
3061 if (win->w_buffer->b_fnum == qf_fnum)
3062 break;
3063 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003064 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003065 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003066 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003067
3068 if (IS_QF_WINDOW(win))
3069 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003070 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003071 // window, unless 'switchbuf' contains 'uselast': in this case we
3072 // try to jump to the previously used window first.
3073 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3074 win = prevwin;
3075 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003076 win = altwin;
3077 else if (curwin->w_prev != NULL)
3078 win = curwin->w_prev;
3079 else
3080 win = curwin->w_next;
3081 break;
3082 }
3083
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003084 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003085 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003086 altwin = win;
3087 }
3088
3089 win_goto(win);
3090}
3091
3092/*
3093 * Find a suitable window for opening a file (qf_fnum) from the
3094 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003095 * window, jump to it. Otherwise open a new window to display the file. If
3096 * 'newwin' is TRUE, then always open a new window. This is called from either
3097 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003098 */
3099 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003100qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003101{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003102 win_T *usable_wp = NULL;
3103 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003104 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003105
Bram Moolenaarb2443732018-11-11 22:50:27 +01003106 // If opening a new window, then don't use the location list referred by
3107 // the current window. Otherwise two windows will refer to the same
3108 // location list.
3109 if (!newwin)
3110 ll_ref = curwin->w_llist_ref;
3111
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003112 if (ll_ref != NULL)
3113 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003114 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003115 usable_wp = qf_find_win_with_loclist(ll_ref);
3116 if (usable_wp != NULL)
3117 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003118 }
3119
3120 if (!usable_win)
3121 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003122 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003123 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003124 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003125 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003126 }
3127
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003128 // If no usable window is found and 'switchbuf' contains "usetab"
3129 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003130 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003131 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003132
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003133 // If there is only one window and it is the quickfix window, create a
3134 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003135 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003136 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003137 if (qf_open_new_file_win(ll_ref) != OK)
3138 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003139 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003140 }
3141 else
3142 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003143 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003144 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003145 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003146 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003147 }
3148
3149 return OK;
3150}
3151
3152/*
3153 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003154 * Returns OK if successfully edited the file, FAIL on failing to open the
3155 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3156 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003157 */
3158 static int
3159qf_jump_edit_buffer(
3160 qf_info_T *qi,
3161 qfline_T *qf_ptr,
3162 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003163 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003164 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003165{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003166 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003167 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003168 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003170 int old_qf_curlist = qi->qf_curlist;
3171 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003172
3173 if (qf_ptr->qf_type == 1)
3174 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003175 // Open help file (do_ecmd() will set b_help flag, readfile() will
3176 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003177 if (!can_abandon(curbuf, forceit))
3178 {
3179 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003180 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003181 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003182
3183 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3184 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003185 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003186 }
3187 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003188 retval = buflist_getfile(qf_ptr->qf_fnum,
3189 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003190
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003191 // If a location list, check whether the associated window is still
3192 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003193 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003194 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003195 win_T *wp = win_id2wp(prev_winid);
3196 if (wp == NULL && curwin->w_llist != qi)
3197 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003198 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003199 *opened_window = FALSE;
3200 return NOTDONE;
3201 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003202 }
3203
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003204 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003205 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003206 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003207 return NOTDONE;
3208 }
3209
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003210 // Check if the list was changed. The pointers may happen to be identical,
3211 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003212 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003213 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003214 || !is_qf_entry_present(qfl, qf_ptr))
3215 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003216 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003217 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003218 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003219 emsg(_(e_current_location_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003220 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003221 }
3222
3223 return retval;
3224}
3225
3226/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003227 * Go to the error line in the current file using either line/column number or
3228 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003229 */
3230 static void
3231qf_jump_goto_line(
3232 linenr_T qf_lnum,
3233 int qf_col,
3234 char_u qf_viscol,
3235 char_u *qf_pattern)
3236{
3237 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003238
3239 if (qf_pattern == NULL)
3240 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003241 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003242 i = qf_lnum;
3243 if (i > 0)
3244 {
3245 if (i > curbuf->b_ml.ml_line_count)
3246 i = curbuf->b_ml.ml_line_count;
3247 curwin->w_cursor.lnum = i;
3248 }
3249 if (qf_col > 0)
3250 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003251 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003252 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003253 coladvance(qf_col - 1);
3254 else
3255 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003256 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003257 check_cursor();
3258 }
3259 else
3260 beginline(BL_WHITE | BL_FIX);
3261 }
3262 else
3263 {
3264 pos_T save_cursor;
3265
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003266 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003267 save_cursor = curwin->w_cursor;
3268 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003269 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003270 curwin->w_cursor = save_cursor;
3271 }
3272}
3273
3274/*
3275 * Display quickfix list index and size message
3276 */
3277 static void
3278qf_jump_print_msg(
3279 qf_info_T *qi,
3280 int qf_index,
3281 qfline_T *qf_ptr,
3282 buf_T *old_curbuf,
3283 linenr_T old_lnum)
3284{
3285 linenr_T i;
3286 int len;
3287
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003288 // Update the screen before showing the message, unless the screen
3289 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003290 if (!msg_scrolled)
3291 update_topline_redraw();
3292 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003293 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003294 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3295 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003296 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003297 len = (int)STRLEN(IObuff);
3298 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3299
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003300 // Output the message. Overwrite to avoid scrolling when the 'O'
3301 // flag is present in 'shortmess'; But when not jumping, print the
3302 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003303 i = msg_scroll;
3304 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3305 msg_scroll = TRUE;
3306 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3307 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003308 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003309 msg_scroll = i;
3310}
3311
3312/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003313 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003314 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3315 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003316 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3317 * able to jump/open a window. Returns NOTDONE if a file is not associated
3318 * with the entry.
3319 */
3320 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003321qf_jump_open_window(
3322 qf_info_T *qi,
3323 qfline_T *qf_ptr,
3324 int newwin,
3325 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003326{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003327 qf_list_T *qfl = qf_get_curlist(qi);
3328 int old_changedtick = qfl->qf_changedtick;
3329 int old_qf_curlist = qi->qf_curlist;
3330 qfltype_T qfl_type = qfl->qfl_type;
3331
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003332 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003333 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3334 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003335 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003336 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003337 if (old_qf_curlist != qi->qf_curlist
3338 || old_changedtick != qfl->qf_changedtick
3339 || !is_qf_entry_present(qfl, qf_ptr))
3340 {
3341 if (qfl_type == QFLT_QUICKFIX)
3342 emsg(_(e_current_quickfix_list_was_changed));
3343 else
3344 emsg(_(e_current_location_list_was_changed));
3345 return FAIL;
3346 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003347
3348 // If currently in the quickfix window, find another window to show the
3349 // file in.
3350 if (bt_quickfix(curbuf) && !*opened_window)
3351 {
3352 // If there is no file specified, we don't know where to go.
3353 // But do advance, otherwise ":cn" gets stuck.
3354 if (qf_ptr->qf_fnum == 0)
3355 return NOTDONE;
3356
Bram Moolenaarb2443732018-11-11 22:50:27 +01003357 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3358 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003359 return FAIL;
3360 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003361 if (old_qf_curlist != qi->qf_curlist
3362 || old_changedtick != qfl->qf_changedtick
3363 || !is_qf_entry_present(qfl, qf_ptr))
3364 {
3365 if (qfl_type == QFLT_QUICKFIX)
3366 emsg(_(e_current_quickfix_list_was_changed));
3367 else
3368 emsg(_(e_current_location_list_was_changed));
3369 return FAIL;
3370 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003371
3372 return OK;
3373}
3374
3375/*
3376 * Edit a selected file from the quickfix/location list and jump to a
3377 * particular line/column, adjust the folds and display a message about the
3378 * jump.
3379 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3380 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3381 * the file.
3382 */
3383 static int
3384qf_jump_to_buffer(
3385 qf_info_T *qi,
3386 int qf_index,
3387 qfline_T *qf_ptr,
3388 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003389 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003390 int *opened_window,
3391 int openfold,
3392 int print_message)
3393{
3394 buf_T *old_curbuf;
3395 linenr_T old_lnum;
3396 int retval = OK;
3397
3398 // If there is a file name, read the wanted file if needed, and check
3399 // autowrite etc.
3400 old_curbuf = curbuf;
3401 old_lnum = curwin->w_cursor.lnum;
3402
3403 if (qf_ptr->qf_fnum != 0)
3404 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003405 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003406 opened_window);
3407 if (retval != OK)
3408 return retval;
3409 }
3410
3411 // When not switched to another buffer, still need to set pc mark
3412 if (curbuf == old_curbuf)
3413 setpcmark();
3414
3415 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3416 qf_ptr->qf_pattern);
3417
3418#ifdef FEAT_FOLDING
3419 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3420 foldOpenCursor();
3421#endif
3422 if (print_message)
3423 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3424
3425 return retval;
3426}
3427
3428/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003429 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
3431 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003432qf_jump(qf_info_T *qi,
3433 int dir,
3434 int errornr,
3435 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003437 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3438}
3439
3440/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003441 * Jump to a quickfix line.
3442 * If dir == 0 go to entry "errornr".
3443 * If dir == FORWARD go "errornr" valid entries forward.
3444 * If dir == BACKWARD go "errornr" valid entries backward.
3445 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3446 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3447 * else if "errornr" is zero, redisplay the same line
3448 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003449 * If 'newwin' is TRUE, then open the file in a new window.
3450 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003451 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003452qf_jump_newwin(qf_info_T *qi,
3453 int dir,
3454 int errornr,
3455 int forceit,
3456 int newwin)
3457{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003458 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003459 qfline_T *qf_ptr;
3460 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003463 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003464 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003465 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003468 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003469 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003471 if (qi == NULL)
3472 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003473
Bram Moolenaar0398e002019-03-21 21:12:49 +01003474 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003476 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return;
3478 }
3479
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003480 incr_quickfix_busy();
3481
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003482 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003483
3484 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003486 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003488
3489 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3490 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003492 qf_ptr = old_qf_ptr;
3493 qf_index = old_qf_index;
3494 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003497 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003498 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003499 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003500 // No need to print the error message if it's visible in the error
3501 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 print_message = FALSE;
3503
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003504 prev_winid = curwin->w_id;
3505
Bram Moolenaarb2443732018-11-11 22:50:27 +01003506 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003507 if (retval == FAIL)
3508 goto failed;
3509 if (retval == NOTDONE)
3510 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003511
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003512 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003513 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003514 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003516 // Quickfix/location list is freed by an autocmd
3517 qi = NULL;
3518 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003521 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003524 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003525 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003527 // Couldn't open file, so put index back where it was. This could
3528 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 qf_ptr = old_qf_ptr;
3531 qf_index = old_qf_index;
3532 }
3533 }
3534theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003535 if (qi != NULL)
3536 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003537 qfl->qf_ptr = qf_ptr;
3538 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003539 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003540 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003542 // Restore old 'switchbuf' value, but not when an autocommand or
3543 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003544 p_swb = old_swb;
3545 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003547 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548}
3549
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003550// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003551static int qfFileAttr;
3552static int qfSepAttr;
3553static int qfLineAttr;
3554
3555/*
3556 * Display information about a single entry from the quickfix/location list.
3557 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003558 * 'cursel' will be set to TRUE for the currently selected entry in the
3559 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003560 */
3561 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003562qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003563{
3564 char_u *fname;
3565 buf_T *buf;
3566 int filter_entry;
3567
3568 fname = NULL;
3569 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3570 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3571 (char *)qfp->qf_module);
3572 else {
3573 if (qfp->qf_fnum != 0
3574 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3575 {
3576 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003577 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003578 fname = gettail(fname);
3579 }
3580 if (fname == NULL)
3581 sprintf((char *)IObuff, "%2d", qf_idx);
3582 else
3583 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3584 qf_idx, (char *)fname);
3585 }
3586
3587 // Support for filtering entries using :filter /pat/ clist
3588 // Match against the module name, file name, search pattern and
3589 // text of the entry.
3590 filter_entry = TRUE;
3591 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3592 filter_entry &= message_filtered(qfp->qf_module);
3593 if (filter_entry && fname != NULL)
3594 filter_entry &= message_filtered(fname);
3595 if (filter_entry && qfp->qf_pattern != NULL)
3596 filter_entry &= message_filtered(qfp->qf_pattern);
3597 if (filter_entry)
3598 filter_entry &= message_filtered(qfp->qf_text);
3599 if (filter_entry)
3600 return;
3601
3602 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003603 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003604
3605 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003606 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003607 if (qfp->qf_lnum == 0)
3608 IObuff[0] = NUL;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003609 else
thinca6864efa2021-06-19 20:45:20 +02003610 qf_range_text(qfp, IObuff, IOSIZE);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003611 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3612 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003613 msg_puts_attr((char *)IObuff, qfLineAttr);
3614 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003615 if (qfp->qf_pattern != NULL)
3616 {
3617 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003618 msg_puts((char *)IObuff);
3619 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003620 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003621 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003622
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003623 // Remove newlines and leading whitespace from the text. For an
3624 // unrecognized line keep the indent, the compiler may mark a word
3625 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003626 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3627 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3628 IObuff, IOSIZE);
3629 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003630 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003631}
3632
3633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003635 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
3637 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003638qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003640 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003641 qfline_T *qfp;
3642 int i;
3643 int idx1 = 1;
3644 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003645 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003646 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003647 int all = eap->forceit; // if not :cl!, only show
3648 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003649 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003651 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3652 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003653
Bram Moolenaar0398e002019-03-21 21:12:49 +01003654 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003656 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 return;
3658 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003659 if (*arg == '+')
3660 {
3661 ++arg;
3662 plus = TRUE;
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3665 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003666 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return;
3668 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003669 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003670 if (plus)
3671 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003672 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003673 idx2 = i + idx1;
3674 idx1 = i;
3675 }
3676 else
3677 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003678 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003679 if (idx1 < 0)
3680 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3681 if (idx2 < 0)
3682 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003685 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003686 shorten_fnames(FALSE);
3687
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003688 // Get the attributes for the different quickfix highlight items. Note
3689 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003690 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3691 if (qfFileAttr == 0)
3692 qfFileAttr = HL_ATTR(HLF_D);
3693 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3694 if (qfSepAttr == 0)
3695 qfSepAttr = HL_ATTR(HLF_D);
3696 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3697 if (qfLineAttr == 0)
3698 qfLineAttr = HL_ATTR(HLF_N);
3699
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003700 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003702 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003705 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 ui_breakcheck();
3708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709}
3710
3711/*
3712 * Remove newlines and leading whitespace from an error message.
3713 * Put the result in "buf[bufsize]".
3714 */
3715 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003716qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
3718 int i;
3719 char_u *p = text;
3720
3721 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3722 {
3723 if (*p == '\n')
3724 {
3725 buf[i] = ' ';
3726 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003727 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 break;
3729 }
3730 else
3731 buf[i] = *p++;
3732 }
3733 buf[i] = NUL;
3734}
3735
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003736/*
thinca6864efa2021-06-19 20:45:20 +02003737 * Range information from lnum, col, end_lnum, and end_col.
3738 * Put the result in "buf[bufsize]".
3739 */
3740 static void
3741qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3742{
3743 int len;
3744 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3745 len = (int)STRLEN(buf);
3746
3747 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3748 {
3749 vim_snprintf((char *)buf + len, bufsize - len,
3750 "-%ld", qfp->qf_end_lnum);
3751 len += (int)STRLEN(buf + len);
3752 }
3753 if (qfp->qf_col > 0)
3754 {
3755 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3756 len += (int)STRLEN(buf + len);
3757 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3758 {
3759 vim_snprintf((char *)buf + len, bufsize - len,
3760 "-%d", qfp->qf_end_col);
3761 len += (int)STRLEN(buf + len);
3762 }
3763 }
3764 buf[len] = NUL;
3765}
3766
3767/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003768 * Display information (list number, list size and the title) about a
3769 * quickfix/location list.
3770 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003771 static void
3772qf_msg(qf_info_T *qi, int which, char *lead)
3773{
3774 char *title = (char *)qi->qf_lists[which].qf_title;
3775 int count = qi->qf_lists[which].qf_count;
3776 char_u buf[IOSIZE];
3777
3778 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3779 lead,
3780 which + 1,
3781 qi->qf_listcount,
3782 count);
3783
3784 if (title != NULL)
3785 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003786 size_t len = STRLEN(buf);
3787
3788 if (len < 34)
3789 {
3790 vim_memset(buf + len, ' ', 34 - len);
3791 buf[34] = NUL;
3792 }
3793 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003794 }
3795 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003796 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003797}
3798
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799/*
3800 * ":colder [count]": Up in the quickfix stack.
3801 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802 * ":lolder [count]": Up in the location list stack.
3803 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 */
3805 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003806qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003808 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 int count;
3810
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003811 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3812 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (eap->addr_count != 0)
3815 count = eap->line2;
3816 else
3817 count = 1;
3818 while (count--)
3819 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003820 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003822 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003824 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003825 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003827 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829 else
3830 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003831 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003833 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003836 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003839 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003840 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841}
3842
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003843/*
3844 * Display the information about all the quickfix/location lists in the stack
3845 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003846 void
3847qf_history(exarg_T *eap)
3848{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003849 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003850 int i;
3851
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003852 if (eap->addr_count > 0)
3853 {
3854 if (qi == NULL)
3855 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003856 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003857 return;
3858 }
3859
3860 // Jump to the specified quickfix list
3861 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3862 {
3863 qi->qf_curlist = eap->line2 - 1;
3864 qf_msg(qi, qi->qf_curlist, "");
3865 qf_update_buffer(qi, NULL);
3866 }
3867 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003868 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003869
3870 return;
3871 }
3872
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003873 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003874 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003875 else
3876 for (i = 0; i < qi->qf_listcount; ++i)
3877 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3878}
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003881 * Free all the entries in the error list "idx". Note that other information
3882 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 */
3884 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003885qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003887 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003888 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003889 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003891 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003893 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003894 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003895 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003896 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003897 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003898 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003899 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003900 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003901 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003902 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003903 // Somehow qf_count may have an incorrect value, set it to 1
3904 // to avoid crashing when it's wrong.
3905 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003906 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003907 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003908 qfl->qf_start = qfpnext;
3909 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003911
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003912 qfl->qf_index = 0;
3913 qfl->qf_start = NULL;
3914 qfl->qf_last = NULL;
3915 qfl->qf_ptr = NULL;
3916 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003917
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003918 qf_clean_dir_stack(&qfl->qf_dir_stack);
3919 qfl->qf_directory = NULL;
3920 qf_clean_dir_stack(&qfl->qf_file_stack);
3921 qfl->qf_currfile = NULL;
3922 qfl->qf_multiline = FALSE;
3923 qfl->qf_multiignore = FALSE;
3924 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925}
3926
3927/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003928 * Free error list "idx". Frees all the entries in the quickfix list,
3929 * associated context information and the title.
3930 */
3931 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003932qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003933{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003934 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003935
Bram Moolenaard23a8232018-02-10 18:45:26 +01003936 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003937 free_tv(qfl->qf_ctx);
3938 qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02003939 free_callback(&qfl->qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003940 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003941 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003942}
3943
3944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 * qf_mark_adjust: adjust marks
3946 */
3947 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003948qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003949 win_T *wp,
3950 linenr_T line1,
3951 linenr_T line2,
3952 long amount,
3953 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003955 int i;
3956 qfline_T *qfp;
3957 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003958 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003959 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003960 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
Bram Moolenaarc1542742016-07-20 21:44:37 +02003962 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003963 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003964 if (wp != NULL)
3965 {
3966 if (wp->w_llist == NULL)
3967 return;
3968 qi = wp->w_llist;
3969 }
3970
3971 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003972 {
3973 qf_list_T *qfl = qf_get_list(qi, idx);
3974
3975 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003976 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (qfp->qf_fnum == curbuf->b_fnum)
3978 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003979 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3981 {
3982 if (amount == MAXLNUM)
3983 qfp->qf_cleared = TRUE;
3984 else
3985 qfp->qf_lnum += amount;
3986 }
3987 else if (amount_after && qfp->qf_lnum > line2)
3988 qfp->qf_lnum += amount_after;
3989 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003990 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003991
3992 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003993 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994}
3995
3996/*
3997 * Make a nice message out of the error character and the error number:
3998 * char number message
3999 * e or E 0 " error"
4000 * w or W 0 " warning"
4001 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004002 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 * 0 0 ""
4004 * other 0 " c"
4005 * e or E n " error n"
4006 * w or W n " warning n"
4007 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004008 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 * 0 n " error n"
4010 * other n " c n"
4011 * 1 x "" :helpgrep
4012 */
4013 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004014qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
4016 static char_u buf[20];
4017 static char_u cc[3];
4018 char_u *p;
4019
4020 if (c == 'W' || c == 'w')
4021 p = (char_u *)" warning";
4022 else if (c == 'I' || c == 'i')
4023 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004024 else if (c == 'N' || c == 'n')
4025 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4027 p = (char_u *)" error";
4028 else if (c == 0 || c == 1)
4029 p = (char_u *)"";
4030 else
4031 {
4032 cc[0] = ' ';
4033 cc[1] = c;
4034 cc[2] = NUL;
4035 p = cc;
4036 }
4037
4038 if (nr <= 0)
4039 return p;
4040
4041 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4042 return buf;
4043}
4044
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004046 * When "split" is FALSE: Open the entry/result under the cursor.
4047 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4048 */
4049 void
4050qf_view_result(int split)
4051{
4052 qf_info_T *qi = &ql_info;
4053
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004054 if (IS_LL_WINDOW(curwin))
4055 qi = GET_LOC_LIST(curwin);
4056
Bram Moolenaar0398e002019-03-21 21:12:49 +01004057 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004058 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004059 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004060 return;
4061 }
4062
4063 if (split)
4064 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004065 // Open the selected entry in a new window
4066 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4067 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004068 return;
4069 }
4070
4071 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4072}
4073
4074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * ":cwindow": open the quickfix window if we have errors to display,
4076 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004077 * ":lwindow": open the location list window if we have locations to display,
4078 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 */
4080 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004081ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004083 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004084 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 win_T *win;
4086
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004087 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4088 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004089
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004090 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004091
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004092 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004093 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004095 // If a quickfix window is open but we have no errors to display,
4096 // close the window. If a quickfix window is not open, then open
4097 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004098 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004099 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004100 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 if (win != NULL)
4103 ex_cclose(eap);
4104 }
4105 else if (win == NULL)
4106 ex_copen(eap);
4107}
4108
4109/*
4110 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004111 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004114ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004116 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004117 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004119 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4120 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004122 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004123 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (win != NULL)
4125 win_close(win, FALSE);
4126}
4127
4128/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004129 * Set "w:quickfix_title" if "qi" has a title.
4130 */
4131 static void
4132qf_set_title_var(qf_list_T *qfl)
4133{
4134 if (qfl->qf_title != NULL)
4135 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4136}
4137
4138/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004139 * Goto a quickfix or location list window (if present).
4140 * Returns OK if the window is found, FAIL otherwise.
4141 */
4142 static int
4143qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4144{
4145 win_T *win;
4146
4147 win = qf_find_win(qi);
4148 if (win == NULL)
4149 return FAIL;
4150
4151 win_goto(win);
4152 if (resize)
4153 {
4154 if (vertsplit)
4155 {
4156 if (sz != win->w_width)
4157 win_setwidth(sz);
4158 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004159 else if (sz != win->w_height && win->w_height
4160 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004161 win_setheight(sz);
4162 }
4163
4164 return OK;
4165}
4166
4167/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004168 * Set options for the buffer in the quickfix or location list window.
4169 */
4170 static void
4171qf_set_cwindow_options(void)
4172{
4173 // switch off 'swapfile'
4174 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4175 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4176 OPT_LOCAL);
4177 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4178 RESET_BINDING(curwin);
4179#ifdef FEAT_DIFF
4180 curwin->w_p_diff = FALSE;
4181#endif
4182#ifdef FEAT_FOLDING
4183 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4184 OPT_LOCAL);
4185#endif
4186}
4187
4188/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004189 * Open a new quickfix or location list window, load the quickfix buffer and
4190 * set the appropriate options for the window.
4191 * Returns FAIL if the window could not be opened.
4192 */
4193 static int
4194qf_open_new_cwindow(qf_info_T *qi, int height)
4195{
4196 buf_T *qf_buf;
4197 win_T *oldwin = curwin;
4198 tabpage_T *prevtab = curtab;
4199 int flags = 0;
4200 win_T *win;
4201
4202 qf_buf = qf_find_buf(qi);
4203
4204 // The current window becomes the previous window afterwards.
4205 win = curwin;
4206
Bram Moolenaare1004402020-10-24 20:49:43 +02004207 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004208 // Create the new quickfix window at the very bottom, except when
4209 // :belowright or :aboveleft is used.
4210 win_goto(lastwin);
4211 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004212 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004213 flags = WSP_BELOW;
4214 flags |= WSP_NEWLOC;
4215 if (win_split(height, flags) == FAIL)
4216 return FAIL; // not enough room for window
4217 RESET_BINDING(curwin);
4218
4219 if (IS_LL_STACK(qi))
4220 {
4221 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004222 // location list stack from the window 'win'.
4223 curwin->w_llist_ref = qi;
4224 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004225 }
4226
4227 if (oldwin != curwin)
4228 oldwin = NULL; // don't store info when in another window
4229 if (qf_buf != NULL)
4230 {
4231 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004232 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004233 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004234 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004235 }
4236 else
4237 {
4238 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004239 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4240 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004241 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004242
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004243 // save the number of the new buffer
4244 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004245 }
4246
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004247 // Set the options for the quickfix buffer/window (if not already done)
4248 // Do this even if the quickfix buffer was already present, as an autocmd
4249 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004250 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004251 qf_set_cwindow_options();
4252
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004253 // Only set the height when still in the same tab page and there is no
4254 // window to the side.
4255 if (curtab == prevtab && curwin->w_width == Columns)
4256 win_setheight(height);
4257 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4258 if (win_valid(win))
4259 prevwin = win;
4260
4261 return OK;
4262}
4263
4264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004266 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 */
4268 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004269ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004271 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004272 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004274 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004275 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004277 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4278 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004279
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004280 incr_quickfix_busy();
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 if (eap->addr_count != 0)
4283 height = eap->line2;
4284 else
4285 height = QF_WINHEIGHT;
4286
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004287 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288#ifdef FEAT_GUI
4289 need_mouse_correct = TRUE;
4290#endif
4291
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004292 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004293 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004294 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004295 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004296 if (status == FAIL)
4297 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004298 {
4299 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004300 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004303 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004304 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004305 // Save the current index here, as updating the quickfix buffer may free
4306 // the quickfix list
4307 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004308
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004309 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004310 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004312 decr_quickfix_busy();
4313
4314 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 curwin->w_cursor.col = 0;
4316 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004317 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318}
4319
4320/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004321 * Move the cursor in the quickfix window to "lnum".
4322 */
4323 static void
4324qf_win_goto(win_T *win, linenr_T lnum)
4325{
4326 win_T *old_curwin = curwin;
4327
4328 curwin = win;
4329 curbuf = win->w_buffer;
4330 curwin->w_cursor.lnum = lnum;
4331 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004332 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004333 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004334 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004335 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004336 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004337 curwin = old_curwin;
4338 curbuf = curwin->w_buffer;
4339}
4340
4341/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004342 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004343 */
4344 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004345ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004346{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004347 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004348 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004349
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004350 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4351 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004352
4353 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004354 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4355 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4356}
4357
4358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 * Return the number of the current entry (line number in the quickfix
4360 * window).
4361 */
4362 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004363qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004365 qf_info_T *qi = &ql_info;
4366
4367 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004368 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004369 qi = wp->w_llist_ref;
4370
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004371 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372}
4373
4374/*
4375 * Update the cursor position in the quickfix window to the current error.
4376 * Return TRUE if there is a quickfix window.
4377 */
4378 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004379qf_win_pos_update(
4380 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004381 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
4383 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004384 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004386 // Put the cursor on the current error in the quickfix window, so that
4387 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004388 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 if (win != NULL
4390 && qf_index <= win->w_buffer->b_ml.ml_line_count
4391 && old_qf_index != qf_index)
4392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (qf_index > old_qf_index)
4394 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004395 win->w_redraw_top = old_qf_index;
4396 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 else
4399 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004400 win->w_redraw_top = qf_index;
4401 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004403 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
4405 return win != NULL;
4406}
4407
4408/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004409 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004410 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004411 */
4412 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004413is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004414{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004415 // A window displaying the quickfix buffer will have the w_llist_ref field
4416 // set to NULL.
4417 // A window displaying a location list buffer will have the w_llist_ref
4418 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004419 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004420 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4421 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004422 return TRUE;
4423
4424 return FALSE;
4425}
4426
4427/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004428 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4429 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004430 */
4431 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004432qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004433{
4434 win_T *win;
4435
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004436 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004437 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004438 return win;
4439 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004440}
4441
4442/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004443 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004444 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 */
4446 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004447qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004449 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004450 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004452 if (qi->qf_bufnr != INVALID_QFBUFNR)
4453 {
4454 buf_T *qfbuf;
4455 qfbuf = buflist_findnr(qi->qf_bufnr);
4456 if (qfbuf != NULL)
4457 return qfbuf;
4458 // buffer is no longer present
4459 qi->qf_bufnr = INVALID_QFBUFNR;
4460 }
4461
Bram Moolenaar9c102382006-05-03 21:26:49 +00004462 FOR_ALL_TAB_WINDOWS(tp, win)
4463 if (is_qf_win(win, qi))
4464 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004465
Bram Moolenaar9c102382006-05-03 21:26:49 +00004466 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467}
4468
4469/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004470 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004471 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004472 */
4473 int
4474qf_process_qftf_option(void)
4475{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004476 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004477}
4478
4479/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004480 * Update the w:quickfix_title variable in the quickfix/location list window in
4481 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004482 */
4483 static void
4484qf_update_win_titlevar(qf_info_T *qi)
4485{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004486 qf_list_T *qfl = qf_get_curlist(qi);
4487 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004488 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004489 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004490
Bram Moolenaar530bed92020-12-16 21:02:56 +01004491 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004492 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004493 if (is_qf_win(win, qi))
4494 {
4495 curwin = win;
4496 qf_set_title_var(qfl);
4497 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004498 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004499 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004500}
4501
4502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 * Find the quickfix buffer. If it exists, update the contents.
4504 */
4505 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004506qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507{
4508 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004509 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004512 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004513 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 if (buf != NULL)
4515 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004516 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004517 int qf_winid = 0;
4518
4519 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004520 {
4521 if (curwin->w_llist == qi)
4522 win = curwin;
4523 else
4524 {
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004525 // Find the file window (non-quickfix) with this location list
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004526 win = qf_find_win_with_loclist(qi);
4527 if (win == NULL)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004528 // File window is not found. Find the location list window.
4529 win = qf_find_win(qi);
4530 if (win == NULL)
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004531 return;
4532 }
4533 qf_winid = win->w_id;
4534 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004535
4536 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004537 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004538 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaard823fa92016-08-12 16:29:27 +02004540 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004541
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004542 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004543 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004544
Bram Moolenaar864293a2016-06-02 13:40:04 +02004545 if (old_last == NULL)
4546 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004547 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004548
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004549 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004550 aucmd_restbuf(&aco);
4551 }
4552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004553 // Only redraw when added lines are visible. This avoids flickering
4554 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004555 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4556 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558}
4559
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004560/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004561 * Add an error line to the quickfix buffer.
4562 */
4563 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004564qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004565 buf_T *buf, // quickfix window buffer
4566 linenr_T lnum,
4567 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004568 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004569 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004570 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004571{
4572 int len;
4573 buf_T *errbuf;
4574
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004575 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004576 // for this entry, then use it.
4577 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004578 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004579 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004580 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004581 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004582 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004583 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4584 len = (int)STRLEN(IObuff);
4585 }
4586 else if (qfp->qf_fnum != 0
4587 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4588 && errbuf->b_fname != NULL)
4589 {
4590 if (qfp->qf_type == 1) // :helpgrep
4591 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4592 else
4593 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004594 // Shorten the file name if not done already.
4595 // For optimization, do this only for the first entry in a
4596 // buffer.
4597 if (first_bufline && (errbuf->b_sfname == NULL
4598 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004599 {
4600 if (*dirname == NUL)
4601 mch_dirname(dirname, MAXPATHL);
4602 shorten_buf_fname(errbuf, dirname, FALSE);
4603 }
4604 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4605 }
4606 len = (int)STRLEN(IObuff);
4607 }
4608 else
4609 len = 0;
4610
4611 if (len < IOSIZE - 1)
4612 IObuff[len++] = '|';
4613
4614 if (qfp->qf_lnum > 0)
4615 {
thinca6864efa2021-06-19 20:45:20 +02004616 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004617 len += (int)STRLEN(IObuff + len);
4618
Bram Moolenaar858ba062020-05-31 23:11:59 +02004619 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4620 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004621 len += (int)STRLEN(IObuff + len);
4622 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004623 else if (qfp->qf_pattern != NULL)
4624 {
4625 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4626 len += (int)STRLEN(IObuff + len);
4627 }
4628 if (len < IOSIZE - 2)
4629 {
4630 IObuff[len++] = '|';
4631 IObuff[len++] = ' ';
4632 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004633
Bram Moolenaar858ba062020-05-31 23:11:59 +02004634 // Remove newlines and leading whitespace from the text.
4635 // For an unrecognized line keep the indent, the compiler may
4636 // mark a word with ^^^^.
4637 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4638 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004639 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004640
4641 if (ml_append_buf(buf, lnum, IObuff,
4642 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4643 return FAIL;
4644
4645 return OK;
4646}
4647
Bram Moolenaard43906d2020-07-20 21:31:32 +02004648/*
4649 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4650 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4651 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004652 static list_T *
4653call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4654{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004655 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004656 list_T *qftf_list = NULL;
4657
4658 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4659 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4660 // set.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004661 if (qfl->qftf_cb.cb_name != NULL)
4662 cb = &qfl->qftf_cb;
4663 if (cb != NULL && cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004664 {
4665 typval_T args[1];
4666 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004667 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004668
4669 // create the dict argument
4670 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4671 return NULL;
4672 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4673 dict_add_number(d, "winid", (long)qf_winid);
4674 dict_add_number(d, "id", (long)qfl->qf_id);
4675 dict_add_number(d, "start_idx", start_idx);
4676 dict_add_number(d, "end_idx", end_idx);
4677 ++d->dv_refcount;
4678 args[0].v_type = VAR_DICT;
4679 args[0].vval.v_dict = d;
4680
Bram Moolenaard43906d2020-07-20 21:31:32 +02004681 qftf_list = NULL;
4682 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4683 {
4684 if (rettv.v_type == VAR_LIST)
4685 {
4686 qftf_list = rettv.vval.v_list;
4687 qftf_list->lv_refcount++;
4688 }
4689 clear_tv(&rettv);
4690 }
4691 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004692 }
4693
4694 return qftf_list;
4695}
4696
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 * Fill current buffer with quickfix errors, replacing any previous contents.
4699 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004700 * If "old_last" is not NULL append the items after this one.
4701 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4702 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 */
4704 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004705qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004707 linenr_T lnum;
4708 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004709 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004710 list_T *qftf_list = NULL;
4711 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
Bram Moolenaar864293a2016-06-02 13:40:04 +02004713 if (old_last == NULL)
4714 {
4715 if (buf != curbuf)
4716 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004717 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004718 return;
4719 }
4720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004721 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004722 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004723 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004726 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004727 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004729 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004730 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004731 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004732
4733 *dirname = NUL;
4734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004735 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004736 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004737 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004738 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004739 lnum = 0;
4740 }
4741 else
4742 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004743 if (old_last->qf_next != NULL)
4744 qfp = old_last->qf_next;
4745 else
4746 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004747 lnum = buf->b_ml.ml_line_count;
4748 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004749
4750 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4751 (long)qfl->qf_count);
4752 if (qftf_list != NULL)
4753 qftf_li = qftf_list->lv_first;
4754
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004755 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004757 char_u *qftf_str = NULL;
4758
Bram Moolenaard43906d2020-07-20 21:31:32 +02004759 // Use the text supplied by the user defined function (if any).
4760 // If the returned value is not string, then ignore the rest
4761 // of the returned values and use the default.
4762 if (qftf_li != NULL && !invalid_val)
4763 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004764 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004765 if (qftf_str == NULL)
4766 invalid_val = TRUE;
4767 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004768
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004769 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4770 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004772
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004773 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004774 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004776 if (qfp == NULL)
4777 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004778
4779 if (qftf_li != NULL)
4780 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004782
4783 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004784 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004785 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004788 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 check_lnums(TRUE);
4790
Bram Moolenaar864293a2016-06-02 13:40:04 +02004791 if (old_last == NULL)
4792 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004793 // Set the 'filetype' to "qf" each time after filling the buffer.
4794 // This resembles reading a file into a buffer, it's more logical when
4795 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004796 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004797 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4798 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004800 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004801 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004803 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004805 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004806 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004807
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004808 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004809 redraw_curbuf_later(NOT_VALID);
4810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004812 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 KeyTyped = old_KeyTyped;
4814}
4815
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004816/*
4817 * For every change made to the quickfix list, update the changed tick.
4818 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004819 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004820qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004821{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004822 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004823}
4824
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004826 * Return the quickfix/location list number with the given identifier.
4827 * Returns -1 if list is not found.
4828 */
4829 static int
4830qf_id2nr(qf_info_T *qi, int_u qfid)
4831{
4832 int qf_idx;
4833
4834 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4835 if (qi->qf_lists[qf_idx].qf_id == qfid)
4836 return qf_idx;
4837 return INVALID_QFIDX;
4838}
4839
4840/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004841 * If the current list is not "save_qfid" and we can find the list with that ID
4842 * then make it the current list.
4843 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004844 * Returns OK if successfully restored the list. Returns FAIL if the list with
4845 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004846 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004847 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004848qf_restore_list(qf_info_T *qi, int_u save_qfid)
4849{
4850 int curlist;
4851
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004852 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004853 {
4854 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004855 if (curlist < 0)
4856 // list is not present
4857 return FAIL;
4858 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004859 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004860 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004861}
4862
4863/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004864 * Jump to the first entry if there is one.
4865 */
4866 static void
4867qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4868{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004869 if (qf_restore_list(qi, save_qfid) == FAIL)
4870 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004871
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004872 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004873 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004874 qf_jump(qi, 0, 0, forceit);
4875}
4876
4877/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004878 * Return TRUE when using ":vimgrep" for ":grep".
4879 */
4880 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004881grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004882{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004883 return ((cmdidx == CMD_grep
4884 || cmdidx == CMD_lgrep
4885 || cmdidx == CMD_grepadd
4886 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004887 && STRCMP("internal",
4888 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4889}
4890
4891/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004892 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004894 static char_u *
4895make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004897 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004898 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004899 case CMD_make: return (char_u *)"make";
4900 case CMD_lmake: return (char_u *)"lmake";
4901 case CMD_grep: return (char_u *)"grep";
4902 case CMD_lgrep: return (char_u *)"lgrep";
4903 case CMD_grepadd: return (char_u *)"grepadd";
4904 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4905 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907}
4908
4909/*
4910 * Return the name for the errorfile, in allocated memory.
4911 * Find a new unique name when 'makeef' contains "##".
4912 * Returns NULL for error.
4913 */
4914 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004915get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
4917 char_u *p;
4918 char_u *name;
4919 static int start = -1;
4920 static int off = 0;
4921#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004922 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923#endif
4924
4925 if (*p_mef == NUL)
4926 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004927 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004929 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return name;
4931 }
4932
4933 for (p = p_mef; *p; ++p)
4934 if (p[0] == '#' && p[1] == '#')
4935 break;
4936
4937 if (*p == NUL)
4938 return vim_strsave(p_mef);
4939
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004940 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 for (;;)
4942 {
4943 if (start == -1)
4944 start = mch_get_pid();
4945 else
4946 off += 19;
4947
Bram Moolenaar964b3742019-05-24 18:54:09 +02004948 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (name == NULL)
4950 break;
4951 STRCPY(name, p_mef);
4952 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4953 STRCAT(name, p + 2);
4954 if (mch_getperm(name) < 0
4955#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004956 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 && mch_lstat((char *)name, &sb) < 0
4958#endif
4959 )
4960 break;
4961 vim_free(name);
4962 }
4963 return name;
4964}
4965
4966/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004967 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4968 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4969 */
4970 static char_u *
4971make_get_fullcmd(char_u *makecmd, char_u *fname)
4972{
4973 char_u *cmd;
4974 unsigned len;
4975
4976 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4977 if (*p_sp != NUL)
4978 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4979 cmd = alloc(len);
4980 if (cmd == NULL)
4981 return NULL;
4982 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4983 (char *)p_shq);
4984
4985 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4986 if (*p_sp != NUL)
4987 append_redir(cmd, len, p_sp, fname);
4988
4989 // Display the fully formed command. Output a newline if there's something
4990 // else than the :make command that was typed (in which case the cursor is
4991 // in column 0).
4992 if (msg_col == 0)
4993 msg_didout = FALSE;
4994 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004995 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004996 msg_outtrans(cmd); // show what we are doing
4997
4998 return cmd;
4999}
5000
5001/*
5002 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5003 */
5004 void
5005ex_make(exarg_T *eap)
5006{
5007 char_u *fname;
5008 char_u *cmd;
5009 char_u *enc = NULL;
5010 win_T *wp = NULL;
5011 qf_info_T *qi = &ql_info;
5012 int res;
5013 char_u *au_name = NULL;
5014 int_u save_qfid;
5015
5016 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5017 if (grep_internal(eap->cmdidx))
5018 {
5019 ex_vimgrep(eap);
5020 return;
5021 }
5022
5023 au_name = make_get_auname(eap->cmdidx);
5024 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5025 curbuf->b_fname, TRUE, curbuf))
5026 {
5027#ifdef FEAT_EVAL
5028 if (aborting())
5029 return;
5030#endif
5031 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005032 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005033
5034 if (is_loclist_cmd(eap->cmdidx))
5035 wp = curwin;
5036
5037 autowrite_all();
5038 fname = get_mef_name();
5039 if (fname == NULL)
5040 return;
5041 mch_remove(fname); // in case it's not unique
5042
5043 cmd = make_get_fullcmd(eap->arg, fname);
5044 if (cmd == NULL)
5045 return;
5046
5047 // let the shell know if we are redirecting output or not
5048 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5049
5050#ifdef AMIGA
5051 out_flush();
5052 // read window status report and redraw before message
5053 (void)char_avail();
5054#endif
5055
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005056 incr_quickfix_busy();
5057
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005058 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
5059 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
5060 (eap->cmdidx != CMD_grepadd
5061 && eap->cmdidx != CMD_lgrepadd),
5062 qf_cmdtitle(*eap->cmdlinep), enc);
5063 if (wp != NULL)
5064 {
5065 qi = GET_LOC_LIST(wp);
5066 if (qi == NULL)
5067 goto cleanup;
5068 }
5069 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005070 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005071
5072 // Remember the current quickfix list identifier, so that we can
5073 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005074 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005075 if (au_name != NULL)
5076 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5077 curbuf->b_fname, TRUE, curbuf);
5078 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5079 // display the first error
5080 qf_jump_first(qi, save_qfid, FALSE);
5081
5082cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005083 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005084 mch_remove(fname);
5085 vim_free(fname);
5086 vim_free(cmd);
5087}
5088
5089/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005090 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005091 */
5092 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005093qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005094{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005095 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005096
5097 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5098 return 0;
5099 return qf_get_curlist(qi)->qf_count;
5100}
5101
5102/*
5103 * Returns the number of valid entries in the current quickfix/location list.
5104 */
5105 int
5106qf_get_valid_size(exarg_T *eap)
5107{
5108 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005109 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005110 qfline_T *qfp;
5111 int i, sz = 0;
5112 int prev_fnum = 0;
5113
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005114 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5115 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005116
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005117 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005118 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005119 {
5120 if (qfp->qf_valid)
5121 {
5122 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005123 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005124 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5125 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005126 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005127 sz++;
5128 prev_fnum = qfp->qf_fnum;
5129 }
5130 }
5131 }
5132
5133 return sz;
5134}
5135
5136/*
5137 * Returns the current index of the quickfix/location list.
5138 * Returns 0 if there is an error.
5139 */
5140 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005141qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005142{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005143 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005144
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005145 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5146 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005147
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005148 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005149}
5150
5151/*
5152 * Returns the current index in the quickfix/location list (counting only valid
5153 * entries). If no valid entries are in the list, then returns 1.
5154 */
5155 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005156qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005157{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005158 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005159 qf_list_T *qfl;
5160 qfline_T *qfp;
5161 int i, eidx = 0;
5162 int prev_fnum = 0;
5163
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005164 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5165 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005166
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005167 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005168 qfp = qfl->qf_start;
5169
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005170 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005171 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005172 return 1;
5173
5174 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5175 {
5176 if (qfp->qf_valid)
5177 {
5178 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5179 {
5180 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5181 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005182 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005183 eidx++;
5184 prev_fnum = qfp->qf_fnum;
5185 }
5186 }
5187 else
5188 eidx++;
5189 }
5190 }
5191
5192 return eidx ? eidx : 1;
5193}
5194
5195/*
5196 * Get the 'n'th valid error entry in the quickfix or location list.
5197 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5198 * For :cdo and :ldo returns the 'n'th valid error entry.
5199 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5200 */
5201 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005202qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005203{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005204 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005205 int i, eidx;
5206 int prev_fnum = 0;
5207
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005208 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005209 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005210 return 1;
5211
Bram Moolenaar95946f12019-03-31 15:31:59 +02005212 eidx = 0;
5213 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005214 {
5215 if (qfp->qf_valid)
5216 {
5217 if (fdo)
5218 {
5219 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5220 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005221 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005222 eidx++;
5223 prev_fnum = qfp->qf_fnum;
5224 }
5225 }
5226 else
5227 eidx++;
5228 }
5229
5230 if (eidx == n)
5231 break;
5232 }
5233
5234 if (i <= qfl->qf_count)
5235 return i;
5236 else
5237 return 1;
5238}
5239
5240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005242 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005243 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 */
5245 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005246ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005248 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005249 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005250
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005251 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5252 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005253
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005254 if (eap->addr_count > 0)
5255 errornr = (int)eap->line2;
5256 else
5257 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005258 switch (eap->cmdidx)
5259 {
5260 case CMD_cc: case CMD_ll:
5261 errornr = 0;
5262 break;
5263 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5264 case CMD_lfirst:
5265 errornr = 1;
5266 break;
5267 default:
5268 errornr = 32767;
5269 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005270 }
5271
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005272 // For cdo and ldo commands, jump to the nth valid error.
5273 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005274 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5275 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005276 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005277 eap->addr_count > 0 ? (int)eap->line1 : 1,
5278 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5279
5280 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281}
5282
5283/*
5284 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005285 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005286 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 */
5288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005289ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005291 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005292 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005293 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005294
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005295 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5296 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005297
Bram Moolenaar55b69262017-08-13 13:42:01 +02005298 if (eap->addr_count > 0
5299 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5300 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005301 errornr = (int)eap->line2;
5302 else
5303 errornr = 1;
5304
Bram Moolenaar39665952018-08-15 20:59:48 +02005305 // Depending on the command jump to either next or previous entry/file.
5306 switch (eap->cmdidx)
5307 {
5308 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5309 dir = FORWARD;
5310 break;
5311 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5312 case CMD_lNext:
5313 dir = BACKWARD;
5314 break;
5315 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5316 dir = FORWARD_FILE;
5317 break;
5318 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5319 dir = BACKWARD_FILE;
5320 break;
5321 default:
5322 dir = FORWARD;
5323 break;
5324 }
5325
5326 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327}
5328
5329/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005330 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5331 * The index of the entry is stored in 'errornr'.
5332 * Returns NULL if an entry is not found.
5333 */
5334 static qfline_T *
5335qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5336{
5337 qfline_T *qfp = NULL;
5338 int idx = 0;
5339
5340 // Find the first entry in this file
5341 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5342 if (qfp->qf_fnum == bnr)
5343 break;
5344
5345 *errornr = idx;
5346 return qfp;
5347}
5348
5349/*
5350 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5351 * with the error number for the first entry. Assumes the entries are sorted in
5352 * the quickfix list by line number.
5353 */
5354 static qfline_T *
5355qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5356{
5357 while (!got_int
5358 && entry->qf_prev != NULL
5359 && entry->qf_fnum == entry->qf_prev->qf_fnum
5360 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5361 {
5362 entry = entry->qf_prev;
5363 --*errornr;
5364 }
5365
5366 return entry;
5367}
5368
5369/*
5370 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5371 * with the error number for the last entry. Assumes the entries are sorted in
5372 * the quickfix list by line number.
5373 */
5374 static qfline_T *
5375qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5376{
5377 while (!got_int &&
5378 entry->qf_next != NULL
5379 && entry->qf_fnum == entry->qf_next->qf_fnum
5380 && entry->qf_lnum == entry->qf_next->qf_lnum)
5381 {
5382 entry = entry->qf_next;
5383 ++*errornr;
5384 }
5385
5386 return entry;
5387}
5388
5389/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005390 * Returns TRUE if the specified quickfix entry is
5391 * after the given line (linewise is TRUE)
5392 * or after the line and column.
5393 */
5394 static int
5395qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5396{
5397 if (linewise)
5398 return qfp->qf_lnum > pos->lnum;
5399 else
5400 return (qfp->qf_lnum > pos->lnum ||
5401 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5402}
5403
5404/*
5405 * Returns TRUE if the specified quickfix entry is
5406 * before the given line (linewise is TRUE)
5407 * or before the line and column.
5408 */
5409 static int
5410qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5411{
5412 if (linewise)
5413 return qfp->qf_lnum < pos->lnum;
5414 else
5415 return (qfp->qf_lnum < pos->lnum ||
5416 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5417}
5418
5419/*
5420 * Returns TRUE if the specified quickfix entry is
5421 * on or after the given line (linewise is TRUE)
5422 * or on or after the line and column.
5423 */
5424 static int
5425qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5426{
5427 if (linewise)
5428 return qfp->qf_lnum >= pos->lnum;
5429 else
5430 return (qfp->qf_lnum > pos->lnum ||
5431 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5432}
5433
5434/*
5435 * Returns TRUE if the specified quickfix entry is
5436 * on or before the given line (linewise is TRUE)
5437 * or on or before the line and column.
5438 */
5439 static int
5440qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5441{
5442 if (linewise)
5443 return qfp->qf_lnum <= pos->lnum;
5444 else
5445 return (qfp->qf_lnum < pos->lnum ||
5446 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5447}
5448
5449/*
5450 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5451 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5452 * multiple entries on a single line as one. Otherwise returns the entry after
5453 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005454 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5455 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005456 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005457 */
5458 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005459qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005460 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005461 pos_T *pos,
5462 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005463 qfline_T *qfp,
5464 int *errornr)
5465{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005466 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005467 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005468 return qfp;
5469
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005470 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005471 while (qfp->qf_next != NULL
5472 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005473 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005474 {
5475 qfp = qfp->qf_next;
5476 ++*errornr;
5477 }
5478
5479 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005480 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005481 return NULL;
5482
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005483 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005484 qfp = qfp->qf_next;
5485 ++*errornr;
5486
5487 return qfp;
5488}
5489
5490/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005491 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5492 * If 'linewise' is TRUE, returns the entry before the specified line and
5493 * treats multiple entries on a single line as one. Otherwise returns the entry
5494 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005495 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5496 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005497 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005498 */
5499 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005500qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005501 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005502 pos_T *pos,
5503 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005504 qfline_T *qfp,
5505 int *errornr)
5506{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005507 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005508 while (qfp->qf_next != NULL
5509 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005510 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005511 {
5512 qfp = qfp->qf_next;
5513 ++*errornr;
5514 }
5515
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005516 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005517 return NULL;
5518
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005519 if (linewise)
5520 // If multiple entries are on the same line, then use the first entry
5521 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005522
5523 return qfp;
5524}
5525
5526/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005527 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005528 * the direction 'dir'.
5529 */
5530 static qfline_T *
5531qf_find_closest_entry(
5532 qf_list_T *qfl,
5533 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005534 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005535 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005536 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005537 int *errornr)
5538{
5539 qfline_T *qfp;
5540
5541 *errornr = 0;
5542
5543 // Find the first entry in this file
5544 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5545 if (qfp == NULL)
5546 return NULL; // no entry in this file
5547
5548 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005549 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005550 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005551 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005552
5553 return qfp;
5554}
5555
5556/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005557 * Get the nth quickfix entry below the specified entry. Searches forward in
5558 * the list. If linewise is TRUE, then treat multiple entries on a single line
5559 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005560 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005561 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005562qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005563{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005564 qfline_T *entry = entry_arg;
5565
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005566 while (n-- > 0 && !got_int)
5567 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005568 int first_errornr = *errornr;
5569
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005570 if (linewise)
5571 // Treat all the entries on the same line in this file as one
5572 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005573
5574 if (entry->qf_next == NULL
5575 || entry->qf_next->qf_fnum != entry->qf_fnum)
5576 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005577 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005578 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005579 break;
5580 }
5581
5582 entry = entry->qf_next;
5583 ++*errornr;
5584 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005585}
5586
5587/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005588 * Get the nth quickfix entry above the specified entry. Searches backwards in
5589 * the list. If linewise is TRUE, then treat multiple entries on a single line
5590 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005591 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005592 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005593qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005594{
5595 while (n-- > 0 && !got_int)
5596 {
5597 if (entry->qf_prev == NULL
5598 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5599 break;
5600
5601 entry = entry->qf_prev;
5602 --*errornr;
5603
5604 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005605 if (linewise)
5606 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005607 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005608}
5609
5610/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005611 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5612 * the specified direction. Returns the error number in the quickfix list or 0
5613 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005614 */
5615 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005616qf_find_nth_adj_entry(
5617 qf_list_T *qfl,
5618 int bnr,
5619 pos_T *pos,
5620 int n,
5621 int dir,
5622 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005623{
5624 qfline_T *adj_entry;
5625 int errornr;
5626
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005627 // Find an entry closest to the specified position
5628 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005629 if (adj_entry == NULL)
5630 return 0;
5631
5632 if (--n > 0)
5633 {
5634 // Go to the n'th entry in the current buffer
5635 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005636 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005637 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005638 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005639 }
5640
5641 return errornr;
5642}
5643
5644/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005645 * Jump to a quickfix entry in the current file nearest to the current line or
5646 * current line/col.
5647 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5648 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005649 */
5650 void
5651ex_cbelow(exarg_T *eap)
5652{
5653 qf_info_T *qi;
5654 qf_list_T *qfl;
5655 int dir;
5656 int buf_has_flag;
5657 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005658 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005659
5660 if (eap->addr_count > 0 && eap->line2 <= 0)
5661 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005662 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005663 return;
5664 }
5665
5666 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005667 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5668 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005669 buf_has_flag = BUF_HAS_QF_ENTRY;
5670 else
5671 buf_has_flag = BUF_HAS_LL_ENTRY;
5672 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5673 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005674 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005675 return;
5676 }
5677
5678 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5679 return;
5680
5681 qfl = qf_get_curlist(qi);
5682 // check if the list has valid errors
5683 if (!qf_list_has_valid_entries(qfl))
5684 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005685 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005686 return;
5687 }
5688
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005689 if (eap->cmdidx == CMD_cbelow
5690 || eap->cmdidx == CMD_lbelow
5691 || eap->cmdidx == CMD_cafter
5692 || eap->cmdidx == CMD_lafter)
5693 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005694 dir = FORWARD;
5695 else
5696 dir = BACKWARD;
5697
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005698 pos = curwin->w_cursor;
5699 // A quickfix entry column number is 1 based whereas cursor column
5700 // number is 0 based. Adjust the column number.
5701 pos.col++;
5702 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5703 eap->addr_count > 0 ? eap->line2 : 0, dir,
5704 eap->cmdidx == CMD_cbelow
5705 || eap->cmdidx == CMD_lbelow
5706 || eap->cmdidx == CMD_cabove
5707 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005708
5709 if (errornr > 0)
5710 qf_jump(qi, 0, errornr, FALSE);
5711 else
5712 emsg(_(e_no_more_items));
5713}
5714
5715/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005716 * Return the autocmd name for the :cfile Ex commands
5717 */
5718 static char_u *
5719cfile_get_auname(cmdidx_T cmdidx)
5720{
5721 switch (cmdidx)
5722 {
5723 case CMD_cfile: return (char_u *)"cfile";
5724 case CMD_cgetfile: return (char_u *)"cgetfile";
5725 case CMD_caddfile: return (char_u *)"caddfile";
5726 case CMD_lfile: return (char_u *)"lfile";
5727 case CMD_lgetfile: return (char_u *)"lgetfile";
5728 case CMD_laddfile: return (char_u *)"laddfile";
5729 default: return NULL;
5730 }
5731}
5732
5733/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005734 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005735 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 */
5737 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005738ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005740 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005741 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005742 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005743 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005744 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005745 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005746
Bram Moolenaara16123a2019-03-28 20:31:07 +01005747 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005748 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5749 NULL, FALSE, curbuf))
5750 {
5751#ifdef FEAT_EVAL
5752 if (aborting())
5753 return;
5754#endif
5755 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005756
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005757 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005758#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005759 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005760 {
5761 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005762 NULL, NULL,
5763 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005764 if (browse_file == NULL)
5765 return;
5766 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5767 vim_free(browse_file);
5768 }
5769 else
5770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005772 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005773
Bram Moolenaar39665952018-08-15 20:59:48 +02005774 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005775 wp = curwin;
5776
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005777 incr_quickfix_busy();
5778
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005779 // This function is used by the :cfile, :cgetfile and :caddfile
5780 // commands.
5781 // :cfile always creates a new quickfix list and jumps to the
5782 // first error.
5783 // :cgetfile creates a new quickfix list but doesn't jump to the
5784 // first error.
5785 // :caddfile adds to an existing quickfix list. If there is no
5786 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005787 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005788 && eap->cmdidx != CMD_laddfile),
5789 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005790 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005791 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005792 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005793 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005794 {
5795 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005796 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005797 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005798 }
5799 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005800 qf_list_changed(qf_get_curlist(qi));
5801 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005802 if (au_name != NULL)
5803 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005804
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005805 // Jump to the first error for a new list and if autocmds didn't
5806 // free the list.
5807 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5808 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005809 // display the first error
5810 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005811
5812 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005813}
5814
5815/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005816 * Return the vimgrep autocmd name.
5817 */
5818 static char_u *
5819vgr_get_auname(cmdidx_T cmdidx)
5820{
5821 switch (cmdidx)
5822 {
5823 case CMD_vimgrep: return (char_u *)"vimgrep";
5824 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5825 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5826 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5827 case CMD_grep: return (char_u *)"grep";
5828 case CMD_lgrep: return (char_u *)"lgrep";
5829 case CMD_grepadd: return (char_u *)"grepadd";
5830 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5831 default: return NULL;
5832 }
5833}
5834
5835/*
5836 * Initialize the regmatch used by vimgrep for pattern "s".
5837 */
5838 static void
5839vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5840{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005841 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005842 regmatch->regprog = NULL;
5843
5844 if (s == NULL || *s == NUL)
5845 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005846 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005847 if (last_search_pat() == NULL)
5848 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005849 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005850 return;
5851 }
5852 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5853 }
5854 else
5855 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5856
5857 regmatch->rmm_ic = p_ic;
5858 regmatch->rmm_maxcol = 0;
5859}
5860
5861/*
5862 * Display a file name when vimgrep is running.
5863 */
5864 static void
5865vgr_display_fname(char_u *fname)
5866{
5867 char_u *p;
5868
5869 msg_start();
5870 p = msg_strtrunc(fname, TRUE);
5871 if (p == NULL)
5872 msg_outtrans(fname);
5873 else
5874 {
5875 msg_outtrans(p);
5876 vim_free(p);
5877 }
5878 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005879 msg_didout = FALSE; // overwrite this message
5880 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005881 msg_col = 0;
5882 out_flush();
5883}
5884
5885/*
5886 * Load a dummy buffer to search for a pattern using vimgrep.
5887 */
5888 static buf_T *
5889vgr_load_dummy_buf(
5890 char_u *fname,
5891 char_u *dirname_start,
5892 char_u *dirname_now)
5893{
5894 int save_mls;
5895#if defined(FEAT_SYN_HL)
5896 char_u *save_ei = NULL;
5897#endif
5898 buf_T *buf;
5899
5900#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005901 // Don't do Filetype autocommands to avoid loading syntax and
5902 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005903 save_ei = au_event_disable(",Filetype");
5904#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005905 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005906 save_mls = p_mls;
5907 p_mls = 0;
5908
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005909 // Load file into a buffer, so that 'fileencoding' is detected,
5910 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005911 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5912
5913 p_mls = save_mls;
5914#if defined(FEAT_SYN_HL)
5915 au_event_restore(save_ei);
5916#endif
5917
5918 return buf;
5919}
5920
5921/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005922 * Check whether a quickfix/location list is valid. Autocmds may remove or
5923 * change a quickfix list when vimgrep is running. If the list is not found,
5924 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005925 */
5926 static int
5927vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005928 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005929 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005930 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005931 char_u *title)
5932{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005933 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005934 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005935 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005936 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005937 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005938 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005939 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005940 return FALSE;
5941 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005942 else
5943 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005944 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005945 qf_new_list(qi, title);
5946 return TRUE;
5947 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005948 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005949
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005950 if (qf_restore_list(qi, qfid) == FAIL)
5951 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005952
5953 return TRUE;
5954}
5955
5956/*
5957 * Search for a pattern in all the lines in a buffer and add the matching lines
5958 * to a quickfix list.
5959 */
5960 static int
5961vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005962 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005963 char_u *fname,
5964 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005965 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005966 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005967 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005968 int duplicate_name,
5969 int flags)
5970{
5971 int found_match = FALSE;
5972 long lnum;
5973 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02005974 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005975
Bram Moolenaar1c299432018-10-28 14:36:09 +01005976 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005977 {
5978 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005979 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005980 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005981 // Regular expression match
5982 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5983 col, NULL, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005984 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005985 // Pass the buffer number so that it gets used even for a
5986 // dummy buffer, unless duplicate_name is set, then the
5987 // buffer will be wiped out below.
5988 if (qf_add_entry(qfl,
5989 NULL, // dir
5990 fname,
5991 NULL,
5992 duplicate_name ? 0 : buf->b_fnum,
5993 ml_get_buf(buf,
5994 regmatch->startpos[0].lnum + lnum, FALSE),
5995 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02005996 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005997 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02005998 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005999 FALSE, // vis_col
6000 NULL, // search pattern
6001 0, // nr
6002 0, // type
6003 TRUE // valid
6004 ) == QF_FAIL)
6005 {
6006 got_int = TRUE;
6007 break;
6008 }
6009 found_match = TRUE;
6010 if (--*tomatch == 0)
6011 break;
6012 if ((flags & VGR_GLOBAL) == 0
6013 || regmatch->endpos[0].lnum > 0)
6014 break;
6015 col = regmatch->endpos[0].col
6016 + (col == regmatch->endpos[0].col);
6017 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6018 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006019 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006020 }
6021 else
6022 {
6023 char_u *str = ml_get_buf(buf, lnum, FALSE);
6024 int score;
6025 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006026 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006027
6028 // Fuzzy string match
6029 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6030 {
6031 // Pass the buffer number so that it gets used even for a
6032 // dummy buffer, unless duplicate_name is set, then the
6033 // buffer will be wiped out below.
6034 if (qf_add_entry(qfl,
6035 NULL, // dir
6036 fname,
6037 NULL,
6038 duplicate_name ? 0 : buf->b_fnum,
6039 str,
6040 lnum,
thinca6864efa2021-06-19 20:45:20 +02006041 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006042 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006043 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006044 FALSE, // vis_col
6045 NULL, // search pattern
6046 0, // nr
6047 0, // type
6048 TRUE // valid
6049 ) == QF_FAIL)
6050 {
6051 got_int = TRUE;
6052 break;
6053 }
6054 found_match = TRUE;
6055 if (--*tomatch == 0)
6056 break;
6057 if ((flags & VGR_GLOBAL) == 0)
6058 break;
6059 col = matches[pat_len - 1] + col + 1;
6060 if (col > (colnr_T)STRLEN(str))
6061 break;
6062 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006063 }
6064 line_breakcheck();
6065 if (got_int)
6066 break;
6067 }
6068
6069 return found_match;
6070}
6071
6072/*
6073 * Jump to the first match and update the directory.
6074 */
6075 static void
6076vgr_jump_to_match(
6077 qf_info_T *qi,
6078 int forceit,
6079 int *redraw_for_dummy,
6080 buf_T *first_match_buf,
6081 char_u *target_dir)
6082{
6083 buf_T *buf;
6084
6085 buf = curbuf;
6086 qf_jump(qi, 0, 0, forceit);
6087 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006088 // If we jumped to another buffer redrawing will already be
6089 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006090 *redraw_for_dummy = FALSE;
6091
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006092 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006093 if (curbuf == first_match_buf && target_dir != NULL)
6094 {
6095 exarg_T ea;
6096
Bram Moolenaara80faa82020-04-12 19:37:17 +02006097 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006098 ea.arg = target_dir;
6099 ea.cmdidx = CMD_lcd;
6100 ex_cd(&ea);
6101 }
6102}
6103
6104/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006105 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006106 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006107typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006108{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006109 long tomatch; // maximum number of matches to find
6110 char_u *spat; // search pattern
6111 int flags; // search modifier
6112 char_u **fnames; // list of files to search
6113 int fcount; // number of files
6114 regmmatch_T regmatch; // compiled search pattern
6115 char_u *qf_title; // quickfix list title
6116} vgr_args_T;
6117
6118/*
6119 * Process :vimgrep command arguments. The command syntax is:
6120 *
6121 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6122 */
6123 static int
6124vgr_process_args(
6125 exarg_T *eap,
6126 vgr_args_T *args)
6127{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006128 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006129
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006130 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006131
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006132 args->regmatch.regprog = NULL;
6133 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006134
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006135 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006136 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006137 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006138 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006139
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006140 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006141 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006142 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006143 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006144 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006145 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006146 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006147
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006148 vgr_init_regmatch(&args->regmatch, args->spat);
6149 if (args->regmatch.regprog == NULL)
6150 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006151
6152 p = skipwhite(p);
6153 if (*p == NUL)
6154 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006155 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006156 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006157 }
6158
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006159 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006160 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6161 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006162 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006163 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006164 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006165 }
6166
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006167 return OK;
6168}
6169
6170/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006171 * Return TRUE if "buf" had an existing swap file, the current swap file does
6172 * not end in ".swp".
6173 */
6174 static int
6175existing_swapfile(buf_T *buf)
6176{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006177 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006178 {
6179 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6180 size_t len = STRLEN(fname);
6181
6182 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6183 }
6184 return FALSE;
6185}
6186
6187/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006188 * Search for a pattern in a list of files and populate the quickfix list with
6189 * the matches.
6190 */
6191 static int
6192vgr_process_files(
6193 win_T *wp,
6194 qf_info_T *qi,
6195 vgr_args_T *cmd_args,
6196 int *redraw_for_dummy,
6197 buf_T **first_match_buf,
6198 char_u **target_dir)
6199{
6200 int status = FAIL;
6201 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6202 time_t seconds = 0;
6203 char_u *fname;
6204 int fi;
6205 buf_T *buf;
6206 int duplicate_name = FALSE;
6207 int using_dummy;
6208 char_u *dirname_start = NULL;
6209 char_u *dirname_now = NULL;
6210 int found_match;
6211 aco_save_T aco;
6212
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006213 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6214 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006215 if (dirname_start == NULL || dirname_now == NULL)
6216 goto theend;
6217
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006218 // Remember the current directory, because a BufRead autocommand that does
6219 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006220 mch_dirname(dirname_start, MAXPATHL);
6221
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006222 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006223 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6224 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006225 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006226 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006227 if (time(NULL) > seconds)
6228 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006229 // Display the file name every second or so, show the user we are
6230 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006231 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006232 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006233 }
6234
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006235 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006236 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6237 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006238 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006239 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006240 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006241 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006242
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006243 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006244 }
6245 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006246 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006247 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006248
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006249 // Check whether the quickfix list is still valid. When loading a
6250 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006251 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006252 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006253
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006254 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006255
Bram Moolenaar81695252004-12-29 20:58:21 +00006256 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006257 {
6258 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006259 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006260 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006261 else
6262 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006263 // Try for a match in all lines of the buffer.
6264 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006265 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006266 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006267 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006268
Bram Moolenaar81695252004-12-29 20:58:21 +00006269 if (using_dummy)
6270 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006271 if (found_match && *first_match_buf == NULL)
6272 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006273 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006274 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006275 // Never keep a dummy buffer if there is another buffer
6276 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006277 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006278 buf = NULL;
6279 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006280 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006281 || buf->b_p_bh[0] == 'u' // "unload"
6282 || buf->b_p_bh[0] == 'w' // "wipe"
6283 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006284 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006285 // When no match was found we don't need to remember the
6286 // buffer, wipe it out. If there was a match and it
6287 // wasn't the first one or we won't jump there: only
6288 // unload the buffer.
6289 // Ignore 'hidden' here, because it may lead to having too
6290 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006291 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006292 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006293 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006294 buf = NULL;
6295 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006296 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006297 || (cmd_args->flags & VGR_NOJUMP)
6298 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006299 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006300 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006301 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006302 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006303 buf = NULL;
6304 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006305 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006306
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006307 if (buf != NULL)
6308 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006309 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006310 buf->b_flags &= ~BF_DUMMY;
6311
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006312 // If the buffer is still loaded we need to use the
6313 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006314 if (buf == *first_match_buf
6315 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006316 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006317 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006318
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006319 // The buffer is still loaded, the Filetype autocommands
6320 // need to be done now, in that buffer. And the modelines
6321 // need to be done (again). But not the window-local
6322 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006323 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006324#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006325 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6326 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006327#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006328 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006329 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006330 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006331 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006332 }
6333 }
6334
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006335 status = OK;
6336
6337theend:
6338 vim_free(dirname_now);
6339 vim_free(dirname_start);
6340 return status;
6341}
6342
6343/*
6344 * ":vimgrep {pattern} file(s)"
6345 * ":vimgrepadd {pattern} file(s)"
6346 * ":lvimgrep {pattern} file(s)"
6347 * ":lvimgrepadd {pattern} file(s)"
6348 */
6349 void
6350ex_vimgrep(exarg_T *eap)
6351{
6352 vgr_args_T args;
6353 qf_info_T *qi;
6354 qf_list_T *qfl;
6355 int_u save_qfid;
6356 win_T *wp = NULL;
6357 int redraw_for_dummy = FALSE;
6358 buf_T *first_match_buf = NULL;
6359 char_u *target_dir = NULL;
6360 char_u *au_name = NULL;
6361 int status;
6362
6363 au_name = vgr_get_auname(eap->cmdidx);
6364 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6365 curbuf->b_fname, TRUE, curbuf))
6366 {
6367#ifdef FEAT_EVAL
6368 if (aborting())
6369 return;
6370#endif
6371 }
6372
6373 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6374 if (qi == NULL)
6375 return;
6376
6377 if (vgr_process_args(eap, &args) == FAIL)
6378 goto theend;
6379
6380 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6381 && eap->cmdidx != CMD_vimgrepadd
6382 && eap->cmdidx != CMD_lvimgrepadd)
6383 || qf_stack_empty(qi))
6384 // make place for a new list
6385 qf_new_list(qi, args.qf_title);
6386
6387 incr_quickfix_busy();
6388
6389 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6390 &first_match_buf, &target_dir);
6391 if (status != OK)
6392 {
6393 FreeWild(args.fcount, args.fnames);
6394 decr_quickfix_busy();
6395 goto theend;
6396 }
6397
6398 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006399
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006400 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006401 qfl->qf_nonevalid = FALSE;
6402 qfl->qf_ptr = qfl->qf_start;
6403 qfl->qf_index = 1;
6404 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006405
Bram Moolenaar864293a2016-06-02 13:40:04 +02006406 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006407
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006408 // Remember the current quickfix list identifier, so that we can check for
6409 // autocommands changing the current quickfix list.
6410 save_qfid = qf_get_curlist(qi)->qf_id;
6411
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006412 if (au_name != NULL)
6413 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6414 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006415 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6416 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006417 if (!qflist_valid(wp, save_qfid)
6418 || qf_restore_list(qi, save_qfid) == FAIL)
6419 {
6420 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006421 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006422 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006423
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006424 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006425 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006427 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006428 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6429 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006431 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006432 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006433
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006434 decr_quickfix_busy();
6435
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006436 // If we loaded a dummy buffer into the current window, the autocommands
6437 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006438 if (redraw_for_dummy)
6439 {
6440#ifdef FEAT_FOLDING
6441 foldUpdateAll(curwin);
6442#else
6443 redraw_later(NOT_VALID);
6444#endif
6445 }
6446
Bram Moolenaar86b68352004-12-27 21:59:20 +00006447theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006448 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006449 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006450 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006451}
6452
6453/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006454 * Restore current working directory to "dirname_start" if they differ, taking
6455 * into account whether it is set locally or globally.
6456 */
6457 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006458restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006459{
6460 char_u *dirname_now = alloc(MAXPATHL);
6461
6462 if (NULL != dirname_now)
6463 {
6464 mch_dirname(dirname_now, MAXPATHL);
6465 if (STRCMP(dirname_start, dirname_now) != 0)
6466 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006467 // If the directory has changed, change it back by building up an
6468 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006469 exarg_T ea;
6470
Bram Moolenaara80faa82020-04-12 19:37:17 +02006471 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006472 ea.arg = dirname_start;
6473 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6474 ex_cd(&ea);
6475 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006476 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006477 }
6478}
6479
6480/*
6481 * Load file "fname" into a dummy buffer and return the buffer pointer,
6482 * placing the directory resulting from the buffer load into the
6483 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6484 * prior to calling this function. Restores directory to "dirname_start" prior
6485 * to returning, if autocmds or the 'autochdir' option have changed it.
6486 *
6487 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6488 * or wipe_dummy_buffer() later!
6489 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006490 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006491 */
6492 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006493load_dummy_buffer(
6494 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006495 char_u *dirname_start, // in: old directory
6496 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006497{
6498 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006499 bufref_T newbufref;
6500 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006501 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006502 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006503 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006504
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006505 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006506 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6507 if (newbuf == NULL)
6508 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006509 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006510
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006511 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006512 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6513
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006514 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006515 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006516 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006517 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006518 ++newbuf->b_locked;
6519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006520 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006521 aucmd_prepbuf(&aco, newbuf);
6522
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006523 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006524 (void)setfname(curbuf, fname, NULL, FALSE);
6525
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006526 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006527 check_need_swap(TRUE);
6528
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006529 // Remove the "dummy" flag, otherwise autocommands may not
6530 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006531 curbuf->b_flags &= ~BF_DUMMY;
6532
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006533 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006534 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006535 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006536 NULL, READ_NEW | READ_DUMMY);
6537 --newbuf->b_locked;
6538 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006539 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006540 && !(curbuf->b_flags & BF_NEW))
6541 {
6542 failed = FALSE;
6543 if (curbuf != newbuf)
6544 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006545 // Bloody autocommands changed the buffer! Can happen when
6546 // using netrw and editing a remote file. Use the current
6547 // buffer instead, delete the dummy one after restoring the
6548 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006549 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006550 newbuf = curbuf;
6551 }
6552 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006554 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006555 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006556 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6557 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006558
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006559 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6560 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006561 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006562 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006563
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006564 // When autocommands/'autochdir' option changed directory: go back.
6565 // Let the caller know what the resulting dir was first, in case it is
6566 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006567 mch_dirname(resulting_dir, MAXPATHL);
6568 restore_start_dir(dirname_start);
6569
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006570 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006571 return NULL;
6572 if (failed)
6573 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006574 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006575 return NULL;
6576 }
6577 return newbuf;
6578}
6579
6580/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006581 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6582 * directory to "dirname_start" prior to returning, if autocmds or the
6583 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006584 */
6585 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006586wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006587{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006588 // If any autocommand opened a window on the dummy buffer, close that
6589 // window. If we can't close them all then give up.
6590 while (buf->b_nwindows > 0)
6591 {
6592 int did_one = FALSE;
6593 win_T *wp;
6594
6595 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006596 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006597 if (wp->w_buffer == buf)
6598 {
6599 if (win_close(wp, FALSE) == OK)
6600 did_one = TRUE;
6601 break;
6602 }
6603 if (!did_one)
6604 return;
6605 }
6606
6607 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006608 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006609#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006610 cleanup_T cs;
6611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006612 // Reset the error/interrupt/exception state here so that aborting()
6613 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6614 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006615 enter_cleanup(&cs);
6616#endif
6617
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006618 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006619
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006620#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006621 // Restore the error/interrupt/exception state if not discarded by a
6622 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006623 leave_cleanup(&cs);
6624#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006625 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006626 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006627 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006628}
6629
6630/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006631 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6632 * directory to "dirname_start" prior to returning, if autocmds or the
6633 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006634 */
6635 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006636unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006637{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006638 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006639 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006640 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006641
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006642 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006643 restore_start_dir(dirname_start);
6644 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006645}
6646
Bram Moolenaar05159a02005-02-26 23:04:13 +00006647#if defined(FEAT_EVAL) || defined(PROTO)
6648/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006649 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006650 * to 'list'. Returns OK on success.
6651 */
6652 static int
6653get_qfline_items(qfline_T *qfp, list_T *list)
6654{
6655 int bufnum;
6656 dict_T *dict;
6657 char_u buf[2];
6658
6659 // Handle entries with a non-existing buffer number.
6660 bufnum = qfp->qf_fnum;
6661 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6662 bufnum = 0;
6663
6664 if ((dict = dict_alloc()) == NULL)
6665 return FAIL;
6666 if (list_append_dict(list, dict) == FAIL)
6667 return FAIL;
6668
6669 buf[0] = qfp->qf_type;
6670 buf[1] = NUL;
6671 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006672 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6673 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6674 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6675 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6676 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6677 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006678 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6679 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6680 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6681 || dict_add_string(dict, "type", buf) == FAIL
6682 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6683 return FAIL;
6684
6685 return OK;
6686}
6687
6688/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006689 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006690 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006691 * If eidx is not 0, then return only the specified entry. Otherwise return
6692 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006693 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006694 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006695get_errorlist(
6696 qf_info_T *qi_arg,
6697 win_T *wp,
6698 int qf_idx,
6699 int eidx,
6700 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006701{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006702 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006703 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006704 qfline_T *qfp;
6705 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006706
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006707 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006708 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006709 qi = &ql_info;
6710 if (wp != NULL)
6711 {
6712 qi = GET_LOC_LIST(wp);
6713 if (qi == NULL)
6714 return FAIL;
6715 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006716 }
6717
Bram Moolenaar858ba062020-05-31 23:11:59 +02006718 if (eidx < 0)
6719 return OK;
6720
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006721 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006722 qf_idx = qi->qf_curlist;
6723
Bram Moolenaar0398e002019-03-21 21:12:49 +01006724 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006725 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006726
Bram Moolenaar0398e002019-03-21 21:12:49 +01006727 qfl = qf_get_list(qi, qf_idx);
6728 if (qf_list_empty(qfl))
6729 return FAIL;
6730
Bram Moolenaara16123a2019-03-28 20:31:07 +01006731 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006732 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006733 if (eidx > 0)
6734 {
6735 if (eidx == i)
6736 return get_qfline_items(qfp, list);
6737 }
6738 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006739 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006740 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006741
Bram Moolenaar05159a02005-02-26 23:04:13 +00006742 return OK;
6743}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006744
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006745// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006746enum {
6747 QF_GETLIST_NONE = 0x0,
6748 QF_GETLIST_TITLE = 0x1,
6749 QF_GETLIST_ITEMS = 0x2,
6750 QF_GETLIST_NR = 0x4,
6751 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006752 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006753 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006754 QF_GETLIST_IDX = 0x40,
6755 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006756 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006757 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006758 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006759 QF_GETLIST_QFTF = 0x800,
6760 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006761};
6762
6763/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006764 * Parse text from 'di' and return the quickfix list items.
6765 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006766 */
6767 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006768qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006769{
6770 int status = FAIL;
6771 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006772 char_u *errorformat = p_efm;
6773 dictitem_T *efm_di;
6774 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006775
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006776 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006777 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006778 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006779 // If errorformat is supplied then use it, otherwise use the 'efm'
6780 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006781 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6782 {
6783 if (efm_di->di_tv.v_type != VAR_STRING ||
6784 efm_di->di_tv.vval.v_string == NULL)
6785 return FAIL;
6786 errorformat = efm_di->di_tv.vval.v_string;
6787 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006788
Bram Moolenaar36538222017-09-02 19:51:44 +02006789 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006790 if (l == NULL)
6791 return FAIL;
6792
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006793 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006794 if (qi != NULL)
6795 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006796 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006797 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6798 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006799 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006800 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006801 }
6802 free(qi);
6803 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006804 dict_add_list(retdict, "items", l);
6805 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006806 }
6807
6808 return status;
6809}
6810
6811/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006812 * Return the quickfix/location list window identifier in the current tabpage.
6813 */
6814 static int
6815qf_winid(qf_info_T *qi)
6816{
6817 win_T *win;
6818
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006819 // The quickfix window can be opened even if the quickfix list is not set
6820 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006821 if (qi == NULL)
6822 return 0;
6823 win = qf_find_win(qi);
6824 if (win != NULL)
6825 return win->w_id;
6826 return 0;
6827}
6828
6829/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006830 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006831 * window. If there is no buffer associated with the list or the buffer is
6832 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006833 */
6834 static int
6835qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6836{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006837 int bufnum = 0;
6838
6839 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6840 bufnum = qi->qf_bufnr;
6841
6842 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006843}
6844
6845/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006846 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006847 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006848 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006849qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006850{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006851 int flags = QF_GETLIST_NONE;
6852
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006853 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006854 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006855 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006856 if (!loclist)
6857 // File window ID is applicable only to location list windows
6858 flags &= ~ QF_GETLIST_FILEWINID;
6859 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006860
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006861 if (dict_find(what, (char_u *)"title", -1) != NULL)
6862 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006863
Bram Moolenaara6d48492017-12-12 22:45:31 +01006864 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6865 flags |= QF_GETLIST_NR;
6866
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006867 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6868 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006869
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006870 if (dict_find(what, (char_u *)"context", -1) != NULL)
6871 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006872
Bram Moolenaara6d48492017-12-12 22:45:31 +01006873 if (dict_find(what, (char_u *)"id", -1) != NULL)
6874 flags |= QF_GETLIST_ID;
6875
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006876 if (dict_find(what, (char_u *)"items", -1) != NULL)
6877 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006878
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006879 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6880 flags |= QF_GETLIST_IDX;
6881
6882 if (dict_find(what, (char_u *)"size", -1) != NULL)
6883 flags |= QF_GETLIST_SIZE;
6884
Bram Moolenaarb254af32017-12-18 19:48:58 +01006885 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6886 flags |= QF_GETLIST_TICK;
6887
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006888 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6889 flags |= QF_GETLIST_FILEWINID;
6890
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006891 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6892 flags |= QF_GETLIST_QFBUFNR;
6893
Bram Moolenaard43906d2020-07-20 21:31:32 +02006894 if (dict_find(what, (char_u *)"quickfixtextfunc", -1) != NULL)
6895 flags |= QF_GETLIST_QFTF;
6896
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006897 return flags;
6898}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006899
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006900/*
6901 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6902 * If 'nr' and 'id' are not present in 'what' then return the current
6903 * quickfix list index.
6904 * If 'nr' is zero then return the current quickfix list index.
6905 * If 'nr' is '$' then return the last quickfix list index.
6906 * If 'id' is present then return the index of the quickfix list with that id.
6907 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6908 * Return -1, if quickfix list is not present or if the stack is empty.
6909 */
6910 static int
6911qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6912{
6913 int qf_idx;
6914 dictitem_T *di;
6915
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006916 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006917 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6918 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006919 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006920 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006921 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006922 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006923 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006924 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006925 qf_idx = di->di_tv.vval.v_number - 1;
6926 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006927 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006928 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006929 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006930 else if (di->di_tv.v_type == VAR_STRING
6931 && di->di_tv.vval.v_string != NULL
6932 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006933 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006934 qf_idx = qi->qf_listcount - 1;
6935 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006936 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006937 }
6938
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006939 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6940 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006941 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006942 if (di->di_tv.v_type == VAR_NUMBER)
6943 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006944 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006945 if (di->di_tv.vval.v_number != 0)
6946 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6947 }
6948 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006949 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006950 }
6951
6952 return qf_idx;
6953}
6954
6955/*
6956 * Return default values for quickfix list properties in retdict.
6957 */
6958 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006959qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006960{
6961 int status = OK;
6962
6963 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006964 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006965 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6966 {
6967 list_T *l = list_alloc();
6968 if (l != NULL)
6969 status = dict_add_list(retdict, "items", l);
6970 else
6971 status = FAIL;
6972 }
6973 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006974 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006975 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006976 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006977 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006978 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006979 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006980 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006981 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006982 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006983 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006984 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006985 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006986 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006987 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006988 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006989 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6990 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02006991 if ((status == OK) && (flags & QF_GETLIST_QFTF))
6992 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006993
6994 return status;
6995}
6996
6997/*
6998 * Return the quickfix list title as 'title' in retdict
6999 */
7000 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007001qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007002{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007003 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007004}
7005
7006/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007007 * Returns the identifier of the window used to display files from a location
7008 * list. If there is no associated window, then returns 0. Useful only when
7009 * called from a location list window.
7010 */
7011 static int
7012qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7013{
7014 int winid = 0;
7015
7016 if (wp != NULL && IS_LL_WINDOW(wp))
7017 {
7018 win_T *ll_wp = qf_find_win_with_loclist(qi);
7019 if (ll_wp != NULL)
7020 winid = ll_wp->w_id;
7021 }
7022
7023 return dict_add_number(retdict, "filewinid", winid);
7024}
7025
7026/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007027 * Return the quickfix list items/entries as 'items' in retdict.
7028 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007029 */
7030 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007031qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007032{
7033 int status = OK;
7034 list_T *l = list_alloc();
7035 if (l != NULL)
7036 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007037 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007038 dict_add_list(retdict, "items", l);
7039 }
7040 else
7041 status = FAIL;
7042
7043 return status;
7044}
7045
7046/*
7047 * Return the quickfix list context (if any) as 'context' in retdict.
7048 */
7049 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007050qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007051{
7052 int status;
7053 dictitem_T *di;
7054
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007055 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007056 {
7057 di = dictitem_alloc((char_u *)"context");
7058 if (di != NULL)
7059 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007060 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007061 status = dict_add(retdict, di);
7062 if (status == FAIL)
7063 dictitem_free(di);
7064 }
7065 else
7066 status = FAIL;
7067 }
7068 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007069 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007070
7071 return status;
7072}
7073
7074/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007075 * Return the current quickfix list index as 'idx' in retdict.
7076 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007077 */
7078 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007079qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007080{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007081 if (eidx == 0)
7082 {
7083 eidx = qfl->qf_index;
7084 if (qf_list_empty(qfl))
7085 // For empty lists, current index is set to 0
7086 eidx = 0;
7087 }
7088 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007089}
7090
7091/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007092 * Return the 'quickfixtextfunc' function of a quickfix/location list
7093 */
7094 static int
7095qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7096{
7097 int status;
7098
7099 if (qfl->qftf_cb.cb_name != NULL)
7100 {
7101 typval_T tv;
7102
7103 put_callback(&qfl->qftf_cb, &tv);
7104 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7105 clear_tv(&tv);
7106 }
7107 else
7108 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7109
7110 return status;
7111}
7112
7113/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007114 * Return quickfix/location list details (title) as a
7115 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7116 * then current list is used. Otherwise the specified list is used.
7117 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007118 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007119qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7120{
7121 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007122 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007123 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007124 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007125 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007126 dictitem_T *di;
7127 int flags = QF_GETLIST_NONE;
7128
7129 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7130 return qf_get_list_from_lines(what, di, retdict);
7131
7132 if (wp != NULL)
7133 qi = GET_LOC_LIST(wp);
7134
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007135 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007136
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007137 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007138 qf_idx = qf_getprop_qfidx(qi, what);
7139
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007140 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007141 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007142 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007143
Bram Moolenaar0398e002019-03-21 21:12:49 +01007144 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007145
Bram Moolenaar858ba062020-05-31 23:11:59 +02007146 // If an entry index is specified, use that
7147 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7148 {
7149 if (di->di_tv.v_type != VAR_NUMBER)
7150 return FAIL;
7151 eidx = di->di_tv.vval.v_number;
7152 }
7153
Bram Moolenaard823fa92016-08-12 16:29:27 +02007154 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007155 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007156 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007157 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007158 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007159 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007160 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007161 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007162 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007163 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007164 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007165 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007166 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007167 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007168 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007169 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007170 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007171 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007172 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7173 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007174 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7175 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007176 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7177 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007178
Bram Moolenaard823fa92016-08-12 16:29:27 +02007179 return status;
7180}
7181
7182/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007183 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007184 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7185 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007186 */
7187 static int
7188qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007189 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007190 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007191 int first_entry,
7192 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007193{
7194 static int did_bufnr_emsg;
7195 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007196 int bufnum, valid, status, col, end_col, vcol, nr;
7197 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007198
7199 if (first_entry)
7200 did_bufnr_emsg = FALSE;
7201
Bram Moolenaar8f667172018-12-14 15:38:31 +01007202 filename = dict_get_string(d, (char_u *)"filename", TRUE);
7203 module = dict_get_string(d, (char_u *)"module", TRUE);
7204 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
7205 lnum = (int)dict_get_number(d, (char_u *)"lnum");
thinca6864efa2021-06-19 20:45:20 +02007206 end_lnum = (int)dict_get_number(d, (char_u *)"end_lnum");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007207 col = (int)dict_get_number(d, (char_u *)"col");
thinca6864efa2021-06-19 20:45:20 +02007208 end_col = (int)dict_get_number(d, (char_u *)"end_col");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007209 vcol = (int)dict_get_number(d, (char_u *)"vcol");
7210 nr = (int)dict_get_number(d, (char_u *)"nr");
7211 type = dict_get_string(d, (char_u *)"type", TRUE);
7212 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
7213 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007214 if (text == NULL)
7215 text = vim_strsave((char_u *)"");
7216
7217 valid = TRUE;
7218 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7219 valid = FALSE;
7220
7221 // Mark entries with non-existing buffer number as not valid. Give the
7222 // error message only once.
7223 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7224 {
7225 if (!did_bufnr_emsg)
7226 {
7227 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007228 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007229 }
7230 valid = FALSE;
7231 bufnum = 0;
7232 }
7233
7234 // If the 'valid' field is present it overrules the detected value.
7235 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar401f0c02020-09-05 22:37:39 +02007236 valid = (int)dict_get_bool(d, (char_u *)"valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007237
Bram Moolenaar0398e002019-03-21 21:12:49 +01007238 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007239 NULL, // dir
7240 filename,
7241 module,
7242 bufnum,
7243 text,
7244 lnum,
thinca6864efa2021-06-19 20:45:20 +02007245 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007246 col,
thinca6864efa2021-06-19 20:45:20 +02007247 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007248 vcol, // vis_col
7249 pattern, // search pattern
7250 nr,
7251 type == NULL ? NUL : *type,
7252 valid);
7253
7254 vim_free(filename);
7255 vim_free(module);
7256 vim_free(pattern);
7257 vim_free(text);
7258 vim_free(type);
7259
Bram Moolenaar9752c722018-12-22 16:49:34 +01007260 if (valid)
7261 *valid_entry = TRUE;
7262
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007263 return status;
7264}
7265
7266/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007267 * Add list of entries to quickfix/location list. Each list entry is
7268 * a dictionary with item information.
7269 */
7270 static int
7271qf_add_entries(
7272 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007273 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007274 list_T *list,
7275 char_u *title,
7276 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007277{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007278 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007279 listitem_T *li;
7280 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007281 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007282 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007283 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007284
Bram Moolenaara3921f42017-06-04 15:30:34 +02007285 if (action == ' ' || qf_idx == qi->qf_listcount)
7286 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007287 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007288 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007289 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007290 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007291 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007292 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007293 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007294 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007295 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007296 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007297 qf_free_items(qfl);
7298 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007299 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007300
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007301 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007302 {
7303 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007304 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007305
7306 d = li->li_tv.vval.v_dict;
7307 if (d == NULL)
7308 continue;
7309
Bram Moolenaar0398e002019-03-21 21:12:49 +01007310 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007311 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007312 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007313 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007314 }
7315
Bram Moolenaar9752c722018-12-22 16:49:34 +01007316 // Check if any valid error entries are added to the list.
7317 if (valid_entry)
7318 qfl->qf_nonevalid = FALSE;
7319 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007320 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007321 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007322
7323 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007324 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007325 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007326
7327 // Update the current error index if not appending to the list or if the
7328 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007329 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007330 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007331
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007332 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007333 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007334
7335 return retval;
7336}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007337
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007338/*
7339 * Get the quickfix list index from 'nr' or 'id'
7340 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007341 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007342qf_setprop_get_qfidx(
7343 qf_info_T *qi,
7344 dict_T *what,
7345 int action,
7346 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007347{
7348 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007349 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007350
Bram Moolenaard823fa92016-08-12 16:29:27 +02007351 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7352 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007353 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007354 if (di->di_tv.v_type == VAR_NUMBER)
7355 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007356 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007357 if (di->di_tv.vval.v_number != 0)
7358 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007359
Bram Moolenaar55b69262017-08-13 13:42:01 +02007360 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7361 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007362 // When creating a new list, accept qf_idx pointing to the next
7363 // non-available list and add the new list at the end of the
7364 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007365 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007366 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007367 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007368 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007369 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007370 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007371 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007372 }
7373 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007374 && di->di_tv.vval.v_string != NULL
7375 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007376 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007377 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007378 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007379 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007380 qf_idx = 0;
7381 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007382 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007383 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007384 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007385 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007386 }
7387
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007388 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007389 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007390 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007391 if (di->di_tv.v_type != VAR_NUMBER)
7392 return INVALID_QFIDX;
7393
7394 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007395 }
7396
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007397 return qf_idx;
7398}
7399
7400/*
7401 * Set the quickfix list title.
7402 */
7403 static int
7404qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7405{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007406 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007407
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007408 if (di->di_tv.v_type != VAR_STRING)
7409 return FAIL;
7410
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007411 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007412 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007413 if (qf_idx == qi->qf_curlist)
7414 qf_update_win_titlevar(qi);
7415
7416 return OK;
7417}
7418
7419/*
7420 * Set quickfix list items/entries.
7421 */
7422 static int
7423qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7424{
7425 int retval = FAIL;
7426 char_u *title_save;
7427
7428 if (di->di_tv.v_type != VAR_LIST)
7429 return FAIL;
7430
7431 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7432 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7433 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007434 vim_free(title_save);
7435
7436 return retval;
7437}
7438
7439/*
7440 * Set quickfix list items/entries from a list of lines.
7441 */
7442 static int
7443qf_setprop_items_from_lines(
7444 qf_info_T *qi,
7445 int qf_idx,
7446 dict_T *what,
7447 dictitem_T *di,
7448 int action)
7449{
7450 char_u *errorformat = p_efm;
7451 dictitem_T *efm_di;
7452 int retval = FAIL;
7453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007454 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007455 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7456 {
7457 if (efm_di->di_tv.v_type != VAR_STRING ||
7458 efm_di->di_tv.vval.v_string == NULL)
7459 return FAIL;
7460 errorformat = efm_di->di_tv.vval.v_string;
7461 }
7462
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007463 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007464 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7465 return FAIL;
7466
7467 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007468 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007469 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007470 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007471 retval = OK;
7472
7473 return retval;
7474}
7475
7476/*
7477 * Set quickfix list context.
7478 */
7479 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007480qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007481{
7482 typval_T *ctx;
7483
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007484 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007485 ctx = alloc_tv();
7486 if (ctx != NULL)
7487 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007488 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007489
7490 return OK;
7491}
7492
7493/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007494 * Set the current index in the specified quickfix list
7495 */
7496 static int
7497qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7498{
7499 int denote = FALSE;
7500 int newidx;
7501 int old_qfidx;
7502 qfline_T *qf_ptr;
7503
7504 // If the specified index is '$', then use the last entry
7505 if (di->di_tv.v_type == VAR_STRING
7506 && di->di_tv.vval.v_string != NULL
7507 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7508 newidx = qfl->qf_count;
7509 else
7510 {
7511 // Otherwise use the specified index
7512 newidx = tv_get_number_chk(&di->di_tv, &denote);
7513 if (denote)
7514 return FAIL;
7515 }
7516
7517 if (newidx < 1) // sanity check
7518 return FAIL;
7519 if (newidx > qfl->qf_count)
7520 newidx = qfl->qf_count;
7521
7522 old_qfidx = qfl->qf_index;
7523 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7524 if (qf_ptr == NULL)
7525 return FAIL;
7526 qfl->qf_ptr = qf_ptr;
7527 qfl->qf_index = newidx;
7528
7529 // If the current list is modified and it is displayed in the quickfix
7530 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007531 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007532 qf_win_pos_update(qi, old_qfidx);
7533
7534 return OK;
7535}
7536
7537/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007538 * Set the current index in the specified quickfix list
7539 */
7540 static int
7541qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7542{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007543 callback_T cb;
7544
7545 free_callback(&qfl->qftf_cb);
7546 cb = get_callback(&di->di_tv);
7547 if (cb.cb_name != NULL && *cb.cb_name != NUL)
7548 set_callback(&qfl->qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007549
7550 return OK;
7551}
7552
7553/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007554 * Set quickfix/location list properties (title, items, context).
7555 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007556 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007557 */
7558 static int
7559qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7560{
7561 dictitem_T *di;
7562 int retval = FAIL;
7563 int qf_idx;
7564 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007565 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007566
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007567 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007568 newlist = TRUE;
7569
7570 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007571 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007572 return FAIL;
7573
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007574 if (newlist)
7575 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007576 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007577 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007578 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007579 }
7580
Bram Moolenaar0398e002019-03-21 21:12:49 +01007581 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007582 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007583 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007584 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007585 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007586 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007587 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007588 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007589 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007590 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7591 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007592 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7593 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007594
Bram Moolenaar287153c2020-11-29 14:20:27 +01007595 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007596 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007597 if (newlist)
7598 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007599
Bram Moolenaard823fa92016-08-12 16:29:27 +02007600 return retval;
7601}
7602
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007603/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007604 * Free the entire quickfix/location list stack.
7605 * If the quickfix/location list window is open, then clear it.
7606 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007607 static void
7608qf_free_stack(win_T *wp, qf_info_T *qi)
7609{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007610 win_T *qfwin = qf_find_win(qi);
7611 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007612
7613 if (qfwin != NULL)
7614 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007615 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007616 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007617 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007618 qf_update_buffer(qi, NULL);
7619 }
7620
7621 if (wp != NULL && IS_LL_WINDOW(wp))
7622 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007623 // If in the location list window, then use the non-location list
7624 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007625 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007626 if (llwin != NULL)
7627 wp = llwin;
7628 }
7629
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007630 qf_free_all(wp);
7631 if (wp == NULL)
7632 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007633 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007634 qi->qf_curlist = 0;
7635 qi->qf_listcount = 0;
7636 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007637 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007638 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007639 // If the location list window is open, then create a new empty
7640 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007641 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007642
Bram Moolenaar95946f12019-03-31 15:31:59 +02007643 if (new_ll != NULL)
7644 {
7645 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007646
Bram Moolenaar95946f12019-03-31 15:31:59 +02007647 // first free the list reference in the location list window
7648 ll_free_all(&qfwin->w_llist_ref);
7649
7650 qfwin->w_llist_ref = new_ll;
7651 if (wp != qfwin)
7652 win_set_loclist(wp, new_ll);
7653 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007654 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007655}
7656
Bram Moolenaard823fa92016-08-12 16:29:27 +02007657/*
7658 * Populate the quickfix list with the items supplied in the list
7659 * of dictionaries. "title" will be copied to w:quickfix_title.
7660 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007661 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007662 */
7663 int
7664set_errorlist(
7665 win_T *wp,
7666 list_T *list,
7667 int action,
7668 char_u *title,
7669 dict_T *what)
7670{
7671 qf_info_T *qi = &ql_info;
7672 int retval = OK;
7673
7674 if (wp != NULL)
7675 {
7676 qi = ll_get_or_alloc_list(wp);
7677 if (qi == NULL)
7678 return FAIL;
7679 }
7680
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007681 if (action == 'f')
7682 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007683 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007684 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007685 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007686 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007687
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007688 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007689 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007690 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007691 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007692 _("cannot have both a list and a \"what\" argument"));
7693 return FAIL;
7694 }
7695
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007696 incr_quickfix_busy();
7697
7698 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007699 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007700 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007701 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007702 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007703 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007704 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007705 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007706
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007707 decr_quickfix_busy();
7708
Bram Moolenaard823fa92016-08-12 16:29:27 +02007709 return retval;
7710}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007711
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007712/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007713 * Mark the quickfix context and callback function as in use for all the lists
7714 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007715 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007716 static int
7717mark_quickfix_ctx(qf_info_T *qi, int copyID)
7718{
7719 int i;
7720 int abort = FALSE;
7721 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007722 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007723
7724 for (i = 0; i < LISTCOUNT && !abort; ++i)
7725 {
7726 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007727 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7728 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007729 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7730
7731 cb = &qi->qf_lists[i].qftf_cb;
7732 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007733 }
7734
7735 return abort;
7736}
7737
7738/*
7739 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007740 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007741 */
7742 int
7743set_ref_in_quickfix(int copyID)
7744{
7745 int abort = FALSE;
7746 tabpage_T *tp;
7747 win_T *win;
7748
7749 abort = mark_quickfix_ctx(&ql_info, copyID);
7750 if (abort)
7751 return abort;
7752
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007753 abort = set_ref_in_callback(&qftf_cb, copyID);
7754 if (abort)
7755 return abort;
7756
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007757 FOR_ALL_TAB_WINDOWS(tp, win)
7758 {
7759 if (win->w_llist != NULL)
7760 {
7761 abort = mark_quickfix_ctx(win->w_llist, copyID);
7762 if (abort)
7763 return abort;
7764 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007765 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7766 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007767 // In a location list window and none of the other windows is
7768 // referring to this location list. Mark the location list
7769 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007770 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7771 if (abort)
7772 return abort;
7773 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007774 }
7775
7776 return abort;
7777}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007778#endif
7779
Bram Moolenaar81695252004-12-29 20:58:21 +00007780/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007781 * Return the autocmd name for the :cbuffer Ex commands
7782 */
7783 static char_u *
7784cbuffer_get_auname(cmdidx_T cmdidx)
7785{
7786 switch (cmdidx)
7787 {
7788 case CMD_cbuffer: return (char_u *)"cbuffer";
7789 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7790 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7791 case CMD_lbuffer: return (char_u *)"lbuffer";
7792 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7793 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7794 default: return NULL;
7795 }
7796}
7797
7798/*
7799 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7800 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7801 */
7802 static int
7803cbuffer_process_args(
7804 exarg_T *eap,
7805 buf_T **bufp,
7806 linenr_T *line1,
7807 linenr_T *line2)
7808{
7809 buf_T *buf = NULL;
7810
7811 if (*eap->arg == NUL)
7812 buf = curbuf;
7813 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7814 buf = buflist_findnr(atoi((char *)eap->arg));
7815
7816 if (buf == NULL)
7817 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007818 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007819 return FAIL;
7820 }
7821
7822 if (buf->b_ml.ml_mfp == NULL)
7823 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007824 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007825 return FAIL;
7826 }
7827
7828 if (eap->addr_count == 0)
7829 {
7830 eap->line1 = 1;
7831 eap->line2 = buf->b_ml.ml_line_count;
7832 }
7833
7834 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7835 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7836 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007837 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007838 return FAIL;
7839 }
7840
7841 *line1 = eap->line1;
7842 *line2 = eap->line2;
7843 *bufp = buf;
7844
7845 return OK;
7846}
7847
7848/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007849 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007850 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007851 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007852 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007853 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007854 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007855 */
7856 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007857ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007858{
7859 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007860 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007861 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007862 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007863 int_u save_qfid;
7864 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007865 char_u *qf_title;
7866 linenr_T line1;
7867 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007868
Bram Moolenaara16123a2019-03-28 20:31:07 +01007869 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007870 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007871 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007872 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007873#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007874 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007875 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007876#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007877 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007878
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007879 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007880 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7881 if (qi == NULL)
7882 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007883
Bram Moolenaara16123a2019-03-28 20:31:07 +01007884 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7885 return;
7886
7887 qf_title = qf_cmdtitle(*eap->cmdlinep);
7888
7889 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007890 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007891 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7892 (char *)qf_title, (char *)buf->b_sfname);
7893 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007894 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007895
7896 incr_quickfix_busy();
7897
7898 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7899 (eap->cmdidx != CMD_caddbuffer
7900 && eap->cmdidx != CMD_laddbuffer),
7901 line1, line2,
7902 qf_title, NULL);
7903 if (qf_stack_empty(qi))
7904 {
7905 decr_quickfix_busy();
7906 return;
7907 }
7908 if (res >= 0)
7909 qf_list_changed(qf_get_curlist(qi));
7910
7911 // Remember the current quickfix list identifier, so that we can
7912 // check for autocommands changing the current quickfix list.
7913 save_qfid = qf_get_curlist(qi)->qf_id;
7914 if (au_name != NULL)
7915 {
7916 buf_T *curbuf_old = curbuf;
7917
7918 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7919 TRUE, curbuf);
7920 if (curbuf != curbuf_old)
7921 // Autocommands changed buffer, don't jump now, "qi" may
7922 // be invalid.
7923 res = 0;
7924 }
7925 // Jump to the first error for a new list and if autocmds didn't
7926 // free the list.
7927 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7928 eap->cmdidx == CMD_lbuffer)
7929 && qflist_valid(wp, save_qfid))
7930 // display the first error
7931 qf_jump_first(qi, save_qfid, eap->forceit);
7932
7933 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007934}
7935
Bram Moolenaar1e015462005-09-25 22:16:38 +00007936#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007937/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007938 * Return the autocmd name for the :cexpr Ex commands.
7939 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007940 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007941cexpr_get_auname(cmdidx_T cmdidx)
7942{
7943 switch (cmdidx)
7944 {
7945 case CMD_cexpr: return (char_u *)"cexpr";
7946 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7947 case CMD_caddexpr: return (char_u *)"caddexpr";
7948 case CMD_lexpr: return (char_u *)"lexpr";
7949 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7950 case CMD_laddexpr: return (char_u *)"laddexpr";
7951 default: return NULL;
7952 }
7953}
7954
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007955 int
7956trigger_cexpr_autocmd(int cmdidx)
7957{
7958 char_u *au_name = cexpr_get_auname(cmdidx);
7959
7960 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7961 curbuf->b_fname, TRUE, curbuf))
7962 {
7963 if (aborting())
7964 return FAIL;
7965 }
7966 return OK;
7967}
7968
7969 int
7970cexpr_core(exarg_T *eap, typval_T *tv)
7971{
7972 qf_info_T *qi;
7973 win_T *wp = NULL;
7974
7975 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7976 if (qi == NULL)
7977 return FAIL;
7978
7979 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7980 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7981 {
7982 int res;
7983 int_u save_qfid;
7984 char_u *au_name = cexpr_get_auname(eap->cmdidx);
7985
7986 incr_quickfix_busy();
7987 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
7988 (eap->cmdidx != CMD_caddexpr
7989 && eap->cmdidx != CMD_laddexpr),
7990 (linenr_T)0, (linenr_T)0,
7991 qf_cmdtitle(*eap->cmdlinep), NULL);
7992 if (qf_stack_empty(qi))
7993 {
7994 decr_quickfix_busy();
7995 return FAIL;
7996 }
7997 if (res >= 0)
7998 qf_list_changed(qf_get_curlist(qi));
7999
8000 // Remember the current quickfix list identifier, so that we can
8001 // check for autocommands changing the current quickfix list.
8002 save_qfid = qf_get_curlist(qi)->qf_id;
8003 if (au_name != NULL)
8004 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8005 curbuf->b_fname, TRUE, curbuf);
8006
8007 // Jump to the first error for a new list and if autocmds didn't
8008 // free the list.
8009 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8010 && qflist_valid(wp, save_qfid))
8011 // display the first error
8012 qf_jump_first(qi, save_qfid, eap->forceit);
8013 decr_quickfix_busy();
8014 return OK;
8015 }
8016
Bram Moolenaar677658a2022-01-05 16:09:06 +00008017 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008018 return FAIL;
8019}
8020
Bram Moolenaara16123a2019-03-28 20:31:07 +01008021/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008022 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8023 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008024 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008025 */
8026 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008027ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008028{
8029 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008030
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008031 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008032 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008033
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008034 // Evaluate the expression. When the result is a string or a list we can
8035 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008036 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008037 if (tv != NULL)
8038 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008039 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008040 free_tv(tv);
8041 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008042}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008043#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008044
8045/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008046 * Get the location list for ":lhelpgrep"
8047 */
8048 static qf_info_T *
8049hgr_get_ll(int *new_ll)
8050{
8051 win_T *wp;
8052 qf_info_T *qi;
8053
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008054 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008055 if (bt_help(curwin->w_buffer))
8056 wp = curwin;
8057 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008058 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008059 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008060
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008061 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008062 qi = NULL;
8063 else
8064 qi = wp->w_llist;
8065
8066 if (qi == NULL)
8067 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008068 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008069 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008070 return NULL;
8071 *new_ll = TRUE;
8072 }
8073
8074 return qi;
8075}
8076
8077/*
8078 * Search for a pattern in a help file.
8079 */
8080 static void
8081hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008082 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008083 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008084 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008085 regmatch_T *p_regmatch)
8086{
8087 FILE *fd;
8088 long lnum;
8089
8090 fd = mch_fopen((char *)fname, "r");
8091 if (fd == NULL)
8092 return;
8093
8094 lnum = 1;
8095 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8096 {
8097 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008098
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008099 // Convert a line if 'encoding' is not utf-8 and
8100 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008101 if (p_vc->vc_type != CONV_NONE
8102 && has_non_ascii(IObuff))
8103 {
8104 line = string_convert(p_vc, IObuff, NULL);
8105 if (line == NULL)
8106 line = IObuff;
8107 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008108
8109 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8110 {
8111 int l = (int)STRLEN(line);
8112
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008113 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008114 while (l > 0 && line[l - 1] <= ' ')
8115 line[--l] = NUL;
8116
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008117 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008118 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008119 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008120 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008121 0,
8122 line,
8123 lnum,
thinca6864efa2021-06-19 20:45:20 +02008124 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008125 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008126 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008127 (int)(p_regmatch->endp[0] - line)
8128 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008129 FALSE, // vis_col
8130 NULL, // search pattern
8131 0, // nr
8132 1, // type
8133 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008134 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008135 {
8136 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008137 if (line != IObuff)
8138 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008139 break;
8140 }
8141 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008142 if (line != IObuff)
8143 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008144 ++lnum;
8145 line_breakcheck();
8146 }
8147 fclose(fd);
8148}
8149
8150/*
8151 * Search for a pattern in all the help files in the doc directory under
8152 * the given directory.
8153 */
8154 static void
8155hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008156 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008157 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008158 regmatch_T *p_regmatch,
8159 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008160#ifdef FEAT_MULTI_LANG
8161 , char_u *lang
8162#endif
8163 )
8164{
8165 int fcount;
8166 char_u **fnames;
8167 int fi;
8168
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008169 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008170 add_pathsep(dirname);
8171 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8172 if (gen_expand_wildcards(1, &dirname, &fcount,
8173 &fnames, EW_FILE|EW_SILENT) == OK
8174 && fcount > 0)
8175 {
8176 for (fi = 0; fi < fcount && !got_int; ++fi)
8177 {
8178#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008179 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008180 if (lang != NULL
8181 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008182 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008183 && !(STRNICMP(lang, "en", 2) == 0
8184 && STRNICMP("txt", fnames[fi]
8185 + STRLEN(fnames[fi]) - 3, 3) == 0))
8186 continue;
8187#endif
8188
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008189 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008190 }
8191 FreeWild(fcount, fnames);
8192 }
8193}
8194
8195/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008196 * Search for a pattern in all the help files in the 'runtimepath'
8197 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008198 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008199 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008200 */
8201 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008202hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008203{
8204 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008205
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008206 vimconv_T vc;
8207
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008208 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8209 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008210 vc.vc_type = CONV_NONE;
8211 if (!enc_utf8)
8212 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008213
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008214 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008215 p = p_rtp;
8216 while (*p != NUL && !got_int)
8217 {
8218 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8219
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008220 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008221#ifdef FEAT_MULTI_LANG
8222 , lang
8223#endif
8224 );
8225 }
8226
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008227 if (vc.vc_type != CONV_NONE)
8228 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008229}
8230
8231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 * ":helpgrep {pattern}"
8233 */
8234 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008235ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 regmatch_T regmatch;
8238 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008239 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008240 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008241 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008242 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008243 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008244 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245
Bram Moolenaar73633f82012-01-20 13:39:07 +01008246 switch (eap->cmdidx)
8247 {
8248 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8249 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8250 default: break;
8251 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008252 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8253 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008254 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008255#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008256 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008257 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008258#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008259 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008260
Bram Moolenaar39665952018-08-15 20:59:48 +02008261 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008262 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008263 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008264 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008265 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008266 }
8267
Bram Moolenaara16123a2019-03-28 20:31:07 +01008268 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8269 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008270 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008271 p_cpo = empty_option;
8272
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008273 incr_quickfix_busy();
8274
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008275#ifdef FEAT_MULTI_LANG
8276 // Check for a specified language
8277 lang = check_help_lang(eap->arg);
8278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8280 regmatch.rm_ic = FALSE;
8281 if (regmatch.regprog != NULL)
8282 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008283 qf_list_T *qfl;
8284
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008285 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008286 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008287 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008289 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008290
Bram Moolenaar473de612013-06-08 18:19:48 +02008291 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008293 qfl->qf_nonevalid = FALSE;
8294 qfl->qf_ptr = qfl->qf_start;
8295 qfl->qf_index = 1;
8296 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008297 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 }
8299
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008300 if (p_cpo == empty_option)
8301 p_cpo = save_cpo;
8302 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008303 {
8304 // Darn, some plugin changed the value. If it's still empty it was
8305 // changed and restored, need to restore in the complicated way.
8306 if (*p_cpo == NUL)
8307 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008308 if (save_cpo_allocated)
8309 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008310 }
8311
8312 if (updated)
8313 // This may open a window and source scripts, do this after 'cpo' was
8314 // restored.
8315 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar73633f82012-01-20 13:39:07 +01008317 if (au_name != NULL)
8318 {
8319 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8320 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008321 // When adding a location list to an existing location list stack,
8322 // if the autocmd made the stack invalid, then just return.
8323 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008324 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008325 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008326 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008327 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008328 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008329
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008330 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008331 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008332 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008333 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008334 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008335
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008336 decr_quickfix_busy();
8337
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008338 if (eap->cmdidx == CMD_lhelpgrep)
8339 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008340 // If the help window is not opened or if it already points to the
8341 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008342 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008343 {
8344 if (new_qi)
8345 ll_free_all(&qi);
8346 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008347 else if (curwin->w_llist == NULL && new_qi)
8348 // current window didn't have a location list associated with it
8349 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008350 curwin->w_llist = qi;
8351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008353#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008354
8355#if defined(FEAT_EVAL) || defined(PROTO)
8356# ifdef FEAT_QUICKFIX
8357 static void
8358get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8359{
8360 if (what_arg->v_type == VAR_UNKNOWN)
8361 {
8362 if (rettv_list_alloc(rettv) == OK)
8363 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008364 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008365 }
8366 else
8367 {
8368 if (rettv_dict_alloc(rettv) == OK)
8369 if (is_qf || (wp != NULL))
8370 {
8371 if (what_arg->v_type == VAR_DICT)
8372 {
8373 dict_T *d = what_arg->vval.v_dict;
8374
8375 if (d != NULL)
8376 qf_get_properties(wp, d, rettv->vval.v_dict);
8377 }
8378 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008379 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008380 }
8381 }
8382}
8383# endif
8384
8385/*
8386 * "getloclist()" function
8387 */
8388 void
8389f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8390{
8391# ifdef FEAT_QUICKFIX
8392 win_T *wp;
8393
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008394 if (in_vim9script()
8395 && (check_for_number_arg(argvars, 0) == FAIL
8396 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8397 return;
8398
Bram Moolenaare677df82019-09-02 22:31:11 +02008399 wp = find_win_by_nr_or_id(&argvars[0]);
8400 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8401# endif
8402}
8403
8404/*
8405 * "getqflist()" function
8406 */
8407 void
8408f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8409{
8410# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008411 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8412 return;
8413
Bram Moolenaare677df82019-09-02 22:31:11 +02008414 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8415# endif
8416}
8417
8418/*
8419 * Used by "setqflist()" and "setloclist()" functions
8420 */
8421 static void
8422set_qf_ll_list(
8423 win_T *wp UNUSED,
8424 typval_T *list_arg UNUSED,
8425 typval_T *action_arg UNUSED,
8426 typval_T *what_arg UNUSED,
8427 typval_T *rettv)
8428{
8429# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008430 char_u *act;
8431 int action = 0;
8432 static int recursive = 0;
8433# endif
8434
8435 rettv->vval.v_number = -1;
8436
8437# ifdef FEAT_QUICKFIX
8438 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008439 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008440 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008441 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008442 else
8443 {
8444 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008445 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008446 int valid_dict = TRUE;
8447
8448 if (action_arg->v_type == VAR_STRING)
8449 {
8450 act = tv_get_string_chk(action_arg);
8451 if (act == NULL)
8452 return; // type error; errmsg already given
8453 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8454 act[1] == NUL)
8455 action = *act;
8456 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008457 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008458 }
8459 else if (action_arg->v_type == VAR_UNKNOWN)
8460 action = ' ';
8461 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008462 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008463
8464 if (action_arg->v_type != VAR_UNKNOWN
8465 && what_arg->v_type != VAR_UNKNOWN)
8466 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008467 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8468 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008469 else
8470 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008471 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008472 valid_dict = FALSE;
8473 }
8474 }
8475
8476 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008477 if (l != NULL && action && valid_dict
8478 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008479 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008480 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008481 rettv->vval.v_number = 0;
8482 --recursive;
8483 }
8484# endif
8485}
8486
8487/*
8488 * "setloclist()" function
8489 */
8490 void
8491f_setloclist(typval_T *argvars, typval_T *rettv)
8492{
8493 win_T *win;
8494
8495 rettv->vval.v_number = -1;
8496
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008497 if (in_vim9script()
8498 && (check_for_number_arg(argvars, 0) == FAIL
8499 || check_for_list_arg(argvars, 1) == FAIL
8500 || check_for_opt_string_arg(argvars, 2) == FAIL
8501 || (argvars[2].v_type != VAR_UNKNOWN
8502 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8503 return;
8504
Bram Moolenaare677df82019-09-02 22:31:11 +02008505 win = find_win_by_nr_or_id(&argvars[0]);
8506 if (win != NULL)
8507 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8508}
8509
8510/*
8511 * "setqflist()" function
8512 */
8513 void
8514f_setqflist(typval_T *argvars, typval_T *rettv)
8515{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008516 if (in_vim9script()
8517 && (check_for_list_arg(argvars, 0) == FAIL
8518 || check_for_opt_string_arg(argvars, 1) == FAIL
8519 || (argvars[1].v_type != VAR_UNKNOWN
8520 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8521 return;
8522
Bram Moolenaare677df82019-09-02 22:31:11 +02008523 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8524}
8525#endif