blob: ab288e0c4d6032ec1e24a4cf94c39c3397f1cad8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
thinca6864efa2021-06-19 20:45:20 +020033 linenr_T qf_end_lnum; // line number when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020034 int qf_fnum; // file number for the line
35 int qf_col; // column where the error occurred
thinca6864efa2021-06-19 20:45:20 +020036 int qf_end_col; // column when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020037 int qf_nr; // error number
38 char_u *qf_module; // module name for this error
39 char_u *qf_pattern; // search pattern for the error
40 char_u *qf_text; // description of the error
thinca6864efa2021-06-19 20:45:20 +020041 char_u qf_viscol; // set to TRUE if qf_col and qf_end_col is
42 // screen column
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020043 char_u qf_cleared; // set to TRUE if line has been deleted
44 char_u qf_type; // type of the error (mostly 'E'); 1 for
45 // :helpgrep
46 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000047};
48
49/*
50 * There is a stack of error lists.
51 */
52#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020053#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010054#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020056/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010057 * Quickfix list type.
58 */
59typedef enum
60{
61 QFLT_QUICKFIX, // Quickfix list - global list
62 QFLT_LOCATION, // Location list - per window list
63 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
64} qfltype_T;
65
66/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 * Quickfix/Location list definition
68 * Contains a list of entries (qfline_T). qf_start points to the first entry
69 * and qf_last points to the last entry. qf_count contains the list size.
70 *
71 * Usually the list contains one or more entries. But an empty list can be
72 * created using setqflist()/setloclist() with a title and/or user context
73 * information and entries can be added later using setqflist()/setloclist().
74 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000075typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020077 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010078 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020079 qfline_T *qf_start; // pointer to the first error
80 qfline_T *qf_last; // pointer to the last error
81 qfline_T *qf_ptr; // pointer to the current error
82 int qf_count; // number of errors (0 means empty list)
83 int qf_index; // current index in the error list
84 int qf_nonevalid; // TRUE if not a single valid entry found
85 char_u *qf_title; // title derived from the command that created
86 // the error list or set by setqflist
87 typval_T *qf_ctx; // context set by setqflist/setloclist
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +010088 callback_T qf_qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020089
90 struct dir_stack_T *qf_dir_stack;
91 char_u *qf_directory;
92 struct dir_stack_T *qf_file_stack;
93 char_u *qf_currfile;
94 int qf_multiline;
95 int qf_multiignore;
96 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010097 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000098} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar6a8958d2017-06-22 21:33:20 +0200100/*
101 * Quickfix/Location list stack definition
102 * Contains a list of quickfix/location lists (qf_list_T)
103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000104struct qf_info_S
105{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 // Count of references to this list. Used only for location lists.
107 // When a location list window reference this list, qf_refcount
108 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
109 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200111 int qf_listcount; // current number of lists
112 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100114 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100115 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000116};
117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200118static qf_info_T ql_info; // global quickfix list
119static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
haya14busae023d492022-02-08 18:09:29 +0000121#define FMT_PATTERNS 13 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
124 * Structure used to hold the info of one part of 'errorformat'
125 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000126typedef struct efm_S efm_T;
127struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200129 regprog_T *prog; // pre-formatted part of 'errorformat'
130 efm_T *next; // pointer to next (NULL if last)
131 char_u addr[FMT_PATTERNS]; // indices of used % patterns
132 char_u prefix; // prefix of this format line:
133 // 'D' enter directory
134 // 'X' leave directory
135 // 'A' start of multi-line message
136 // 'E' error message
137 // 'W' warning message
138 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200139 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200140 // 'C' continuation line
141 // 'Z' end of multi-line message
142 // 'G' general, unspecific message
143 // 'P' push file (partial) message
144 // 'Q' pop/quit file (partial) message
145 // 'O' overread (partial) message
146 char_u flags; // additional flags given in prefix
147 // '-' do not include this line
148 // '+' include whole line in message
149 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150};
151
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200152// List of location lists to be deleted.
153// Used to delay the deletion of locations lists by autocmds.
154typedef struct qf_delq_S
155{
156 struct qf_delq_S *next;
157 qf_info_T *qi;
158} qf_delq_T;
159static qf_delq_T *qf_delq_head = NULL;
160
161// Counter to prevent autocmds from freeing up location lists when they are
162// still being used.
163static int quickfix_busy = 0;
164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200165static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100166
Bram Moolenaard43906d2020-07-20 21:31:32 +0200167// callback function for 'quickfixtextfunc'
168static callback_T qftf_cb;
169
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_new_list(qf_info_T *qi, char_u *qf_title);
thinca6864efa2021-06-19 20:45:20 +0200171static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, long end_lnum, int col, int end_col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200172static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100174static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200175static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200177static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200178static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
thinca6864efa2021-06-19 20:45:20 +0200180static void qf_range_text(qfline_T *qfp, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100181static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static win_T *qf_find_win(qf_info_T *qi);
183static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200184static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200185static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100186static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
187static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
188static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
189static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200191// Quickfix window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000192#define IS_QF_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200193// Location list window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000194#define IS_LL_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200195
196// Quickfix and location list stack check helper macros
kylo252ae6f1d82022-02-16 19:24:07 +0000197#define IS_QF_STACK(qi) ((qi)->qfl_type == QFLT_QUICKFIX)
198#define IS_LL_STACK(qi) ((qi)->qfl_type == QFLT_LOCATION)
199#define IS_QF_LIST(qfl) ((qfl)->qfl_type == QFLT_QUICKFIX)
200#define IS_LL_LIST(qfl) ((qfl)->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200201
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000202/*
203 * Return location list for window 'wp'
204 * For location list window, return the referenced location list
205 */
kylo252ae6f1d82022-02-16 19:24:07 +0000206#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? (wp)->w_llist_ref : (wp)->w_llist)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000207
Bram Moolenaar95946f12019-03-31 15:31:59 +0200208// Macro to loop through all the items in a quickfix list
209// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100210#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
kylo252ae6f1d82022-02-16 19:24:07 +0000211 for ((i) = 1, (qfp) = (qfl)->qf_start; \
212 !got_int && (i) <= (qfl)->qf_count && (qfp) != NULL; \
213 ++(i), (qfp) = (qfp)->qf_next)
Bram Moolenaara16123a2019-03-28 20:31:07 +0100214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200216 * Looking up a buffer can be slow if there are many. Remember the last one
217 * to make this a lot faster if there are multiple matches in the same file.
218 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200219static char_u *qf_last_bufname = NULL;
220static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200221
222/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200223 * Maximum number of bytes allowed per line while reading a errorfile.
224 */
225#define LINE_MAXLEN 4096
226
haya14busae023d492022-02-08 18:09:29 +0000227/*
228 * Patterns used. Keep in sync with qf_parse_fmt[].
229 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200230static struct fmtpattern
231{
232 char_u convchar;
233 char *pattern;
234} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200235 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200236 {'f', ".\\+"}, // only used when at end
haya14busae023d492022-02-08 18:09:29 +0000237 {'n', "\\d\\+"}, // 1
238 {'l', "\\d\\+"}, // 2
239 {'e', "\\d\\+"}, // 3
240 {'c', "\\d\\+"}, // 4
241 {'k', "\\d\\+"}, // 5
242 {'t', "."}, // 6
243#define FMT_PATTERN_M 7
244 {'m', ".\\+"}, // 7
245#define FMT_PATTERN_R 8
246 {'r', ".*"}, // 8
247 {'p', "[- .]*"}, // 9
248 {'v', "\\d\\+"}, // 10
249 {'s', ".\\+"}, // 11
250 {'o', ".\\+"} // 12
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200251 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200252
253/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 * See fmt_pat definition above for the list of supported patterns. The
256 * pattern specifier is supplied in "efmpat". The converted pattern is stored
257 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 */
259 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260efmpat_to_regpat(
261 char_u *efmpat,
262 char_u *regpat,
263 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200264 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100265 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266{
267 char_u *srcptr;
268
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200271 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000272 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200273 return NULL;
274 }
haya14busae023d492022-02-08 18:09:29 +0000275 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000277 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000280 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 return NULL;
282 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283 efminfo->addr[idx] = (char_u)++round;
284 *regpat++ = '\\';
285 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200288 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200289 // Also match "c:" in the file name, even when
290 // checking for a colon next: "%f:".
291 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 STRCPY(regpat, "\\%(\\a:\\)\\=");
293 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294 }
295#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200300 // A file name may contain spaces, but this isn't
301 // in "\f". For "%f:%l:%m" there may be a ":" in
302 // the file name. Use ".\{-1,}x" instead (x is
303 // the next character), the requirement that :999:
304 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 STRCPY(regpat, ".\\{-1,}");
306 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 }
308 else
309 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200310 // File name followed by '\\' or '%': include as
311 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 STRCPY(regpat, "\\f\\+");
313 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314 }
315 }
316 else
317 {
318 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 while ((*regpat = *srcptr++) != NUL)
320 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200322 *regpat++ = '\\';
323 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200325 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326}
327
328/*
329 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 */
332 static char_u *
333scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 char_u *efm,
336 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100337 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338{
339 char_u *efmp = *pefmp;
340
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200343 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 {
345 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200346 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347 if (efmp < efm + len)
348 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200349 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200350 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200352 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353 if (efmp == efm + len)
354 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000355 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 return NULL;
357 }
358 }
359 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200360 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 *regpat++ = *++efmp;
362 *regpat++ = '\\';
363 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 }
365 else
366 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200367 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000368 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369 return NULL;
370 }
371
372 *pefmp = efmp;
373
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375}
376
377/*
378 * Analyze/parse an errorformat prefix.
379 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100381efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200383 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200385 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200386 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200387 else
388 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000389 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 }
392
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394}
395
396/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200397 * Converts a 'errorformat' string part in 'efm' to a regular expression
398 * pattern. The resulting regex pattern is returned in "regpat". Additional
399 * information about the 'erroformat' pattern is returned in "fmt_ptr".
400 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200401 */
402 static int
403efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200404 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200405 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200406 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100407 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200408{
409 char_u *ptr;
410 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200411 int round;
412 int idx = 0;
413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200414 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200415 ptr = regpat;
416 *ptr++ = '^';
417 round = 0;
418 for (efmp = efm; efmp < efm + len; ++efmp)
419 {
420 if (*efmp == '%')
421 {
422 ++efmp;
423 for (idx = 0; idx < FMT_PATTERNS; ++idx)
424 if (fmt_pat[idx].convchar == *efmp)
425 break;
426 if (idx < FMT_PATTERNS)
427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100428 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200429 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200430 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200431 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200432 }
433 else if (*efmp == '*')
434 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200435 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100436 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200437 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200438 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200439 }
440 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200441 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 else if (*efmp == '#')
443 *ptr++ = '*';
444 else if (*efmp == '>')
445 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200446 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200448 // prefix is allowed only at the beginning of the errorformat
449 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100450 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200451 if (efmp == NULL)
452 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 }
454 else
455 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000456 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200457 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458 }
459 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200460 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200461 {
462 if (*efmp == '\\' && efmp + 1 < efm + len)
463 ++efmp;
464 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200465 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200466 if (*efmp)
467 *ptr++ = *efmp;
468 }
469 }
470 *ptr++ = '$';
471 *ptr = NUL;
472
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200473 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200474}
475
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200476/*
477 * Free the 'errorformat' information list
478 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200479 static void
480free_efm_list(efm_T **efm_first)
481{
482 efm_T *efm_ptr;
483
484 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
485 {
486 *efm_first = efm_ptr->next;
487 vim_regfree(efm_ptr->prog);
488 vim_free(efm_ptr);
489 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100490 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200491}
492
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200493/*
494 * Compute the size of the buffer used to convert a 'errorformat' pattern into
495 * a regular expression pattern.
496 */
497 static int
498efm_regpat_bufsz(char_u *efm)
499{
500 int sz;
501 int i;
502
503 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
504 for (i = FMT_PATTERNS; i > 0; )
505 sz += (int)STRLEN(fmt_pat[--i].pattern);
506#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200507 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200508#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200509 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200510#endif
511
512 return sz;
513}
514
515/*
516 * Return the length of a 'errorformat' option part (separated by ",").
517 */
518 static int
519efm_option_part_len(char_u *efm)
520{
521 int len;
522
523 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
524 if (efm[len] == '\\' && efm[len + 1] != NUL)
525 ++len;
526
527 return len;
528}
529
530/*
531 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
532 * are parsed and converted to regular expressions. Returns information about
533 * the parsed 'errorformat' option.
534 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200535 static efm_T *
536parse_efm_option(char_u *efm)
537{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200538 efm_T *fmt_ptr = NULL;
539 efm_T *fmt_first = NULL;
540 efm_T *fmt_last = NULL;
541 char_u *fmtstr = NULL;
542 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200543 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200545 // Each part of the format string is copied and modified from errorformat
546 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200547
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200548 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200549 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000550 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 goto parse_efm_error;
552
553 while (efm[0] != NUL)
554 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200555 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000556 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200557 if (fmt_ptr == NULL)
558 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200559 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200560 fmt_first = fmt_ptr;
561 else
562 fmt_last->next = fmt_ptr;
563 fmt_last = fmt_ptr;
564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200565 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200566 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100568 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200569 goto parse_efm_error;
570 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
571 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200572 // Advance to next part
573 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200574 }
575
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200576 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000577 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200578
579 goto parse_efm_end;
580
581parse_efm_error:
582 free_efm_list(&fmt_first);
583
584parse_efm_end:
585 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200586
587 return fmt_first;
588}
589
Bram Moolenaare0d37972016-07-15 22:36:01 +0200590enum {
591 QF_FAIL = 0,
592 QF_OK = 1,
593 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200594 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200595 QF_IGNORE_LINE = 4,
596 QF_MULTISCAN = 5,
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +0100597 QF_ABORT = 6
Bram Moolenaare0d37972016-07-15 22:36:01 +0200598};
599
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200600/*
601 * State information used to parse lines and add entries to a quickfix/location
602 * list.
603 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200604typedef struct {
605 char_u *linebuf;
606 int linelen;
607 char_u *growbuf;
608 int growbufsiz;
609 FILE *fd;
610 typval_T *tv;
611 char_u *p_str;
612 listitem_T *p_li;
613 buf_T *buf;
614 linenr_T buflnum;
615 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100616 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200617} qfstate_T;
618
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200619/*
620 * Allocate more memory for the line buffer used for parsing lines.
621 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200622 static char_u *
623qf_grow_linebuf(qfstate_T *state, int newsz)
624{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200625 char_u *p;
626
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200627 // If the line exceeds LINE_MAXLEN exclude the last
628 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200629 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
630 if (state->growbuf == NULL)
631 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000632 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200633 if (state->growbuf == NULL)
634 return NULL;
635 state->growbufsiz = state->linelen;
636 }
637 else if (state->linelen > state->growbufsiz)
638 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200639 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200640 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200641 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200642 state->growbufsiz = state->linelen;
643 }
644 return state->growbuf;
645}
646
647/*
648 * Get the next string (separated by newline) from state->p_str.
649 */
650 static int
651qf_get_next_str_line(qfstate_T *state)
652{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200653 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200654 char_u *p_str = state->p_str;
655 char_u *p;
656 int len;
657
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200658 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200659 return QF_END_OF_INPUT;
660
661 p = vim_strchr(p_str, '\n');
662 if (p != NULL)
663 len = (int)(p - p_str) + 1;
664 else
665 len = (int)STRLEN(p_str);
666
667 if (len > IOSIZE - 2)
668 {
669 state->linebuf = qf_grow_linebuf(state, len);
670 if (state->linebuf == NULL)
671 return QF_NOMEM;
672 }
673 else
674 {
675 state->linebuf = IObuff;
676 state->linelen = len;
677 }
678 vim_strncpy(state->linebuf, p_str, state->linelen);
679
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200680 // Increment using len in order to discard the rest of the
681 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200682 p_str += len;
683 state->p_str = p_str;
684
685 return QF_OK;
686}
687
688/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000689 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200690 */
691 static int
692qf_get_next_list_line(qfstate_T *state)
693{
694 listitem_T *p_li = state->p_li;
695 int len;
696
697 while (p_li != NULL
698 && (p_li->li_tv.v_type != VAR_STRING
699 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200700 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200701
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200702 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200703 {
704 state->p_li = NULL;
705 return QF_END_OF_INPUT;
706 }
707
708 len = (int)STRLEN(p_li->li_tv.vval.v_string);
709 if (len > IOSIZE - 2)
710 {
711 state->linebuf = qf_grow_linebuf(state, len);
712 if (state->linebuf == NULL)
713 return QF_NOMEM;
714 }
715 else
716 {
717 state->linebuf = IObuff;
718 state->linelen = len;
719 }
720
721 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
722
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200723 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200724 return QF_OK;
725}
726
727/*
728 * Get the next string from state->buf.
729 */
730 static int
731qf_get_next_buf_line(qfstate_T *state)
732{
733 char_u *p_buf = NULL;
734 int len;
735
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200736 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200737 if (state->buflnum > state->lnumlast)
738 return QF_END_OF_INPUT;
739
740 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
741 state->buflnum += 1;
742
743 len = (int)STRLEN(p_buf);
744 if (len > IOSIZE - 2)
745 {
746 state->linebuf = qf_grow_linebuf(state, len);
747 if (state->linebuf == NULL)
748 return QF_NOMEM;
749 }
750 else
751 {
752 state->linebuf = IObuff;
753 state->linelen = len;
754 }
755 vim_strncpy(state->linebuf, p_buf, state->linelen);
756
757 return QF_OK;
758}
759
760/*
761 * Get the next string from file state->fd.
762 */
763 static int
764qf_get_next_file_line(qfstate_T *state)
765{
766 int discard;
767 int growbuflen;
768
769 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
770 return QF_END_OF_INPUT;
771
772 discard = FALSE;
773 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200774 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200775 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200776 // The current line exceeds IObuff, continue reading using
777 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200778 if (state->growbuf == NULL)
779 {
780 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000781 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200782 if (state->growbuf == NULL)
783 return QF_NOMEM;
784 }
785
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200786 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200787 memcpy(state->growbuf, IObuff, IOSIZE - 1);
788 growbuflen = state->linelen;
789
790 for (;;)
791 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200792 char_u *p;
793
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 if (fgets((char *)state->growbuf + growbuflen,
795 state->growbufsiz - growbuflen, state->fd) == NULL)
796 break;
797 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
798 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200799 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800 break;
801 if (state->growbufsiz == LINE_MAXLEN)
802 {
803 discard = TRUE;
804 break;
805 }
806
807 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
808 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200809 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200810 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200811 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200812 }
813
814 while (discard)
815 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200816 // The current line is longer than LINE_MAXLEN, continue
817 // reading but discard everything until EOL or EOF is
818 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200819 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
820 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200821 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200822 break;
823 }
824
825 state->linebuf = state->growbuf;
826 state->linelen = growbuflen;
827 }
828 else
829 state->linebuf = IObuff;
830
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200831 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200832 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
833 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100834 char_u *line;
835
836 line = string_convert(&state->vc, state->linebuf, &state->linelen);
837 if (line != NULL)
838 {
839 if (state->linelen < IOSIZE)
840 {
841 STRCPY(state->linebuf, line);
842 vim_free(line);
843 }
844 else
845 {
846 vim_free(state->growbuf);
847 state->linebuf = state->growbuf = line;
848 state->growbufsiz = state->linelen < LINE_MAXLEN
849 ? state->linelen : LINE_MAXLEN;
850 }
851 }
852 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100853
Bram Moolenaare0d37972016-07-15 22:36:01 +0200854 return QF_OK;
855}
856
857/*
858 * Get the next string from a file/buffer/list/string.
859 */
860 static int
861qf_get_nextline(qfstate_T *state)
862{
863 int status = QF_FAIL;
864
865 if (state->fd == NULL)
866 {
867 if (state->tv != NULL)
868 {
869 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200870 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200871 status = qf_get_next_str_line(state);
872 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 status = qf_get_next_list_line(state);
875 }
876 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200877 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200878 status = qf_get_next_buf_line(state);
879 }
880 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200881 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882 status = qf_get_next_file_line(state);
883
884 if (status != QF_OK)
885 return status;
886
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200887 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200890 state->linebuf[state->linelen - 1] = NUL;
891#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200892 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
893 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200894#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200895 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200896
Bram Moolenaare0d37972016-07-15 22:36:01 +0200897 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200898
899 return QF_OK;
900}
901
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200902typedef struct {
903 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200904 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200905 char_u *errmsg;
906 int errmsglen;
907 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200908 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200909 int col;
thinca6864efa2021-06-19 20:45:20 +0200910 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200911 char_u use_viscol;
912 char_u *pattern;
913 int enr;
914 int type;
915 int valid;
916} qffields_T;
917
918/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200919 * Parse the match for filename ('%f') pattern in regmatch.
920 * Return the matched value in "fields->namebuf".
921 */
922 static int
923qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
924{
925 int c;
926
927 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
928 return QF_FAIL;
929
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200930 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200931 c = *rmp->endp[midx];
932 *rmp->endp[midx] = NUL;
933 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
934 *rmp->endp[midx] = c;
935
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200936 // For separate filename patterns (%O, %P and %Q), the specified file
937 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200938 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
939 && mch_getperm(fields->namebuf) == -1)
940 return QF_FAIL;
941
942 return QF_OK;
943}
944
945/*
946 * Parse the match for error number ('%n') pattern in regmatch.
947 * Return the matched value in "fields->enr".
948 */
949 static int
950qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
951{
952 if (rmp->startp[midx] == NULL)
953 return QF_FAIL;
954 fields->enr = (int)atol((char *)rmp->startp[midx]);
955 return QF_OK;
956}
957
958/*
haya14busae023d492022-02-08 18:09:29 +0000959 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200960 * Return the matched value in "fields->lnum".
961 */
962 static int
963qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
964{
965 if (rmp->startp[midx] == NULL)
966 return QF_FAIL;
967 fields->lnum = atol((char *)rmp->startp[midx]);
968 return QF_OK;
969}
970
971/*
haya14busae023d492022-02-08 18:09:29 +0000972 * Parse the match for end line number ('%e') pattern in regmatch.
973 * Return the matched value in "fields->end_lnum".
974 */
975 static int
976qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
977{
978 if (rmp->startp[midx] == NULL)
979 return QF_FAIL;
980 fields->end_lnum = atol((char *)rmp->startp[midx]);
981 return QF_OK;
982}
983
984/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200985 * Parse the match for column number ('%c') pattern in regmatch.
986 * Return the matched value in "fields->col".
987 */
988 static int
989qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
990{
991 if (rmp->startp[midx] == NULL)
992 return QF_FAIL;
993 fields->col = (int)atol((char *)rmp->startp[midx]);
994 return QF_OK;
995}
996
997/*
haya14busae023d492022-02-08 18:09:29 +0000998 * Parse the match for end column number ('%k') pattern in regmatch.
999 * Return the matched value in "fields->end_col".
1000 */
1001 static int
1002qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1003{
1004 if (rmp->startp[midx] == NULL)
1005 return QF_FAIL;
1006 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1007 return QF_OK;
1008}
1009
1010/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001011 * Parse the match for error type ('%t') pattern in regmatch.
1012 * Return the matched value in "fields->type".
1013 */
1014 static int
1015qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1016{
1017 if (rmp->startp[midx] == NULL)
1018 return QF_FAIL;
1019 fields->type = *rmp->startp[midx];
1020 return QF_OK;
1021}
1022
1023/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001024 * Copy a non-error line into the error string. Return the matched line in
1025 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001026 */
1027 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001028copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001029{
1030 char_u *p;
1031
1032 if (linelen >= fields->errmsglen)
1033 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001034 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001035 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1036 return QF_NOMEM;
1037 fields->errmsg = p;
1038 fields->errmsglen = linelen + 1;
1039 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001040 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001041 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001042
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001043 return QF_OK;
1044}
1045
1046/*
1047 * Parse the match for error message ('%m') pattern in regmatch.
1048 * Return the matched value in "fields->errmsg".
1049 */
1050 static int
1051qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1052{
1053 char_u *p;
1054 int len;
1055
1056 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1057 return QF_FAIL;
1058 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1059 if (len >= fields->errmsglen)
1060 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001061 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001062 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1063 return QF_NOMEM;
1064 fields->errmsg = p;
1065 fields->errmsglen = len + 1;
1066 }
1067 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1068 return QF_OK;
1069}
1070
1071/*
1072 * Parse the match for rest of a single-line file message ('%r') pattern.
1073 * Return the matched value in "tail".
1074 */
1075 static int
1076qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1077{
1078 if (rmp->startp[midx] == NULL)
1079 return QF_FAIL;
1080 *tail = rmp->startp[midx];
1081 return QF_OK;
1082}
1083
1084/*
1085 * Parse the match for the pointer line ('%p') pattern in regmatch.
1086 * Return the matched value in "fields->col".
1087 */
1088 static int
1089qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1090{
1091 char_u *match_ptr;
1092
1093 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1094 return QF_FAIL;
1095 fields->col = 0;
1096 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1097 ++match_ptr)
1098 {
1099 ++fields->col;
1100 if (*match_ptr == TAB)
1101 {
1102 fields->col += 7;
1103 fields->col -= fields->col % 8;
1104 }
1105 }
1106 ++fields->col;
1107 fields->use_viscol = TRUE;
1108 return QF_OK;
1109}
1110
1111/*
1112 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1113 * Return the matched value in "fields->col".
1114 */
1115 static int
1116qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1117{
1118 if (rmp->startp[midx] == NULL)
1119 return QF_FAIL;
1120 fields->col = (int)atol((char *)rmp->startp[midx]);
1121 fields->use_viscol = TRUE;
1122 return QF_OK;
1123}
1124
1125/*
1126 * Parse the match for the search text ('%s') pattern in regmatch.
1127 * Return the matched value in "fields->pattern".
1128 */
1129 static int
1130qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1131{
1132 int len;
1133
1134 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1135 return QF_FAIL;
1136 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1137 if (len > CMDBUFFSIZE - 5)
1138 len = CMDBUFFSIZE - 5;
1139 STRCPY(fields->pattern, "^\\V");
1140 STRNCAT(fields->pattern, rmp->startp[midx], len);
1141 fields->pattern[len + 3] = '\\';
1142 fields->pattern[len + 4] = '$';
1143 fields->pattern[len + 5] = NUL;
1144 return QF_OK;
1145}
1146
1147/*
1148 * Parse the match for the module ('%o') pattern in regmatch.
1149 * Return the matched value in "fields->module".
1150 */
1151 static int
1152qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1153{
1154 int len;
1155
1156 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1157 return QF_FAIL;
1158 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1159 if (len > CMDBUFFSIZE)
1160 len = CMDBUFFSIZE;
1161 STRNCAT(fields->module, rmp->startp[midx], len);
1162 return QF_OK;
1163}
1164
1165/*
1166 * 'errorformat' format pattern parser functions.
1167 * The '%f' and '%r' formats are parsed differently from other formats.
1168 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001169 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001170 */
1171static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1172{
haya14busae023d492022-02-08 18:09:29 +00001173 NULL, // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001174 qf_parse_fmt_n,
1175 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001176 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001177 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001178 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001179 qf_parse_fmt_t,
1180 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001181 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001182 qf_parse_fmt_p,
1183 qf_parse_fmt_v,
1184 qf_parse_fmt_s,
1185 qf_parse_fmt_o
1186};
1187
1188/*
1189 * Parse the error format pattern matches in "regmatch" and set the values in
1190 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1191 * match. Returns QF_OK if all the matches are successfully parsed. On
1192 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001193 */
1194 static int
1195qf_parse_match(
1196 char_u *linebuf,
1197 int linelen,
1198 efm_T *fmt_ptr,
1199 regmatch_T *regmatch,
1200 qffields_T *fields,
1201 int qf_multiline,
1202 int qf_multiscan,
1203 char_u **tail)
1204{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001205 int idx = fmt_ptr->prefix;
1206 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001207 int midx;
1208 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001209
1210 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1211 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001212 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001213 fields->type = idx;
1214 else
1215 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001216
1217 // Extract error message data from matched line.
1218 // We check for an actual submatch, because "\[" and "\]" in
1219 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001220 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001221 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001222 status = QF_OK;
1223 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001224 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001225 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001226 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001227 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001228 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001229 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001230 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001231 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001232 }
haya14busae023d492022-02-08 18:09:29 +00001233 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001234 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001235 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001236 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001237
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001238 if (status != QF_OK)
1239 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001240 }
1241
1242 return QF_OK;
1243}
1244
1245/*
1246 * Parse an error line in 'linebuf' using a single error format string in
1247 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1248 * Returns QF_OK if the efm format matches completely and the fields are
1249 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1250 */
1251 static int
1252qf_parse_get_fields(
1253 char_u *linebuf,
1254 int linelen,
1255 efm_T *fmt_ptr,
1256 qffields_T *fields,
1257 int qf_multiline,
1258 int qf_multiscan,
1259 char_u **tail)
1260{
1261 regmatch_T regmatch;
1262 int status = QF_FAIL;
1263 int r;
1264
1265 if (qf_multiscan &&
1266 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1267 return QF_FAIL;
1268
1269 fields->namebuf[0] = NUL;
1270 fields->module[0] = NUL;
1271 fields->pattern[0] = NUL;
1272 if (!qf_multiscan)
1273 fields->errmsg[0] = NUL;
1274 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001275 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001276 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001277 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001278 fields->use_viscol = FALSE;
1279 fields->enr = -1;
1280 fields->type = 0;
1281 *tail = NULL;
1282
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001283 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001284 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001285 regmatch.regprog = fmt_ptr->prog;
1286 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1287 fmt_ptr->prog = regmatch.regprog;
1288 if (r)
1289 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1290 fields, qf_multiline, qf_multiscan, tail);
1291
1292 return status;
1293}
1294
1295/*
1296 * Parse directory error format prefixes (%D and %X).
1297 * Push and pop directories from the directory stack when scanning directory
1298 * names.
1299 */
1300 static int
1301qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1302{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001303 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001304 {
1305 if (*fields->namebuf == NUL)
1306 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001307 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001308 return QF_FAIL;
1309 }
1310 qfl->qf_directory =
1311 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1312 if (qfl->qf_directory == NULL)
1313 return QF_FAIL;
1314 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001315 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001316 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1317
1318 return QF_OK;
1319}
1320
1321/*
1322 * Parse global file name error format prefixes (%O, %P and %Q).
1323 */
1324 static int
1325qf_parse_file_pfx(
1326 int idx,
1327 qffields_T *fields,
1328 qf_list_T *qfl,
1329 char_u *tail)
1330{
1331 fields->valid = FALSE;
1332 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1333 {
1334 if (*fields->namebuf && idx == 'P')
1335 qfl->qf_currfile =
1336 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1337 else if (idx == 'Q')
1338 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1339 *fields->namebuf = NUL;
1340 if (tail && *tail)
1341 {
1342 STRMOVE(IObuff, skipwhite(tail));
1343 qfl->qf_multiscan = TRUE;
1344 return QF_MULTISCAN;
1345 }
1346 }
1347
1348 return QF_OK;
1349}
1350
1351/*
1352 * Parse a non-error line (a line which doesn't match any of the error
1353 * format in 'efm').
1354 */
1355 static int
1356qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1357{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001358 fields->namebuf[0] = NUL; // no match found, remove file name
1359 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001360 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001361
Bram Moolenaarf4140482020-02-15 23:06:45 +01001362 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001363}
1364
1365/*
1366 * Parse multi-line error format prefixes (%C and %Z)
1367 */
1368 static int
1369qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001370 int idx,
1371 qf_list_T *qfl,
1372 qffields_T *fields)
1373{
1374 char_u *ptr;
1375 int len;
1376
1377 if (!qfl->qf_multiignore)
1378 {
1379 qfline_T *qfprev = qfl->qf_last;
1380
1381 if (qfprev == NULL)
1382 return QF_FAIL;
1383 if (*fields->errmsg && !qfl->qf_multiignore)
1384 {
1385 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001386 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1387 aid_qf_multiline_pfx);
1388 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001389 return QF_FAIL;
1390 STRCPY(ptr, qfprev->qf_text);
1391 vim_free(qfprev->qf_text);
1392 qfprev->qf_text = ptr;
1393 *(ptr += len) = '\n';
1394 STRCPY(++ptr, fields->errmsg);
1395 }
1396 if (qfprev->qf_nr == -1)
1397 qfprev->qf_nr = fields->enr;
1398 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001399 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001400 qfprev->qf_type = fields->type;
1401
1402 if (!qfprev->qf_lnum)
1403 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001404 if (!qfprev->qf_end_lnum)
1405 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001406 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001407 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001408 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001409 qfprev->qf_viscol = fields->use_viscol;
1410 }
haya14busae023d492022-02-08 18:09:29 +00001411 if (!qfprev->qf_end_col)
1412 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001413 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001414 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001415 qfl->qf_directory,
1416 *fields->namebuf || qfl->qf_directory != NULL
1417 ? fields->namebuf
1418 : qfl->qf_currfile != NULL && fields->valid
1419 ? qfl->qf_currfile : 0);
1420 }
1421 if (idx == 'Z')
1422 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1423 line_breakcheck();
1424
1425 return QF_IGNORE_LINE;
1426}
1427
1428/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001429 * Parse a line and get the quickfix fields.
1430 * Return the QF_ status.
1431 */
1432 static int
1433qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001434 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 char_u *linebuf,
1436 int linelen,
1437 efm_T *fmt_first,
1438 qffields_T *fields)
1439{
1440 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 int idx = 0;
1442 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001443 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001444
Bram Moolenaare333e792018-04-08 13:27:39 +02001445restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001446 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 if (fmt_start == NULL)
1448 fmt_ptr = fmt_first;
1449 else
1450 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001451 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001452 fmt_ptr = fmt_start;
1453 fmt_start = NULL;
1454 }
1455
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001456 // Try to match each part of 'errorformat' until we find a complete
1457 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1460 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001461 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001462 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1463 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1464 if (status == QF_NOMEM)
1465 return status;
1466 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001469 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001470
1471 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1472 {
1473 if (fmt_ptr != NULL)
1474 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001475 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001476 status = qf_parse_dir_pfx(idx, fields, qfl);
1477 if (status != QF_OK)
1478 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001479 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001480
1481 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1482 if (status != QF_OK)
1483 return status;
1484
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001485 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001486 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001487 }
1488 else if (fmt_ptr != NULL)
1489 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001490 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001491 if (fmt_ptr->conthere)
1492 fmt_start = fmt_ptr;
1493
Bram Moolenaare9283662020-06-07 14:10:47 +02001494 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001495 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001496 qfl->qf_multiline = TRUE; // start of a multi-line message
1497 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001498 }
1499 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001500 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001501 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001502 if (status != QF_OK)
1503 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001504 }
1505 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001506 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001507 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1508 if (status == QF_MULTISCAN)
1509 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001510 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001511 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001512 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001513 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001514 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001515 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001516 return QF_IGNORE_LINE;
1517 }
1518 }
1519
1520 return QF_OK;
1521}
1522
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001523/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001524 * Returns TRUE if the specified quickfix/location stack is empty
1525 */
1526 static int
1527qf_stack_empty(qf_info_T *qi)
1528{
1529 return qi == NULL || qi->qf_listcount <= 0;
1530}
1531
1532/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001533 * Returns TRUE if the specified quickfix/location list is empty.
1534 */
1535 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001536qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001537{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001538 return qfl == NULL || qfl->qf_count <= 0;
1539}
1540
1541/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001542 * Returns TRUE if the specified quickfix/location list is not empty and
1543 * has valid entries.
1544 */
1545 static int
1546qf_list_has_valid_entries(qf_list_T *qfl)
1547{
1548 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1549}
1550
1551/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001552 * Return a pointer to a list in the specified quickfix stack
1553 */
1554 static qf_list_T *
1555qf_get_list(qf_info_T *qi, int idx)
1556{
1557 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001558}
1559
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001560/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001561 * Allocate the fields used for parsing lines and populating a quickfix list.
1562 */
1563 static int
1564qf_alloc_fields(qffields_T *pfields)
1565{
1566 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1567 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1568 pfields->errmsglen = CMDBUFFSIZE + 1;
1569 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1570 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1571 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1572 || pfields->pattern == NULL || pfields->module == NULL)
1573 return FAIL;
1574
1575 return OK;
1576}
1577
1578/*
1579 * Free the fields used for parsing lines and populating a quickfix list.
1580 */
1581 static void
1582qf_free_fields(qffields_T *pfields)
1583{
1584 vim_free(pfields->namebuf);
1585 vim_free(pfields->module);
1586 vim_free(pfields->errmsg);
1587 vim_free(pfields->pattern);
1588}
1589
1590/*
1591 * Setup the state information used for parsing lines and populating a
1592 * quickfix list.
1593 */
1594 static int
1595qf_setup_state(
1596 qfstate_T *pstate,
1597 char_u *enc,
1598 char_u *efile,
1599 typval_T *tv,
1600 buf_T *buf,
1601 linenr_T lnumfirst,
1602 linenr_T lnumlast)
1603{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001604 pstate->vc.vc_type = CONV_NONE;
1605 if (enc != NULL && *enc != NUL)
1606 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001607
1608 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1609 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001610 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001611 return FAIL;
1612 }
1613
1614 if (tv != NULL)
1615 {
1616 if (tv->v_type == VAR_STRING)
1617 pstate->p_str = tv->vval.v_string;
1618 else if (tv->v_type == VAR_LIST)
1619 pstate->p_li = tv->vval.v_list->lv_first;
1620 pstate->tv = tv;
1621 }
1622 pstate->buf = buf;
1623 pstate->buflnum = lnumfirst;
1624 pstate->lnumlast = lnumlast;
1625
1626 return OK;
1627}
1628
1629/*
1630 * Cleanup the state information used for parsing lines and populating a
1631 * quickfix list.
1632 */
1633 static void
1634qf_cleanup_state(qfstate_T *pstate)
1635{
1636 if (pstate->fd != NULL)
1637 fclose(pstate->fd);
1638
1639 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001640 if (pstate->vc.vc_type != CONV_NONE)
1641 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001642}
1643
1644/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001645 * Process the next line from a file/buffer/list/string and add it
1646 * to the quickfix list 'qfl'.
1647 */
1648 static int
1649qf_init_process_nextline(
1650 qf_list_T *qfl,
1651 efm_T *fmt_first,
1652 qfstate_T *state,
1653 qffields_T *fields)
1654{
1655 int status;
1656
1657 // Get the next line from a file/buffer/list/string
1658 status = qf_get_nextline(state);
1659 if (status != QF_OK)
1660 return status;
1661
1662 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1663 fmt_first, fields);
1664 if (status != QF_OK)
1665 return status;
1666
1667 return qf_add_entry(qfl,
1668 qfl->qf_directory,
1669 (*fields->namebuf || qfl->qf_directory != NULL)
1670 ? fields->namebuf
1671 : ((qfl->qf_currfile != NULL && fields->valid)
1672 ? qfl->qf_currfile : (char_u *)NULL),
1673 fields->module,
1674 0,
1675 fields->errmsg,
1676 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001677 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001678 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001679 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001680 fields->use_viscol,
1681 fields->pattern,
1682 fields->enr,
1683 fields->type,
1684 fields->valid);
1685}
1686
1687/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001688 * Read the errorfile "efile" into memory, line by line, building the error
1689 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001690 * Alternative: when "efile" is NULL read errors from buffer "buf".
1691 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001692 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001693 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1694 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001695 * Return -1 for error, number of errors for success.
1696 */
1697 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001698qf_init_ext(
1699 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001700 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001701 char_u *efile,
1702 buf_T *buf,
1703 typval_T *tv,
1704 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001705 int newlist, // TRUE: start a new error list
1706 linenr_T lnumfirst, // first line number to use
1707 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001708 char_u *qf_title,
1709 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001710{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001711 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001712 qfstate_T state;
1713 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001714 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001715 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001716 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001718 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001719 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001720 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001722 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001723 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001724
Bram Moolenaara80faa82020-04-12 19:37:17 +02001725 CLEAR_FIELD(state);
1726 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001727 if ((qf_alloc_fields(&fields) == FAIL) ||
1728 (qf_setup_state(&state, enc, efile, tv, buf,
1729 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 goto qf_init_end;
1731
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001732 if (newlist || qf_idx == qi->qf_listcount)
1733 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001734 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001735 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001736 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001737 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001738 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001739 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001740 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001741 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001742 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001743 qfl = qf_get_list(qi, qf_idx);
1744 if (!qf_list_empty(qfl))
1745 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001748 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001749 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001750 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 else
1752 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001754 // If the errorformat didn't change between calls, then reuse the
1755 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001756 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1757 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001758 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001759 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001760 free_efm_list(&fmt_first);
1761
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001762 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001763 fmt_first = parse_efm_option(efm);
1764 if (fmt_first != NULL)
1765 last_efm = vim_strsave(efm);
1766 }
1767
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001768 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001771 // got_int is reset here, because it was probably set when killing the
1772 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 got_int = FALSE;
1774
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001775 // Read the lines in the error file one by one.
1776 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001777 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001779 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001780 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001781 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001782 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001783 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001784 if (status == QF_FAIL)
1785 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 line_breakcheck();
1788 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001789 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001791 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001793 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001794 qfl->qf_ptr = qfl->qf_start;
1795 qfl->qf_index = 1;
1796 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
1798 else
1799 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001800 qfl->qf_nonevalid = FALSE;
1801 if (qfl->qf_ptr == NULL)
1802 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001804 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001805 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001806 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001808 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001810 if (!adding)
1811 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001812 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001813 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001814 qi->qf_listcount--;
1815 if (qi->qf_curlist > 0)
1816 --qi->qf_curlist;
1817 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001818qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001819 if (qf_idx == qi->qf_curlist)
1820 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001821 qf_cleanup_state(&state);
1822 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 return retval;
1825}
1826
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001827/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001828 * Read the errorfile "efile" into memory, line by line, building the error
1829 * list. Set the error list's title to qf_title.
1830 * Return -1 for error, number of errors for success.
1831 */
1832 int
1833qf_init(win_T *wp,
1834 char_u *efile,
1835 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001836 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001837 char_u *qf_title,
1838 char_u *enc)
1839{
1840 qf_info_T *qi = &ql_info;
1841
1842 if (wp != NULL)
1843 {
1844 qi = ll_get_or_alloc_list(wp);
1845 if (qi == NULL)
1846 return FAIL;
1847 }
1848
1849 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1850 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1851}
1852
1853/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001854 * Set the title of the specified quickfix list. Frees the previous title.
1855 * Prepends ':' to the title.
1856 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001857 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001858qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001859{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001860 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001861
Bram Moolenaarfb604092014-07-23 15:55:00 +02001862 if (title != NULL)
1863 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001864 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001865
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001866 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001867 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001868 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001869 }
1870}
1871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001873 * The title of a quickfix/location list is set, by default, to the command
1874 * that created the quickfix list with the ":" prefix.
1875 * Create a quickfix list title string by prepending ":" to a user command.
1876 * Returns a pointer to a static buffer with the title.
1877 */
1878 static char_u *
1879qf_cmdtitle(char_u *cmd)
1880{
1881 static char_u qftitle_str[IOSIZE];
1882
1883 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1884 return qftitle_str;
1885}
1886
1887/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001888 * Return a pointer to the current list in the specified quickfix stack
1889 */
1890 static qf_list_T *
1891qf_get_curlist(qf_info_T *qi)
1892{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001893 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001894}
1895
1896/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001897 * Prepare for adding a new quickfix list. If the current list is in the
1898 * middle of the stack, then all the following lists are freed and then
1899 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 */
1901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001902qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
1904 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001905 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001907 // If the current entry is not the last entry, delete entries beyond
1908 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001909 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001910 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001911 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001913 // When the stack is full, remove to oldest entry
1914 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001915 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001917 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001919 qi->qf_lists[i - 1] = qi->qf_lists[i];
1920 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
1922 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001923 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001924 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001925 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001926 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001927 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001928 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929}
1930
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001931/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001932 * Queue location list stack delete request.
1933 */
1934 static void
1935locstack_queue_delreq(qf_info_T *qi)
1936{
1937 qf_delq_T *q;
1938
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001939 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001940 if (q != NULL)
1941 {
1942 q->qi = qi;
1943 q->next = qf_delq_head;
1944 qf_delq_head = q;
1945 }
1946}
1947
1948/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001949 * Return the global quickfix stack window buffer number.
1950 */
1951 int
1952qf_stack_get_bufnr(void)
1953{
1954 return ql_info.qf_bufnr;
1955}
1956
1957/*
1958 * Wipe the quickfix window buffer (if present) for the specified
1959 * quickfix/location list.
1960 */
1961 static void
1962wipe_qf_buffer(qf_info_T *qi)
1963{
1964 buf_T *qfbuf;
1965
1966 if (qi->qf_bufnr != INVALID_QFBUFNR)
1967 {
1968 qfbuf = buflist_findnr(qi->qf_bufnr);
1969 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1970 {
1971 // If the quickfix buffer is not loaded in any window, then
1972 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001973 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001974 qi->qf_bufnr = INVALID_QFBUFNR;
1975 }
1976 }
1977}
1978
1979/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001980 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001981 */
1982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001983ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001984{
1985 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001986 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001987
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001988 qi = *pqi;
1989 if (qi == NULL)
1990 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001991 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001992
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001993 // If the location list is still in use, then queue the delete request
1994 // to be processed later.
1995 if (quickfix_busy > 0)
1996 {
1997 locstack_queue_delreq(qi);
1998 return;
1999 }
2000
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002001 qi->qf_refcount--;
2002 if (qi->qf_refcount < 1)
2003 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002004 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002005 // If the quickfix window buffer is loaded, then wipe it
2006 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002007
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002008 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002009 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002010 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002011 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002012}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002013
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002014/*
2015 * Free all the quickfix/location lists in the stack.
2016 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002018qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002019{
2020 int i;
2021 qf_info_T *qi = &ql_info;
2022
2023 if (wp != NULL)
2024 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002025 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002026 ll_free_all(&wp->w_llist);
2027 ll_free_all(&wp->w_llist_ref);
2028 }
2029 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002030 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002031 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002032 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002033}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002036 * Delay freeing of location list stacks when the quickfix code is running.
2037 * Used to avoid problems with autocmds freeing location list stacks when the
2038 * quickfix code is still referencing the stack.
2039 * Must always call decr_quickfix_busy() exactly once after this.
2040 */
2041 static void
2042incr_quickfix_busy(void)
2043{
2044 quickfix_busy++;
2045}
2046
2047/*
2048 * Safe to free location list stacks. Process any delayed delete requests.
2049 */
2050 static void
2051decr_quickfix_busy(void)
2052{
2053 if (--quickfix_busy == 0)
2054 {
2055 // No longer referencing the location lists. Process all the pending
2056 // delete requests.
2057 while (qf_delq_head != NULL)
2058 {
2059 qf_delq_T *q = qf_delq_head;
2060
2061 qf_delq_head = q->next;
2062 ll_free_all(&q->qi);
2063 vim_free(q);
2064 }
2065 }
2066#ifdef ABORT_ON_INTERNAL_ERROR
2067 if (quickfix_busy < 0)
2068 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002069 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002070 abort();
2071 }
2072#endif
2073}
2074
2075#if defined(EXITFREE) || defined(PROTO)
2076 void
2077check_quickfix_busy(void)
2078{
2079 if (quickfix_busy != 0)
2080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002081 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002082# ifdef ABORT_ON_INTERNAL_ERROR
2083 abort();
2084# endif
2085 }
2086}
2087#endif
2088
2089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002091 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 */
2093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002094qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002095 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002096 char_u *dir, // optional directory name
2097 char_u *fname, // file name or NULL
2098 char_u *module, // module name or NULL
2099 int bufnum, // buffer number or zero
2100 char_u *mesg, // message
2101 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002102 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002103 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002104 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002105 int vis_col, // using visual column
2106 char_u *pattern, // search pattern
2107 int nr, // error number
2108 int type, // type character
2109 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002111 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002112 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002114 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002115 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002116 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002117 {
2118 buf_T *buf = buflist_findnr(bufnum);
2119
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002120 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002121 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002122 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002123 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002124 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002125 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002126 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2128 {
2129 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002130 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002133 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002135 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002136 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002137 if (pattern == NULL || *pattern == NUL)
2138 qfp->qf_pattern = NULL;
2139 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2140 {
2141 vim_free(qfp->qf_text);
2142 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002143 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002144 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002145 if (module == NULL || *module == NUL)
2146 qfp->qf_module = NULL;
2147 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2148 {
2149 vim_free(qfp->qf_text);
2150 vim_free(qfp->qf_pattern);
2151 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002152 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002155 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 type = 0;
2157 qfp->qf_type = type;
2158 qfp->qf_valid = valid;
2159
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002160 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002161 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002163 qfl->qf_start = qfp;
2164 qfl->qf_ptr = qfp;
2165 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002166 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
2168 else
2169 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002170 qfp->qf_prev = *lastp;
2171 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002173 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002175 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002176 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002177 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002179 qfl->qf_index = qfl->qf_count;
2180 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
2182
Bram Moolenaar95946f12019-03-31 15:31:59 +02002183 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184}
2185
2186/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002187 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002188 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002189 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002190qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002191{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002192 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002193
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002194 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002195 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002196 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002197 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002198 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002199 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002200 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002201 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002202}
2203
2204/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002205 * Return the location list stack for window 'wp'.
2206 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002207 */
2208 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002209ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002210{
2211 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002212 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002213 return wp->w_llist_ref;
2214
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002215 // For a non-location list window, w_llist_ref should not point to a
2216 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002217 ll_free_all(&wp->w_llist_ref);
2218
2219 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002220 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002221 return wp->w_llist;
2222}
2223
2224/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002225 * Get the quickfix/location list stack to use for the specified Ex command.
2226 * For a location list command, returns the stack for the current window. If
2227 * the location list is not found, then returns NULL and prints an error
2228 * message if 'print_emsg' is TRUE.
2229 */
2230 static qf_info_T *
2231qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2232{
2233 qf_info_T *qi = &ql_info;
2234
2235 if (is_loclist_cmd(eap->cmdidx))
2236 {
2237 qi = GET_LOC_LIST(curwin);
2238 if (qi == NULL)
2239 {
2240 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002241 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002242 return NULL;
2243 }
2244 }
2245
2246 return qi;
2247}
2248
2249/*
2250 * Get the quickfix/location list stack to use for the specified Ex command.
2251 * For a location list command, returns the stack for the current window.
2252 * If the location list is not present, then allocates a new one.
2253 * Returns NULL if the allocation fails. For a location list command, sets
2254 * 'pwinp' to curwin.
2255 */
2256 static qf_info_T *
2257qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2258{
2259 qf_info_T *qi = &ql_info;
2260
2261 if (is_loclist_cmd(eap->cmdidx))
2262 {
2263 qi = ll_get_or_alloc_list(curwin);
2264 if (qi == NULL)
2265 return NULL;
2266 *pwinp = curwin;
2267 }
2268
2269 return qi;
2270}
2271
2272/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002273 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2274 */
2275 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002276copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002277{
2278 int i;
2279 qfline_T *from_qfp;
2280 qfline_T *prevp;
2281
2282 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002283 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002284 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002285 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002286 NULL,
2287 NULL,
2288 from_qfp->qf_module,
2289 0,
2290 from_qfp->qf_text,
2291 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002292 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002293 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002294 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002295 from_qfp->qf_viscol,
2296 from_qfp->qf_pattern,
2297 from_qfp->qf_nr,
2298 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002299 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002300 return FAIL;
2301
2302 // qf_add_entry() will not set the qf_num field, as the
2303 // directory and file names are not supplied. So the qf_fnum
2304 // field is copied here.
2305 prevp = to_qfl->qf_last;
2306 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2307 prevp->qf_type = from_qfp->qf_type; // error type
2308 if (from_qfl->qf_ptr == from_qfp)
2309 to_qfl->qf_ptr = prevp; // current location
2310 }
2311
2312 return OK;
2313}
2314
2315/*
2316 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2317 */
2318 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002319copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002320{
2321 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002322 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002323 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2324 to_qfl->qf_count = 0;
2325 to_qfl->qf_index = 0;
2326 to_qfl->qf_start = NULL;
2327 to_qfl->qf_last = NULL;
2328 to_qfl->qf_ptr = NULL;
2329 if (from_qfl->qf_title != NULL)
2330 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2331 else
2332 to_qfl->qf_title = NULL;
2333 if (from_qfl->qf_ctx != NULL)
2334 {
2335 to_qfl->qf_ctx = alloc_tv();
2336 if (to_qfl->qf_ctx != NULL)
2337 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2338 }
2339 else
2340 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002341 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2342 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002343 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002344 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002345
2346 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002347 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002348 return FAIL;
2349
2350 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2351
2352 // Assign a new ID for the location list
2353 to_qfl->qf_id = ++last_qf_id;
2354 to_qfl->qf_changedtick = 0L;
2355
2356 // When no valid entries are present in the list, qf_ptr points to
2357 // the first item in the list
2358 if (to_qfl->qf_nonevalid)
2359 {
2360 to_qfl->qf_ptr = to_qfl->qf_start;
2361 to_qfl->qf_index = 1;
2362 }
2363
2364 return OK;
2365}
2366
2367/*
2368 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002369 */
2370 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002371copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002372{
2373 qf_info_T *qi;
2374 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002375
Bram Moolenaar09037502018-09-25 22:08:14 +02002376 // When copying from a location list window, copy the referenced
2377 // location list. For other windows, copy the location list for
2378 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002379 if (IS_LL_WINDOW(from))
2380 qi = from->w_llist_ref;
2381 else
2382 qi = from->w_llist;
2383
Bram Moolenaar09037502018-09-25 22:08:14 +02002384 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002385 return;
2386
Bram Moolenaar09037502018-09-25 22:08:14 +02002387 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002388 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002389 return;
2390
2391 to->w_llist->qf_listcount = qi->qf_listcount;
2392
Bram Moolenaar09037502018-09-25 22:08:14 +02002393 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002394 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002395 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002396 to->w_llist->qf_curlist = idx;
2397
Bram Moolenaar0398e002019-03-21 21:12:49 +01002398 if (copy_loclist(qf_get_list(qi, idx),
2399 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002400 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002401 qf_free_all(to);
2402 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002403 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002404 }
2405
Bram Moolenaar09037502018-09-25 22:08:14 +02002406 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002407}
2408
2409/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002410 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002411 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 */
2413 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002414qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
Bram Moolenaar82404332016-07-10 17:00:38 +02002416 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002417 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002418 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002419
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002420 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaare60acc12011-05-10 16:41:25 +02002423#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002424 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002425#endif
2426#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002427 if (directory != NULL)
2428 slash_adjust(directory);
2429 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002430#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002431 if (directory != NULL && !vim_isAbsName(fname)
2432 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2433 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002434 // Here we check if the file really exists.
2435 // This should normally be true, but if make works without
2436 // "leaving directory"-messages we might have missed a
2437 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002438 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002441 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002442 if (directory)
2443 ptr = concat_fnames(directory, fname, TRUE);
2444 else
2445 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002447 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002448 bufname = ptr;
2449 }
2450 else
2451 bufname = fname;
2452
2453 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002454 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002455 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002456 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002457 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002459 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002460 {
2461 vim_free(qf_last_bufname);
2462 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2463 if (bufname == ptr)
2464 qf_last_bufname = bufname;
2465 else
2466 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002467 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002468 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002469 if (buf == NULL)
2470 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002471
Bram Moolenaarc1542742016-07-20 21:44:37 +02002472 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002473 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002474 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475}
2476
2477/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002478 * Push dirbuf onto the directory stack and return pointer to actual dir or
2479 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
2481 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002482qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 struct dir_stack_T *ds_new;
2485 struct dir_stack_T *ds_ptr;
2486
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002487 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002488 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 if (ds_new == NULL)
2490 return NULL;
2491
2492 ds_new->next = *stackptr;
2493 *stackptr = ds_new;
2494
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002495 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 if (vim_isAbsName(dirbuf)
2497 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002498 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 (*stackptr)->dirname = vim_strsave(dirbuf);
2500 else
2501 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002502 // Okay we don't have an absolute path.
2503 // dirbuf must be a subdir of one of the directories on the stack.
2504 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 ds_new = (*stackptr)->next;
2506 (*stackptr)->dirname = NULL;
2507 while (ds_new)
2508 {
2509 vim_free((*stackptr)->dirname);
2510 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2511 TRUE);
2512 if (mch_isdir((*stackptr)->dirname) == TRUE)
2513 break;
2514
2515 ds_new = ds_new->next;
2516 }
2517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002518 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 while ((*stackptr)->next != ds_new)
2520 {
2521 ds_ptr = (*stackptr)->next;
2522 (*stackptr)->next = (*stackptr)->next->next;
2523 vim_free(ds_ptr->dirname);
2524 vim_free(ds_ptr);
2525 }
2526
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002527 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (ds_new == NULL)
2529 {
2530 vim_free((*stackptr)->dirname);
2531 (*stackptr)->dirname = vim_strsave(dirbuf);
2532 }
2533 }
2534
2535 if ((*stackptr)->dirname != NULL)
2536 return (*stackptr)->dirname;
2537 else
2538 {
2539 ds_ptr = *stackptr;
2540 *stackptr = (*stackptr)->next;
2541 vim_free(ds_ptr);
2542 return NULL;
2543 }
2544}
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546/*
2547 * pop dirbuf from the directory stack and return previous directory or NULL if
2548 * stack is empty
2549 */
2550 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002551qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 struct dir_stack_T *ds_ptr;
2554
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002555 // TODO: Should we check if dirbuf is the directory on top of the stack?
2556 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002558 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (*stackptr != NULL)
2560 {
2561 ds_ptr = *stackptr;
2562 *stackptr = (*stackptr)->next;
2563 vim_free(ds_ptr->dirname);
2564 vim_free(ds_ptr);
2565 }
2566
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002567 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return *stackptr ? (*stackptr)->dirname : NULL;
2569}
2570
2571/*
2572 * clean up directory stack
2573 */
2574 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002575qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
2577 struct dir_stack_T *ds_ptr;
2578
2579 while ((ds_ptr = *stackptr) != NULL)
2580 {
2581 *stackptr = (*stackptr)->next;
2582 vim_free(ds_ptr->dirname);
2583 vim_free(ds_ptr);
2584 }
2585}
2586
2587/*
2588 * Check in which directory of the directory stack the given file can be
2589 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002590 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 * Cleans up intermediate directory entries.
2592 *
2593 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002594 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 * ./
2596 * ./aa
2597 * ./aa/bb
2598 * ./bb
2599 * ./bb/x.c
2600 * and make says:
2601 * making all in aa
2602 * making all in bb
2603 * x.c:9: Error
2604 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2605 * qf_guess_filepath will return NULL.
2606 */
2607 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002608qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 struct dir_stack_T *ds_ptr;
2611 struct dir_stack_T *ds_tmp;
2612 char_u *fullname;
2613
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002614 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002615 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 return NULL;
2617
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002618 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 fullname = NULL;
2620 while (ds_ptr)
2621 {
2622 vim_free(fullname);
2623 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2624
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002625 // If concat_fnames failed, just go on. The worst thing that can happen
2626 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2628 break;
2629
2630 ds_ptr = ds_ptr->next;
2631 }
2632
2633 vim_free(fullname);
2634
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002635 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002636 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002638 ds_tmp = qfl->qf_dir_stack->next;
2639 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 vim_free(ds_tmp->dirname);
2641 vim_free(ds_tmp);
2642 }
2643
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002644 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645}
2646
2647/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002648 * Returns TRUE if a quickfix/location list with the given identifier exists.
2649 */
2650 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002651qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002652{
2653 qf_info_T *qi = &ql_info;
2654 int i;
2655
2656 if (wp != NULL)
2657 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002658 if (!win_valid(wp))
2659 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002660 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002661 if (qi == NULL)
2662 return FALSE;
2663 }
2664
2665 for (i = 0; i < qi->qf_listcount; ++i)
2666 if (qi->qf_lists[i].qf_id == qf_id)
2667 return TRUE;
2668
2669 return FALSE;
2670}
2671
2672/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002673 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002674 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002675 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002676 * Similar to location list.
2677 */
2678 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002679is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002680{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002681 qfline_T *qfp;
2682 int i;
2683
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002684 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002685 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2686 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002687 break;
2688
Bram Moolenaar95946f12019-03-31 15:31:59 +02002689 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002690 return FALSE;
2691
2692 return TRUE;
2693}
2694
2695/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002696 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002697 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002698 */
2699 static qfline_T *
2700get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002701 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002702 qfline_T *qf_ptr,
2703 int *qf_index,
2704 int dir)
2705{
2706 int idx;
2707 int old_qf_fnum;
2708
2709 idx = *qf_index;
2710 old_qf_fnum = qf_ptr->qf_fnum;
2711
2712 do
2713 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002714 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002715 return NULL;
2716 ++idx;
2717 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002718 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002719 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2720
2721 *qf_index = idx;
2722 return qf_ptr;
2723}
2724
2725/*
2726 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002727 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002728 */
2729 static qfline_T *
2730get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002731 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002732 qfline_T *qf_ptr,
2733 int *qf_index,
2734 int dir)
2735{
2736 int idx;
2737 int old_qf_fnum;
2738
2739 idx = *qf_index;
2740 old_qf_fnum = qf_ptr->qf_fnum;
2741
2742 do
2743 {
2744 if (idx == 1 || qf_ptr->qf_prev == NULL)
2745 return NULL;
2746 --idx;
2747 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002748 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002749 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2750
2751 *qf_index = idx;
2752 return qf_ptr;
2753}
2754
2755/*
2756 * Get the n'th (errornr) previous/next valid entry from the current entry in
2757 * the quickfix list.
2758 * dir == FORWARD or FORWARD_FILE: next valid entry
2759 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2760 */
2761 static qfline_T *
2762get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002763 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002764 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002765 int dir,
2766 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002767{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002768 qfline_T *qf_ptr = qfl->qf_ptr;
2769 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002770 qfline_T *prev_qf_ptr;
2771 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002772 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002773
2774 while (errornr--)
2775 {
2776 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002777 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002778
2779 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002780 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002781 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002782 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002783 if (qf_ptr == NULL)
2784 {
2785 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002786 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002787 if (err != NULL)
2788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002789 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002790 return NULL;
2791 }
2792 break;
2793 }
2794
2795 err = NULL;
2796 }
2797
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002798 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002799 return qf_ptr;
2800}
2801
2802/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002803 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2804 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002805 */
2806 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002807get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002808{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002809 qfline_T *qf_ptr = qfl->qf_ptr;
2810 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002811
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002812 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002813 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2814 {
2815 --qf_idx;
2816 qf_ptr = qf_ptr->qf_prev;
2817 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002818 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002819 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2820 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002821 {
2822 ++qf_idx;
2823 qf_ptr = qf_ptr->qf_next;
2824 }
2825
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002826 *new_qfidx = qf_idx;
2827 return qf_ptr;
2828}
2829
2830/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002831 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002832 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2833 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2834 * Returns a pointer to the entry and the index of the new entry is stored in
2835 * 'new_qfidx'.
2836 */
2837 static qfline_T *
2838qf_get_entry(
2839 qf_list_T *qfl,
2840 int errornr,
2841 int dir,
2842 int *new_qfidx)
2843{
2844 qfline_T *qf_ptr = qfl->qf_ptr;
2845 int qfidx = qfl->qf_index;
2846
2847 if (dir != 0) // next/prev valid entry
2848 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2849 else if (errornr != 0) // go to specified number
2850 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2851
2852 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002853 return qf_ptr;
2854}
2855
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002856/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002857 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002858 */
2859 static win_T *
2860qf_find_help_win(void)
2861{
2862 win_T *wp;
2863
2864 FOR_ALL_WINDOWS(wp)
2865 if (bt_help(wp->w_buffer))
2866 return wp;
2867
2868 return NULL;
2869}
2870
2871/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002872 * Set the location list for the specified window to 'qi'.
2873 */
2874 static void
2875win_set_loclist(win_T *wp, qf_info_T *qi)
2876{
2877 wp->w_llist = qi;
2878 qi->qf_refcount++;
2879}
2880
2881/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002882 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2883 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002884 */
2885 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002886jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002887{
2888 win_T *wp;
2889 int flags;
2890
Bram Moolenaare1004402020-10-24 20:49:43 +02002891 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002892 wp = NULL;
2893 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002894 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002895 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2896 win_enter(wp, TRUE);
2897 else
2898 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002899 // Split off help window; put it at far top if no position
2900 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002901 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002902 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002903 && curwin->w_width < 80)
2904 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002905 // If the user asks to open a new window, then copy the location list.
2906 // Otherwise, don't copy the location list.
2907 if (IS_LL_STACK(qi) && !newwin)
2908 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002909
2910 if (win_split(0, flags) == FAIL)
2911 return FAIL;
2912
2913 *opened_window = TRUE;
2914
2915 if (curwin->w_height < p_hh)
2916 win_setheight((int)p_hh);
2917
Bram Moolenaarb2443732018-11-11 22:50:27 +01002918 // When using location list, the new window should use the supplied
2919 // location list. If the user asks to open a new window, then the new
2920 // window will get a copy of the location list.
2921 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002922 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002923 }
2924
2925 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002926 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002927
2928 return OK;
2929}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002930
2931/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002932 * Find a non-quickfix window using the given location list stack in the
2933 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002934 * Returns NULL if a matching window is not found.
2935 */
2936 static win_T *
2937qf_find_win_with_loclist(qf_info_T *ll)
2938{
2939 win_T *wp;
2940
2941 FOR_ALL_WINDOWS(wp)
2942 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2943 return wp;
2944
2945 return NULL;
2946}
2947
2948/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002949 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002950 */
2951 static win_T *
2952qf_find_win_with_normal_buf(void)
2953{
2954 win_T *wp;
2955
2956 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002957 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002958 return wp;
2959
2960 return NULL;
2961}
2962
2963/*
2964 * Go to a window in any tabpage containing the specified file. Returns TRUE
2965 * if successfully jumped to the window. Otherwise returns FALSE.
2966 */
2967 static int
2968qf_goto_tabwin_with_file(int fnum)
2969{
2970 tabpage_T *tp;
2971 win_T *wp;
2972
2973 FOR_ALL_TAB_WINDOWS(tp, wp)
2974 if (wp->w_buffer->b_fnum == fnum)
2975 {
2976 goto_tabpage_win(tp, wp);
2977 return TRUE;
2978 }
2979
2980 return FALSE;
2981}
2982
2983/*
2984 * Create a new window to show a file above the quickfix window. Called when
2985 * only the quickfix window is present.
2986 */
2987 static int
2988qf_open_new_file_win(qf_info_T *ll_ref)
2989{
2990 int flags;
2991
2992 flags = WSP_ABOVE;
2993 if (ll_ref != NULL)
2994 flags |= WSP_NEWLOC;
2995 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002996 return FAIL; // not enough room for window
2997 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002998 swb_flags = 0;
2999 RESET_BINDING(curwin);
3000 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003001 // The new window should use the location list from the
3002 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003003 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003004 return OK;
3005}
3006
3007/*
3008 * Go to a window that shows the right buffer. If the window is not found, go
3009 * to the window just above the location list window. This is used for opening
3010 * a file from a location window and not from a quickfix window. If some usable
3011 * window is previously found, then it is supplied in 'use_win'.
3012 */
3013 static void
3014qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3015{
3016 win_T *win = use_win;
3017
3018 if (win == NULL)
3019 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003020 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003021 FOR_ALL_WINDOWS(win)
3022 if (win->w_buffer->b_fnum == qf_fnum)
3023 break;
3024 if (win == NULL)
3025 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003026 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003027 win = curwin;
3028 do
3029 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003030 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003031 break;
3032 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003033 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003034 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003035 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003036 } while (win != curwin);
3037 }
3038 }
3039 win_goto(win);
3040
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003041 // If the location list for the window is not set, then set it
3042 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003043 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003044 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003045}
3046
3047/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003048 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3049 * not found, then go to the window just above the quickfix window. This is
3050 * used for opening a file from a quickfix window and not from a location
3051 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003052 */
3053 static void
3054qf_goto_win_with_qfl_file(int qf_fnum)
3055{
3056 win_T *win;
3057 win_T *altwin;
3058
3059 win = curwin;
3060 altwin = NULL;
3061 for (;;)
3062 {
3063 if (win->w_buffer->b_fnum == qf_fnum)
3064 break;
3065 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003066 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003067 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003068 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003069
3070 if (IS_QF_WINDOW(win))
3071 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003072 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003073 // window, unless 'switchbuf' contains 'uselast': in this case we
3074 // try to jump to the previously used window first.
3075 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3076 win = prevwin;
3077 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003078 win = altwin;
3079 else if (curwin->w_prev != NULL)
3080 win = curwin->w_prev;
3081 else
3082 win = curwin->w_next;
3083 break;
3084 }
3085
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003086 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003087 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003088 altwin = win;
3089 }
3090
3091 win_goto(win);
3092}
3093
3094/*
3095 * Find a suitable window for opening a file (qf_fnum) from the
3096 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003097 * window, jump to it. Otherwise open a new window to display the file. If
3098 * 'newwin' is TRUE, then always open a new window. This is called from either
3099 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003100 */
3101 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003102qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003103{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003104 win_T *usable_wp = NULL;
3105 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003106 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003107
Bram Moolenaarb2443732018-11-11 22:50:27 +01003108 // If opening a new window, then don't use the location list referred by
3109 // the current window. Otherwise two windows will refer to the same
3110 // location list.
3111 if (!newwin)
3112 ll_ref = curwin->w_llist_ref;
3113
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003114 if (ll_ref != NULL)
3115 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003116 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003117 usable_wp = qf_find_win_with_loclist(ll_ref);
3118 if (usable_wp != NULL)
3119 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003120 }
3121
3122 if (!usable_win)
3123 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003124 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003125 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003126 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003127 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003128 }
3129
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003130 // If no usable window is found and 'switchbuf' contains "usetab"
3131 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003132 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003133 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003134
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003135 // If there is only one window and it is the quickfix window, create a
3136 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003137 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003138 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003139 if (qf_open_new_file_win(ll_ref) != OK)
3140 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003141 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003142 }
3143 else
3144 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003145 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003146 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003147 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003148 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003149 }
3150
3151 return OK;
3152}
3153
3154/*
3155 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003156 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003157 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003158 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003159 */
3160 static int
3161qf_jump_edit_buffer(
3162 qf_info_T *qi,
3163 qfline_T *qf_ptr,
3164 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003165 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003166 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003167{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003168 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003169 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003170 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003171 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003172 int old_qf_curlist = qi->qf_curlist;
3173 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003174
3175 if (qf_ptr->qf_type == 1)
3176 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003177 // Open help file (do_ecmd() will set b_help flag, readfile() will
3178 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003179 if (!can_abandon(curbuf, forceit))
3180 {
3181 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003182 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003183 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003184
3185 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3186 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003187 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003188 }
3189 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003190 retval = buflist_getfile(qf_ptr->qf_fnum,
3191 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003192
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003193 // If a location list, check whether the associated window is still
3194 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003195 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003196 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003197 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003198
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003199 if (wp == NULL && curwin->w_llist != qi)
3200 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003201 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003202 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003203 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003204 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003205 }
3206
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003207 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003208 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003209 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003210 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003211 }
3212
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003213 // Check if the list was changed. The pointers may happen to be identical,
3214 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003215 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003216 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003217 || !is_qf_entry_present(qfl, qf_ptr))
3218 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003219 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003220 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003221 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003222 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003223 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003224 }
3225
3226 return retval;
3227}
3228
3229/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003230 * Go to the error line in the current file using either line/column number or
3231 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003232 */
3233 static void
3234qf_jump_goto_line(
3235 linenr_T qf_lnum,
3236 int qf_col,
3237 char_u qf_viscol,
3238 char_u *qf_pattern)
3239{
3240 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003241
3242 if (qf_pattern == NULL)
3243 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003244 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003245 i = qf_lnum;
3246 if (i > 0)
3247 {
3248 if (i > curbuf->b_ml.ml_line_count)
3249 i = curbuf->b_ml.ml_line_count;
3250 curwin->w_cursor.lnum = i;
3251 }
3252 if (qf_col > 0)
3253 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003254 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003255 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003256 coladvance(qf_col - 1);
3257 else
3258 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003259 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003260 check_cursor();
3261 }
3262 else
3263 beginline(BL_WHITE | BL_FIX);
3264 }
3265 else
3266 {
3267 pos_T save_cursor;
3268
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003269 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003270 save_cursor = curwin->w_cursor;
3271 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003272 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003273 curwin->w_cursor = save_cursor;
3274 }
3275}
3276
3277/*
3278 * Display quickfix list index and size message
3279 */
3280 static void
3281qf_jump_print_msg(
3282 qf_info_T *qi,
3283 int qf_index,
3284 qfline_T *qf_ptr,
3285 buf_T *old_curbuf,
3286 linenr_T old_lnum)
3287{
3288 linenr_T i;
3289 int len;
3290
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003291 // Update the screen before showing the message, unless the screen
3292 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003293 if (!msg_scrolled)
3294 update_topline_redraw();
3295 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003296 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003297 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3298 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003299 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003300 len = (int)STRLEN(IObuff);
3301 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3302
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003303 // Output the message. Overwrite to avoid scrolling when the 'O'
3304 // flag is present in 'shortmess'; But when not jumping, print the
3305 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003306 i = msg_scroll;
3307 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3308 msg_scroll = TRUE;
3309 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3310 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003311 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003312 msg_scroll = i;
3313}
3314
3315/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003316 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003317 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3318 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003319 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3320 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003321 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3322 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003323 */
3324 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003325qf_jump_open_window(
3326 qf_info_T *qi,
3327 qfline_T *qf_ptr,
3328 int newwin,
3329 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003330{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003331 qf_list_T *qfl = qf_get_curlist(qi);
3332 int old_changedtick = qfl->qf_changedtick;
3333 int old_qf_curlist = qi->qf_curlist;
3334 qfltype_T qfl_type = qfl->qfl_type;
3335
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003336 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003337 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3338 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003339 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003340 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003341 if (old_qf_curlist != qi->qf_curlist
3342 || old_changedtick != qfl->qf_changedtick
3343 || !is_qf_entry_present(qfl, qf_ptr))
3344 {
3345 if (qfl_type == QFLT_QUICKFIX)
3346 emsg(_(e_current_quickfix_list_was_changed));
3347 else
3348 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003349 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003350 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003351
3352 // If currently in the quickfix window, find another window to show the
3353 // file in.
3354 if (bt_quickfix(curbuf) && !*opened_window)
3355 {
3356 // If there is no file specified, we don't know where to go.
3357 // But do advance, otherwise ":cn" gets stuck.
3358 if (qf_ptr->qf_fnum == 0)
3359 return NOTDONE;
3360
Bram Moolenaarb2443732018-11-11 22:50:27 +01003361 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3362 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003363 return FAIL;
3364 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003365 if (old_qf_curlist != qi->qf_curlist
3366 || old_changedtick != qfl->qf_changedtick
3367 || !is_qf_entry_present(qfl, qf_ptr))
3368 {
3369 if (qfl_type == QFLT_QUICKFIX)
3370 emsg(_(e_current_quickfix_list_was_changed));
3371 else
3372 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003373 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003374 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003375
3376 return OK;
3377}
3378
3379/*
3380 * Edit a selected file from the quickfix/location list and jump to a
3381 * particular line/column, adjust the folds and display a message about the
3382 * jump.
3383 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003384 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003385 * the file.
3386 */
3387 static int
3388qf_jump_to_buffer(
3389 qf_info_T *qi,
3390 int qf_index,
3391 qfline_T *qf_ptr,
3392 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003393 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003394 int *opened_window,
3395 int openfold,
3396 int print_message)
3397{
3398 buf_T *old_curbuf;
3399 linenr_T old_lnum;
3400 int retval = OK;
3401
3402 // If there is a file name, read the wanted file if needed, and check
3403 // autowrite etc.
3404 old_curbuf = curbuf;
3405 old_lnum = curwin->w_cursor.lnum;
3406
3407 if (qf_ptr->qf_fnum != 0)
3408 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003409 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003410 opened_window);
3411 if (retval != OK)
3412 return retval;
3413 }
3414
3415 // When not switched to another buffer, still need to set pc mark
3416 if (curbuf == old_curbuf)
3417 setpcmark();
3418
3419 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3420 qf_ptr->qf_pattern);
3421
3422#ifdef FEAT_FOLDING
3423 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3424 foldOpenCursor();
3425#endif
3426 if (print_message)
3427 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3428
3429 return retval;
3430}
3431
3432/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003433 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 */
3435 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003436qf_jump(qf_info_T *qi,
3437 int dir,
3438 int errornr,
3439 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003441 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3442}
3443
3444/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003445 * Jump to a quickfix line.
3446 * If dir == 0 go to entry "errornr".
3447 * If dir == FORWARD go "errornr" valid entries forward.
3448 * If dir == BACKWARD go "errornr" valid entries backward.
3449 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3450 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3451 * else if "errornr" is zero, redisplay the same line
3452 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003453 * If 'newwin' is TRUE, then open the file in a new window.
3454 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003455 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003456qf_jump_newwin(qf_info_T *qi,
3457 int dir,
3458 int errornr,
3459 int forceit,
3460 int newwin)
3461{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003462 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003463 qfline_T *qf_ptr;
3464 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003467 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003468 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003469 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003472 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003473 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003475 if (qi == NULL)
3476 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003477
Bram Moolenaar0398e002019-03-21 21:12:49 +01003478 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003480 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 return;
3482 }
3483
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003484 incr_quickfix_busy();
3485
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003486 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003487
3488 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003490 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003492
3493 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3494 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003496 qf_ptr = old_qf_ptr;
3497 qf_index = old_qf_index;
3498 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003501 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003502 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003503 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003504 // No need to print the error message if it's visible in the error
3505 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 print_message = FALSE;
3507
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003508 prev_winid = curwin->w_id;
3509
Bram Moolenaarb2443732018-11-11 22:50:27 +01003510 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003511 if (retval == FAIL)
3512 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003513 if (retval == QF_ABORT)
3514 {
3515 qi = NULL;
3516 qf_ptr = NULL;
3517 goto theend;
3518 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003519 if (retval == NOTDONE)
3520 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003521
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003522 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003523 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003524 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003526 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003527 qi = NULL;
3528 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003531 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003534 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003535 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003537 // Couldn't open file, so put index back where it was. This could
3538 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 qf_ptr = old_qf_ptr;
3541 qf_index = old_qf_index;
3542 }
3543 }
3544theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003545 if (qi != NULL)
3546 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003547 qfl->qf_ptr = qf_ptr;
3548 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003549 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003550 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003552 // Restore old 'switchbuf' value, but not when an autocommand or
3553 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003554 p_swb = old_swb;
3555 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003557 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558}
3559
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003560// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003561static int qfFileAttr;
3562static int qfSepAttr;
3563static int qfLineAttr;
3564
3565/*
3566 * Display information about a single entry from the quickfix/location list.
3567 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003568 * 'cursel' will be set to TRUE for the currently selected entry in the
3569 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003570 */
3571 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003572qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003573{
3574 char_u *fname;
3575 buf_T *buf;
3576 int filter_entry;
3577
3578 fname = NULL;
3579 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3580 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3581 (char *)qfp->qf_module);
3582 else {
3583 if (qfp->qf_fnum != 0
3584 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3585 {
3586 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003587 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003588 fname = gettail(fname);
3589 }
3590 if (fname == NULL)
3591 sprintf((char *)IObuff, "%2d", qf_idx);
3592 else
3593 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3594 qf_idx, (char *)fname);
3595 }
3596
3597 // Support for filtering entries using :filter /pat/ clist
3598 // Match against the module name, file name, search pattern and
3599 // text of the entry.
3600 filter_entry = TRUE;
3601 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3602 filter_entry &= message_filtered(qfp->qf_module);
3603 if (filter_entry && fname != NULL)
3604 filter_entry &= message_filtered(fname);
3605 if (filter_entry && qfp->qf_pattern != NULL)
3606 filter_entry &= message_filtered(qfp->qf_pattern);
3607 if (filter_entry)
3608 filter_entry &= message_filtered(qfp->qf_text);
3609 if (filter_entry)
3610 return;
3611
3612 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003613 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003614
3615 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003616 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003617 if (qfp->qf_lnum == 0)
3618 IObuff[0] = NUL;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003619 else
thinca6864efa2021-06-19 20:45:20 +02003620 qf_range_text(qfp, IObuff, IOSIZE);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003621 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3622 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003623 msg_puts_attr((char *)IObuff, qfLineAttr);
3624 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003625 if (qfp->qf_pattern != NULL)
3626 {
3627 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003628 msg_puts((char *)IObuff);
3629 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003630 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003631 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003632
Bram Moolenaar5f30e262022-07-28 11:56:01 +01003633 {
3634 char_u *tbuf = IObuff;
3635 size_t tbuflen = IOSIZE;
3636 size_t len = STRLEN(qfp->qf_text) + 3;
3637
3638 if (len > IOSIZE)
3639 {
3640 tbuf = alloc(len);
3641 if (tbuf != NULL)
3642 tbuflen = len;
3643 else
3644 tbuf = IObuff;
3645 }
3646
3647 // Remove newlines and leading whitespace from the text. For an
3648 // unrecognized line keep the indent, the compiler may mark a word
3649 // with ^^^^.
3650 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3651 ? skipwhite(qfp->qf_text) : qfp->qf_text,
Mike Williamsab146da2022-08-01 14:00:31 +01003652 tbuf, (int)tbuflen);
Bram Moolenaar5f30e262022-07-28 11:56:01 +01003653 msg_prt_line(tbuf, FALSE);
3654
3655 if (tbuf != IObuff)
3656 vim_free(tbuf);
3657 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003658 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003659}
3660
3661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003663 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
3665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003666qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003668 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003669 qfline_T *qfp;
3670 int i;
3671 int idx1 = 1;
3672 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003673 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003674 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003675 int all = eap->forceit; // if not :cl!, only show
3676 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003677 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003679 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3680 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003681
Bram Moolenaar0398e002019-03-21 21:12:49 +01003682 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003684 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 return;
3686 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003687 if (*arg == '+')
3688 {
3689 ++arg;
3690 plus = TRUE;
3691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3693 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003694 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 return;
3696 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003697 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003698 if (plus)
3699 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003700 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003701 idx2 = i + idx1;
3702 idx1 = i;
3703 }
3704 else
3705 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003706 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003707 if (idx1 < 0)
3708 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3709 if (idx2 < 0)
3710 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003713 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003714 shorten_fnames(FALSE);
3715
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003716 // Get the attributes for the different quickfix highlight items. Note
3717 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003718 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3719 if (qfFileAttr == 0)
3720 qfFileAttr = HL_ATTR(HLF_D);
3721 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3722 if (qfSepAttr == 0)
3723 qfSepAttr = HL_ATTR(HLF_D);
3724 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3725 if (qfLineAttr == 0)
3726 qfLineAttr = HL_ATTR(HLF_N);
3727
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003728 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003730 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
3732 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003733 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003734
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 ui_breakcheck();
3736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737}
3738
3739/*
3740 * Remove newlines and leading whitespace from an error message.
3741 * Put the result in "buf[bufsize]".
3742 */
3743 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003744qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
3746 int i;
3747 char_u *p = text;
3748
3749 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3750 {
3751 if (*p == '\n')
3752 {
3753 buf[i] = ' ';
3754 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003755 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 break;
3757 }
3758 else
3759 buf[i] = *p++;
3760 }
3761 buf[i] = NUL;
3762}
3763
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003764/*
thinca6864efa2021-06-19 20:45:20 +02003765 * Range information from lnum, col, end_lnum, and end_col.
3766 * Put the result in "buf[bufsize]".
3767 */
3768 static void
3769qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3770{
3771 int len;
3772 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3773 len = (int)STRLEN(buf);
3774
3775 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3776 {
3777 vim_snprintf((char *)buf + len, bufsize - len,
3778 "-%ld", qfp->qf_end_lnum);
3779 len += (int)STRLEN(buf + len);
3780 }
3781 if (qfp->qf_col > 0)
3782 {
3783 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3784 len += (int)STRLEN(buf + len);
3785 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3786 {
3787 vim_snprintf((char *)buf + len, bufsize - len,
3788 "-%d", qfp->qf_end_col);
3789 len += (int)STRLEN(buf + len);
3790 }
3791 }
3792 buf[len] = NUL;
3793}
3794
3795/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003796 * Display information (list number, list size and the title) about a
3797 * quickfix/location list.
3798 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003799 static void
3800qf_msg(qf_info_T *qi, int which, char *lead)
3801{
3802 char *title = (char *)qi->qf_lists[which].qf_title;
3803 int count = qi->qf_lists[which].qf_count;
3804 char_u buf[IOSIZE];
3805
3806 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3807 lead,
3808 which + 1,
3809 qi->qf_listcount,
3810 count);
3811
3812 if (title != NULL)
3813 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003814 size_t len = STRLEN(buf);
3815
3816 if (len < 34)
3817 {
3818 vim_memset(buf + len, ' ', 34 - len);
3819 buf[34] = NUL;
3820 }
3821 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003822 }
3823 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003824 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003825}
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827/*
3828 * ":colder [count]": Up in the quickfix stack.
3829 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003830 * ":lolder [count]": Up in the location list stack.
3831 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 */
3833 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003834qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003836 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 int count;
3838
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003839 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3840 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (eap->addr_count != 0)
3843 count = eap->line2;
3844 else
3845 count = 1;
3846 while (count--)
3847 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003848 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003852 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003855 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857 else
3858 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003859 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003861 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003864 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003867 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003868 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869}
3870
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003871/*
3872 * Display the information about all the quickfix/location lists in the stack
3873 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003874 void
3875qf_history(exarg_T *eap)
3876{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003877 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003878 int i;
3879
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003880 if (eap->addr_count > 0)
3881 {
3882 if (qi == NULL)
3883 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003884 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003885 return;
3886 }
3887
3888 // Jump to the specified quickfix list
3889 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3890 {
3891 qi->qf_curlist = eap->line2 - 1;
3892 qf_msg(qi, qi->qf_curlist, "");
3893 qf_update_buffer(qi, NULL);
3894 }
3895 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003896 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003897
3898 return;
3899 }
3900
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003901 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003902 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003903 else
3904 for (i = 0; i < qi->qf_listcount; ++i)
3905 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3906}
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003909 * Free all the entries in the error list "idx". Note that other information
3910 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 */
3912 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003913qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003915 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003916 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003917 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003919 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003921 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003922 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003923 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003924 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003925 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003926 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003927 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003928 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003929 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003930 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003931 // Somehow qf_count may have an incorrect value, set it to 1
3932 // to avoid crashing when it's wrong.
3933 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003934 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003935 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003936 qfl->qf_start = qfpnext;
3937 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003939
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003940 qfl->qf_index = 0;
3941 qfl->qf_start = NULL;
3942 qfl->qf_last = NULL;
3943 qfl->qf_ptr = NULL;
3944 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003945
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003946 qf_clean_dir_stack(&qfl->qf_dir_stack);
3947 qfl->qf_directory = NULL;
3948 qf_clean_dir_stack(&qfl->qf_file_stack);
3949 qfl->qf_currfile = NULL;
3950 qfl->qf_multiline = FALSE;
3951 qfl->qf_multiignore = FALSE;
3952 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954
3955/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003956 * Free error list "idx". Frees all the entries in the quickfix list,
3957 * associated context information and the title.
3958 */
3959 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003960qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003961{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003962 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003963
Bram Moolenaard23a8232018-02-10 18:45:26 +01003964 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003965 free_tv(qfl->qf_ctx);
3966 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01003967 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003968 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003969 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003970}
3971
3972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 * qf_mark_adjust: adjust marks
3974 */
3975 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003976qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003977 win_T *wp,
3978 linenr_T line1,
3979 linenr_T line2,
3980 long amount,
3981 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003983 int i;
3984 qfline_T *qfp;
3985 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003986 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003987 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003988 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
Bram Moolenaarc1542742016-07-20 21:44:37 +02003990 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003991 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003992 if (wp != NULL)
3993 {
3994 if (wp->w_llist == NULL)
3995 return;
3996 qi = wp->w_llist;
3997 }
3998
3999 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004000 {
4001 qf_list_T *qfl = qf_get_list(qi, idx);
4002
4003 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004004 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (qfp->qf_fnum == curbuf->b_fnum)
4006 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004007 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4009 {
4010 if (amount == MAXLNUM)
4011 qfp->qf_cleared = TRUE;
4012 else
4013 qfp->qf_lnum += amount;
4014 }
4015 else if (amount_after && qfp->qf_lnum > line2)
4016 qfp->qf_lnum += amount_after;
4017 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004018 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004019
4020 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004021 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022}
4023
4024/*
4025 * Make a nice message out of the error character and the error number:
4026 * char number message
4027 * e or E 0 " error"
4028 * w or W 0 " warning"
4029 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004030 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 * 0 0 ""
4032 * other 0 " c"
4033 * e or E n " error n"
4034 * w or W n " warning n"
4035 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004036 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 * 0 n " error n"
4038 * other n " c n"
4039 * 1 x "" :helpgrep
4040 */
4041 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004042qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
4044 static char_u buf[20];
4045 static char_u cc[3];
4046 char_u *p;
4047
4048 if (c == 'W' || c == 'w')
4049 p = (char_u *)" warning";
4050 else if (c == 'I' || c == 'i')
4051 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004052 else if (c == 'N' || c == 'n')
4053 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4055 p = (char_u *)" error";
4056 else if (c == 0 || c == 1)
4057 p = (char_u *)"";
4058 else
4059 {
4060 cc[0] = ' ';
4061 cc[1] = c;
4062 cc[2] = NUL;
4063 p = cc;
4064 }
4065
4066 if (nr <= 0)
4067 return p;
4068
4069 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4070 return buf;
4071}
4072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004074 * When "split" is FALSE: Open the entry/result under the cursor.
4075 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4076 */
4077 void
4078qf_view_result(int split)
4079{
4080 qf_info_T *qi = &ql_info;
4081
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004082 if (IS_LL_WINDOW(curwin))
4083 qi = GET_LOC_LIST(curwin);
4084
Bram Moolenaar0398e002019-03-21 21:12:49 +01004085 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004086 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004087 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004088 return;
4089 }
4090
4091 if (split)
4092 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004093 // Open the selected entry in a new window
4094 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4095 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004096 return;
4097 }
4098
4099 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4100}
4101
4102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 * ":cwindow": open the quickfix window if we have errors to display,
4104 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004105 * ":lwindow": open the location list window if we have locations to display,
4106 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 */
4108 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004109ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004111 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004112 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 win_T *win;
4114
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004115 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4116 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004117
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004118 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004119
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004120 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004121 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004123 // If a quickfix window is open but we have no errors to display,
4124 // close the window. If a quickfix window is not open, then open
4125 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004126 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004127 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004128 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 if (win != NULL)
4131 ex_cclose(eap);
4132 }
4133 else if (win == NULL)
4134 ex_copen(eap);
4135}
4136
4137/*
4138 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004139 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004142ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004144 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004145 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004147 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4148 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004150 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004151 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (win != NULL)
4153 win_close(win, FALSE);
4154}
4155
4156/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004157 * Set "w:quickfix_title" if "qi" has a title.
4158 */
4159 static void
4160qf_set_title_var(qf_list_T *qfl)
4161{
4162 if (qfl->qf_title != NULL)
4163 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4164}
4165
4166/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004167 * Goto a quickfix or location list window (if present).
4168 * Returns OK if the window is found, FAIL otherwise.
4169 */
4170 static int
4171qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4172{
4173 win_T *win;
4174
4175 win = qf_find_win(qi);
4176 if (win == NULL)
4177 return FAIL;
4178
4179 win_goto(win);
4180 if (resize)
4181 {
4182 if (vertsplit)
4183 {
4184 if (sz != win->w_width)
4185 win_setwidth(sz);
4186 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004187 else if (sz != win->w_height && win->w_height
4188 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004189 win_setheight(sz);
4190 }
4191
4192 return OK;
4193}
4194
4195/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004196 * Set options for the buffer in the quickfix or location list window.
4197 */
4198 static void
4199qf_set_cwindow_options(void)
4200{
4201 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004202 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4203 set_option_value_give_err((char_u *)"bt",
4204 0L, (char_u *)"quickfix", OPT_LOCAL);
4205 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004206 RESET_BINDING(curwin);
4207#ifdef FEAT_DIFF
4208 curwin->w_p_diff = FALSE;
4209#endif
4210#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004211 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004212 OPT_LOCAL);
4213#endif
4214}
4215
4216/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004217 * Open a new quickfix or location list window, load the quickfix buffer and
4218 * set the appropriate options for the window.
4219 * Returns FAIL if the window could not be opened.
4220 */
4221 static int
4222qf_open_new_cwindow(qf_info_T *qi, int height)
4223{
4224 buf_T *qf_buf;
4225 win_T *oldwin = curwin;
4226 tabpage_T *prevtab = curtab;
4227 int flags = 0;
4228 win_T *win;
4229
4230 qf_buf = qf_find_buf(qi);
4231
4232 // The current window becomes the previous window afterwards.
4233 win = curwin;
4234
Bram Moolenaare1004402020-10-24 20:49:43 +02004235 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004236 // Create the new quickfix window at the very bottom, except when
4237 // :belowright or :aboveleft is used.
4238 win_goto(lastwin);
4239 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004240 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004241 flags = WSP_BELOW;
4242 flags |= WSP_NEWLOC;
4243 if (win_split(height, flags) == FAIL)
4244 return FAIL; // not enough room for window
4245 RESET_BINDING(curwin);
4246
4247 if (IS_LL_STACK(qi))
4248 {
4249 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004250 // location list stack from the window 'win'.
4251 curwin->w_llist_ref = qi;
4252 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004253 }
4254
4255 if (oldwin != curwin)
4256 oldwin = NULL; // don't store info when in another window
4257 if (qf_buf != NULL)
4258 {
4259 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004260 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004261 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004262 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004263 }
4264 else
4265 {
4266 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004267 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4268 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004269 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004270
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004271 // save the number of the new buffer
4272 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004273 }
4274
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004275 // Set the options for the quickfix buffer/window (if not already done)
4276 // Do this even if the quickfix buffer was already present, as an autocmd
4277 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004278 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004279 qf_set_cwindow_options();
4280
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004281 // Only set the height when still in the same tab page and there is no
4282 // window to the side.
4283 if (curtab == prevtab && curwin->w_width == Columns)
4284 win_setheight(height);
4285 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4286 if (win_valid(win))
4287 prevwin = win;
4288
4289 return OK;
4290}
4291
4292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004294 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 */
4296 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004297ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004299 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004300 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004302 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004303 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004305 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4306 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004307
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004308 incr_quickfix_busy();
4309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 if (eap->addr_count != 0)
4311 height = eap->line2;
4312 else
4313 height = QF_WINHEIGHT;
4314
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004315 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316#ifdef FEAT_GUI
4317 need_mouse_correct = TRUE;
4318#endif
4319
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004320 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004321 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004322 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004323 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004324 if (status == FAIL)
4325 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004326 {
4327 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004328 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004331 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004332 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004333 // Save the current index here, as updating the quickfix buffer may free
4334 // the quickfix list
4335 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004336
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004337 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004338 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004340 decr_quickfix_busy();
4341
4342 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 curwin->w_cursor.col = 0;
4344 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004345 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346}
4347
4348/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004349 * Move the cursor in the quickfix window to "lnum".
4350 */
4351 static void
4352qf_win_goto(win_T *win, linenr_T lnum)
4353{
4354 win_T *old_curwin = curwin;
4355
4356 curwin = win;
4357 curbuf = win->w_buffer;
4358 curwin->w_cursor.lnum = lnum;
4359 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004360 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004361 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004362 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004363 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004364 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004365 curwin = old_curwin;
4366 curbuf = curwin->w_buffer;
4367}
4368
4369/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004370 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004371 */
4372 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004373ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004374{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004375 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004376 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004377
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004378 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4379 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004380
4381 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004382 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4383 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4384}
4385
4386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 * Return the number of the current entry (line number in the quickfix
4388 * window).
4389 */
4390 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004391qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004393 qf_info_T *qi = &ql_info;
4394
4395 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004396 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004397 qi = wp->w_llist_ref;
4398
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004399 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400}
4401
4402/*
4403 * Update the cursor position in the quickfix window to the current error.
4404 * Return TRUE if there is a quickfix window.
4405 */
4406 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004407qf_win_pos_update(
4408 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004409 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004412 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004414 // Put the cursor on the current error in the quickfix window, so that
4415 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004416 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 if (win != NULL
4418 && qf_index <= win->w_buffer->b_ml.ml_line_count
4419 && old_qf_index != qf_index)
4420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 if (qf_index > old_qf_index)
4422 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004423 win->w_redraw_top = old_qf_index;
4424 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 else
4427 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004428 win->w_redraw_top = qf_index;
4429 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004431 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433 return win != NULL;
4434}
4435
4436/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004437 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004438 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004439 */
4440 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004441is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004442{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004443 // A window displaying the quickfix buffer will have the w_llist_ref field
4444 // set to NULL.
4445 // A window displaying a location list buffer will have the w_llist_ref
4446 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004447 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004448 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4449 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004450 return TRUE;
4451
4452 return FALSE;
4453}
4454
4455/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004456 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4457 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004458 */
4459 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004460qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004461{
4462 win_T *win;
4463
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004464 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004465 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004466 return win;
4467 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004468}
4469
4470/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004471 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004472 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 */
4474 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004475qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004477 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004478 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004480 if (qi->qf_bufnr != INVALID_QFBUFNR)
4481 {
4482 buf_T *qfbuf;
4483 qfbuf = buflist_findnr(qi->qf_bufnr);
4484 if (qfbuf != NULL)
4485 return qfbuf;
4486 // buffer is no longer present
4487 qi->qf_bufnr = INVALID_QFBUFNR;
4488 }
4489
Bram Moolenaar9c102382006-05-03 21:26:49 +00004490 FOR_ALL_TAB_WINDOWS(tp, win)
4491 if (is_qf_win(win, qi))
4492 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004493
Bram Moolenaar9c102382006-05-03 21:26:49 +00004494 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495}
4496
4497/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004498 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004499 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004500 */
4501 int
4502qf_process_qftf_option(void)
4503{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004504 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004505}
4506
4507/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004508 * Update the w:quickfix_title variable in the quickfix/location list window in
4509 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004510 */
4511 static void
4512qf_update_win_titlevar(qf_info_T *qi)
4513{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004514 qf_list_T *qfl = qf_get_curlist(qi);
4515 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004516 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004517 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004518
Bram Moolenaar530bed92020-12-16 21:02:56 +01004519 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004520 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004521 if (is_qf_win(win, qi))
4522 {
4523 curwin = win;
4524 qf_set_title_var(qfl);
4525 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004526 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004527 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004528}
4529
4530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 * Find the quickfix buffer. If it exists, update the contents.
4532 */
4533 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004534qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535{
4536 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004537 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004540 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004541 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (buf != NULL)
4543 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004544 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004545 int qf_winid = 0;
4546
4547 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004548 {
4549 if (curwin->w_llist == qi)
4550 win = curwin;
4551 else
4552 {
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004553 // Find the file window (non-quickfix) with this location list
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004554 win = qf_find_win_with_loclist(qi);
4555 if (win == NULL)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004556 // File window is not found. Find the location list window.
4557 win = qf_find_win(qi);
4558 if (win == NULL)
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004559 return;
4560 }
4561 qf_winid = win->w_id;
4562 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004563
4564 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004565 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004566 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaard823fa92016-08-12 16:29:27 +02004568 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004569
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004570 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004571 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004572
Bram Moolenaar864293a2016-06-02 13:40:04 +02004573 if (old_last == NULL)
4574 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004575 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004576
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004577 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004578 aucmd_restbuf(&aco);
4579 }
4580
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004581 // Only redraw when added lines are visible. This avoids flickering
4582 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004583 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004584 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586}
4587
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004588/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004589 * Add an error line to the quickfix buffer.
4590 */
4591 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004592qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004593 buf_T *buf, // quickfix window buffer
4594 linenr_T lnum,
4595 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004596 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004597 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004598 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004599{
4600 int len;
4601 buf_T *errbuf;
4602
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004603 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004604 // for this entry, then use it.
4605 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004606 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004607 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004608 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004609 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004610 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004611 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4612 len = (int)STRLEN(IObuff);
4613 }
4614 else if (qfp->qf_fnum != 0
4615 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4616 && errbuf->b_fname != NULL)
4617 {
4618 if (qfp->qf_type == 1) // :helpgrep
4619 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4620 else
4621 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004622 // Shorten the file name if not done already.
4623 // For optimization, do this only for the first entry in a
4624 // buffer.
4625 if (first_bufline && (errbuf->b_sfname == NULL
4626 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004627 {
4628 if (*dirname == NUL)
4629 mch_dirname(dirname, MAXPATHL);
4630 shorten_buf_fname(errbuf, dirname, FALSE);
4631 }
4632 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4633 }
4634 len = (int)STRLEN(IObuff);
4635 }
4636 else
4637 len = 0;
4638
4639 if (len < IOSIZE - 1)
4640 IObuff[len++] = '|';
4641
4642 if (qfp->qf_lnum > 0)
4643 {
thinca6864efa2021-06-19 20:45:20 +02004644 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004645 len += (int)STRLEN(IObuff + len);
4646
Bram Moolenaar858ba062020-05-31 23:11:59 +02004647 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4648 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004649 len += (int)STRLEN(IObuff + len);
4650 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004651 else if (qfp->qf_pattern != NULL)
4652 {
4653 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4654 len += (int)STRLEN(IObuff + len);
4655 }
4656 if (len < IOSIZE - 2)
4657 {
4658 IObuff[len++] = '|';
4659 IObuff[len++] = ' ';
4660 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004661
Bram Moolenaar858ba062020-05-31 23:11:59 +02004662 // Remove newlines and leading whitespace from the text.
4663 // For an unrecognized line keep the indent, the compiler may
4664 // mark a word with ^^^^.
4665 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4666 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004667 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004668
4669 if (ml_append_buf(buf, lnum, IObuff,
4670 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4671 return FAIL;
4672
4673 return OK;
4674}
4675
Bram Moolenaard43906d2020-07-20 21:31:32 +02004676/*
4677 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4678 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4679 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004680 static list_T *
4681call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4682{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004683 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004684 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004685 static int recursive = FALSE;
4686
4687 if (recursive)
4688 return NULL; // this doesn't work properly recursively
4689 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004690
4691 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4692 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4693 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004694 if (qfl->qf_qftf_cb.cb_name != NULL)
4695 cb = &qfl->qf_qftf_cb;
4696 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004697 {
4698 typval_T args[1];
4699 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004700 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004701
4702 // create the dict argument
4703 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01004704 {
4705 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004706 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004707 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004708 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4709 dict_add_number(d, "winid", (long)qf_winid);
4710 dict_add_number(d, "id", (long)qfl->qf_id);
4711 dict_add_number(d, "start_idx", start_idx);
4712 dict_add_number(d, "end_idx", end_idx);
4713 ++d->dv_refcount;
4714 args[0].v_type = VAR_DICT;
4715 args[0].vval.v_dict = d;
4716
Bram Moolenaard43906d2020-07-20 21:31:32 +02004717 qftf_list = NULL;
4718 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4719 {
4720 if (rettv.v_type == VAR_LIST)
4721 {
4722 qftf_list = rettv.vval.v_list;
4723 qftf_list->lv_refcount++;
4724 }
4725 clear_tv(&rettv);
4726 }
4727 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004728 }
4729
Bram Moolenaard6c67622022-08-24 20:07:22 +01004730 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004731 return qftf_list;
4732}
4733
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 * Fill current buffer with quickfix errors, replacing any previous contents.
4736 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004737 * If "old_last" is not NULL append the items after this one.
4738 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4739 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 */
4741 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004742qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004744 linenr_T lnum;
4745 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004746 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004747 list_T *qftf_list = NULL;
4748 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaar864293a2016-06-02 13:40:04 +02004750 if (old_last == NULL)
4751 {
4752 if (buf != curbuf)
4753 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004754 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004755 return;
4756 }
4757
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004758 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004759 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004760 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004763 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004764 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004766 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004767 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004768 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004769
4770 *dirname = NUL;
4771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004772 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004773 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004774 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004775 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004776 lnum = 0;
4777 }
4778 else
4779 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004780 if (old_last->qf_next != NULL)
4781 qfp = old_last->qf_next;
4782 else
4783 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004784 lnum = buf->b_ml.ml_line_count;
4785 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004786
4787 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4788 (long)qfl->qf_count);
4789 if (qftf_list != NULL)
4790 qftf_li = qftf_list->lv_first;
4791
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004792 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004794 char_u *qftf_str = NULL;
4795
Bram Moolenaard43906d2020-07-20 21:31:32 +02004796 // Use the text supplied by the user defined function (if any).
4797 // If the returned value is not string, then ignore the rest
4798 // of the returned values and use the default.
4799 if (qftf_li != NULL && !invalid_val)
4800 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004801 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004802 if (qftf_str == NULL)
4803 invalid_val = TRUE;
4804 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004805
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004806 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4807 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004809
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004810 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004811 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004813 if (qfp == NULL)
4814 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004815
4816 if (qftf_li != NULL)
4817 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004819
4820 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004821 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004822 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
4824
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004825 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 check_lnums(TRUE);
4827
Bram Moolenaar864293a2016-06-02 13:40:04 +02004828 if (old_last == NULL)
4829 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004830 // Set the 'filetype' to "qf" each time after filling the buffer.
4831 // This resembles reading a file into a buffer, it's more logical when
4832 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004833 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004834 set_option_value_give_err((char_u *)"ft",
4835 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004836 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004838 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004839 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004841 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004843 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004844 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004845
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004846 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004847 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004850 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 KeyTyped = old_KeyTyped;
4852}
4853
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004854/*
4855 * For every change made to the quickfix list, update the changed tick.
4856 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004857 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004858qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004859{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004860 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004861}
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004864 * Return the quickfix/location list number with the given identifier.
4865 * Returns -1 if list is not found.
4866 */
4867 static int
4868qf_id2nr(qf_info_T *qi, int_u qfid)
4869{
4870 int qf_idx;
4871
4872 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4873 if (qi->qf_lists[qf_idx].qf_id == qfid)
4874 return qf_idx;
4875 return INVALID_QFIDX;
4876}
4877
4878/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004879 * If the current list is not "save_qfid" and we can find the list with that ID
4880 * then make it the current list.
4881 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004882 * Returns OK if successfully restored the list. Returns FAIL if the list with
4883 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004884 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004885 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004886qf_restore_list(qf_info_T *qi, int_u save_qfid)
4887{
4888 int curlist;
4889
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004890 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004891 {
4892 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004893 if (curlist < 0)
4894 // list is not present
4895 return FAIL;
4896 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004897 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004898 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004899}
4900
4901/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004902 * Jump to the first entry if there is one.
4903 */
4904 static void
4905qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4906{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004907 if (qf_restore_list(qi, save_qfid) == FAIL)
4908 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004909
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004910 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004911 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004912 qf_jump(qi, 0, 0, forceit);
4913}
4914
4915/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004916 * Return TRUE when using ":vimgrep" for ":grep".
4917 */
4918 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004919grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004920{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004921 return ((cmdidx == CMD_grep
4922 || cmdidx == CMD_lgrep
4923 || cmdidx == CMD_grepadd
4924 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004925 && STRCMP("internal",
4926 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4927}
4928
4929/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004930 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004932 static char_u *
4933make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004935 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004936 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004937 case CMD_make: return (char_u *)"make";
4938 case CMD_lmake: return (char_u *)"lmake";
4939 case CMD_grep: return (char_u *)"grep";
4940 case CMD_lgrep: return (char_u *)"lgrep";
4941 case CMD_grepadd: return (char_u *)"grepadd";
4942 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4943 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945}
4946
4947/*
4948 * Return the name for the errorfile, in allocated memory.
4949 * Find a new unique name when 'makeef' contains "##".
4950 * Returns NULL for error.
4951 */
4952 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004953get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 char_u *p;
4956 char_u *name;
4957 static int start = -1;
4958 static int off = 0;
4959#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004960 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#endif
4962
4963 if (*p_mef == NUL)
4964 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004965 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004967 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 return name;
4969 }
4970
4971 for (p = p_mef; *p; ++p)
4972 if (p[0] == '#' && p[1] == '#')
4973 break;
4974
4975 if (*p == NUL)
4976 return vim_strsave(p_mef);
4977
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004978 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 for (;;)
4980 {
4981 if (start == -1)
4982 start = mch_get_pid();
4983 else
4984 off += 19;
4985
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004986 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (name == NULL)
4988 break;
4989 STRCPY(name, p_mef);
4990 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4991 STRCAT(name, p + 2);
4992 if (mch_getperm(name) < 0
4993#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004994 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 && mch_lstat((char *)name, &sb) < 0
4996#endif
4997 )
4998 break;
4999 vim_free(name);
5000 }
5001 return name;
5002}
5003
5004/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005005 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5006 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5007 */
5008 static char_u *
5009make_get_fullcmd(char_u *makecmd, char_u *fname)
5010{
5011 char_u *cmd;
5012 unsigned len;
5013
5014 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5015 if (*p_sp != NUL)
5016 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005017 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005018 if (cmd == NULL)
5019 return NULL;
5020 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5021 (char *)p_shq);
5022
5023 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5024 if (*p_sp != NUL)
5025 append_redir(cmd, len, p_sp, fname);
5026
5027 // Display the fully formed command. Output a newline if there's something
5028 // else than the :make command that was typed (in which case the cursor is
5029 // in column 0).
5030 if (msg_col == 0)
5031 msg_didout = FALSE;
5032 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005033 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005034 msg_outtrans(cmd); // show what we are doing
5035
5036 return cmd;
5037}
5038
5039/*
5040 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5041 */
5042 void
5043ex_make(exarg_T *eap)
5044{
5045 char_u *fname;
5046 char_u *cmd;
5047 char_u *enc = NULL;
5048 win_T *wp = NULL;
5049 qf_info_T *qi = &ql_info;
5050 int res;
5051 char_u *au_name = NULL;
5052 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005053 char_u *errorformat = p_efm;
5054 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005055
5056 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5057 if (grep_internal(eap->cmdidx))
5058 {
5059 ex_vimgrep(eap);
5060 return;
5061 }
5062
5063 au_name = make_get_auname(eap->cmdidx);
5064 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5065 curbuf->b_fname, TRUE, curbuf))
5066 {
5067#ifdef FEAT_EVAL
5068 if (aborting())
5069 return;
5070#endif
5071 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005072 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005073
5074 if (is_loclist_cmd(eap->cmdidx))
5075 wp = curwin;
5076
5077 autowrite_all();
5078 fname = get_mef_name();
5079 if (fname == NULL)
5080 return;
5081 mch_remove(fname); // in case it's not unique
5082
5083 cmd = make_get_fullcmd(eap->arg, fname);
5084 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005085 {
5086 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005087 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005088 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005089
5090 // let the shell know if we are redirecting output or not
5091 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5092
5093#ifdef AMIGA
5094 out_flush();
5095 // read window status report and redraw before message
5096 (void)char_avail();
5097#endif
5098
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005099 incr_quickfix_busy();
5100
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005101 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5102 errorformat = p_gefm;
5103 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5104 newlist = FALSE;
5105
5106 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5107 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005108 if (wp != NULL)
5109 {
5110 qi = GET_LOC_LIST(wp);
5111 if (qi == NULL)
5112 goto cleanup;
5113 }
5114 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005115 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005116
5117 // Remember the current quickfix list identifier, so that we can
5118 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005119 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005120 if (au_name != NULL)
5121 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5122 curbuf->b_fname, TRUE, curbuf);
5123 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5124 // display the first error
5125 qf_jump_first(qi, save_qfid, FALSE);
5126
5127cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005128 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005129 mch_remove(fname);
5130 vim_free(fname);
5131 vim_free(cmd);
5132}
5133
5134/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005135 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005136 */
5137 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005138qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005139{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005140 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005141
5142 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5143 return 0;
5144 return qf_get_curlist(qi)->qf_count;
5145}
5146
5147/*
5148 * Returns the number of valid entries in the current quickfix/location list.
5149 */
5150 int
5151qf_get_valid_size(exarg_T *eap)
5152{
5153 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005154 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005155 qfline_T *qfp;
5156 int i, sz = 0;
5157 int prev_fnum = 0;
5158
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005159 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5160 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005161
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005162 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005163 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005164 {
5165 if (qfp->qf_valid)
5166 {
5167 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005168 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005169 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5170 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005171 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005172 sz++;
5173 prev_fnum = qfp->qf_fnum;
5174 }
5175 }
5176 }
5177
5178 return sz;
5179}
5180
5181/*
5182 * Returns the current index of the quickfix/location list.
5183 * Returns 0 if there is an error.
5184 */
5185 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005186qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005187{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005188 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005189
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005190 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5191 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005192
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005193 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005194}
5195
5196/*
5197 * Returns the current index in the quickfix/location list (counting only valid
5198 * entries). If no valid entries are in the list, then returns 1.
5199 */
5200 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005201qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005202{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005203 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005204 qf_list_T *qfl;
5205 qfline_T *qfp;
5206 int i, eidx = 0;
5207 int prev_fnum = 0;
5208
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005209 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5210 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005211
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005212 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005213 qfp = qfl->qf_start;
5214
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005215 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005216 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005217 return 1;
5218
5219 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5220 {
5221 if (qfp->qf_valid)
5222 {
5223 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5224 {
5225 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005227 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005228 eidx++;
5229 prev_fnum = qfp->qf_fnum;
5230 }
5231 }
5232 else
5233 eidx++;
5234 }
5235 }
5236
5237 return eidx ? eidx : 1;
5238}
5239
5240/*
5241 * Get the 'n'th valid error entry in the quickfix or location list.
5242 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5243 * For :cdo and :ldo returns the 'n'th valid error entry.
5244 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5245 */
5246 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005247qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005248{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005249 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005250 int i, eidx;
5251 int prev_fnum = 0;
5252
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005253 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005254 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005255 return 1;
5256
Bram Moolenaar95946f12019-03-31 15:31:59 +02005257 eidx = 0;
5258 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005259 {
5260 if (qfp->qf_valid)
5261 {
5262 if (fdo)
5263 {
5264 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5265 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005266 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005267 eidx++;
5268 prev_fnum = qfp->qf_fnum;
5269 }
5270 }
5271 else
5272 eidx++;
5273 }
5274
5275 if (eidx == n)
5276 break;
5277 }
5278
5279 if (i <= qfl->qf_count)
5280 return i;
5281 else
5282 return 1;
5283}
5284
5285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005287 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005288 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 */
5290 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005291ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005293 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005294 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005295
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005296 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5297 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005298
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005299 if (eap->addr_count > 0)
5300 errornr = (int)eap->line2;
5301 else
5302 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005303 switch (eap->cmdidx)
5304 {
5305 case CMD_cc: case CMD_ll:
5306 errornr = 0;
5307 break;
5308 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5309 case CMD_lfirst:
5310 errornr = 1;
5311 break;
5312 default:
5313 errornr = 32767;
5314 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005315 }
5316
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005317 // For cdo and ldo commands, jump to the nth valid error.
5318 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005319 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5320 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005321 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005322 eap->addr_count > 0 ? (int)eap->line1 : 1,
5323 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5324
5325 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326}
5327
5328/*
5329 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005330 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005331 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 */
5333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005334ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005336 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005337 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005338 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005339
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005340 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5341 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005342
Bram Moolenaar55b69262017-08-13 13:42:01 +02005343 if (eap->addr_count > 0
5344 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5345 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005346 errornr = (int)eap->line2;
5347 else
5348 errornr = 1;
5349
Bram Moolenaar39665952018-08-15 20:59:48 +02005350 // Depending on the command jump to either next or previous entry/file.
5351 switch (eap->cmdidx)
5352 {
5353 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5354 dir = FORWARD;
5355 break;
5356 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5357 case CMD_lNext:
5358 dir = BACKWARD;
5359 break;
5360 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5361 dir = FORWARD_FILE;
5362 break;
5363 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5364 dir = BACKWARD_FILE;
5365 break;
5366 default:
5367 dir = FORWARD;
5368 break;
5369 }
5370
5371 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372}
5373
5374/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005375 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5376 * The index of the entry is stored in 'errornr'.
5377 * Returns NULL if an entry is not found.
5378 */
5379 static qfline_T *
5380qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5381{
5382 qfline_T *qfp = NULL;
5383 int idx = 0;
5384
5385 // Find the first entry in this file
5386 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5387 if (qfp->qf_fnum == bnr)
5388 break;
5389
5390 *errornr = idx;
5391 return qfp;
5392}
5393
5394/*
5395 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5396 * with the error number for the first entry. Assumes the entries are sorted in
5397 * the quickfix list by line number.
5398 */
5399 static qfline_T *
5400qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5401{
5402 while (!got_int
5403 && entry->qf_prev != NULL
5404 && entry->qf_fnum == entry->qf_prev->qf_fnum
5405 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5406 {
5407 entry = entry->qf_prev;
5408 --*errornr;
5409 }
5410
5411 return entry;
5412}
5413
5414/*
5415 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5416 * with the error number for the last entry. Assumes the entries are sorted in
5417 * the quickfix list by line number.
5418 */
5419 static qfline_T *
5420qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5421{
5422 while (!got_int &&
5423 entry->qf_next != NULL
5424 && entry->qf_fnum == entry->qf_next->qf_fnum
5425 && entry->qf_lnum == entry->qf_next->qf_lnum)
5426 {
5427 entry = entry->qf_next;
5428 ++*errornr;
5429 }
5430
5431 return entry;
5432}
5433
5434/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005435 * Returns TRUE if the specified quickfix entry is
5436 * after the given line (linewise is TRUE)
5437 * or after the line and column.
5438 */
5439 static int
5440qf_entry_after_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 * Returns TRUE if the specified quickfix entry is
5451 * before the given line (linewise is TRUE)
5452 * or before the line and column.
5453 */
5454 static int
5455qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5456{
5457 if (linewise)
5458 return qfp->qf_lnum < pos->lnum;
5459 else
5460 return (qfp->qf_lnum < pos->lnum ||
5461 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5462}
5463
5464/*
5465 * Returns TRUE if the specified quickfix entry is
5466 * on or after the given line (linewise is TRUE)
5467 * or on or after the line and column.
5468 */
5469 static int
5470qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5471{
5472 if (linewise)
5473 return qfp->qf_lnum >= pos->lnum;
5474 else
5475 return (qfp->qf_lnum > pos->lnum ||
5476 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5477}
5478
5479/*
5480 * Returns TRUE if the specified quickfix entry is
5481 * on or before the given line (linewise is TRUE)
5482 * or on or before the line and column.
5483 */
5484 static int
5485qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5486{
5487 if (linewise)
5488 return qfp->qf_lnum <= pos->lnum;
5489 else
5490 return (qfp->qf_lnum < pos->lnum ||
5491 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5492}
5493
5494/*
5495 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5496 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5497 * multiple entries on a single line as one. Otherwise returns the entry after
5498 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005499 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5500 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005501 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005502 */
5503 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005504qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005505 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005506 pos_T *pos,
5507 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005508 qfline_T *qfp,
5509 int *errornr)
5510{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005511 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005512 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005513 return qfp;
5514
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005515 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005516 while (qfp->qf_next != NULL
5517 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005518 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005519 {
5520 qfp = qfp->qf_next;
5521 ++*errornr;
5522 }
5523
5524 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005525 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005526 return NULL;
5527
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005528 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005529 qfp = qfp->qf_next;
5530 ++*errornr;
5531
5532 return qfp;
5533}
5534
5535/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005536 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5537 * If 'linewise' is TRUE, returns the entry before the specified line and
5538 * treats multiple entries on a single line as one. Otherwise returns the entry
5539 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005540 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5541 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005542 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005543 */
5544 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005545qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005546 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005547 pos_T *pos,
5548 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005549 qfline_T *qfp,
5550 int *errornr)
5551{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005552 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005553 while (qfp->qf_next != NULL
5554 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005555 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005556 {
5557 qfp = qfp->qf_next;
5558 ++*errornr;
5559 }
5560
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005561 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005562 return NULL;
5563
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005564 if (linewise)
5565 // If multiple entries are on the same line, then use the first entry
5566 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005567
5568 return qfp;
5569}
5570
5571/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005572 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005573 * the direction 'dir'.
5574 */
5575 static qfline_T *
5576qf_find_closest_entry(
5577 qf_list_T *qfl,
5578 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005579 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005580 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005581 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005582 int *errornr)
5583{
5584 qfline_T *qfp;
5585
5586 *errornr = 0;
5587
5588 // Find the first entry in this file
5589 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5590 if (qfp == NULL)
5591 return NULL; // no entry in this file
5592
5593 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005594 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005595 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005596 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005597
5598 return qfp;
5599}
5600
5601/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005602 * Get the nth quickfix entry below the specified entry. Searches forward in
5603 * the list. If linewise is TRUE, then treat multiple entries on a single line
5604 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005605 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005606 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005607qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005608{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005609 qfline_T *entry = entry_arg;
5610
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005611 while (n-- > 0 && !got_int)
5612 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005613 int first_errornr = *errornr;
5614
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005615 if (linewise)
5616 // Treat all the entries on the same line in this file as one
5617 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005618
5619 if (entry->qf_next == NULL
5620 || entry->qf_next->qf_fnum != entry->qf_fnum)
5621 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005622 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005623 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005624 break;
5625 }
5626
5627 entry = entry->qf_next;
5628 ++*errornr;
5629 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005630}
5631
5632/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005633 * Get the nth quickfix entry above the specified entry. Searches backwards in
5634 * the list. If linewise is TRUE, then treat multiple entries on a single line
5635 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005636 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005637 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005638qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005639{
5640 while (n-- > 0 && !got_int)
5641 {
5642 if (entry->qf_prev == NULL
5643 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5644 break;
5645
5646 entry = entry->qf_prev;
5647 --*errornr;
5648
5649 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005650 if (linewise)
5651 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005652 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005653}
5654
5655/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005656 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5657 * the specified direction. Returns the error number in the quickfix list or 0
5658 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005659 */
5660 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005661qf_find_nth_adj_entry(
5662 qf_list_T *qfl,
5663 int bnr,
5664 pos_T *pos,
5665 int n,
5666 int dir,
5667 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005668{
5669 qfline_T *adj_entry;
5670 int errornr;
5671
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005672 // Find an entry closest to the specified position
5673 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005674 if (adj_entry == NULL)
5675 return 0;
5676
5677 if (--n > 0)
5678 {
5679 // Go to the n'th entry in the current buffer
5680 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005681 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005682 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005683 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005684 }
5685
5686 return errornr;
5687}
5688
5689/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005690 * Jump to a quickfix entry in the current file nearest to the current line or
5691 * current line/col.
5692 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5693 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005694 */
5695 void
5696ex_cbelow(exarg_T *eap)
5697{
5698 qf_info_T *qi;
5699 qf_list_T *qfl;
5700 int dir;
5701 int buf_has_flag;
5702 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005703 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005704
5705 if (eap->addr_count > 0 && eap->line2 <= 0)
5706 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005707 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005708 return;
5709 }
5710
5711 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005712 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5713 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005714 buf_has_flag = BUF_HAS_QF_ENTRY;
5715 else
5716 buf_has_flag = BUF_HAS_LL_ENTRY;
5717 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5718 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005719 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005720 return;
5721 }
5722
5723 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5724 return;
5725
5726 qfl = qf_get_curlist(qi);
5727 // check if the list has valid errors
5728 if (!qf_list_has_valid_entries(qfl))
5729 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005730 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005731 return;
5732 }
5733
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005734 if (eap->cmdidx == CMD_cbelow
5735 || eap->cmdidx == CMD_lbelow
5736 || eap->cmdidx == CMD_cafter
5737 || eap->cmdidx == CMD_lafter)
5738 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005739 dir = FORWARD;
5740 else
5741 dir = BACKWARD;
5742
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005743 pos = curwin->w_cursor;
5744 // A quickfix entry column number is 1 based whereas cursor column
5745 // number is 0 based. Adjust the column number.
5746 pos.col++;
5747 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5748 eap->addr_count > 0 ? eap->line2 : 0, dir,
5749 eap->cmdidx == CMD_cbelow
5750 || eap->cmdidx == CMD_lbelow
5751 || eap->cmdidx == CMD_cabove
5752 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005753
5754 if (errornr > 0)
5755 qf_jump(qi, 0, errornr, FALSE);
5756 else
5757 emsg(_(e_no_more_items));
5758}
5759
5760/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005761 * Return the autocmd name for the :cfile Ex commands
5762 */
5763 static char_u *
5764cfile_get_auname(cmdidx_T cmdidx)
5765{
5766 switch (cmdidx)
5767 {
5768 case CMD_cfile: return (char_u *)"cfile";
5769 case CMD_cgetfile: return (char_u *)"cgetfile";
5770 case CMD_caddfile: return (char_u *)"caddfile";
5771 case CMD_lfile: return (char_u *)"lfile";
5772 case CMD_lgetfile: return (char_u *)"lgetfile";
5773 case CMD_laddfile: return (char_u *)"laddfile";
5774 default: return NULL;
5775 }
5776}
5777
5778/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005779 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005780 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 */
5782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005783ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005785 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005786 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005787 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005788 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005789 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005790 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005791
Bram Moolenaara16123a2019-03-28 20:31:07 +01005792 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005793 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5794 NULL, FALSE, curbuf))
5795 {
5796#ifdef FEAT_EVAL
5797 if (aborting())
5798 return;
5799#endif
5800 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005801
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005802 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005803#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005804 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005805 {
5806 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005807 NULL, NULL,
5808 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005809 if (browse_file == NULL)
5810 return;
5811 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5812 vim_free(browse_file);
5813 }
5814 else
5815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005817 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005818
Bram Moolenaar39665952018-08-15 20:59:48 +02005819 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005820 wp = curwin;
5821
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005822 incr_quickfix_busy();
5823
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005824 // This function is used by the :cfile, :cgetfile and :caddfile
5825 // commands.
5826 // :cfile always creates a new quickfix list and jumps to the
5827 // first error.
5828 // :cgetfile creates a new quickfix list but doesn't jump to the
5829 // first error.
5830 // :caddfile adds to an existing quickfix list. If there is no
5831 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005832 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005833 && eap->cmdidx != CMD_laddfile),
5834 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005835 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005836 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005837 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005838 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005839 {
5840 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005841 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005842 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005843 }
5844 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005845 qf_list_changed(qf_get_curlist(qi));
5846 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005847 if (au_name != NULL)
5848 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005849
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005850 // Jump to the first error for a new list and if autocmds didn't
5851 // free the list.
5852 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5853 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005854 // display the first error
5855 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005856
5857 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005858}
5859
5860/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005861 * Return the vimgrep autocmd name.
5862 */
5863 static char_u *
5864vgr_get_auname(cmdidx_T cmdidx)
5865{
5866 switch (cmdidx)
5867 {
5868 case CMD_vimgrep: return (char_u *)"vimgrep";
5869 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5870 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5871 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5872 case CMD_grep: return (char_u *)"grep";
5873 case CMD_lgrep: return (char_u *)"lgrep";
5874 case CMD_grepadd: return (char_u *)"grepadd";
5875 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5876 default: return NULL;
5877 }
5878}
5879
5880/*
5881 * Initialize the regmatch used by vimgrep for pattern "s".
5882 */
5883 static void
5884vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5885{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005886 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005887 regmatch->regprog = NULL;
5888
5889 if (s == NULL || *s == NUL)
5890 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005891 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005892 if (last_search_pat() == NULL)
5893 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005894 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005895 return;
5896 }
5897 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5898 }
5899 else
5900 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5901
5902 regmatch->rmm_ic = p_ic;
5903 regmatch->rmm_maxcol = 0;
5904}
5905
5906/*
5907 * Display a file name when vimgrep is running.
5908 */
5909 static void
5910vgr_display_fname(char_u *fname)
5911{
5912 char_u *p;
5913
5914 msg_start();
5915 p = msg_strtrunc(fname, TRUE);
5916 if (p == NULL)
5917 msg_outtrans(fname);
5918 else
5919 {
5920 msg_outtrans(p);
5921 vim_free(p);
5922 }
5923 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005924 msg_didout = FALSE; // overwrite this message
5925 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005926 msg_col = 0;
5927 out_flush();
5928}
5929
5930/*
5931 * Load a dummy buffer to search for a pattern using vimgrep.
5932 */
5933 static buf_T *
5934vgr_load_dummy_buf(
5935 char_u *fname,
5936 char_u *dirname_start,
5937 char_u *dirname_now)
5938{
5939 int save_mls;
5940#if defined(FEAT_SYN_HL)
5941 char_u *save_ei = NULL;
5942#endif
5943 buf_T *buf;
5944
5945#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005946 // Don't do Filetype autocommands to avoid loading syntax and
5947 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005948 save_ei = au_event_disable(",Filetype");
5949#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005950 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005951 save_mls = p_mls;
5952 p_mls = 0;
5953
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005954 // Load file into a buffer, so that 'fileencoding' is detected,
5955 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005956 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5957
5958 p_mls = save_mls;
5959#if defined(FEAT_SYN_HL)
5960 au_event_restore(save_ei);
5961#endif
5962
5963 return buf;
5964}
5965
5966/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005967 * Check whether a quickfix/location list is valid. Autocmds may remove or
5968 * change a quickfix list when vimgrep is running. If the list is not found,
5969 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005970 */
5971 static int
5972vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005973 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005974 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005975 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005976 char_u *title)
5977{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005978 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005979 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005980 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005981 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005982 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005983 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005984 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005985 return FALSE;
5986 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005987 else
5988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005989 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005990 qf_new_list(qi, title);
5991 return TRUE;
5992 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005993 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005994
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005995 if (qf_restore_list(qi, qfid) == FAIL)
5996 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005997
5998 return TRUE;
5999}
6000
6001/*
6002 * Search for a pattern in all the lines in a buffer and add the matching lines
6003 * to a quickfix list.
6004 */
6005 static int
6006vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006007 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006008 char_u *fname,
6009 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006010 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006011 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006012 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006013 int duplicate_name,
6014 int flags)
6015{
6016 int found_match = FALSE;
6017 long lnum;
6018 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006019 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006020
Bram Moolenaar1c299432018-10-28 14:36:09 +01006021 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006022 {
6023 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006024 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006025 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006026 // Regular expression match
6027 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Paul Ollis65745772022-06-05 16:55:54 +01006028 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006029 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006030 // Pass the buffer number so that it gets used even for a
6031 // dummy buffer, unless duplicate_name is set, then the
6032 // buffer will be wiped out below.
6033 if (qf_add_entry(qfl,
6034 NULL, // dir
6035 fname,
6036 NULL,
6037 duplicate_name ? 0 : buf->b_fnum,
6038 ml_get_buf(buf,
6039 regmatch->startpos[0].lnum + lnum, FALSE),
6040 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006041 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006042 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006043 regmatch->endpos[0].col + 1,
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 || regmatch->endpos[0].lnum > 0)
6059 break;
6060 col = regmatch->endpos[0].col
6061 + (col == regmatch->endpos[0].col);
6062 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6063 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006064 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006065 }
6066 else
6067 {
6068 char_u *str = ml_get_buf(buf, lnum, FALSE);
6069 int score;
6070 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006071 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006072
6073 // Fuzzy string match
6074 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6075 {
6076 // Pass the buffer number so that it gets used even for a
6077 // dummy buffer, unless duplicate_name is set, then the
6078 // buffer will be wiped out below.
6079 if (qf_add_entry(qfl,
6080 NULL, // dir
6081 fname,
6082 NULL,
6083 duplicate_name ? 0 : buf->b_fnum,
6084 str,
6085 lnum,
thinca6864efa2021-06-19 20:45:20 +02006086 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006087 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006088 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006089 FALSE, // vis_col
6090 NULL, // search pattern
6091 0, // nr
6092 0, // type
6093 TRUE // valid
6094 ) == QF_FAIL)
6095 {
6096 got_int = TRUE;
6097 break;
6098 }
6099 found_match = TRUE;
6100 if (--*tomatch == 0)
6101 break;
6102 if ((flags & VGR_GLOBAL) == 0)
6103 break;
6104 col = matches[pat_len - 1] + col + 1;
6105 if (col > (colnr_T)STRLEN(str))
6106 break;
6107 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006108 }
6109 line_breakcheck();
6110 if (got_int)
6111 break;
6112 }
6113
6114 return found_match;
6115}
6116
6117/*
6118 * Jump to the first match and update the directory.
6119 */
6120 static void
6121vgr_jump_to_match(
6122 qf_info_T *qi,
6123 int forceit,
6124 int *redraw_for_dummy,
6125 buf_T *first_match_buf,
6126 char_u *target_dir)
6127{
6128 buf_T *buf;
6129
6130 buf = curbuf;
6131 qf_jump(qi, 0, 0, forceit);
6132 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006133 // If we jumped to another buffer redrawing will already be
6134 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006135 *redraw_for_dummy = FALSE;
6136
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006137 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006138 if (curbuf == first_match_buf && target_dir != NULL)
6139 {
6140 exarg_T ea;
6141
Bram Moolenaara80faa82020-04-12 19:37:17 +02006142 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006143 ea.arg = target_dir;
6144 ea.cmdidx = CMD_lcd;
6145 ex_cd(&ea);
6146 }
6147}
6148
6149/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006150 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006151 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006152typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006153{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006154 long tomatch; // maximum number of matches to find
6155 char_u *spat; // search pattern
6156 int flags; // search modifier
6157 char_u **fnames; // list of files to search
6158 int fcount; // number of files
6159 regmmatch_T regmatch; // compiled search pattern
6160 char_u *qf_title; // quickfix list title
6161} vgr_args_T;
6162
6163/*
6164 * Process :vimgrep command arguments. The command syntax is:
6165 *
6166 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6167 */
6168 static int
6169vgr_process_args(
6170 exarg_T *eap,
6171 vgr_args_T *args)
6172{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006173 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006174
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006175 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006176
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006177 args->regmatch.regprog = NULL;
6178 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006179
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006180 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006181 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006182 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006183 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006184
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006185 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006186 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006187 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006188 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006189 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006190 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006191 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006192
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006193 vgr_init_regmatch(&args->regmatch, args->spat);
6194 if (args->regmatch.regprog == NULL)
6195 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006196
6197 p = skipwhite(p);
6198 if (*p == NUL)
6199 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006200 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006201 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006202 }
6203
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006204 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006205 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6206 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006207 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006208 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006209 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006210 }
6211
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006212 return OK;
6213}
6214
6215/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006216 * Return TRUE if "buf" had an existing swap file, the current swap file does
6217 * not end in ".swp".
6218 */
6219 static int
6220existing_swapfile(buf_T *buf)
6221{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006222 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006223 {
6224 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6225 size_t len = STRLEN(fname);
6226
6227 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6228 }
6229 return FALSE;
6230}
6231
6232/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006233 * Search for a pattern in a list of files and populate the quickfix list with
6234 * the matches.
6235 */
6236 static int
6237vgr_process_files(
6238 win_T *wp,
6239 qf_info_T *qi,
6240 vgr_args_T *cmd_args,
6241 int *redraw_for_dummy,
6242 buf_T **first_match_buf,
6243 char_u **target_dir)
6244{
6245 int status = FAIL;
6246 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6247 time_t seconds = 0;
6248 char_u *fname;
6249 int fi;
6250 buf_T *buf;
6251 int duplicate_name = FALSE;
6252 int using_dummy;
6253 char_u *dirname_start = NULL;
6254 char_u *dirname_now = NULL;
6255 int found_match;
6256 aco_save_T aco;
6257
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006258 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6259 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006260 if (dirname_start == NULL || dirname_now == NULL)
6261 goto theend;
6262
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006263 // Remember the current directory, because a BufRead autocommand that does
6264 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006265 mch_dirname(dirname_start, MAXPATHL);
6266
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006267 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006268 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6269 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006270 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006271 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006272 if (time(NULL) > seconds)
6273 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006274 // Display the file name every second or so, show the user we are
6275 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006276 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006277 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006278 }
6279
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006280 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006281 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6282 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006283 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006284 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006285 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006286 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006287
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006288 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006289 }
6290 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006291 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006292 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006293
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006294 // Check whether the quickfix list is still valid. When loading a
6295 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006296 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006297 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006298
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006299 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006300
Bram Moolenaar81695252004-12-29 20:58:21 +00006301 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006302 {
6303 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006304 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006305 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006306 else
6307 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006308 // Try for a match in all lines of the buffer.
6309 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006310 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006311 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006312 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006313
Bram Moolenaar81695252004-12-29 20:58:21 +00006314 if (using_dummy)
6315 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006316 if (found_match && *first_match_buf == NULL)
6317 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006318 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006319 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006320 // Never keep a dummy buffer if there is another buffer
6321 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006322 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006323 buf = NULL;
6324 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006325 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006326 || buf->b_p_bh[0] == 'u' // "unload"
6327 || buf->b_p_bh[0] == 'w' // "wipe"
6328 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006329 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006330 // When no match was found we don't need to remember the
6331 // buffer, wipe it out. If there was a match and it
6332 // wasn't the first one or we won't jump there: only
6333 // unload the buffer.
6334 // Ignore 'hidden' here, because it may lead to having too
6335 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006336 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006337 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006338 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006339 buf = NULL;
6340 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006341 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006342 || (cmd_args->flags & VGR_NOJUMP)
6343 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006344 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006345 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006346 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006347 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006348 buf = NULL;
6349 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006350 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006351
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006352 if (buf != NULL)
6353 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006354 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006355 buf->b_flags &= ~BF_DUMMY;
6356
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006357 // If the buffer is still loaded we need to use the
6358 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006359 if (buf == *first_match_buf
6360 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006361 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006362 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006363
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006364 // The buffer is still loaded, the Filetype autocommands
6365 // need to be done now, in that buffer. And the modelines
6366 // need to be done (again). But not the window-local
6367 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006368 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006369#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006370 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6371 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006372#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006373 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006374 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006375 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006376 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006377 }
6378 }
6379
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006380 status = OK;
6381
6382theend:
6383 vim_free(dirname_now);
6384 vim_free(dirname_start);
6385 return status;
6386}
6387
6388/*
6389 * ":vimgrep {pattern} file(s)"
6390 * ":vimgrepadd {pattern} file(s)"
6391 * ":lvimgrep {pattern} file(s)"
6392 * ":lvimgrepadd {pattern} file(s)"
6393 */
6394 void
6395ex_vimgrep(exarg_T *eap)
6396{
6397 vgr_args_T args;
6398 qf_info_T *qi;
6399 qf_list_T *qfl;
6400 int_u save_qfid;
6401 win_T *wp = NULL;
6402 int redraw_for_dummy = FALSE;
6403 buf_T *first_match_buf = NULL;
6404 char_u *target_dir = NULL;
6405 char_u *au_name = NULL;
6406 int status;
6407
6408 au_name = vgr_get_auname(eap->cmdidx);
6409 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6410 curbuf->b_fname, TRUE, curbuf))
6411 {
6412#ifdef FEAT_EVAL
6413 if (aborting())
6414 return;
6415#endif
6416 }
6417
6418 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6419 if (qi == NULL)
6420 return;
6421
6422 if (vgr_process_args(eap, &args) == FAIL)
6423 goto theend;
6424
6425 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6426 && eap->cmdidx != CMD_vimgrepadd
6427 && eap->cmdidx != CMD_lvimgrepadd)
6428 || qf_stack_empty(qi))
6429 // make place for a new list
6430 qf_new_list(qi, args.qf_title);
6431
6432 incr_quickfix_busy();
6433
6434 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6435 &first_match_buf, &target_dir);
6436 if (status != OK)
6437 {
6438 FreeWild(args.fcount, args.fnames);
6439 decr_quickfix_busy();
6440 goto theend;
6441 }
6442
6443 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006444
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006445 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006446 qfl->qf_nonevalid = FALSE;
6447 qfl->qf_ptr = qfl->qf_start;
6448 qfl->qf_index = 1;
6449 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006450
Bram Moolenaar864293a2016-06-02 13:40:04 +02006451 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006452
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006453 // Remember the current quickfix list identifier, so that we can check for
6454 // autocommands changing the current quickfix list.
6455 save_qfid = qf_get_curlist(qi)->qf_id;
6456
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006457 if (au_name != NULL)
6458 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6459 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006460 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6461 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006462 if (!qflist_valid(wp, save_qfid)
6463 || qf_restore_list(qi, save_qfid) == FAIL)
6464 {
6465 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006466 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006467 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006468
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006469 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006470 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006471 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006472 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006473 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6474 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006475 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006476 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006477 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006478
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006479 decr_quickfix_busy();
6480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006481 // If we loaded a dummy buffer into the current window, the autocommands
6482 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006483 if (redraw_for_dummy)
6484 {
6485#ifdef FEAT_FOLDING
6486 foldUpdateAll(curwin);
6487#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006488 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006489#endif
6490 }
6491
Bram Moolenaar86b68352004-12-27 21:59:20 +00006492theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006493 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006494 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006495 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006496}
6497
6498/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006499 * Restore current working directory to "dirname_start" if they differ, taking
6500 * into account whether it is set locally or globally.
6501 */
6502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006503restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006504{
6505 char_u *dirname_now = alloc(MAXPATHL);
6506
6507 if (NULL != dirname_now)
6508 {
6509 mch_dirname(dirname_now, MAXPATHL);
6510 if (STRCMP(dirname_start, dirname_now) != 0)
6511 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006512 // If the directory has changed, change it back by building up an
6513 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006514 exarg_T ea;
6515
Bram Moolenaara80faa82020-04-12 19:37:17 +02006516 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006517 ea.arg = dirname_start;
6518 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6519 ex_cd(&ea);
6520 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006521 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006522 }
6523}
6524
6525/*
6526 * Load file "fname" into a dummy buffer and return the buffer pointer,
6527 * placing the directory resulting from the buffer load into the
6528 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6529 * prior to calling this function. Restores directory to "dirname_start" prior
6530 * to returning, if autocmds or the 'autochdir' option have changed it.
6531 *
6532 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6533 * or wipe_dummy_buffer() later!
6534 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006535 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006536 */
6537 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006538load_dummy_buffer(
6539 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006540 char_u *dirname_start, // in: old directory
6541 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006542{
6543 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006544 bufref_T newbufref;
6545 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006546 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006547 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006548 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006549
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006550 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006551 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6552 if (newbuf == NULL)
6553 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006554 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006555
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006556 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006557 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6558
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006559 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006560 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006561 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006562 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006563 ++newbuf->b_locked;
6564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006565 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006566 aucmd_prepbuf(&aco, newbuf);
6567
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006568 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006569 (void)setfname(curbuf, fname, NULL, FALSE);
6570
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006571 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006572 check_need_swap(TRUE);
6573
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006574 // Remove the "dummy" flag, otherwise autocommands may not
6575 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006576 curbuf->b_flags &= ~BF_DUMMY;
6577
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006578 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006579 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006580 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006581 NULL, READ_NEW | READ_DUMMY);
6582 --newbuf->b_locked;
6583 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006584 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006585 && !(curbuf->b_flags & BF_NEW))
6586 {
6587 failed = FALSE;
6588 if (curbuf != newbuf)
6589 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006590 // Bloody autocommands changed the buffer! Can happen when
6591 // using netrw and editing a remote file. Use the current
6592 // buffer instead, delete the dummy one after restoring the
6593 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006594 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006595 newbuf = curbuf;
6596 }
6597 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006598
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006599 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006600 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006601 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6602 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006603
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006604 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6605 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006606 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006607 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006608
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006609 // When autocommands/'autochdir' option changed directory: go back.
6610 // Let the caller know what the resulting dir was first, in case it is
6611 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006612 mch_dirname(resulting_dir, MAXPATHL);
6613 restore_start_dir(dirname_start);
6614
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006615 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006616 return NULL;
6617 if (failed)
6618 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006619 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006620 return NULL;
6621 }
6622 return newbuf;
6623}
6624
6625/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006626 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6627 * directory to "dirname_start" prior to returning, if autocmds or the
6628 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006629 */
6630 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006631wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006632{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006633 // If any autocommand opened a window on the dummy buffer, close that
6634 // window. If we can't close them all then give up.
6635 while (buf->b_nwindows > 0)
6636 {
6637 int did_one = FALSE;
6638 win_T *wp;
6639
6640 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006641 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006642 if (wp->w_buffer == buf)
6643 {
6644 if (win_close(wp, FALSE) == OK)
6645 did_one = TRUE;
6646 break;
6647 }
6648 if (!did_one)
6649 return;
6650 }
6651
6652 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006653 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006654#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006655 cleanup_T cs;
6656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006657 // Reset the error/interrupt/exception state here so that aborting()
6658 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6659 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006660 enter_cleanup(&cs);
6661#endif
6662
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006663 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006664
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006665#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006666 // Restore the error/interrupt/exception state if not discarded by a
6667 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006668 leave_cleanup(&cs);
6669#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006670 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006671 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006672 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006673}
6674
6675/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006676 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6677 * directory to "dirname_start" prior to returning, if autocmds or the
6678 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006679 */
6680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006681unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006682{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006683 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006684 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006685 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006686
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006687 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006688 restore_start_dir(dirname_start);
6689 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006690}
6691
Bram Moolenaar05159a02005-02-26 23:04:13 +00006692#if defined(FEAT_EVAL) || defined(PROTO)
6693/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006694 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006695 * to 'list'. Returns OK on success.
6696 */
6697 static int
6698get_qfline_items(qfline_T *qfp, list_T *list)
6699{
6700 int bufnum;
6701 dict_T *dict;
6702 char_u buf[2];
6703
6704 // Handle entries with a non-existing buffer number.
6705 bufnum = qfp->qf_fnum;
6706 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6707 bufnum = 0;
6708
6709 if ((dict = dict_alloc()) == NULL)
6710 return FAIL;
6711 if (list_append_dict(list, dict) == FAIL)
6712 return FAIL;
6713
6714 buf[0] = qfp->qf_type;
6715 buf[1] = NUL;
6716 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006717 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6718 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6719 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6720 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6721 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6722 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006723 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6724 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6725 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6726 || dict_add_string(dict, "type", buf) == FAIL
6727 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6728 return FAIL;
6729
6730 return OK;
6731}
6732
6733/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006734 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006735 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006736 * If eidx is not 0, then return only the specified entry. Otherwise return
6737 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006738 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006739 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006740get_errorlist(
6741 qf_info_T *qi_arg,
6742 win_T *wp,
6743 int qf_idx,
6744 int eidx,
6745 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006746{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006747 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006748 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006749 qfline_T *qfp;
6750 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006751
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006752 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006753 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006754 qi = &ql_info;
6755 if (wp != NULL)
6756 {
6757 qi = GET_LOC_LIST(wp);
6758 if (qi == NULL)
6759 return FAIL;
6760 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006761 }
6762
Bram Moolenaar858ba062020-05-31 23:11:59 +02006763 if (eidx < 0)
6764 return OK;
6765
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006766 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006767 qf_idx = qi->qf_curlist;
6768
Bram Moolenaar0398e002019-03-21 21:12:49 +01006769 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006770 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006771
Bram Moolenaar0398e002019-03-21 21:12:49 +01006772 qfl = qf_get_list(qi, qf_idx);
6773 if (qf_list_empty(qfl))
6774 return FAIL;
6775
Bram Moolenaara16123a2019-03-28 20:31:07 +01006776 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006777 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006778 if (eidx > 0)
6779 {
6780 if (eidx == i)
6781 return get_qfline_items(qfp, list);
6782 }
6783 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006784 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006785 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006786
Bram Moolenaar05159a02005-02-26 23:04:13 +00006787 return OK;
6788}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006789
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006790// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006791enum {
6792 QF_GETLIST_NONE = 0x0,
6793 QF_GETLIST_TITLE = 0x1,
6794 QF_GETLIST_ITEMS = 0x2,
6795 QF_GETLIST_NR = 0x4,
6796 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006797 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006798 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006799 QF_GETLIST_IDX = 0x40,
6800 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006801 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006802 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006803 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006804 QF_GETLIST_QFTF = 0x800,
6805 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006806};
6807
6808/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006809 * Parse text from 'di' and return the quickfix list items.
6810 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006811 */
6812 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006813qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006814{
6815 int status = FAIL;
6816 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006817 char_u *errorformat = p_efm;
6818 dictitem_T *efm_di;
6819 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006820
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006821 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006822 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006823 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006824 // If errorformat is supplied then use it, otherwise use the 'efm'
6825 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006826 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6827 {
6828 if (efm_di->di_tv.v_type != VAR_STRING ||
6829 efm_di->di_tv.vval.v_string == NULL)
6830 return FAIL;
6831 errorformat = efm_di->di_tv.vval.v_string;
6832 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006833
Bram Moolenaar36538222017-09-02 19:51:44 +02006834 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006835 if (l == NULL)
6836 return FAIL;
6837
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006838 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006839 if (qi != NULL)
6840 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006841 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006842 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6843 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006844 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006845 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006846 }
6847 free(qi);
6848 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006849 dict_add_list(retdict, "items", l);
6850 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006851 }
6852
6853 return status;
6854}
6855
6856/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006857 * Return the quickfix/location list window identifier in the current tabpage.
6858 */
6859 static int
6860qf_winid(qf_info_T *qi)
6861{
6862 win_T *win;
6863
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006864 // The quickfix window can be opened even if the quickfix list is not set
6865 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006866 if (qi == NULL)
6867 return 0;
6868 win = qf_find_win(qi);
6869 if (win != NULL)
6870 return win->w_id;
6871 return 0;
6872}
6873
6874/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006875 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006876 * window. If there is no buffer associated with the list or the buffer is
6877 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006878 */
6879 static int
6880qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6881{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006882 int bufnum = 0;
6883
6884 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6885 bufnum = qi->qf_bufnr;
6886
6887 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006888}
6889
6890/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006891 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006892 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006893 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006894qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006895{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006896 int flags = QF_GETLIST_NONE;
6897
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006898 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006899 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006900 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006901 if (!loclist)
6902 // File window ID is applicable only to location list windows
6903 flags &= ~ QF_GETLIST_FILEWINID;
6904 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006905
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006906 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006907 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006908
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006909 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006910 flags |= QF_GETLIST_NR;
6911
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006912 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006913 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006914
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006915 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006916 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006917
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006918 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006919 flags |= QF_GETLIST_ID;
6920
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006921 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006922 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006923
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006924 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006925 flags |= QF_GETLIST_IDX;
6926
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006927 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006928 flags |= QF_GETLIST_SIZE;
6929
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006930 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01006931 flags |= QF_GETLIST_TICK;
6932
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006933 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006934 flags |= QF_GETLIST_FILEWINID;
6935
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006936 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006937 flags |= QF_GETLIST_QFBUFNR;
6938
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006939 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02006940 flags |= QF_GETLIST_QFTF;
6941
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006942 return flags;
6943}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006944
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006945/*
6946 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6947 * If 'nr' and 'id' are not present in 'what' then return the current
6948 * quickfix list index.
6949 * If 'nr' is zero then return the current quickfix list index.
6950 * If 'nr' is '$' then return the last quickfix list index.
6951 * If 'id' is present then return the index of the quickfix list with that id.
6952 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6953 * Return -1, if quickfix list is not present or if the stack is empty.
6954 */
6955 static int
6956qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6957{
6958 int qf_idx;
6959 dictitem_T *di;
6960
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006961 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006962 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6963 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006964 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006965 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006966 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006967 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006968 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006969 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006970 qf_idx = di->di_tv.vval.v_number - 1;
6971 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006972 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006973 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006974 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006975 else if (di->di_tv.v_type == VAR_STRING
6976 && di->di_tv.vval.v_string != NULL
6977 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006978 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006979 qf_idx = qi->qf_listcount - 1;
6980 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006981 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006982 }
6983
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006984 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6985 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006986 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006987 if (di->di_tv.v_type == VAR_NUMBER)
6988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006989 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006990 if (di->di_tv.vval.v_number != 0)
6991 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6992 }
6993 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006994 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006995 }
6996
6997 return qf_idx;
6998}
6999
7000/*
7001 * Return default values for quickfix list properties in retdict.
7002 */
7003 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007004qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007005{
7006 int status = OK;
7007
7008 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007009 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007010 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7011 {
7012 list_T *l = list_alloc();
7013 if (l != NULL)
7014 status = dict_add_list(retdict, "items", l);
7015 else
7016 status = FAIL;
7017 }
7018 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007019 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007020 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007021 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007022 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007023 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007024 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007025 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007026 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007027 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007028 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007029 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007030 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007031 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007032 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007033 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007034 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7035 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007036 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7037 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007038
7039 return status;
7040}
7041
7042/*
7043 * Return the quickfix list title as 'title' in retdict
7044 */
7045 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007046qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007047{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007048 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007049}
7050
7051/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007052 * Returns the identifier of the window used to display files from a location
7053 * list. If there is no associated window, then returns 0. Useful only when
7054 * called from a location list window.
7055 */
7056 static int
7057qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7058{
7059 int winid = 0;
7060
7061 if (wp != NULL && IS_LL_WINDOW(wp))
7062 {
7063 win_T *ll_wp = qf_find_win_with_loclist(qi);
7064 if (ll_wp != NULL)
7065 winid = ll_wp->w_id;
7066 }
7067
7068 return dict_add_number(retdict, "filewinid", winid);
7069}
7070
7071/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007072 * Return the quickfix list items/entries as 'items' in retdict.
7073 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007074 */
7075 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007076qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007077{
7078 int status = OK;
7079 list_T *l = list_alloc();
7080 if (l != NULL)
7081 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007082 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007083 dict_add_list(retdict, "items", l);
7084 }
7085 else
7086 status = FAIL;
7087
7088 return status;
7089}
7090
7091/*
7092 * Return the quickfix list context (if any) as 'context' in retdict.
7093 */
7094 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007095qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007096{
7097 int status;
7098 dictitem_T *di;
7099
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007100 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007101 {
7102 di = dictitem_alloc((char_u *)"context");
7103 if (di != NULL)
7104 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007105 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007106 status = dict_add(retdict, di);
7107 if (status == FAIL)
7108 dictitem_free(di);
7109 }
7110 else
7111 status = FAIL;
7112 }
7113 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007114 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007115
7116 return status;
7117}
7118
7119/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007120 * Return the current quickfix list index as 'idx' in retdict.
7121 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007122 */
7123 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007124qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007125{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007126 if (eidx == 0)
7127 {
7128 eidx = qfl->qf_index;
7129 if (qf_list_empty(qfl))
7130 // For empty lists, current index is set to 0
7131 eidx = 0;
7132 }
7133 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007134}
7135
7136/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007137 * Return the 'quickfixtextfunc' function of a quickfix/location list
7138 */
7139 static int
7140qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7141{
7142 int status;
7143
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007144 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007145 {
7146 typval_T tv;
7147
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007148 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007149 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7150 clear_tv(&tv);
7151 }
7152 else
7153 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7154
7155 return status;
7156}
7157
7158/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007159 * Return quickfix/location list details (title) as a
7160 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7161 * then current list is used. Otherwise the specified list is used.
7162 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007163 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007164qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7165{
7166 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007167 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007168 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007169 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007170 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007171 dictitem_T *di;
7172 int flags = QF_GETLIST_NONE;
7173
7174 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7175 return qf_get_list_from_lines(what, di, retdict);
7176
7177 if (wp != NULL)
7178 qi = GET_LOC_LIST(wp);
7179
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007180 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007181
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007182 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007183 qf_idx = qf_getprop_qfidx(qi, what);
7184
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007185 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007186 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007187 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007188
Bram Moolenaar0398e002019-03-21 21:12:49 +01007189 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007190
Bram Moolenaar858ba062020-05-31 23:11:59 +02007191 // If an entry index is specified, use that
7192 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7193 {
7194 if (di->di_tv.v_type != VAR_NUMBER)
7195 return FAIL;
7196 eidx = di->di_tv.vval.v_number;
7197 }
7198
Bram Moolenaard823fa92016-08-12 16:29:27 +02007199 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007200 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007201 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007202 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007203 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007204 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007205 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007206 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007207 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007208 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007209 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007210 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007211 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007212 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007213 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007214 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007215 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007216 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007217 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7218 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007219 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7220 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007221 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7222 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007223
Bram Moolenaard823fa92016-08-12 16:29:27 +02007224 return status;
7225}
7226
7227/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007228 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007229 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7230 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007231 */
7232 static int
7233qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007234 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007235 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007236 int first_entry,
7237 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007238{
7239 static int did_bufnr_emsg;
7240 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007241 int bufnum, valid, status, col, end_col, vcol, nr;
7242 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007243
7244 if (first_entry)
7245 did_bufnr_emsg = FALSE;
7246
Bram Moolenaard61efa52022-07-23 09:52:04 +01007247 filename = dict_get_string(d, "filename", TRUE);
7248 module = dict_get_string(d, "module", TRUE);
7249 bufnum = (int)dict_get_number(d, "bufnr");
7250 lnum = (int)dict_get_number(d, "lnum");
7251 end_lnum = (int)dict_get_number(d, "end_lnum");
7252 col = (int)dict_get_number(d, "col");
7253 end_col = (int)dict_get_number(d, "end_col");
7254 vcol = (int)dict_get_number(d, "vcol");
7255 nr = (int)dict_get_number(d, "nr");
7256 type = dict_get_string(d, "type", TRUE);
7257 pattern = dict_get_string(d, "pattern", TRUE);
7258 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007259 if (text == NULL)
7260 text = vim_strsave((char_u *)"");
7261
7262 valid = TRUE;
7263 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7264 valid = FALSE;
7265
7266 // Mark entries with non-existing buffer number as not valid. Give the
7267 // error message only once.
7268 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7269 {
7270 if (!did_bufnr_emsg)
7271 {
7272 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007273 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007274 }
7275 valid = FALSE;
7276 bufnum = 0;
7277 }
7278
7279 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007280 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007281 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007282
Bram Moolenaar0398e002019-03-21 21:12:49 +01007283 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007284 NULL, // dir
7285 filename,
7286 module,
7287 bufnum,
7288 text,
7289 lnum,
thinca6864efa2021-06-19 20:45:20 +02007290 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007291 col,
thinca6864efa2021-06-19 20:45:20 +02007292 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007293 vcol, // vis_col
7294 pattern, // search pattern
7295 nr,
7296 type == NULL ? NUL : *type,
7297 valid);
7298
7299 vim_free(filename);
7300 vim_free(module);
7301 vim_free(pattern);
7302 vim_free(text);
7303 vim_free(type);
7304
Bram Moolenaar9752c722018-12-22 16:49:34 +01007305 if (valid)
7306 *valid_entry = TRUE;
7307
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007308 return status;
7309}
7310
7311/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007312 * Add list of entries to quickfix/location list. Each list entry is
7313 * a dictionary with item information.
7314 */
7315 static int
7316qf_add_entries(
7317 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007318 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007319 list_T *list,
7320 char_u *title,
7321 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007322{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007323 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007324 listitem_T *li;
7325 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007326 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007327 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007328 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007329
Bram Moolenaara3921f42017-06-04 15:30:34 +02007330 if (action == ' ' || qf_idx == qi->qf_listcount)
7331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007332 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007333 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007334 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007335 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007336 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007337 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007338 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007339 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007340 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007341 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007342 qf_free_items(qfl);
7343 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007344 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007345
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007346 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007347 {
7348 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007349 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007350
7351 d = li->li_tv.vval.v_dict;
7352 if (d == NULL)
7353 continue;
7354
Bram Moolenaar0398e002019-03-21 21:12:49 +01007355 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007356 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007357 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007358 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007359 }
7360
Bram Moolenaar9752c722018-12-22 16:49:34 +01007361 // Check if any valid error entries are added to the list.
7362 if (valid_entry)
7363 qfl->qf_nonevalid = FALSE;
7364 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007365 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007366 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007367
7368 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007369 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007370 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007371
7372 // Update the current error index if not appending to the list or if the
7373 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007374 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007375 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007376
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007377 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007378 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007379
7380 return retval;
7381}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007382
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007383/*
7384 * Get the quickfix list index from 'nr' or 'id'
7385 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007386 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007387qf_setprop_get_qfidx(
7388 qf_info_T *qi,
7389 dict_T *what,
7390 int action,
7391 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007392{
7393 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007394 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007395
Bram Moolenaard823fa92016-08-12 16:29:27 +02007396 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7397 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007398 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007399 if (di->di_tv.v_type == VAR_NUMBER)
7400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007401 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007402 if (di->di_tv.vval.v_number != 0)
7403 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007404
Bram Moolenaar55b69262017-08-13 13:42:01 +02007405 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7406 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007407 // When creating a new list, accept qf_idx pointing to the next
7408 // non-available list and add the new list at the end of the
7409 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007410 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007411 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007412 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007413 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007414 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007415 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007416 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007417 }
7418 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007419 && di->di_tv.vval.v_string != NULL
7420 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007421 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007422 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007423 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007424 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007425 qf_idx = 0;
7426 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007427 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007428 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007429 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007430 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007431 }
7432
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007433 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007434 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007435 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007436 if (di->di_tv.v_type != VAR_NUMBER)
7437 return INVALID_QFIDX;
7438
7439 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007440 }
7441
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007442 return qf_idx;
7443}
7444
7445/*
7446 * Set the quickfix list title.
7447 */
7448 static int
7449qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7450{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007451 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007452
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007453 if (di->di_tv.v_type != VAR_STRING)
7454 return FAIL;
7455
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007456 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007457 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007458 if (qf_idx == qi->qf_curlist)
7459 qf_update_win_titlevar(qi);
7460
7461 return OK;
7462}
7463
7464/*
7465 * Set quickfix list items/entries.
7466 */
7467 static int
7468qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7469{
7470 int retval = FAIL;
7471 char_u *title_save;
7472
7473 if (di->di_tv.v_type != VAR_LIST)
7474 return FAIL;
7475
7476 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7477 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7478 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007479 vim_free(title_save);
7480
7481 return retval;
7482}
7483
7484/*
7485 * Set quickfix list items/entries from a list of lines.
7486 */
7487 static int
7488qf_setprop_items_from_lines(
7489 qf_info_T *qi,
7490 int qf_idx,
7491 dict_T *what,
7492 dictitem_T *di,
7493 int action)
7494{
7495 char_u *errorformat = p_efm;
7496 dictitem_T *efm_di;
7497 int retval = FAIL;
7498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007499 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007500 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7501 {
7502 if (efm_di->di_tv.v_type != VAR_STRING ||
7503 efm_di->di_tv.vval.v_string == NULL)
7504 return FAIL;
7505 errorformat = efm_di->di_tv.vval.v_string;
7506 }
7507
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007508 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007509 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7510 return FAIL;
7511
7512 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007513 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007514 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007515 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007516 retval = OK;
7517
7518 return retval;
7519}
7520
7521/*
7522 * Set quickfix list context.
7523 */
7524 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007525qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007526{
7527 typval_T *ctx;
7528
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007529 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007530 ctx = alloc_tv();
7531 if (ctx != NULL)
7532 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007533 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007534
7535 return OK;
7536}
7537
7538/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007539 * Set the current index in the specified quickfix list
7540 */
7541 static int
7542qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7543{
7544 int denote = FALSE;
7545 int newidx;
7546 int old_qfidx;
7547 qfline_T *qf_ptr;
7548
7549 // If the specified index is '$', then use the last entry
7550 if (di->di_tv.v_type == VAR_STRING
7551 && di->di_tv.vval.v_string != NULL
7552 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7553 newidx = qfl->qf_count;
7554 else
7555 {
7556 // Otherwise use the specified index
7557 newidx = tv_get_number_chk(&di->di_tv, &denote);
7558 if (denote)
7559 return FAIL;
7560 }
7561
7562 if (newidx < 1) // sanity check
7563 return FAIL;
7564 if (newidx > qfl->qf_count)
7565 newidx = qfl->qf_count;
7566
7567 old_qfidx = qfl->qf_index;
7568 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7569 if (qf_ptr == NULL)
7570 return FAIL;
7571 qfl->qf_ptr = qf_ptr;
7572 qfl->qf_index = newidx;
7573
7574 // If the current list is modified and it is displayed in the quickfix
7575 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007576 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007577 qf_win_pos_update(qi, old_qfidx);
7578
7579 return OK;
7580}
7581
7582/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007583 * Set the current index in the specified quickfix list
7584 */
7585 static int
7586qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7587{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007588 callback_T cb;
7589
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007590 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007591 cb = get_callback(&di->di_tv);
7592 if (cb.cb_name != NULL && *cb.cb_name != NUL)
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007593 set_callback(&qfl->qf_qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007594
7595 return OK;
7596}
7597
7598/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007599 * Set quickfix/location list properties (title, items, context).
7600 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007601 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007602 */
7603 static int
7604qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7605{
7606 dictitem_T *di;
7607 int retval = FAIL;
7608 int qf_idx;
7609 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007610 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007611
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007612 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007613 newlist = TRUE;
7614
7615 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007616 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007617 return FAIL;
7618
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007619 if (newlist)
7620 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007621 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007622 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007623 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007624 }
7625
Bram Moolenaar0398e002019-03-21 21:12:49 +01007626 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007627 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007628 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007629 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007630 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007631 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007632 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007633 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007634 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007635 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7636 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007637 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7638 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007639
Bram Moolenaar287153c2020-11-29 14:20:27 +01007640 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007641 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007642 if (newlist)
7643 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007644
Bram Moolenaard823fa92016-08-12 16:29:27 +02007645 return retval;
7646}
7647
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007648/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007649 * Free the entire quickfix/location list stack.
7650 * If the quickfix/location list window is open, then clear it.
7651 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007652 static void
7653qf_free_stack(win_T *wp, qf_info_T *qi)
7654{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007655 win_T *qfwin = qf_find_win(qi);
7656 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007657
7658 if (qfwin != NULL)
7659 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007660 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007661 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007662 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007663 qf_update_buffer(qi, NULL);
7664 }
7665
7666 if (wp != NULL && IS_LL_WINDOW(wp))
7667 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007668 // If in the location list window, then use the non-location list
7669 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007670 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007671 if (llwin != NULL)
7672 wp = llwin;
7673 }
7674
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007675 qf_free_all(wp);
7676 if (wp == NULL)
7677 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007678 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007679 qi->qf_curlist = 0;
7680 qi->qf_listcount = 0;
7681 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007682 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007683 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007684 // If the location list window is open, then create a new empty
7685 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007686 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007687
Bram Moolenaar95946f12019-03-31 15:31:59 +02007688 if (new_ll != NULL)
7689 {
7690 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007691
Bram Moolenaar95946f12019-03-31 15:31:59 +02007692 // first free the list reference in the location list window
7693 ll_free_all(&qfwin->w_llist_ref);
7694
7695 qfwin->w_llist_ref = new_ll;
7696 if (wp != qfwin)
7697 win_set_loclist(wp, new_ll);
7698 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007699 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007700}
7701
Bram Moolenaard823fa92016-08-12 16:29:27 +02007702/*
7703 * Populate the quickfix list with the items supplied in the list
7704 * of dictionaries. "title" will be copied to w:quickfix_title.
7705 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007706 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007707 */
7708 int
7709set_errorlist(
7710 win_T *wp,
7711 list_T *list,
7712 int action,
7713 char_u *title,
7714 dict_T *what)
7715{
7716 qf_info_T *qi = &ql_info;
7717 int retval = OK;
7718
7719 if (wp != NULL)
7720 {
7721 qi = ll_get_or_alloc_list(wp);
7722 if (qi == NULL)
7723 return FAIL;
7724 }
7725
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007726 if (action == 'f')
7727 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007728 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007729 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007730 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007731 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007732
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007733 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007734 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007735 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007736 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007737 _("cannot have both a list and a \"what\" argument"));
7738 return FAIL;
7739 }
7740
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007741 incr_quickfix_busy();
7742
7743 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007744 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007745 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007746 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007747 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007748 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007749 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007750 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007751
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007752 decr_quickfix_busy();
7753
Bram Moolenaard823fa92016-08-12 16:29:27 +02007754 return retval;
7755}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007756
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007757/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007758 * Mark the quickfix context and callback function as in use for all the lists
7759 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007760 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007761 static int
7762mark_quickfix_ctx(qf_info_T *qi, int copyID)
7763{
7764 int i;
7765 int abort = FALSE;
7766 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007767 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007768
7769 for (i = 0; i < LISTCOUNT && !abort; ++i)
7770 {
7771 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007772 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7773 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007774 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7775
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007776 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007777 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007778 }
7779
7780 return abort;
7781}
7782
7783/*
7784 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007785 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007786 */
7787 int
7788set_ref_in_quickfix(int copyID)
7789{
7790 int abort = FALSE;
7791 tabpage_T *tp;
7792 win_T *win;
7793
7794 abort = mark_quickfix_ctx(&ql_info, copyID);
7795 if (abort)
7796 return abort;
7797
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007798 abort = set_ref_in_callback(&qftf_cb, copyID);
7799 if (abort)
7800 return abort;
7801
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007802 FOR_ALL_TAB_WINDOWS(tp, win)
7803 {
7804 if (win->w_llist != NULL)
7805 {
7806 abort = mark_quickfix_ctx(win->w_llist, copyID);
7807 if (abort)
7808 return abort;
7809 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007810 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7811 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007812 // In a location list window and none of the other windows is
7813 // referring to this location list. Mark the location list
7814 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007815 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7816 if (abort)
7817 return abort;
7818 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007819 }
7820
7821 return abort;
7822}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007823#endif
7824
Bram Moolenaar81695252004-12-29 20:58:21 +00007825/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007826 * Return the autocmd name for the :cbuffer Ex commands
7827 */
7828 static char_u *
7829cbuffer_get_auname(cmdidx_T cmdidx)
7830{
7831 switch (cmdidx)
7832 {
7833 case CMD_cbuffer: return (char_u *)"cbuffer";
7834 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7835 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7836 case CMD_lbuffer: return (char_u *)"lbuffer";
7837 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7838 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7839 default: return NULL;
7840 }
7841}
7842
7843/*
7844 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7845 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7846 */
7847 static int
7848cbuffer_process_args(
7849 exarg_T *eap,
7850 buf_T **bufp,
7851 linenr_T *line1,
7852 linenr_T *line2)
7853{
7854 buf_T *buf = NULL;
7855
7856 if (*eap->arg == NUL)
7857 buf = curbuf;
7858 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7859 buf = buflist_findnr(atoi((char *)eap->arg));
7860
7861 if (buf == NULL)
7862 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007863 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007864 return FAIL;
7865 }
7866
7867 if (buf->b_ml.ml_mfp == NULL)
7868 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007869 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007870 return FAIL;
7871 }
7872
7873 if (eap->addr_count == 0)
7874 {
7875 eap->line1 = 1;
7876 eap->line2 = buf->b_ml.ml_line_count;
7877 }
7878
7879 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7880 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7881 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007882 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007883 return FAIL;
7884 }
7885
7886 *line1 = eap->line1;
7887 *line2 = eap->line2;
7888 *bufp = buf;
7889
7890 return OK;
7891}
7892
7893/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007894 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007895 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007896 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007897 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007898 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007899 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007900 */
7901 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007902ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007903{
7904 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007905 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007906 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007907 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007908 int_u save_qfid;
7909 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007910 char_u *qf_title;
7911 linenr_T line1;
7912 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007913
Bram Moolenaara16123a2019-03-28 20:31:07 +01007914 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007915 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007916 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007917 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007918#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007919 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007920 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007921#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007922 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007923
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007924 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007925 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7926 if (qi == NULL)
7927 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007928
Bram Moolenaara16123a2019-03-28 20:31:07 +01007929 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7930 return;
7931
7932 qf_title = qf_cmdtitle(*eap->cmdlinep);
7933
7934 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007935 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007936 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7937 (char *)qf_title, (char *)buf->b_sfname);
7938 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007939 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007940
7941 incr_quickfix_busy();
7942
7943 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7944 (eap->cmdidx != CMD_caddbuffer
7945 && eap->cmdidx != CMD_laddbuffer),
7946 line1, line2,
7947 qf_title, NULL);
7948 if (qf_stack_empty(qi))
7949 {
7950 decr_quickfix_busy();
7951 return;
7952 }
7953 if (res >= 0)
7954 qf_list_changed(qf_get_curlist(qi));
7955
7956 // Remember the current quickfix list identifier, so that we can
7957 // check for autocommands changing the current quickfix list.
7958 save_qfid = qf_get_curlist(qi)->qf_id;
7959 if (au_name != NULL)
7960 {
7961 buf_T *curbuf_old = curbuf;
7962
7963 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7964 TRUE, curbuf);
7965 if (curbuf != curbuf_old)
7966 // Autocommands changed buffer, don't jump now, "qi" may
7967 // be invalid.
7968 res = 0;
7969 }
7970 // Jump to the first error for a new list and if autocmds didn't
7971 // free the list.
7972 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7973 eap->cmdidx == CMD_lbuffer)
7974 && qflist_valid(wp, save_qfid))
7975 // display the first error
7976 qf_jump_first(qi, save_qfid, eap->forceit);
7977
7978 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007979}
7980
Bram Moolenaar1e015462005-09-25 22:16:38 +00007981#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007982/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007983 * Return the autocmd name for the :cexpr Ex commands.
7984 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007985 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007986cexpr_get_auname(cmdidx_T cmdidx)
7987{
7988 switch (cmdidx)
7989 {
7990 case CMD_cexpr: return (char_u *)"cexpr";
7991 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7992 case CMD_caddexpr: return (char_u *)"caddexpr";
7993 case CMD_lexpr: return (char_u *)"lexpr";
7994 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7995 case CMD_laddexpr: return (char_u *)"laddexpr";
7996 default: return NULL;
7997 }
7998}
7999
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008000 int
8001trigger_cexpr_autocmd(int cmdidx)
8002{
8003 char_u *au_name = cexpr_get_auname(cmdidx);
8004
8005 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8006 curbuf->b_fname, TRUE, curbuf))
8007 {
8008 if (aborting())
8009 return FAIL;
8010 }
8011 return OK;
8012}
8013
8014 int
8015cexpr_core(exarg_T *eap, typval_T *tv)
8016{
8017 qf_info_T *qi;
8018 win_T *wp = NULL;
8019
8020 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8021 if (qi == NULL)
8022 return FAIL;
8023
8024 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8025 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8026 {
8027 int res;
8028 int_u save_qfid;
8029 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8030
8031 incr_quickfix_busy();
8032 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8033 (eap->cmdidx != CMD_caddexpr
8034 && eap->cmdidx != CMD_laddexpr),
8035 (linenr_T)0, (linenr_T)0,
8036 qf_cmdtitle(*eap->cmdlinep), NULL);
8037 if (qf_stack_empty(qi))
8038 {
8039 decr_quickfix_busy();
8040 return FAIL;
8041 }
8042 if (res >= 0)
8043 qf_list_changed(qf_get_curlist(qi));
8044
8045 // Remember the current quickfix list identifier, so that we can
8046 // check for autocommands changing the current quickfix list.
8047 save_qfid = qf_get_curlist(qi)->qf_id;
8048 if (au_name != NULL)
8049 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8050 curbuf->b_fname, TRUE, curbuf);
8051
8052 // Jump to the first error for a new list and if autocmds didn't
8053 // free the list.
8054 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8055 && qflist_valid(wp, save_qfid))
8056 // display the first error
8057 qf_jump_first(qi, save_qfid, eap->forceit);
8058 decr_quickfix_busy();
8059 return OK;
8060 }
8061
Bram Moolenaar677658a2022-01-05 16:09:06 +00008062 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008063 return FAIL;
8064}
8065
Bram Moolenaara16123a2019-03-28 20:31:07 +01008066/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008067 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8068 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008069 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008070 */
8071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008072ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008073{
8074 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008075
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008076 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008077 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008078
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008079 // Evaluate the expression. When the result is a string or a list we can
8080 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008081 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008082 if (tv != NULL)
8083 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008084 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008085 free_tv(tv);
8086 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008087}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008088#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008089
8090/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008091 * Get the location list for ":lhelpgrep"
8092 */
8093 static qf_info_T *
8094hgr_get_ll(int *new_ll)
8095{
8096 win_T *wp;
8097 qf_info_T *qi;
8098
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008099 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008100 if (bt_help(curwin->w_buffer))
8101 wp = curwin;
8102 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008103 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008104 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008105
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008106 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008107 qi = NULL;
8108 else
8109 qi = wp->w_llist;
8110
8111 if (qi == NULL)
8112 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008113 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008114 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008115 return NULL;
8116 *new_ll = TRUE;
8117 }
8118
8119 return qi;
8120}
8121
8122/*
8123 * Search for a pattern in a help file.
8124 */
8125 static void
8126hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008127 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008128 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008129 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008130 regmatch_T *p_regmatch)
8131{
8132 FILE *fd;
8133 long lnum;
8134
8135 fd = mch_fopen((char *)fname, "r");
8136 if (fd == NULL)
8137 return;
8138
8139 lnum = 1;
8140 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8141 {
8142 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008143
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008144 // Convert a line if 'encoding' is not utf-8 and
8145 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008146 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008147 {
8148 line = string_convert(p_vc, IObuff, NULL);
8149 if (line == NULL)
8150 line = IObuff;
8151 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008152
8153 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8154 {
8155 int l = (int)STRLEN(line);
8156
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008157 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008158 while (l > 0 && line[l - 1] <= ' ')
8159 line[--l] = NUL;
8160
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008161 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008162 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008163 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008164 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008165 0,
8166 line,
8167 lnum,
thinca6864efa2021-06-19 20:45:20 +02008168 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008169 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008170 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008171 (int)(p_regmatch->endp[0] - line)
8172 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008173 FALSE, // vis_col
8174 NULL, // search pattern
8175 0, // nr
8176 1, // type
8177 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008178 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008179 {
8180 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008181 if (line != IObuff)
8182 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008183 break;
8184 }
8185 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008186 if (line != IObuff)
8187 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008188 ++lnum;
8189 line_breakcheck();
8190 }
8191 fclose(fd);
8192}
8193
8194/*
8195 * Search for a pattern in all the help files in the doc directory under
8196 * the given directory.
8197 */
8198 static void
8199hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008200 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008201 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008202 regmatch_T *p_regmatch,
8203 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008204#ifdef FEAT_MULTI_LANG
8205 , char_u *lang
8206#endif
8207 )
8208{
8209 int fcount;
8210 char_u **fnames;
8211 int fi;
8212
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008213 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008214 add_pathsep(dirname);
8215 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8216 if (gen_expand_wildcards(1, &dirname, &fcount,
8217 &fnames, EW_FILE|EW_SILENT) == OK
8218 && fcount > 0)
8219 {
8220 for (fi = 0; fi < fcount && !got_int; ++fi)
8221 {
8222#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008223 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008224 if (lang != NULL
8225 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008226 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008227 && !(STRNICMP(lang, "en", 2) == 0
8228 && STRNICMP("txt", fnames[fi]
8229 + STRLEN(fnames[fi]) - 3, 3) == 0))
8230 continue;
8231#endif
8232
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008233 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008234 }
8235 FreeWild(fcount, fnames);
8236 }
8237}
8238
8239/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008240 * Search for a pattern in all the help files in the 'runtimepath'
8241 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008242 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008243 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008244 */
8245 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008246hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008247{
8248 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008249
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008250 vimconv_T vc;
8251
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008252 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8253 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008254 vc.vc_type = CONV_NONE;
8255 if (!enc_utf8)
8256 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008257
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008258 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008259 p = p_rtp;
8260 while (*p != NUL && !got_int)
8261 {
8262 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8263
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008264 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008265#ifdef FEAT_MULTI_LANG
8266 , lang
8267#endif
8268 );
8269 }
8270
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008271 if (vc.vc_type != CONV_NONE)
8272 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008273}
8274
8275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 * ":helpgrep {pattern}"
8277 */
8278 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008279ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
8281 regmatch_T regmatch;
8282 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008283 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008284 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008285 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008286 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008287 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008288 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
Bram Moolenaar73633f82012-01-20 13:39:07 +01008290 switch (eap->cmdidx)
8291 {
8292 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8293 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8294 default: break;
8295 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008296 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8297 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008298 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008299#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008300 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008301 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008302#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008303 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008304
Bram Moolenaar39665952018-08-15 20:59:48 +02008305 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008306 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008307 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008308 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008309 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008310 }
8311
Bram Moolenaara16123a2019-03-28 20:31:07 +01008312 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8313 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008314 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008315 p_cpo = empty_option;
8316
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008317 incr_quickfix_busy();
8318
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008319#ifdef FEAT_MULTI_LANG
8320 // Check for a specified language
8321 lang = check_help_lang(eap->arg);
8322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8324 regmatch.rm_ic = FALSE;
8325 if (regmatch.regprog != NULL)
8326 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008327 qf_list_T *qfl;
8328
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008329 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008330 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008331 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008333 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008334
Bram Moolenaar473de612013-06-08 18:19:48 +02008335 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008337 qfl->qf_nonevalid = FALSE;
8338 qfl->qf_ptr = qfl->qf_start;
8339 qfl->qf_index = 1;
8340 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008341 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008344 if (p_cpo == empty_option)
8345 p_cpo = save_cpo;
8346 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008347 {
8348 // Darn, some plugin changed the value. If it's still empty it was
8349 // changed and restored, need to restore in the complicated way.
8350 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008351 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008352 if (save_cpo_allocated)
8353 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008354 }
8355
8356 if (updated)
8357 // This may open a window and source scripts, do this after 'cpo' was
8358 // restored.
8359 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaar73633f82012-01-20 13:39:07 +01008361 if (au_name != NULL)
8362 {
8363 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8364 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008365 // When adding a location list to an existing location list stack,
8366 // if the autocmd made the stack invalid, then just return.
8367 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008368 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008369 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008370 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008371 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008372 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008373
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008374 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008375 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008376 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008377 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008378 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008379
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008380 decr_quickfix_busy();
8381
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008382 if (eap->cmdidx == CMD_lhelpgrep)
8383 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008384 // If the help window is not opened or if it already points to the
8385 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008386 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008387 {
8388 if (new_qi)
8389 ll_free_all(&qi);
8390 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008391 else if (curwin->w_llist == NULL && new_qi)
8392 // current window didn't have a location list associated with it
8393 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008394 curwin->w_llist = qi;
8395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008397#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008398
8399#if defined(FEAT_EVAL) || defined(PROTO)
8400# ifdef FEAT_QUICKFIX
8401 static void
8402get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8403{
8404 if (what_arg->v_type == VAR_UNKNOWN)
8405 {
8406 if (rettv_list_alloc(rettv) == OK)
8407 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008408 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008409 }
8410 else
8411 {
8412 if (rettv_dict_alloc(rettv) == OK)
8413 if (is_qf || (wp != NULL))
8414 {
8415 if (what_arg->v_type == VAR_DICT)
8416 {
8417 dict_T *d = what_arg->vval.v_dict;
8418
8419 if (d != NULL)
8420 qf_get_properties(wp, d, rettv->vval.v_dict);
8421 }
8422 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008423 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008424 }
8425 }
8426}
8427# endif
8428
8429/*
8430 * "getloclist()" function
8431 */
8432 void
8433f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8434{
8435# ifdef FEAT_QUICKFIX
8436 win_T *wp;
8437
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008438 if (in_vim9script()
8439 && (check_for_number_arg(argvars, 0) == FAIL
8440 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8441 return;
8442
Bram Moolenaare677df82019-09-02 22:31:11 +02008443 wp = find_win_by_nr_or_id(&argvars[0]);
8444 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8445# endif
8446}
8447
8448/*
8449 * "getqflist()" function
8450 */
8451 void
8452f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8453{
8454# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008455 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8456 return;
8457
Bram Moolenaare677df82019-09-02 22:31:11 +02008458 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8459# endif
8460}
8461
8462/*
8463 * Used by "setqflist()" and "setloclist()" functions
8464 */
8465 static void
8466set_qf_ll_list(
8467 win_T *wp UNUSED,
8468 typval_T *list_arg UNUSED,
8469 typval_T *action_arg UNUSED,
8470 typval_T *what_arg UNUSED,
8471 typval_T *rettv)
8472{
8473# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008474 char_u *act;
8475 int action = 0;
8476 static int recursive = 0;
8477# endif
8478
8479 rettv->vval.v_number = -1;
8480
8481# ifdef FEAT_QUICKFIX
8482 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008483 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008484 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008485 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008486 else
8487 {
8488 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008489 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008490 int valid_dict = TRUE;
8491
8492 if (action_arg->v_type == VAR_STRING)
8493 {
8494 act = tv_get_string_chk(action_arg);
8495 if (act == NULL)
8496 return; // type error; errmsg already given
8497 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8498 act[1] == NUL)
8499 action = *act;
8500 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008501 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008502 }
8503 else if (action_arg->v_type == VAR_UNKNOWN)
8504 action = ' ';
8505 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008506 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008507
8508 if (action_arg->v_type != VAR_UNKNOWN
8509 && what_arg->v_type != VAR_UNKNOWN)
8510 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008511 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8512 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008513 else
8514 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008515 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008516 valid_dict = FALSE;
8517 }
8518 }
8519
8520 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008521 if (l != NULL && action && valid_dict
8522 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008523 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008524 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008525 rettv->vval.v_number = 0;
8526 --recursive;
8527 }
8528# endif
8529}
8530
8531/*
8532 * "setloclist()" function
8533 */
8534 void
8535f_setloclist(typval_T *argvars, typval_T *rettv)
8536{
8537 win_T *win;
8538
8539 rettv->vval.v_number = -1;
8540
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008541 if (in_vim9script()
8542 && (check_for_number_arg(argvars, 0) == FAIL
8543 || check_for_list_arg(argvars, 1) == FAIL
8544 || check_for_opt_string_arg(argvars, 2) == FAIL
8545 || (argvars[2].v_type != VAR_UNKNOWN
8546 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8547 return;
8548
Bram Moolenaare677df82019-09-02 22:31:11 +02008549 win = find_win_by_nr_or_id(&argvars[0]);
8550 if (win != NULL)
8551 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8552}
8553
8554/*
8555 * "setqflist()" function
8556 */
8557 void
8558f_setqflist(typval_T *argvars, typval_T *rettv)
8559{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008560 if (in_vim9script()
8561 && (check_for_list_arg(argvars, 0) == FAIL
8562 || check_for_opt_string_arg(argvars, 1) == FAIL
8563 || (argvars[1].v_type != VAR_UNKNOWN
8564 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8565 return;
8566
Bram Moolenaare677df82019-09-02 22:31:11 +02008567 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8568}
8569#endif