blob: 5e0d3c35247df0022ab43deabb948684d76bc6d0 [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
33 int qf_fnum; // file number for the line
34 int qf_col; // column where the error occurred
35 int qf_nr; // error number
36 char_u *qf_module; // module name for this error
37 char_u *qf_pattern; // search pattern for the error
38 char_u *qf_text; // description of the error
39 char_u qf_viscol; // set to TRUE if qf_col is screen column
40 char_u qf_cleared; // set to TRUE if line has been deleted
41 char_u qf_type; // type of the error (mostly 'E'); 1 for
42 // :helpgrep
43 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010051#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020053/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010054 * Quickfix list type.
55 */
56typedef enum
57{
58 QFLT_QUICKFIX, // Quickfix list - global list
59 QFLT_LOCATION, // Location list - per window list
60 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
61} qfltype_T;
62
63/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020064 * Quickfix/Location list definition
65 * Contains a list of entries (qfline_T). qf_start points to the first entry
66 * and qf_last points to the last entry. qf_count contains the list size.
67 *
68 * Usually the list contains one or more entries. But an empty list can be
69 * created using setqflist()/setloclist() with a title and/or user context
70 * information and entries can be added later using setqflist()/setloclist().
71 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000072typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020074 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010075 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020076 qfline_T *qf_start; // pointer to the first error
77 qfline_T *qf_last; // pointer to the last error
78 qfline_T *qf_ptr; // pointer to the current error
79 int qf_count; // number of errors (0 means empty list)
80 int qf_index; // current index in the error list
81 int qf_nonevalid; // TRUE if not a single valid entry found
82 char_u *qf_title; // title derived from the command that created
83 // the error list or set by setqflist
84 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaard43906d2020-07-20 21:31:32 +020085 callback_T qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020086
87 struct dir_stack_T *qf_dir_stack;
88 char_u *qf_directory;
89 struct dir_stack_T *qf_file_stack;
90 char_u *qf_currfile;
91 int qf_multiline;
92 int qf_multiignore;
93 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010094 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000095} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020097/*
98 * Quickfix/Location list stack definition
99 * Contains a list of quickfix/location lists (qf_list_T)
100 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000101struct qf_info_S
102{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200103 // Count of references to this list. Used only for location lists.
104 // When a location list window reference this list, qf_refcount
105 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
106 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000107 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200108 int qf_listcount; // current number of lists
109 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100111 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100112 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113};
114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200115static qf_info_T ql_info; // global quickfix list
116static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200118#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
121 * Structure used to hold the info of one part of 'errorformat'
122 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000123typedef struct efm_S efm_T;
124struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200126 regprog_T *prog; // pre-formatted part of 'errorformat'
127 efm_T *next; // pointer to next (NULL if last)
128 char_u addr[FMT_PATTERNS]; // indices of used % patterns
129 char_u prefix; // prefix of this format line:
130 // 'D' enter directory
131 // 'X' leave directory
132 // 'A' start of multi-line message
133 // 'E' error message
134 // 'W' warning message
135 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200136 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200137 // 'C' continuation line
138 // 'Z' end of multi-line message
139 // 'G' general, unspecific message
140 // 'P' push file (partial) message
141 // 'Q' pop/quit file (partial) message
142 // 'O' overread (partial) message
143 char_u flags; // additional flags given in prefix
144 // '-' do not include this line
145 // '+' include whole line in message
146 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147};
148
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200149// List of location lists to be deleted.
150// Used to delay the deletion of locations lists by autocmds.
151typedef struct qf_delq_S
152{
153 struct qf_delq_S *next;
154 qf_info_T *qi;
155} qf_delq_T;
156static qf_delq_T *qf_delq_head = NULL;
157
158// Counter to prevent autocmds from freeing up location lists when they are
159// still being used.
160static int quickfix_busy = 0;
161
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200162static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100163
Bram Moolenaard43906d2020-07-20 21:31:32 +0200164// callback function for 'quickfixtextfunc'
165static callback_T qftf_cb;
166
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100168static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200169static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100171static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200172static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200174static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200175static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100178static win_T *qf_find_win(qf_info_T *qi);
179static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200180static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200181static 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 +0100182static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
183static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
184static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
185static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar3ff33112019-05-03 21:56:35 +0200186static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200188// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000189#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200190// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000191#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200192
193// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100194#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
195#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
196#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
197#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200198
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000199/*
200 * Return location list for window 'wp'
201 * For location list window, return the referenced location list
202 */
203#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
204
Bram Moolenaar95946f12019-03-31 15:31:59 +0200205// Macro to loop through all the items in a quickfix list
206// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100207#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200208 for (i = 1, qfp = qfl->qf_start; \
209 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100210 ++i, qfp = qfp->qf_next)
211
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200213 * Looking up a buffer can be slow if there are many. Remember the last one
214 * to make this a lot faster if there are multiple matches in the same file.
215 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200216static char_u *qf_last_bufname = NULL;
217static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200218
Bram Moolenaar4d170af2020-09-13 22:21:22 +0200219static char *e_current_quickfix_list_was_changed =
220 N_("E925: Current quickfix list was changed");
221static char *e_current_location_list_was_changed =
Bram Moolenaar3c097222017-12-21 20:54:49 +0100222 N_("E926: Current location list was changed");
223
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200224/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200225 * Maximum number of bytes allowed per line while reading a errorfile.
226 */
227#define LINE_MAXLEN 4096
228
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200229static struct fmtpattern
230{
231 char_u convchar;
232 char *pattern;
233} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200234 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200235 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200236 {'n', "\\d\\+"},
237 {'l', "\\d\\+"},
238 {'c', "\\d\\+"},
239 {'t', "."},
240 {'m', ".\\+"},
241 {'r', ".*"},
242 {'p', "[- .]*"},
243 {'v', "\\d\\+"},
244 {'s', ".\\+"},
245 {'o', ".\\+"}
246 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200247
248/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200249 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200250 * See fmt_pat definition above for the list of supported patterns. The
251 * pattern specifier is supplied in "efmpat". The converted pattern is stored
252 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253 */
254 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255efmpat_to_regpat(
256 char_u *efmpat,
257 char_u *regpat,
258 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200259 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100260 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200261{
262 char_u *srcptr;
263
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200266 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100267 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268 return NULL;
269 }
270 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200271 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200272 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100275 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 return NULL;
277 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 efminfo->addr[idx] = (char_u)++round;
279 *regpat++ = '\\';
280 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200282 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200283 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200284 // Also match "c:" in the file name, even when
285 // checking for a colon next: "%f:".
286 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 STRCPY(regpat, "\\%(\\a:\\)\\=");
288 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 }
290#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200293 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200295 // A file name may contain spaces, but this isn't
296 // in "\f". For "%f:%l:%m" there may be a ":" in
297 // the file name. Use ".\{-1,}x" instead (x is
298 // the next character), the requirement that :999:
299 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200300 STRCPY(regpat, ".\\{-1,}");
301 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 }
303 else
304 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200305 // File name followed by '\\' or '%': include as
306 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 STRCPY(regpat, "\\f\\+");
308 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200309 }
310 }
311 else
312 {
313 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200314 while ((*regpat = *srcptr++) != NUL)
315 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200316 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200317 *regpat++ = '\\';
318 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321}
322
323/*
324 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200325 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326 */
327 static char_u *
328scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200329 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 char_u *efm,
331 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100332 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333{
334 char_u *efmp = *pefmp;
335
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200336 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200338 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200339 {
340 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 if (efmp < efm + len)
343 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200344 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200345 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200346 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200347 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200348 if (efmp == efm + len)
349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100350 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 return NULL;
352 }
353 }
354 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200355 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200356 *regpat++ = *++efmp;
357 *regpat++ = '\\';
358 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200359 }
360 else
361 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200362 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100363 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 return NULL;
365 }
366
367 *pefmp = efmp;
368
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200369 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370}
371
372/*
373 * Analyze/parse an errorformat prefix.
374 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200375 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100376efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200377{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200379 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200380 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200381 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382 else
383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100384 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200385 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200386 }
387
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200388 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200389}
390
391/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200392 * Converts a 'errorformat' string part in 'efm' to a regular expression
393 * pattern. The resulting regex pattern is returned in "regpat". Additional
394 * information about the 'erroformat' pattern is returned in "fmt_ptr".
395 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200396 */
397 static int
398efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200399 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200400 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200401 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100402 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200403{
404 char_u *ptr;
405 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200406 int round;
407 int idx = 0;
408
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200409 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200410 ptr = regpat;
411 *ptr++ = '^';
412 round = 0;
413 for (efmp = efm; efmp < efm + len; ++efmp)
414 {
415 if (*efmp == '%')
416 {
417 ++efmp;
418 for (idx = 0; idx < FMT_PATTERNS; ++idx)
419 if (fmt_pat[idx].convchar == *efmp)
420 break;
421 if (idx < FMT_PATTERNS)
422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200424 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200425 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200426 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200427 }
428 else if (*efmp == '*')
429 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200430 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100431 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200432 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200433 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200434 }
435 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200436 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200437 else if (*efmp == '#')
438 *ptr++ = '*';
439 else if (*efmp == '>')
440 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200441 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200443 // prefix is allowed only at the beginning of the errorformat
444 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100445 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200446 if (efmp == NULL)
447 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200448 }
449 else
450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100451 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200452 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 }
454 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200455 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200456 {
457 if (*efmp == '\\' && efmp + 1 < efm + len)
458 ++efmp;
459 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200460 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200461 if (*efmp)
462 *ptr++ = *efmp;
463 }
464 }
465 *ptr++ = '$';
466 *ptr = NUL;
467
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200468 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200469}
470
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200471/*
472 * Free the 'errorformat' information list
473 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200474 static void
475free_efm_list(efm_T **efm_first)
476{
477 efm_T *efm_ptr;
478
479 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
480 {
481 *efm_first = efm_ptr->next;
482 vim_regfree(efm_ptr->prog);
483 vim_free(efm_ptr);
484 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100485 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200486}
487
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200488/*
489 * Compute the size of the buffer used to convert a 'errorformat' pattern into
490 * a regular expression pattern.
491 */
492 static int
493efm_regpat_bufsz(char_u *efm)
494{
495 int sz;
496 int i;
497
498 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
499 for (i = FMT_PATTERNS; i > 0; )
500 sz += (int)STRLEN(fmt_pat[--i].pattern);
501#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200502 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200503#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200504 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200505#endif
506
507 return sz;
508}
509
510/*
511 * Return the length of a 'errorformat' option part (separated by ",").
512 */
513 static int
514efm_option_part_len(char_u *efm)
515{
516 int len;
517
518 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
519 if (efm[len] == '\\' && efm[len + 1] != NUL)
520 ++len;
521
522 return len;
523}
524
525/*
526 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
527 * are parsed and converted to regular expressions. Returns information about
528 * the parsed 'errorformat' option.
529 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200530 static efm_T *
531parse_efm_option(char_u *efm)
532{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200533 efm_T *fmt_ptr = NULL;
534 efm_T *fmt_first = NULL;
535 efm_T *fmt_last = NULL;
536 char_u *fmtstr = NULL;
537 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200538 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200539
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200540 // Each part of the format string is copied and modified from errorformat
541 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200542
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200543 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200544 sz = efm_regpat_bufsz(efm);
545 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200546 goto parse_efm_error;
547
548 while (efm[0] != NUL)
549 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200550 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200551 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200552 if (fmt_ptr == NULL)
553 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200554 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555 fmt_first = fmt_ptr;
556 else
557 fmt_last->next = fmt_ptr;
558 fmt_last = fmt_ptr;
559
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200560 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200561 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200562
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100563 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200564 goto parse_efm_error;
565 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
566 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200567 // Advance to next part
568 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200569 }
570
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200571 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100572 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200573
574 goto parse_efm_end;
575
576parse_efm_error:
577 free_efm_list(&fmt_first);
578
579parse_efm_end:
580 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200581
582 return fmt_first;
583}
584
Bram Moolenaare0d37972016-07-15 22:36:01 +0200585enum {
586 QF_FAIL = 0,
587 QF_OK = 1,
588 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200589 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200590 QF_IGNORE_LINE = 4,
591 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200592};
593
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200594/*
595 * State information used to parse lines and add entries to a quickfix/location
596 * list.
597 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200598typedef struct {
599 char_u *linebuf;
600 int linelen;
601 char_u *growbuf;
602 int growbufsiz;
603 FILE *fd;
604 typval_T *tv;
605 char_u *p_str;
606 listitem_T *p_li;
607 buf_T *buf;
608 linenr_T buflnum;
609 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100610 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200611} qfstate_T;
612
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200613/*
614 * Allocate more memory for the line buffer used for parsing lines.
615 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616 static char_u *
617qf_grow_linebuf(qfstate_T *state, int newsz)
618{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200619 char_u *p;
620
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200621 // If the line exceeds LINE_MAXLEN exclude the last
622 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200623 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
624 if (state->growbuf == NULL)
625 {
626 state->growbuf = alloc(state->linelen + 1);
627 if (state->growbuf == NULL)
628 return NULL;
629 state->growbufsiz = state->linelen;
630 }
631 else if (state->linelen > state->growbufsiz)
632 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200633 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200634 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200635 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200636 state->growbufsiz = state->linelen;
637 }
638 return state->growbuf;
639}
640
641/*
642 * Get the next string (separated by newline) from state->p_str.
643 */
644 static int
645qf_get_next_str_line(qfstate_T *state)
646{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200647 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200648 char_u *p_str = state->p_str;
649 char_u *p;
650 int len;
651
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200652 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200653 return QF_END_OF_INPUT;
654
655 p = vim_strchr(p_str, '\n');
656 if (p != NULL)
657 len = (int)(p - p_str) + 1;
658 else
659 len = (int)STRLEN(p_str);
660
661 if (len > IOSIZE - 2)
662 {
663 state->linebuf = qf_grow_linebuf(state, len);
664 if (state->linebuf == NULL)
665 return QF_NOMEM;
666 }
667 else
668 {
669 state->linebuf = IObuff;
670 state->linelen = len;
671 }
672 vim_strncpy(state->linebuf, p_str, state->linelen);
673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200674 // Increment using len in order to discard the rest of the
675 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200676 p_str += len;
677 state->p_str = p_str;
678
679 return QF_OK;
680}
681
682/*
683 * Get the next string from state->p_Li.
684 */
685 static int
686qf_get_next_list_line(qfstate_T *state)
687{
688 listitem_T *p_li = state->p_li;
689 int len;
690
691 while (p_li != NULL
692 && (p_li->li_tv.v_type != VAR_STRING
693 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200694 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200695
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200696 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200697 {
698 state->p_li = NULL;
699 return QF_END_OF_INPUT;
700 }
701
702 len = (int)STRLEN(p_li->li_tv.vval.v_string);
703 if (len > IOSIZE - 2)
704 {
705 state->linebuf = qf_grow_linebuf(state, len);
706 if (state->linebuf == NULL)
707 return QF_NOMEM;
708 }
709 else
710 {
711 state->linebuf = IObuff;
712 state->linelen = len;
713 }
714
715 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
716
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200717 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200718 return QF_OK;
719}
720
721/*
722 * Get the next string from state->buf.
723 */
724 static int
725qf_get_next_buf_line(qfstate_T *state)
726{
727 char_u *p_buf = NULL;
728 int len;
729
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200730 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200731 if (state->buflnum > state->lnumlast)
732 return QF_END_OF_INPUT;
733
734 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
735 state->buflnum += 1;
736
737 len = (int)STRLEN(p_buf);
738 if (len > IOSIZE - 2)
739 {
740 state->linebuf = qf_grow_linebuf(state, len);
741 if (state->linebuf == NULL)
742 return QF_NOMEM;
743 }
744 else
745 {
746 state->linebuf = IObuff;
747 state->linelen = len;
748 }
749 vim_strncpy(state->linebuf, p_buf, state->linelen);
750
751 return QF_OK;
752}
753
754/*
755 * Get the next string from file state->fd.
756 */
757 static int
758qf_get_next_file_line(qfstate_T *state)
759{
760 int discard;
761 int growbuflen;
762
763 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
764 return QF_END_OF_INPUT;
765
766 discard = FALSE;
767 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200768 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200769 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200770 // The current line exceeds IObuff, continue reading using
771 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200772 if (state->growbuf == NULL)
773 {
774 state->growbufsiz = 2 * (IOSIZE - 1);
775 state->growbuf = alloc(state->growbufsiz);
776 if (state->growbuf == NULL)
777 return QF_NOMEM;
778 }
779
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200780 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200781 memcpy(state->growbuf, IObuff, IOSIZE - 1);
782 growbuflen = state->linelen;
783
784 for (;;)
785 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200786 char_u *p;
787
Bram Moolenaare0d37972016-07-15 22:36:01 +0200788 if (fgets((char *)state->growbuf + growbuflen,
789 state->growbufsiz - growbuflen, state->fd) == NULL)
790 break;
791 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
792 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200793 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 break;
795 if (state->growbufsiz == LINE_MAXLEN)
796 {
797 discard = TRUE;
798 break;
799 }
800
801 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
802 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200803 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200805 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806 }
807
808 while (discard)
809 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200810 // The current line is longer than LINE_MAXLEN, continue
811 // reading but discard everything until EOL or EOF is
812 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200813 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
814 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200815 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200816 break;
817 }
818
819 state->linebuf = state->growbuf;
820 state->linelen = growbuflen;
821 }
822 else
823 state->linebuf = IObuff;
824
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200825 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200826 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
827 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100828 char_u *line;
829
830 line = string_convert(&state->vc, state->linebuf, &state->linelen);
831 if (line != NULL)
832 {
833 if (state->linelen < IOSIZE)
834 {
835 STRCPY(state->linebuf, line);
836 vim_free(line);
837 }
838 else
839 {
840 vim_free(state->growbuf);
841 state->linebuf = state->growbuf = line;
842 state->growbufsiz = state->linelen < LINE_MAXLEN
843 ? state->linelen : LINE_MAXLEN;
844 }
845 }
846 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100847
Bram Moolenaare0d37972016-07-15 22:36:01 +0200848 return QF_OK;
849}
850
851/*
852 * Get the next string from a file/buffer/list/string.
853 */
854 static int
855qf_get_nextline(qfstate_T *state)
856{
857 int status = QF_FAIL;
858
859 if (state->fd == NULL)
860 {
861 if (state->tv != NULL)
862 {
863 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200864 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200865 status = qf_get_next_str_line(state);
866 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200867 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200868 status = qf_get_next_list_line(state);
869 }
870 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200871 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200872 status = qf_get_next_buf_line(state);
873 }
874 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200875 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200876 status = qf_get_next_file_line(state);
877
878 if (status != QF_OK)
879 return status;
880
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200881 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200883 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884 state->linebuf[state->linelen - 1] = NUL;
885#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200886 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
887 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200890
Bram Moolenaare0d37972016-07-15 22:36:01 +0200891 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200892
893 return QF_OK;
894}
895
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200896typedef struct {
897 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200898 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200899 char_u *errmsg;
900 int errmsglen;
901 long lnum;
902 int col;
903 char_u use_viscol;
904 char_u *pattern;
905 int enr;
906 int type;
907 int valid;
908} qffields_T;
909
910/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200911 * Parse the match for filename ('%f') pattern in regmatch.
912 * Return the matched value in "fields->namebuf".
913 */
914 static int
915qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
916{
917 int c;
918
919 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
920 return QF_FAIL;
921
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200922 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200923 c = *rmp->endp[midx];
924 *rmp->endp[midx] = NUL;
925 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
926 *rmp->endp[midx] = c;
927
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200928 // For separate filename patterns (%O, %P and %Q), the specified file
929 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200930 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
931 && mch_getperm(fields->namebuf) == -1)
932 return QF_FAIL;
933
934 return QF_OK;
935}
936
937/*
938 * Parse the match for error number ('%n') pattern in regmatch.
939 * Return the matched value in "fields->enr".
940 */
941 static int
942qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
943{
944 if (rmp->startp[midx] == NULL)
945 return QF_FAIL;
946 fields->enr = (int)atol((char *)rmp->startp[midx]);
947 return QF_OK;
948}
949
950/*
951 * Parse the match for line number (%l') pattern in regmatch.
952 * Return the matched value in "fields->lnum".
953 */
954 static int
955qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
956{
957 if (rmp->startp[midx] == NULL)
958 return QF_FAIL;
959 fields->lnum = atol((char *)rmp->startp[midx]);
960 return QF_OK;
961}
962
963/*
964 * Parse the match for column number ('%c') pattern in regmatch.
965 * Return the matched value in "fields->col".
966 */
967 static int
968qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
969{
970 if (rmp->startp[midx] == NULL)
971 return QF_FAIL;
972 fields->col = (int)atol((char *)rmp->startp[midx]);
973 return QF_OK;
974}
975
976/*
977 * Parse the match for error type ('%t') pattern in regmatch.
978 * Return the matched value in "fields->type".
979 */
980 static int
981qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
982{
983 if (rmp->startp[midx] == NULL)
984 return QF_FAIL;
985 fields->type = *rmp->startp[midx];
986 return QF_OK;
987}
988
989/*
Bram Moolenaarf4140482020-02-15 23:06:45 +0100990 * Copy a non-error line into the error string. Return the matched line in
991 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200992 */
993 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +0100994copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200995{
996 char_u *p;
997
998 if (linelen >= fields->errmsglen)
999 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001000 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001001 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1002 return QF_NOMEM;
1003 fields->errmsg = p;
1004 fields->errmsglen = linelen + 1;
1005 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001006 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001007 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001008
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001009 return QF_OK;
1010}
1011
1012/*
1013 * Parse the match for error message ('%m') pattern in regmatch.
1014 * Return the matched value in "fields->errmsg".
1015 */
1016 static int
1017qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1018{
1019 char_u *p;
1020 int len;
1021
1022 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1023 return QF_FAIL;
1024 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1025 if (len >= fields->errmsglen)
1026 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001027 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001028 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1029 return QF_NOMEM;
1030 fields->errmsg = p;
1031 fields->errmsglen = len + 1;
1032 }
1033 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1034 return QF_OK;
1035}
1036
1037/*
1038 * Parse the match for rest of a single-line file message ('%r') pattern.
1039 * Return the matched value in "tail".
1040 */
1041 static int
1042qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1043{
1044 if (rmp->startp[midx] == NULL)
1045 return QF_FAIL;
1046 *tail = rmp->startp[midx];
1047 return QF_OK;
1048}
1049
1050/*
1051 * Parse the match for the pointer line ('%p') pattern in regmatch.
1052 * Return the matched value in "fields->col".
1053 */
1054 static int
1055qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1056{
1057 char_u *match_ptr;
1058
1059 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1060 return QF_FAIL;
1061 fields->col = 0;
1062 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1063 ++match_ptr)
1064 {
1065 ++fields->col;
1066 if (*match_ptr == TAB)
1067 {
1068 fields->col += 7;
1069 fields->col -= fields->col % 8;
1070 }
1071 }
1072 ++fields->col;
1073 fields->use_viscol = TRUE;
1074 return QF_OK;
1075}
1076
1077/*
1078 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1079 * Return the matched value in "fields->col".
1080 */
1081 static int
1082qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1083{
1084 if (rmp->startp[midx] == NULL)
1085 return QF_FAIL;
1086 fields->col = (int)atol((char *)rmp->startp[midx]);
1087 fields->use_viscol = TRUE;
1088 return QF_OK;
1089}
1090
1091/*
1092 * Parse the match for the search text ('%s') pattern in regmatch.
1093 * Return the matched value in "fields->pattern".
1094 */
1095 static int
1096qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1097{
1098 int len;
1099
1100 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1101 return QF_FAIL;
1102 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1103 if (len > CMDBUFFSIZE - 5)
1104 len = CMDBUFFSIZE - 5;
1105 STRCPY(fields->pattern, "^\\V");
1106 STRNCAT(fields->pattern, rmp->startp[midx], len);
1107 fields->pattern[len + 3] = '\\';
1108 fields->pattern[len + 4] = '$';
1109 fields->pattern[len + 5] = NUL;
1110 return QF_OK;
1111}
1112
1113/*
1114 * Parse the match for the module ('%o') pattern in regmatch.
1115 * Return the matched value in "fields->module".
1116 */
1117 static int
1118qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1119{
1120 int len;
1121
1122 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1123 return QF_FAIL;
1124 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1125 if (len > CMDBUFFSIZE)
1126 len = CMDBUFFSIZE;
1127 STRNCAT(fields->module, rmp->startp[midx], len);
1128 return QF_OK;
1129}
1130
1131/*
1132 * 'errorformat' format pattern parser functions.
1133 * The '%f' and '%r' formats are parsed differently from other formats.
1134 * See qf_parse_match() for details.
1135 */
1136static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1137{
1138 NULL,
1139 qf_parse_fmt_n,
1140 qf_parse_fmt_l,
1141 qf_parse_fmt_c,
1142 qf_parse_fmt_t,
1143 qf_parse_fmt_m,
1144 NULL,
1145 qf_parse_fmt_p,
1146 qf_parse_fmt_v,
1147 qf_parse_fmt_s,
1148 qf_parse_fmt_o
1149};
1150
1151/*
1152 * Parse the error format pattern matches in "regmatch" and set the values in
1153 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1154 * match. Returns QF_OK if all the matches are successfully parsed. On
1155 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001156 */
1157 static int
1158qf_parse_match(
1159 char_u *linebuf,
1160 int linelen,
1161 efm_T *fmt_ptr,
1162 regmatch_T *regmatch,
1163 qffields_T *fields,
1164 int qf_multiline,
1165 int qf_multiscan,
1166 char_u **tail)
1167{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001168 int idx = fmt_ptr->prefix;
1169 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001170 int midx;
1171 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001172
1173 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1174 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001175 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001176 fields->type = idx;
1177 else
1178 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001179
1180 // Extract error message data from matched line.
1181 // We check for an actual submatch, because "\[" and "\]" in
1182 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001183 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001184 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001185 status = QF_OK;
1186 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001187 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1189 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001191 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001192 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001193 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001195 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001196 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001197 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001198 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001199 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001200
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001201 if (status != QF_OK)
1202 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001203 }
1204
1205 return QF_OK;
1206}
1207
1208/*
1209 * Parse an error line in 'linebuf' using a single error format string in
1210 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1211 * Returns QF_OK if the efm format matches completely and the fields are
1212 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1213 */
1214 static int
1215qf_parse_get_fields(
1216 char_u *linebuf,
1217 int linelen,
1218 efm_T *fmt_ptr,
1219 qffields_T *fields,
1220 int qf_multiline,
1221 int qf_multiscan,
1222 char_u **tail)
1223{
1224 regmatch_T regmatch;
1225 int status = QF_FAIL;
1226 int r;
1227
1228 if (qf_multiscan &&
1229 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1230 return QF_FAIL;
1231
1232 fields->namebuf[0] = NUL;
1233 fields->module[0] = NUL;
1234 fields->pattern[0] = NUL;
1235 if (!qf_multiscan)
1236 fields->errmsg[0] = NUL;
1237 fields->lnum = 0;
1238 fields->col = 0;
1239 fields->use_viscol = FALSE;
1240 fields->enr = -1;
1241 fields->type = 0;
1242 *tail = NULL;
1243
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001244 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001245 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001246 regmatch.regprog = fmt_ptr->prog;
1247 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1248 fmt_ptr->prog = regmatch.regprog;
1249 if (r)
1250 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1251 fields, qf_multiline, qf_multiscan, tail);
1252
1253 return status;
1254}
1255
1256/*
1257 * Parse directory error format prefixes (%D and %X).
1258 * Push and pop directories from the directory stack when scanning directory
1259 * names.
1260 */
1261 static int
1262qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1263{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001264 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001265 {
1266 if (*fields->namebuf == NUL)
1267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001268 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001269 return QF_FAIL;
1270 }
1271 qfl->qf_directory =
1272 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1273 if (qfl->qf_directory == NULL)
1274 return QF_FAIL;
1275 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001276 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001277 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1278
1279 return QF_OK;
1280}
1281
1282/*
1283 * Parse global file name error format prefixes (%O, %P and %Q).
1284 */
1285 static int
1286qf_parse_file_pfx(
1287 int idx,
1288 qffields_T *fields,
1289 qf_list_T *qfl,
1290 char_u *tail)
1291{
1292 fields->valid = FALSE;
1293 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1294 {
1295 if (*fields->namebuf && idx == 'P')
1296 qfl->qf_currfile =
1297 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1298 else if (idx == 'Q')
1299 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1300 *fields->namebuf = NUL;
1301 if (tail && *tail)
1302 {
1303 STRMOVE(IObuff, skipwhite(tail));
1304 qfl->qf_multiscan = TRUE;
1305 return QF_MULTISCAN;
1306 }
1307 }
1308
1309 return QF_OK;
1310}
1311
1312/*
1313 * Parse a non-error line (a line which doesn't match any of the error
1314 * format in 'efm').
1315 */
1316 static int
1317qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1318{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001319 fields->namebuf[0] = NUL; // no match found, remove file name
1320 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001321 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001322
Bram Moolenaarf4140482020-02-15 23:06:45 +01001323 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001324}
1325
1326/*
1327 * Parse multi-line error format prefixes (%C and %Z)
1328 */
1329 static int
1330qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001331 int idx,
1332 qf_list_T *qfl,
1333 qffields_T *fields)
1334{
1335 char_u *ptr;
1336 int len;
1337
1338 if (!qfl->qf_multiignore)
1339 {
1340 qfline_T *qfprev = qfl->qf_last;
1341
1342 if (qfprev == NULL)
1343 return QF_FAIL;
1344 if (*fields->errmsg && !qfl->qf_multiignore)
1345 {
1346 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001347 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001348 == NULL)
1349 return QF_FAIL;
1350 STRCPY(ptr, qfprev->qf_text);
1351 vim_free(qfprev->qf_text);
1352 qfprev->qf_text = ptr;
1353 *(ptr += len) = '\n';
1354 STRCPY(++ptr, fields->errmsg);
1355 }
1356 if (qfprev->qf_nr == -1)
1357 qfprev->qf_nr = fields->enr;
1358 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001359 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001360 qfprev->qf_type = fields->type;
1361
1362 if (!qfprev->qf_lnum)
1363 qfprev->qf_lnum = fields->lnum;
1364 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001365 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001366 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001367 qfprev->qf_viscol = fields->use_viscol;
1368 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001369 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001370 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001371 qfl->qf_directory,
1372 *fields->namebuf || qfl->qf_directory != NULL
1373 ? fields->namebuf
1374 : qfl->qf_currfile != NULL && fields->valid
1375 ? qfl->qf_currfile : 0);
1376 }
1377 if (idx == 'Z')
1378 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1379 line_breakcheck();
1380
1381 return QF_IGNORE_LINE;
1382}
1383
1384/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001385 * Parse a line and get the quickfix fields.
1386 * Return the QF_ status.
1387 */
1388 static int
1389qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001390 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001391 char_u *linebuf,
1392 int linelen,
1393 efm_T *fmt_first,
1394 qffields_T *fields)
1395{
1396 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001397 int idx = 0;
1398 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001399 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400
Bram Moolenaare333e792018-04-08 13:27:39 +02001401restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001402 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001403 if (fmt_start == NULL)
1404 fmt_ptr = fmt_first;
1405 else
1406 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001407 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408 fmt_ptr = fmt_start;
1409 fmt_start = NULL;
1410 }
1411
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001412 // Try to match each part of 'errorformat' until we find a complete
1413 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001414 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1416 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001418 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1419 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1420 if (status == QF_NOMEM)
1421 return status;
1422 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001424 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001425 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001426
1427 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1428 {
1429 if (fmt_ptr != NULL)
1430 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001431 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001432 status = qf_parse_dir_pfx(idx, fields, qfl);
1433 if (status != QF_OK)
1434 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001436
1437 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1438 if (status != QF_OK)
1439 return status;
1440
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001442 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 }
1444 else if (fmt_ptr != NULL)
1445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001446 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 if (fmt_ptr->conthere)
1448 fmt_start = fmt_ptr;
1449
Bram Moolenaare9283662020-06-07 14:10:47 +02001450 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001451 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001452 qfl->qf_multiline = TRUE; // start of a multi-line message
1453 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 }
1455 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001456 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001457 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001458 if (status != QF_OK)
1459 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 }
1461 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001462 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001463 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1464 if (status == QF_MULTISCAN)
1465 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001467 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001469 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001470 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001471 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001472 return QF_IGNORE_LINE;
1473 }
1474 }
1475
1476 return QF_OK;
1477}
1478
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001479/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001480 * Returns TRUE if the specified quickfix/location stack is empty
1481 */
1482 static int
1483qf_stack_empty(qf_info_T *qi)
1484{
1485 return qi == NULL || qi->qf_listcount <= 0;
1486}
1487
1488/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001489 * Returns TRUE if the specified quickfix/location list is empty.
1490 */
1491 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001492qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001493{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001494 return qfl == NULL || qfl->qf_count <= 0;
1495}
1496
1497/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001498 * Returns TRUE if the specified quickfix/location list is not empty and
1499 * has valid entries.
1500 */
1501 static int
1502qf_list_has_valid_entries(qf_list_T *qfl)
1503{
1504 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1505}
1506
1507/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001508 * Return a pointer to a list in the specified quickfix stack
1509 */
1510 static qf_list_T *
1511qf_get_list(qf_info_T *qi, int idx)
1512{
1513 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001514}
1515
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001516/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001517 * Allocate the fields used for parsing lines and populating a quickfix list.
1518 */
1519 static int
1520qf_alloc_fields(qffields_T *pfields)
1521{
1522 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1523 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1524 pfields->errmsglen = CMDBUFFSIZE + 1;
1525 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1526 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1527 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1528 || pfields->pattern == NULL || pfields->module == NULL)
1529 return FAIL;
1530
1531 return OK;
1532}
1533
1534/*
1535 * Free the fields used for parsing lines and populating a quickfix list.
1536 */
1537 static void
1538qf_free_fields(qffields_T *pfields)
1539{
1540 vim_free(pfields->namebuf);
1541 vim_free(pfields->module);
1542 vim_free(pfields->errmsg);
1543 vim_free(pfields->pattern);
1544}
1545
1546/*
1547 * Setup the state information used for parsing lines and populating a
1548 * quickfix list.
1549 */
1550 static int
1551qf_setup_state(
1552 qfstate_T *pstate,
1553 char_u *enc,
1554 char_u *efile,
1555 typval_T *tv,
1556 buf_T *buf,
1557 linenr_T lnumfirst,
1558 linenr_T lnumlast)
1559{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001560 pstate->vc.vc_type = CONV_NONE;
1561 if (enc != NULL && *enc != NUL)
1562 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001563
1564 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001566 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001567 return FAIL;
1568 }
1569
1570 if (tv != NULL)
1571 {
1572 if (tv->v_type == VAR_STRING)
1573 pstate->p_str = tv->vval.v_string;
1574 else if (tv->v_type == VAR_LIST)
1575 pstate->p_li = tv->vval.v_list->lv_first;
1576 pstate->tv = tv;
1577 }
1578 pstate->buf = buf;
1579 pstate->buflnum = lnumfirst;
1580 pstate->lnumlast = lnumlast;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Cleanup the state information used for parsing lines and populating a
1587 * quickfix list.
1588 */
1589 static void
1590qf_cleanup_state(qfstate_T *pstate)
1591{
1592 if (pstate->fd != NULL)
1593 fclose(pstate->fd);
1594
1595 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001596 if (pstate->vc.vc_type != CONV_NONE)
1597 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001598}
1599
1600/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001601 * Process the next line from a file/buffer/list/string and add it
1602 * to the quickfix list 'qfl'.
1603 */
1604 static int
1605qf_init_process_nextline(
1606 qf_list_T *qfl,
1607 efm_T *fmt_first,
1608 qfstate_T *state,
1609 qffields_T *fields)
1610{
1611 int status;
1612
1613 // Get the next line from a file/buffer/list/string
1614 status = qf_get_nextline(state);
1615 if (status != QF_OK)
1616 return status;
1617
1618 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1619 fmt_first, fields);
1620 if (status != QF_OK)
1621 return status;
1622
1623 return qf_add_entry(qfl,
1624 qfl->qf_directory,
1625 (*fields->namebuf || qfl->qf_directory != NULL)
1626 ? fields->namebuf
1627 : ((qfl->qf_currfile != NULL && fields->valid)
1628 ? qfl->qf_currfile : (char_u *)NULL),
1629 fields->module,
1630 0,
1631 fields->errmsg,
1632 fields->lnum,
1633 fields->col,
1634 fields->use_viscol,
1635 fields->pattern,
1636 fields->enr,
1637 fields->type,
1638 fields->valid);
1639}
1640
1641/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001642 * Read the errorfile "efile" into memory, line by line, building the error
1643 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001644 * Alternative: when "efile" is NULL read errors from buffer "buf".
1645 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001646 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001647 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1648 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001649 * Return -1 for error, number of errors for success.
1650 */
1651 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001652qf_init_ext(
1653 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001654 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001655 char_u *efile,
1656 buf_T *buf,
1657 typval_T *tv,
1658 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001659 int newlist, // TRUE: start a new error list
1660 linenr_T lnumfirst, // first line number to use
1661 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001662 char_u *qf_title,
1663 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001664{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001665 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001666 qfstate_T state;
1667 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001668 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001669 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001670 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001672 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001673 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001674 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001676 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001677 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001678
Bram Moolenaara80faa82020-04-12 19:37:17 +02001679 CLEAR_FIELD(state);
1680 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001681 if ((qf_alloc_fields(&fields) == FAIL) ||
1682 (qf_setup_state(&state, enc, efile, tv, buf,
1683 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 goto qf_init_end;
1685
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001686 if (newlist || qf_idx == qi->qf_listcount)
1687 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001688 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001689 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001690 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001691 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001693 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001694 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001695 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001696 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001697 qfl = qf_get_list(qi, qf_idx);
1698 if (!qf_list_empty(qfl))
1699 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001702 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001703 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001704 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 else
1706 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001708 // If the errorformat didn't change between calls, then reuse the
1709 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001710 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1711 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001712 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001713 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001714 free_efm_list(&fmt_first);
1715
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001716 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001717 fmt_first = parse_efm_option(efm);
1718 if (fmt_first != NULL)
1719 last_efm = vim_strsave(efm);
1720 }
1721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001722 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001725 // got_int is reset here, because it was probably set when killing the
1726 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 got_int = FALSE;
1728
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001729 // Read the lines in the error file one by one.
1730 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001731 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001733 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001734 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001735 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001736 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001737 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001738 if (status == QF_FAIL)
1739 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 line_breakcheck();
1742 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001743 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001745 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001747 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001748 qfl->qf_ptr = qfl->qf_start;
1749 qfl->qf_index = 1;
1750 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 else
1753 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 qfl->qf_nonevalid = FALSE;
1755 if (qfl->qf_ptr == NULL)
1756 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001758 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001759 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001760 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001762 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001764 if (!adding)
1765 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001766 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001767 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001768 qi->qf_listcount--;
1769 if (qi->qf_curlist > 0)
1770 --qi->qf_curlist;
1771 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001772qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001773 if (qf_idx == qi->qf_curlist)
1774 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001775 qf_cleanup_state(&state);
1776 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 return retval;
1779}
1780
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001781/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001782 * Read the errorfile "efile" into memory, line by line, building the error
1783 * list. Set the error list's title to qf_title.
1784 * Return -1 for error, number of errors for success.
1785 */
1786 int
1787qf_init(win_T *wp,
1788 char_u *efile,
1789 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001790 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001791 char_u *qf_title,
1792 char_u *enc)
1793{
1794 qf_info_T *qi = &ql_info;
1795
1796 if (wp != NULL)
1797 {
1798 qi = ll_get_or_alloc_list(wp);
1799 if (qi == NULL)
1800 return FAIL;
1801 }
1802
1803 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1804 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1805}
1806
1807/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001808 * Set the title of the specified quickfix list. Frees the previous title.
1809 * Prepends ':' to the title.
1810 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001811 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001814 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001815
Bram Moolenaarfb604092014-07-23 15:55:00 +02001816 if (title != NULL)
1817 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001818 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001819
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001820 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001821 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001822 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001823 }
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001827 * The title of a quickfix/location list is set, by default, to the command
1828 * that created the quickfix list with the ":" prefix.
1829 * Create a quickfix list title string by prepending ":" to a user command.
1830 * Returns a pointer to a static buffer with the title.
1831 */
1832 static char_u *
1833qf_cmdtitle(char_u *cmd)
1834{
1835 static char_u qftitle_str[IOSIZE];
1836
1837 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1838 return qftitle_str;
1839}
1840
1841/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001842 * Return a pointer to the current list in the specified quickfix stack
1843 */
1844 static qf_list_T *
1845qf_get_curlist(qf_info_T *qi)
1846{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001847 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001848}
1849
1850/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001851 * Prepare for adding a new quickfix list. If the current list is in the
1852 * middle of the stack, then all the following lists are freed and then
1853 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 */
1855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001856qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001859 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001861 // If the current entry is not the last entry, delete entries beyond
1862 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001863 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001865 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001867 // When the stack is full, remove to oldest entry
1868 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001871 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873 qi->qf_lists[i - 1] = qi->qf_lists[i];
1874 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001878 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001879 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001880 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001881 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001882 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883}
1884
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001885/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001886 * Queue location list stack delete request.
1887 */
1888 static void
1889locstack_queue_delreq(qf_info_T *qi)
1890{
1891 qf_delq_T *q;
1892
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001893 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001894 if (q != NULL)
1895 {
1896 q->qi = qi;
1897 q->next = qf_delq_head;
1898 qf_delq_head = q;
1899 }
1900}
1901
1902/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001903 * Return the global quickfix stack window buffer number.
1904 */
1905 int
1906qf_stack_get_bufnr(void)
1907{
1908 return ql_info.qf_bufnr;
1909}
1910
1911/*
1912 * Wipe the quickfix window buffer (if present) for the specified
1913 * quickfix/location list.
1914 */
1915 static void
1916wipe_qf_buffer(qf_info_T *qi)
1917{
1918 buf_T *qfbuf;
1919
1920 if (qi->qf_bufnr != INVALID_QFBUFNR)
1921 {
1922 qfbuf = buflist_findnr(qi->qf_bufnr);
1923 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1924 {
1925 // If the quickfix buffer is not loaded in any window, then
1926 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001927 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001928 qi->qf_bufnr = INVALID_QFBUFNR;
1929 }
1930 }
1931}
1932
1933/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001934 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001935 */
1936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001937ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001938{
1939 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001940 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001941
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 qi = *pqi;
1943 if (qi == NULL)
1944 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001945 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001947 // If the location list is still in use, then queue the delete request
1948 // to be processed later.
1949 if (quickfix_busy > 0)
1950 {
1951 locstack_queue_delreq(qi);
1952 return;
1953 }
1954
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 qi->qf_refcount--;
1956 if (qi->qf_refcount < 1)
1957 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001958 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001959 // If the quickfix window buffer is loaded, then wipe it
1960 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001961
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001962 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001963 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001964 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001965 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001966}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001967
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001968/*
1969 * Free all the quickfix/location lists in the stack.
1970 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001971 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001972qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973{
1974 int i;
1975 qf_info_T *qi = &ql_info;
1976
1977 if (wp != NULL)
1978 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001979 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 ll_free_all(&wp->w_llist);
1981 ll_free_all(&wp->w_llist_ref);
1982 }
1983 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001984 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001985 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001986 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001987}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001990 * Delay freeing of location list stacks when the quickfix code is running.
1991 * Used to avoid problems with autocmds freeing location list stacks when the
1992 * quickfix code is still referencing the stack.
1993 * Must always call decr_quickfix_busy() exactly once after this.
1994 */
1995 static void
1996incr_quickfix_busy(void)
1997{
1998 quickfix_busy++;
1999}
2000
2001/*
2002 * Safe to free location list stacks. Process any delayed delete requests.
2003 */
2004 static void
2005decr_quickfix_busy(void)
2006{
2007 if (--quickfix_busy == 0)
2008 {
2009 // No longer referencing the location lists. Process all the pending
2010 // delete requests.
2011 while (qf_delq_head != NULL)
2012 {
2013 qf_delq_T *q = qf_delq_head;
2014
2015 qf_delq_head = q->next;
2016 ll_free_all(&q->qi);
2017 vim_free(q);
2018 }
2019 }
2020#ifdef ABORT_ON_INTERNAL_ERROR
2021 if (quickfix_busy < 0)
2022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002023 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002024 abort();
2025 }
2026#endif
2027}
2028
2029#if defined(EXITFREE) || defined(PROTO)
2030 void
2031check_quickfix_busy(void)
2032{
2033 if (quickfix_busy != 0)
2034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002036# ifdef ABORT_ON_INTERNAL_ERROR
2037 abort();
2038# endif
2039 }
2040}
2041#endif
2042
2043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002045 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 */
2047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002048qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002049 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002050 char_u *dir, // optional directory name
2051 char_u *fname, // file name or NULL
2052 char_u *module, // module name or NULL
2053 int bufnum, // buffer number or zero
2054 char_u *mesg, // message
2055 long lnum, // line number
2056 int col, // column
2057 int vis_col, // using visual column
2058 char_u *pattern, // search pattern
2059 int nr, // error number
2060 int type, // type character
2061 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002063 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002064 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002066 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002067 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002068 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002069 {
2070 buf_T *buf = buflist_findnr(bufnum);
2071
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002072 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002073 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002074 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002075 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002076 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002077 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002078 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2080 {
2081 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002082 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 qfp->qf_lnum = lnum;
2085 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002086 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002087 if (pattern == NULL || *pattern == NUL)
2088 qfp->qf_pattern = NULL;
2089 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2090 {
2091 vim_free(qfp->qf_text);
2092 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002093 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002094 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002095 if (module == NULL || *module == NUL)
2096 qfp->qf_module = NULL;
2097 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2098 {
2099 vim_free(qfp->qf_text);
2100 vim_free(qfp->qf_pattern);
2101 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002102 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002105 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 type = 0;
2107 qfp->qf_type = type;
2108 qfp->qf_valid = valid;
2109
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002110 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002111 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002113 qfl->qf_start = qfp;
2114 qfl->qf_ptr = qfp;
2115 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002116 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118 else
2119 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002120 qfp->qf_prev = *lastp;
2121 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002123 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002125 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002126 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002127 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002129 qfl->qf_index = qfl->qf_count;
2130 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132
Bram Moolenaar95946f12019-03-31 15:31:59 +02002133 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134}
2135
2136/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002137 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002139 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002140qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002141{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002142 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002143
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002144 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002145 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002146 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002147 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002148 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002149 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002150 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002151 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152}
2153
2154/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002155 * Return the location list stack for window 'wp'.
2156 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 */
2158 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002159ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160{
2161 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002162 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 return wp->w_llist_ref;
2164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002165 // For a non-location list window, w_llist_ref should not point to a
2166 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002167 ll_free_all(&wp->w_llist_ref);
2168
2169 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002170 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171 return wp->w_llist;
2172}
2173
2174/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002175 * Get the quickfix/location list stack to use for the specified Ex command.
2176 * For a location list command, returns the stack for the current window. If
2177 * the location list is not found, then returns NULL and prints an error
2178 * message if 'print_emsg' is TRUE.
2179 */
2180 static qf_info_T *
2181qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2182{
2183 qf_info_T *qi = &ql_info;
2184
2185 if (is_loclist_cmd(eap->cmdidx))
2186 {
2187 qi = GET_LOC_LIST(curwin);
2188 if (qi == NULL)
2189 {
2190 if (print_emsg)
2191 emsg(_(e_loclist));
2192 return NULL;
2193 }
2194 }
2195
2196 return qi;
2197}
2198
2199/*
2200 * Get the quickfix/location list stack to use for the specified Ex command.
2201 * For a location list command, returns the stack for the current window.
2202 * If the location list is not present, then allocates a new one.
2203 * Returns NULL if the allocation fails. For a location list command, sets
2204 * 'pwinp' to curwin.
2205 */
2206 static qf_info_T *
2207qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2208{
2209 qf_info_T *qi = &ql_info;
2210
2211 if (is_loclist_cmd(eap->cmdidx))
2212 {
2213 qi = ll_get_or_alloc_list(curwin);
2214 if (qi == NULL)
2215 return NULL;
2216 *pwinp = curwin;
2217 }
2218
2219 return qi;
2220}
2221
2222/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002223 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2224 */
2225 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002226copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002227{
2228 int i;
2229 qfline_T *from_qfp;
2230 qfline_T *prevp;
2231
2232 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002233 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002234 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002235 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002236 NULL,
2237 NULL,
2238 from_qfp->qf_module,
2239 0,
2240 from_qfp->qf_text,
2241 from_qfp->qf_lnum,
2242 from_qfp->qf_col,
2243 from_qfp->qf_viscol,
2244 from_qfp->qf_pattern,
2245 from_qfp->qf_nr,
2246 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002247 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002248 return FAIL;
2249
2250 // qf_add_entry() will not set the qf_num field, as the
2251 // directory and file names are not supplied. So the qf_fnum
2252 // field is copied here.
2253 prevp = to_qfl->qf_last;
2254 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2255 prevp->qf_type = from_qfp->qf_type; // error type
2256 if (from_qfl->qf_ptr == from_qfp)
2257 to_qfl->qf_ptr = prevp; // current location
2258 }
2259
2260 return OK;
2261}
2262
2263/*
2264 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2265 */
2266 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002267copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002268{
2269 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002270 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002271 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2272 to_qfl->qf_count = 0;
2273 to_qfl->qf_index = 0;
2274 to_qfl->qf_start = NULL;
2275 to_qfl->qf_last = NULL;
2276 to_qfl->qf_ptr = NULL;
2277 if (from_qfl->qf_title != NULL)
2278 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2279 else
2280 to_qfl->qf_title = NULL;
2281 if (from_qfl->qf_ctx != NULL)
2282 {
2283 to_qfl->qf_ctx = alloc_tv();
2284 if (to_qfl->qf_ctx != NULL)
2285 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2286 }
2287 else
2288 to_qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002289 if (from_qfl->qftf_cb.cb_name != NULL)
2290 copy_callback(&to_qfl->qftf_cb, &from_qfl->qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002291 else
Bram Moolenaard43906d2020-07-20 21:31:32 +02002292 to_qfl->qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002293
2294 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002295 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002296 return FAIL;
2297
2298 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2299
2300 // Assign a new ID for the location list
2301 to_qfl->qf_id = ++last_qf_id;
2302 to_qfl->qf_changedtick = 0L;
2303
2304 // When no valid entries are present in the list, qf_ptr points to
2305 // the first item in the list
2306 if (to_qfl->qf_nonevalid)
2307 {
2308 to_qfl->qf_ptr = to_qfl->qf_start;
2309 to_qfl->qf_index = 1;
2310 }
2311
2312 return OK;
2313}
2314
2315/*
2316 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002317 */
2318 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002319copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002320{
2321 qf_info_T *qi;
2322 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002323
Bram Moolenaar09037502018-09-25 22:08:14 +02002324 // When copying from a location list window, copy the referenced
2325 // location list. For other windows, copy the location list for
2326 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002327 if (IS_LL_WINDOW(from))
2328 qi = from->w_llist_ref;
2329 else
2330 qi = from->w_llist;
2331
Bram Moolenaar09037502018-09-25 22:08:14 +02002332 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002333 return;
2334
Bram Moolenaar09037502018-09-25 22:08:14 +02002335 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002336 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 return;
2338
2339 to->w_llist->qf_listcount = qi->qf_listcount;
2340
Bram Moolenaar09037502018-09-25 22:08:14 +02002341 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002342 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002343 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002344 to->w_llist->qf_curlist = idx;
2345
Bram Moolenaar0398e002019-03-21 21:12:49 +01002346 if (copy_loclist(qf_get_list(qi, idx),
2347 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002348 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002349 qf_free_all(to);
2350 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002351 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002352 }
2353
Bram Moolenaar09037502018-09-25 22:08:14 +02002354 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002355}
2356
2357/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002358 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002359 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
2361 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002362qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaar82404332016-07-10 17:00:38 +02002364 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002365 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002366 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002367
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002368 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
Bram Moolenaare60acc12011-05-10 16:41:25 +02002371#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002372 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002373#endif
2374#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002375 if (directory != NULL)
2376 slash_adjust(directory);
2377 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002378#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002379 if (directory != NULL && !vim_isAbsName(fname)
2380 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2381 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002382 // Here we check if the file really exists.
2383 // This should normally be true, but if make works without
2384 // "leaving directory"-messages we might have missed a
2385 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002386 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002389 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002390 if (directory)
2391 ptr = concat_fnames(directory, fname, TRUE);
2392 else
2393 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002395 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002396 bufname = ptr;
2397 }
2398 else
2399 bufname = fname;
2400
2401 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002402 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002403 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002404 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002405 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002407 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002408 {
2409 vim_free(qf_last_bufname);
2410 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2411 if (bufname == ptr)
2412 qf_last_bufname = bufname;
2413 else
2414 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002415 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002416 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002417 if (buf == NULL)
2418 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002419
Bram Moolenaarc1542742016-07-20 21:44:37 +02002420 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002421 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002422 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423}
2424
2425/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002426 * Push dirbuf onto the directory stack and return pointer to actual dir or
2427 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 */
2429 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002430qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432 struct dir_stack_T *ds_new;
2433 struct dir_stack_T *ds_ptr;
2434
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002435 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002436 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 if (ds_new == NULL)
2438 return NULL;
2439
2440 ds_new->next = *stackptr;
2441 *stackptr = ds_new;
2442
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002443 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (vim_isAbsName(dirbuf)
2445 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002446 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 (*stackptr)->dirname = vim_strsave(dirbuf);
2448 else
2449 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002450 // Okay we don't have an absolute path.
2451 // dirbuf must be a subdir of one of the directories on the stack.
2452 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 ds_new = (*stackptr)->next;
2454 (*stackptr)->dirname = NULL;
2455 while (ds_new)
2456 {
2457 vim_free((*stackptr)->dirname);
2458 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2459 TRUE);
2460 if (mch_isdir((*stackptr)->dirname) == TRUE)
2461 break;
2462
2463 ds_new = ds_new->next;
2464 }
2465
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002466 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 while ((*stackptr)->next != ds_new)
2468 {
2469 ds_ptr = (*stackptr)->next;
2470 (*stackptr)->next = (*stackptr)->next->next;
2471 vim_free(ds_ptr->dirname);
2472 vim_free(ds_ptr);
2473 }
2474
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002475 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 if (ds_new == NULL)
2477 {
2478 vim_free((*stackptr)->dirname);
2479 (*stackptr)->dirname = vim_strsave(dirbuf);
2480 }
2481 }
2482
2483 if ((*stackptr)->dirname != NULL)
2484 return (*stackptr)->dirname;
2485 else
2486 {
2487 ds_ptr = *stackptr;
2488 *stackptr = (*stackptr)->next;
2489 vim_free(ds_ptr);
2490 return NULL;
2491 }
2492}
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494/*
2495 * pop dirbuf from the directory stack and return previous directory or NULL if
2496 * stack is empty
2497 */
2498 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002499qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 struct dir_stack_T *ds_ptr;
2502
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002503 // TODO: Should we check if dirbuf is the directory on top of the stack?
2504 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002506 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (*stackptr != NULL)
2508 {
2509 ds_ptr = *stackptr;
2510 *stackptr = (*stackptr)->next;
2511 vim_free(ds_ptr->dirname);
2512 vim_free(ds_ptr);
2513 }
2514
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002515 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 return *stackptr ? (*stackptr)->dirname : NULL;
2517}
2518
2519/*
2520 * clean up directory stack
2521 */
2522 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002523qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 struct dir_stack_T *ds_ptr;
2526
2527 while ((ds_ptr = *stackptr) != NULL)
2528 {
2529 *stackptr = (*stackptr)->next;
2530 vim_free(ds_ptr->dirname);
2531 vim_free(ds_ptr);
2532 }
2533}
2534
2535/*
2536 * Check in which directory of the directory stack the given file can be
2537 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002538 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * Cleans up intermediate directory entries.
2540 *
2541 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002542 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 * ./
2544 * ./aa
2545 * ./aa/bb
2546 * ./bb
2547 * ./bb/x.c
2548 * and make says:
2549 * making all in aa
2550 * making all in bb
2551 * x.c:9: Error
2552 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2553 * qf_guess_filepath will return NULL.
2554 */
2555 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002556qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 struct dir_stack_T *ds_ptr;
2559 struct dir_stack_T *ds_tmp;
2560 char_u *fullname;
2561
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002562 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002563 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 return NULL;
2565
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002566 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 fullname = NULL;
2568 while (ds_ptr)
2569 {
2570 vim_free(fullname);
2571 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2572
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002573 // If concat_fnames failed, just go on. The worst thing that can happen
2574 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2576 break;
2577
2578 ds_ptr = ds_ptr->next;
2579 }
2580
2581 vim_free(fullname);
2582
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002583 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002584 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002586 ds_tmp = qfl->qf_dir_stack->next;
2587 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 vim_free(ds_tmp->dirname);
2589 vim_free(ds_tmp);
2590 }
2591
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002592 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593}
2594
2595/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002596 * Returns TRUE if a quickfix/location list with the given identifier exists.
2597 */
2598 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002599qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002600{
2601 qf_info_T *qi = &ql_info;
2602 int i;
2603
2604 if (wp != NULL)
2605 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002606 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002607 if (qi == NULL)
2608 return FALSE;
2609 }
2610
2611 for (i = 0; i < qi->qf_listcount; ++i)
2612 if (qi->qf_lists[i].qf_id == qf_id)
2613 return TRUE;
2614
2615 return FALSE;
2616}
2617
2618/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002619 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002620 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002621 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002622 * Similar to location list.
2623 */
2624 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002625is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002626{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002627 qfline_T *qfp;
2628 int i;
2629
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002630 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002631 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2632 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002633 break;
2634
Bram Moolenaar95946f12019-03-31 15:31:59 +02002635 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002636 return FALSE;
2637
2638 return TRUE;
2639}
2640
2641/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002642 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002643 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002644 */
2645 static qfline_T *
2646get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002647 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002648 qfline_T *qf_ptr,
2649 int *qf_index,
2650 int dir)
2651{
2652 int idx;
2653 int old_qf_fnum;
2654
2655 idx = *qf_index;
2656 old_qf_fnum = qf_ptr->qf_fnum;
2657
2658 do
2659 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002660 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002661 return NULL;
2662 ++idx;
2663 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002664 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002665 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2666
2667 *qf_index = idx;
2668 return qf_ptr;
2669}
2670
2671/*
2672 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002673 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002674 */
2675 static qfline_T *
2676get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002677 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002678 qfline_T *qf_ptr,
2679 int *qf_index,
2680 int dir)
2681{
2682 int idx;
2683 int old_qf_fnum;
2684
2685 idx = *qf_index;
2686 old_qf_fnum = qf_ptr->qf_fnum;
2687
2688 do
2689 {
2690 if (idx == 1 || qf_ptr->qf_prev == NULL)
2691 return NULL;
2692 --idx;
2693 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002694 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002695 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2696
2697 *qf_index = idx;
2698 return qf_ptr;
2699}
2700
2701/*
2702 * Get the n'th (errornr) previous/next valid entry from the current entry in
2703 * the quickfix list.
2704 * dir == FORWARD or FORWARD_FILE: next valid entry
2705 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2706 */
2707 static qfline_T *
2708get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002709 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002710 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002711 int dir,
2712 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002713{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002714 qfline_T *qf_ptr = qfl->qf_ptr;
2715 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002716 qfline_T *prev_qf_ptr;
2717 int prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002718 char_u *err = e_no_more_items;
2719
2720 while (errornr--)
2721 {
2722 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002723 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002724
2725 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002726 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002727 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002728 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002729 if (qf_ptr == NULL)
2730 {
2731 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002732 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002733 if (err != NULL)
2734 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002735 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002736 return NULL;
2737 }
2738 break;
2739 }
2740
2741 err = NULL;
2742 }
2743
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002744 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002745 return qf_ptr;
2746}
2747
2748/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002749 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2750 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002751 */
2752 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002753get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002754{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002755 qfline_T *qf_ptr = qfl->qf_ptr;
2756 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002757
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002758 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002759 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2760 {
2761 --qf_idx;
2762 qf_ptr = qf_ptr->qf_prev;
2763 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002764 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002765 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2766 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002767 {
2768 ++qf_idx;
2769 qf_ptr = qf_ptr->qf_next;
2770 }
2771
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002772 *new_qfidx = qf_idx;
2773 return qf_ptr;
2774}
2775
2776/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002777 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002778 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2779 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2780 * Returns a pointer to the entry and the index of the new entry is stored in
2781 * 'new_qfidx'.
2782 */
2783 static qfline_T *
2784qf_get_entry(
2785 qf_list_T *qfl,
2786 int errornr,
2787 int dir,
2788 int *new_qfidx)
2789{
2790 qfline_T *qf_ptr = qfl->qf_ptr;
2791 int qfidx = qfl->qf_index;
2792
2793 if (dir != 0) // next/prev valid entry
2794 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2795 else if (errornr != 0) // go to specified number
2796 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2797
2798 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002799 return qf_ptr;
2800}
2801
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002802/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002803 * Find a window displaying a Vim help file.
2804 */
2805 static win_T *
2806qf_find_help_win(void)
2807{
2808 win_T *wp;
2809
2810 FOR_ALL_WINDOWS(wp)
2811 if (bt_help(wp->w_buffer))
2812 return wp;
2813
2814 return NULL;
2815}
2816
2817/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002818 * Set the location list for the specified window to 'qi'.
2819 */
2820 static void
2821win_set_loclist(win_T *wp, qf_info_T *qi)
2822{
2823 wp->w_llist = qi;
2824 qi->qf_refcount++;
2825}
2826
2827/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002828 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2829 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002830 */
2831 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002832jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002833{
2834 win_T *wp;
2835 int flags;
2836
Bram Moolenaarb2443732018-11-11 22:50:27 +01002837 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002838 wp = NULL;
2839 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002840 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002841 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2842 win_enter(wp, TRUE);
2843 else
2844 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002845 // Split off help window; put it at far top if no position
2846 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002847 flags = WSP_HELP;
2848 if (cmdmod.split == 0 && curwin->w_width != Columns
2849 && curwin->w_width < 80)
2850 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002851 // If the user asks to open a new window, then copy the location list.
2852 // Otherwise, don't copy the location list.
2853 if (IS_LL_STACK(qi) && !newwin)
2854 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002855
2856 if (win_split(0, flags) == FAIL)
2857 return FAIL;
2858
2859 *opened_window = TRUE;
2860
2861 if (curwin->w_height < p_hh)
2862 win_setheight((int)p_hh);
2863
Bram Moolenaarb2443732018-11-11 22:50:27 +01002864 // When using location list, the new window should use the supplied
2865 // location list. If the user asks to open a new window, then the new
2866 // window will get a copy of the location list.
2867 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002868 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002869 }
2870
2871 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002872 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002873
2874 return OK;
2875}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002876
2877/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002878 * Find a non-quickfix window in the current tabpage using the given location
2879 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002880 * Returns NULL if a matching window is not found.
2881 */
2882 static win_T *
2883qf_find_win_with_loclist(qf_info_T *ll)
2884{
2885 win_T *wp;
2886
2887 FOR_ALL_WINDOWS(wp)
2888 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2889 return wp;
2890
2891 return NULL;
2892}
2893
2894/*
2895 * Find a window containing a normal buffer
2896 */
2897 static win_T *
2898qf_find_win_with_normal_buf(void)
2899{
2900 win_T *wp;
2901
2902 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002903 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002904 return wp;
2905
2906 return NULL;
2907}
2908
2909/*
2910 * Go to a window in any tabpage containing the specified file. Returns TRUE
2911 * if successfully jumped to the window. Otherwise returns FALSE.
2912 */
2913 static int
2914qf_goto_tabwin_with_file(int fnum)
2915{
2916 tabpage_T *tp;
2917 win_T *wp;
2918
2919 FOR_ALL_TAB_WINDOWS(tp, wp)
2920 if (wp->w_buffer->b_fnum == fnum)
2921 {
2922 goto_tabpage_win(tp, wp);
2923 return TRUE;
2924 }
2925
2926 return FALSE;
2927}
2928
2929/*
2930 * Create a new window to show a file above the quickfix window. Called when
2931 * only the quickfix window is present.
2932 */
2933 static int
2934qf_open_new_file_win(qf_info_T *ll_ref)
2935{
2936 int flags;
2937
2938 flags = WSP_ABOVE;
2939 if (ll_ref != NULL)
2940 flags |= WSP_NEWLOC;
2941 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002942 return FAIL; // not enough room for window
2943 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002944 swb_flags = 0;
2945 RESET_BINDING(curwin);
2946 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002947 // The new window should use the location list from the
2948 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002949 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002950 return OK;
2951}
2952
2953/*
2954 * Go to a window that shows the right buffer. If the window is not found, go
2955 * to the window just above the location list window. This is used for opening
2956 * a file from a location window and not from a quickfix window. If some usable
2957 * window is previously found, then it is supplied in 'use_win'.
2958 */
2959 static void
2960qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2961{
2962 win_T *win = use_win;
2963
2964 if (win == NULL)
2965 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002966 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002967 FOR_ALL_WINDOWS(win)
2968 if (win->w_buffer->b_fnum == qf_fnum)
2969 break;
2970 if (win == NULL)
2971 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002972 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 win = curwin;
2974 do
2975 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002976 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002977 break;
2978 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002979 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002980 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002981 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002982 } while (win != curwin);
2983 }
2984 }
2985 win_goto(win);
2986
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002987 // If the location list for the window is not set, then set it
2988 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002989 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002990 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002991}
2992
2993/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002994 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2995 * not found, then go to the window just above the quickfix window. This is
2996 * used for opening a file from a quickfix window and not from a location
2997 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002998 */
2999 static void
3000qf_goto_win_with_qfl_file(int qf_fnum)
3001{
3002 win_T *win;
3003 win_T *altwin;
3004
3005 win = curwin;
3006 altwin = NULL;
3007 for (;;)
3008 {
3009 if (win->w_buffer->b_fnum == qf_fnum)
3010 break;
3011 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003012 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003013 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003014 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003015
3016 if (IS_QF_WINDOW(win))
3017 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003018 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003019 // window, unless 'switchbuf' contains 'uselast': in this case we
3020 // try to jump to the previously used window first.
3021 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3022 win = prevwin;
3023 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003024 win = altwin;
3025 else if (curwin->w_prev != NULL)
3026 win = curwin->w_prev;
3027 else
3028 win = curwin->w_next;
3029 break;
3030 }
3031
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003032 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003033 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003034 altwin = win;
3035 }
3036
3037 win_goto(win);
3038}
3039
3040/*
3041 * Find a suitable window for opening a file (qf_fnum) from the
3042 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003043 * window, jump to it. Otherwise open a new window to display the file. If
3044 * 'newwin' is TRUE, then always open a new window. This is called from either
3045 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003046 */
3047 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003048qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003049{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003050 win_T *usable_wp = NULL;
3051 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003052 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003053
Bram Moolenaarb2443732018-11-11 22:50:27 +01003054 // If opening a new window, then don't use the location list referred by
3055 // the current window. Otherwise two windows will refer to the same
3056 // location list.
3057 if (!newwin)
3058 ll_ref = curwin->w_llist_ref;
3059
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003060 if (ll_ref != NULL)
3061 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003062 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003063 usable_wp = qf_find_win_with_loclist(ll_ref);
3064 if (usable_wp != NULL)
3065 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003066 }
3067
3068 if (!usable_win)
3069 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003070 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003071 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003072 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003073 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003074 }
3075
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003076 // If no usable window is found and 'switchbuf' contains "usetab"
3077 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003078 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003079 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003080
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003081 // If there is only one window and it is the quickfix window, create a
3082 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003083 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003084 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003085 if (qf_open_new_file_win(ll_ref) != OK)
3086 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003087 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003088 }
3089 else
3090 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003091 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003092 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003093 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003094 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003095 }
3096
3097 return OK;
3098}
3099
3100/*
3101 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003102 * Returns OK if successfully edited the file, FAIL on failing to open the
3103 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3104 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003105 */
3106 static int
3107qf_jump_edit_buffer(
3108 qf_info_T *qi,
3109 qfline_T *qf_ptr,
3110 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003111 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003112 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003113{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003114 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003115 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003116 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003117 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003118 int old_qf_curlist = qi->qf_curlist;
3119 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003120
3121 if (qf_ptr->qf_type == 1)
3122 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003123 // Open help file (do_ecmd() will set b_help flag, readfile() will
3124 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003125 if (!can_abandon(curbuf, forceit))
3126 {
3127 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003128 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003129 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003130
3131 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3132 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003133 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003134 }
3135 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003136 retval = buflist_getfile(qf_ptr->qf_fnum,
3137 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003138
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003139 // If a location list, check whether the associated window is still
3140 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003141 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003142 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003143 win_T *wp = win_id2wp(prev_winid);
3144 if (wp == NULL && curwin->w_llist != qi)
3145 {
3146 emsg(_("E924: Current window was closed"));
3147 *opened_window = FALSE;
3148 return NOTDONE;
3149 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003150 }
3151
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003152 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003153 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003154 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003155 return NOTDONE;
3156 }
3157
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003158 // Check if the list was changed. The pointers may happen to be identical,
3159 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003160 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003161 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003162 || !is_qf_entry_present(qfl, qf_ptr))
3163 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003164 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003165 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003166 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003167 emsg(_(e_current_location_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003168 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 }
3170
3171 return retval;
3172}
3173
3174/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003175 * Go to the error line in the current file using either line/column number or
3176 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003177 */
3178 static void
3179qf_jump_goto_line(
3180 linenr_T qf_lnum,
3181 int qf_col,
3182 char_u qf_viscol,
3183 char_u *qf_pattern)
3184{
3185 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003186
3187 if (qf_pattern == NULL)
3188 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003189 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003190 i = qf_lnum;
3191 if (i > 0)
3192 {
3193 if (i > curbuf->b_ml.ml_line_count)
3194 i = curbuf->b_ml.ml_line_count;
3195 curwin->w_cursor.lnum = i;
3196 }
3197 if (qf_col > 0)
3198 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003199 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003200 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003201 coladvance(qf_col - 1);
3202 else
3203 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003204 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003205 check_cursor();
3206 }
3207 else
3208 beginline(BL_WHITE | BL_FIX);
3209 }
3210 else
3211 {
3212 pos_T save_cursor;
3213
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003214 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003215 save_cursor = curwin->w_cursor;
3216 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003217 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003218 curwin->w_cursor = save_cursor;
3219 }
3220}
3221
3222/*
3223 * Display quickfix list index and size message
3224 */
3225 static void
3226qf_jump_print_msg(
3227 qf_info_T *qi,
3228 int qf_index,
3229 qfline_T *qf_ptr,
3230 buf_T *old_curbuf,
3231 linenr_T old_lnum)
3232{
3233 linenr_T i;
3234 int len;
3235
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003236 // Update the screen before showing the message, unless the screen
3237 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003238 if (!msg_scrolled)
3239 update_topline_redraw();
3240 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003241 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003242 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3243 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003244 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003245 len = (int)STRLEN(IObuff);
3246 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3247
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003248 // Output the message. Overwrite to avoid scrolling when the 'O'
3249 // flag is present in 'shortmess'; But when not jumping, print the
3250 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003251 i = msg_scroll;
3252 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3253 msg_scroll = TRUE;
3254 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3255 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003256 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003257 msg_scroll = i;
3258}
3259
3260/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003261 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003262 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3263 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003264 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3265 * able to jump/open a window. Returns NOTDONE if a file is not associated
3266 * with the entry.
3267 */
3268 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003269qf_jump_open_window(
3270 qf_info_T *qi,
3271 qfline_T *qf_ptr,
3272 int newwin,
3273 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003274{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003275 qf_list_T *qfl = qf_get_curlist(qi);
3276 int old_changedtick = qfl->qf_changedtick;
3277 int old_qf_curlist = qi->qf_curlist;
3278 qfltype_T qfl_type = qfl->qfl_type;
3279
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003280 // For ":helpgrep" find a help window or open one.
3281 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003282 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003283 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003284 if (old_qf_curlist != qi->qf_curlist
3285 || old_changedtick != qfl->qf_changedtick
3286 || !is_qf_entry_present(qfl, qf_ptr))
3287 {
3288 if (qfl_type == QFLT_QUICKFIX)
3289 emsg(_(e_current_quickfix_list_was_changed));
3290 else
3291 emsg(_(e_current_location_list_was_changed));
3292 return FAIL;
3293 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003294
3295 // If currently in the quickfix window, find another window to show the
3296 // file in.
3297 if (bt_quickfix(curbuf) && !*opened_window)
3298 {
3299 // If there is no file specified, we don't know where to go.
3300 // But do advance, otherwise ":cn" gets stuck.
3301 if (qf_ptr->qf_fnum == 0)
3302 return NOTDONE;
3303
Bram Moolenaarb2443732018-11-11 22:50:27 +01003304 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3305 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003306 return FAIL;
3307 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003308 if (old_qf_curlist != qi->qf_curlist
3309 || old_changedtick != qfl->qf_changedtick
3310 || !is_qf_entry_present(qfl, qf_ptr))
3311 {
3312 if (qfl_type == QFLT_QUICKFIX)
3313 emsg(_(e_current_quickfix_list_was_changed));
3314 else
3315 emsg(_(e_current_location_list_was_changed));
3316 return FAIL;
3317 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003318
3319 return OK;
3320}
3321
3322/*
3323 * Edit a selected file from the quickfix/location list and jump to a
3324 * particular line/column, adjust the folds and display a message about the
3325 * jump.
3326 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3327 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3328 * the file.
3329 */
3330 static int
3331qf_jump_to_buffer(
3332 qf_info_T *qi,
3333 int qf_index,
3334 qfline_T *qf_ptr,
3335 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003336 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003337 int *opened_window,
3338 int openfold,
3339 int print_message)
3340{
3341 buf_T *old_curbuf;
3342 linenr_T old_lnum;
3343 int retval = OK;
3344
3345 // If there is a file name, read the wanted file if needed, and check
3346 // autowrite etc.
3347 old_curbuf = curbuf;
3348 old_lnum = curwin->w_cursor.lnum;
3349
3350 if (qf_ptr->qf_fnum != 0)
3351 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003352 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003353 opened_window);
3354 if (retval != OK)
3355 return retval;
3356 }
3357
3358 // When not switched to another buffer, still need to set pc mark
3359 if (curbuf == old_curbuf)
3360 setpcmark();
3361
3362 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3363 qf_ptr->qf_pattern);
3364
3365#ifdef FEAT_FOLDING
3366 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3367 foldOpenCursor();
3368#endif
3369 if (print_message)
3370 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3371
3372 return retval;
3373}
3374
3375/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003376 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 */
3378 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003379qf_jump(qf_info_T *qi,
3380 int dir,
3381 int errornr,
3382 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003384 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3385}
3386
3387/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003388 * Jump to a quickfix line.
3389 * If dir == 0 go to entry "errornr".
3390 * If dir == FORWARD go "errornr" valid entries forward.
3391 * If dir == BACKWARD go "errornr" valid entries backward.
3392 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3393 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3394 * else if "errornr" is zero, redisplay the same line
3395 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003396 * If 'newwin' is TRUE, then open the file in a new window.
3397 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003398 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003399qf_jump_newwin(qf_info_T *qi,
3400 int dir,
3401 int errornr,
3402 int forceit,
3403 int newwin)
3404{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003405 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003406 qfline_T *qf_ptr;
3407 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003410 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003411 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003412 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003415 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003416 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003418 if (qi == NULL)
3419 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003420
Bram Moolenaar0398e002019-03-21 21:12:49 +01003421 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003423 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 return;
3425 }
3426
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003427 incr_quickfix_busy();
3428
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003429 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003430
3431 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003433 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003435
3436 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3437 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003439 qf_ptr = old_qf_ptr;
3440 qf_index = old_qf_index;
3441 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003444 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003446 // No need to print the error message if it's visible in the error
3447 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 print_message = FALSE;
3449
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003450 prev_winid = curwin->w_id;
3451
Bram Moolenaarb2443732018-11-11 22:50:27 +01003452 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003453 if (retval == FAIL)
3454 goto failed;
3455 if (retval == NOTDONE)
3456 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003457
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003458 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003459 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003460 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003462 // Quickfix/location list is freed by an autocmd
3463 qi = NULL;
3464 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003467 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003470 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003471 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003473 // Couldn't open file, so put index back where it was. This could
3474 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 qf_ptr = old_qf_ptr;
3477 qf_index = old_qf_index;
3478 }
3479 }
3480theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003481 if (qi != NULL)
3482 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003483 qfl->qf_ptr = qf_ptr;
3484 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003485 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003486 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003488 // Restore old 'switchbuf' value, but not when an autocommand or
3489 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003490 p_swb = old_swb;
3491 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003493 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494}
3495
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003496// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003497static int qfFileAttr;
3498static int qfSepAttr;
3499static int qfLineAttr;
3500
3501/*
3502 * Display information about a single entry from the quickfix/location list.
3503 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003504 * 'cursel' will be set to TRUE for the currently selected entry in the
3505 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003506 */
3507 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003508qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003509{
3510 char_u *fname;
3511 buf_T *buf;
3512 int filter_entry;
3513
3514 fname = NULL;
3515 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3516 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3517 (char *)qfp->qf_module);
3518 else {
3519 if (qfp->qf_fnum != 0
3520 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3521 {
3522 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003523 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003524 fname = gettail(fname);
3525 }
3526 if (fname == NULL)
3527 sprintf((char *)IObuff, "%2d", qf_idx);
3528 else
3529 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3530 qf_idx, (char *)fname);
3531 }
3532
3533 // Support for filtering entries using :filter /pat/ clist
3534 // Match against the module name, file name, search pattern and
3535 // text of the entry.
3536 filter_entry = TRUE;
3537 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3538 filter_entry &= message_filtered(qfp->qf_module);
3539 if (filter_entry && fname != NULL)
3540 filter_entry &= message_filtered(fname);
3541 if (filter_entry && qfp->qf_pattern != NULL)
3542 filter_entry &= message_filtered(qfp->qf_pattern);
3543 if (filter_entry)
3544 filter_entry &= message_filtered(qfp->qf_text);
3545 if (filter_entry)
3546 return;
3547
3548 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003549 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003550
3551 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003552 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003553 if (qfp->qf_lnum == 0)
3554 IObuff[0] = NUL;
3555 else if (qfp->qf_col == 0)
3556 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3557 else
3558 sprintf((char *)IObuff, "%ld col %d",
3559 qfp->qf_lnum, qfp->qf_col);
3560 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3561 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003562 msg_puts_attr((char *)IObuff, qfLineAttr);
3563 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003564 if (qfp->qf_pattern != NULL)
3565 {
3566 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003567 msg_puts((char *)IObuff);
3568 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003569 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003570 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003571
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003572 // Remove newlines and leading whitespace from the text. For an
3573 // unrecognized line keep the indent, the compiler may mark a word
3574 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003575 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3576 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3577 IObuff, IOSIZE);
3578 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003579 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003580}
3581
3582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003584 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 */
3586 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003587qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003589 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003590 qfline_T *qfp;
3591 int i;
3592 int idx1 = 1;
3593 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003594 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003595 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003596 int all = eap->forceit; // if not :cl!, only show
3597 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003598 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003600 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3601 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003602
Bram Moolenaar0398e002019-03-21 21:12:49 +01003603 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003605 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 return;
3607 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003608 if (*arg == '+')
3609 {
3610 ++arg;
3611 plus = TRUE;
3612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3614 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003615 semsg(_(e_trailing_arg), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return;
3617 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003618 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003619 if (plus)
3620 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003621 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003622 idx2 = i + idx1;
3623 idx1 = i;
3624 }
3625 else
3626 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003627 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003628 if (idx1 < 0)
3629 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3630 if (idx2 < 0)
3631 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003634 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003635 shorten_fnames(FALSE);
3636
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003637 // Get the attributes for the different quickfix highlight items. Note
3638 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003639 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3640 if (qfFileAttr == 0)
3641 qfFileAttr = HL_ATTR(HLF_D);
3642 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3643 if (qfSepAttr == 0)
3644 qfSepAttr = HL_ATTR(HLF_D);
3645 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3646 if (qfLineAttr == 0)
3647 qfLineAttr = HL_ATTR(HLF_N);
3648
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003649 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003651 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
3653 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003654 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 ui_breakcheck();
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658}
3659
3660/*
3661 * Remove newlines and leading whitespace from an error message.
3662 * Put the result in "buf[bufsize]".
3663 */
3664 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003665qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666{
3667 int i;
3668 char_u *p = text;
3669
3670 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3671 {
3672 if (*p == '\n')
3673 {
3674 buf[i] = ' ';
3675 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003676 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 break;
3678 }
3679 else
3680 buf[i] = *p++;
3681 }
3682 buf[i] = NUL;
3683}
3684
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003685/*
3686 * Display information (list number, list size and the title) about a
3687 * quickfix/location list.
3688 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003689 static void
3690qf_msg(qf_info_T *qi, int which, char *lead)
3691{
3692 char *title = (char *)qi->qf_lists[which].qf_title;
3693 int count = qi->qf_lists[which].qf_count;
3694 char_u buf[IOSIZE];
3695
3696 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3697 lead,
3698 which + 1,
3699 qi->qf_listcount,
3700 count);
3701
3702 if (title != NULL)
3703 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003704 size_t len = STRLEN(buf);
3705
3706 if (len < 34)
3707 {
3708 vim_memset(buf + len, ' ', 34 - len);
3709 buf[34] = NUL;
3710 }
3711 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003712 }
3713 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003714 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003715}
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717/*
3718 * ":colder [count]": Up in the quickfix stack.
3719 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003720 * ":lolder [count]": Up in the location list stack.
3721 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 */
3723 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003724qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003726 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 int count;
3728
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003729 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3730 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (eap->addr_count != 0)
3733 count = eap->line2;
3734 else
3735 count = 1;
3736 while (count--)
3737 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003738 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003740 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003742 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003745 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747 else
3748 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003751 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003752 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003754 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003757 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003758 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759}
3760
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003761/*
3762 * Display the information about all the quickfix/location lists in the stack
3763 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003764 void
3765qf_history(exarg_T *eap)
3766{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003767 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003768 int i;
3769
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003770 if (eap->addr_count > 0)
3771 {
3772 if (qi == NULL)
3773 {
3774 emsg(_(e_loclist));
3775 return;
3776 }
3777
3778 // Jump to the specified quickfix list
3779 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3780 {
3781 qi->qf_curlist = eap->line2 - 1;
3782 qf_msg(qi, qi->qf_curlist, "");
3783 qf_update_buffer(qi, NULL);
3784 }
3785 else
3786 emsg(_(e_invrange));
3787
3788 return;
3789 }
3790
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003791 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003792 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003793 else
3794 for (i = 0; i < qi->qf_listcount; ++i)
3795 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3796}
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003799 * Free all the entries in the error list "idx". Note that other information
3800 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 */
3802 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003803qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003805 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003806 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003807 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003809 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003811 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003812 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003813 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003814 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003815 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003816 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003817 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003818 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003819 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003820 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003821 // Somehow qf_count may have an incorrect value, set it to 1
3822 // to avoid crashing when it's wrong.
3823 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003824 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003825 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003826 qfl->qf_start = qfpnext;
3827 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003829
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003830 qfl->qf_index = 0;
3831 qfl->qf_start = NULL;
3832 qfl->qf_last = NULL;
3833 qfl->qf_ptr = NULL;
3834 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003835
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003836 qf_clean_dir_stack(&qfl->qf_dir_stack);
3837 qfl->qf_directory = NULL;
3838 qf_clean_dir_stack(&qfl->qf_file_stack);
3839 qfl->qf_currfile = NULL;
3840 qfl->qf_multiline = FALSE;
3841 qfl->qf_multiignore = FALSE;
3842 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843}
3844
3845/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003846 * Free error list "idx". Frees all the entries in the quickfix list,
3847 * associated context information and the title.
3848 */
3849 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003850qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003851{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003852 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003853
Bram Moolenaard23a8232018-02-10 18:45:26 +01003854 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003855 free_tv(qfl->qf_ctx);
3856 qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02003857 free_callback(&qfl->qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003858 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003859 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003860}
3861
3862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 * qf_mark_adjust: adjust marks
3864 */
3865 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003866qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003867 win_T *wp,
3868 linenr_T line1,
3869 linenr_T line2,
3870 long amount,
3871 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003873 int i;
3874 qfline_T *qfp;
3875 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003876 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003877 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003878 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
Bram Moolenaarc1542742016-07-20 21:44:37 +02003880 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003881 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003882 if (wp != NULL)
3883 {
3884 if (wp->w_llist == NULL)
3885 return;
3886 qi = wp->w_llist;
3887 }
3888
3889 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003890 {
3891 qf_list_T *qfl = qf_get_list(qi, idx);
3892
3893 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003894 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (qfp->qf_fnum == curbuf->b_fnum)
3896 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003897 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3899 {
3900 if (amount == MAXLNUM)
3901 qfp->qf_cleared = TRUE;
3902 else
3903 qfp->qf_lnum += amount;
3904 }
3905 else if (amount_after && qfp->qf_lnum > line2)
3906 qfp->qf_lnum += amount_after;
3907 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003908 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003909
3910 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003911 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912}
3913
3914/*
3915 * Make a nice message out of the error character and the error number:
3916 * char number message
3917 * e or E 0 " error"
3918 * w or W 0 " warning"
3919 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02003920 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 * 0 0 ""
3922 * other 0 " c"
3923 * e or E n " error n"
3924 * w or W n " warning n"
3925 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02003926 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 * 0 n " error n"
3928 * other n " c n"
3929 * 1 x "" :helpgrep
3930 */
3931 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003932qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934 static char_u buf[20];
3935 static char_u cc[3];
3936 char_u *p;
3937
3938 if (c == 'W' || c == 'w')
3939 p = (char_u *)" warning";
3940 else if (c == 'I' || c == 'i')
3941 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02003942 else if (c == 'N' || c == 'n')
3943 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3945 p = (char_u *)" error";
3946 else if (c == 0 || c == 1)
3947 p = (char_u *)"";
3948 else
3949 {
3950 cc[0] = ' ';
3951 cc[1] = c;
3952 cc[2] = NUL;
3953 p = cc;
3954 }
3955
3956 if (nr <= 0)
3957 return p;
3958
3959 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3960 return buf;
3961}
3962
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003964 * When "split" is FALSE: Open the entry/result under the cursor.
3965 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3966 */
3967 void
3968qf_view_result(int split)
3969{
3970 qf_info_T *qi = &ql_info;
3971
3972 if (!bt_quickfix(curbuf))
3973 return;
3974
3975 if (IS_LL_WINDOW(curwin))
3976 qi = GET_LOC_LIST(curwin);
3977
Bram Moolenaar0398e002019-03-21 21:12:49 +01003978 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003979 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003980 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003981 return;
3982 }
3983
3984 if (split)
3985 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003986 // Open the selected entry in a new window
3987 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3988 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003989 return;
3990 }
3991
3992 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3993}
3994
3995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 * ":cwindow": open the quickfix window if we have errors to display,
3997 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003998 * ":lwindow": open the location list window if we have locations to display,
3999 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 */
4001 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004002ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004004 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004005 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 win_T *win;
4007
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004008 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4009 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004010
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004011 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004012
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004013 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004014 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004016 // If a quickfix window is open but we have no errors to display,
4017 // close the window. If a quickfix window is not open, then open
4018 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004019 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004020 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004021 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
4023 if (win != NULL)
4024 ex_cclose(eap);
4025 }
4026 else if (win == NULL)
4027 ex_copen(eap);
4028}
4029
4030/*
4031 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004032 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004035ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004037 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004038 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004040 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4041 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004043 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004044 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (win != NULL)
4046 win_close(win, FALSE);
4047}
4048
4049/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004050 * Set "w:quickfix_title" if "qi" has a title.
4051 */
4052 static void
4053qf_set_title_var(qf_list_T *qfl)
4054{
4055 if (qfl->qf_title != NULL)
4056 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4057}
4058
4059/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004060 * Goto a quickfix or location list window (if present).
4061 * Returns OK if the window is found, FAIL otherwise.
4062 */
4063 static int
4064qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4065{
4066 win_T *win;
4067
4068 win = qf_find_win(qi);
4069 if (win == NULL)
4070 return FAIL;
4071
4072 win_goto(win);
4073 if (resize)
4074 {
4075 if (vertsplit)
4076 {
4077 if (sz != win->w_width)
4078 win_setwidth(sz);
4079 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004080 else if (sz != win->w_height && win->w_height
4081 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004082 win_setheight(sz);
4083 }
4084
4085 return OK;
4086}
4087
4088/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004089 * Set options for the buffer in the quickfix or location list window.
4090 */
4091 static void
4092qf_set_cwindow_options(void)
4093{
4094 // switch off 'swapfile'
4095 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4096 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4097 OPT_LOCAL);
4098 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4099 RESET_BINDING(curwin);
4100#ifdef FEAT_DIFF
4101 curwin->w_p_diff = FALSE;
4102#endif
4103#ifdef FEAT_FOLDING
4104 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4105 OPT_LOCAL);
4106#endif
4107}
4108
4109/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004110 * Open a new quickfix or location list window, load the quickfix buffer and
4111 * set the appropriate options for the window.
4112 * Returns FAIL if the window could not be opened.
4113 */
4114 static int
4115qf_open_new_cwindow(qf_info_T *qi, int height)
4116{
4117 buf_T *qf_buf;
4118 win_T *oldwin = curwin;
4119 tabpage_T *prevtab = curtab;
4120 int flags = 0;
4121 win_T *win;
4122
4123 qf_buf = qf_find_buf(qi);
4124
4125 // The current window becomes the previous window afterwards.
4126 win = curwin;
4127
4128 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4129 // Create the new quickfix window at the very bottom, except when
4130 // :belowright or :aboveleft is used.
4131 win_goto(lastwin);
4132 // Default is to open the window below the current window
4133 if (cmdmod.split == 0)
4134 flags = WSP_BELOW;
4135 flags |= WSP_NEWLOC;
4136 if (win_split(height, flags) == FAIL)
4137 return FAIL; // not enough room for window
4138 RESET_BINDING(curwin);
4139
4140 if (IS_LL_STACK(qi))
4141 {
4142 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004143 // location list stack from the window 'win'.
4144 curwin->w_llist_ref = qi;
4145 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004146 }
4147
4148 if (oldwin != curwin)
4149 oldwin = NULL; // don't store info when in another window
4150 if (qf_buf != NULL)
4151 {
4152 // Use the existing quickfix buffer
4153 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4154 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4155 }
4156 else
4157 {
4158 // Create a new quickfix buffer
4159 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4160
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004161 // save the number of the new buffer
4162 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004163 }
4164
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004165 // Set the options for the quickfix buffer/window (if not already done)
4166 // Do this even if the quickfix buffer was already present, as an autocmd
4167 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004168 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004169 qf_set_cwindow_options();
4170
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004171 // Only set the height when still in the same tab page and there is no
4172 // window to the side.
4173 if (curtab == prevtab && curwin->w_width == Columns)
4174 win_setheight(height);
4175 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4176 if (win_valid(win))
4177 prevwin = win;
4178
4179 return OK;
4180}
4181
4182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004184 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 */
4186 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004187ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004189 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004190 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004192 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004193 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004195 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4196 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004197
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004198 incr_quickfix_busy();
4199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (eap->addr_count != 0)
4201 height = eap->line2;
4202 else
4203 height = QF_WINHEIGHT;
4204
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004205 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#ifdef FEAT_GUI
4207 need_mouse_correct = TRUE;
4208#endif
4209
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004210 // Find an existing quickfix window, or open a new one.
4211 if (cmdmod.tab == 0)
4212 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4213 cmdmod.split & WSP_VERT);
4214 if (status == FAIL)
4215 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004216 {
4217 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004218 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004221 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004222 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004223 // Save the current index here, as updating the quickfix buffer may free
4224 // the quickfix list
4225 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004226
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004227 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004228 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004230 decr_quickfix_busy();
4231
4232 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 curwin->w_cursor.col = 0;
4234 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004235 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236}
4237
4238/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004239 * Move the cursor in the quickfix window to "lnum".
4240 */
4241 static void
4242qf_win_goto(win_T *win, linenr_T lnum)
4243{
4244 win_T *old_curwin = curwin;
4245
4246 curwin = win;
4247 curbuf = win->w_buffer;
4248 curwin->w_cursor.lnum = lnum;
4249 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004250 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004251 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004252 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004253 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004254 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004255 curwin = old_curwin;
4256 curbuf = curwin->w_buffer;
4257}
4258
4259/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004260 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004261 */
4262 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004263ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004264{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004265 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004266 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004267
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004268 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4269 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004270
4271 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004272 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4273 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4274}
4275
4276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 * Return the number of the current entry (line number in the quickfix
4278 * window).
4279 */
4280 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004281qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004283 qf_info_T *qi = &ql_info;
4284
4285 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004286 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004287 qi = wp->w_llist_ref;
4288
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004289 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290}
4291
4292/*
4293 * Update the cursor position in the quickfix window to the current error.
4294 * Return TRUE if there is a quickfix window.
4295 */
4296 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004297qf_win_pos_update(
4298 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004299 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
4301 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004302 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004304 // Put the cursor on the current error in the quickfix window, so that
4305 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004306 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 if (win != NULL
4308 && qf_index <= win->w_buffer->b_ml.ml_line_count
4309 && old_qf_index != qf_index)
4310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (qf_index > old_qf_index)
4312 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004313 win->w_redraw_top = old_qf_index;
4314 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 else
4317 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004318 win->w_redraw_top = qf_index;
4319 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004321 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323 return win != NULL;
4324}
4325
4326/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004327 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004328 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004329 */
4330 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004331is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004332{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004333 // A window displaying the quickfix buffer will have the w_llist_ref field
4334 // set to NULL.
4335 // A window displaying a location list buffer will have the w_llist_ref
4336 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004337 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004338 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4339 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004340 return TRUE;
4341
4342 return FALSE;
4343}
4344
4345/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004346 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004347 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004348 */
4349 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004350qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004351{
4352 win_T *win;
4353
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004354 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004355 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004356 return win;
4357 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004358}
4359
4360/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004361 * Find a quickfix buffer.
4362 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 */
4364 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004365qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004367 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004368 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004370 if (qi->qf_bufnr != INVALID_QFBUFNR)
4371 {
4372 buf_T *qfbuf;
4373 qfbuf = buflist_findnr(qi->qf_bufnr);
4374 if (qfbuf != NULL)
4375 return qfbuf;
4376 // buffer is no longer present
4377 qi->qf_bufnr = INVALID_QFBUFNR;
4378 }
4379
Bram Moolenaar9c102382006-05-03 21:26:49 +00004380 FOR_ALL_TAB_WINDOWS(tp, win)
4381 if (is_qf_win(win, qi))
4382 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004383
Bram Moolenaar9c102382006-05-03 21:26:49 +00004384 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385}
4386
4387/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004388 * Process the 'quickfixtextfunc' option value.
4389 */
4390 int
4391qf_process_qftf_option(void)
4392{
4393 typval_T *tv;
4394 callback_T cb;
4395
4396 if (p_qftf == NULL || *p_qftf == NUL)
4397 {
4398 free_callback(&qftf_cb);
4399 return TRUE;
4400 }
4401
4402 if (*p_qftf == '{')
4403 {
4404 // Lambda expression
4405 tv = eval_expr(p_qftf, NULL);
4406 if (tv == NULL)
4407 return FALSE;
4408 }
4409 else
4410 {
4411 // treat everything else as a function name string
4412 tv = alloc_string_tv(vim_strsave(p_qftf));
4413 if (tv == NULL)
4414 return FALSE;
4415 }
4416
4417 cb = get_callback(tv);
4418 if (cb.cb_name == NULL)
4419 {
4420 free_tv(tv);
4421 return FALSE;
4422 }
4423
4424 free_callback(&qftf_cb);
4425 set_callback(&qftf_cb, &cb);
4426 free_tv(tv);
4427 return TRUE;
4428}
4429
4430/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004431 * Update the w:quickfix_title variable in the quickfix/location list window
4432 */
4433 static void
4434qf_update_win_titlevar(qf_info_T *qi)
4435{
4436 win_T *win;
4437 win_T *curwin_save;
4438
4439 if ((win = qf_find_win(qi)) != NULL)
4440 {
4441 curwin_save = curwin;
4442 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004443 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004444 curwin = curwin_save;
4445 }
4446}
4447
4448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 * Find the quickfix buffer. If it exists, update the contents.
4450 */
4451 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004452qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004455 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004458 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004459 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (buf != NULL)
4461 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004462 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004463 int qf_winid = 0;
4464
4465 if (IS_LL_STACK(qi))
4466 qf_winid = curwin->w_id;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004467
4468 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004469 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004470 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
Bram Moolenaard823fa92016-08-12 16:29:27 +02004472 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004473
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004474 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004475 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004476
Bram Moolenaar864293a2016-06-02 13:40:04 +02004477 if (old_last == NULL)
4478 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004479 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004481 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004482 aucmd_restbuf(&aco);
4483 }
4484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004485 // Only redraw when added lines are visible. This avoids flickering
4486 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004487 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4488 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490}
4491
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004492/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004493 * Add an error line to the quickfix buffer.
4494 */
4495 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004496qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004497 buf_T *buf, // quickfix window buffer
4498 linenr_T lnum,
4499 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004500 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004501 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004502 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004503{
4504 int len;
4505 buf_T *errbuf;
4506
Bram Moolenaard43906d2020-07-20 21:31:32 +02004507 // If the 'quickfixtextfunc' function returned an non-empty custom string
4508 // for this entry, then use it.
4509 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004510 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004511 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004512 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004513 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004514 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004515 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4516 len = (int)STRLEN(IObuff);
4517 }
4518 else if (qfp->qf_fnum != 0
4519 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4520 && errbuf->b_fname != NULL)
4521 {
4522 if (qfp->qf_type == 1) // :helpgrep
4523 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4524 else
4525 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004526 // Shorten the file name if not done already.
4527 // For optimization, do this only for the first entry in a
4528 // buffer.
4529 if (first_bufline && (errbuf->b_sfname == NULL
4530 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004531 {
4532 if (*dirname == NUL)
4533 mch_dirname(dirname, MAXPATHL);
4534 shorten_buf_fname(errbuf, dirname, FALSE);
4535 }
4536 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4537 }
4538 len = (int)STRLEN(IObuff);
4539 }
4540 else
4541 len = 0;
4542
4543 if (len < IOSIZE - 1)
4544 IObuff[len++] = '|';
4545
4546 if (qfp->qf_lnum > 0)
4547 {
4548 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%ld",
4549 qfp->qf_lnum);
4550 len += (int)STRLEN(IObuff + len);
4551
4552 if (qfp->qf_col > 0)
4553 {
4554 vim_snprintf((char *)IObuff + len, IOSIZE - len,
4555 " col %d", qfp->qf_col);
4556 len += (int)STRLEN(IObuff + len);
4557 }
4558
4559 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4560 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004561 len += (int)STRLEN(IObuff + len);
4562 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004563 else if (qfp->qf_pattern != NULL)
4564 {
4565 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4566 len += (int)STRLEN(IObuff + len);
4567 }
4568 if (len < IOSIZE - 2)
4569 {
4570 IObuff[len++] = '|';
4571 IObuff[len++] = ' ';
4572 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004573
Bram Moolenaar858ba062020-05-31 23:11:59 +02004574 // Remove newlines and leading whitespace from the text.
4575 // For an unrecognized line keep the indent, the compiler may
4576 // mark a word with ^^^^.
4577 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4578 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004579 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004580
4581 if (ml_append_buf(buf, lnum, IObuff,
4582 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4583 return FAIL;
4584
4585 return OK;
4586}
4587
Bram Moolenaard43906d2020-07-20 21:31:32 +02004588/*
4589 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4590 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4591 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004592 static list_T *
4593call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4594{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004595 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004596 list_T *qftf_list = NULL;
4597
4598 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4599 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4600 // set.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004601 if (qfl->qftf_cb.cb_name != NULL)
4602 cb = &qfl->qftf_cb;
4603 if (cb != NULL && cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004604 {
4605 typval_T args[1];
4606 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004607 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004608
4609 // create the dict argument
4610 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4611 return NULL;
4612 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4613 dict_add_number(d, "winid", (long)qf_winid);
4614 dict_add_number(d, "id", (long)qfl->qf_id);
4615 dict_add_number(d, "start_idx", start_idx);
4616 dict_add_number(d, "end_idx", end_idx);
4617 ++d->dv_refcount;
4618 args[0].v_type = VAR_DICT;
4619 args[0].vval.v_dict = d;
4620
Bram Moolenaard43906d2020-07-20 21:31:32 +02004621 qftf_list = NULL;
4622 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4623 {
4624 if (rettv.v_type == VAR_LIST)
4625 {
4626 qftf_list = rettv.vval.v_list;
4627 qftf_list->lv_refcount++;
4628 }
4629 clear_tv(&rettv);
4630 }
4631 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004632 }
4633
4634 return qftf_list;
4635}
4636
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 * Fill current buffer with quickfix errors, replacing any previous contents.
4639 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004640 * If "old_last" is not NULL append the items after this one.
4641 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4642 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 */
4644 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004645qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004647 linenr_T lnum;
4648 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004649 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004650 list_T *qftf_list = NULL;
4651 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
Bram Moolenaar864293a2016-06-02 13:40:04 +02004653 if (old_last == NULL)
4654 {
4655 if (buf != curbuf)
4656 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004657 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004658 return;
4659 }
4660
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004661 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004662 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004663 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004666 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004667 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004669 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004670 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004671 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004672
4673 *dirname = NUL;
4674
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004675 // Add one line for each error
Bram Moolenaar99234f22020-02-10 22:56:54 +01004676 if (old_last == NULL || old_last->qf_next == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004677 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004678 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004679 lnum = 0;
4680 }
4681 else
4682 {
4683 qfp = old_last->qf_next;
4684 lnum = buf->b_ml.ml_line_count;
4685 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004686
4687 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4688 (long)qfl->qf_count);
4689 if (qftf_list != NULL)
4690 qftf_li = qftf_list->lv_first;
4691
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004692 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004694 char_u *qftf_str = NULL;
4695
Bram Moolenaard43906d2020-07-20 21:31:32 +02004696 // Use the text supplied by the user defined function (if any).
4697 // If the returned value is not string, then ignore the rest
4698 // of the returned values and use the default.
4699 if (qftf_li != NULL && !invalid_val)
4700 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004701 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004702 if (qftf_str == NULL)
4703 invalid_val = TRUE;
4704 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004705
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004706 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4707 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004709
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004710 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004711 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004713 if (qfp == NULL)
4714 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004715
4716 if (qftf_li != NULL)
4717 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004719
4720 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004721 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004722 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
4724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004725 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 check_lnums(TRUE);
4727
Bram Moolenaar864293a2016-06-02 13:40:04 +02004728 if (old_last == NULL)
4729 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004730 // Set the 'filetype' to "qf" each time after filling the buffer.
4731 // This resembles reading a file into a buffer, it's more logical when
4732 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004733 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004734 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4735 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004737 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004738 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004740 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004742 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004743 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004744
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004745 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004746 redraw_curbuf_later(NOT_VALID);
4747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004749 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 KeyTyped = old_KeyTyped;
4751}
4752
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004753/*
4754 * For every change made to the quickfix list, update the changed tick.
4755 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004756 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004757qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004758{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004759 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004760}
4761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004763 * Return the quickfix/location list number with the given identifier.
4764 * Returns -1 if list is not found.
4765 */
4766 static int
4767qf_id2nr(qf_info_T *qi, int_u qfid)
4768{
4769 int qf_idx;
4770
4771 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4772 if (qi->qf_lists[qf_idx].qf_id == qfid)
4773 return qf_idx;
4774 return INVALID_QFIDX;
4775}
4776
4777/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004778 * If the current list is not "save_qfid" and we can find the list with that ID
4779 * then make it the current list.
4780 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004781 * Returns OK if successfully restored the list. Returns FAIL if the list with
4782 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004783 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004784 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004785qf_restore_list(qf_info_T *qi, int_u save_qfid)
4786{
4787 int curlist;
4788
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004789 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004790 {
4791 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004792 if (curlist < 0)
4793 // list is not present
4794 return FAIL;
4795 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004796 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004797 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004798}
4799
4800/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004801 * Jump to the first entry if there is one.
4802 */
4803 static void
4804qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4805{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004806 if (qf_restore_list(qi, save_qfid) == FAIL)
4807 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004808
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004809 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004810 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004811 qf_jump(qi, 0, 0, forceit);
4812}
4813
4814/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004815 * Return TRUE when using ":vimgrep" for ":grep".
4816 */
4817 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004818grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004819{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004820 return ((cmdidx == CMD_grep
4821 || cmdidx == CMD_lgrep
4822 || cmdidx == CMD_grepadd
4823 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004824 && STRCMP("internal",
4825 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4826}
4827
4828/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004829 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004831 static char_u *
4832make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004834 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004835 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004836 case CMD_make: return (char_u *)"make";
4837 case CMD_lmake: return (char_u *)"lmake";
4838 case CMD_grep: return (char_u *)"grep";
4839 case CMD_lgrep: return (char_u *)"lgrep";
4840 case CMD_grepadd: return (char_u *)"grepadd";
4841 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4842 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844}
4845
4846/*
4847 * Return the name for the errorfile, in allocated memory.
4848 * Find a new unique name when 'makeef' contains "##".
4849 * Returns NULL for error.
4850 */
4851 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004852get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 char_u *p;
4855 char_u *name;
4856 static int start = -1;
4857 static int off = 0;
4858#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004859 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860#endif
4861
4862 if (*p_mef == NUL)
4863 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004864 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004866 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 return name;
4868 }
4869
4870 for (p = p_mef; *p; ++p)
4871 if (p[0] == '#' && p[1] == '#')
4872 break;
4873
4874 if (*p == NUL)
4875 return vim_strsave(p_mef);
4876
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004877 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 for (;;)
4879 {
4880 if (start == -1)
4881 start = mch_get_pid();
4882 else
4883 off += 19;
4884
Bram Moolenaar964b3742019-05-24 18:54:09 +02004885 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (name == NULL)
4887 break;
4888 STRCPY(name, p_mef);
4889 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4890 STRCAT(name, p + 2);
4891 if (mch_getperm(name) < 0
4892#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004893 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 && mch_lstat((char *)name, &sb) < 0
4895#endif
4896 )
4897 break;
4898 vim_free(name);
4899 }
4900 return name;
4901}
4902
4903/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004904 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4905 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4906 */
4907 static char_u *
4908make_get_fullcmd(char_u *makecmd, char_u *fname)
4909{
4910 char_u *cmd;
4911 unsigned len;
4912
4913 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4914 if (*p_sp != NUL)
4915 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4916 cmd = alloc(len);
4917 if (cmd == NULL)
4918 return NULL;
4919 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4920 (char *)p_shq);
4921
4922 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4923 if (*p_sp != NUL)
4924 append_redir(cmd, len, p_sp, fname);
4925
4926 // Display the fully formed command. Output a newline if there's something
4927 // else than the :make command that was typed (in which case the cursor is
4928 // in column 0).
4929 if (msg_col == 0)
4930 msg_didout = FALSE;
4931 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004932 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004933 msg_outtrans(cmd); // show what we are doing
4934
4935 return cmd;
4936}
4937
4938/*
4939 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4940 */
4941 void
4942ex_make(exarg_T *eap)
4943{
4944 char_u *fname;
4945 char_u *cmd;
4946 char_u *enc = NULL;
4947 win_T *wp = NULL;
4948 qf_info_T *qi = &ql_info;
4949 int res;
4950 char_u *au_name = NULL;
4951 int_u save_qfid;
4952
4953 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4954 if (grep_internal(eap->cmdidx))
4955 {
4956 ex_vimgrep(eap);
4957 return;
4958 }
4959
4960 au_name = make_get_auname(eap->cmdidx);
4961 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4962 curbuf->b_fname, TRUE, curbuf))
4963 {
4964#ifdef FEAT_EVAL
4965 if (aborting())
4966 return;
4967#endif
4968 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004969 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004970
4971 if (is_loclist_cmd(eap->cmdidx))
4972 wp = curwin;
4973
4974 autowrite_all();
4975 fname = get_mef_name();
4976 if (fname == NULL)
4977 return;
4978 mch_remove(fname); // in case it's not unique
4979
4980 cmd = make_get_fullcmd(eap->arg, fname);
4981 if (cmd == NULL)
4982 return;
4983
4984 // let the shell know if we are redirecting output or not
4985 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4986
4987#ifdef AMIGA
4988 out_flush();
4989 // read window status report and redraw before message
4990 (void)char_avail();
4991#endif
4992
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004993 incr_quickfix_busy();
4994
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004995 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4996 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4997 (eap->cmdidx != CMD_grepadd
4998 && eap->cmdidx != CMD_lgrepadd),
4999 qf_cmdtitle(*eap->cmdlinep), enc);
5000 if (wp != NULL)
5001 {
5002 qi = GET_LOC_LIST(wp);
5003 if (qi == NULL)
5004 goto cleanup;
5005 }
5006 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005007 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005008
5009 // Remember the current quickfix list identifier, so that we can
5010 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005011 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005012 if (au_name != NULL)
5013 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5014 curbuf->b_fname, TRUE, curbuf);
5015 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5016 // display the first error
5017 qf_jump_first(qi, save_qfid, FALSE);
5018
5019cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005020 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005021 mch_remove(fname);
5022 vim_free(fname);
5023 vim_free(cmd);
5024}
5025
5026/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005027 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005028 */
5029 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005030qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005031{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005032 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005033
5034 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5035 return 0;
5036 return qf_get_curlist(qi)->qf_count;
5037}
5038
5039/*
5040 * Returns the number of valid entries in the current quickfix/location list.
5041 */
5042 int
5043qf_get_valid_size(exarg_T *eap)
5044{
5045 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005046 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005047 qfline_T *qfp;
5048 int i, sz = 0;
5049 int prev_fnum = 0;
5050
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005051 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5052 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005053
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005054 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005055 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005056 {
5057 if (qfp->qf_valid)
5058 {
5059 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005060 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005061 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5062 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005063 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005064 sz++;
5065 prev_fnum = qfp->qf_fnum;
5066 }
5067 }
5068 }
5069
5070 return sz;
5071}
5072
5073/*
5074 * Returns the current index of the quickfix/location list.
5075 * Returns 0 if there is an error.
5076 */
5077 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005078qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005079{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005080 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005081
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005082 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5083 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005084
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005085 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005086}
5087
5088/*
5089 * Returns the current index in the quickfix/location list (counting only valid
5090 * entries). If no valid entries are in the list, then returns 1.
5091 */
5092 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005093qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005094{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005095 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005096 qf_list_T *qfl;
5097 qfline_T *qfp;
5098 int i, eidx = 0;
5099 int prev_fnum = 0;
5100
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005101 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5102 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005103
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005104 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005105 qfp = qfl->qf_start;
5106
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005107 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005108 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005109 return 1;
5110
5111 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5112 {
5113 if (qfp->qf_valid)
5114 {
5115 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5116 {
5117 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5118 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005119 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005120 eidx++;
5121 prev_fnum = qfp->qf_fnum;
5122 }
5123 }
5124 else
5125 eidx++;
5126 }
5127 }
5128
5129 return eidx ? eidx : 1;
5130}
5131
5132/*
5133 * Get the 'n'th valid error entry in the quickfix or location list.
5134 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5135 * For :cdo and :ldo returns the 'n'th valid error entry.
5136 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5137 */
5138 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005139qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005140{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005141 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005142 int i, eidx;
5143 int prev_fnum = 0;
5144
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005145 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005146 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005147 return 1;
5148
Bram Moolenaar95946f12019-03-31 15:31:59 +02005149 eidx = 0;
5150 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005151 {
5152 if (qfp->qf_valid)
5153 {
5154 if (fdo)
5155 {
5156 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5157 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005158 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005159 eidx++;
5160 prev_fnum = qfp->qf_fnum;
5161 }
5162 }
5163 else
5164 eidx++;
5165 }
5166
5167 if (eidx == n)
5168 break;
5169 }
5170
5171 if (i <= qfl->qf_count)
5172 return i;
5173 else
5174 return 1;
5175}
5176
5177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005179 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005180 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
5182 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005183ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005185 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005186 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005187
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005188 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5189 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005190
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005191 if (eap->addr_count > 0)
5192 errornr = (int)eap->line2;
5193 else
5194 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005195 switch (eap->cmdidx)
5196 {
5197 case CMD_cc: case CMD_ll:
5198 errornr = 0;
5199 break;
5200 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5201 case CMD_lfirst:
5202 errornr = 1;
5203 break;
5204 default:
5205 errornr = 32767;
5206 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005207 }
5208
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005209 // For cdo and ldo commands, jump to the nth valid error.
5210 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005211 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5212 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005213 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005214 eap->addr_count > 0 ? (int)eap->line1 : 1,
5215 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5216
5217 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218}
5219
5220/*
5221 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005222 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005223 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
5225 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005226ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005228 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005229 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005230 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005231
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005232 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5233 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005234
Bram Moolenaar55b69262017-08-13 13:42:01 +02005235 if (eap->addr_count > 0
5236 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5237 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005238 errornr = (int)eap->line2;
5239 else
5240 errornr = 1;
5241
Bram Moolenaar39665952018-08-15 20:59:48 +02005242 // Depending on the command jump to either next or previous entry/file.
5243 switch (eap->cmdidx)
5244 {
5245 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5246 dir = FORWARD;
5247 break;
5248 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5249 case CMD_lNext:
5250 dir = BACKWARD;
5251 break;
5252 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5253 dir = FORWARD_FILE;
5254 break;
5255 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5256 dir = BACKWARD_FILE;
5257 break;
5258 default:
5259 dir = FORWARD;
5260 break;
5261 }
5262
5263 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264}
5265
5266/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005267 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5268 * The index of the entry is stored in 'errornr'.
5269 * Returns NULL if an entry is not found.
5270 */
5271 static qfline_T *
5272qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5273{
5274 qfline_T *qfp = NULL;
5275 int idx = 0;
5276
5277 // Find the first entry in this file
5278 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5279 if (qfp->qf_fnum == bnr)
5280 break;
5281
5282 *errornr = idx;
5283 return qfp;
5284}
5285
5286/*
5287 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5288 * with the error number for the first entry. Assumes the entries are sorted in
5289 * the quickfix list by line number.
5290 */
5291 static qfline_T *
5292qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5293{
5294 while (!got_int
5295 && entry->qf_prev != NULL
5296 && entry->qf_fnum == entry->qf_prev->qf_fnum
5297 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5298 {
5299 entry = entry->qf_prev;
5300 --*errornr;
5301 }
5302
5303 return entry;
5304}
5305
5306/*
5307 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5308 * with the error number for the last entry. Assumes the entries are sorted in
5309 * the quickfix list by line number.
5310 */
5311 static qfline_T *
5312qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5313{
5314 while (!got_int &&
5315 entry->qf_next != NULL
5316 && entry->qf_fnum == entry->qf_next->qf_fnum
5317 && entry->qf_lnum == entry->qf_next->qf_lnum)
5318 {
5319 entry = entry->qf_next;
5320 ++*errornr;
5321 }
5322
5323 return entry;
5324}
5325
5326/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005327 * Returns TRUE if the specified quickfix entry is
5328 * after the given line (linewise is TRUE)
5329 * or after the line and column.
5330 */
5331 static int
5332qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5333{
5334 if (linewise)
5335 return qfp->qf_lnum > pos->lnum;
5336 else
5337 return (qfp->qf_lnum > pos->lnum ||
5338 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5339}
5340
5341/*
5342 * Returns TRUE if the specified quickfix entry is
5343 * before the given line (linewise is TRUE)
5344 * or before the line and column.
5345 */
5346 static int
5347qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5348{
5349 if (linewise)
5350 return qfp->qf_lnum < pos->lnum;
5351 else
5352 return (qfp->qf_lnum < pos->lnum ||
5353 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5354}
5355
5356/*
5357 * Returns TRUE if the specified quickfix entry is
5358 * on or after the given line (linewise is TRUE)
5359 * or on or after the line and column.
5360 */
5361 static int
5362qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5363{
5364 if (linewise)
5365 return qfp->qf_lnum >= pos->lnum;
5366 else
5367 return (qfp->qf_lnum > pos->lnum ||
5368 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5369}
5370
5371/*
5372 * Returns TRUE if the specified quickfix entry is
5373 * on or before the given line (linewise is TRUE)
5374 * or on or before the line and column.
5375 */
5376 static int
5377qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5378{
5379 if (linewise)
5380 return qfp->qf_lnum <= pos->lnum;
5381 else
5382 return (qfp->qf_lnum < pos->lnum ||
5383 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5384}
5385
5386/*
5387 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5388 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5389 * multiple entries on a single line as one. Otherwise returns the entry after
5390 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005391 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5392 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005393 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005394 */
5395 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005396qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005397 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005398 pos_T *pos,
5399 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005400 qfline_T *qfp,
5401 int *errornr)
5402{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005403 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005404 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005405 return qfp;
5406
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005407 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005408 while (qfp->qf_next != NULL
5409 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005410 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005411 {
5412 qfp = qfp->qf_next;
5413 ++*errornr;
5414 }
5415
5416 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005417 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005418 return NULL;
5419
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005420 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005421 qfp = qfp->qf_next;
5422 ++*errornr;
5423
5424 return qfp;
5425}
5426
5427/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005428 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5429 * If 'linewise' is TRUE, returns the entry before the specified line and
5430 * treats multiple entries on a single line as one. Otherwise returns the entry
5431 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005432 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5433 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005434 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005435 */
5436 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005437qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005438 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005439 pos_T *pos,
5440 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005441 qfline_T *qfp,
5442 int *errornr)
5443{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005444 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005445 while (qfp->qf_next != NULL
5446 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005447 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005448 {
5449 qfp = qfp->qf_next;
5450 ++*errornr;
5451 }
5452
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005453 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005454 return NULL;
5455
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005456 if (linewise)
5457 // If multiple entries are on the same line, then use the first entry
5458 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005459
5460 return qfp;
5461}
5462
5463/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005464 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005465 * the direction 'dir'.
5466 */
5467 static qfline_T *
5468qf_find_closest_entry(
5469 qf_list_T *qfl,
5470 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005471 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005472 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005473 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005474 int *errornr)
5475{
5476 qfline_T *qfp;
5477
5478 *errornr = 0;
5479
5480 // Find the first entry in this file
5481 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5482 if (qfp == NULL)
5483 return NULL; // no entry in this file
5484
5485 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005486 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005487 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005488 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005489
5490 return qfp;
5491}
5492
5493/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005494 * Get the nth quickfix entry below the specified entry. Searches forward in
5495 * the list. If linewise is TRUE, then treat multiple entries on a single line
5496 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005497 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005498 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005499qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005500{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005501 qfline_T *entry = entry_arg;
5502
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005503 while (n-- > 0 && !got_int)
5504 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005505 int first_errornr = *errornr;
5506
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005507 if (linewise)
5508 // Treat all the entries on the same line in this file as one
5509 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005510
5511 if (entry->qf_next == NULL
5512 || entry->qf_next->qf_fnum != entry->qf_fnum)
5513 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005514 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005515 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005516 break;
5517 }
5518
5519 entry = entry->qf_next;
5520 ++*errornr;
5521 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005522}
5523
5524/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005525 * Get the nth quickfix entry above the specified entry. Searches backwards in
5526 * the list. If linewise is TRUE, then treat multiple entries on a single line
5527 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005528 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005529 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005530qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005531{
5532 while (n-- > 0 && !got_int)
5533 {
5534 if (entry->qf_prev == NULL
5535 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5536 break;
5537
5538 entry = entry->qf_prev;
5539 --*errornr;
5540
5541 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005542 if (linewise)
5543 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005544 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005545}
5546
5547/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005548 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5549 * the specified direction. Returns the error number in the quickfix list or 0
5550 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005551 */
5552 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005553qf_find_nth_adj_entry(
5554 qf_list_T *qfl,
5555 int bnr,
5556 pos_T *pos,
5557 int n,
5558 int dir,
5559 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005560{
5561 qfline_T *adj_entry;
5562 int errornr;
5563
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005564 // Find an entry closest to the specified position
5565 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005566 if (adj_entry == NULL)
5567 return 0;
5568
5569 if (--n > 0)
5570 {
5571 // Go to the n'th entry in the current buffer
5572 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005573 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005574 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005575 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005576 }
5577
5578 return errornr;
5579}
5580
5581/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005582 * Jump to a quickfix entry in the current file nearest to the current line or
5583 * current line/col.
5584 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5585 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005586 */
5587 void
5588ex_cbelow(exarg_T *eap)
5589{
5590 qf_info_T *qi;
5591 qf_list_T *qfl;
5592 int dir;
5593 int buf_has_flag;
5594 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005595 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005596
5597 if (eap->addr_count > 0 && eap->line2 <= 0)
5598 {
5599 emsg(_(e_invrange));
5600 return;
5601 }
5602
5603 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005604 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5605 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005606 buf_has_flag = BUF_HAS_QF_ENTRY;
5607 else
5608 buf_has_flag = BUF_HAS_LL_ENTRY;
5609 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5610 {
5611 emsg(_(e_quickfix));
5612 return;
5613 }
5614
5615 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5616 return;
5617
5618 qfl = qf_get_curlist(qi);
5619 // check if the list has valid errors
5620 if (!qf_list_has_valid_entries(qfl))
5621 {
5622 emsg(_(e_quickfix));
5623 return;
5624 }
5625
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005626 if (eap->cmdidx == CMD_cbelow
5627 || eap->cmdidx == CMD_lbelow
5628 || eap->cmdidx == CMD_cafter
5629 || eap->cmdidx == CMD_lafter)
5630 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005631 dir = FORWARD;
5632 else
5633 dir = BACKWARD;
5634
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005635 pos = curwin->w_cursor;
5636 // A quickfix entry column number is 1 based whereas cursor column
5637 // number is 0 based. Adjust the column number.
5638 pos.col++;
5639 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5640 eap->addr_count > 0 ? eap->line2 : 0, dir,
5641 eap->cmdidx == CMD_cbelow
5642 || eap->cmdidx == CMD_lbelow
5643 || eap->cmdidx == CMD_cabove
5644 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005645
5646 if (errornr > 0)
5647 qf_jump(qi, 0, errornr, FALSE);
5648 else
5649 emsg(_(e_no_more_items));
5650}
5651
5652/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005653 * Return the autocmd name for the :cfile Ex commands
5654 */
5655 static char_u *
5656cfile_get_auname(cmdidx_T cmdidx)
5657{
5658 switch (cmdidx)
5659 {
5660 case CMD_cfile: return (char_u *)"cfile";
5661 case CMD_cgetfile: return (char_u *)"cgetfile";
5662 case CMD_caddfile: return (char_u *)"caddfile";
5663 case CMD_lfile: return (char_u *)"lfile";
5664 case CMD_lgetfile: return (char_u *)"lgetfile";
5665 case CMD_laddfile: return (char_u *)"laddfile";
5666 default: return NULL;
5667 }
5668}
5669
5670/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005671 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005672 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 */
5674 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005675ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005677 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005678 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005679 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005680 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005681 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005682 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005683
Bram Moolenaara16123a2019-03-28 20:31:07 +01005684 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005685 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5686 NULL, FALSE, curbuf))
5687 {
5688#ifdef FEAT_EVAL
5689 if (aborting())
5690 return;
5691#endif
5692 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005693
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005694 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005695#ifdef FEAT_BROWSE
5696 if (cmdmod.browse)
5697 {
5698 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005699 NULL, NULL,
5700 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005701 if (browse_file == NULL)
5702 return;
5703 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5704 vim_free(browse_file);
5705 }
5706 else
5707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005709 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005710
Bram Moolenaar39665952018-08-15 20:59:48 +02005711 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005712 wp = curwin;
5713
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005714 incr_quickfix_busy();
5715
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005716 // This function is used by the :cfile, :cgetfile and :caddfile
5717 // commands.
5718 // :cfile always creates a new quickfix list and jumps to the
5719 // first error.
5720 // :cgetfile creates a new quickfix list but doesn't jump to the
5721 // first error.
5722 // :caddfile adds to an existing quickfix list. If there is no
5723 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005724 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005725 && eap->cmdidx != CMD_laddfile),
5726 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005727 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005728 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005729 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005730 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005731 {
5732 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005733 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005734 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005735 }
5736 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005737 qf_list_changed(qf_get_curlist(qi));
5738 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005739 if (au_name != NULL)
5740 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005741
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005742 // Jump to the first error for a new list and if autocmds didn't
5743 // free the list.
5744 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5745 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005746 // display the first error
5747 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005748
5749 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005750}
5751
5752/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005753 * Return the vimgrep autocmd name.
5754 */
5755 static char_u *
5756vgr_get_auname(cmdidx_T cmdidx)
5757{
5758 switch (cmdidx)
5759 {
5760 case CMD_vimgrep: return (char_u *)"vimgrep";
5761 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5762 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5763 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5764 case CMD_grep: return (char_u *)"grep";
5765 case CMD_lgrep: return (char_u *)"lgrep";
5766 case CMD_grepadd: return (char_u *)"grepadd";
5767 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5768 default: return NULL;
5769 }
5770}
5771
5772/*
5773 * Initialize the regmatch used by vimgrep for pattern "s".
5774 */
5775 static void
5776vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5777{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005778 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005779 regmatch->regprog = NULL;
5780
5781 if (s == NULL || *s == NUL)
5782 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005783 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005784 if (last_search_pat() == NULL)
5785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005786 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005787 return;
5788 }
5789 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5790 }
5791 else
5792 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5793
5794 regmatch->rmm_ic = p_ic;
5795 regmatch->rmm_maxcol = 0;
5796}
5797
5798/*
5799 * Display a file name when vimgrep is running.
5800 */
5801 static void
5802vgr_display_fname(char_u *fname)
5803{
5804 char_u *p;
5805
5806 msg_start();
5807 p = msg_strtrunc(fname, TRUE);
5808 if (p == NULL)
5809 msg_outtrans(fname);
5810 else
5811 {
5812 msg_outtrans(p);
5813 vim_free(p);
5814 }
5815 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005816 msg_didout = FALSE; // overwrite this message
5817 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005818 msg_col = 0;
5819 out_flush();
5820}
5821
5822/*
5823 * Load a dummy buffer to search for a pattern using vimgrep.
5824 */
5825 static buf_T *
5826vgr_load_dummy_buf(
5827 char_u *fname,
5828 char_u *dirname_start,
5829 char_u *dirname_now)
5830{
5831 int save_mls;
5832#if defined(FEAT_SYN_HL)
5833 char_u *save_ei = NULL;
5834#endif
5835 buf_T *buf;
5836
5837#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005838 // Don't do Filetype autocommands to avoid loading syntax and
5839 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005840 save_ei = au_event_disable(",Filetype");
5841#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005842 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005843 save_mls = p_mls;
5844 p_mls = 0;
5845
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005846 // Load file into a buffer, so that 'fileencoding' is detected,
5847 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005848 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5849
5850 p_mls = save_mls;
5851#if defined(FEAT_SYN_HL)
5852 au_event_restore(save_ei);
5853#endif
5854
5855 return buf;
5856}
5857
5858/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005859 * Check whether a quickfix/location list is valid. Autocmds may remove or
5860 * change a quickfix list when vimgrep is running. If the list is not found,
5861 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005862 */
5863 static int
5864vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005865 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005866 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005867 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005868 char_u *title)
5869{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005870 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005871 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005872 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005873 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005874 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005875 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005876 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005877 return FALSE;
5878 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005879 else
5880 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005881 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005882 qf_new_list(qi, title);
5883 return TRUE;
5884 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005885 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005886
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005887 if (qf_restore_list(qi, qfid) == FAIL)
5888 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005889
5890 return TRUE;
5891}
5892
5893/*
5894 * Search for a pattern in all the lines in a buffer and add the matching lines
5895 * to a quickfix list.
5896 */
5897 static int
5898vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005899 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005900 char_u *fname,
5901 buf_T *buf,
5902 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005903 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005904 int duplicate_name,
5905 int flags)
5906{
5907 int found_match = FALSE;
5908 long lnum;
5909 colnr_T col;
5910
Bram Moolenaar1c299432018-10-28 14:36:09 +01005911 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005912 {
5913 col = 0;
5914 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5915 col, NULL, NULL) > 0)
5916 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005917 // Pass the buffer number so that it gets used even for a
5918 // dummy buffer, unless duplicate_name is set, then the
5919 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005920 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005921 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005922 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005923 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005924 duplicate_name ? 0 : buf->b_fnum,
5925 ml_get_buf(buf,
5926 regmatch->startpos[0].lnum + lnum, FALSE),
5927 regmatch->startpos[0].lnum + lnum,
5928 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005929 FALSE, // vis_col
5930 NULL, // search pattern
5931 0, // nr
5932 0, // type
5933 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005934 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005935 {
5936 got_int = TRUE;
5937 break;
5938 }
5939 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005940 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005941 break;
5942 if ((flags & VGR_GLOBAL) == 0
5943 || regmatch->endpos[0].lnum > 0)
5944 break;
5945 col = regmatch->endpos[0].col
5946 + (col == regmatch->endpos[0].col);
5947 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5948 break;
5949 }
5950 line_breakcheck();
5951 if (got_int)
5952 break;
5953 }
5954
5955 return found_match;
5956}
5957
5958/*
5959 * Jump to the first match and update the directory.
5960 */
5961 static void
5962vgr_jump_to_match(
5963 qf_info_T *qi,
5964 int forceit,
5965 int *redraw_for_dummy,
5966 buf_T *first_match_buf,
5967 char_u *target_dir)
5968{
5969 buf_T *buf;
5970
5971 buf = curbuf;
5972 qf_jump(qi, 0, 0, forceit);
5973 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005974 // If we jumped to another buffer redrawing will already be
5975 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005976 *redraw_for_dummy = FALSE;
5977
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005978 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005979 if (curbuf == first_match_buf && target_dir != NULL)
5980 {
5981 exarg_T ea;
5982
Bram Moolenaara80faa82020-04-12 19:37:17 +02005983 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005984 ea.arg = target_dir;
5985 ea.cmdidx = CMD_lcd;
5986 ex_cd(&ea);
5987 }
5988}
5989
5990/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005991 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00005992 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005993typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00005994{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005995 long tomatch; // maximum number of matches to find
5996 char_u *spat; // search pattern
5997 int flags; // search modifier
5998 char_u **fnames; // list of files to search
5999 int fcount; // number of files
6000 regmmatch_T regmatch; // compiled search pattern
6001 char_u *qf_title; // quickfix list title
6002} vgr_args_T;
6003
6004/*
6005 * Process :vimgrep command arguments. The command syntax is:
6006 *
6007 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6008 */
6009 static int
6010vgr_process_args(
6011 exarg_T *eap,
6012 vgr_args_T *args)
6013{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006014 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006015
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006016 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006017
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006018 args->regmatch.regprog = NULL;
6019 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006020
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006021 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006022 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006023 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006024 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006025
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006026 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006027 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006028 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006030 emsg(_(e_invalpat));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006031 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006032 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006033
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006034 vgr_init_regmatch(&args->regmatch, args->spat);
6035 if (args->regmatch.regprog == NULL)
6036 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006037
6038 p = skipwhite(p);
6039 if (*p == NUL)
6040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006041 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006042 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006043 }
6044
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006045 // parse the list of arguments
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006046 if (get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL)
6047 return FAIL;
6048 if (args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006050 emsg(_(e_nomatch));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006051 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006052 }
6053
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006054 return OK;
6055}
6056
6057/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006058 * Return TRUE if "buf" had an existing swap file, the current swap file does
6059 * not end in ".swp".
6060 */
6061 static int
6062existing_swapfile(buf_T *buf)
6063{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006064 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006065 {
6066 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6067 size_t len = STRLEN(fname);
6068
6069 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6070 }
6071 return FALSE;
6072}
6073
6074/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006075 * Search for a pattern in a list of files and populate the quickfix list with
6076 * the matches.
6077 */
6078 static int
6079vgr_process_files(
6080 win_T *wp,
6081 qf_info_T *qi,
6082 vgr_args_T *cmd_args,
6083 int *redraw_for_dummy,
6084 buf_T **first_match_buf,
6085 char_u **target_dir)
6086{
6087 int status = FAIL;
6088 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6089 time_t seconds = 0;
6090 char_u *fname;
6091 int fi;
6092 buf_T *buf;
6093 int duplicate_name = FALSE;
6094 int using_dummy;
6095 char_u *dirname_start = NULL;
6096 char_u *dirname_now = NULL;
6097 int found_match;
6098 aco_save_T aco;
6099
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006100 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6101 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006102 if (dirname_start == NULL || dirname_now == NULL)
6103 goto theend;
6104
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006105 // Remember the current directory, because a BufRead autocommand that does
6106 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006107 mch_dirname(dirname_start, MAXPATHL);
6108
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006109 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006110 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6111 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006112 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006113 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006114 if (time(NULL) > seconds)
6115 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006116 // Display the file name every second or so, show the user we are
6117 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006118 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006119 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006120 }
6121
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006122 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006123 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6124 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006125 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006126 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006127 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006128 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006129
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006130 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006131 }
6132 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006133 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006134 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006135
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006136 // Check whether the quickfix list is still valid. When loading a
6137 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006138 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006139 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006140
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006141 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006142
Bram Moolenaar81695252004-12-29 20:58:21 +00006143 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006144 {
6145 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006146 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006147 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006148 else
6149 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006150 // Try for a match in all lines of the buffer.
6151 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006152 found_match = vgr_match_buflines(qf_get_curlist(qi),
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006153 fname, buf, &cmd_args->regmatch,
6154 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006155
Bram Moolenaar81695252004-12-29 20:58:21 +00006156 if (using_dummy)
6157 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006158 if (found_match && *first_match_buf == NULL)
6159 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006160 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006161 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006162 // Never keep a dummy buffer if there is another buffer
6163 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006164 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006165 buf = NULL;
6166 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00006167 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006168 || buf->b_p_bh[0] == 'u' // "unload"
6169 || buf->b_p_bh[0] == 'w' // "wipe"
6170 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006171 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006172 // When no match was found we don't need to remember the
6173 // buffer, wipe it out. If there was a match and it
6174 // wasn't the first one or we won't jump there: only
6175 // unload the buffer.
6176 // Ignore 'hidden' here, because it may lead to having too
6177 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006178 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006179 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006180 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006181 buf = NULL;
6182 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006183 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006184 || (cmd_args->flags & VGR_NOJUMP)
6185 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006186 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006187 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006188 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006189 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006190 buf = NULL;
6191 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006192 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006193
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006194 if (buf != NULL)
6195 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006196 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006197 buf->b_flags &= ~BF_DUMMY;
6198
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006199 // If the buffer is still loaded we need to use the
6200 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006201 if (buf == *first_match_buf
6202 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006203 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006204 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006205
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006206 // The buffer is still loaded, the Filetype autocommands
6207 // need to be done now, in that buffer. And the modelines
6208 // need to be done (again). But not the window-local
6209 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006210 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006211#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006212 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6213 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006214#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006215 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006216 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006217 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006218 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006219 }
6220 }
6221
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006222 status = OK;
6223
6224theend:
6225 vim_free(dirname_now);
6226 vim_free(dirname_start);
6227 return status;
6228}
6229
6230/*
6231 * ":vimgrep {pattern} file(s)"
6232 * ":vimgrepadd {pattern} file(s)"
6233 * ":lvimgrep {pattern} file(s)"
6234 * ":lvimgrepadd {pattern} file(s)"
6235 */
6236 void
6237ex_vimgrep(exarg_T *eap)
6238{
6239 vgr_args_T args;
6240 qf_info_T *qi;
6241 qf_list_T *qfl;
6242 int_u save_qfid;
6243 win_T *wp = NULL;
6244 int redraw_for_dummy = FALSE;
6245 buf_T *first_match_buf = NULL;
6246 char_u *target_dir = NULL;
6247 char_u *au_name = NULL;
6248 int status;
6249
6250 au_name = vgr_get_auname(eap->cmdidx);
6251 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6252 curbuf->b_fname, TRUE, curbuf))
6253 {
6254#ifdef FEAT_EVAL
6255 if (aborting())
6256 return;
6257#endif
6258 }
6259
6260 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6261 if (qi == NULL)
6262 return;
6263
6264 if (vgr_process_args(eap, &args) == FAIL)
6265 goto theend;
6266
6267 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6268 && eap->cmdidx != CMD_vimgrepadd
6269 && eap->cmdidx != CMD_lvimgrepadd)
6270 || qf_stack_empty(qi))
6271 // make place for a new list
6272 qf_new_list(qi, args.qf_title);
6273
6274 incr_quickfix_busy();
6275
6276 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6277 &first_match_buf, &target_dir);
6278 if (status != OK)
6279 {
6280 FreeWild(args.fcount, args.fnames);
6281 decr_quickfix_busy();
6282 goto theend;
6283 }
6284
6285 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006286
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006287 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006288 qfl->qf_nonevalid = FALSE;
6289 qfl->qf_ptr = qfl->qf_start;
6290 qfl->qf_index = 1;
6291 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006292
Bram Moolenaar864293a2016-06-02 13:40:04 +02006293 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006294
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006295 // Remember the current quickfix list identifier, so that we can check for
6296 // autocommands changing the current quickfix list.
6297 save_qfid = qf_get_curlist(qi)->qf_id;
6298
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006299 if (au_name != NULL)
6300 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6301 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006302 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6303 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006304 if (!qflist_valid(wp, save_qfid)
6305 || qf_restore_list(qi, save_qfid) == FAIL)
6306 {
6307 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006308 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006309 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006310
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006311 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006312 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006313 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006314 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006315 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6316 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006317 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006318 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006319 semsg(_(e_nomatch2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006320
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006321 decr_quickfix_busy();
6322
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006323 // If we loaded a dummy buffer into the current window, the autocommands
6324 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006325 if (redraw_for_dummy)
6326 {
6327#ifdef FEAT_FOLDING
6328 foldUpdateAll(curwin);
6329#else
6330 redraw_later(NOT_VALID);
6331#endif
6332 }
6333
Bram Moolenaar86b68352004-12-27 21:59:20 +00006334theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006335 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006336 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006337 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006338}
6339
6340/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006341 * Restore current working directory to "dirname_start" if they differ, taking
6342 * into account whether it is set locally or globally.
6343 */
6344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006345restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006346{
6347 char_u *dirname_now = alloc(MAXPATHL);
6348
6349 if (NULL != dirname_now)
6350 {
6351 mch_dirname(dirname_now, MAXPATHL);
6352 if (STRCMP(dirname_start, dirname_now) != 0)
6353 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006354 // If the directory has changed, change it back by building up an
6355 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006356 exarg_T ea;
6357
Bram Moolenaara80faa82020-04-12 19:37:17 +02006358 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006359 ea.arg = dirname_start;
6360 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6361 ex_cd(&ea);
6362 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006363 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006364 }
6365}
6366
6367/*
6368 * Load file "fname" into a dummy buffer and return the buffer pointer,
6369 * placing the directory resulting from the buffer load into the
6370 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6371 * prior to calling this function. Restores directory to "dirname_start" prior
6372 * to returning, if autocmds or the 'autochdir' option have changed it.
6373 *
6374 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6375 * or wipe_dummy_buffer() later!
6376 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006377 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006378 */
6379 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006380load_dummy_buffer(
6381 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006382 char_u *dirname_start, // in: old directory
6383 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006384{
6385 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006386 bufref_T newbufref;
6387 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006388 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006389 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006390 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006391
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006392 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006393 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6394 if (newbuf == NULL)
6395 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006396 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006397
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006398 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006399 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6400
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006401 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006402 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006403 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006404 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006405 ++newbuf->b_locked;
6406
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006407 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006408 aucmd_prepbuf(&aco, newbuf);
6409
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006410 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006411 (void)setfname(curbuf, fname, NULL, FALSE);
6412
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006413 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006414 check_need_swap(TRUE);
6415
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006416 // Remove the "dummy" flag, otherwise autocommands may not
6417 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006418 curbuf->b_flags &= ~BF_DUMMY;
6419
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006420 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006421 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006422 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006423 NULL, READ_NEW | READ_DUMMY);
6424 --newbuf->b_locked;
6425 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006426 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006427 && !(curbuf->b_flags & BF_NEW))
6428 {
6429 failed = FALSE;
6430 if (curbuf != newbuf)
6431 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006432 // Bloody autocommands changed the buffer! Can happen when
6433 // using netrw and editing a remote file. Use the current
6434 // buffer instead, delete the dummy one after restoring the
6435 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006436 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006437 newbuf = curbuf;
6438 }
6439 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006440
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006441 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006442 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006443 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6444 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006445
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006446 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6447 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006448 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006449 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006450
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006451 // When autocommands/'autochdir' option changed directory: go back.
6452 // Let the caller know what the resulting dir was first, in case it is
6453 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006454 mch_dirname(resulting_dir, MAXPATHL);
6455 restore_start_dir(dirname_start);
6456
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006457 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006458 return NULL;
6459 if (failed)
6460 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006461 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006462 return NULL;
6463 }
6464 return newbuf;
6465}
6466
6467/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006468 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6469 * directory to "dirname_start" prior to returning, if autocmds or the
6470 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006471 */
6472 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006473wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006474{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006475 // If any autocommand opened a window on the dummy buffer, close that
6476 // window. If we can't close them all then give up.
6477 while (buf->b_nwindows > 0)
6478 {
6479 int did_one = FALSE;
6480 win_T *wp;
6481
6482 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006483 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006484 if (wp->w_buffer == buf)
6485 {
6486 if (win_close(wp, FALSE) == OK)
6487 did_one = TRUE;
6488 break;
6489 }
6490 if (!did_one)
6491 return;
6492 }
6493
6494 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006495 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006496#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006497 cleanup_T cs;
6498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006499 // Reset the error/interrupt/exception state here so that aborting()
6500 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6501 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006502 enter_cleanup(&cs);
6503#endif
6504
Bram Moolenaar81695252004-12-29 20:58:21 +00006505 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006506
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006507#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006508 // Restore the error/interrupt/exception state if not discarded by a
6509 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006510 leave_cleanup(&cs);
6511#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006512 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006513 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006514 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006515}
6516
6517/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006518 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6519 * directory to "dirname_start" prior to returning, if autocmds or the
6520 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006521 */
6522 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006523unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006524{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006525 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006526 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006527 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006528
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006529 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006530 restore_start_dir(dirname_start);
6531 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006532}
6533
Bram Moolenaar05159a02005-02-26 23:04:13 +00006534#if defined(FEAT_EVAL) || defined(PROTO)
6535/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006536 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006537 * to 'list'. Returns OK on success.
6538 */
6539 static int
6540get_qfline_items(qfline_T *qfp, list_T *list)
6541{
6542 int bufnum;
6543 dict_T *dict;
6544 char_u buf[2];
6545
6546 // Handle entries with a non-existing buffer number.
6547 bufnum = qfp->qf_fnum;
6548 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6549 bufnum = 0;
6550
6551 if ((dict = dict_alloc()) == NULL)
6552 return FAIL;
6553 if (list_append_dict(list, dict) == FAIL)
6554 return FAIL;
6555
6556 buf[0] = qfp->qf_type;
6557 buf[1] = NUL;
6558 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
6559 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6560 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6561 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6562 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
6563 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6564 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6565 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6566 || dict_add_string(dict, "type", buf) == FAIL
6567 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6568 return FAIL;
6569
6570 return OK;
6571}
6572
6573/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006574 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006575 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006576 * If eidx is not 0, then return only the specified entry. Otherwise return
6577 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006578 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006579 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006580get_errorlist(
6581 qf_info_T *qi_arg,
6582 win_T *wp,
6583 int qf_idx,
6584 int eidx,
6585 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006586{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006587 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006588 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006589 qfline_T *qfp;
6590 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006591
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006592 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006593 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006594 qi = &ql_info;
6595 if (wp != NULL)
6596 {
6597 qi = GET_LOC_LIST(wp);
6598 if (qi == NULL)
6599 return FAIL;
6600 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006601 }
6602
Bram Moolenaar858ba062020-05-31 23:11:59 +02006603 if (eidx < 0)
6604 return OK;
6605
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006606 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006607 qf_idx = qi->qf_curlist;
6608
Bram Moolenaar0398e002019-03-21 21:12:49 +01006609 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006610 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006611
Bram Moolenaar0398e002019-03-21 21:12:49 +01006612 qfl = qf_get_list(qi, qf_idx);
6613 if (qf_list_empty(qfl))
6614 return FAIL;
6615
Bram Moolenaara16123a2019-03-28 20:31:07 +01006616 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006617 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006618 if (eidx > 0)
6619 {
6620 if (eidx == i)
6621 return get_qfline_items(qfp, list);
6622 }
6623 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006624 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006625 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006626
Bram Moolenaar05159a02005-02-26 23:04:13 +00006627 return OK;
6628}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006629
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006630// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006631enum {
6632 QF_GETLIST_NONE = 0x0,
6633 QF_GETLIST_TITLE = 0x1,
6634 QF_GETLIST_ITEMS = 0x2,
6635 QF_GETLIST_NR = 0x4,
6636 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006637 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006638 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006639 QF_GETLIST_IDX = 0x40,
6640 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006641 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006642 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006643 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006644 QF_GETLIST_QFTF = 0x800,
6645 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006646};
6647
6648/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006649 * Parse text from 'di' and return the quickfix list items.
6650 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006651 */
6652 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006653qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006654{
6655 int status = FAIL;
6656 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006657 char_u *errorformat = p_efm;
6658 dictitem_T *efm_di;
6659 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006660
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006661 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006662 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006663 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006664 // If errorformat is supplied then use it, otherwise use the 'efm'
6665 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006666 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6667 {
6668 if (efm_di->di_tv.v_type != VAR_STRING ||
6669 efm_di->di_tv.vval.v_string == NULL)
6670 return FAIL;
6671 errorformat = efm_di->di_tv.vval.v_string;
6672 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006673
Bram Moolenaar36538222017-09-02 19:51:44 +02006674 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006675 if (l == NULL)
6676 return FAIL;
6677
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006678 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006679 if (qi != NULL)
6680 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006681 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006682 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6683 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006684 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006685 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006686 }
6687 free(qi);
6688 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006689 dict_add_list(retdict, "items", l);
6690 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006691 }
6692
6693 return status;
6694}
6695
6696/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006697 * Return the quickfix/location list window identifier in the current tabpage.
6698 */
6699 static int
6700qf_winid(qf_info_T *qi)
6701{
6702 win_T *win;
6703
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006704 // The quickfix window can be opened even if the quickfix list is not set
6705 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006706 if (qi == NULL)
6707 return 0;
6708 win = qf_find_win(qi);
6709 if (win != NULL)
6710 return win->w_id;
6711 return 0;
6712}
6713
6714/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006715 * Returns the number of the buffer displayed in the quickfix/location list
6716 * window. If there is no buffer associated with the list, then returns 0.
6717 */
6718 static int
6719qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6720{
6721 return dict_add_number(retdict, "qfbufnr",
6722 (qi == NULL) ? 0 : qi->qf_bufnr);
6723}
6724
6725/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006726 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006727 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006728 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006729qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006730{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006731 int flags = QF_GETLIST_NONE;
6732
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006733 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006734 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006735 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006736 if (!loclist)
6737 // File window ID is applicable only to location list windows
6738 flags &= ~ QF_GETLIST_FILEWINID;
6739 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006740
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006741 if (dict_find(what, (char_u *)"title", -1) != NULL)
6742 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006743
Bram Moolenaara6d48492017-12-12 22:45:31 +01006744 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6745 flags |= QF_GETLIST_NR;
6746
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006747 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6748 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006749
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006750 if (dict_find(what, (char_u *)"context", -1) != NULL)
6751 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006752
Bram Moolenaara6d48492017-12-12 22:45:31 +01006753 if (dict_find(what, (char_u *)"id", -1) != NULL)
6754 flags |= QF_GETLIST_ID;
6755
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006756 if (dict_find(what, (char_u *)"items", -1) != NULL)
6757 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006758
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006759 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6760 flags |= QF_GETLIST_IDX;
6761
6762 if (dict_find(what, (char_u *)"size", -1) != NULL)
6763 flags |= QF_GETLIST_SIZE;
6764
Bram Moolenaarb254af32017-12-18 19:48:58 +01006765 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6766 flags |= QF_GETLIST_TICK;
6767
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006768 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6769 flags |= QF_GETLIST_FILEWINID;
6770
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006771 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6772 flags |= QF_GETLIST_QFBUFNR;
6773
Bram Moolenaard43906d2020-07-20 21:31:32 +02006774 if (dict_find(what, (char_u *)"quickfixtextfunc", -1) != NULL)
6775 flags |= QF_GETLIST_QFTF;
6776
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006777 return flags;
6778}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006779
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006780/*
6781 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6782 * If 'nr' and 'id' are not present in 'what' then return the current
6783 * quickfix list index.
6784 * If 'nr' is zero then return the current quickfix list index.
6785 * If 'nr' is '$' then return the last quickfix list index.
6786 * If 'id' is present then return the index of the quickfix list with that id.
6787 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6788 * Return -1, if quickfix list is not present or if the stack is empty.
6789 */
6790 static int
6791qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6792{
6793 int qf_idx;
6794 dictitem_T *di;
6795
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006796 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006797 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6798 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006799 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006800 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006801 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006802 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006803 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006804 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006805 qf_idx = di->di_tv.vval.v_number - 1;
6806 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006807 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006808 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006809 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006810 else if (di->di_tv.v_type == VAR_STRING
6811 && di->di_tv.vval.v_string != NULL
6812 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006813 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006814 qf_idx = qi->qf_listcount - 1;
6815 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006816 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006817 }
6818
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006819 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6820 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006821 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006822 if (di->di_tv.v_type == VAR_NUMBER)
6823 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006824 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006825 if (di->di_tv.vval.v_number != 0)
6826 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6827 }
6828 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006829 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006830 }
6831
6832 return qf_idx;
6833}
6834
6835/*
6836 * Return default values for quickfix list properties in retdict.
6837 */
6838 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006839qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006840{
6841 int status = OK;
6842
6843 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006844 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006845 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6846 {
6847 list_T *l = list_alloc();
6848 if (l != NULL)
6849 status = dict_add_list(retdict, "items", l);
6850 else
6851 status = FAIL;
6852 }
6853 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006854 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006855 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006856 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006857 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006858 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006859 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006860 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006861 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006862 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006863 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006864 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006865 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006866 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006867 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006868 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006869 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6870 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02006871 if ((status == OK) && (flags & QF_GETLIST_QFTF))
6872 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006873
6874 return status;
6875}
6876
6877/*
6878 * Return the quickfix list title as 'title' in retdict
6879 */
6880 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006881qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006882{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006883 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006884}
6885
6886/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006887 * Returns the identifier of the window used to display files from a location
6888 * list. If there is no associated window, then returns 0. Useful only when
6889 * called from a location list window.
6890 */
6891 static int
6892qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6893{
6894 int winid = 0;
6895
6896 if (wp != NULL && IS_LL_WINDOW(wp))
6897 {
6898 win_T *ll_wp = qf_find_win_with_loclist(qi);
6899 if (ll_wp != NULL)
6900 winid = ll_wp->w_id;
6901 }
6902
6903 return dict_add_number(retdict, "filewinid", winid);
6904}
6905
6906/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006907 * Return the quickfix list items/entries as 'items' in retdict.
6908 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006909 */
6910 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006911qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006912{
6913 int status = OK;
6914 list_T *l = list_alloc();
6915 if (l != NULL)
6916 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006917 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006918 dict_add_list(retdict, "items", l);
6919 }
6920 else
6921 status = FAIL;
6922
6923 return status;
6924}
6925
6926/*
6927 * Return the quickfix list context (if any) as 'context' in retdict.
6928 */
6929 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006930qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006931{
6932 int status;
6933 dictitem_T *di;
6934
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006935 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006936 {
6937 di = dictitem_alloc((char_u *)"context");
6938 if (di != NULL)
6939 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006940 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006941 status = dict_add(retdict, di);
6942 if (status == FAIL)
6943 dictitem_free(di);
6944 }
6945 else
6946 status = FAIL;
6947 }
6948 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006949 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006950
6951 return status;
6952}
6953
6954/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006955 * Return the current quickfix list index as 'idx' in retdict.
6956 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006957 */
6958 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006959qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006960{
Bram Moolenaar858ba062020-05-31 23:11:59 +02006961 if (eidx == 0)
6962 {
6963 eidx = qfl->qf_index;
6964 if (qf_list_empty(qfl))
6965 // For empty lists, current index is set to 0
6966 eidx = 0;
6967 }
6968 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006969}
6970
6971/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02006972 * Return the 'quickfixtextfunc' function of a quickfix/location list
6973 */
6974 static int
6975qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
6976{
6977 int status;
6978
6979 if (qfl->qftf_cb.cb_name != NULL)
6980 {
6981 typval_T tv;
6982
6983 put_callback(&qfl->qftf_cb, &tv);
6984 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
6985 clear_tv(&tv);
6986 }
6987 else
6988 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
6989
6990 return status;
6991}
6992
6993/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006994 * Return quickfix/location list details (title) as a
6995 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6996 * then current list is used. Otherwise the specified list is used.
6997 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006998 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006999qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7000{
7001 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007002 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007003 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007004 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007005 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007006 dictitem_T *di;
7007 int flags = QF_GETLIST_NONE;
7008
7009 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7010 return qf_get_list_from_lines(what, di, retdict);
7011
7012 if (wp != NULL)
7013 qi = GET_LOC_LIST(wp);
7014
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007015 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007016
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007017 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007018 qf_idx = qf_getprop_qfidx(qi, what);
7019
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007020 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007021 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007022 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007023
Bram Moolenaar0398e002019-03-21 21:12:49 +01007024 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007025
Bram Moolenaar858ba062020-05-31 23:11:59 +02007026 // If an entry index is specified, use that
7027 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7028 {
7029 if (di->di_tv.v_type != VAR_NUMBER)
7030 return FAIL;
7031 eidx = di->di_tv.vval.v_number;
7032 }
7033
Bram Moolenaard823fa92016-08-12 16:29:27 +02007034 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007035 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007036 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007037 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007038 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007039 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007040 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007041 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007042 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007043 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007044 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007045 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007046 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007047 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007048 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007049 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007050 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007051 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007052 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7053 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007054 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7055 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007056 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7057 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007058
Bram Moolenaard823fa92016-08-12 16:29:27 +02007059 return status;
7060}
7061
7062/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007063 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007064 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7065 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007066 */
7067 static int
7068qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007069 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007070 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007071 int first_entry,
7072 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007073{
7074 static int did_bufnr_emsg;
7075 char_u *filename, *module, *pattern, *text, *type;
7076 int bufnum, valid, status, col, vcol, nr;
7077 long lnum;
7078
7079 if (first_entry)
7080 did_bufnr_emsg = FALSE;
7081
Bram Moolenaar8f667172018-12-14 15:38:31 +01007082 filename = dict_get_string(d, (char_u *)"filename", TRUE);
7083 module = dict_get_string(d, (char_u *)"module", TRUE);
7084 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
7085 lnum = (int)dict_get_number(d, (char_u *)"lnum");
7086 col = (int)dict_get_number(d, (char_u *)"col");
7087 vcol = (int)dict_get_number(d, (char_u *)"vcol");
7088 nr = (int)dict_get_number(d, (char_u *)"nr");
7089 type = dict_get_string(d, (char_u *)"type", TRUE);
7090 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
7091 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007092 if (text == NULL)
7093 text = vim_strsave((char_u *)"");
7094
7095 valid = TRUE;
7096 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7097 valid = FALSE;
7098
7099 // Mark entries with non-existing buffer number as not valid. Give the
7100 // error message only once.
7101 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7102 {
7103 if (!did_bufnr_emsg)
7104 {
7105 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01007106 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007107 }
7108 valid = FALSE;
7109 bufnum = 0;
7110 }
7111
7112 // If the 'valid' field is present it overrules the detected value.
7113 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar401f0c02020-09-05 22:37:39 +02007114 valid = (int)dict_get_bool(d, (char_u *)"valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007115
Bram Moolenaar0398e002019-03-21 21:12:49 +01007116 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007117 NULL, // dir
7118 filename,
7119 module,
7120 bufnum,
7121 text,
7122 lnum,
7123 col,
7124 vcol, // vis_col
7125 pattern, // search pattern
7126 nr,
7127 type == NULL ? NUL : *type,
7128 valid);
7129
7130 vim_free(filename);
7131 vim_free(module);
7132 vim_free(pattern);
7133 vim_free(text);
7134 vim_free(type);
7135
Bram Moolenaar9752c722018-12-22 16:49:34 +01007136 if (valid)
7137 *valid_entry = TRUE;
7138
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007139 return status;
7140}
7141
7142/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007143 * Add list of entries to quickfix/location list. Each list entry is
7144 * a dictionary with item information.
7145 */
7146 static int
7147qf_add_entries(
7148 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007149 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007150 list_T *list,
7151 char_u *title,
7152 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007153{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007154 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007155 listitem_T *li;
7156 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007157 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007158 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007159 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007160
Bram Moolenaara3921f42017-06-04 15:30:34 +02007161 if (action == ' ' || qf_idx == qi->qf_listcount)
7162 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007163 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007164 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007165 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007166 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007167 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007168 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007169 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007170 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007171 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007172 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007173 qf_free_items(qfl);
7174 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007175 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007176
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007177 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007178 {
7179 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007180 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007181
7182 d = li->li_tv.vval.v_dict;
7183 if (d == NULL)
7184 continue;
7185
Bram Moolenaar0398e002019-03-21 21:12:49 +01007186 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007187 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007188 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007189 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007190 }
7191
Bram Moolenaar9752c722018-12-22 16:49:34 +01007192 // Check if any valid error entries are added to the list.
7193 if (valid_entry)
7194 qfl->qf_nonevalid = FALSE;
7195 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007196 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007197 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007198
7199 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007200 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007201 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007202
7203 // Update the current error index if not appending to the list or if the
7204 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007205 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007206 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007207
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007208 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007209 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007210
7211 return retval;
7212}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007213
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007214/*
7215 * Get the quickfix list index from 'nr' or 'id'
7216 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007217 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007218qf_setprop_get_qfidx(
7219 qf_info_T *qi,
7220 dict_T *what,
7221 int action,
7222 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007223{
7224 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007225 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007226
Bram Moolenaard823fa92016-08-12 16:29:27 +02007227 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7228 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007229 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007230 if (di->di_tv.v_type == VAR_NUMBER)
7231 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007232 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007233 if (di->di_tv.vval.v_number != 0)
7234 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007235
Bram Moolenaar55b69262017-08-13 13:42:01 +02007236 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7237 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007238 // When creating a new list, accept qf_idx pointing to the next
7239 // non-available list and add the new list at the end of the
7240 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007241 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007242 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007243 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007244 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007245 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007246 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007247 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007248 }
7249 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007250 && di->di_tv.vval.v_string != NULL
7251 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007252 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007253 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007254 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007255 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007256 qf_idx = 0;
7257 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007258 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007259 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007260 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007261 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007262 }
7263
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007264 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007265 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007266 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007267 if (di->di_tv.v_type != VAR_NUMBER)
7268 return INVALID_QFIDX;
7269
7270 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007271 }
7272
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007273 return qf_idx;
7274}
7275
7276/*
7277 * Set the quickfix list title.
7278 */
7279 static int
7280qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7281{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007282 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007283
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007284 if (di->di_tv.v_type != VAR_STRING)
7285 return FAIL;
7286
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007287 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007288 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007289 if (qf_idx == qi->qf_curlist)
7290 qf_update_win_titlevar(qi);
7291
7292 return OK;
7293}
7294
7295/*
7296 * Set quickfix list items/entries.
7297 */
7298 static int
7299qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7300{
7301 int retval = FAIL;
7302 char_u *title_save;
7303
7304 if (di->di_tv.v_type != VAR_LIST)
7305 return FAIL;
7306
7307 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7308 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7309 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007310 vim_free(title_save);
7311
7312 return retval;
7313}
7314
7315/*
7316 * Set quickfix list items/entries from a list of lines.
7317 */
7318 static int
7319qf_setprop_items_from_lines(
7320 qf_info_T *qi,
7321 int qf_idx,
7322 dict_T *what,
7323 dictitem_T *di,
7324 int action)
7325{
7326 char_u *errorformat = p_efm;
7327 dictitem_T *efm_di;
7328 int retval = FAIL;
7329
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007330 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007331 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7332 {
7333 if (efm_di->di_tv.v_type != VAR_STRING ||
7334 efm_di->di_tv.vval.v_string == NULL)
7335 return FAIL;
7336 errorformat = efm_di->di_tv.vval.v_string;
7337 }
7338
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007339 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007340 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7341 return FAIL;
7342
7343 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007344 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007345 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
7346 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7347 retval = OK;
7348
7349 return retval;
7350}
7351
7352/*
7353 * Set quickfix list context.
7354 */
7355 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007356qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007357{
7358 typval_T *ctx;
7359
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007360 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007361 ctx = alloc_tv();
7362 if (ctx != NULL)
7363 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007364 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007365
7366 return OK;
7367}
7368
7369/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007370 * Set the current index in the specified quickfix list
7371 */
7372 static int
7373qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7374{
7375 int denote = FALSE;
7376 int newidx;
7377 int old_qfidx;
7378 qfline_T *qf_ptr;
7379
7380 // If the specified index is '$', then use the last entry
7381 if (di->di_tv.v_type == VAR_STRING
7382 && di->di_tv.vval.v_string != NULL
7383 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7384 newidx = qfl->qf_count;
7385 else
7386 {
7387 // Otherwise use the specified index
7388 newidx = tv_get_number_chk(&di->di_tv, &denote);
7389 if (denote)
7390 return FAIL;
7391 }
7392
7393 if (newidx < 1) // sanity check
7394 return FAIL;
7395 if (newidx > qfl->qf_count)
7396 newidx = qfl->qf_count;
7397
7398 old_qfidx = qfl->qf_index;
7399 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7400 if (qf_ptr == NULL)
7401 return FAIL;
7402 qfl->qf_ptr = qf_ptr;
7403 qfl->qf_index = newidx;
7404
7405 // If the current list is modified and it is displayed in the quickfix
7406 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007407 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007408 qf_win_pos_update(qi, old_qfidx);
7409
7410 return OK;
7411}
7412
7413/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007414 * Set the current index in the specified quickfix list
7415 */
7416 static int
7417qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7418{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007419 callback_T cb;
7420
7421 free_callback(&qfl->qftf_cb);
7422 cb = get_callback(&di->di_tv);
7423 if (cb.cb_name != NULL && *cb.cb_name != NUL)
7424 set_callback(&qfl->qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007425
7426 return OK;
7427}
7428
7429/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007430 * Set quickfix/location list properties (title, items, context).
7431 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007432 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007433 */
7434 static int
7435qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7436{
7437 dictitem_T *di;
7438 int retval = FAIL;
7439 int qf_idx;
7440 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007441 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007442
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007443 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007444 newlist = TRUE;
7445
7446 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007447 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007448 return FAIL;
7449
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007450 if (newlist)
7451 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007452 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007453 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007454 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007455 }
7456
Bram Moolenaar0398e002019-03-21 21:12:49 +01007457 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007458 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007459 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007460 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007461 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007462 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007463 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007464 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007465 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007466 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7467 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007468 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7469 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007470
Bram Moolenaarb254af32017-12-18 19:48:58 +01007471 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007472 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007473
Bram Moolenaard823fa92016-08-12 16:29:27 +02007474 return retval;
7475}
7476
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007477/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007478 * Free the entire quickfix/location list stack.
7479 * If the quickfix/location list window is open, then clear it.
7480 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007481 static void
7482qf_free_stack(win_T *wp, qf_info_T *qi)
7483{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007484 win_T *qfwin = qf_find_win(qi);
7485 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007486
7487 if (qfwin != NULL)
7488 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007489 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007490 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007491 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007492 qf_update_buffer(qi, NULL);
7493 }
7494
7495 if (wp != NULL && IS_LL_WINDOW(wp))
7496 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007497 // If in the location list window, then use the non-location list
7498 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007499 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007500 if (llwin != NULL)
7501 wp = llwin;
7502 }
7503
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007504 qf_free_all(wp);
7505 if (wp == NULL)
7506 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007507 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007508 qi->qf_curlist = 0;
7509 qi->qf_listcount = 0;
7510 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007511 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007512 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007513 // If the location list window is open, then create a new empty
7514 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007515 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007516
Bram Moolenaar95946f12019-03-31 15:31:59 +02007517 if (new_ll != NULL)
7518 {
7519 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007520
Bram Moolenaar95946f12019-03-31 15:31:59 +02007521 // first free the list reference in the location list window
7522 ll_free_all(&qfwin->w_llist_ref);
7523
7524 qfwin->w_llist_ref = new_ll;
7525 if (wp != qfwin)
7526 win_set_loclist(wp, new_ll);
7527 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007528 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007529}
7530
Bram Moolenaard823fa92016-08-12 16:29:27 +02007531/*
7532 * Populate the quickfix list with the items supplied in the list
7533 * of dictionaries. "title" will be copied to w:quickfix_title.
7534 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007535 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007536 */
7537 int
7538set_errorlist(
7539 win_T *wp,
7540 list_T *list,
7541 int action,
7542 char_u *title,
7543 dict_T *what)
7544{
7545 qf_info_T *qi = &ql_info;
7546 int retval = OK;
7547
7548 if (wp != NULL)
7549 {
7550 qi = ll_get_or_alloc_list(wp);
7551 if (qi == NULL)
7552 return FAIL;
7553 }
7554
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007555 if (action == 'f')
7556 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007557 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007558 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007559 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007560 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007561
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007562 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007563 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007564 {
7565 semsg(_(e_invarg2),
7566 _("cannot have both a list and a \"what\" argument"));
7567 return FAIL;
7568 }
7569
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007570 incr_quickfix_busy();
7571
7572 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007573 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007574 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007575 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007576 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007577 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007578 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007579 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007580
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007581 decr_quickfix_busy();
7582
Bram Moolenaard823fa92016-08-12 16:29:27 +02007583 return retval;
7584}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007585
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007586/*
7587 * Mark the context as in use for all the lists in a quickfix stack.
7588 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007589 static int
7590mark_quickfix_ctx(qf_info_T *qi, int copyID)
7591{
7592 int i;
7593 int abort = FALSE;
7594 typval_T *ctx;
7595
7596 for (i = 0; i < LISTCOUNT && !abort; ++i)
7597 {
7598 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007599 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7600 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007601 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
7602 }
7603
7604 return abort;
7605}
7606
7607/*
7608 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007609 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007610 */
7611 int
7612set_ref_in_quickfix(int copyID)
7613{
7614 int abort = FALSE;
7615 tabpage_T *tp;
7616 win_T *win;
7617
7618 abort = mark_quickfix_ctx(&ql_info, copyID);
7619 if (abort)
7620 return abort;
7621
7622 FOR_ALL_TAB_WINDOWS(tp, win)
7623 {
7624 if (win->w_llist != NULL)
7625 {
7626 abort = mark_quickfix_ctx(win->w_llist, copyID);
7627 if (abort)
7628 return abort;
7629 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007630 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7631 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007632 // In a location list window and none of the other windows is
7633 // referring to this location list. Mark the location list
7634 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007635 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7636 if (abort)
7637 return abort;
7638 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007639 }
7640
7641 return abort;
7642}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007643#endif
7644
Bram Moolenaar81695252004-12-29 20:58:21 +00007645/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007646 * Return the autocmd name for the :cbuffer Ex commands
7647 */
7648 static char_u *
7649cbuffer_get_auname(cmdidx_T cmdidx)
7650{
7651 switch (cmdidx)
7652 {
7653 case CMD_cbuffer: return (char_u *)"cbuffer";
7654 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7655 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7656 case CMD_lbuffer: return (char_u *)"lbuffer";
7657 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7658 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7659 default: return NULL;
7660 }
7661}
7662
7663/*
7664 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7665 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7666 */
7667 static int
7668cbuffer_process_args(
7669 exarg_T *eap,
7670 buf_T **bufp,
7671 linenr_T *line1,
7672 linenr_T *line2)
7673{
7674 buf_T *buf = NULL;
7675
7676 if (*eap->arg == NUL)
7677 buf = curbuf;
7678 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7679 buf = buflist_findnr(atoi((char *)eap->arg));
7680
7681 if (buf == NULL)
7682 {
7683 emsg(_(e_invarg));
7684 return FAIL;
7685 }
7686
7687 if (buf->b_ml.ml_mfp == NULL)
7688 {
7689 emsg(_("E681: Buffer is not loaded"));
7690 return FAIL;
7691 }
7692
7693 if (eap->addr_count == 0)
7694 {
7695 eap->line1 = 1;
7696 eap->line2 = buf->b_ml.ml_line_count;
7697 }
7698
7699 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7700 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7701 {
7702 emsg(_(e_invrange));
7703 return FAIL;
7704 }
7705
7706 *line1 = eap->line1;
7707 *line2 = eap->line2;
7708 *bufp = buf;
7709
7710 return OK;
7711}
7712
7713/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007714 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007715 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007716 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007717 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007718 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007719 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007720 */
7721 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007722ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007723{
7724 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007725 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007726 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007727 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007728 int_u save_qfid;
7729 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007730 char_u *qf_title;
7731 linenr_T line1;
7732 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007733
Bram Moolenaara16123a2019-03-28 20:31:07 +01007734 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007735 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007736 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007737 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007738#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007739 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007740 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007741#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007742 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007744 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007745 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7746 if (qi == NULL)
7747 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007748
Bram Moolenaara16123a2019-03-28 20:31:07 +01007749 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7750 return;
7751
7752 qf_title = qf_cmdtitle(*eap->cmdlinep);
7753
7754 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007755 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007756 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7757 (char *)qf_title, (char *)buf->b_sfname);
7758 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007759 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007760
7761 incr_quickfix_busy();
7762
7763 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7764 (eap->cmdidx != CMD_caddbuffer
7765 && eap->cmdidx != CMD_laddbuffer),
7766 line1, line2,
7767 qf_title, NULL);
7768 if (qf_stack_empty(qi))
7769 {
7770 decr_quickfix_busy();
7771 return;
7772 }
7773 if (res >= 0)
7774 qf_list_changed(qf_get_curlist(qi));
7775
7776 // Remember the current quickfix list identifier, so that we can
7777 // check for autocommands changing the current quickfix list.
7778 save_qfid = qf_get_curlist(qi)->qf_id;
7779 if (au_name != NULL)
7780 {
7781 buf_T *curbuf_old = curbuf;
7782
7783 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7784 TRUE, curbuf);
7785 if (curbuf != curbuf_old)
7786 // Autocommands changed buffer, don't jump now, "qi" may
7787 // be invalid.
7788 res = 0;
7789 }
7790 // Jump to the first error for a new list and if autocmds didn't
7791 // free the list.
7792 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7793 eap->cmdidx == CMD_lbuffer)
7794 && qflist_valid(wp, save_qfid))
7795 // display the first error
7796 qf_jump_first(qi, save_qfid, eap->forceit);
7797
7798 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007799}
7800
Bram Moolenaar1e015462005-09-25 22:16:38 +00007801#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007802/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007803 * Return the autocmd name for the :cexpr Ex commands.
7804 */
7805 static char_u *
7806cexpr_get_auname(cmdidx_T cmdidx)
7807{
7808 switch (cmdidx)
7809 {
7810 case CMD_cexpr: return (char_u *)"cexpr";
7811 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7812 case CMD_caddexpr: return (char_u *)"caddexpr";
7813 case CMD_lexpr: return (char_u *)"lexpr";
7814 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7815 case CMD_laddexpr: return (char_u *)"laddexpr";
7816 default: return NULL;
7817 }
7818}
7819
7820/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007821 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7822 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007823 */
7824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007825ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007826{
7827 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007828 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007829 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007830 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007831 int_u save_qfid;
7832 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007833
Bram Moolenaara16123a2019-03-28 20:31:07 +01007834 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007835 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7836 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007837 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007838#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007839 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007840 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007841#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007842 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007843
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007844 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7845 if (qi == NULL)
7846 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007847
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007848 // Evaluate the expression. When the result is a string or a list we can
7849 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007850 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007851 if (tv != NULL)
7852 {
7853 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7854 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7855 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007856 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007857 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007858 (eap->cmdidx != CMD_caddexpr
7859 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007860 (linenr_T)0, (linenr_T)0,
7861 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007862 if (qf_stack_empty(qi))
7863 {
7864 decr_quickfix_busy();
7865 goto cleanup;
7866 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007867 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007868 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007869
7870 // Remember the current quickfix list identifier, so that we can
7871 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007872 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007873 if (au_name != NULL)
7874 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7875 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007876
7877 // Jump to the first error for a new list and if autocmds didn't
7878 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007879 if (res > 0 && (eap->cmdidx == CMD_cexpr
7880 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007881 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007882 // display the first error
7883 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007884 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007885 }
7886 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007887 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007888cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007889 free_tv(tv);
7890 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007891}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007892#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007893
7894/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007895 * Get the location list for ":lhelpgrep"
7896 */
7897 static qf_info_T *
7898hgr_get_ll(int *new_ll)
7899{
7900 win_T *wp;
7901 qf_info_T *qi;
7902
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007903 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007904 if (bt_help(curwin->w_buffer))
7905 wp = curwin;
7906 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007907 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007908 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007909
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007910 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007911 qi = NULL;
7912 else
7913 qi = wp->w_llist;
7914
7915 if (qi == NULL)
7916 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007917 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007918 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007919 return NULL;
7920 *new_ll = TRUE;
7921 }
7922
7923 return qi;
7924}
7925
7926/*
7927 * Search for a pattern in a help file.
7928 */
7929 static void
7930hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007931 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007932 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007933 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007934 regmatch_T *p_regmatch)
7935{
7936 FILE *fd;
7937 long lnum;
7938
7939 fd = mch_fopen((char *)fname, "r");
7940 if (fd == NULL)
7941 return;
7942
7943 lnum = 1;
7944 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7945 {
7946 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007947
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007948 // Convert a line if 'encoding' is not utf-8 and
7949 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007950 if (p_vc->vc_type != CONV_NONE
7951 && has_non_ascii(IObuff))
7952 {
7953 line = string_convert(p_vc, IObuff, NULL);
7954 if (line == NULL)
7955 line = IObuff;
7956 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007957
7958 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7959 {
7960 int l = (int)STRLEN(line);
7961
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007962 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007963 while (l > 0 && line[l - 1] <= ' ')
7964 line[--l] = NUL;
7965
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007966 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007967 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007968 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007969 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007970 0,
7971 line,
7972 lnum,
7973 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007974 + 1, // col
7975 FALSE, // vis_col
7976 NULL, // search pattern
7977 0, // nr
7978 1, // type
7979 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007980 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007981 {
7982 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007983 if (line != IObuff)
7984 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007985 break;
7986 }
7987 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007988 if (line != IObuff)
7989 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007990 ++lnum;
7991 line_breakcheck();
7992 }
7993 fclose(fd);
7994}
7995
7996/*
7997 * Search for a pattern in all the help files in the doc directory under
7998 * the given directory.
7999 */
8000 static void
8001hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008002 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008003 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008004 regmatch_T *p_regmatch,
8005 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008006#ifdef FEAT_MULTI_LANG
8007 , char_u *lang
8008#endif
8009 )
8010{
8011 int fcount;
8012 char_u **fnames;
8013 int fi;
8014
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008015 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008016 add_pathsep(dirname);
8017 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8018 if (gen_expand_wildcards(1, &dirname, &fcount,
8019 &fnames, EW_FILE|EW_SILENT) == OK
8020 && fcount > 0)
8021 {
8022 for (fi = 0; fi < fcount && !got_int; ++fi)
8023 {
8024#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008025 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008026 if (lang != NULL
8027 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008028 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008029 && !(STRNICMP(lang, "en", 2) == 0
8030 && STRNICMP("txt", fnames[fi]
8031 + STRLEN(fnames[fi]) - 3, 3) == 0))
8032 continue;
8033#endif
8034
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008035 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008036 }
8037 FreeWild(fcount, fnames);
8038 }
8039}
8040
8041/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008042 * Search for a pattern in all the help files in the 'runtimepath'
8043 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008044 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008045 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008046 */
8047 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008048hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008049{
8050 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008051
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008052 vimconv_T vc;
8053
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008054 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8055 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008056 vc.vc_type = CONV_NONE;
8057 if (!enc_utf8)
8058 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008059
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008060 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008061 p = p_rtp;
8062 while (*p != NUL && !got_int)
8063 {
8064 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8065
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008066 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008067#ifdef FEAT_MULTI_LANG
8068 , lang
8069#endif
8070 );
8071 }
8072
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008073 if (vc.vc_type != CONV_NONE)
8074 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008075}
8076
8077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 * ":helpgrep {pattern}"
8079 */
8080 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008081ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082{
8083 regmatch_T regmatch;
8084 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008085 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008086 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008087 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008088 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089
Bram Moolenaar73633f82012-01-20 13:39:07 +01008090 switch (eap->cmdidx)
8091 {
8092 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8093 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8094 default: break;
8095 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008096 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8097 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008098 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008099#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008100 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008101 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008102#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008103 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008104
Bram Moolenaar39665952018-08-15 20:59:48 +02008105 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008106 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008107 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008108 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008109 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008110 }
8111
Bram Moolenaara16123a2019-03-28 20:31:07 +01008112 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8113 save_cpo = p_cpo;
8114 p_cpo = empty_option;
8115
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008116 incr_quickfix_busy();
8117
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008118#ifdef FEAT_MULTI_LANG
8119 // Check for a specified language
8120 lang = check_help_lang(eap->arg);
8121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8123 regmatch.rm_ic = FALSE;
8124 if (regmatch.regprog != NULL)
8125 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008126 qf_list_T *qfl;
8127
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008128 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008129 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008130 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008132 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008133
Bram Moolenaar473de612013-06-08 18:19:48 +02008134 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008136 qfl->qf_nonevalid = FALSE;
8137 qfl->qf_ptr = qfl->qf_start;
8138 qfl->qf_index = 1;
8139 qf_list_changed(qfl);
8140 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008143 if (p_cpo == empty_option)
8144 p_cpo = save_cpo;
8145 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008146 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008147 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148
Bram Moolenaar73633f82012-01-20 13:39:07 +01008149 if (au_name != NULL)
8150 {
8151 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8152 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008153 // When adding a location list to an existing location list stack,
8154 // if the autocmd made the stack invalid, then just return.
8155 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008156 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008157 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008158 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008159 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008160 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008161
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008162 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008163 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008164 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008165 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008166 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008167
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008168 decr_quickfix_busy();
8169
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008170 if (eap->cmdidx == CMD_lhelpgrep)
8171 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008172 // If the help window is not opened or if it already points to the
8173 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008174 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008175 {
8176 if (new_qi)
8177 ll_free_all(&qi);
8178 }
8179 else if (curwin->w_llist == NULL)
8180 curwin->w_llist = qi;
8181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008183#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008184
8185#if defined(FEAT_EVAL) || defined(PROTO)
8186# ifdef FEAT_QUICKFIX
8187 static void
8188get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8189{
8190 if (what_arg->v_type == VAR_UNKNOWN)
8191 {
8192 if (rettv_list_alloc(rettv) == OK)
8193 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008194 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008195 }
8196 else
8197 {
8198 if (rettv_dict_alloc(rettv) == OK)
8199 if (is_qf || (wp != NULL))
8200 {
8201 if (what_arg->v_type == VAR_DICT)
8202 {
8203 dict_T *d = what_arg->vval.v_dict;
8204
8205 if (d != NULL)
8206 qf_get_properties(wp, d, rettv->vval.v_dict);
8207 }
8208 else
8209 emsg(_(e_dictreq));
8210 }
8211 }
8212}
8213# endif
8214
8215/*
8216 * "getloclist()" function
8217 */
8218 void
8219f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8220{
8221# ifdef FEAT_QUICKFIX
8222 win_T *wp;
8223
8224 wp = find_win_by_nr_or_id(&argvars[0]);
8225 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8226# endif
8227}
8228
8229/*
8230 * "getqflist()" function
8231 */
8232 void
8233f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8234{
8235# ifdef FEAT_QUICKFIX
8236 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8237# endif
8238}
8239
8240/*
8241 * Used by "setqflist()" and "setloclist()" functions
8242 */
8243 static void
8244set_qf_ll_list(
8245 win_T *wp UNUSED,
8246 typval_T *list_arg UNUSED,
8247 typval_T *action_arg UNUSED,
8248 typval_T *what_arg UNUSED,
8249 typval_T *rettv)
8250{
8251# ifdef FEAT_QUICKFIX
8252 static char *e_invact = N_("E927: Invalid action: '%s'");
8253 char_u *act;
8254 int action = 0;
8255 static int recursive = 0;
8256# endif
8257
8258 rettv->vval.v_number = -1;
8259
8260# ifdef FEAT_QUICKFIX
8261 if (list_arg->v_type != VAR_LIST)
8262 emsg(_(e_listreq));
8263 else if (recursive != 0)
8264 emsg(_(e_au_recursive));
8265 else
8266 {
8267 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008268 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008269 int valid_dict = TRUE;
8270
8271 if (action_arg->v_type == VAR_STRING)
8272 {
8273 act = tv_get_string_chk(action_arg);
8274 if (act == NULL)
8275 return; // type error; errmsg already given
8276 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8277 act[1] == NUL)
8278 action = *act;
8279 else
8280 semsg(_(e_invact), act);
8281 }
8282 else if (action_arg->v_type == VAR_UNKNOWN)
8283 action = ' ';
8284 else
8285 emsg(_(e_stringreq));
8286
8287 if (action_arg->v_type != VAR_UNKNOWN
8288 && what_arg->v_type != VAR_UNKNOWN)
8289 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008290 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8291 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008292 else
8293 {
8294 emsg(_(e_dictreq));
8295 valid_dict = FALSE;
8296 }
8297 }
8298
8299 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008300 if (l != NULL && action && valid_dict
8301 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008302 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008303 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008304 rettv->vval.v_number = 0;
8305 --recursive;
8306 }
8307# endif
8308}
8309
8310/*
8311 * "setloclist()" function
8312 */
8313 void
8314f_setloclist(typval_T *argvars, typval_T *rettv)
8315{
8316 win_T *win;
8317
8318 rettv->vval.v_number = -1;
8319
8320 win = find_win_by_nr_or_id(&argvars[0]);
8321 if (win != NULL)
8322 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8323}
8324
8325/*
8326 * "setqflist()" function
8327 */
8328 void
8329f_setqflist(typval_T *argvars, typval_T *rettv)
8330{
8331 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8332}
8333#endif