blob: 510d8ddcb259df71d502464959c87c52d6affcb7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
55 qfline_T *qf_ptr; /* pointer to the current error */
56 int qf_count; /* number of errors (0 means no error list) */
57 int qf_index; /* current index in the error list */
58 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059 char_u *qf_title; /* title derived from the command that created
60 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
75};
76
77static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar68b76a62005-03-25 21:53:48 +000079#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * Structure used to hold the info of one part of 'errorformat'
83 */
Bram Moolenaar01265852006-03-20 21:50:15 +000084typedef struct efm_S efm_T;
85struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000088 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
90 char_u prefix; /* prefix of this format line: */
91 /* 'D' enter directory */
92 /* 'X' leave directory */
93 /* 'A' start of multi-line message */
94 /* 'E' error message */
95 /* 'W' warning message */
96 /* 'I' informational message */
97 /* 'C' continuation line */
98 /* 'Z' end of multi-line message */
99 /* 'G' general, unspecific message */
100 /* 'P' push file (partial) message */
101 /* 'Q' pop/quit file (partial) message */
102 /* 'O' overread (partial) message */
103 char_u flags; /* additional flags given in prefix */
104 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000105 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000106 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107};
108
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100109static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
110static void qf_store_title(qf_info_T *qi, char_u *title);
111static void qf_new_list(qf_info_T *qi, char_u *qf_title);
112static void ll_free_all(qf_info_T **pqi);
113static int qf_add_entry(qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
114static qf_info_T *ll_new_list(void);
115static void qf_msg(qf_info_T *qi);
116static void qf_free(qf_info_T *qi, int idx);
117static char_u *qf_types(int, int);
118static int qf_get_fnum(char_u *, char_u *);
119static char_u *qf_push_dir(char_u *, struct dir_stack_T **);
120static char_u *qf_pop_dir(struct dir_stack_T **);
121static char_u *qf_guess_filepath(char_u *);
122static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
123static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100125static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
126static int is_qf_win(win_T *win, qf_info_T *qi);
127static win_T *qf_find_win(qf_info_T *qi);
128static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200129static void qf_update_buffer(qf_info_T *qi, int update_cursor);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void qf_set_title_var(qf_info_T *qi);
131static void qf_fill_buffer(qf_info_T *qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static char_u *get_mef_name(void);
134static void restore_start_dir(char_u *dirname_start);
135static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
136static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
137static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
138static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000140/* Quickfix window check helper macro */
141#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
142/* Location list window check helper macro */
143#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
144/*
145 * Return location list for window 'wp'
146 * For location list window, return the referenced location list
147 */
148#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000151 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200152 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 * Return -1 for error, number of errors for success.
154 */
155 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100156qf_init(
157 win_T *wp,
158 char_u *efile,
159 char_u *errorformat,
160 int newlist, /* TRUE: start a new error list */
161 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163 qf_info_T *qi = &ql_info;
164
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000165 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166 {
167 qi = ll_get_or_alloc_list(wp);
168 if (qi == NULL)
169 return FAIL;
170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171
172 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200173 (linenr_T)0, (linenr_T)0,
174 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000175}
176
177/*
178 * Read the errorfile "efile" into memory, line by line, building the error
179 * list.
180 * Alternative: when "efile" is null read errors from buffer "buf".
181 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200182 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
183 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000184 * Return -1 for error, number of errors for success.
185 */
186 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100187qf_init_ext(
188 qf_info_T *qi,
189 char_u *efile,
190 buf_T *buf,
191 typval_T *tv,
192 char_u *errorformat,
193 int newlist, /* TRUE: start a new error list */
194 linenr_T lnumfirst, /* first line number to use */
195 linenr_T lnumlast, /* last line number to use */
196 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000197{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 char_u *namebuf;
199 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000200 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 char_u *fmtstr = NULL;
202 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000203 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 int type = 0;
205 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000206 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 long lnum = 0L;
208 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000209 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000210 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000212 efm_T *fmt_first = NULL;
213 efm_T *fmt_last = NULL;
214 efm_T *fmt_ptr;
215 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 char_u *efm;
217 char_u *ptr;
218 char_u *srcptr;
219 int len;
220 int i;
221 int round;
222 int idx = 0;
223 int multiline = FALSE;
224 int multiignore = FALSE;
225 int multiscan = FALSE;
226 int retval = -1; /* default: return error flag */
227 char_u *directory = NULL;
228 char_u *currfile = NULL;
229 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000230 char_u *p_str = NULL;
231 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 struct dir_stack_T *file_stack = NULL;
233 regmatch_T regmatch;
234 static struct fmtpattern
235 {
236 char_u convchar;
237 char *pattern;
238 } fmt_pat[FMT_PATTERNS] =
239 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000240 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {'n', "\\d\\+"},
242 {'l', "\\d\\+"},
243 {'c', "\\d\\+"},
244 {'t', "."},
245 {'m', ".\\+"},
246 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200247 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000248 {'v', "\\d\\+"},
249 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 };
251
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100252 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
253 errmsg = alloc_id(CMDBUFFSIZE + 1, aid_qf_errmsg);
254 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000255 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 goto qf_init_end;
257
Bram Moolenaar86b68352004-12-27 21:59:20 +0000258 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 {
260 EMSG2(_(e_openerrf), efile);
261 goto qf_init_end;
262 }
263
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000264 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100266 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000267 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000269 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200270 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 ;
272
273/*
274 * Each part of the format string is copied and modified from errorformat to
275 * regex prog. Only a few % characters are allowed.
276 */
277 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000278 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000279 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 else
281 efm = errorformat;
282 /*
283 * Get some space to modify the format string into.
284 */
285 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
286 for (round = FMT_PATTERNS; round > 0; )
287 i += (int)STRLEN(fmt_pat[--round].pattern);
288#ifdef COLON_IN_FILENAME
289 i += 12; /* "%f" can become twelve chars longer */
290#else
291 i += 2; /* "%f" can become two chars longer */
292#endif
293 if ((fmtstr = alloc(i)) == NULL)
294 goto error2;
295
Bram Moolenaar01265852006-03-20 21:50:15 +0000296 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 {
298 /*
299 * Allocate a new eformat structure and put it at the end of the list
300 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000301 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 if (fmt_ptr == NULL)
303 goto error2;
304 if (fmt_first == NULL) /* first one */
305 fmt_first = fmt_ptr;
306 else
307 fmt_last->next = fmt_ptr;
308 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
310 /*
311 * Isolate one part in the 'errorformat' option
312 */
313 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
314 if (efm[len] == '\\' && efm[len + 1] != NUL)
315 ++len;
316
317 /*
318 * Build regexp pattern from current 'errorformat' option
319 */
320 ptr = fmtstr;
321 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000322 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 for (efmp = efm; efmp < efm + len; ++efmp)
324 {
325 if (*efmp == '%')
326 {
327 ++efmp;
328 for (idx = 0; idx < FMT_PATTERNS; ++idx)
329 if (fmt_pat[idx].convchar == *efmp)
330 break;
331 if (idx < FMT_PATTERNS)
332 {
333 if (fmt_ptr->addr[idx])
334 {
335 sprintf((char *)errmsg,
336 _("E372: Too many %%%c in format string"), *efmp);
337 EMSG(errmsg);
338 goto error2;
339 }
340 if ((idx
341 && idx < 6
342 && vim_strchr((char_u *)"DXOPQ",
343 fmt_ptr->prefix) != NULL)
344 || (idx == 6
345 && vim_strchr((char_u *)"OPQ",
346 fmt_ptr->prefix) == NULL))
347 {
348 sprintf((char *)errmsg,
349 _("E373: Unexpected %%%c in format string"), *efmp);
350 EMSG(errmsg);
351 goto error2;
352 }
353 fmt_ptr->addr[idx] = (char_u)++round;
354 *ptr++ = '\\';
355 *ptr++ = '(';
356#ifdef BACKSLASH_IN_FILENAME
357 if (*efmp == 'f')
358 {
359 /* Also match "c:" in the file name, even when
360 * checking for a colon next: "%f:".
361 * "\%(\a:\)\=" */
362 STRCPY(ptr, "\\%(\\a:\\)\\=");
363 ptr += 10;
364 }
365#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000366 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000368 if (efmp[1] != '\\' && efmp[1] != '%')
369 {
370 /* A file name may contain spaces, but this isn't
371 * in "\f". For "%f:%l:%m" there may be a ":" in
372 * the file name. Use ".\{-1,}x" instead (x is
373 * the next character), the requirement that :999:
374 * follows should work. */
375 STRCPY(ptr, ".\\{-1,}");
376 ptr += 7;
377 }
378 else
379 {
380 /* File name followed by '\\' or '%': include as
381 * many file name chars as possible. */
382 STRCPY(ptr, "\\f\\+");
383 ptr += 4;
384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 }
386 else
387 {
388 srcptr = (char_u *)fmt_pat[idx].pattern;
389 while ((*ptr = *srcptr++) != NUL)
390 ++ptr;
391 }
392 *ptr++ = '\\';
393 *ptr++ = ')';
394 }
395 else if (*efmp == '*')
396 {
397 if (*++efmp == '[' || *efmp == '\\')
398 {
399 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
400 {
401 if (efmp[1] == '^')
402 *ptr++ = *++efmp;
403 if (efmp < efm + len)
404 {
405 *ptr++ = *++efmp; /* could be ']' */
406 while (efmp < efm + len
407 && (*ptr++ = *++efmp) != ']')
408 /* skip */;
409 if (efmp == efm + len)
410 {
411 EMSG(_("E374: Missing ] in format string"));
412 goto error2;
413 }
414 }
415 }
416 else if (efmp < efm + len) /* %*\D, %*\s etc. */
417 *ptr++ = *++efmp;
418 *ptr++ = '\\';
419 *ptr++ = '+';
420 }
421 else
422 {
423 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
424 sprintf((char *)errmsg,
425 _("E375: Unsupported %%%c in format string"), *efmp);
426 EMSG(errmsg);
427 goto error2;
428 }
429 }
430 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
431 *ptr++ = *efmp; /* regexp magic characters */
432 else if (*efmp == '#')
433 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000434 else if (*efmp == '>')
435 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 else if (efmp == efm + 1) /* analyse prefix */
437 {
438 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
439 fmt_ptr->flags = *efmp++;
440 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
441 fmt_ptr->prefix = *efmp;
442 else
443 {
444 sprintf((char *)errmsg,
445 _("E376: Invalid %%%c in format string prefix"), *efmp);
446 EMSG(errmsg);
447 goto error2;
448 }
449 }
450 else
451 {
452 sprintf((char *)errmsg,
453 _("E377: Invalid %%%c in format string"), *efmp);
454 EMSG(errmsg);
455 goto error2;
456 }
457 }
458 else /* copy normal character */
459 {
460 if (*efmp == '\\' && efmp + 1 < efm + len)
461 ++efmp;
462 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
463 *ptr++ = '\\'; /* escape regexp atoms */
464 if (*efmp)
465 *ptr++ = *efmp;
466 }
467 }
468 *ptr++ = '$';
469 *ptr = NUL;
470 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
471 goto error2;
472 /*
473 * Advance to next part
474 */
475 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
476 }
477 if (fmt_first == NULL) /* nothing found */
478 {
479 EMSG(_("E378: 'errorformat' contains no pattern"));
480 goto error2;
481 }
482
483 /*
484 * got_int is reset here, because it was probably set when killing the
485 * ":make" command, but we still want to read the errorfile then.
486 */
487 got_int = FALSE;
488
489 /* Always ignore case when looking for a matching error. */
490 regmatch.rm_ic = TRUE;
491
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000492 if (tv != NULL)
493 {
494 if (tv->v_type == VAR_STRING)
495 p_str = tv->vval.v_string;
496 else if (tv->v_type == VAR_LIST)
497 p_li = tv->vval.v_list->lv_first;
498 }
499
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 /*
501 * Read the lines in the error file one by one.
502 * Try to recognize one of the error formats in each line.
503 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000504 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000506 /* Get the next line. */
507 if (fd == NULL)
508 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000509 if (tv != NULL)
510 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000511 if (tv->v_type == VAR_STRING)
512 {
513 /* Get the next line from the supplied string */
514 char_u *p;
515
516 if (!*p_str) /* Reached the end of the string */
517 break;
518
519 p = vim_strchr(p_str, '\n');
520 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000521 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000522 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000523 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000524
525 if (len > CMDBUFFSIZE - 2)
526 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
527 else
528 vim_strncpy(IObuff, p_str, len);
529
530 p_str += len;
531 }
532 else if (tv->v_type == VAR_LIST)
533 {
534 /* Get the next line from the supplied list */
Bram Moolenaar89c64d52016-03-27 18:44:40 +0200535 while (p_li && (p_li->li_tv.v_type != VAR_STRING
536 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000537 p_li = p_li->li_next; /* Skip non-string items */
538
539 if (!p_li) /* End of the list */
540 break;
541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000542 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000543 if (len > CMDBUFFSIZE - 2)
544 len = CMDBUFFSIZE - 2;
545
546 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
547
548 p_li = p_li->li_next; /* next item */
549 }
550 }
551 else
552 {
553 /* Get the next line from the supplied buffer */
554 if (buflnum > lnumlast)
555 break;
556 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
557 CMDBUFFSIZE - 2);
558 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000559 }
560 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
561 break;
562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200564#ifdef FEAT_MBYTE
565 remove_bom(IObuff);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
569 *efmp = NUL;
570#ifdef USE_CRNL
571 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
572 *efmp = NUL;
573#endif
574
Bram Moolenaar01265852006-03-20 21:50:15 +0000575 /* If there was no %> item start at the first pattern */
576 if (fmt_start == NULL)
577 fmt_ptr = fmt_first;
578 else
579 {
580 fmt_ptr = fmt_start;
581 fmt_start = NULL;
582 }
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 /*
585 * Try to match each part of 'errorformat' until we find a complete
586 * match or no match.
587 */
588 valid = TRUE;
589restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000590 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100592 int r;
593
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 idx = fmt_ptr->prefix;
595 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
596 continue;
597 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000598 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!multiscan)
600 errmsg[0] = NUL;
601 lnum = 0;
602 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000603 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 enr = -1;
605 type = 0;
606 tail = NULL;
607
608 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100609 r = vim_regexec(&regmatch, IObuff, (colnr_T)0);
610 fmt_ptr->prog = regmatch.regprog;
611 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
613 if ((idx == 'C' || idx == 'Z') && !multiline)
614 continue;
615 if (vim_strchr((char_u *)"EWI", idx) != NULL)
616 type = idx;
617 else
618 type = 0;
619 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000620 * Extract error message data from matched line.
621 * We check for an actual submatch, because "\[" and "\]" in
622 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 */
624 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
625 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000626 int c;
627
628 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
629 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000630
631 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000632 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000633 *regmatch.endp[i] = NUL;
634 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
635 *regmatch.endp[i] = c;
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000638 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 continue;
640 }
641 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000642 {
643 if (regmatch.startp[i] == NULL)
644 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000648 {
649 if (regmatch.startp[i] == NULL)
650 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000654 {
655 if (regmatch.startp[i] == NULL)
656 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000660 {
661 if (regmatch.startp[i] == NULL)
662 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000664 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000665 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 STRCPY(errmsg, IObuff);
667 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
668 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000669 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
670 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000672 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
674 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000675 {
676 if (regmatch.startp[i] == NULL)
677 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
681 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200682 char_u *match_ptr;
683
Bram Moolenaar4169da72006-06-20 18:49:32 +0000684 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
685 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200686 col = 0;
687 for (match_ptr = regmatch.startp[i];
688 match_ptr != regmatch.endp[i]; ++match_ptr)
689 {
690 ++col;
691 if (*match_ptr == TAB)
692 {
693 col += 7;
694 col -= col % 8;
695 }
696 }
697 ++col;
698 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
700 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
701 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000702 if (regmatch.startp[i] == NULL)
703 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000705 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000707 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
708 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000709 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
710 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000711 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
712 if (len > CMDBUFFSIZE - 5)
713 len = CMDBUFFSIZE - 5;
714 STRCPY(pattern, "^\\V");
715 STRNCAT(pattern, regmatch.startp[i], len);
716 pattern[len + 3] = '\\';
717 pattern[len + 4] = '$';
718 pattern[len + 5] = NUL;
719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 break;
721 }
722 }
723 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000724
Bram Moolenaar4770d092006-01-12 23:22:24 +0000725 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000727 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {
729 if (idx == 'D') /* enter directory */
730 {
731 if (*namebuf == NUL)
732 {
733 EMSG(_("E379: Missing or empty directory name"));
734 goto error2;
735 }
736 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
737 goto error2;
738 }
739 else if (idx == 'X') /* leave directory */
740 directory = qf_pop_dir(&dir_stack);
741 }
742 namebuf[0] = NUL; /* no match found, remove file name */
743 lnum = 0; /* don't jump to this line */
744 valid = FALSE;
745 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000746 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 multiline = multiignore = FALSE;
748 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000749 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000751 /* honor %> item */
752 if (fmt_ptr->conthere)
753 fmt_start = fmt_ptr;
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100758 multiignore = FALSE; /* reset continuation */
759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
761 { /* continuation of multi-line msg */
762 if (qfprev == NULL)
763 goto error2;
764 if (*errmsg && !multiignore)
765 {
766 len = (int)STRLEN(qfprev->qf_text);
767 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
768 == NULL)
769 goto error2;
770 STRCPY(ptr, qfprev->qf_text);
771 vim_free(qfprev->qf_text);
772 qfprev->qf_text = ptr;
773 *(ptr += len) = '\n';
774 STRCPY(++ptr, errmsg);
775 }
776 if (qfprev->qf_nr == -1)
777 qfprev->qf_nr = enr;
778 if (vim_isprintc(type) && !qfprev->qf_type)
779 qfprev->qf_type = type; /* only printable chars allowed */
780 if (!qfprev->qf_lnum)
781 qfprev->qf_lnum = lnum;
782 if (!qfprev->qf_col)
783 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (!qfprev->qf_fnum)
786 qfprev->qf_fnum = qf_get_fnum(directory,
787 *namebuf || directory ? namebuf
788 : currfile && valid ? currfile : 0);
789 if (idx == 'Z')
790 multiline = multiignore = FALSE;
791 line_breakcheck();
792 continue;
793 }
794 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
795 {
796 /* global file names */
797 valid = FALSE;
798 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
799 {
800 if (*namebuf && idx == 'P')
801 currfile = qf_push_dir(namebuf, &file_stack);
802 else if (idx == 'Q')
803 currfile = qf_pop_dir(&file_stack);
804 *namebuf = NUL;
805 if (tail && *tail)
806 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000807 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 multiscan = TRUE;
809 goto restofline;
810 }
811 }
812 }
813 if (fmt_ptr->flags == '-') /* generally exclude this line */
814 {
815 if (multiline)
816 multiignore = TRUE; /* also exclude continuation lines */
817 continue;
818 }
819 }
820
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000821 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000823 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000825 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000826 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 errmsg,
828 lnum,
829 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000831 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 enr,
833 type,
834 valid) == FAIL)
835 goto error2;
836 line_breakcheck();
837 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000838 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000840 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000842 /* no valid entry found */
843 qi->qf_lists[qi->qf_curlist].qf_ptr =
844 qi->qf_lists[qi->qf_curlist].qf_start;
845 qi->qf_lists[qi->qf_curlist].qf_index = 1;
846 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 else
849 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000850 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
851 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
852 qi->qf_lists[qi->qf_curlist].qf_ptr =
853 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000855 /* return number of matches */
856 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 goto qf_init_ok;
858 }
859 EMSG(_(e_readerrf));
860error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000861 qf_free(qi, qi->qf_curlist);
862 qi->qf_listcount--;
863 if (qi->qf_curlist > 0)
864 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000866 if (fd != NULL)
867 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
869 {
870 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +0200871 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 vim_free(fmt_ptr);
873 }
874 qf_clean_dir_stack(&dir_stack);
875 qf_clean_dir_stack(&file_stack);
876qf_init_end:
877 vim_free(namebuf);
878 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000879 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 vim_free(fmtstr);
881
882#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200883 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#endif
885
886 return retval;
887}
888
Bram Moolenaarfb604092014-07-23 15:55:00 +0200889 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100890qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +0200891{
892 if (title != NULL)
893 {
894 char_u *p = alloc((int)STRLEN(title) + 2);
895
896 qi->qf_lists[qi->qf_curlist].qf_title = p;
897 if (p != NULL)
898 sprintf((char *)p, ":%s", (char *)title);
899 }
900}
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902/*
903 * Prepare for adding a new quickfix list.
904 */
905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100906qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
908 int i;
909
910 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +0200911 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 * the current entry. This makes it possible to browse in a tree-like
913 * way with ":grep'.
914 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000915 while (qi->qf_listcount > qi->qf_curlist + 1)
916 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 /*
919 * When the stack is full, remove to oldest entry
920 * Otherwise, add a new entry.
921 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000922 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000924 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000926 qi->qf_lists[i - 1] = qi->qf_lists[i];
927 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 }
929 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000930 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100931 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +0200932 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933}
934
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000935/*
936 * Free a location list
937 */
938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100939ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000940{
941 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000942 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000943
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000944 qi = *pqi;
945 if (qi == NULL)
946 return;
947 *pqi = NULL; /* Remove reference to this list */
948
949 qi->qf_refcount--;
950 if (qi->qf_refcount < 1)
951 {
952 /* No references to this location list */
953 for (i = 0; i < qi->qf_listcount; ++i)
954 qf_free(qi, i);
955 vim_free(qi);
956 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000957}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000958
959 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100960qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000961{
962 int i;
963 qf_info_T *qi = &ql_info;
964
965 if (wp != NULL)
966 {
967 /* location list */
968 ll_free_all(&wp->w_llist);
969 ll_free_all(&wp->w_llist_ref);
970 }
971 else
972 /* quickfix list */
973 for (i = 0; i < qi->qf_listcount; ++i)
974 qf_free(qi, i);
975}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977/*
978 * Add an entry to the end of the list of errors.
979 * Returns OK or FAIL.
980 */
981 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100982qf_add_entry(
983 qf_info_T *qi, /* quickfix list */
984 qfline_T **prevp, /* pointer to previously added entry or NULL */
985 char_u *dir, /* optional directory name */
986 char_u *fname, /* file name or NULL */
987 int bufnum, /* buffer number or zero */
988 char_u *mesg, /* message */
989 long lnum, /* line number */
990 int col, /* column */
991 int vis_col, /* using visual column */
992 char_u *pattern, /* search pattern */
993 int nr, /* error number */
994 int type, /* type character */
995 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000997 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000999 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001001 if (bufnum != 0)
1002 qfp->qf_fnum = bufnum;
1003 else
1004 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1006 {
1007 vim_free(qfp);
1008 return FAIL;
1009 }
1010 qfp->qf_lnum = lnum;
1011 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001012 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001013 if (pattern == NULL || *pattern == NUL)
1014 qfp->qf_pattern = NULL;
1015 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1016 {
1017 vim_free(qfp->qf_text);
1018 vim_free(qfp);
1019 return FAIL;
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 qfp->qf_nr = nr;
1022 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1023 type = 0;
1024 qfp->qf_type = type;
1025 qfp->qf_valid = valid;
1026
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001027 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1028 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001030 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001031 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1032 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 qfp->qf_prev = qfp; /* first element points to itself */
1034 }
1035 else
1036 {
1037 qfp->qf_prev = *prevp;
1038 (*prevp)->qf_next = qfp;
1039 }
1040 qfp->qf_next = qfp; /* last element points to itself */
1041 qfp->qf_cleared = FALSE;
1042 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001043 ++qi->qf_lists[qi->qf_curlist].qf_count;
1044 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1045 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001047 qi->qf_lists[qi->qf_curlist].qf_index =
1048 qi->qf_lists[qi->qf_curlist].qf_count;
1049 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 }
1051
1052 return OK;
1053}
1054
1055/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001056 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001057 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001058 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001059ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001060{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001061 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001062
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001063 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1064 if (qi != NULL)
1065 {
1066 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1067 qi->qf_refcount++;
1068 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001069
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001070 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001071}
1072
1073/*
1074 * Return the location list for window 'wp'.
1075 * If not present, allocate a location list
1076 */
1077 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001078ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001079{
1080 if (IS_LL_WINDOW(wp))
1081 /* For a location list window, use the referenced location list */
1082 return wp->w_llist_ref;
1083
1084 /*
1085 * For a non-location list window, w_llist_ref should not point to a
1086 * location list.
1087 */
1088 ll_free_all(&wp->w_llist_ref);
1089
1090 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001091 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001092 return wp->w_llist;
1093}
1094
1095/*
1096 * Copy the location list from window "from" to window "to".
1097 */
1098 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001099copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001100{
1101 qf_info_T *qi;
1102 int idx;
1103 int i;
1104
1105 /*
1106 * When copying from a location list window, copy the referenced
1107 * location list. For other windows, copy the location list for
1108 * that window.
1109 */
1110 if (IS_LL_WINDOW(from))
1111 qi = from->w_llist_ref;
1112 else
1113 qi = from->w_llist;
1114
1115 if (qi == NULL) /* no location list to copy */
1116 return;
1117
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001118 /* allocate a new location list */
1119 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001120 return;
1121
1122 to->w_llist->qf_listcount = qi->qf_listcount;
1123
1124 /* Copy the location lists one at a time */
1125 for (idx = 0; idx < qi->qf_listcount; idx++)
1126 {
1127 qf_list_T *from_qfl;
1128 qf_list_T *to_qfl;
1129
1130 to->w_llist->qf_curlist = idx;
1131
1132 from_qfl = &qi->qf_lists[idx];
1133 to_qfl = &to->w_llist->qf_lists[idx];
1134
1135 /* Some of the fields are populated by qf_add_entry() */
1136 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1137 to_qfl->qf_count = 0;
1138 to_qfl->qf_index = 0;
1139 to_qfl->qf_start = NULL;
1140 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001141 if (from_qfl->qf_title != NULL)
1142 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1143 else
1144 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001145
1146 if (from_qfl->qf_count)
1147 {
1148 qfline_T *from_qfp;
1149 qfline_T *prevp = NULL;
1150
1151 /* copy all the location entries in this list */
1152 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1153 ++i, from_qfp = from_qfp->qf_next)
1154 {
1155 if (qf_add_entry(to->w_llist, &prevp,
1156 NULL,
1157 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001158 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001159 from_qfp->qf_text,
1160 from_qfp->qf_lnum,
1161 from_qfp->qf_col,
1162 from_qfp->qf_viscol,
1163 from_qfp->qf_pattern,
1164 from_qfp->qf_nr,
1165 0,
1166 from_qfp->qf_valid) == FAIL)
1167 {
1168 qf_free_all(to);
1169 return;
1170 }
1171 /*
1172 * qf_add_entry() will not set the qf_num field, as the
1173 * directory and file names are not supplied. So the qf_fnum
1174 * field is copied here.
1175 */
1176 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1177 prevp->qf_type = from_qfp->qf_type; /* error type */
1178 if (from_qfl->qf_ptr == from_qfp)
1179 to_qfl->qf_ptr = prevp; /* current location */
1180 }
1181 }
1182
1183 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1184
1185 /* When no valid entries are present in the list, qf_ptr points to
1186 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001187 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001188 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001189 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001190 to_qfl->qf_index = 1;
1191 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001192 }
1193
1194 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1195}
1196
1197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 * get buffer number for file "dir.name"
1199 */
1200 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001201qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
1203 if (fname == NULL || *fname == NUL) /* no file name */
1204 return 0;
1205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 char_u *ptr;
1207 int fnum;
1208
Bram Moolenaare60acc12011-05-10 16:41:25 +02001209#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001211#endif
1212#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (directory != NULL)
1214 slash_adjust(directory);
1215 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (directory != NULL && !vim_isAbsName(fname)
1218 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1219 {
1220 /*
1221 * Here we check if the file really exists.
1222 * This should normally be true, but if make works without
1223 * "leaving directory"-messages we might have missed a
1224 * directory change.
1225 */
1226 if (mch_getperm(ptr) < 0)
1227 {
1228 vim_free(ptr);
1229 directory = qf_guess_filepath(fname);
1230 if (directory)
1231 ptr = concat_fnames(directory, fname, TRUE);
1232 else
1233 ptr = vim_strsave(fname);
1234 }
1235 /* Use concatenated directory name and file name */
1236 fnum = buflist_add(ptr, 0);
1237 vim_free(ptr);
1238 return fnum;
1239 }
1240 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242}
1243
1244/*
1245 * push dirbuf onto the directory stack and return pointer to actual dir or
1246 * NULL on error
1247 */
1248 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001249qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250{
1251 struct dir_stack_T *ds_new;
1252 struct dir_stack_T *ds_ptr;
1253
1254 /* allocate new stack element and hook it in */
1255 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1256 if (ds_new == NULL)
1257 return NULL;
1258
1259 ds_new->next = *stackptr;
1260 *stackptr = ds_new;
1261
1262 /* store directory on the stack */
1263 if (vim_isAbsName(dirbuf)
1264 || (*stackptr)->next == NULL
1265 || (*stackptr && dir_stack != *stackptr))
1266 (*stackptr)->dirname = vim_strsave(dirbuf);
1267 else
1268 {
1269 /* Okay we don't have an absolute path.
1270 * dirbuf must be a subdir of one of the directories on the stack.
1271 * Let's search...
1272 */
1273 ds_new = (*stackptr)->next;
1274 (*stackptr)->dirname = NULL;
1275 while (ds_new)
1276 {
1277 vim_free((*stackptr)->dirname);
1278 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1279 TRUE);
1280 if (mch_isdir((*stackptr)->dirname) == TRUE)
1281 break;
1282
1283 ds_new = ds_new->next;
1284 }
1285
1286 /* clean up all dirs we already left */
1287 while ((*stackptr)->next != ds_new)
1288 {
1289 ds_ptr = (*stackptr)->next;
1290 (*stackptr)->next = (*stackptr)->next->next;
1291 vim_free(ds_ptr->dirname);
1292 vim_free(ds_ptr);
1293 }
1294
1295 /* Nothing found -> it must be on top level */
1296 if (ds_new == NULL)
1297 {
1298 vim_free((*stackptr)->dirname);
1299 (*stackptr)->dirname = vim_strsave(dirbuf);
1300 }
1301 }
1302
1303 if ((*stackptr)->dirname != NULL)
1304 return (*stackptr)->dirname;
1305 else
1306 {
1307 ds_ptr = *stackptr;
1308 *stackptr = (*stackptr)->next;
1309 vim_free(ds_ptr);
1310 return NULL;
1311 }
1312}
1313
1314
1315/*
1316 * pop dirbuf from the directory stack and return previous directory or NULL if
1317 * stack is empty
1318 */
1319 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001320qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 struct dir_stack_T *ds_ptr;
1323
1324 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1325 * What to do if it isn't? */
1326
1327 /* pop top element and free it */
1328 if (*stackptr != NULL)
1329 {
1330 ds_ptr = *stackptr;
1331 *stackptr = (*stackptr)->next;
1332 vim_free(ds_ptr->dirname);
1333 vim_free(ds_ptr);
1334 }
1335
1336 /* return NEW top element as current dir or NULL if stack is empty*/
1337 return *stackptr ? (*stackptr)->dirname : NULL;
1338}
1339
1340/*
1341 * clean up directory stack
1342 */
1343 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001344qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
1346 struct dir_stack_T *ds_ptr;
1347
1348 while ((ds_ptr = *stackptr) != NULL)
1349 {
1350 *stackptr = (*stackptr)->next;
1351 vim_free(ds_ptr->dirname);
1352 vim_free(ds_ptr);
1353 }
1354}
1355
1356/*
1357 * Check in which directory of the directory stack the given file can be
1358 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001359 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 * Cleans up intermediate directory entries.
1361 *
1362 * TODO: How to solve the following problem?
1363 * If we have the this directory tree:
1364 * ./
1365 * ./aa
1366 * ./aa/bb
1367 * ./bb
1368 * ./bb/x.c
1369 * and make says:
1370 * making all in aa
1371 * making all in bb
1372 * x.c:9: Error
1373 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1374 * qf_guess_filepath will return NULL.
1375 */
1376 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001377qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
1379 struct dir_stack_T *ds_ptr;
1380 struct dir_stack_T *ds_tmp;
1381 char_u *fullname;
1382
1383 /* no dirs on the stack - there's nothing we can do */
1384 if (dir_stack == NULL)
1385 return NULL;
1386
1387 ds_ptr = dir_stack->next;
1388 fullname = NULL;
1389 while (ds_ptr)
1390 {
1391 vim_free(fullname);
1392 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1393
1394 /* If concat_fnames failed, just go on. The worst thing that can happen
1395 * is that we delete the entire stack.
1396 */
1397 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1398 break;
1399
1400 ds_ptr = ds_ptr->next;
1401 }
1402
1403 vim_free(fullname);
1404
1405 /* clean up all dirs we already left */
1406 while (dir_stack->next != ds_ptr)
1407 {
1408 ds_tmp = dir_stack->next;
1409 dir_stack->next = dir_stack->next->next;
1410 vim_free(ds_tmp->dirname);
1411 vim_free(ds_tmp);
1412 }
1413
1414 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1415
1416}
1417
1418/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001419 * When loading a file from the quickfix, the auto commands may modify it.
1420 * This may invalidate the current quickfix entry. This function checks
1421 * whether a entry is still present in the quickfix.
1422 * Similar to location list.
1423 */
1424 static int
1425is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1426{
1427 qf_list_T *qfl;
1428 qfline_T *qfp;
1429 int i;
1430
1431 qfl = &qi->qf_lists[qi->qf_curlist];
1432
1433 /* Search for the entry in the current list */
1434 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1435 ++i, qfp = qfp->qf_next)
1436 if (qfp == qf_ptr)
1437 break;
1438
1439 if (i == qfl->qf_count) /* Entry is not found */
1440 return FALSE;
1441
1442 return TRUE;
1443}
1444
1445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 * jump to a quickfix line
1447 * if dir == FORWARD go "errornr" valid entries forward
1448 * if dir == BACKWARD go "errornr" valid entries backward
1449 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1450 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1451 * else if "errornr" is zero, redisplay the same line
1452 * else go to entry "errornr"
1453 */
1454 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001455qf_jump(
1456 qf_info_T *qi,
1457 int dir,
1458 int errornr,
1459 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001461 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001462 qfline_T *qf_ptr;
1463 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 int qf_index;
1465 int old_qf_fnum;
1466 int old_qf_index;
1467 int prev_index;
1468 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1469 char_u *err = e_no_more_items;
1470 linenr_T i;
1471 buf_T *old_curbuf;
1472 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 colnr_T screen_col;
1474 colnr_T char_col;
1475 char_u *line;
1476#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001477 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001478 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 int opened_window = FALSE;
1480 win_T *win;
1481 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001482 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001484 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int print_message = TRUE;
1486 int len;
1487#ifdef FEAT_FOLDING
1488 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1489#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001490 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001491 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001493 if (qi == NULL)
1494 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001495
1496 if (qi->qf_curlist >= qi->qf_listcount
1497 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 {
1499 EMSG(_(e_quickfix));
1500 return;
1501 }
1502
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001503 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001505 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 old_qf_index = qf_index;
1507 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1508 {
1509 while (errornr--)
1510 {
1511 old_qf_ptr = qf_ptr;
1512 prev_index = qf_index;
1513 old_qf_fnum = qf_ptr->qf_fnum;
1514 do
1515 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001516 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 || qf_ptr->qf_next == NULL)
1518 {
1519 qf_ptr = old_qf_ptr;
1520 qf_index = prev_index;
1521 if (err != NULL)
1522 {
1523 EMSG(_(err));
1524 goto theend;
1525 }
1526 errornr = 0;
1527 break;
1528 }
1529 ++qf_index;
1530 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1532 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1534 err = NULL;
1535 }
1536 }
1537 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1538 {
1539 while (errornr--)
1540 {
1541 old_qf_ptr = qf_ptr;
1542 prev_index = qf_index;
1543 old_qf_fnum = qf_ptr->qf_fnum;
1544 do
1545 {
1546 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1547 {
1548 qf_ptr = old_qf_ptr;
1549 qf_index = prev_index;
1550 if (err != NULL)
1551 {
1552 EMSG(_(err));
1553 goto theend;
1554 }
1555 errornr = 0;
1556 break;
1557 }
1558 --qf_index;
1559 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001560 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1561 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1563 err = NULL;
1564 }
1565 }
1566 else if (errornr != 0) /* go to specified number */
1567 {
1568 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1569 {
1570 --qf_index;
1571 qf_ptr = qf_ptr->qf_prev;
1572 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001573 while (errornr > qf_index && qf_index <
1574 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 && qf_ptr->qf_next != NULL)
1576 {
1577 ++qf_index;
1578 qf_ptr = qf_ptr->qf_next;
1579 }
1580 }
1581
1582#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001583 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1584 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 /* No need to print the error message if it's visible in the error
1586 * window */
1587 print_message = FALSE;
1588
1589 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001590 * For ":helpgrep" find a help window or open one.
1591 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001592 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001593 {
1594 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001595
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001596 if (cmdmod.tab != 0)
1597 wp = NULL;
1598 else
1599 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1600 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1601 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001602 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1603 win_enter(wp, TRUE);
1604 else
1605 {
1606 /*
1607 * Split off help window; put it at far top if no position
1608 * specified, the current window is vertically split and narrow.
1609 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001610 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001611 if (cmdmod.split == 0 && curwin->w_width != Columns
1612 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001613 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001614 if (qi != &ql_info)
1615 flags |= WSP_NEWLOC; /* don't copy the location list */
1616
1617 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001618 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001619 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001620
1621 if (curwin->w_height < p_hh)
1622 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001623
1624 if (qi != &ql_info) /* not a quickfix list */
1625 {
1626 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001627 curwin->w_llist = qi;
1628 qi->qf_refcount++;
1629 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001630 }
1631
1632 if (!p_im)
1633 restart_edit = 0; /* don't want insert mode in help file */
1634 }
1635
1636 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 * If currently in the quickfix window, find another window to show the
1638 * file in.
1639 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001640 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001642 win_T *usable_win_ptr = NULL;
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 /*
1645 * If there is no file specified, we don't know where to go.
1646 * But do advance, otherwise ":cn" gets stuck.
1647 */
1648 if (qf_ptr->qf_fnum == 0)
1649 goto theend;
1650
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001651 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001652
1653 ll_ref = curwin->w_llist_ref;
1654 if (ll_ref != NULL)
1655 {
1656 /* Find a window using the same location list that is not a
1657 * quickfix window. */
1658 FOR_ALL_WINDOWS(usable_win_ptr)
1659 if (usable_win_ptr->w_llist == ll_ref
1660 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001661 {
1662 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001663 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001664 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001665 }
1666
1667 if (!usable_win)
1668 {
1669 /* Locate a window showing a normal buffer */
1670 FOR_ALL_WINDOWS(win)
1671 if (win->w_buffer->b_p_bt[0] == NUL)
1672 {
1673 usable_win = 1;
1674 break;
1675 }
1676 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001677
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001679 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001680 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001682 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001683 {
1684 tabpage_T *tp;
1685 win_T *wp;
1686
1687 FOR_ALL_TAB_WINDOWS(tp, wp)
1688 {
1689 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1690 {
1691 goto_tabpage_win(tp, wp);
1692 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001693 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001694 }
1695 }
1696 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001697win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001698
1699 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001700 * If there is only one window and it is the quickfix window, create a
1701 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001702 */
1703 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001705 flags = WSP_ABOVE;
1706 if (ll_ref != NULL)
1707 flags |= WSP_NEWLOC;
1708 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 goto failed; /* not enough room for window */
1710 opened_window = TRUE; /* close it when fail */
1711 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001712 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001713 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001714 if (ll_ref != NULL)
1715 {
1716 /* The new window should use the location list from the
1717 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001718 curwin->w_llist = ll_ref;
1719 ll_ref->qf_refcount++;
1720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 }
1722 else
1723 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001724 if (curwin->w_llist_ref != NULL)
1725 {
1726 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001727 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001728 if (win == NULL)
1729 {
1730 /* Find the window showing the selected file */
1731 FOR_ALL_WINDOWS(win)
1732 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1733 break;
1734 if (win == NULL)
1735 {
1736 /* Find a previous usable window */
1737 win = curwin;
1738 do
1739 {
1740 if (win->w_buffer->b_p_bt[0] == NUL)
1741 break;
1742 if (win->w_prev == NULL)
1743 win = lastwin; /* wrap around the top */
1744 else
1745 win = win->w_prev; /* go to previous window */
1746 } while (win != curwin);
1747 }
1748 }
1749 win_goto(win);
1750
1751 /* If the location list for the window is not set, then set it
1752 * to the location list from the location window */
1753 if (win->w_llist == NULL)
1754 {
1755 win->w_llist = ll_ref;
1756 ll_ref->qf_refcount++;
1757 }
1758 }
1759 else
1760 {
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 /*
1763 * Try to find a window that shows the right buffer.
1764 * Default to the window just above the quickfix buffer.
1765 */
1766 win = curwin;
1767 altwin = NULL;
1768 for (;;)
1769 {
1770 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1771 break;
1772 if (win->w_prev == NULL)
1773 win = lastwin; /* wrap around the top */
1774 else
1775 win = win->w_prev; /* go to previous window */
1776
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001777 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
1779 /* Didn't find it, go to the window before the quickfix
1780 * window. */
1781 if (altwin != NULL)
1782 win = altwin;
1783 else if (curwin->w_prev != NULL)
1784 win = curwin->w_prev;
1785 else
1786 win = curwin->w_next;
1787 break;
1788 }
1789
1790 /* Remember a usable window. */
1791 if (altwin == NULL && !win->w_p_pvw
1792 && win->w_buffer->b_p_bt[0] == NUL)
1793 altwin = win;
1794 }
1795
1796 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
1799 }
1800#endif
1801
1802 /*
1803 * If there is a file name,
1804 * read the wanted file if needed, and check autowrite etc.
1805 */
1806 old_curbuf = curbuf;
1807 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001808
1809 if (qf_ptr->qf_fnum != 0)
1810 {
1811 if (qf_ptr->qf_type == 1)
1812 {
1813 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1814 * set b_p_ro flag). */
1815 if (!can_abandon(curbuf, forceit))
1816 {
1817 EMSG(_(e_nowrtmsg));
1818 ok = FALSE;
1819 }
1820 else
1821 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001822 ECMD_HIDE + ECMD_SET_HELP,
1823 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001824 }
1825 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01001826 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001827 int old_qf_curlist = qi->qf_curlist;
1828 int is_abort = FALSE;
1829
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001830 ok = buflist_getfile(qf_ptr->qf_fnum,
1831 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01001832 if (qi != &ql_info && !win_valid(oldwin))
1833 {
1834 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001835 is_abort = TRUE;
1836 opened_window = FALSE;
1837 }
1838 else if (old_qf_curlist != qi->qf_curlist
1839 || !is_qf_entry_present(qi, qf_ptr))
1840 {
1841 if (qi == &ql_info)
1842 EMSG(_("E925: Current quickfix was changed"));
1843 else
1844 EMSG(_("E926: Current location list was changed"));
1845 is_abort = TRUE;
1846 }
1847
1848 if (is_abort)
1849 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01001850 ok = FALSE;
1851 qi = NULL;
1852 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01001853 }
1854 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001855 }
1856
1857 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
1859 /* When not switched to another buffer, still need to set pc mark */
1860 if (curbuf == old_curbuf)
1861 setpcmark();
1862
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001863 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001865 /*
1866 * Go to line with error, unless qf_lnum is 0.
1867 */
1868 i = qf_ptr->qf_lnum;
1869 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001871 if (i > curbuf->b_ml.ml_line_count)
1872 i = curbuf->b_ml.ml_line_count;
1873 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001875 if (qf_ptr->qf_col > 0)
1876 {
1877 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02001878#ifdef FEAT_VIRTUALEDIT
1879 curwin->w_cursor.coladd = 0;
1880#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001881 if (qf_ptr->qf_viscol == TRUE)
1882 {
1883 /*
1884 * Check each character from the beginning of the error
1885 * line up to the error column. For each tab character
1886 * found, reduce the error column value by the length of
1887 * a tab character.
1888 */
1889 line = ml_get_curline();
1890 screen_col = 0;
1891 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1892 {
1893 if (*line == NUL)
1894 break;
1895 if (*line++ == '\t')
1896 {
1897 curwin->w_cursor.col -= 7 - (screen_col % 8);
1898 screen_col += 8 - (screen_col % 8);
1899 }
1900 else
1901 ++screen_col;
1902 }
1903 }
1904 check_cursor();
1905 }
1906 else
1907 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001910 {
1911 pos_T save_cursor;
1912
1913 /* Move the cursor to the first line in the buffer */
1914 save_cursor = curwin->w_cursor;
1915 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001916 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1917 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001918 curwin->w_cursor = save_cursor;
1919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921#ifdef FEAT_FOLDING
1922 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1923 foldOpenCursor();
1924#endif
1925 if (print_message)
1926 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001927 /* Update the screen before showing the message, unless the screen
1928 * scrolled up. */
1929 if (!msg_scrolled)
1930 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001932 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1934 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1935 /* Add the message, skipping leading whitespace and newlines. */
1936 len = (int)STRLEN(IObuff);
1937 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1938
1939 /* Output the message. Overwrite to avoid scrolling when the 'O'
1940 * flag is present in 'shortmess'; But when not jumping, print the
1941 * whole message. */
1942 i = msg_scroll;
1943 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1944 msg_scroll = TRUE;
1945 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1946 msg_scroll = FALSE;
1947 msg_attr_keep(IObuff, 0, TRUE);
1948 msg_scroll = i;
1949 }
1950 }
1951 else
1952 {
1953#ifdef FEAT_WINDOWS
1954 if (opened_window)
1955 win_close(curwin, TRUE); /* Close opened window */
1956#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01001957 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 /*
1960 * Couldn't open file, so put index back where it was. This could
1961 * happen if the file was readonly and we changed something.
1962 */
1963#ifdef FEAT_WINDOWS
1964failed:
1965#endif
1966 qf_ptr = old_qf_ptr;
1967 qf_index = old_qf_index;
1968 }
1969 }
1970theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01001971 if (qi != NULL)
1972 {
1973 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1974 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976#ifdef FEAT_WINDOWS
1977 if (p_swb != old_swb && opened_window)
1978 {
1979 /* Restore old 'switchbuf' value, but not when an autocommand or
1980 * modeline has changed the value. */
1981 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001984 swb_flags = old_swb_flags;
1985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 else
1987 free_string_option(old_swb);
1988 }
1989#endif
1990}
1991
1992/*
1993 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001994 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 */
1996 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001997qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001999 buf_T *buf;
2000 char_u *fname;
2001 qfline_T *qfp;
2002 int i;
2003 int idx1 = 1;
2004 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002005 char_u *arg = eap->arg;
2006 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002008 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002010 if (eap->cmdidx == CMD_llist)
2011 {
2012 qi = GET_LOC_LIST(curwin);
2013 if (qi == NULL)
2014 {
2015 EMSG(_(e_loclist));
2016 return;
2017 }
2018 }
2019
2020 if (qi->qf_curlist >= qi->qf_listcount
2021 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
2023 EMSG(_(e_quickfix));
2024 return;
2025 }
2026 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2027 {
2028 EMSG(_(e_trailing));
2029 return;
2030 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002031 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (idx1 < 0)
2033 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2034 if (idx2 < 0)
2035 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2036
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002037 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002039 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2040 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2043 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002044 msg_putchar('\n');
2045 if (got_int)
2046 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002047
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002048 fname = NULL;
2049 if (qfp->qf_fnum != 0
2050 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2051 {
2052 fname = buf->b_fname;
2053 if (qfp->qf_type == 1) /* :helpgrep */
2054 fname = gettail(fname);
2055 }
2056 if (fname == NULL)
2057 sprintf((char *)IObuff, "%2d", i);
2058 else
2059 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2060 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002061 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002062 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2063 if (qfp->qf_lnum == 0)
2064 IObuff[0] = NUL;
2065 else if (qfp->qf_col == 0)
2066 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2067 else
2068 sprintf((char *)IObuff, ":%ld col %d",
2069 qfp->qf_lnum, qfp->qf_col);
2070 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2071 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2072 msg_puts_attr(IObuff, hl_attr(HLF_N));
2073 if (qfp->qf_pattern != NULL)
2074 {
2075 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2076 STRCAT(IObuff, ":");
2077 msg_puts(IObuff);
2078 }
2079 msg_puts((char_u *)" ");
2080
2081 /* Remove newlines and leading whitespace from the text. For an
2082 * unrecognized line keep the indent, the compiler may mark a word
2083 * with ^^^^. */
2084 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2086 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002087 msg_prt_line(IObuff, FALSE);
2088 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002090
2091 qfp = qfp->qf_next;
2092 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 ui_breakcheck();
2094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095}
2096
2097/*
2098 * Remove newlines and leading whitespace from an error message.
2099 * Put the result in "buf[bufsize]".
2100 */
2101 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002102qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
2104 int i;
2105 char_u *p = text;
2106
2107 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2108 {
2109 if (*p == '\n')
2110 {
2111 buf[i] = ' ';
2112 while (*++p != NUL)
2113 if (!vim_iswhite(*p) && *p != '\n')
2114 break;
2115 }
2116 else
2117 buf[i] = *p++;
2118 }
2119 buf[i] = NUL;
2120}
2121
2122/*
2123 * ":colder [count]": Up in the quickfix stack.
2124 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002125 * ":lolder [count]": Up in the location list stack.
2126 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 */
2128 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002129qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 int count;
2133
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002134 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2135 {
2136 qi = GET_LOC_LIST(curwin);
2137 if (qi == NULL)
2138 {
2139 EMSG(_(e_loclist));
2140 return;
2141 }
2142 }
2143
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (eap->addr_count != 0)
2145 count = eap->line2;
2146 else
2147 count = 1;
2148 while (count--)
2149 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002150 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159 else
2160 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002161 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
2163 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002164 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002166 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
2168 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002169 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170}
2171
2172 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002173qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002176 qi->qf_curlist + 1, qi->qf_listcount,
2177 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002179 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180#endif
2181}
2182
2183/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002184 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 */
2186 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002187qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002189 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002190 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002192 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002194 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002195 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002196 {
2197 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002198 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002199 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2200 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002201 if (stop)
2202 /* Somehow qf_count may have an incorrect value, set it to 1
2203 * to avoid crashing when it's wrong.
2204 * TODO: Avoid qf_count being incorrect. */
2205 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002206 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002207 qi->qf_lists[idx].qf_start = qfp;
2208 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002210 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002211 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002212 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213}
2214
2215/*
2216 * qf_mark_adjust: adjust marks
2217 */
2218 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002219qf_mark_adjust(
2220 win_T *wp,
2221 linenr_T line1,
2222 linenr_T line2,
2223 long amount,
2224 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002226 int i;
2227 qfline_T *qfp;
2228 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002229 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002231 if (wp != NULL)
2232 {
2233 if (wp->w_llist == NULL)
2234 return;
2235 qi = wp->w_llist;
2236 }
2237
2238 for (idx = 0; idx < qi->qf_listcount; ++idx)
2239 if (qi->qf_lists[idx].qf_count)
2240 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2241 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (qfp->qf_fnum == curbuf->b_fnum)
2243 {
2244 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2245 {
2246 if (amount == MAXLNUM)
2247 qfp->qf_cleared = TRUE;
2248 else
2249 qfp->qf_lnum += amount;
2250 }
2251 else if (amount_after && qfp->qf_lnum > line2)
2252 qfp->qf_lnum += amount_after;
2253 }
2254}
2255
2256/*
2257 * Make a nice message out of the error character and the error number:
2258 * char number message
2259 * e or E 0 " error"
2260 * w or W 0 " warning"
2261 * i or I 0 " info"
2262 * 0 0 ""
2263 * other 0 " c"
2264 * e or E n " error n"
2265 * w or W n " warning n"
2266 * i or I n " info n"
2267 * 0 n " error n"
2268 * other n " c n"
2269 * 1 x "" :helpgrep
2270 */
2271 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002272qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274 static char_u buf[20];
2275 static char_u cc[3];
2276 char_u *p;
2277
2278 if (c == 'W' || c == 'w')
2279 p = (char_u *)" warning";
2280 else if (c == 'I' || c == 'i')
2281 p = (char_u *)" info";
2282 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2283 p = (char_u *)" error";
2284 else if (c == 0 || c == 1)
2285 p = (char_u *)"";
2286 else
2287 {
2288 cc[0] = ' ';
2289 cc[1] = c;
2290 cc[2] = NUL;
2291 p = cc;
2292 }
2293
2294 if (nr <= 0)
2295 return p;
2296
2297 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2298 return buf;
2299}
2300
2301#if defined(FEAT_WINDOWS) || defined(PROTO)
2302/*
2303 * ":cwindow": open the quickfix window if we have errors to display,
2304 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002305 * ":lwindow": open the location list window if we have locations to display,
2306 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
2308 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002309ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002311 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 win_T *win;
2313
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002314 if (eap->cmdidx == CMD_lwindow)
2315 {
2316 qi = GET_LOC_LIST(curwin);
2317 if (qi == NULL)
2318 return;
2319 }
2320
2321 /* Look for an existing quickfix window. */
2322 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324 /*
2325 * If a quickfix window is open but we have no errors to display,
2326 * close the window. If a quickfix window is not open, then open
2327 * it if we have errors; otherwise, leave it closed.
2328 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002330 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002331 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 if (win != NULL)
2334 ex_cclose(eap);
2335 }
2336 else if (win == NULL)
2337 ex_copen(eap);
2338}
2339
2340/*
2341 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002342 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002345ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347 win_T *win = NULL;
2348 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002350 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2351 {
2352 qi = GET_LOC_LIST(curwin);
2353 if (qi == NULL)
2354 return;
2355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002357 /* Find existing quickfix window and close it. */
2358 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (win != NULL)
2360 win_close(win, FALSE);
2361}
2362
2363/*
2364 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002365 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
2367 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002368ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002370 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002373 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002374 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002375 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002377 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2378 {
2379 qi = GET_LOC_LIST(curwin);
2380 if (qi == NULL)
2381 {
2382 EMSG(_(e_loclist));
2383 return;
2384 }
2385 }
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (eap->addr_count != 0)
2388 height = eap->line2;
2389 else
2390 height = QF_WINHEIGHT;
2391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393#ifdef FEAT_GUI
2394 need_mouse_correct = TRUE;
2395#endif
2396
2397 /*
2398 * Find existing quickfix window, or open a new one.
2399 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002400 win = qf_find_win(qi);
2401
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002402 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002405 if (eap->addr_count != 0)
2406 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002407 if (cmdmod.split & WSP_VERT)
2408 {
2409 if (height != W_WIDTH(win))
2410 win_setwidth(height);
2411 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002412 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002413 win_setheight(height);
2414 }
2415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 else
2417 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002418 qf_buf = qf_find_buf(qi);
2419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 /* The current window becomes the previous window afterwards. */
2421 win = curwin;
2422
Bram Moolenaar77642c02012-11-20 17:55:10 +01002423 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2424 && cmdmod.split == 0)
2425 /* Create the new window at the very bottom, except when
2426 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002427 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002428 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002430 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002432 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002434 /*
2435 * For the location list window, create a reference to the
2436 * location list from the window 'win'.
2437 */
2438 curwin->w_llist_ref = win->w_llist;
2439 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002441
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002442 if (oldwin != curwin)
2443 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002444 if (qf_buf != NULL)
2445 /* Use the existing quickfix buffer */
2446 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002447 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002448 else
2449 {
2450 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002451 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002452 /* switch off 'swapfile' */
2453 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2454 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002455 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002456 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002457 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002458#ifdef FEAT_DIFF
2459 curwin->w_p_diff = FALSE;
2460#endif
2461#ifdef FEAT_FOLDING
2462 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2463 OPT_LOCAL);
2464#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002467 /* Only set the height when still in the same tab page and there is no
2468 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002469 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 win_setheight(height);
2471 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2472 if (win_valid(win))
2473 prevwin = win;
2474 }
2475
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002476 qf_set_title_var(qi);
2477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 /*
2479 * Fill the buffer with the quickfix list.
2480 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002481 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002483 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 curwin->w_cursor.col = 0;
2485 check_cursor();
2486 update_topline(); /* scroll to show the line */
2487}
2488
2489/*
2490 * Return the number of the current entry (line number in the quickfix
2491 * window).
2492 */
2493 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002494qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002496 qf_info_T *qi = &ql_info;
2497
2498 if (IS_LL_WINDOW(wp))
2499 /* In the location list window, use the referenced location list */
2500 qi = wp->w_llist_ref;
2501
2502 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503}
2504
2505/*
2506 * Update the cursor position in the quickfix window to the current error.
2507 * Return TRUE if there is a quickfix window.
2508 */
2509 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002510qf_win_pos_update(
2511 qf_info_T *qi,
2512 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002515 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517 /*
2518 * Put the cursor on the current error in the quickfix window, so that
2519 * it's viewable.
2520 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002521 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (win != NULL
2523 && qf_index <= win->w_buffer->b_ml.ml_line_count
2524 && old_qf_index != qf_index)
2525 {
2526 win_T *old_curwin = curwin;
2527
2528 curwin = win;
2529 curbuf = win->w_buffer;
2530 if (qf_index > old_qf_index)
2531 {
2532 curwin->w_redraw_top = old_qf_index;
2533 curwin->w_redraw_bot = qf_index;
2534 }
2535 else
2536 {
2537 curwin->w_redraw_top = qf_index;
2538 curwin->w_redraw_bot = old_qf_index;
2539 }
2540 curwin->w_cursor.lnum = qf_index;
2541 curwin->w_cursor.col = 0;
2542 update_topline(); /* scroll to show the line */
2543 redraw_later(VALID);
2544 curwin->w_redr_status = TRUE; /* update ruler */
2545 curwin = old_curwin;
2546 curbuf = curwin->w_buffer;
2547 }
2548 return win != NULL;
2549}
2550
2551/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002552 * Check whether the given window is displaying the specified quickfix/location
2553 * list buffer
2554 */
2555 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002556is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002557{
2558 /*
2559 * A window displaying the quickfix buffer will have the w_llist_ref field
2560 * set to NULL.
2561 * A window displaying a location list buffer will have the w_llist_ref
2562 * pointing to the location list.
2563 */
2564 if (bt_quickfix(win->w_buffer))
2565 if ((qi == &ql_info && win->w_llist_ref == NULL)
2566 || (qi != &ql_info && win->w_llist_ref == qi))
2567 return TRUE;
2568
2569 return FALSE;
2570}
2571
2572/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002573 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002574 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002575 */
2576 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002577qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002578{
2579 win_T *win;
2580
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002581 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002582 if (is_qf_win(win, qi))
2583 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002584
2585 return win;
2586}
2587
2588/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002589 * Find a quickfix buffer.
2590 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 */
2592 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002593qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002595 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002596 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Bram Moolenaar9c102382006-05-03 21:26:49 +00002598 FOR_ALL_TAB_WINDOWS(tp, win)
2599 if (is_qf_win(win, qi))
2600 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002601
Bram Moolenaar9c102382006-05-03 21:26:49 +00002602 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603}
2604
2605/*
2606 * Find the quickfix buffer. If it exists, update the contents.
2607 */
2608 static void
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002609qf_update_buffer(qf_info_T *qi, int update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610{
2611 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002612 win_T *win;
2613 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
2616 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002617 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 if (buf != NULL)
2619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 /* set curwin/curbuf to buf and save a few things */
2621 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002623 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002624 {
2625 curwin_save = curwin;
2626 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002627 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002628 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002629 }
2630
Bram Moolenaar6920c722016-01-22 22:44:10 +01002631 qf_fill_buffer(qi);
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /* restore curwin/curbuf and a few other things */
2634 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002636 if (update_cursor)
2637 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639}
2640
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002641/*
2642 * Set "w:quickfix_title" if "qi" has a title.
2643 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002644 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002645qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002646{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002647 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2648 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002649 qi->qf_lists[qi->qf_curlist].qf_title);
2650}
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652/*
2653 * Fill current buffer with quickfix errors, replacing any previous contents.
2654 * curbuf must be the quickfix buffer!
2655 */
2656 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002657qf_fill_buffer(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002659 linenr_T lnum;
2660 qfline_T *qfp;
2661 buf_T *errbuf;
2662 int len;
2663 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 /* delete all existing lines */
2666 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2667 (void)ml_delete((linenr_T)1, FALSE);
2668
2669 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002670 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
2672 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002673 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2674 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
2676 if (qfp->qf_fnum != 0
2677 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2678 && errbuf->b_fname != NULL)
2679 {
2680 if (qfp->qf_type == 1) /* :helpgrep */
2681 STRCPY(IObuff, gettail(errbuf->b_fname));
2682 else
2683 STRCPY(IObuff, errbuf->b_fname);
2684 len = (int)STRLEN(IObuff);
2685 }
2686 else
2687 len = 0;
2688 IObuff[len++] = '|';
2689
2690 if (qfp->qf_lnum > 0)
2691 {
2692 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2693 len += (int)STRLEN(IObuff + len);
2694
2695 if (qfp->qf_col > 0)
2696 {
2697 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2698 len += (int)STRLEN(IObuff + len);
2699 }
2700
2701 sprintf((char *)IObuff + len, "%s",
2702 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2703 len += (int)STRLEN(IObuff + len);
2704 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002705 else if (qfp->qf_pattern != NULL)
2706 {
2707 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2708 len += (int)STRLEN(IObuff + len);
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 IObuff[len++] = '|';
2711 IObuff[len++] = ' ';
2712
2713 /* Remove newlines and leading whitespace from the text.
2714 * For an unrecognized line keep the indent, the compiler may
2715 * mark a word with ^^^^. */
2716 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2717 IObuff + len, IOSIZE - len);
2718
2719 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2720 == FAIL)
2721 break;
2722 qfp = qfp->qf_next;
2723 }
2724 /* Delete the empty line which is now at the end */
2725 (void)ml_delete(lnum + 1, FALSE);
2726 }
2727
2728 /* correct cursor position */
2729 check_lnums(TRUE);
2730
2731 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2732 * resembles reading a file into a buffer, it's more logical when using
2733 * autocommands. */
2734 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2735 curbuf->b_p_ma = FALSE;
2736
2737#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002738 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2740 FALSE, curbuf);
2741 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2742 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002743 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744#endif
2745
2746 /* make sure it will be redrawn */
2747 redraw_curbuf_later(NOT_VALID);
2748
2749 /* Restore KeyTyped, setting 'filetype' may reset it. */
2750 KeyTyped = old_KeyTyped;
2751}
2752
2753#endif /* FEAT_WINDOWS */
2754
2755/*
2756 * Return TRUE if "buf" is the quickfix buffer.
2757 */
2758 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002759bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002761 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762}
2763
2764/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002765 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2766 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 */
2768 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002769bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002771 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2772 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773}
2774
2775/*
2776 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2777 */
2778 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002779bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002781 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782}
2783
2784 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002785bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
2787 if (bt_dontwrite(buf))
2788 {
2789 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2790 return TRUE;
2791 }
2792 return FALSE;
2793}
2794
2795/*
2796 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2797 * and 'bufhidden'.
2798 */
2799 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002800buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2803 switch (buf->b_p_bh[0])
2804 {
2805 case 'u': /* "unload" */
2806 case 'w': /* "wipe" */
2807 case 'd': return FALSE; /* "delete" */
2808 case 'h': return TRUE; /* "hide" */
2809 }
2810 return (p_hid || cmdmod.hide);
2811}
2812
2813/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002814 * Return TRUE when using ":vimgrep" for ":grep".
2815 */
2816 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002817grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002818{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002819 return ((cmdidx == CMD_grep
2820 || cmdidx == CMD_lgrep
2821 || cmdidx == CMD_grepadd
2822 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002823 && STRCMP("internal",
2824 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2825}
2826
2827/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002828 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 */
2830 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002831ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002833 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 char_u *cmd;
2835 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002836 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002837 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002838 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002839#ifdef FEAT_AUTOCMD
2840 char_u *au_name = NULL;
2841
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002842 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2843 if (grep_internal(eap->cmdidx))
2844 {
2845 ex_vimgrep(eap);
2846 return;
2847 }
2848
Bram Moolenaar7c626922005-02-07 22:01:03 +00002849 switch (eap->cmdidx)
2850 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002851 case CMD_make: au_name = (char_u *)"make"; break;
2852 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2853 case CMD_grep: au_name = (char_u *)"grep"; break;
2854 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2855 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2856 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002857 default: break;
2858 }
2859 if (au_name != NULL)
2860 {
2861 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2862 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002863# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002864 if (did_throw || force_abort)
2865 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002866# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002867 }
2868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaara6557602006-02-04 22:43:20 +00002870 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2871 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002872 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002875 fname = get_mef_name();
2876 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002878 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 /*
2881 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2882 */
2883 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2884 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002885 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 cmd = alloc(len);
2887 if (cmd == NULL)
2888 return;
2889 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2890 (char *)p_shq);
2891 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002892 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 /*
2894 * Output a newline if there's something else than the :make command that
2895 * was typed (in which case the cursor is in column 0).
2896 */
2897 if (msg_col == 0)
2898 msg_didout = FALSE;
2899 msg_start();
2900 MSG_PUTS(":!");
2901 msg_outtrans(cmd); /* show what we are doing */
2902
2903 /* let the shell know if we are redirecting output or not */
2904 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2905
2906#ifdef AMIGA
2907 out_flush();
2908 /* read window status report and redraw before message */
2909 (void)char_avail();
2910#endif
2911
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002912 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002913 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2914 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002915 && eap->cmdidx != CMD_lgrepadd),
2916 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002917 if (wp != NULL)
2918 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002919#ifdef FEAT_AUTOCMD
2920 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002921 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002922 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2923 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002924 if (qi->qf_curlist < qi->qf_listcount)
2925 res = qi->qf_lists[qi->qf_curlist].qf_count;
2926 else
2927 res = 0;
2928 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002929#endif
2930 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002931 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
Bram Moolenaar7c626922005-02-07 22:01:03 +00002933 mch_remove(fname);
2934 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 vim_free(cmd);
2936}
2937
2938/*
2939 * Return the name for the errorfile, in allocated memory.
2940 * Find a new unique name when 'makeef' contains "##".
2941 * Returns NULL for error.
2942 */
2943 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002944get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 char_u *p;
2947 char_u *name;
2948 static int start = -1;
2949 static int off = 0;
2950#ifdef HAVE_LSTAT
2951 struct stat sb;
2952#endif
2953
2954 if (*p_mef == NUL)
2955 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002956 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (name == NULL)
2958 EMSG(_(e_notmp));
2959 return name;
2960 }
2961
2962 for (p = p_mef; *p; ++p)
2963 if (p[0] == '#' && p[1] == '#')
2964 break;
2965
2966 if (*p == NUL)
2967 return vim_strsave(p_mef);
2968
2969 /* Keep trying until the name doesn't exist yet. */
2970 for (;;)
2971 {
2972 if (start == -1)
2973 start = mch_get_pid();
2974 else
2975 off += 19;
2976
2977 name = alloc((unsigned)STRLEN(p_mef) + 30);
2978 if (name == NULL)
2979 break;
2980 STRCPY(name, p_mef);
2981 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2982 STRCAT(name, p + 2);
2983 if (mch_getperm(name) < 0
2984#ifdef HAVE_LSTAT
2985 /* Don't accept a symbolic link, its a security risk. */
2986 && mch_lstat((char *)name, &sb) < 0
2987#endif
2988 )
2989 break;
2990 vim_free(name);
2991 }
2992 return name;
2993}
2994
2995/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002996 * Returns the number of valid entries in the current quickfix/location list.
2997 */
2998 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002999qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003000{
3001 qf_info_T *qi = &ql_info;
3002 qfline_T *qfp;
3003 int i, sz = 0;
3004 int prev_fnum = 0;
3005
3006 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3007 {
3008 /* Location list */
3009 qi = GET_LOC_LIST(curwin);
3010 if (qi == NULL)
3011 return 0;
3012 }
3013
3014 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3015 (i < qi->qf_lists[qi->qf_curlist].qf_count) && (qfp != NULL);
3016 ++i, qfp = qfp->qf_next)
3017 {
3018 if (qfp->qf_valid)
3019 {
3020 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3021 sz++; /* Count all valid entries */
3022 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3023 {
3024 /* Count the number of files */
3025 sz++;
3026 prev_fnum = qfp->qf_fnum;
3027 }
3028 }
3029 }
3030
3031 return sz;
3032}
3033
3034/*
3035 * Returns the current index of the quickfix/location list.
3036 * Returns 0 if there is an error.
3037 */
3038 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003039qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003040{
3041 qf_info_T *qi = &ql_info;
3042
3043 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3044 {
3045 /* Location list */
3046 qi = GET_LOC_LIST(curwin);
3047 if (qi == NULL)
3048 return 0;
3049 }
3050
3051 return qi->qf_lists[qi->qf_curlist].qf_index;
3052}
3053
3054/*
3055 * Returns the current index in the quickfix/location list (counting only valid
3056 * entries). If no valid entries are in the list, then returns 1.
3057 */
3058 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003059qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003060{
3061 qf_info_T *qi = &ql_info;
3062 qf_list_T *qfl;
3063 qfline_T *qfp;
3064 int i, eidx = 0;
3065 int prev_fnum = 0;
3066
3067 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3068 {
3069 /* Location list */
3070 qi = GET_LOC_LIST(curwin);
3071 if (qi == NULL)
3072 return 1;
3073 }
3074
3075 qfl = &qi->qf_lists[qi->qf_curlist];
3076 qfp = qfl->qf_start;
3077
3078 /* check if the list has valid errors */
3079 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3080 return 1;
3081
3082 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3083 {
3084 if (qfp->qf_valid)
3085 {
3086 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3087 {
3088 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3089 {
3090 /* Count the number of files */
3091 eidx++;
3092 prev_fnum = qfp->qf_fnum;
3093 }
3094 }
3095 else
3096 eidx++;
3097 }
3098 }
3099
3100 return eidx ? eidx : 1;
3101}
3102
3103/*
3104 * Get the 'n'th valid error entry in the quickfix or location list.
3105 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3106 * For :cdo and :ldo returns the 'n'th valid error entry.
3107 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3108 */
3109 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003110qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003111{
3112 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3113 qfline_T *qfp = qfl->qf_start;
3114 int i, eidx;
3115 int prev_fnum = 0;
3116
3117 /* check if the list has valid errors */
3118 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3119 return 1;
3120
3121 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp!= NULL;
3122 i++, qfp = qfp->qf_next)
3123 {
3124 if (qfp->qf_valid)
3125 {
3126 if (fdo)
3127 {
3128 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3129 {
3130 /* Count the number of files */
3131 eidx++;
3132 prev_fnum = qfp->qf_fnum;
3133 }
3134 }
3135 else
3136 eidx++;
3137 }
3138
3139 if (eidx == n)
3140 break;
3141 }
3142
3143 if (i <= qfl->qf_count)
3144 return i;
3145 else
3146 return 1;
3147}
3148
3149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003151 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003152 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 */
3154 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003155ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003157 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003158 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003159
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003160 if (eap->cmdidx == CMD_ll
3161 || eap->cmdidx == CMD_lrewind
3162 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003163 || eap->cmdidx == CMD_llast
3164 || eap->cmdidx == CMD_ldo
3165 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003166 {
3167 qi = GET_LOC_LIST(curwin);
3168 if (qi == NULL)
3169 {
3170 EMSG(_(e_loclist));
3171 return;
3172 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003173 }
3174
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003175 if (eap->addr_count > 0)
3176 errornr = (int)eap->line2;
3177 else
3178 {
3179 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3180 errornr = 0;
3181 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3182 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3183 errornr = 1;
3184 else
3185 errornr = 32767;
3186 }
3187
3188 /* For cdo and ldo commands, jump to the nth valid error.
3189 * For cfdo and lfdo commands, jump to the nth valid file entry.
3190 */
3191 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3192 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3193 errornr = qf_get_nth_valid_entry(qi,
3194 eap->addr_count > 0 ? (int)eap->line1 : 1,
3195 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3196
3197 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198}
3199
3200/*
3201 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003202 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003203 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 */
3205 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003206ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003208 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003209 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003210
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003211 if (eap->cmdidx == CMD_lnext
3212 || eap->cmdidx == CMD_lNext
3213 || eap->cmdidx == CMD_lprevious
3214 || eap->cmdidx == CMD_lnfile
3215 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003216 || eap->cmdidx == CMD_lpfile
3217 || eap->cmdidx == CMD_ldo
3218 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003219 {
3220 qi = GET_LOC_LIST(curwin);
3221 if (qi == NULL)
3222 {
3223 EMSG(_(e_loclist));
3224 return;
3225 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003226 }
3227
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003228 if (eap->addr_count > 0 &&
3229 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3230 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3231 errornr = (int)eap->line2;
3232 else
3233 errornr = 1;
3234
3235 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3236 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003238 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3239 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003241 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3242 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 ? BACKWARD_FILE
3244 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003245 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246}
3247
3248/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003249 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003250 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 */
3252 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003253ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003255 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003256 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003257#ifdef FEAT_AUTOCMD
3258 char_u *au_name = NULL;
3259#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003260
3261 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003262 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003263 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003264
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003265#ifdef FEAT_AUTOCMD
3266 switch (eap->cmdidx)
3267 {
3268 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3269 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3270 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3271 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3272 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3273 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3274 default: break;
3275 }
3276 if (au_name != NULL)
3277 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3278#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003279#ifdef FEAT_BROWSE
3280 if (cmdmod.browse)
3281 {
3282 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3283 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3284 if (browse_file == NULL)
3285 return;
3286 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3287 vim_free(browse_file);
3288 }
3289 else
3290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003292 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003293
3294 /*
3295 * This function is used by the :cfile, :cgetfile and :caddfile
3296 * commands.
3297 * :cfile always creates a new quickfix list and jumps to the
3298 * first error.
3299 * :cgetfile creates a new quickfix list but doesn't jump to the
3300 * first error.
3301 * :caddfile adds to an existing quickfix list. If there is no
3302 * quickfix list then a new list is created.
3303 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003304 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003305 && eap->cmdidx != CMD_laddfile),
3306 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003307 && (eap->cmdidx == CMD_cfile
3308 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003309 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003310#ifdef FEAT_AUTOCMD
3311 if (au_name != NULL)
3312 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3313#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003314 if (wp != NULL)
3315 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003316 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003317 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003318
3319 else
3320 {
3321#ifdef FEAT_AUTOCMD
3322 if (au_name != NULL)
3323 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3324#endif
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326}
3327
3328/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003329 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003330 * ":vimgrepadd {pattern} file(s)"
3331 * ":lvimgrep {pattern} file(s)"
3332 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003333 */
3334 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003335ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003336{
Bram Moolenaar81695252004-12-29 20:58:21 +00003337 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003338 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003339 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003340 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003341 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003342 char_u *s;
3343 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003344 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003345 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003346#ifdef FEAT_AUTOCMD
3347 qfline_T *cur_qf_start;
3348#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003349 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003350 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003351 buf_T *buf;
3352 int duplicate_name = FALSE;
3353 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003354 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003355 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003356 buf_T *first_match_buf = NULL;
3357 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003358 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003359#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3360 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003361#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003362 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003363 int flags = 0;
3364 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003365 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003366 char_u *dirname_start = NULL;
3367 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003368 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003369#ifdef FEAT_AUTOCMD
3370 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003371
3372 switch (eap->cmdidx)
3373 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003374 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3375 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3376 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003377 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003378 case CMD_grep: au_name = (char_u *)"grep"; break;
3379 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3380 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3381 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003382 default: break;
3383 }
3384 if (au_name != NULL)
3385 {
3386 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3387 curbuf->b_fname, TRUE, curbuf);
3388 if (did_throw || force_abort)
3389 return;
3390 }
3391#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003392
Bram Moolenaar754b5602006-02-09 23:53:20 +00003393 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003394 || eap->cmdidx == CMD_lvimgrep
3395 || eap->cmdidx == CMD_lgrepadd
3396 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003397 {
3398 qi = ll_get_or_alloc_list(curwin);
3399 if (qi == NULL)
3400 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003401 }
3402
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003403 if (eap->addr_count > 0)
3404 tomatch = eap->line2;
3405 else
3406 tomatch = MAXLNUM;
3407
Bram Moolenaar81695252004-12-29 20:58:21 +00003408 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003409 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003410 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003411 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003412 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003413 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003414 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003415 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003416 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003417
3418 if (s != NULL && *s == NUL)
3419 {
3420 /* Pattern is empty, use last search pattern. */
3421 if (last_search_pat() == NULL)
3422 {
3423 EMSG(_(e_noprevre));
3424 goto theend;
3425 }
3426 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3427 }
3428 else
3429 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3430
Bram Moolenaar86b68352004-12-27 21:59:20 +00003431 if (regmatch.regprog == NULL)
3432 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003433 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003434 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003435
3436 p = skipwhite(p);
3437 if (*p == NUL)
3438 {
3439 EMSG(_("E683: File name missing or invalid pattern"));
3440 goto theend;
3441 }
3442
Bram Moolenaar754b5602006-02-09 23:53:20 +00003443 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003444 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003446 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003447 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003448 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003449 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003450 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003451 prevp->qf_next != prevp; prevp = prevp->qf_next)
3452 ;
3453
3454 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003455 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003456 goto theend;
3457 if (fcount == 0)
3458 {
3459 EMSG(_(e_nomatch));
3460 goto theend;
3461 }
3462
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003463 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3464 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003465 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003466 {
3467 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003468 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003469 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003470
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003471 /* Remember the current directory, because a BufRead autocommand that does
3472 * ":lcd %:p:h" changes the meaning of short path names. */
3473 mch_dirname(dirname_start, MAXPATHL);
3474
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003475#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003476 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003477 * changing the current quickfix list. */
3478 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3479#endif
3480
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003481 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003482 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003483 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003484 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003485 if (time(NULL) > seconds)
3486 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003487 /* Display the file name every second or so, show the user we are
3488 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003489 seconds = time(NULL);
3490 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003491 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003492 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003493 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003494 else
3495 {
3496 msg_outtrans(p);
3497 vim_free(p);
3498 }
3499 msg_clr_eos();
3500 msg_didout = FALSE; /* overwrite this message */
3501 msg_nowait = TRUE; /* don't wait for this message */
3502 msg_col = 0;
3503 out_flush();
3504 }
3505
Bram Moolenaar81695252004-12-29 20:58:21 +00003506 buf = buflist_findname_exp(fnames[fi]);
3507 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3508 {
3509 /* Remember that a buffer with this name already exists. */
3510 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003511 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003512 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003513
3514#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3515 /* Don't do Filetype autocommands to avoid loading syntax and
3516 * indent scripts, a great speed improvement. */
3517 save_ei = au_event_disable(",Filetype");
3518#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003519 /* Don't use modelines here, it's useless. */
3520 save_mls = p_mls;
3521 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003522
3523 /* Load file into a buffer, so that 'fileencoding' is detected,
3524 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003525 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003526
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003527 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003528#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3529 au_event_restore(save_ei);
3530#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003531 }
3532 else
3533 /* Use existing, loaded buffer. */
3534 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003535
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003536#ifdef FEAT_AUTOCMD
3537 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3538 {
3539 int idx;
3540
3541 /* Autocommands changed the quickfix list. Find the one we were
3542 * using and restore it. */
3543 for (idx = 0; idx < LISTCOUNT; ++idx)
3544 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3545 {
3546 qi->qf_curlist = idx;
3547 break;
3548 }
3549 if (idx == LISTCOUNT)
3550 {
3551 /* List cannot be found, create a new one. */
3552 qf_new_list(qi, *eap->cmdlinep);
3553 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3554 }
3555 }
3556#endif
3557
Bram Moolenaar81695252004-12-29 20:58:21 +00003558 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003559 {
3560 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003561 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003562 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003563 else
3564 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003565 /* Try for a match in all lines of the buffer.
3566 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003567 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003568 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3569 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003570 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003571 col = 0;
3572 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003573 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003574 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003575 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003576 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003577 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003578 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003579 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003580 ml_get_buf(buf,
3581 regmatch.startpos[0].lnum + lnum, FALSE),
3582 regmatch.startpos[0].lnum + lnum,
3583 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003584 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003585 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003586 0, /* nr */
3587 0, /* type */
3588 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003589 ) == FAIL)
3590 {
3591 got_int = TRUE;
3592 break;
3593 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003594 found_match = TRUE;
3595 if (--tomatch == 0)
3596 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003597 if ((flags & VGR_GLOBAL) == 0
3598 || regmatch.endpos[0].lnum > 0)
3599 break;
3600 col = regmatch.endpos[0].col
3601 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003602 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003603 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003604 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003605 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003606 if (got_int)
3607 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003608 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003609#ifdef FEAT_AUTOCMD
3610 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3611#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003612
3613 if (using_dummy)
3614 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003615 if (found_match && first_match_buf == NULL)
3616 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003617 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003618 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003619 /* Never keep a dummy buffer if there is another buffer
3620 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003621 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003622 buf = NULL;
3623 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003624 else if (!cmdmod.hide
3625 || buf->b_p_bh[0] == 'u' /* "unload" */
3626 || buf->b_p_bh[0] == 'w' /* "wipe" */
3627 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003628 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003629 /* When no match was found we don't need to remember the
3630 * buffer, wipe it out. If there was a match and it
3631 * wasn't the first one or we won't jump there: only
3632 * unload the buffer.
3633 * Ignore 'hidden' here, because it may lead to having too
3634 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003635 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003636 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003637 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003638 buf = NULL;
3639 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003640 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003641 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003642 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003643 buf = NULL;
3644 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003645 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003646
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003647 if (buf != NULL)
3648 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003649 /* If the buffer is still loaded we need to use the
3650 * directory we jumped to below. */
3651 if (buf == first_match_buf
3652 && target_dir == NULL
3653 && STRCMP(dirname_start, dirname_now) != 0)
3654 target_dir = vim_strsave(dirname_now);
3655
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003656 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003657 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003658 * need to be done (again). But not the window-local
3659 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003660 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003661#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003662 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3663 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003664#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003665 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003666 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003667 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003668 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003669 }
3670 }
3671
3672 FreeWild(fcount, fnames);
3673
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003674 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3675 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3676 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003677
3678#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003679 qf_update_buffer(qi, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003680#endif
3681
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003682#ifdef FEAT_AUTOCMD
3683 if (au_name != NULL)
3684 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3685 curbuf->b_fname, TRUE, curbuf);
3686#endif
3687
Bram Moolenaar86b68352004-12-27 21:59:20 +00003688 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003689 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003690 {
3691 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003692 {
3693 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003694 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003695 if (buf != curbuf)
3696 /* If we jumped to another buffer redrawing will already be
3697 * taken care of. */
3698 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003699
3700 /* Jump to the directory used after loading the buffer. */
3701 if (curbuf == first_match_buf && target_dir != NULL)
3702 {
3703 exarg_T ea;
3704
3705 ea.arg = target_dir;
3706 ea.cmdidx = CMD_lcd;
3707 ex_cd(&ea);
3708 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003709 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003710 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003711 else
3712 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003713
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003714 /* If we loaded a dummy buffer into the current window, the autocommands
3715 * may have messed up things, need to redraw and recompute folds. */
3716 if (redraw_for_dummy)
3717 {
3718#ifdef FEAT_FOLDING
3719 foldUpdateAll(curwin);
3720#else
3721 redraw_later(NOT_VALID);
3722#endif
3723 }
3724
Bram Moolenaar86b68352004-12-27 21:59:20 +00003725theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003726 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003727 vim_free(dirname_now);
3728 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003729 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003730 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003731}
3732
3733/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003734 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003735 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003736 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3737 * If "s" is not NULL terminate the pattern with a NUL.
3738 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003739 */
3740 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003741skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003742{
3743 int c;
3744
3745 if (vim_isIDc(*p))
3746 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003747 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003748 if (s != NULL)
3749 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003750 p = skiptowhite(p);
3751 if (s != NULL && *p != NUL)
3752 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003753 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003754 else
3755 {
3756 /* ":vimgrep /pattern/[g][j] fname" */
3757 if (s != NULL)
3758 *s = p + 1;
3759 c = *p;
3760 p = skip_regexp(p + 1, c, TRUE, NULL);
3761 if (*p != c)
3762 return NULL;
3763
3764 /* Truncate the pattern. */
3765 if (s != NULL)
3766 *p = NUL;
3767 ++p;
3768
3769 /* Find the flags */
3770 while (*p == 'g' || *p == 'j')
3771 {
3772 if (flags != NULL)
3773 {
3774 if (*p == 'g')
3775 *flags |= VGR_GLOBAL;
3776 else
3777 *flags |= VGR_NOJUMP;
3778 }
3779 ++p;
3780 }
3781 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003782 return p;
3783}
3784
3785/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003786 * Restore current working directory to "dirname_start" if they differ, taking
3787 * into account whether it is set locally or globally.
3788 */
3789 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003790restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003791{
3792 char_u *dirname_now = alloc(MAXPATHL);
3793
3794 if (NULL != dirname_now)
3795 {
3796 mch_dirname(dirname_now, MAXPATHL);
3797 if (STRCMP(dirname_start, dirname_now) != 0)
3798 {
3799 /* If the directory has changed, change it back by building up an
3800 * appropriate ex command and executing it. */
3801 exarg_T ea;
3802
3803 ea.arg = dirname_start;
3804 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3805 ex_cd(&ea);
3806 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003807 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003808 }
3809}
3810
3811/*
3812 * Load file "fname" into a dummy buffer and return the buffer pointer,
3813 * placing the directory resulting from the buffer load into the
3814 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3815 * prior to calling this function. Restores directory to "dirname_start" prior
3816 * to returning, if autocmds or the 'autochdir' option have changed it.
3817 *
3818 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3819 * or wipe_dummy_buffer() later!
3820 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003821 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003822 */
3823 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003824load_dummy_buffer(
3825 char_u *fname,
3826 char_u *dirname_start, /* in: old directory */
3827 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003828{
3829 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003830 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003831 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003832 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003833
3834 /* Allocate a buffer without putting it in the buffer list. */
3835 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3836 if (newbuf == NULL)
3837 return NULL;
3838
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003839 /* Init the options. */
3840 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3841
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003842 /* need to open the memfile before putting the buffer in a window */
3843 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003844 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003845 /* set curwin/curbuf to buf and save a few things */
3846 aucmd_prepbuf(&aco, newbuf);
3847
3848 /* Need to set the filename for autocommands. */
3849 (void)setfname(curbuf, fname, NULL, FALSE);
3850
Bram Moolenaar81695252004-12-29 20:58:21 +00003851 /* Create swap file now to avoid the ATTENTION message. */
3852 check_need_swap(TRUE);
3853
3854 /* Remove the "dummy" flag, otherwise autocommands may not
3855 * work. */
3856 curbuf->b_flags &= ~BF_DUMMY;
3857
3858 if (readfile(fname, NULL,
3859 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3860 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003861 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003862 && !(curbuf->b_flags & BF_NEW))
3863 {
3864 failed = FALSE;
3865 if (curbuf != newbuf)
3866 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003867 /* Bloody autocommands changed the buffer! Can happen when
3868 * using netrw and editing a remote file. Use the current
3869 * buffer instead, delete the dummy one after restoring the
3870 * window stuff. */
3871 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003872 newbuf = curbuf;
3873 }
3874 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003875
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003876 /* restore curwin/curbuf and a few other things */
3877 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003878 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3879 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003880 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003881
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003882 /*
3883 * When autocommands/'autochdir' option changed directory: go back.
3884 * Let the caller know what the resulting dir was first, in case it is
3885 * important.
3886 */
3887 mch_dirname(resulting_dir, MAXPATHL);
3888 restore_start_dir(dirname_start);
3889
Bram Moolenaar81695252004-12-29 20:58:21 +00003890 if (!buf_valid(newbuf))
3891 return NULL;
3892 if (failed)
3893 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003894 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003895 return NULL;
3896 }
3897 return newbuf;
3898}
3899
3900/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003901 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3902 * directory to "dirname_start" prior to returning, if autocmds or the
3903 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003904 */
3905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003906wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003907{
3908 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003909 {
3910#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3911 cleanup_T cs;
3912
3913 /* Reset the error/interrupt/exception state here so that aborting()
3914 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3915 * work when got_int is set. */
3916 enter_cleanup(&cs);
3917#endif
3918
Bram Moolenaar81695252004-12-29 20:58:21 +00003919 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003920
3921#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3922 /* Restore the error/interrupt/exception state if not discarded by a
3923 * new aborting error, interrupt, or uncaught exception. */
3924 leave_cleanup(&cs);
3925#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003926 /* When autocommands/'autochdir' option changed directory: go back. */
3927 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003928 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003929}
3930
3931/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003932 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3933 * directory to "dirname_start" prior to returning, if autocmds or the
3934 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003935 */
3936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003937unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003938{
3939 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003940 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003941 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003942
3943 /* When autocommands/'autochdir' option changed directory: go back. */
3944 restore_start_dir(dirname_start);
3945 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003946}
3947
Bram Moolenaar05159a02005-02-26 23:04:13 +00003948#if defined(FEAT_EVAL) || defined(PROTO)
3949/*
3950 * Add each quickfix error to list "list" as a dictionary.
3951 */
3952 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003953get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003954{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003955 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003956 dict_T *dict;
3957 char_u buf[2];
3958 qfline_T *qfp;
3959 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003960 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003961
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003962 if (wp != NULL)
3963 {
3964 qi = GET_LOC_LIST(wp);
3965 if (qi == NULL)
3966 return FAIL;
3967 }
3968
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003969 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003970 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003971 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003972
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003973 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3974 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003975 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003976 /* Handle entries with a non-existing buffer number. */
3977 bufnum = qfp->qf_fnum;
3978 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3979 bufnum = 0;
3980
Bram Moolenaar05159a02005-02-26 23:04:13 +00003981 if ((dict = dict_alloc()) == NULL)
3982 return FAIL;
3983 if (list_append_dict(list, dict) == FAIL)
3984 return FAIL;
3985
3986 buf[0] = qfp->qf_type;
3987 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003988 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003989 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3990 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3991 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3992 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003993 || dict_add_nr_str(dict, "pattern", 0L,
3994 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3995 || dict_add_nr_str(dict, "text", 0L,
3996 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003997 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3998 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3999 return FAIL;
4000
4001 qfp = qfp->qf_next;
4002 }
4003 return OK;
4004}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004005
4006/*
4007 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02004008 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004009 */
4010 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004011set_errorlist(
4012 win_T *wp,
4013 list_T *list,
4014 int action,
4015 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004016{
4017 listitem_T *li;
4018 dict_T *d;
4019 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004020 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004021 long lnum;
4022 int col, nr;
4023 int vcol;
4024 qfline_T *prevp = NULL;
4025 int valid, status;
4026 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004027 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004028 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004029
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004030 if (wp != NULL)
4031 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004032 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004033 if (qi == NULL)
4034 return FAIL;
4035 }
4036
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004037 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004038 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004039 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004040 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004041 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004042 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004043 prevp->qf_next != prevp; prevp = prevp->qf_next)
4044 ;
4045 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004046 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004047 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004048 qf_store_title(qi, title);
4049 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004050
4051 for (li = list->lv_first; li != NULL; li = li->li_next)
4052 {
4053 if (li->li_tv.v_type != VAR_DICT)
4054 continue; /* Skip non-dict items */
4055
4056 d = li->li_tv.vval.v_dict;
4057 if (d == NULL)
4058 continue;
4059
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004060 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004061 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004062 lnum = get_dict_number(d, (char_u *)"lnum");
4063 col = get_dict_number(d, (char_u *)"col");
4064 vcol = get_dict_number(d, (char_u *)"vcol");
4065 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004066 type = get_dict_string(d, (char_u *)"type", TRUE);
4067 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4068 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004069 if (text == NULL)
4070 text = vim_strsave((char_u *)"");
4071
4072 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004073 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004074 valid = FALSE;
4075
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004076 /* Mark entries with non-existing buffer number as not valid. Give the
4077 * error message only once. */
4078 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4079 {
4080 if (!did_bufnr_emsg)
4081 {
4082 did_bufnr_emsg = TRUE;
4083 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4084 }
4085 valid = FALSE;
4086 bufnum = 0;
4087 }
4088
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004089 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004090 NULL, /* dir */
4091 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004092 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004093 text,
4094 lnum,
4095 col,
4096 vcol, /* vis_col */
4097 pattern, /* search pattern */
4098 nr,
4099 type == NULL ? NUL : *type,
4100 valid);
4101
4102 vim_free(filename);
4103 vim_free(pattern);
4104 vim_free(text);
4105 vim_free(type);
4106
4107 if (status == FAIL)
4108 {
4109 retval = FAIL;
4110 break;
4111 }
4112 }
4113
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004114 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004115 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004116 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4117 else
4118 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004119 if (action != 'a') {
4120 qi->qf_lists[qi->qf_curlist].qf_ptr =
4121 qi->qf_lists[qi->qf_curlist].qf_start;
4122 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4123 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4124 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004125
4126#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004127 /* Don't update the cursor in quickfix window when appending entries */
4128 qf_update_buffer(qi, (action != 'a'));
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004129#endif
4130
4131 return retval;
4132}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004133#endif
4134
Bram Moolenaar81695252004-12-29 20:58:21 +00004135/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004136 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004137 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004138 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004139 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004140 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004141 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004142 */
4143 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004144ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004145{
4146 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004147 qf_info_T *qi = &ql_info;
4148
Bram Moolenaardb552d602006-03-23 22:59:57 +00004149 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4150 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004151 {
4152 qi = ll_get_or_alloc_list(curwin);
4153 if (qi == NULL)
4154 return;
4155 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004156
4157 if (*eap->arg == NUL)
4158 buf = curbuf;
4159 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4160 buf = buflist_findnr(atoi((char *)eap->arg));
4161 if (buf == NULL)
4162 EMSG(_(e_invarg));
4163 else if (buf->b_ml.ml_mfp == NULL)
4164 EMSG(_("E681: Buffer is not loaded"));
4165 else
4166 {
4167 if (eap->addr_count == 0)
4168 {
4169 eap->line1 = 1;
4170 eap->line2 = buf->b_ml.ml_line_count;
4171 }
4172 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4173 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4174 EMSG(_(e_invrange));
4175 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004176 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004177 char_u *qf_title = *eap->cmdlinep;
4178
4179 if (buf->b_sfname)
4180 {
4181 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4182 (char *)qf_title, (char *)buf->b_sfname);
4183 qf_title = IObuff;
4184 }
4185
Bram Moolenaardb552d602006-03-23 22:59:57 +00004186 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4187 (eap->cmdidx != CMD_caddbuffer
4188 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004189 eap->line1, eap->line2,
4190 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004191 && (eap->cmdidx == CMD_cbuffer
4192 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004193 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4194 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004195 }
4196}
4197
Bram Moolenaar1e015462005-09-25 22:16:38 +00004198#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004199/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004200 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4201 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004202 */
4203 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004204ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004205{
4206 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004207 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004208
Bram Moolenaardb552d602006-03-23 22:59:57 +00004209 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4210 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004211 {
4212 qi = ll_get_or_alloc_list(curwin);
4213 if (qi == NULL)
4214 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004215 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004216
Bram Moolenaar4770d092006-01-12 23:22:24 +00004217 /* Evaluate the expression. When the result is a string or a list we can
4218 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004219 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004220 if (tv != NULL)
4221 {
4222 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4223 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4224 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004225 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004226 (eap->cmdidx != CMD_caddexpr
4227 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004228 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004229 && (eap->cmdidx == CMD_cexpr
4230 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004231 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004232 }
4233 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004234 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004235 free_tv(tv);
4236 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004237}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004238#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004239
4240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * ":helpgrep {pattern}"
4242 */
4243 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004244ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 regmatch_T regmatch;
4247 char_u *save_cpo;
4248 char_u *p;
4249 int fcount;
4250 char_u **fnames;
4251 FILE *fd;
4252 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004253 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004255#ifdef FEAT_MULTI_LANG
4256 char_u *lang;
4257#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004259 int new_qi = FALSE;
4260 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004261#ifdef FEAT_AUTOCMD
4262 char_u *au_name = NULL;
4263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004265#ifdef FEAT_MULTI_LANG
4266 /* Check for a specified language */
4267 lang = check_help_lang(eap->arg);
4268#endif
4269
Bram Moolenaar73633f82012-01-20 13:39:07 +01004270#ifdef FEAT_AUTOCMD
4271 switch (eap->cmdidx)
4272 {
4273 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4274 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4275 default: break;
4276 }
4277 if (au_name != NULL)
4278 {
4279 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4280 curbuf->b_fname, TRUE, curbuf);
4281 if (did_throw || force_abort)
4282 return;
4283 }
4284#endif
4285
4286 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4287 save_cpo = p_cpo;
4288 p_cpo = empty_option;
4289
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004290 if (eap->cmdidx == CMD_lhelpgrep)
4291 {
4292 /* Find an existing help window */
4293 FOR_ALL_WINDOWS(wp)
4294 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4295 break;
4296
4297 if (wp == NULL) /* Help window not found */
4298 qi = NULL;
4299 else
4300 qi = wp->w_llist;
4301
4302 if (qi == NULL)
4303 {
4304 /* Allocate a new location list for help text matches */
4305 if ((qi = ll_new_list()) == NULL)
4306 return;
4307 new_qi = TRUE;
4308 }
4309 }
4310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4312 regmatch.rm_ic = FALSE;
4313 if (regmatch.regprog != NULL)
4314 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004315#ifdef FEAT_MBYTE
4316 vimconv_T vc;
4317
4318 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4319 * differs. */
4320 vc.vc_type = CONV_NONE;
4321 if (!enc_utf8)
4322 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4323#endif
4324
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004326 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 /* Go through all directories in 'runtimepath' */
4329 p = p_rtp;
4330 while (*p != NUL && !got_int)
4331 {
4332 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4333
4334 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4335 add_pathsep(NameBuff);
4336 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4337 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4338 &fnames, EW_FILE|EW_SILENT) == OK
4339 && fcount > 0)
4340 {
4341 for (fi = 0; fi < fcount && !got_int; ++fi)
4342 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004343#ifdef FEAT_MULTI_LANG
4344 /* Skip files for a different language. */
4345 if (lang != NULL
4346 && STRNICMP(lang, fnames[fi]
4347 + STRLEN(fnames[fi]) - 3, 2) != 0
4348 && !(STRNICMP(lang, "en", 2) == 0
4349 && STRNICMP("txt", fnames[fi]
4350 + STRLEN(fnames[fi]) - 3, 3) == 0))
4351 continue;
4352#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004353 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (fd != NULL)
4355 {
4356 lnum = 1;
4357 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4358 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004359 char_u *line = IObuff;
4360#ifdef FEAT_MBYTE
4361 /* Convert a line if 'encoding' is not utf-8 and
4362 * the line contains a non-ASCII character. */
4363 if (vc.vc_type != CONV_NONE
4364 && has_non_ascii(IObuff)) {
4365 line = string_convert(&vc, IObuff, NULL);
4366 if (line == NULL)
4367 line = IObuff;
4368 }
4369#endif
4370
4371 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004373 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
4375 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004376 while (l > 0 && line[l - 1] <= ' ')
4377 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004379 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 NULL, /* dir */
4381 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004382 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004383 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004385 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004386 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004387 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004388 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 0, /* nr */
4390 1, /* type */
4391 TRUE /* valid */
4392 ) == FAIL)
4393 {
4394 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004395#ifdef FEAT_MBYTE
4396 if (line != IObuff)
4397 vim_free(line);
4398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 break;
4400 }
4401 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004402#ifdef FEAT_MBYTE
4403 if (line != IObuff)
4404 vim_free(line);
4405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 ++lnum;
4407 line_breakcheck();
4408 }
4409 fclose(fd);
4410 }
4411 }
4412 FreeWild(fcount, fnames);
4413 }
4414 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004415
Bram Moolenaar473de612013-06-08 18:19:48 +02004416 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004417#ifdef FEAT_MBYTE
4418 if (vc.vc_type != CONV_NONE)
4419 convert_setup(&vc, NULL, NULL);
4420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004422 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4423 qi->qf_lists[qi->qf_curlist].qf_ptr =
4424 qi->qf_lists[qi->qf_curlist].qf_start;
4425 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004428 if (p_cpo == empty_option)
4429 p_cpo = save_cpo;
4430 else
4431 /* Darn, some plugin changed the value. */
4432 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004435 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436#endif
4437
Bram Moolenaar73633f82012-01-20 13:39:07 +01004438#ifdef FEAT_AUTOCMD
4439 if (au_name != NULL)
4440 {
4441 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4442 curbuf->b_fname, TRUE, curbuf);
4443 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4444 /* autocommands made "qi" invalid */
4445 return;
4446 }
4447#endif
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004450 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004451 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004452 else
4453 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004454
4455 if (eap->cmdidx == CMD_lhelpgrep)
4456 {
4457 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004458 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004459 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4460 {
4461 if (new_qi)
4462 ll_free_all(&qi);
4463 }
4464 else if (curwin->w_llist == NULL)
4465 curwin->w_llist = qi;
4466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467}
4468
4469#endif /* FEAT_QUICKFIX */