blob: 378026237491127f7385f479867ee52ad53fcedd [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);
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +0100179static void qf_fmt_text(garray_T *gap, char_u *text);
180static void qf_range_text(garray_T *gap, qfline_T *qfp);
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;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003289 garray_T ga;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003290
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();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003295 vim_snprintf((char *)IObuff, IOSIZE, _("(%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.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003300 ga_init2(&ga, 1, 256);
3301 ga_concat(&ga, IObuff);
3302 qf_fmt_text(&ga, skipwhite(qf_ptr->qf_text));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003303
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003304 // Output the message. Overwrite to avoid scrolling when the 'O'
3305 // flag is present in 'shortmess'; But when not jumping, print the
3306 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003307 i = msg_scroll;
3308 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3309 msg_scroll = TRUE;
3310 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3311 msg_scroll = FALSE;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003312 msg_attr_keep((char *)ga.ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003313 msg_scroll = i;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003314 ga_clear(&ga);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003315}
3316
3317/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003318 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003319 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3320 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003321 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3322 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003323 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3324 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003325 */
3326 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003327qf_jump_open_window(
3328 qf_info_T *qi,
3329 qfline_T *qf_ptr,
3330 int newwin,
3331 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003332{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003333 qf_list_T *qfl = qf_get_curlist(qi);
3334 int old_changedtick = qfl->qf_changedtick;
3335 int old_qf_curlist = qi->qf_curlist;
3336 qfltype_T qfl_type = qfl->qfl_type;
3337
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003338 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003339 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3340 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003341 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003342 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003343 if (old_qf_curlist != qi->qf_curlist
3344 || old_changedtick != qfl->qf_changedtick
3345 || !is_qf_entry_present(qfl, qf_ptr))
3346 {
3347 if (qfl_type == QFLT_QUICKFIX)
3348 emsg(_(e_current_quickfix_list_was_changed));
3349 else
3350 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003351 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003352 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003353
3354 // If currently in the quickfix window, find another window to show the
3355 // file in.
3356 if (bt_quickfix(curbuf) && !*opened_window)
3357 {
3358 // If there is no file specified, we don't know where to go.
3359 // But do advance, otherwise ":cn" gets stuck.
3360 if (qf_ptr->qf_fnum == 0)
3361 return NOTDONE;
3362
Bram Moolenaarb2443732018-11-11 22:50:27 +01003363 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3364 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003365 return FAIL;
3366 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003367 if (old_qf_curlist != qi->qf_curlist
3368 || old_changedtick != qfl->qf_changedtick
3369 || !is_qf_entry_present(qfl, qf_ptr))
3370 {
3371 if (qfl_type == QFLT_QUICKFIX)
3372 emsg(_(e_current_quickfix_list_was_changed));
3373 else
3374 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003375 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003376 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003377
3378 return OK;
3379}
3380
3381/*
3382 * Edit a selected file from the quickfix/location list and jump to a
3383 * particular line/column, adjust the folds and display a message about the
3384 * jump.
3385 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003386 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003387 * the file.
3388 */
3389 static int
3390qf_jump_to_buffer(
3391 qf_info_T *qi,
3392 int qf_index,
3393 qfline_T *qf_ptr,
3394 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003395 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003396 int *opened_window,
3397 int openfold,
3398 int print_message)
3399{
3400 buf_T *old_curbuf;
3401 linenr_T old_lnum;
3402 int retval = OK;
3403
3404 // If there is a file name, read the wanted file if needed, and check
3405 // autowrite etc.
3406 old_curbuf = curbuf;
3407 old_lnum = curwin->w_cursor.lnum;
3408
3409 if (qf_ptr->qf_fnum != 0)
3410 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003411 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003412 opened_window);
3413 if (retval != OK)
3414 return retval;
3415 }
3416
3417 // When not switched to another buffer, still need to set pc mark
3418 if (curbuf == old_curbuf)
3419 setpcmark();
3420
3421 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3422 qf_ptr->qf_pattern);
3423
3424#ifdef FEAT_FOLDING
3425 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3426 foldOpenCursor();
3427#endif
3428 if (print_message)
3429 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3430
3431 return retval;
3432}
3433
3434/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003435 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
3437 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003438qf_jump(qf_info_T *qi,
3439 int dir,
3440 int errornr,
3441 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003443 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3444}
3445
3446/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003447 * Jump to a quickfix line.
3448 * If dir == 0 go to entry "errornr".
3449 * If dir == FORWARD go "errornr" valid entries forward.
3450 * If dir == BACKWARD go "errornr" valid entries backward.
3451 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3452 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3453 * else if "errornr" is zero, redisplay the same line
3454 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003455 * If 'newwin' is TRUE, then open the file in a new window.
3456 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003457 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003458qf_jump_newwin(qf_info_T *qi,
3459 int dir,
3460 int errornr,
3461 int forceit,
3462 int newwin)
3463{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003464 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003465 qfline_T *qf_ptr;
3466 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003469 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003470 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003471 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003474 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003475 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003477 if (qi == NULL)
3478 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479
Bram Moolenaar0398e002019-03-21 21:12:49 +01003480 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003482 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 return;
3484 }
3485
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003486 incr_quickfix_busy();
3487
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003488 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003489
3490 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003492 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003494
3495 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3496 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003498 qf_ptr = old_qf_ptr;
3499 qf_index = old_qf_index;
3500 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003503 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003504 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003505 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003506 // No need to print the error message if it's visible in the error
3507 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 print_message = FALSE;
3509
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003510 prev_winid = curwin->w_id;
3511
Bram Moolenaarb2443732018-11-11 22:50:27 +01003512 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003513 if (retval == FAIL)
3514 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003515 if (retval == QF_ABORT)
3516 {
3517 qi = NULL;
3518 qf_ptr = NULL;
3519 goto theend;
3520 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003521 if (retval == NOTDONE)
3522 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003523
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003524 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003525 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003526 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003528 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003529 qi = NULL;
3530 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003533 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003536 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003537 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003539 // Couldn't open file, so put index back where it was. This could
3540 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 qf_ptr = old_qf_ptr;
3543 qf_index = old_qf_index;
3544 }
3545 }
3546theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003547 if (qi != NULL)
3548 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003549 qfl->qf_ptr = qf_ptr;
3550 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003551 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003552 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003554 // Restore old 'switchbuf' value, but not when an autocommand or
3555 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003556 p_swb = old_swb;
3557 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003559 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560}
3561
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003562// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003563static int qfFileAttr;
3564static int qfSepAttr;
3565static int qfLineAttr;
3566
3567/*
3568 * Display information about a single entry from the quickfix/location list.
3569 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003570 * 'cursel' will be set to TRUE for the currently selected entry in the
3571 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003572 */
3573 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003574qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003575{
3576 char_u *fname;
3577 buf_T *buf;
3578 int filter_entry;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003579 garray_T ga;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003580
3581 fname = NULL;
3582 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3583 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3584 (char *)qfp->qf_module);
3585 else {
3586 if (qfp->qf_fnum != 0
3587 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3588 {
3589 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003590 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003591 fname = gettail(fname);
3592 }
3593 if (fname == NULL)
3594 sprintf((char *)IObuff, "%2d", qf_idx);
3595 else
3596 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3597 qf_idx, (char *)fname);
3598 }
3599
3600 // Support for filtering entries using :filter /pat/ clist
3601 // Match against the module name, file name, search pattern and
3602 // text of the entry.
3603 filter_entry = TRUE;
3604 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3605 filter_entry &= message_filtered(qfp->qf_module);
3606 if (filter_entry && fname != NULL)
3607 filter_entry &= message_filtered(fname);
3608 if (filter_entry && qfp->qf_pattern != NULL)
3609 filter_entry &= message_filtered(qfp->qf_pattern);
3610 if (filter_entry)
3611 filter_entry &= message_filtered(qfp->qf_text);
3612 if (filter_entry)
3613 return;
3614
3615 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003616 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003617
3618 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003619 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003620 ga_init2(&ga, 1, 256);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003621 if (qfp->qf_lnum == 0)
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003622 ga_append(&ga, NUL);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003623 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003624 qf_range_text(&ga, qfp);
3625 ga_concat(&ga, qf_types(qfp->qf_type, qfp->qf_nr));
3626 ga_append(&ga, NUL);
3627 msg_puts_attr((char *)ga.ga_data, qfLineAttr);
3628 ga_clear(&ga);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003629 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003630 if (qfp->qf_pattern != NULL)
3631 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003632 qf_fmt_text(&ga, qfp->qf_pattern);
3633 msg_puts((char *)ga.ga_data);
3634 ga_clear(&ga);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003635 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003636 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003637 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003638
Bram Moolenaar5f30e262022-07-28 11:56:01 +01003639 {
Bram Moolenaar5f30e262022-07-28 11:56:01 +01003640 // Remove newlines and leading whitespace from the text. For an
3641 // unrecognized line keep the indent, the compiler may mark a word
3642 // with ^^^^.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003643 qf_fmt_text(&ga, (fname != NULL || qfp->qf_lnum != 0)
3644 ? skipwhite(qfp->qf_text) : qfp->qf_text);
3645 msg_prt_line((char_u *)ga.ga_data, FALSE);
3646 ga_clear(&ga);
Bram Moolenaar5f30e262022-07-28 11:56:01 +01003647 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003648 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003649}
3650
3651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003653 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 */
3655 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003656qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003658 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003659 qfline_T *qfp;
3660 int i;
3661 int idx1 = 1;
3662 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003663 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003664 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003665 int all = eap->forceit; // if not :cl!, only show
3666 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003667 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003669 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3670 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003671
Bram Moolenaar0398e002019-03-21 21:12:49 +01003672 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003674 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return;
3676 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003677 if (*arg == '+')
3678 {
3679 ++arg;
3680 plus = TRUE;
3681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3683 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003684 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 return;
3686 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003687 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003688 if (plus)
3689 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003690 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003691 idx2 = i + idx1;
3692 idx1 = i;
3693 }
3694 else
3695 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003696 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003697 if (idx1 < 0)
3698 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3699 if (idx2 < 0)
3700 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003703 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003704 shorten_fnames(FALSE);
3705
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003706 // Get the attributes for the different quickfix highlight items. Note
3707 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003708 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3709 if (qfFileAttr == 0)
3710 qfFileAttr = HL_ATTR(HLF_D);
3711 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3712 if (qfSepAttr == 0)
3713 qfSepAttr = HL_ATTR(HLF_D);
3714 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3715 if (qfLineAttr == 0)
3716 qfLineAttr = HL_ATTR(HLF_N);
3717
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003718 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003720 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
3722 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003723 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 ui_breakcheck();
3726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727}
3728
3729/*
3730 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003731 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
3733 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003734qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 char_u *p = text;
3737
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003738 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
3740 if (*p == '\n')
3741 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003742 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003744 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 break;
3746 }
3747 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003748 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003750
3751 ga_append(gap, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752}
3753
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003754/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003755 * Add the range information from the lnum, col, end_lnum, and end_col values
3756 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02003757 */
3758 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003759qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02003760{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003761 char_u *buf = IObuff;
3762 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02003763 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003764
thinca6864efa2021-06-19 20:45:20 +02003765 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3766 len = (int)STRLEN(buf);
3767
3768 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3769 {
3770 vim_snprintf((char *)buf + len, bufsize - len,
3771 "-%ld", qfp->qf_end_lnum);
3772 len += (int)STRLEN(buf + len);
3773 }
3774 if (qfp->qf_col > 0)
3775 {
3776 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3777 len += (int)STRLEN(buf + len);
3778 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3779 {
3780 vim_snprintf((char *)buf + len, bufsize - len,
3781 "-%d", qfp->qf_end_col);
3782 len += (int)STRLEN(buf + len);
3783 }
3784 }
3785 buf[len] = NUL;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003786
3787 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02003788}
3789
3790/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003791 * Display information (list number, list size and the title) about a
3792 * quickfix/location list.
3793 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003794 static void
3795qf_msg(qf_info_T *qi, int which, char *lead)
3796{
3797 char *title = (char *)qi->qf_lists[which].qf_title;
3798 int count = qi->qf_lists[which].qf_count;
3799 char_u buf[IOSIZE];
3800
3801 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3802 lead,
3803 which + 1,
3804 qi->qf_listcount,
3805 count);
3806
3807 if (title != NULL)
3808 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003809 size_t len = STRLEN(buf);
3810
3811 if (len < 34)
3812 {
3813 vim_memset(buf + len, ' ', 34 - len);
3814 buf[34] = NUL;
3815 }
3816 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003817 }
3818 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003819 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003820}
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822/*
3823 * ":colder [count]": Up in the quickfix stack.
3824 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003825 * ":lolder [count]": Up in the location list stack.
3826 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 */
3828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003829qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003831 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 int count;
3833
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003834 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3835 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (eap->addr_count != 0)
3838 count = eap->line2;
3839 else
3840 count = 1;
3841 while (count--)
3842 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003843 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003845 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003847 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 else
3853 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003854 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003856 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003857 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003859 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003862 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003863 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864}
3865
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003866/*
3867 * Display the information about all the quickfix/location lists in the stack
3868 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003869 void
3870qf_history(exarg_T *eap)
3871{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003872 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003873 int i;
3874
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003875 if (eap->addr_count > 0)
3876 {
3877 if (qi == NULL)
3878 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003879 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003880 return;
3881 }
3882
3883 // Jump to the specified quickfix list
3884 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3885 {
3886 qi->qf_curlist = eap->line2 - 1;
3887 qf_msg(qi, qi->qf_curlist, "");
3888 qf_update_buffer(qi, NULL);
3889 }
3890 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003891 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003892
3893 return;
3894 }
3895
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003896 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003897 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003898 else
3899 for (i = 0; i < qi->qf_listcount; ++i)
3900 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3901}
3902
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003904 * Free all the entries in the error list "idx". Note that other information
3905 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 */
3907 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003908qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003910 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003911 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003912 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003914 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003916 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003917 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003918 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003919 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003920 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003921 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003922 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003923 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003924 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003925 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003926 // Somehow qf_count may have an incorrect value, set it to 1
3927 // to avoid crashing when it's wrong.
3928 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003929 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003930 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003931 qfl->qf_start = qfpnext;
3932 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003934
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003935 qfl->qf_index = 0;
3936 qfl->qf_start = NULL;
3937 qfl->qf_last = NULL;
3938 qfl->qf_ptr = NULL;
3939 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003940
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003941 qf_clean_dir_stack(&qfl->qf_dir_stack);
3942 qfl->qf_directory = NULL;
3943 qf_clean_dir_stack(&qfl->qf_file_stack);
3944 qfl->qf_currfile = NULL;
3945 qfl->qf_multiline = FALSE;
3946 qfl->qf_multiignore = FALSE;
3947 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948}
3949
3950/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003951 * Free error list "idx". Frees all the entries in the quickfix list,
3952 * associated context information and the title.
3953 */
3954 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003955qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003956{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003957 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003958
Bram Moolenaard23a8232018-02-10 18:45:26 +01003959 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003960 free_tv(qfl->qf_ctx);
3961 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01003962 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003963 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003964 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003965}
3966
3967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 * qf_mark_adjust: adjust marks
3969 */
3970 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003971qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003972 win_T *wp,
3973 linenr_T line1,
3974 linenr_T line2,
3975 long amount,
3976 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003978 int i;
3979 qfline_T *qfp;
3980 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003981 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003982 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003983 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaarc1542742016-07-20 21:44:37 +02003985 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003986 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003987 if (wp != NULL)
3988 {
3989 if (wp->w_llist == NULL)
3990 return;
3991 qi = wp->w_llist;
3992 }
3993
3994 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003995 {
3996 qf_list_T *qfl = qf_get_list(qi, idx);
3997
3998 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003999 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (qfp->qf_fnum == curbuf->b_fnum)
4001 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004002 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4004 {
4005 if (amount == MAXLNUM)
4006 qfp->qf_cleared = TRUE;
4007 else
4008 qfp->qf_lnum += amount;
4009 }
4010 else if (amount_after && qfp->qf_lnum > line2)
4011 qfp->qf_lnum += amount_after;
4012 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004013 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004014
4015 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004016 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017}
4018
4019/*
4020 * Make a nice message out of the error character and the error number:
4021 * char number message
4022 * e or E 0 " error"
4023 * w or W 0 " warning"
4024 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004025 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 * 0 0 ""
4027 * other 0 " c"
4028 * e or E n " error n"
4029 * w or W n " warning n"
4030 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004031 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 * 0 n " error n"
4033 * other n " c n"
4034 * 1 x "" :helpgrep
4035 */
4036 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004037qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
4039 static char_u buf[20];
4040 static char_u cc[3];
4041 char_u *p;
4042
4043 if (c == 'W' || c == 'w')
4044 p = (char_u *)" warning";
4045 else if (c == 'I' || c == 'i')
4046 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004047 else if (c == 'N' || c == 'n')
4048 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4050 p = (char_u *)" error";
4051 else if (c == 0 || c == 1)
4052 p = (char_u *)"";
4053 else
4054 {
4055 cc[0] = ' ';
4056 cc[1] = c;
4057 cc[2] = NUL;
4058 p = cc;
4059 }
4060
4061 if (nr <= 0)
4062 return p;
4063
4064 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4065 return buf;
4066}
4067
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004069 * When "split" is FALSE: Open the entry/result under the cursor.
4070 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4071 */
4072 void
4073qf_view_result(int split)
4074{
4075 qf_info_T *qi = &ql_info;
4076
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004077 if (IS_LL_WINDOW(curwin))
4078 qi = GET_LOC_LIST(curwin);
4079
Bram Moolenaar0398e002019-03-21 21:12:49 +01004080 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004081 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004082 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004083 return;
4084 }
4085
4086 if (split)
4087 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004088 // Open the selected entry in a new window
4089 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4090 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004091 return;
4092 }
4093
4094 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4095}
4096
4097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 * ":cwindow": open the quickfix window if we have errors to display,
4099 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004100 * ":lwindow": open the location list window if we have locations to display,
4101 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 */
4103 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004104ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004106 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004107 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 win_T *win;
4109
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004110 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4111 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004112
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004113 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004115 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004116 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004118 // If a quickfix window is open but we have no errors to display,
4119 // close the window. If a quickfix window is not open, then open
4120 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004121 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004122 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004123 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
4125 if (win != NULL)
4126 ex_cclose(eap);
4127 }
4128 else if (win == NULL)
4129 ex_copen(eap);
4130}
4131
4132/*
4133 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004134 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004137ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004139 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004140 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004142 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4143 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004145 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004146 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (win != NULL)
4148 win_close(win, FALSE);
4149}
4150
4151/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004152 * Set "w:quickfix_title" if "qi" has a title.
4153 */
4154 static void
4155qf_set_title_var(qf_list_T *qfl)
4156{
4157 if (qfl->qf_title != NULL)
4158 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4159}
4160
4161/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004162 * Goto a quickfix or location list window (if present).
4163 * Returns OK if the window is found, FAIL otherwise.
4164 */
4165 static int
4166qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4167{
4168 win_T *win;
4169
4170 win = qf_find_win(qi);
4171 if (win == NULL)
4172 return FAIL;
4173
4174 win_goto(win);
4175 if (resize)
4176 {
4177 if (vertsplit)
4178 {
4179 if (sz != win->w_width)
4180 win_setwidth(sz);
4181 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004182 else if (sz != win->w_height && win->w_height
4183 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004184 win_setheight(sz);
4185 }
4186
4187 return OK;
4188}
4189
4190/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004191 * Set options for the buffer in the quickfix or location list window.
4192 */
4193 static void
4194qf_set_cwindow_options(void)
4195{
4196 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004197 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4198 set_option_value_give_err((char_u *)"bt",
4199 0L, (char_u *)"quickfix", OPT_LOCAL);
4200 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004201 RESET_BINDING(curwin);
4202#ifdef FEAT_DIFF
4203 curwin->w_p_diff = FALSE;
4204#endif
4205#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004206 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004207 OPT_LOCAL);
4208#endif
4209}
4210
4211/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004212 * Open a new quickfix or location list window, load the quickfix buffer and
4213 * set the appropriate options for the window.
4214 * Returns FAIL if the window could not be opened.
4215 */
4216 static int
4217qf_open_new_cwindow(qf_info_T *qi, int height)
4218{
4219 buf_T *qf_buf;
4220 win_T *oldwin = curwin;
4221 tabpage_T *prevtab = curtab;
4222 int flags = 0;
4223 win_T *win;
4224
4225 qf_buf = qf_find_buf(qi);
4226
4227 // The current window becomes the previous window afterwards.
4228 win = curwin;
4229
Bram Moolenaare1004402020-10-24 20:49:43 +02004230 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004231 // Create the new quickfix window at the very bottom, except when
4232 // :belowright or :aboveleft is used.
4233 win_goto(lastwin);
4234 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004235 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004236 flags = WSP_BELOW;
4237 flags |= WSP_NEWLOC;
4238 if (win_split(height, flags) == FAIL)
4239 return FAIL; // not enough room for window
4240 RESET_BINDING(curwin);
4241
4242 if (IS_LL_STACK(qi))
4243 {
4244 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004245 // location list stack from the window 'win'.
4246 curwin->w_llist_ref = qi;
4247 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004248 }
4249
4250 if (oldwin != curwin)
4251 oldwin = NULL; // don't store info when in another window
4252 if (qf_buf != NULL)
4253 {
4254 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004255 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004256 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004257 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004258 }
4259 else
4260 {
4261 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004262 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4263 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004264 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004265
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004266 // save the number of the new buffer
4267 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004268 }
4269
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004270 // Set the options for the quickfix buffer/window (if not already done)
4271 // Do this even if the quickfix buffer was already present, as an autocmd
4272 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004273 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004274 qf_set_cwindow_options();
4275
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004276 // Only set the height when still in the same tab page and there is no
4277 // window to the side.
4278 if (curtab == prevtab && curwin->w_width == Columns)
4279 win_setheight(height);
4280 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4281 if (win_valid(win))
4282 prevwin = win;
4283
4284 return OK;
4285}
4286
4287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004289 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 */
4291 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004292ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004294 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004295 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004297 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004298 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004300 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4301 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004302
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004303 incr_quickfix_busy();
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (eap->addr_count != 0)
4306 height = eap->line2;
4307 else
4308 height = QF_WINHEIGHT;
4309
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004310 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#ifdef FEAT_GUI
4312 need_mouse_correct = TRUE;
4313#endif
4314
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004315 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004316 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004317 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004318 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004319 if (status == FAIL)
4320 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004321 {
4322 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004323 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004326 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004327 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004328 // Save the current index here, as updating the quickfix buffer may free
4329 // the quickfix list
4330 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004331
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004332 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004333 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004335 decr_quickfix_busy();
4336
4337 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 curwin->w_cursor.col = 0;
4339 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004340 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341}
4342
4343/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004344 * Move the cursor in the quickfix window to "lnum".
4345 */
4346 static void
4347qf_win_goto(win_T *win, linenr_T lnum)
4348{
4349 win_T *old_curwin = curwin;
4350
4351 curwin = win;
4352 curbuf = win->w_buffer;
4353 curwin->w_cursor.lnum = lnum;
4354 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004355 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004356 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004357 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004358 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004359 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004360 curwin = old_curwin;
4361 curbuf = curwin->w_buffer;
4362}
4363
4364/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004365 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004366 */
4367 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004368ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004369{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004370 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004371 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004372
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004373 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4374 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004375
4376 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004377 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4378 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4379}
4380
4381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 * Return the number of the current entry (line number in the quickfix
4383 * window).
4384 */
4385 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004386qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004388 qf_info_T *qi = &ql_info;
4389
4390 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004391 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004392 qi = wp->w_llist_ref;
4393
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004394 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395}
4396
4397/*
4398 * Update the cursor position in the quickfix window to the current error.
4399 * Return TRUE if there is a quickfix window.
4400 */
4401 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004402qf_win_pos_update(
4403 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004404 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
4406 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004407 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004409 // Put the cursor on the current error in the quickfix window, so that
4410 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004411 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (win != NULL
4413 && qf_index <= win->w_buffer->b_ml.ml_line_count
4414 && old_qf_index != qf_index)
4415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if (qf_index > old_qf_index)
4417 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004418 win->w_redraw_top = old_qf_index;
4419 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421 else
4422 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004423 win->w_redraw_top = qf_index;
4424 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004426 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 return win != NULL;
4429}
4430
4431/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004432 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004433 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004434 */
4435 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004436is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004437{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004438 // A window displaying the quickfix buffer will have the w_llist_ref field
4439 // set to NULL.
4440 // A window displaying a location list buffer will have the w_llist_ref
4441 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004442 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004443 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4444 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004445 return TRUE;
4446
4447 return FALSE;
4448}
4449
4450/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004451 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4452 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004453 */
4454 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004455qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004456{
4457 win_T *win;
4458
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004459 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004460 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004461 return win;
4462 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004463}
4464
4465/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004466 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004467 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 */
4469 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004470qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004472 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004473 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004475 if (qi->qf_bufnr != INVALID_QFBUFNR)
4476 {
4477 buf_T *qfbuf;
4478 qfbuf = buflist_findnr(qi->qf_bufnr);
4479 if (qfbuf != NULL)
4480 return qfbuf;
4481 // buffer is no longer present
4482 qi->qf_bufnr = INVALID_QFBUFNR;
4483 }
4484
Bram Moolenaar9c102382006-05-03 21:26:49 +00004485 FOR_ALL_TAB_WINDOWS(tp, win)
4486 if (is_qf_win(win, qi))
4487 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004488
Bram Moolenaar9c102382006-05-03 21:26:49 +00004489 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490}
4491
4492/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004493 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004494 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004495 */
4496 int
4497qf_process_qftf_option(void)
4498{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004499 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004500}
4501
4502/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004503 * Update the w:quickfix_title variable in the quickfix/location list window in
4504 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004505 */
4506 static void
4507qf_update_win_titlevar(qf_info_T *qi)
4508{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004509 qf_list_T *qfl = qf_get_curlist(qi);
4510 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004511 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004512 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004513
Bram Moolenaar530bed92020-12-16 21:02:56 +01004514 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004515 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004516 if (is_qf_win(win, qi))
4517 {
4518 curwin = win;
4519 qf_set_title_var(qfl);
4520 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004521 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004522 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004523}
4524
4525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 * Find the quickfix buffer. If it exists, update the contents.
4527 */
4528 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004529qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530{
4531 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004532 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004535 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004536 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (buf != NULL)
4538 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004539 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004540 int qf_winid = 0;
4541
4542 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004543 {
4544 if (curwin->w_llist == qi)
4545 win = curwin;
4546 else
4547 {
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004548 // Find the file window (non-quickfix) with this location list
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004549 win = qf_find_win_with_loclist(qi);
4550 if (win == NULL)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004551 // File window is not found. Find the location list window.
4552 win = qf_find_win(qi);
4553 if (win == NULL)
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004554 return;
4555 }
4556 qf_winid = win->w_id;
4557 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004558
4559 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004560 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004561 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaard823fa92016-08-12 16:29:27 +02004563 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004564
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004565 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004566 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004567
Bram Moolenaar864293a2016-06-02 13:40:04 +02004568 if (old_last == NULL)
4569 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004570 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004571
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004572 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004573 aucmd_restbuf(&aco);
4574 }
4575
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004576 // Only redraw when added lines are visible. This avoids flickering
4577 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004578 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004579 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581}
4582
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004583/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004584 * Add an error line to the quickfix buffer.
4585 */
4586 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004587qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004588 buf_T *buf, // quickfix window buffer
4589 linenr_T lnum,
4590 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004591 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004592 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004593 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004594{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004595 buf_T *errbuf;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004596 garray_T ga;
4597
4598 ga_init2(&ga, 1, 256);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004599
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004600 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004601 // for this entry, then use it.
4602 if (qftf_str != NULL && *qftf_str != NUL)
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004603 ga_concat(&ga, qftf_str);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004604 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004605 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004606 if (qfp->qf_module != NULL)
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004607 ga_concat(&ga, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004608 else if (qfp->qf_fnum != 0
4609 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4610 && errbuf->b_fname != NULL)
4611 {
4612 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004613 ga_concat(&ga, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02004614 else
4615 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004616 // Shorten the file name if not done already.
4617 // For optimization, do this only for the first entry in a
4618 // buffer.
4619 if (first_bufline && (errbuf->b_sfname == NULL
4620 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004621 {
4622 if (*dirname == NUL)
4623 mch_dirname(dirname, MAXPATHL);
4624 shorten_buf_fname(errbuf, dirname, FALSE);
4625 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004626 ga_concat(&ga, errbuf->b_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004627 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004628 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004629
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004630 ga_append(&ga, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02004631
4632 if (qfp->qf_lnum > 0)
4633 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004634 qf_range_text(&ga, qfp);
4635 ga_concat(&ga, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004636 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004637 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004638 qf_fmt_text(&ga, qfp->qf_pattern);
4639 ga_append(&ga, '|');
4640 ga_append(&ga, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004641
Bram Moolenaar858ba062020-05-31 23:11:59 +02004642 // Remove newlines and leading whitespace from the text.
4643 // For an unrecognized line keep the indent, the compiler may
4644 // mark a word with ^^^^.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004645 qf_fmt_text(&ga, ga.ga_len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004646 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004647
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004648 ga_append(&ga, NUL);
4649
4650 if (ml_append_buf(buf, lnum, ga.ga_data, ga.ga_len + 1, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004651 return FAIL;
4652
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004653 ga_clear(&ga);
4654
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004655 return OK;
4656}
4657
Bram Moolenaard43906d2020-07-20 21:31:32 +02004658/*
4659 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4660 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4661 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004662 static list_T *
4663call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4664{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004665 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004666 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004667 static int recursive = FALSE;
4668
4669 if (recursive)
4670 return NULL; // this doesn't work properly recursively
4671 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004672
4673 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4674 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4675 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004676 if (qfl->qf_qftf_cb.cb_name != NULL)
4677 cb = &qfl->qf_qftf_cb;
4678 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004679 {
4680 typval_T args[1];
4681 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004682 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004683
4684 // create the dict argument
4685 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01004686 {
4687 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004688 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004689 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004690 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4691 dict_add_number(d, "winid", (long)qf_winid);
4692 dict_add_number(d, "id", (long)qfl->qf_id);
4693 dict_add_number(d, "start_idx", start_idx);
4694 dict_add_number(d, "end_idx", end_idx);
4695 ++d->dv_refcount;
4696 args[0].v_type = VAR_DICT;
4697 args[0].vval.v_dict = d;
4698
Bram Moolenaard43906d2020-07-20 21:31:32 +02004699 qftf_list = NULL;
4700 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4701 {
4702 if (rettv.v_type == VAR_LIST)
4703 {
4704 qftf_list = rettv.vval.v_list;
4705 qftf_list->lv_refcount++;
4706 }
4707 clear_tv(&rettv);
4708 }
4709 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004710 }
4711
Bram Moolenaard6c67622022-08-24 20:07:22 +01004712 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004713 return qftf_list;
4714}
4715
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 * Fill current buffer with quickfix errors, replacing any previous contents.
4718 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004719 * If "old_last" is not NULL append the items after this one.
4720 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4721 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 */
4723 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004724qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004726 linenr_T lnum;
4727 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004728 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004729 list_T *qftf_list = NULL;
4730 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
Bram Moolenaar864293a2016-06-02 13:40:04 +02004732 if (old_last == NULL)
4733 {
4734 if (buf != curbuf)
4735 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004736 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004737 return;
4738 }
4739
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004740 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004741 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004742 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004745 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004746 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004748 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004749 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004750 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004751
4752 *dirname = NUL;
4753
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004754 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004755 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004756 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004757 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004758 lnum = 0;
4759 }
4760 else
4761 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004762 if (old_last->qf_next != NULL)
4763 qfp = old_last->qf_next;
4764 else
4765 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004766 lnum = buf->b_ml.ml_line_count;
4767 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004768
4769 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4770 (long)qfl->qf_count);
4771 if (qftf_list != NULL)
4772 qftf_li = qftf_list->lv_first;
4773
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004774 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004776 char_u *qftf_str = NULL;
4777
Bram Moolenaard43906d2020-07-20 21:31:32 +02004778 // Use the text supplied by the user defined function (if any).
4779 // If the returned value is not string, then ignore the rest
4780 // of the returned values and use the default.
4781 if (qftf_li != NULL && !invalid_val)
4782 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004783 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004784 if (qftf_str == NULL)
4785 invalid_val = TRUE;
4786 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004787
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004788 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4789 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004791
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004792 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004793 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004795 if (qfp == NULL)
4796 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004797
4798 if (qftf_li != NULL)
4799 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004801
4802 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004803 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004804 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
4806
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004807 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 check_lnums(TRUE);
4809
Bram Moolenaar864293a2016-06-02 13:40:04 +02004810 if (old_last == NULL)
4811 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004812 // Set the 'filetype' to "qf" each time after filling the buffer.
4813 // This resembles reading a file into a buffer, it's more logical when
4814 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004815 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004816 set_option_value_give_err((char_u *)"ft",
4817 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004818 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004820 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004821 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004823 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004825 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004826 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004827
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004828 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004829 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004832 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 KeyTyped = old_KeyTyped;
4834}
4835
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004836/*
4837 * For every change made to the quickfix list, update the changed tick.
4838 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004839 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004840qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004841{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004842 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004843}
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004846 * Return the quickfix/location list number with the given identifier.
4847 * Returns -1 if list is not found.
4848 */
4849 static int
4850qf_id2nr(qf_info_T *qi, int_u qfid)
4851{
4852 int qf_idx;
4853
4854 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4855 if (qi->qf_lists[qf_idx].qf_id == qfid)
4856 return qf_idx;
4857 return INVALID_QFIDX;
4858}
4859
4860/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004861 * If the current list is not "save_qfid" and we can find the list with that ID
4862 * then make it the current list.
4863 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004864 * Returns OK if successfully restored the list. Returns FAIL if the list with
4865 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004866 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004867 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004868qf_restore_list(qf_info_T *qi, int_u save_qfid)
4869{
4870 int curlist;
4871
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004872 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004873 {
4874 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004875 if (curlist < 0)
4876 // list is not present
4877 return FAIL;
4878 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004879 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004880 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004881}
4882
4883/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004884 * Jump to the first entry if there is one.
4885 */
4886 static void
4887qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4888{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004889 if (qf_restore_list(qi, save_qfid) == FAIL)
4890 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004891
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004892 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004893 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004894 qf_jump(qi, 0, 0, forceit);
4895}
4896
4897/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004898 * Return TRUE when using ":vimgrep" for ":grep".
4899 */
4900 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004901grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004902{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004903 return ((cmdidx == CMD_grep
4904 || cmdidx == CMD_lgrep
4905 || cmdidx == CMD_grepadd
4906 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004907 && STRCMP("internal",
4908 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4909}
4910
4911/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004912 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004914 static char_u *
4915make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004917 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004918 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004919 case CMD_make: return (char_u *)"make";
4920 case CMD_lmake: return (char_u *)"lmake";
4921 case CMD_grep: return (char_u *)"grep";
4922 case CMD_lgrep: return (char_u *)"lgrep";
4923 case CMD_grepadd: return (char_u *)"grepadd";
4924 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4925 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927}
4928
4929/*
4930 * Return the name for the errorfile, in allocated memory.
4931 * Find a new unique name when 'makeef' contains "##".
4932 * Returns NULL for error.
4933 */
4934 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004935get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 char_u *p;
4938 char_u *name;
4939 static int start = -1;
4940 static int off = 0;
4941#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004942 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943#endif
4944
4945 if (*p_mef == NUL)
4946 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004947 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004949 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 return name;
4951 }
4952
4953 for (p = p_mef; *p; ++p)
4954 if (p[0] == '#' && p[1] == '#')
4955 break;
4956
4957 if (*p == NUL)
4958 return vim_strsave(p_mef);
4959
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004960 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 for (;;)
4962 {
4963 if (start == -1)
4964 start = mch_get_pid();
4965 else
4966 off += 19;
4967
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004968 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (name == NULL)
4970 break;
4971 STRCPY(name, p_mef);
4972 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4973 STRCAT(name, p + 2);
4974 if (mch_getperm(name) < 0
4975#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004976 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 && mch_lstat((char *)name, &sb) < 0
4978#endif
4979 )
4980 break;
4981 vim_free(name);
4982 }
4983 return name;
4984}
4985
4986/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004987 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4988 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4989 */
4990 static char_u *
4991make_get_fullcmd(char_u *makecmd, char_u *fname)
4992{
4993 char_u *cmd;
4994 unsigned len;
4995
4996 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4997 if (*p_sp != NUL)
4998 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00004999 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005000 if (cmd == NULL)
5001 return NULL;
5002 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5003 (char *)p_shq);
5004
5005 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5006 if (*p_sp != NUL)
5007 append_redir(cmd, len, p_sp, fname);
5008
5009 // Display the fully formed command. Output a newline if there's something
5010 // else than the :make command that was typed (in which case the cursor is
5011 // in column 0).
5012 if (msg_col == 0)
5013 msg_didout = FALSE;
5014 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005015 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005016 msg_outtrans(cmd); // show what we are doing
5017
5018 return cmd;
5019}
5020
5021/*
5022 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5023 */
5024 void
5025ex_make(exarg_T *eap)
5026{
5027 char_u *fname;
5028 char_u *cmd;
5029 char_u *enc = NULL;
5030 win_T *wp = NULL;
5031 qf_info_T *qi = &ql_info;
5032 int res;
5033 char_u *au_name = NULL;
5034 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005035 char_u *errorformat = p_efm;
5036 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005037
5038 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5039 if (grep_internal(eap->cmdidx))
5040 {
5041 ex_vimgrep(eap);
5042 return;
5043 }
5044
5045 au_name = make_get_auname(eap->cmdidx);
5046 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5047 curbuf->b_fname, TRUE, curbuf))
5048 {
5049#ifdef FEAT_EVAL
5050 if (aborting())
5051 return;
5052#endif
5053 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005054 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005055
5056 if (is_loclist_cmd(eap->cmdidx))
5057 wp = curwin;
5058
5059 autowrite_all();
5060 fname = get_mef_name();
5061 if (fname == NULL)
5062 return;
5063 mch_remove(fname); // in case it's not unique
5064
5065 cmd = make_get_fullcmd(eap->arg, fname);
5066 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005067 {
5068 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005069 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005070 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005071
5072 // let the shell know if we are redirecting output or not
5073 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5074
5075#ifdef AMIGA
5076 out_flush();
5077 // read window status report and redraw before message
5078 (void)char_avail();
5079#endif
5080
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005081 incr_quickfix_busy();
5082
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005083 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5084 errorformat = p_gefm;
5085 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5086 newlist = FALSE;
5087
5088 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5089 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005090 if (wp != NULL)
5091 {
5092 qi = GET_LOC_LIST(wp);
5093 if (qi == NULL)
5094 goto cleanup;
5095 }
5096 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005097 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005098
5099 // Remember the current quickfix list identifier, so that we can
5100 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005101 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005102 if (au_name != NULL)
5103 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5104 curbuf->b_fname, TRUE, curbuf);
5105 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5106 // display the first error
5107 qf_jump_first(qi, save_qfid, FALSE);
5108
5109cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005110 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005111 mch_remove(fname);
5112 vim_free(fname);
5113 vim_free(cmd);
5114}
5115
5116/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005117 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005118 */
5119 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005120qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005121{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005122 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005123
5124 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5125 return 0;
5126 return qf_get_curlist(qi)->qf_count;
5127}
5128
5129/*
5130 * Returns the number of valid entries in the current quickfix/location list.
5131 */
5132 int
5133qf_get_valid_size(exarg_T *eap)
5134{
5135 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005136 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005137 qfline_T *qfp;
5138 int i, sz = 0;
5139 int prev_fnum = 0;
5140
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005141 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5142 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005143
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005144 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005145 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005146 {
5147 if (qfp->qf_valid)
5148 {
5149 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005150 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005151 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5152 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005153 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005154 sz++;
5155 prev_fnum = qfp->qf_fnum;
5156 }
5157 }
5158 }
5159
5160 return sz;
5161}
5162
5163/*
5164 * Returns the current index of the quickfix/location list.
5165 * Returns 0 if there is an error.
5166 */
5167 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005168qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005169{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005170 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005171
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005172 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5173 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005174
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005175 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005176}
5177
5178/*
5179 * Returns the current index in the quickfix/location list (counting only valid
5180 * entries). If no valid entries are in the list, then returns 1.
5181 */
5182 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005183qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005184{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005185 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005186 qf_list_T *qfl;
5187 qfline_T *qfp;
5188 int i, eidx = 0;
5189 int prev_fnum = 0;
5190
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005191 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5192 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005193
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005194 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005195 qfp = qfl->qf_start;
5196
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005197 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005198 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005199 return 1;
5200
5201 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5202 {
5203 if (qfp->qf_valid)
5204 {
5205 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5206 {
5207 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5208 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005209 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005210 eidx++;
5211 prev_fnum = qfp->qf_fnum;
5212 }
5213 }
5214 else
5215 eidx++;
5216 }
5217 }
5218
5219 return eidx ? eidx : 1;
5220}
5221
5222/*
5223 * Get the 'n'th valid error entry in the quickfix or location list.
5224 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5225 * For :cdo and :ldo returns the 'n'th valid error entry.
5226 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5227 */
5228 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005229qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005230{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005231 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005232 int i, eidx;
5233 int prev_fnum = 0;
5234
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005235 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005236 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005237 return 1;
5238
Bram Moolenaar95946f12019-03-31 15:31:59 +02005239 eidx = 0;
5240 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005241 {
5242 if (qfp->qf_valid)
5243 {
5244 if (fdo)
5245 {
5246 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5247 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005248 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005249 eidx++;
5250 prev_fnum = qfp->qf_fnum;
5251 }
5252 }
5253 else
5254 eidx++;
5255 }
5256
5257 if (eidx == n)
5258 break;
5259 }
5260
5261 if (i <= qfl->qf_count)
5262 return i;
5263 else
5264 return 1;
5265}
5266
5267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005269 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005270 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 */
5272 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005273ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005275 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005276 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005277
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005278 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5279 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005280
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005281 if (eap->addr_count > 0)
5282 errornr = (int)eap->line2;
5283 else
5284 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005285 switch (eap->cmdidx)
5286 {
5287 case CMD_cc: case CMD_ll:
5288 errornr = 0;
5289 break;
5290 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5291 case CMD_lfirst:
5292 errornr = 1;
5293 break;
5294 default:
5295 errornr = 32767;
5296 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005297 }
5298
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005299 // For cdo and ldo commands, jump to the nth valid error.
5300 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005301 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5302 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005303 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005304 eap->addr_count > 0 ? (int)eap->line1 : 1,
5305 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5306
5307 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308}
5309
5310/*
5311 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005312 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005313 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 */
5315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005316ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005318 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005319 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005320 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005321
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005322 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5323 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005324
Bram Moolenaar55b69262017-08-13 13:42:01 +02005325 if (eap->addr_count > 0
5326 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5327 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005328 errornr = (int)eap->line2;
5329 else
5330 errornr = 1;
5331
Bram Moolenaar39665952018-08-15 20:59:48 +02005332 // Depending on the command jump to either next or previous entry/file.
5333 switch (eap->cmdidx)
5334 {
5335 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5336 dir = FORWARD;
5337 break;
5338 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5339 case CMD_lNext:
5340 dir = BACKWARD;
5341 break;
5342 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5343 dir = FORWARD_FILE;
5344 break;
5345 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5346 dir = BACKWARD_FILE;
5347 break;
5348 default:
5349 dir = FORWARD;
5350 break;
5351 }
5352
5353 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354}
5355
5356/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005357 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5358 * The index of the entry is stored in 'errornr'.
5359 * Returns NULL if an entry is not found.
5360 */
5361 static qfline_T *
5362qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5363{
5364 qfline_T *qfp = NULL;
5365 int idx = 0;
5366
5367 // Find the first entry in this file
5368 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5369 if (qfp->qf_fnum == bnr)
5370 break;
5371
5372 *errornr = idx;
5373 return qfp;
5374}
5375
5376/*
5377 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5378 * with the error number for the first entry. Assumes the entries are sorted in
5379 * the quickfix list by line number.
5380 */
5381 static qfline_T *
5382qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5383{
5384 while (!got_int
5385 && entry->qf_prev != NULL
5386 && entry->qf_fnum == entry->qf_prev->qf_fnum
5387 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5388 {
5389 entry = entry->qf_prev;
5390 --*errornr;
5391 }
5392
5393 return entry;
5394}
5395
5396/*
5397 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5398 * with the error number for the last entry. Assumes the entries are sorted in
5399 * the quickfix list by line number.
5400 */
5401 static qfline_T *
5402qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5403{
5404 while (!got_int &&
5405 entry->qf_next != NULL
5406 && entry->qf_fnum == entry->qf_next->qf_fnum
5407 && entry->qf_lnum == entry->qf_next->qf_lnum)
5408 {
5409 entry = entry->qf_next;
5410 ++*errornr;
5411 }
5412
5413 return entry;
5414}
5415
5416/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005417 * Returns TRUE if the specified quickfix entry is
5418 * after the given line (linewise is TRUE)
5419 * or after the line and column.
5420 */
5421 static int
5422qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5423{
5424 if (linewise)
5425 return qfp->qf_lnum > pos->lnum;
5426 else
5427 return (qfp->qf_lnum > pos->lnum ||
5428 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5429}
5430
5431/*
5432 * Returns TRUE if the specified quickfix entry is
5433 * before the given line (linewise is TRUE)
5434 * or before the line and column.
5435 */
5436 static int
5437qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5438{
5439 if (linewise)
5440 return qfp->qf_lnum < pos->lnum;
5441 else
5442 return (qfp->qf_lnum < pos->lnum ||
5443 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5444}
5445
5446/*
5447 * Returns TRUE if the specified quickfix entry is
5448 * on or after the given line (linewise is TRUE)
5449 * or on or after the line and column.
5450 */
5451 static int
5452qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5453{
5454 if (linewise)
5455 return qfp->qf_lnum >= pos->lnum;
5456 else
5457 return (qfp->qf_lnum > pos->lnum ||
5458 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5459}
5460
5461/*
5462 * Returns TRUE if the specified quickfix entry is
5463 * on or before the given line (linewise is TRUE)
5464 * or on or before the line and column.
5465 */
5466 static int
5467qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5468{
5469 if (linewise)
5470 return qfp->qf_lnum <= pos->lnum;
5471 else
5472 return (qfp->qf_lnum < pos->lnum ||
5473 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5474}
5475
5476/*
5477 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5478 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5479 * multiple entries on a single line as one. Otherwise returns the entry after
5480 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005481 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5482 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005483 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005484 */
5485 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005486qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005487 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005488 pos_T *pos,
5489 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005490 qfline_T *qfp,
5491 int *errornr)
5492{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005493 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005494 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005495 return qfp;
5496
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005497 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005498 while (qfp->qf_next != NULL
5499 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005500 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005501 {
5502 qfp = qfp->qf_next;
5503 ++*errornr;
5504 }
5505
5506 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005507 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005508 return NULL;
5509
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005510 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005511 qfp = qfp->qf_next;
5512 ++*errornr;
5513
5514 return qfp;
5515}
5516
5517/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005518 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5519 * If 'linewise' is TRUE, returns the entry before the specified line and
5520 * treats multiple entries on a single line as one. Otherwise returns the entry
5521 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005522 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5523 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005524 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005525 */
5526 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005527qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005528 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005529 pos_T *pos,
5530 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005531 qfline_T *qfp,
5532 int *errornr)
5533{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005534 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005535 while (qfp->qf_next != NULL
5536 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005537 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005538 {
5539 qfp = qfp->qf_next;
5540 ++*errornr;
5541 }
5542
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005543 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005544 return NULL;
5545
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005546 if (linewise)
5547 // If multiple entries are on the same line, then use the first entry
5548 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005549
5550 return qfp;
5551}
5552
5553/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005554 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005555 * the direction 'dir'.
5556 */
5557 static qfline_T *
5558qf_find_closest_entry(
5559 qf_list_T *qfl,
5560 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005561 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005562 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005563 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005564 int *errornr)
5565{
5566 qfline_T *qfp;
5567
5568 *errornr = 0;
5569
5570 // Find the first entry in this file
5571 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5572 if (qfp == NULL)
5573 return NULL; // no entry in this file
5574
5575 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005576 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005577 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005578 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005579
5580 return qfp;
5581}
5582
5583/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005584 * Get the nth quickfix entry below the specified entry. Searches forward in
5585 * the list. If linewise is TRUE, then treat multiple entries on a single line
5586 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005587 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005588 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005589qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005590{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005591 qfline_T *entry = entry_arg;
5592
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005593 while (n-- > 0 && !got_int)
5594 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005595 int first_errornr = *errornr;
5596
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005597 if (linewise)
5598 // Treat all the entries on the same line in this file as one
5599 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005600
5601 if (entry->qf_next == NULL
5602 || entry->qf_next->qf_fnum != entry->qf_fnum)
5603 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005604 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005605 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005606 break;
5607 }
5608
5609 entry = entry->qf_next;
5610 ++*errornr;
5611 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005612}
5613
5614/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005615 * Get the nth quickfix entry above the specified entry. Searches backwards in
5616 * the list. If linewise is TRUE, then treat multiple entries on a single line
5617 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005618 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005619 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005620qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005621{
5622 while (n-- > 0 && !got_int)
5623 {
5624 if (entry->qf_prev == NULL
5625 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5626 break;
5627
5628 entry = entry->qf_prev;
5629 --*errornr;
5630
5631 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005632 if (linewise)
5633 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005634 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005635}
5636
5637/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005638 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5639 * the specified direction. Returns the error number in the quickfix list or 0
5640 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005641 */
5642 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005643qf_find_nth_adj_entry(
5644 qf_list_T *qfl,
5645 int bnr,
5646 pos_T *pos,
5647 int n,
5648 int dir,
5649 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005650{
5651 qfline_T *adj_entry;
5652 int errornr;
5653
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005654 // Find an entry closest to the specified position
5655 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005656 if (adj_entry == NULL)
5657 return 0;
5658
5659 if (--n > 0)
5660 {
5661 // Go to the n'th entry in the current buffer
5662 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005663 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005664 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005665 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005666 }
5667
5668 return errornr;
5669}
5670
5671/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005672 * Jump to a quickfix entry in the current file nearest to the current line or
5673 * current line/col.
5674 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5675 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005676 */
5677 void
5678ex_cbelow(exarg_T *eap)
5679{
5680 qf_info_T *qi;
5681 qf_list_T *qfl;
5682 int dir;
5683 int buf_has_flag;
5684 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005685 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005686
5687 if (eap->addr_count > 0 && eap->line2 <= 0)
5688 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005689 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005690 return;
5691 }
5692
5693 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005694 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5695 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005696 buf_has_flag = BUF_HAS_QF_ENTRY;
5697 else
5698 buf_has_flag = BUF_HAS_LL_ENTRY;
5699 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5700 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005701 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005702 return;
5703 }
5704
5705 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5706 return;
5707
5708 qfl = qf_get_curlist(qi);
5709 // check if the list has valid errors
5710 if (!qf_list_has_valid_entries(qfl))
5711 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005712 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005713 return;
5714 }
5715
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005716 if (eap->cmdidx == CMD_cbelow
5717 || eap->cmdidx == CMD_lbelow
5718 || eap->cmdidx == CMD_cafter
5719 || eap->cmdidx == CMD_lafter)
5720 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005721 dir = FORWARD;
5722 else
5723 dir = BACKWARD;
5724
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005725 pos = curwin->w_cursor;
5726 // A quickfix entry column number is 1 based whereas cursor column
5727 // number is 0 based. Adjust the column number.
5728 pos.col++;
5729 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5730 eap->addr_count > 0 ? eap->line2 : 0, dir,
5731 eap->cmdidx == CMD_cbelow
5732 || eap->cmdidx == CMD_lbelow
5733 || eap->cmdidx == CMD_cabove
5734 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005735
5736 if (errornr > 0)
5737 qf_jump(qi, 0, errornr, FALSE);
5738 else
5739 emsg(_(e_no_more_items));
5740}
5741
5742/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005743 * Return the autocmd name for the :cfile Ex commands
5744 */
5745 static char_u *
5746cfile_get_auname(cmdidx_T cmdidx)
5747{
5748 switch (cmdidx)
5749 {
5750 case CMD_cfile: return (char_u *)"cfile";
5751 case CMD_cgetfile: return (char_u *)"cgetfile";
5752 case CMD_caddfile: return (char_u *)"caddfile";
5753 case CMD_lfile: return (char_u *)"lfile";
5754 case CMD_lgetfile: return (char_u *)"lgetfile";
5755 case CMD_laddfile: return (char_u *)"laddfile";
5756 default: return NULL;
5757 }
5758}
5759
5760/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005761 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005762 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 */
5764 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005765ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005767 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005768 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005769 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005770 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005771 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005772 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005773
Bram Moolenaara16123a2019-03-28 20:31:07 +01005774 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005775 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5776 NULL, FALSE, curbuf))
5777 {
5778#ifdef FEAT_EVAL
5779 if (aborting())
5780 return;
5781#endif
5782 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005783
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005784 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005785#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005786 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005787 {
5788 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005789 NULL, NULL,
5790 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005791 if (browse_file == NULL)
5792 return;
5793 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5794 vim_free(browse_file);
5795 }
5796 else
5797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005799 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005800
Bram Moolenaar39665952018-08-15 20:59:48 +02005801 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005802 wp = curwin;
5803
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005804 incr_quickfix_busy();
5805
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005806 // This function is used by the :cfile, :cgetfile and :caddfile
5807 // commands.
5808 // :cfile always creates a new quickfix list and jumps to the
5809 // first error.
5810 // :cgetfile creates a new quickfix list but doesn't jump to the
5811 // first error.
5812 // :caddfile adds to an existing quickfix list. If there is no
5813 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005814 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005815 && eap->cmdidx != CMD_laddfile),
5816 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005817 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005818 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005819 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005820 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005821 {
5822 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005823 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005824 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005825 }
5826 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005827 qf_list_changed(qf_get_curlist(qi));
5828 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005829 if (au_name != NULL)
5830 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005831
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005832 // Jump to the first error for a new list and if autocmds didn't
5833 // free the list.
5834 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5835 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005836 // display the first error
5837 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005838
5839 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005840}
5841
5842/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005843 * Return the vimgrep autocmd name.
5844 */
5845 static char_u *
5846vgr_get_auname(cmdidx_T cmdidx)
5847{
5848 switch (cmdidx)
5849 {
5850 case CMD_vimgrep: return (char_u *)"vimgrep";
5851 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5852 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5853 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5854 case CMD_grep: return (char_u *)"grep";
5855 case CMD_lgrep: return (char_u *)"lgrep";
5856 case CMD_grepadd: return (char_u *)"grepadd";
5857 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5858 default: return NULL;
5859 }
5860}
5861
5862/*
5863 * Initialize the regmatch used by vimgrep for pattern "s".
5864 */
5865 static void
5866vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5867{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005868 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005869 regmatch->regprog = NULL;
5870
5871 if (s == NULL || *s == NUL)
5872 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005873 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005874 if (last_search_pat() == NULL)
5875 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005876 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005877 return;
5878 }
5879 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5880 }
5881 else
5882 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5883
5884 regmatch->rmm_ic = p_ic;
5885 regmatch->rmm_maxcol = 0;
5886}
5887
5888/*
5889 * Display a file name when vimgrep is running.
5890 */
5891 static void
5892vgr_display_fname(char_u *fname)
5893{
5894 char_u *p;
5895
5896 msg_start();
5897 p = msg_strtrunc(fname, TRUE);
5898 if (p == NULL)
5899 msg_outtrans(fname);
5900 else
5901 {
5902 msg_outtrans(p);
5903 vim_free(p);
5904 }
5905 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005906 msg_didout = FALSE; // overwrite this message
5907 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005908 msg_col = 0;
5909 out_flush();
5910}
5911
5912/*
5913 * Load a dummy buffer to search for a pattern using vimgrep.
5914 */
5915 static buf_T *
5916vgr_load_dummy_buf(
5917 char_u *fname,
5918 char_u *dirname_start,
5919 char_u *dirname_now)
5920{
5921 int save_mls;
5922#if defined(FEAT_SYN_HL)
5923 char_u *save_ei = NULL;
5924#endif
5925 buf_T *buf;
5926
5927#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005928 // Don't do Filetype autocommands to avoid loading syntax and
5929 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005930 save_ei = au_event_disable(",Filetype");
5931#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005932 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005933 save_mls = p_mls;
5934 p_mls = 0;
5935
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005936 // Load file into a buffer, so that 'fileencoding' is detected,
5937 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005938 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5939
5940 p_mls = save_mls;
5941#if defined(FEAT_SYN_HL)
5942 au_event_restore(save_ei);
5943#endif
5944
5945 return buf;
5946}
5947
5948/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005949 * Check whether a quickfix/location list is valid. Autocmds may remove or
5950 * change a quickfix list when vimgrep is running. If the list is not found,
5951 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005952 */
5953 static int
5954vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005955 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005956 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005957 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005958 char_u *title)
5959{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005960 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005961 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005962 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005963 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005964 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005965 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005966 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005967 return FALSE;
5968 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005969 else
5970 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005971 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005972 qf_new_list(qi, title);
5973 return TRUE;
5974 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005975 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005976
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005977 if (qf_restore_list(qi, qfid) == FAIL)
5978 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005979
5980 return TRUE;
5981}
5982
5983/*
5984 * Search for a pattern in all the lines in a buffer and add the matching lines
5985 * to a quickfix list.
5986 */
5987 static int
5988vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005989 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005990 char_u *fname,
5991 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005992 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005993 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005994 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005995 int duplicate_name,
5996 int flags)
5997{
5998 int found_match = FALSE;
5999 long lnum;
6000 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006001 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006002
Bram Moolenaar1c299432018-10-28 14:36:09 +01006003 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006004 {
6005 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006006 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006007 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006008 // Regular expression match
6009 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Paul Ollis65745772022-06-05 16:55:54 +01006010 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006011 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006012 // Pass the buffer number so that it gets used even for a
6013 // dummy buffer, unless duplicate_name is set, then the
6014 // buffer will be wiped out below.
6015 if (qf_add_entry(qfl,
6016 NULL, // dir
6017 fname,
6018 NULL,
6019 duplicate_name ? 0 : buf->b_fnum,
6020 ml_get_buf(buf,
6021 regmatch->startpos[0].lnum + lnum, FALSE),
6022 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006023 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006024 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006025 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006026 FALSE, // vis_col
6027 NULL, // search pattern
6028 0, // nr
6029 0, // type
6030 TRUE // valid
6031 ) == QF_FAIL)
6032 {
6033 got_int = TRUE;
6034 break;
6035 }
6036 found_match = TRUE;
6037 if (--*tomatch == 0)
6038 break;
6039 if ((flags & VGR_GLOBAL) == 0
6040 || regmatch->endpos[0].lnum > 0)
6041 break;
6042 col = regmatch->endpos[0].col
6043 + (col == regmatch->endpos[0].col);
6044 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6045 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006046 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006047 }
6048 else
6049 {
6050 char_u *str = ml_get_buf(buf, lnum, FALSE);
6051 int score;
6052 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006053 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006054
6055 // Fuzzy string match
6056 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6057 {
6058 // Pass the buffer number so that it gets used even for a
6059 // dummy buffer, unless duplicate_name is set, then the
6060 // buffer will be wiped out below.
6061 if (qf_add_entry(qfl,
6062 NULL, // dir
6063 fname,
6064 NULL,
6065 duplicate_name ? 0 : buf->b_fnum,
6066 str,
6067 lnum,
thinca6864efa2021-06-19 20:45:20 +02006068 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006069 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006070 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006071 FALSE, // vis_col
6072 NULL, // search pattern
6073 0, // nr
6074 0, // type
6075 TRUE // valid
6076 ) == QF_FAIL)
6077 {
6078 got_int = TRUE;
6079 break;
6080 }
6081 found_match = TRUE;
6082 if (--*tomatch == 0)
6083 break;
6084 if ((flags & VGR_GLOBAL) == 0)
6085 break;
6086 col = matches[pat_len - 1] + col + 1;
6087 if (col > (colnr_T)STRLEN(str))
6088 break;
6089 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006090 }
6091 line_breakcheck();
6092 if (got_int)
6093 break;
6094 }
6095
6096 return found_match;
6097}
6098
6099/*
6100 * Jump to the first match and update the directory.
6101 */
6102 static void
6103vgr_jump_to_match(
6104 qf_info_T *qi,
6105 int forceit,
6106 int *redraw_for_dummy,
6107 buf_T *first_match_buf,
6108 char_u *target_dir)
6109{
6110 buf_T *buf;
6111
6112 buf = curbuf;
6113 qf_jump(qi, 0, 0, forceit);
6114 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006115 // If we jumped to another buffer redrawing will already be
6116 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006117 *redraw_for_dummy = FALSE;
6118
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006119 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006120 if (curbuf == first_match_buf && target_dir != NULL)
6121 {
6122 exarg_T ea;
6123
Bram Moolenaara80faa82020-04-12 19:37:17 +02006124 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006125 ea.arg = target_dir;
6126 ea.cmdidx = CMD_lcd;
6127 ex_cd(&ea);
6128 }
6129}
6130
6131/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006132 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006133 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006134typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006135{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006136 long tomatch; // maximum number of matches to find
6137 char_u *spat; // search pattern
6138 int flags; // search modifier
6139 char_u **fnames; // list of files to search
6140 int fcount; // number of files
6141 regmmatch_T regmatch; // compiled search pattern
6142 char_u *qf_title; // quickfix list title
6143} vgr_args_T;
6144
6145/*
6146 * Process :vimgrep command arguments. The command syntax is:
6147 *
6148 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6149 */
6150 static int
6151vgr_process_args(
6152 exarg_T *eap,
6153 vgr_args_T *args)
6154{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006155 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006156
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006157 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006158
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006159 args->regmatch.regprog = NULL;
6160 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006161
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006162 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006163 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006164 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006165 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006166
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006167 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006168 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006169 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006170 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006171 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006172 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006173 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006174
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006175 vgr_init_regmatch(&args->regmatch, args->spat);
6176 if (args->regmatch.regprog == NULL)
6177 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006178
6179 p = skipwhite(p);
6180 if (*p == NUL)
6181 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006182 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006183 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006184 }
6185
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006186 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006187 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6188 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006189 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006190 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006191 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006192 }
6193
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006194 return OK;
6195}
6196
6197/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006198 * Return TRUE if "buf" had an existing swap file, the current swap file does
6199 * not end in ".swp".
6200 */
6201 static int
6202existing_swapfile(buf_T *buf)
6203{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006204 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006205 {
6206 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6207 size_t len = STRLEN(fname);
6208
6209 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6210 }
6211 return FALSE;
6212}
6213
6214/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006215 * Search for a pattern in a list of files and populate the quickfix list with
6216 * the matches.
6217 */
6218 static int
6219vgr_process_files(
6220 win_T *wp,
6221 qf_info_T *qi,
6222 vgr_args_T *cmd_args,
6223 int *redraw_for_dummy,
6224 buf_T **first_match_buf,
6225 char_u **target_dir)
6226{
6227 int status = FAIL;
6228 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6229 time_t seconds = 0;
6230 char_u *fname;
6231 int fi;
6232 buf_T *buf;
6233 int duplicate_name = FALSE;
6234 int using_dummy;
6235 char_u *dirname_start = NULL;
6236 char_u *dirname_now = NULL;
6237 int found_match;
6238 aco_save_T aco;
6239
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006240 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6241 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006242 if (dirname_start == NULL || dirname_now == NULL)
6243 goto theend;
6244
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006245 // Remember the current directory, because a BufRead autocommand that does
6246 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006247 mch_dirname(dirname_start, MAXPATHL);
6248
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006249 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006250 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6251 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006252 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006253 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006254 if (time(NULL) > seconds)
6255 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006256 // Display the file name every second or so, show the user we are
6257 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006258 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006259 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006260 }
6261
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006262 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006263 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6264 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006265 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006266 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006267 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006268 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006269
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006270 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006271 }
6272 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006273 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006274 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006275
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006276 // Check whether the quickfix list is still valid. When loading a
6277 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006278 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006279 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006280
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006281 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006282
Bram Moolenaar81695252004-12-29 20:58:21 +00006283 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006284 {
6285 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006286 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006287 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006288 else
6289 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006290 // Try for a match in all lines of the buffer.
6291 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006292 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006293 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006294 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006295
Bram Moolenaar81695252004-12-29 20:58:21 +00006296 if (using_dummy)
6297 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006298 if (found_match && *first_match_buf == NULL)
6299 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006300 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006301 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006302 // Never keep a dummy buffer if there is another buffer
6303 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006304 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006305 buf = NULL;
6306 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006307 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006308 || buf->b_p_bh[0] == 'u' // "unload"
6309 || buf->b_p_bh[0] == 'w' // "wipe"
6310 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006311 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006312 // When no match was found we don't need to remember the
6313 // buffer, wipe it out. If there was a match and it
6314 // wasn't the first one or we won't jump there: only
6315 // unload the buffer.
6316 // Ignore 'hidden' here, because it may lead to having too
6317 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006318 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006319 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006320 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006321 buf = NULL;
6322 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006323 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006324 || (cmd_args->flags & VGR_NOJUMP)
6325 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006326 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006327 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006328 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006329 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006330 buf = NULL;
6331 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006332 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006333
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006334 if (buf != NULL)
6335 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006336 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006337 buf->b_flags &= ~BF_DUMMY;
6338
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006339 // If the buffer is still loaded we need to use the
6340 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006341 if (buf == *first_match_buf
6342 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006343 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006344 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006345
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006346 // The buffer is still loaded, the Filetype autocommands
6347 // need to be done now, in that buffer. And the modelines
6348 // need to be done (again). But not the window-local
6349 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006350 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006351#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006352 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6353 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006354#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006355 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006356 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006357 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006358 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006359 }
6360 }
6361
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006362 status = OK;
6363
6364theend:
6365 vim_free(dirname_now);
6366 vim_free(dirname_start);
6367 return status;
6368}
6369
6370/*
6371 * ":vimgrep {pattern} file(s)"
6372 * ":vimgrepadd {pattern} file(s)"
6373 * ":lvimgrep {pattern} file(s)"
6374 * ":lvimgrepadd {pattern} file(s)"
6375 */
6376 void
6377ex_vimgrep(exarg_T *eap)
6378{
6379 vgr_args_T args;
6380 qf_info_T *qi;
6381 qf_list_T *qfl;
6382 int_u save_qfid;
6383 win_T *wp = NULL;
6384 int redraw_for_dummy = FALSE;
6385 buf_T *first_match_buf = NULL;
6386 char_u *target_dir = NULL;
6387 char_u *au_name = NULL;
6388 int status;
6389
6390 au_name = vgr_get_auname(eap->cmdidx);
6391 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6392 curbuf->b_fname, TRUE, curbuf))
6393 {
6394#ifdef FEAT_EVAL
6395 if (aborting())
6396 return;
6397#endif
6398 }
6399
6400 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6401 if (qi == NULL)
6402 return;
6403
6404 if (vgr_process_args(eap, &args) == FAIL)
6405 goto theend;
6406
6407 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6408 && eap->cmdidx != CMD_vimgrepadd
6409 && eap->cmdidx != CMD_lvimgrepadd)
6410 || qf_stack_empty(qi))
6411 // make place for a new list
6412 qf_new_list(qi, args.qf_title);
6413
6414 incr_quickfix_busy();
6415
6416 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6417 &first_match_buf, &target_dir);
6418 if (status != OK)
6419 {
6420 FreeWild(args.fcount, args.fnames);
6421 decr_quickfix_busy();
6422 goto theend;
6423 }
6424
6425 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006426
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006427 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006428 qfl->qf_nonevalid = FALSE;
6429 qfl->qf_ptr = qfl->qf_start;
6430 qfl->qf_index = 1;
6431 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006432
Bram Moolenaar864293a2016-06-02 13:40:04 +02006433 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006434
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006435 // Remember the current quickfix list identifier, so that we can check for
6436 // autocommands changing the current quickfix list.
6437 save_qfid = qf_get_curlist(qi)->qf_id;
6438
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006439 if (au_name != NULL)
6440 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6441 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006442 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6443 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006444 if (!qflist_valid(wp, save_qfid)
6445 || qf_restore_list(qi, save_qfid) == FAIL)
6446 {
6447 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006448 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006449 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006450
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006451 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006452 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006453 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006454 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006455 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6456 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006457 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006458 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006459 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006460
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006461 decr_quickfix_busy();
6462
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006463 // If we loaded a dummy buffer into the current window, the autocommands
6464 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006465 if (redraw_for_dummy)
6466 {
6467#ifdef FEAT_FOLDING
6468 foldUpdateAll(curwin);
6469#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006470 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006471#endif
6472 }
6473
Bram Moolenaar86b68352004-12-27 21:59:20 +00006474theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006475 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006476 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006477 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006478}
6479
6480/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006481 * Restore current working directory to "dirname_start" if they differ, taking
6482 * into account whether it is set locally or globally.
6483 */
6484 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006485restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006486{
6487 char_u *dirname_now = alloc(MAXPATHL);
6488
6489 if (NULL != dirname_now)
6490 {
6491 mch_dirname(dirname_now, MAXPATHL);
6492 if (STRCMP(dirname_start, dirname_now) != 0)
6493 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006494 // If the directory has changed, change it back by building up an
6495 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006496 exarg_T ea;
6497
Bram Moolenaara80faa82020-04-12 19:37:17 +02006498 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006499 ea.arg = dirname_start;
6500 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6501 ex_cd(&ea);
6502 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006503 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006504 }
6505}
6506
6507/*
6508 * Load file "fname" into a dummy buffer and return the buffer pointer,
6509 * placing the directory resulting from the buffer load into the
6510 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6511 * prior to calling this function. Restores directory to "dirname_start" prior
6512 * to returning, if autocmds or the 'autochdir' option have changed it.
6513 *
6514 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6515 * or wipe_dummy_buffer() later!
6516 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006517 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006518 */
6519 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006520load_dummy_buffer(
6521 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006522 char_u *dirname_start, // in: old directory
6523 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006524{
6525 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006526 bufref_T newbufref;
6527 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006528 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006529 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006530 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006531
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006532 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006533 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6534 if (newbuf == NULL)
6535 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006536 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006537
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006538 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006539 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6540
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006541 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006542 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006543 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006544 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006545 ++newbuf->b_locked;
6546
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006547 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006548 aucmd_prepbuf(&aco, newbuf);
6549
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006550 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006551 (void)setfname(curbuf, fname, NULL, FALSE);
6552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006553 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006554 check_need_swap(TRUE);
6555
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006556 // Remove the "dummy" flag, otherwise autocommands may not
6557 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006558 curbuf->b_flags &= ~BF_DUMMY;
6559
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006560 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006561 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006562 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006563 NULL, READ_NEW | READ_DUMMY);
6564 --newbuf->b_locked;
6565 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006566 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006567 && !(curbuf->b_flags & BF_NEW))
6568 {
6569 failed = FALSE;
6570 if (curbuf != newbuf)
6571 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006572 // Bloody autocommands changed the buffer! Can happen when
6573 // using netrw and editing a remote file. Use the current
6574 // buffer instead, delete the dummy one after restoring the
6575 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006576 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006577 newbuf = curbuf;
6578 }
6579 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006580
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006581 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006582 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006583 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6584 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006585
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006586 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6587 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006588 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006589 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006590
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006591 // When autocommands/'autochdir' option changed directory: go back.
6592 // Let the caller know what the resulting dir was first, in case it is
6593 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006594 mch_dirname(resulting_dir, MAXPATHL);
6595 restore_start_dir(dirname_start);
6596
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006597 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006598 return NULL;
6599 if (failed)
6600 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006601 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006602 return NULL;
6603 }
6604 return newbuf;
6605}
6606
6607/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006608 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6609 * directory to "dirname_start" prior to returning, if autocmds or the
6610 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006611 */
6612 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006613wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006614{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006615 // If any autocommand opened a window on the dummy buffer, close that
6616 // window. If we can't close them all then give up.
6617 while (buf->b_nwindows > 0)
6618 {
6619 int did_one = FALSE;
6620 win_T *wp;
6621
6622 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006623 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006624 if (wp->w_buffer == buf)
6625 {
6626 if (win_close(wp, FALSE) == OK)
6627 did_one = TRUE;
6628 break;
6629 }
6630 if (!did_one)
6631 return;
6632 }
6633
6634 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006635 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006636#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006637 cleanup_T cs;
6638
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006639 // Reset the error/interrupt/exception state here so that aborting()
6640 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6641 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006642 enter_cleanup(&cs);
6643#endif
6644
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006645 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006646
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006647#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006648 // Restore the error/interrupt/exception state if not discarded by a
6649 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006650 leave_cleanup(&cs);
6651#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006652 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006653 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006654 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006655}
6656
6657/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006658 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6659 * directory to "dirname_start" prior to returning, if autocmds or the
6660 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006661 */
6662 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006663unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006664{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006665 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006666 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006667 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006669 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006670 restore_start_dir(dirname_start);
6671 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006672}
6673
Bram Moolenaar05159a02005-02-26 23:04:13 +00006674#if defined(FEAT_EVAL) || defined(PROTO)
6675/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006676 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006677 * to 'list'. Returns OK on success.
6678 */
6679 static int
6680get_qfline_items(qfline_T *qfp, list_T *list)
6681{
6682 int bufnum;
6683 dict_T *dict;
6684 char_u buf[2];
6685
6686 // Handle entries with a non-existing buffer number.
6687 bufnum = qfp->qf_fnum;
6688 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6689 bufnum = 0;
6690
6691 if ((dict = dict_alloc()) == NULL)
6692 return FAIL;
6693 if (list_append_dict(list, dict) == FAIL)
6694 return FAIL;
6695
6696 buf[0] = qfp->qf_type;
6697 buf[1] = NUL;
6698 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006699 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6700 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6701 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6702 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6703 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6704 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006705 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6706 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6707 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6708 || dict_add_string(dict, "type", buf) == FAIL
6709 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6710 return FAIL;
6711
6712 return OK;
6713}
6714
6715/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006716 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006717 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006718 * If eidx is not 0, then return only the specified entry. Otherwise return
6719 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006720 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006721 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006722get_errorlist(
6723 qf_info_T *qi_arg,
6724 win_T *wp,
6725 int qf_idx,
6726 int eidx,
6727 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006728{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006729 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006730 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006731 qfline_T *qfp;
6732 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006733
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006734 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006735 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006736 qi = &ql_info;
6737 if (wp != NULL)
6738 {
6739 qi = GET_LOC_LIST(wp);
6740 if (qi == NULL)
6741 return FAIL;
6742 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006743 }
6744
Bram Moolenaar858ba062020-05-31 23:11:59 +02006745 if (eidx < 0)
6746 return OK;
6747
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006748 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006749 qf_idx = qi->qf_curlist;
6750
Bram Moolenaar0398e002019-03-21 21:12:49 +01006751 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006752 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006753
Bram Moolenaar0398e002019-03-21 21:12:49 +01006754 qfl = qf_get_list(qi, qf_idx);
6755 if (qf_list_empty(qfl))
6756 return FAIL;
6757
Bram Moolenaara16123a2019-03-28 20:31:07 +01006758 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006759 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006760 if (eidx > 0)
6761 {
6762 if (eidx == i)
6763 return get_qfline_items(qfp, list);
6764 }
6765 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006766 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006767 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006768
Bram Moolenaar05159a02005-02-26 23:04:13 +00006769 return OK;
6770}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006772// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006773enum {
6774 QF_GETLIST_NONE = 0x0,
6775 QF_GETLIST_TITLE = 0x1,
6776 QF_GETLIST_ITEMS = 0x2,
6777 QF_GETLIST_NR = 0x4,
6778 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006779 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006780 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006781 QF_GETLIST_IDX = 0x40,
6782 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006783 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006784 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006785 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006786 QF_GETLIST_QFTF = 0x800,
6787 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006788};
6789
6790/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006791 * Parse text from 'di' and return the quickfix list items.
6792 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006793 */
6794 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006795qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006796{
6797 int status = FAIL;
6798 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006799 char_u *errorformat = p_efm;
6800 dictitem_T *efm_di;
6801 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006802
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006803 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006804 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006805 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006806 // If errorformat is supplied then use it, otherwise use the 'efm'
6807 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006808 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6809 {
6810 if (efm_di->di_tv.v_type != VAR_STRING ||
6811 efm_di->di_tv.vval.v_string == NULL)
6812 return FAIL;
6813 errorformat = efm_di->di_tv.vval.v_string;
6814 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006815
Bram Moolenaar36538222017-09-02 19:51:44 +02006816 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006817 if (l == NULL)
6818 return FAIL;
6819
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006820 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006821 if (qi != NULL)
6822 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006823 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006824 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6825 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006826 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006827 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006828 }
6829 free(qi);
6830 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006831 dict_add_list(retdict, "items", l);
6832 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006833 }
6834
6835 return status;
6836}
6837
6838/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006839 * Return the quickfix/location list window identifier in the current tabpage.
6840 */
6841 static int
6842qf_winid(qf_info_T *qi)
6843{
6844 win_T *win;
6845
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006846 // The quickfix window can be opened even if the quickfix list is not set
6847 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006848 if (qi == NULL)
6849 return 0;
6850 win = qf_find_win(qi);
6851 if (win != NULL)
6852 return win->w_id;
6853 return 0;
6854}
6855
6856/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006857 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006858 * window. If there is no buffer associated with the list or the buffer is
6859 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006860 */
6861 static int
6862qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6863{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006864 int bufnum = 0;
6865
6866 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6867 bufnum = qi->qf_bufnr;
6868
6869 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006870}
6871
6872/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006873 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006874 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006875 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006876qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006877{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006878 int flags = QF_GETLIST_NONE;
6879
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006880 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006881 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006882 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006883 if (!loclist)
6884 // File window ID is applicable only to location list windows
6885 flags &= ~ QF_GETLIST_FILEWINID;
6886 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006887
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006888 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006889 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006890
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006891 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006892 flags |= QF_GETLIST_NR;
6893
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006894 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006895 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006896
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006897 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006898 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006899
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006900 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006901 flags |= QF_GETLIST_ID;
6902
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006903 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006904 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006905
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006906 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006907 flags |= QF_GETLIST_IDX;
6908
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006909 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006910 flags |= QF_GETLIST_SIZE;
6911
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006912 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01006913 flags |= QF_GETLIST_TICK;
6914
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006915 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006916 flags |= QF_GETLIST_FILEWINID;
6917
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006918 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006919 flags |= QF_GETLIST_QFBUFNR;
6920
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006921 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02006922 flags |= QF_GETLIST_QFTF;
6923
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006924 return flags;
6925}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006926
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006927/*
6928 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6929 * If 'nr' and 'id' are not present in 'what' then return the current
6930 * quickfix list index.
6931 * If 'nr' is zero then return the current quickfix list index.
6932 * If 'nr' is '$' then return the last quickfix list index.
6933 * If 'id' is present then return the index of the quickfix list with that id.
6934 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6935 * Return -1, if quickfix list is not present or if the stack is empty.
6936 */
6937 static int
6938qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6939{
6940 int qf_idx;
6941 dictitem_T *di;
6942
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006943 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006944 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6945 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006946 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006947 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006948 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006949 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006950 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006951 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006952 qf_idx = di->di_tv.vval.v_number - 1;
6953 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006954 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006955 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006956 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006957 else if (di->di_tv.v_type == VAR_STRING
6958 && di->di_tv.vval.v_string != NULL
6959 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006960 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006961 qf_idx = qi->qf_listcount - 1;
6962 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006963 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006964 }
6965
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006966 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6967 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006968 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006969 if (di->di_tv.v_type == VAR_NUMBER)
6970 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006971 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006972 if (di->di_tv.vval.v_number != 0)
6973 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6974 }
6975 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006976 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006977 }
6978
6979 return qf_idx;
6980}
6981
6982/*
6983 * Return default values for quickfix list properties in retdict.
6984 */
6985 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006986qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006987{
6988 int status = OK;
6989
6990 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006991 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006992 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6993 {
6994 list_T *l = list_alloc();
6995 if (l != NULL)
6996 status = dict_add_list(retdict, "items", l);
6997 else
6998 status = FAIL;
6999 }
7000 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007001 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007002 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007003 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007004 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007005 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007006 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007007 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007008 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007009 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007010 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007011 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007012 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007013 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007014 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007015 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007016 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7017 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007018 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7019 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007020
7021 return status;
7022}
7023
7024/*
7025 * Return the quickfix list title as 'title' in retdict
7026 */
7027 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007028qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007029{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007030 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007031}
7032
7033/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007034 * Returns the identifier of the window used to display files from a location
7035 * list. If there is no associated window, then returns 0. Useful only when
7036 * called from a location list window.
7037 */
7038 static int
7039qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7040{
7041 int winid = 0;
7042
7043 if (wp != NULL && IS_LL_WINDOW(wp))
7044 {
7045 win_T *ll_wp = qf_find_win_with_loclist(qi);
7046 if (ll_wp != NULL)
7047 winid = ll_wp->w_id;
7048 }
7049
7050 return dict_add_number(retdict, "filewinid", winid);
7051}
7052
7053/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007054 * Return the quickfix list items/entries as 'items' in retdict.
7055 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007056 */
7057 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007058qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007059{
7060 int status = OK;
7061 list_T *l = list_alloc();
7062 if (l != NULL)
7063 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007064 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007065 dict_add_list(retdict, "items", l);
7066 }
7067 else
7068 status = FAIL;
7069
7070 return status;
7071}
7072
7073/*
7074 * Return the quickfix list context (if any) as 'context' in retdict.
7075 */
7076 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007077qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007078{
7079 int status;
7080 dictitem_T *di;
7081
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007082 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007083 {
7084 di = dictitem_alloc((char_u *)"context");
7085 if (di != NULL)
7086 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007087 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007088 status = dict_add(retdict, di);
7089 if (status == FAIL)
7090 dictitem_free(di);
7091 }
7092 else
7093 status = FAIL;
7094 }
7095 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007096 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007097
7098 return status;
7099}
7100
7101/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007102 * Return the current quickfix list index as 'idx' in retdict.
7103 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007104 */
7105 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007106qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007107{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007108 if (eidx == 0)
7109 {
7110 eidx = qfl->qf_index;
7111 if (qf_list_empty(qfl))
7112 // For empty lists, current index is set to 0
7113 eidx = 0;
7114 }
7115 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007116}
7117
7118/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007119 * Return the 'quickfixtextfunc' function of a quickfix/location list
7120 */
7121 static int
7122qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7123{
7124 int status;
7125
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007126 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007127 {
7128 typval_T tv;
7129
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007130 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007131 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7132 clear_tv(&tv);
7133 }
7134 else
7135 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7136
7137 return status;
7138}
7139
7140/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007141 * Return quickfix/location list details (title) as a
7142 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7143 * then current list is used. Otherwise the specified list is used.
7144 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007145 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007146qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7147{
7148 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007149 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007150 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007151 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007152 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007153 dictitem_T *di;
7154 int flags = QF_GETLIST_NONE;
7155
7156 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7157 return qf_get_list_from_lines(what, di, retdict);
7158
7159 if (wp != NULL)
7160 qi = GET_LOC_LIST(wp);
7161
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007162 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007163
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007164 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007165 qf_idx = qf_getprop_qfidx(qi, what);
7166
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007167 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007168 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007169 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007170
Bram Moolenaar0398e002019-03-21 21:12:49 +01007171 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007172
Bram Moolenaar858ba062020-05-31 23:11:59 +02007173 // If an entry index is specified, use that
7174 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7175 {
7176 if (di->di_tv.v_type != VAR_NUMBER)
7177 return FAIL;
7178 eidx = di->di_tv.vval.v_number;
7179 }
7180
Bram Moolenaard823fa92016-08-12 16:29:27 +02007181 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007182 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007183 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007184 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007185 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007186 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007187 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007188 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007189 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007190 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007191 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007192 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007193 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007194 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007195 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007196 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007197 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007198 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007199 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7200 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007201 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7202 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007203 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7204 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007205
Bram Moolenaard823fa92016-08-12 16:29:27 +02007206 return status;
7207}
7208
7209/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007210 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007211 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7212 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007213 */
7214 static int
7215qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007216 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007217 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007218 int first_entry,
7219 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007220{
7221 static int did_bufnr_emsg;
7222 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007223 int bufnum, valid, status, col, end_col, vcol, nr;
7224 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007225
7226 if (first_entry)
7227 did_bufnr_emsg = FALSE;
7228
Bram Moolenaard61efa52022-07-23 09:52:04 +01007229 filename = dict_get_string(d, "filename", TRUE);
7230 module = dict_get_string(d, "module", TRUE);
7231 bufnum = (int)dict_get_number(d, "bufnr");
7232 lnum = (int)dict_get_number(d, "lnum");
7233 end_lnum = (int)dict_get_number(d, "end_lnum");
7234 col = (int)dict_get_number(d, "col");
7235 end_col = (int)dict_get_number(d, "end_col");
7236 vcol = (int)dict_get_number(d, "vcol");
7237 nr = (int)dict_get_number(d, "nr");
7238 type = dict_get_string(d, "type", TRUE);
7239 pattern = dict_get_string(d, "pattern", TRUE);
7240 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007241 if (text == NULL)
7242 text = vim_strsave((char_u *)"");
7243
7244 valid = TRUE;
7245 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7246 valid = FALSE;
7247
7248 // Mark entries with non-existing buffer number as not valid. Give the
7249 // error message only once.
7250 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7251 {
7252 if (!did_bufnr_emsg)
7253 {
7254 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007255 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007256 }
7257 valid = FALSE;
7258 bufnum = 0;
7259 }
7260
7261 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007262 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007263 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007264
Bram Moolenaar0398e002019-03-21 21:12:49 +01007265 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007266 NULL, // dir
7267 filename,
7268 module,
7269 bufnum,
7270 text,
7271 lnum,
thinca6864efa2021-06-19 20:45:20 +02007272 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007273 col,
thinca6864efa2021-06-19 20:45:20 +02007274 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007275 vcol, // vis_col
7276 pattern, // search pattern
7277 nr,
7278 type == NULL ? NUL : *type,
7279 valid);
7280
7281 vim_free(filename);
7282 vim_free(module);
7283 vim_free(pattern);
7284 vim_free(text);
7285 vim_free(type);
7286
Bram Moolenaar9752c722018-12-22 16:49:34 +01007287 if (valid)
7288 *valid_entry = TRUE;
7289
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007290 return status;
7291}
7292
7293/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007294 * Add list of entries to quickfix/location list. Each list entry is
7295 * a dictionary with item information.
7296 */
7297 static int
7298qf_add_entries(
7299 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007300 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007301 list_T *list,
7302 char_u *title,
7303 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007304{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007305 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007306 listitem_T *li;
7307 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007308 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007309 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007310 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007311
Bram Moolenaara3921f42017-06-04 15:30:34 +02007312 if (action == ' ' || qf_idx == qi->qf_listcount)
7313 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007314 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007315 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007316 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007317 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007318 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007319 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007320 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007321 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007322 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007323 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007324 qf_free_items(qfl);
7325 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007326 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007327
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007328 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007329 {
7330 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007331 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007332
7333 d = li->li_tv.vval.v_dict;
7334 if (d == NULL)
7335 continue;
7336
Bram Moolenaar0398e002019-03-21 21:12:49 +01007337 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007338 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007339 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007340 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007341 }
7342
Bram Moolenaar9752c722018-12-22 16:49:34 +01007343 // Check if any valid error entries are added to the list.
7344 if (valid_entry)
7345 qfl->qf_nonevalid = FALSE;
7346 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007347 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007348 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007349
7350 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007351 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007352 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007353
7354 // Update the current error index if not appending to the list or if the
7355 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007356 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007357 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007358
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007359 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007360 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007361
7362 return retval;
7363}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007364
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007365/*
7366 * Get the quickfix list index from 'nr' or 'id'
7367 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007368 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007369qf_setprop_get_qfidx(
7370 qf_info_T *qi,
7371 dict_T *what,
7372 int action,
7373 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007374{
7375 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007376 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007377
Bram Moolenaard823fa92016-08-12 16:29:27 +02007378 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7379 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007380 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007381 if (di->di_tv.v_type == VAR_NUMBER)
7382 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007383 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007384 if (di->di_tv.vval.v_number != 0)
7385 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007386
Bram Moolenaar55b69262017-08-13 13:42:01 +02007387 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7388 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007389 // When creating a new list, accept qf_idx pointing to the next
7390 // non-available list and add the new list at the end of the
7391 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007392 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007393 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007394 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007395 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007396 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007397 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007398 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007399 }
7400 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007401 && di->di_tv.vval.v_string != NULL
7402 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007403 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007404 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007405 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007406 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007407 qf_idx = 0;
7408 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007409 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007410 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007411 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007412 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007413 }
7414
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007415 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007416 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007417 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007418 if (di->di_tv.v_type != VAR_NUMBER)
7419 return INVALID_QFIDX;
7420
7421 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007422 }
7423
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007424 return qf_idx;
7425}
7426
7427/*
7428 * Set the quickfix list title.
7429 */
7430 static int
7431qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7432{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007433 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007434
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007435 if (di->di_tv.v_type != VAR_STRING)
7436 return FAIL;
7437
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007438 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007439 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007440 if (qf_idx == qi->qf_curlist)
7441 qf_update_win_titlevar(qi);
7442
7443 return OK;
7444}
7445
7446/*
7447 * Set quickfix list items/entries.
7448 */
7449 static int
7450qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7451{
7452 int retval = FAIL;
7453 char_u *title_save;
7454
7455 if (di->di_tv.v_type != VAR_LIST)
7456 return FAIL;
7457
7458 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7459 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7460 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007461 vim_free(title_save);
7462
7463 return retval;
7464}
7465
7466/*
7467 * Set quickfix list items/entries from a list of lines.
7468 */
7469 static int
7470qf_setprop_items_from_lines(
7471 qf_info_T *qi,
7472 int qf_idx,
7473 dict_T *what,
7474 dictitem_T *di,
7475 int action)
7476{
7477 char_u *errorformat = p_efm;
7478 dictitem_T *efm_di;
7479 int retval = FAIL;
7480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007481 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007482 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7483 {
7484 if (efm_di->di_tv.v_type != VAR_STRING ||
7485 efm_di->di_tv.vval.v_string == NULL)
7486 return FAIL;
7487 errorformat = efm_di->di_tv.vval.v_string;
7488 }
7489
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007490 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007491 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7492 return FAIL;
7493
7494 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007495 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007496 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007497 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007498 retval = OK;
7499
7500 return retval;
7501}
7502
7503/*
7504 * Set quickfix list context.
7505 */
7506 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007507qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007508{
7509 typval_T *ctx;
7510
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007511 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007512 ctx = alloc_tv();
7513 if (ctx != NULL)
7514 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007515 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007516
7517 return OK;
7518}
7519
7520/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007521 * Set the current index in the specified quickfix list
7522 */
7523 static int
7524qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7525{
7526 int denote = FALSE;
7527 int newidx;
7528 int old_qfidx;
7529 qfline_T *qf_ptr;
7530
7531 // If the specified index is '$', then use the last entry
7532 if (di->di_tv.v_type == VAR_STRING
7533 && di->di_tv.vval.v_string != NULL
7534 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7535 newidx = qfl->qf_count;
7536 else
7537 {
7538 // Otherwise use the specified index
7539 newidx = tv_get_number_chk(&di->di_tv, &denote);
7540 if (denote)
7541 return FAIL;
7542 }
7543
7544 if (newidx < 1) // sanity check
7545 return FAIL;
7546 if (newidx > qfl->qf_count)
7547 newidx = qfl->qf_count;
7548
7549 old_qfidx = qfl->qf_index;
7550 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7551 if (qf_ptr == NULL)
7552 return FAIL;
7553 qfl->qf_ptr = qf_ptr;
7554 qfl->qf_index = newidx;
7555
7556 // If the current list is modified and it is displayed in the quickfix
7557 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007558 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007559 qf_win_pos_update(qi, old_qfidx);
7560
7561 return OK;
7562}
7563
7564/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007565 * Set the current index in the specified quickfix list
7566 */
7567 static int
7568qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7569{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007570 callback_T cb;
7571
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007572 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007573 cb = get_callback(&di->di_tv);
7574 if (cb.cb_name != NULL && *cb.cb_name != NUL)
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007575 set_callback(&qfl->qf_qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007576
7577 return OK;
7578}
7579
7580/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007581 * Set quickfix/location list properties (title, items, context).
7582 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007583 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007584 */
7585 static int
7586qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7587{
7588 dictitem_T *di;
7589 int retval = FAIL;
7590 int qf_idx;
7591 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007592 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007593
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007594 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007595 newlist = TRUE;
7596
7597 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007598 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007599 return FAIL;
7600
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007601 if (newlist)
7602 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007603 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007604 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007605 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007606 }
7607
Bram Moolenaar0398e002019-03-21 21:12:49 +01007608 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007609 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007610 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007611 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007612 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007613 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007614 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007615 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007616 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007617 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7618 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007619 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7620 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007621
Bram Moolenaar287153c2020-11-29 14:20:27 +01007622 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007623 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007624 if (newlist)
7625 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007626
Bram Moolenaard823fa92016-08-12 16:29:27 +02007627 return retval;
7628}
7629
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007630/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007631 * Free the entire quickfix/location list stack.
7632 * If the quickfix/location list window is open, then clear it.
7633 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007634 static void
7635qf_free_stack(win_T *wp, qf_info_T *qi)
7636{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007637 win_T *qfwin = qf_find_win(qi);
7638 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007639
7640 if (qfwin != NULL)
7641 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007642 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007643 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007644 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007645 qf_update_buffer(qi, NULL);
7646 }
7647
7648 if (wp != NULL && IS_LL_WINDOW(wp))
7649 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007650 // If in the location list window, then use the non-location list
7651 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007652 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007653 if (llwin != NULL)
7654 wp = llwin;
7655 }
7656
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007657 qf_free_all(wp);
7658 if (wp == NULL)
7659 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007660 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007661 qi->qf_curlist = 0;
7662 qi->qf_listcount = 0;
7663 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007664 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007665 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007666 // If the location list window is open, then create a new empty
7667 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007668 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007669
Bram Moolenaar95946f12019-03-31 15:31:59 +02007670 if (new_ll != NULL)
7671 {
7672 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007673
Bram Moolenaar95946f12019-03-31 15:31:59 +02007674 // first free the list reference in the location list window
7675 ll_free_all(&qfwin->w_llist_ref);
7676
7677 qfwin->w_llist_ref = new_ll;
7678 if (wp != qfwin)
7679 win_set_loclist(wp, new_ll);
7680 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007681 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007682}
7683
Bram Moolenaard823fa92016-08-12 16:29:27 +02007684/*
7685 * Populate the quickfix list with the items supplied in the list
7686 * of dictionaries. "title" will be copied to w:quickfix_title.
7687 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007688 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007689 */
7690 int
7691set_errorlist(
7692 win_T *wp,
7693 list_T *list,
7694 int action,
7695 char_u *title,
7696 dict_T *what)
7697{
7698 qf_info_T *qi = &ql_info;
7699 int retval = OK;
7700
7701 if (wp != NULL)
7702 {
7703 qi = ll_get_or_alloc_list(wp);
7704 if (qi == NULL)
7705 return FAIL;
7706 }
7707
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007708 if (action == 'f')
7709 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007710 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007711 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007712 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007713 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007714
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007715 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007716 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007717 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007718 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007719 _("cannot have both a list and a \"what\" argument"));
7720 return FAIL;
7721 }
7722
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007723 incr_quickfix_busy();
7724
7725 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007726 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007727 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007728 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007729 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007730 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007731 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007732 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007733
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007734 decr_quickfix_busy();
7735
Bram Moolenaard823fa92016-08-12 16:29:27 +02007736 return retval;
7737}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007738
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007739/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007740 * Mark the quickfix context and callback function as in use for all the lists
7741 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007742 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007743 static int
7744mark_quickfix_ctx(qf_info_T *qi, int copyID)
7745{
7746 int i;
7747 int abort = FALSE;
7748 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007749 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007750
7751 for (i = 0; i < LISTCOUNT && !abort; ++i)
7752 {
7753 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007754 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7755 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007756 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7757
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007758 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007759 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007760 }
7761
7762 return abort;
7763}
7764
7765/*
7766 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007767 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007768 */
7769 int
7770set_ref_in_quickfix(int copyID)
7771{
7772 int abort = FALSE;
7773 tabpage_T *tp;
7774 win_T *win;
7775
7776 abort = mark_quickfix_ctx(&ql_info, copyID);
7777 if (abort)
7778 return abort;
7779
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007780 abort = set_ref_in_callback(&qftf_cb, copyID);
7781 if (abort)
7782 return abort;
7783
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007784 FOR_ALL_TAB_WINDOWS(tp, win)
7785 {
7786 if (win->w_llist != NULL)
7787 {
7788 abort = mark_quickfix_ctx(win->w_llist, copyID);
7789 if (abort)
7790 return abort;
7791 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007792 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7793 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007794 // In a location list window and none of the other windows is
7795 // referring to this location list. Mark the location list
7796 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007797 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7798 if (abort)
7799 return abort;
7800 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007801 }
7802
7803 return abort;
7804}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007805#endif
7806
Bram Moolenaar81695252004-12-29 20:58:21 +00007807/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007808 * Return the autocmd name for the :cbuffer Ex commands
7809 */
7810 static char_u *
7811cbuffer_get_auname(cmdidx_T cmdidx)
7812{
7813 switch (cmdidx)
7814 {
7815 case CMD_cbuffer: return (char_u *)"cbuffer";
7816 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7817 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7818 case CMD_lbuffer: return (char_u *)"lbuffer";
7819 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7820 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7821 default: return NULL;
7822 }
7823}
7824
7825/*
7826 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7827 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7828 */
7829 static int
7830cbuffer_process_args(
7831 exarg_T *eap,
7832 buf_T **bufp,
7833 linenr_T *line1,
7834 linenr_T *line2)
7835{
7836 buf_T *buf = NULL;
7837
7838 if (*eap->arg == NUL)
7839 buf = curbuf;
7840 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7841 buf = buflist_findnr(atoi((char *)eap->arg));
7842
7843 if (buf == NULL)
7844 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007845 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007846 return FAIL;
7847 }
7848
7849 if (buf->b_ml.ml_mfp == NULL)
7850 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007851 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007852 return FAIL;
7853 }
7854
7855 if (eap->addr_count == 0)
7856 {
7857 eap->line1 = 1;
7858 eap->line2 = buf->b_ml.ml_line_count;
7859 }
7860
7861 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7862 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7863 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007864 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007865 return FAIL;
7866 }
7867
7868 *line1 = eap->line1;
7869 *line2 = eap->line2;
7870 *bufp = buf;
7871
7872 return OK;
7873}
7874
7875/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007876 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007877 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007878 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007879 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007880 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007881 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007882 */
7883 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007884ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007885{
7886 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007887 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007888 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007889 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007890 int_u save_qfid;
7891 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007892 char_u *qf_title;
7893 linenr_T line1;
7894 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007895
Bram Moolenaara16123a2019-03-28 20:31:07 +01007896 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007897 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007898 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007899 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007900#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007901 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007902 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007903#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007904 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007905
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007906 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007907 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7908 if (qi == NULL)
7909 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007910
Bram Moolenaara16123a2019-03-28 20:31:07 +01007911 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7912 return;
7913
7914 qf_title = qf_cmdtitle(*eap->cmdlinep);
7915
7916 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007917 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007918 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7919 (char *)qf_title, (char *)buf->b_sfname);
7920 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007921 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007922
7923 incr_quickfix_busy();
7924
7925 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7926 (eap->cmdidx != CMD_caddbuffer
7927 && eap->cmdidx != CMD_laddbuffer),
7928 line1, line2,
7929 qf_title, NULL);
7930 if (qf_stack_empty(qi))
7931 {
7932 decr_quickfix_busy();
7933 return;
7934 }
7935 if (res >= 0)
7936 qf_list_changed(qf_get_curlist(qi));
7937
7938 // Remember the current quickfix list identifier, so that we can
7939 // check for autocommands changing the current quickfix list.
7940 save_qfid = qf_get_curlist(qi)->qf_id;
7941 if (au_name != NULL)
7942 {
7943 buf_T *curbuf_old = curbuf;
7944
7945 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7946 TRUE, curbuf);
7947 if (curbuf != curbuf_old)
7948 // Autocommands changed buffer, don't jump now, "qi" may
7949 // be invalid.
7950 res = 0;
7951 }
7952 // Jump to the first error for a new list and if autocmds didn't
7953 // free the list.
7954 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7955 eap->cmdidx == CMD_lbuffer)
7956 && qflist_valid(wp, save_qfid))
7957 // display the first error
7958 qf_jump_first(qi, save_qfid, eap->forceit);
7959
7960 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007961}
7962
Bram Moolenaar1e015462005-09-25 22:16:38 +00007963#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007964/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007965 * Return the autocmd name for the :cexpr Ex commands.
7966 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007967 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007968cexpr_get_auname(cmdidx_T cmdidx)
7969{
7970 switch (cmdidx)
7971 {
7972 case CMD_cexpr: return (char_u *)"cexpr";
7973 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7974 case CMD_caddexpr: return (char_u *)"caddexpr";
7975 case CMD_lexpr: return (char_u *)"lexpr";
7976 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7977 case CMD_laddexpr: return (char_u *)"laddexpr";
7978 default: return NULL;
7979 }
7980}
7981
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007982 int
7983trigger_cexpr_autocmd(int cmdidx)
7984{
7985 char_u *au_name = cexpr_get_auname(cmdidx);
7986
7987 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7988 curbuf->b_fname, TRUE, curbuf))
7989 {
7990 if (aborting())
7991 return FAIL;
7992 }
7993 return OK;
7994}
7995
7996 int
7997cexpr_core(exarg_T *eap, typval_T *tv)
7998{
7999 qf_info_T *qi;
8000 win_T *wp = NULL;
8001
8002 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8003 if (qi == NULL)
8004 return FAIL;
8005
8006 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8007 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8008 {
8009 int res;
8010 int_u save_qfid;
8011 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8012
8013 incr_quickfix_busy();
8014 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8015 (eap->cmdidx != CMD_caddexpr
8016 && eap->cmdidx != CMD_laddexpr),
8017 (linenr_T)0, (linenr_T)0,
8018 qf_cmdtitle(*eap->cmdlinep), NULL);
8019 if (qf_stack_empty(qi))
8020 {
8021 decr_quickfix_busy();
8022 return FAIL;
8023 }
8024 if (res >= 0)
8025 qf_list_changed(qf_get_curlist(qi));
8026
8027 // Remember the current quickfix list identifier, so that we can
8028 // check for autocommands changing the current quickfix list.
8029 save_qfid = qf_get_curlist(qi)->qf_id;
8030 if (au_name != NULL)
8031 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8032 curbuf->b_fname, TRUE, curbuf);
8033
8034 // Jump to the first error for a new list and if autocmds didn't
8035 // free the list.
8036 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8037 && qflist_valid(wp, save_qfid))
8038 // display the first error
8039 qf_jump_first(qi, save_qfid, eap->forceit);
8040 decr_quickfix_busy();
8041 return OK;
8042 }
8043
Bram Moolenaar677658a2022-01-05 16:09:06 +00008044 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008045 return FAIL;
8046}
8047
Bram Moolenaara16123a2019-03-28 20:31:07 +01008048/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008049 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8050 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008051 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008052 */
8053 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008054ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008055{
8056 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008057
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008058 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008059 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008060
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008061 // Evaluate the expression. When the result is a string or a list we can
8062 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008063 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008064 if (tv != NULL)
8065 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008066 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008067 free_tv(tv);
8068 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008069}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008070#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008071
8072/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008073 * Get the location list for ":lhelpgrep"
8074 */
8075 static qf_info_T *
8076hgr_get_ll(int *new_ll)
8077{
8078 win_T *wp;
8079 qf_info_T *qi;
8080
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008081 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008082 if (bt_help(curwin->w_buffer))
8083 wp = curwin;
8084 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008085 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008086 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008087
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008088 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008089 qi = NULL;
8090 else
8091 qi = wp->w_llist;
8092
8093 if (qi == NULL)
8094 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008095 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008096 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008097 return NULL;
8098 *new_ll = TRUE;
8099 }
8100
8101 return qi;
8102}
8103
8104/*
8105 * Search for a pattern in a help file.
8106 */
8107 static void
8108hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008109 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008110 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008111 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008112 regmatch_T *p_regmatch)
8113{
8114 FILE *fd;
8115 long lnum;
8116
8117 fd = mch_fopen((char *)fname, "r");
8118 if (fd == NULL)
8119 return;
8120
8121 lnum = 1;
8122 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8123 {
8124 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008125
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008126 // Convert a line if 'encoding' is not utf-8 and
8127 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008128 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008129 {
8130 line = string_convert(p_vc, IObuff, NULL);
8131 if (line == NULL)
8132 line = IObuff;
8133 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008134
8135 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8136 {
8137 int l = (int)STRLEN(line);
8138
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008139 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008140 while (l > 0 && line[l - 1] <= ' ')
8141 line[--l] = NUL;
8142
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008143 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008144 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008145 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008146 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008147 0,
8148 line,
8149 lnum,
thinca6864efa2021-06-19 20:45:20 +02008150 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008151 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008152 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008153 (int)(p_regmatch->endp[0] - line)
8154 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008155 FALSE, // vis_col
8156 NULL, // search pattern
8157 0, // nr
8158 1, // type
8159 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008160 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008161 {
8162 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008163 if (line != IObuff)
8164 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008165 break;
8166 }
8167 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008168 if (line != IObuff)
8169 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008170 ++lnum;
8171 line_breakcheck();
8172 }
8173 fclose(fd);
8174}
8175
8176/*
8177 * Search for a pattern in all the help files in the doc directory under
8178 * the given directory.
8179 */
8180 static void
8181hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008182 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008183 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008184 regmatch_T *p_regmatch,
8185 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008186#ifdef FEAT_MULTI_LANG
8187 , char_u *lang
8188#endif
8189 )
8190{
8191 int fcount;
8192 char_u **fnames;
8193 int fi;
8194
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008195 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008196 add_pathsep(dirname);
8197 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8198 if (gen_expand_wildcards(1, &dirname, &fcount,
8199 &fnames, EW_FILE|EW_SILENT) == OK
8200 && fcount > 0)
8201 {
8202 for (fi = 0; fi < fcount && !got_int; ++fi)
8203 {
8204#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008205 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008206 if (lang != NULL
8207 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008208 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008209 && !(STRNICMP(lang, "en", 2) == 0
8210 && STRNICMP("txt", fnames[fi]
8211 + STRLEN(fnames[fi]) - 3, 3) == 0))
8212 continue;
8213#endif
8214
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008215 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008216 }
8217 FreeWild(fcount, fnames);
8218 }
8219}
8220
8221/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008222 * Search for a pattern in all the help files in the 'runtimepath'
8223 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008224 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008225 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008226 */
8227 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008228hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008229{
8230 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008231
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008232 vimconv_T vc;
8233
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008234 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8235 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008236 vc.vc_type = CONV_NONE;
8237 if (!enc_utf8)
8238 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008239
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008240 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008241 p = p_rtp;
8242 while (*p != NUL && !got_int)
8243 {
8244 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8245
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008246 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008247#ifdef FEAT_MULTI_LANG
8248 , lang
8249#endif
8250 );
8251 }
8252
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008253 if (vc.vc_type != CONV_NONE)
8254 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008255}
8256
8257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 * ":helpgrep {pattern}"
8259 */
8260 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008261ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262{
8263 regmatch_T regmatch;
8264 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008265 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008266 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008267 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008268 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008269 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008270 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
Bram Moolenaar73633f82012-01-20 13:39:07 +01008272 switch (eap->cmdidx)
8273 {
8274 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8275 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8276 default: break;
8277 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008278 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8279 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008280 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008281#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008282 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008283 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008284#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008285 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008286
Bram Moolenaar39665952018-08-15 20:59:48 +02008287 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008288 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008289 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008290 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008291 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008292 }
8293
Bram Moolenaara16123a2019-03-28 20:31:07 +01008294 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8295 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008296 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008297 p_cpo = empty_option;
8298
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008299 incr_quickfix_busy();
8300
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008301#ifdef FEAT_MULTI_LANG
8302 // Check for a specified language
8303 lang = check_help_lang(eap->arg);
8304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8306 regmatch.rm_ic = FALSE;
8307 if (regmatch.regprog != NULL)
8308 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008309 qf_list_T *qfl;
8310
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008311 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008312 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008313 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008315 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008316
Bram Moolenaar473de612013-06-08 18:19:48 +02008317 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008319 qfl->qf_nonevalid = FALSE;
8320 qfl->qf_ptr = qfl->qf_start;
8321 qfl->qf_index = 1;
8322 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008323 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008326 if (p_cpo == empty_option)
8327 p_cpo = save_cpo;
8328 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008329 {
8330 // Darn, some plugin changed the value. If it's still empty it was
8331 // changed and restored, need to restore in the complicated way.
8332 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008333 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008334 if (save_cpo_allocated)
8335 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008336 }
8337
8338 if (updated)
8339 // This may open a window and source scripts, do this after 'cpo' was
8340 // restored.
8341 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
Bram Moolenaar73633f82012-01-20 13:39:07 +01008343 if (au_name != NULL)
8344 {
8345 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8346 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008347 // When adding a location list to an existing location list stack,
8348 // if the autocmd made the stack invalid, then just return.
8349 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008350 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008351 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008352 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008353 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008354 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008355
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008356 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008357 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008358 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008359 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008360 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008361
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008362 decr_quickfix_busy();
8363
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008364 if (eap->cmdidx == CMD_lhelpgrep)
8365 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008366 // If the help window is not opened or if it already points to the
8367 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008368 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008369 {
8370 if (new_qi)
8371 ll_free_all(&qi);
8372 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008373 else if (curwin->w_llist == NULL && new_qi)
8374 // current window didn't have a location list associated with it
8375 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008376 curwin->w_llist = qi;
8377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008379#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008380
8381#if defined(FEAT_EVAL) || defined(PROTO)
8382# ifdef FEAT_QUICKFIX
8383 static void
8384get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8385{
8386 if (what_arg->v_type == VAR_UNKNOWN)
8387 {
8388 if (rettv_list_alloc(rettv) == OK)
8389 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008390 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008391 }
8392 else
8393 {
8394 if (rettv_dict_alloc(rettv) == OK)
8395 if (is_qf || (wp != NULL))
8396 {
8397 if (what_arg->v_type == VAR_DICT)
8398 {
8399 dict_T *d = what_arg->vval.v_dict;
8400
8401 if (d != NULL)
8402 qf_get_properties(wp, d, rettv->vval.v_dict);
8403 }
8404 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008405 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008406 }
8407 }
8408}
8409# endif
8410
8411/*
8412 * "getloclist()" function
8413 */
8414 void
8415f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8416{
8417# ifdef FEAT_QUICKFIX
8418 win_T *wp;
8419
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008420 if (in_vim9script()
8421 && (check_for_number_arg(argvars, 0) == FAIL
8422 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8423 return;
8424
Bram Moolenaare677df82019-09-02 22:31:11 +02008425 wp = find_win_by_nr_or_id(&argvars[0]);
8426 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8427# endif
8428}
8429
8430/*
8431 * "getqflist()" function
8432 */
8433 void
8434f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8435{
8436# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008437 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8438 return;
8439
Bram Moolenaare677df82019-09-02 22:31:11 +02008440 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8441# endif
8442}
8443
8444/*
8445 * Used by "setqflist()" and "setloclist()" functions
8446 */
8447 static void
8448set_qf_ll_list(
8449 win_T *wp UNUSED,
8450 typval_T *list_arg UNUSED,
8451 typval_T *action_arg UNUSED,
8452 typval_T *what_arg UNUSED,
8453 typval_T *rettv)
8454{
8455# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008456 char_u *act;
8457 int action = 0;
8458 static int recursive = 0;
8459# endif
8460
8461 rettv->vval.v_number = -1;
8462
8463# ifdef FEAT_QUICKFIX
8464 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008465 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008466 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008467 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008468 else
8469 {
8470 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008471 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008472 int valid_dict = TRUE;
8473
8474 if (action_arg->v_type == VAR_STRING)
8475 {
8476 act = tv_get_string_chk(action_arg);
8477 if (act == NULL)
8478 return; // type error; errmsg already given
8479 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8480 act[1] == NUL)
8481 action = *act;
8482 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008483 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008484 }
8485 else if (action_arg->v_type == VAR_UNKNOWN)
8486 action = ' ';
8487 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008488 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008489
8490 if (action_arg->v_type != VAR_UNKNOWN
8491 && what_arg->v_type != VAR_UNKNOWN)
8492 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008493 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8494 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008495 else
8496 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008497 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008498 valid_dict = FALSE;
8499 }
8500 }
8501
8502 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008503 if (l != NULL && action && valid_dict
8504 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008505 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008506 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008507 rettv->vval.v_number = 0;
8508 --recursive;
8509 }
8510# endif
8511}
8512
8513/*
8514 * "setloclist()" function
8515 */
8516 void
8517f_setloclist(typval_T *argvars, typval_T *rettv)
8518{
8519 win_T *win;
8520
8521 rettv->vval.v_number = -1;
8522
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008523 if (in_vim9script()
8524 && (check_for_number_arg(argvars, 0) == FAIL
8525 || check_for_list_arg(argvars, 1) == FAIL
8526 || check_for_opt_string_arg(argvars, 2) == FAIL
8527 || (argvars[2].v_type != VAR_UNKNOWN
8528 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8529 return;
8530
Bram Moolenaare677df82019-09-02 22:31:11 +02008531 win = find_win_by_nr_or_id(&argvars[0]);
8532 if (win != NULL)
8533 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8534}
8535
8536/*
8537 * "setqflist()" function
8538 */
8539 void
8540f_setqflist(typval_T *argvars, typval_T *rettv)
8541{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008542 if (in_vim9script()
8543 && (check_for_list_arg(argvars, 0) == FAIL
8544 || check_for_opt_string_arg(argvars, 1) == FAIL
8545 || (argvars[1].v_type != VAR_UNKNOWN
8546 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8547 return;
8548
Bram Moolenaare677df82019-09-02 22:31:11 +02008549 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8550}
8551#endif