blob: a2506e1a32ebb8b2a9bf02b626d6132dcd103a76 [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);
129static void qf_update_buffer(qf_info_T *qi);
130static 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 */
535 while (p_li && p_li->li_tv.v_type != VAR_STRING)
536 p_li = p_li->li_next; /* Skip non-string items */
537
538 if (!p_li) /* End of the list */
539 break;
540
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000541 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000542 if (len > CMDBUFFSIZE - 2)
543 len = CMDBUFFSIZE - 2;
544
545 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
546
547 p_li = p_li->li_next; /* next item */
548 }
549 }
550 else
551 {
552 /* Get the next line from the supplied buffer */
553 if (buflnum > lnumlast)
554 break;
555 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
556 CMDBUFFSIZE - 2);
557 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000558 }
559 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
560 break;
561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200563#ifdef FEAT_MBYTE
564 remove_bom(IObuff);
565#endif
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
568 *efmp = NUL;
569#ifdef USE_CRNL
570 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
571 *efmp = NUL;
572#endif
573
Bram Moolenaar01265852006-03-20 21:50:15 +0000574 /* If there was no %> item start at the first pattern */
575 if (fmt_start == NULL)
576 fmt_ptr = fmt_first;
577 else
578 {
579 fmt_ptr = fmt_start;
580 fmt_start = NULL;
581 }
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 /*
584 * Try to match each part of 'errorformat' until we find a complete
585 * match or no match.
586 */
587 valid = TRUE;
588restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000589 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100591 int r;
592
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 idx = fmt_ptr->prefix;
594 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
595 continue;
596 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000597 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (!multiscan)
599 errmsg[0] = NUL;
600 lnum = 0;
601 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000602 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 enr = -1;
604 type = 0;
605 tail = NULL;
606
607 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100608 r = vim_regexec(&regmatch, IObuff, (colnr_T)0);
609 fmt_ptr->prog = regmatch.regprog;
610 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {
612 if ((idx == 'C' || idx == 'Z') && !multiline)
613 continue;
614 if (vim_strchr((char_u *)"EWI", idx) != NULL)
615 type = idx;
616 else
617 type = 0;
618 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000619 * Extract error message data from matched line.
620 * We check for an actual submatch, because "\[" and "\]" in
621 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 */
623 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
624 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000625 int c;
626
627 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
628 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000629
630 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000631 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000632 *regmatch.endp[i] = NUL;
633 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
634 *regmatch.endp[i] = c;
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000637 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 continue;
639 }
640 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000641 {
642 if (regmatch.startp[i] == NULL)
643 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000647 {
648 if (regmatch.startp[i] == NULL)
649 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000653 {
654 if (regmatch.startp[i] == NULL)
655 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000659 {
660 if (regmatch.startp[i] == NULL)
661 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000663 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000664 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 STRCPY(errmsg, IObuff);
666 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
667 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000668 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
669 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000671 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000674 {
675 if (regmatch.startp[i] == NULL)
676 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
680 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200681 char_u *match_ptr;
682
Bram Moolenaar4169da72006-06-20 18:49:32 +0000683 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
684 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200685 col = 0;
686 for (match_ptr = regmatch.startp[i];
687 match_ptr != regmatch.endp[i]; ++match_ptr)
688 {
689 ++col;
690 if (*match_ptr == TAB)
691 {
692 col += 7;
693 col -= col % 8;
694 }
695 }
696 ++col;
697 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 }
699 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
700 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000701 if (regmatch.startp[i] == NULL)
702 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000706 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
707 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000708 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
709 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000710 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
711 if (len > CMDBUFFSIZE - 5)
712 len = CMDBUFFSIZE - 5;
713 STRCPY(pattern, "^\\V");
714 STRNCAT(pattern, regmatch.startp[i], len);
715 pattern[len + 3] = '\\';
716 pattern[len + 4] = '$';
717 pattern[len + 5] = NUL;
718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 break;
720 }
721 }
722 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000723
Bram Moolenaar4770d092006-01-12 23:22:24 +0000724 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000726 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
728 if (idx == 'D') /* enter directory */
729 {
730 if (*namebuf == NUL)
731 {
732 EMSG(_("E379: Missing or empty directory name"));
733 goto error2;
734 }
735 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
736 goto error2;
737 }
738 else if (idx == 'X') /* leave directory */
739 directory = qf_pop_dir(&dir_stack);
740 }
741 namebuf[0] = NUL; /* no match found, remove file name */
742 lnum = 0; /* don't jump to this line */
743 valid = FALSE;
744 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000745 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 multiline = multiignore = FALSE;
747 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000748 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000750 /* honor %> item */
751 if (fmt_ptr->conthere)
752 fmt_start = fmt_ptr;
753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100757 multiignore = FALSE; /* reset continuation */
758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
760 { /* continuation of multi-line msg */
761 if (qfprev == NULL)
762 goto error2;
763 if (*errmsg && !multiignore)
764 {
765 len = (int)STRLEN(qfprev->qf_text);
766 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
767 == NULL)
768 goto error2;
769 STRCPY(ptr, qfprev->qf_text);
770 vim_free(qfprev->qf_text);
771 qfprev->qf_text = ptr;
772 *(ptr += len) = '\n';
773 STRCPY(++ptr, errmsg);
774 }
775 if (qfprev->qf_nr == -1)
776 qfprev->qf_nr = enr;
777 if (vim_isprintc(type) && !qfprev->qf_type)
778 qfprev->qf_type = type; /* only printable chars allowed */
779 if (!qfprev->qf_lnum)
780 qfprev->qf_lnum = lnum;
781 if (!qfprev->qf_col)
782 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (!qfprev->qf_fnum)
785 qfprev->qf_fnum = qf_get_fnum(directory,
786 *namebuf || directory ? namebuf
787 : currfile && valid ? currfile : 0);
788 if (idx == 'Z')
789 multiline = multiignore = FALSE;
790 line_breakcheck();
791 continue;
792 }
793 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
794 {
795 /* global file names */
796 valid = FALSE;
797 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
798 {
799 if (*namebuf && idx == 'P')
800 currfile = qf_push_dir(namebuf, &file_stack);
801 else if (idx == 'Q')
802 currfile = qf_pop_dir(&file_stack);
803 *namebuf = NUL;
804 if (tail && *tail)
805 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000806 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 multiscan = TRUE;
808 goto restofline;
809 }
810 }
811 }
812 if (fmt_ptr->flags == '-') /* generally exclude this line */
813 {
814 if (multiline)
815 multiignore = TRUE; /* also exclude continuation lines */
816 continue;
817 }
818 }
819
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000820 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000822 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000824 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000825 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 errmsg,
827 lnum,
828 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000830 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 enr,
832 type,
833 valid) == FAIL)
834 goto error2;
835 line_breakcheck();
836 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000837 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000839 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000841 /* no valid entry found */
842 qi->qf_lists[qi->qf_curlist].qf_ptr =
843 qi->qf_lists[qi->qf_curlist].qf_start;
844 qi->qf_lists[qi->qf_curlist].qf_index = 1;
845 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847 else
848 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000849 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
850 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
851 qi->qf_lists[qi->qf_curlist].qf_ptr =
852 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000854 /* return number of matches */
855 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 goto qf_init_ok;
857 }
858 EMSG(_(e_readerrf));
859error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000860 qf_free(qi, qi->qf_curlist);
861 qi->qf_listcount--;
862 if (qi->qf_curlist > 0)
863 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000865 if (fd != NULL)
866 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
868 {
869 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +0200870 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 vim_free(fmt_ptr);
872 }
873 qf_clean_dir_stack(&dir_stack);
874 qf_clean_dir_stack(&file_stack);
875qf_init_end:
876 vim_free(namebuf);
877 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000878 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 vim_free(fmtstr);
880
881#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000882 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#endif
884
885 return retval;
886}
887
Bram Moolenaarfb604092014-07-23 15:55:00 +0200888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100889qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +0200890{
891 if (title != NULL)
892 {
893 char_u *p = alloc((int)STRLEN(title) + 2);
894
895 qi->qf_lists[qi->qf_curlist].qf_title = p;
896 if (p != NULL)
897 sprintf((char *)p, ":%s", (char *)title);
898 }
899}
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901/*
902 * Prepare for adding a new quickfix list.
903 */
904 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
907 int i;
908
909 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +0200910 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * the current entry. This makes it possible to browse in a tree-like
912 * way with ":grep'.
913 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000914 while (qi->qf_listcount > qi->qf_curlist + 1)
915 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 /*
918 * When the stack is full, remove to oldest entry
919 * Otherwise, add a new entry.
920 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000921 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000923 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000925 qi->qf_lists[i - 1] = qi->qf_lists[i];
926 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000929 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100930 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +0200931 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932}
933
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000934/*
935 * Free a location list
936 */
937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100938ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000939{
940 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000941 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000942
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000943 qi = *pqi;
944 if (qi == NULL)
945 return;
946 *pqi = NULL; /* Remove reference to this list */
947
948 qi->qf_refcount--;
949 if (qi->qf_refcount < 1)
950 {
951 /* No references to this location list */
952 for (i = 0; i < qi->qf_listcount; ++i)
953 qf_free(qi, i);
954 vim_free(qi);
955 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000956}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000957
958 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100959qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000960{
961 int i;
962 qf_info_T *qi = &ql_info;
963
964 if (wp != NULL)
965 {
966 /* location list */
967 ll_free_all(&wp->w_llist);
968 ll_free_all(&wp->w_llist_ref);
969 }
970 else
971 /* quickfix list */
972 for (i = 0; i < qi->qf_listcount; ++i)
973 qf_free(qi, i);
974}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976/*
977 * Add an entry to the end of the list of errors.
978 * Returns OK or FAIL.
979 */
980 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100981qf_add_entry(
982 qf_info_T *qi, /* quickfix list */
983 qfline_T **prevp, /* pointer to previously added entry or NULL */
984 char_u *dir, /* optional directory name */
985 char_u *fname, /* file name or NULL */
986 int bufnum, /* buffer number or zero */
987 char_u *mesg, /* message */
988 long lnum, /* line number */
989 int col, /* column */
990 int vis_col, /* using visual column */
991 char_u *pattern, /* search pattern */
992 int nr, /* error number */
993 int type, /* type character */
994 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000996 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000998 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001000 if (bufnum != 0)
1001 qfp->qf_fnum = bufnum;
1002 else
1003 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1005 {
1006 vim_free(qfp);
1007 return FAIL;
1008 }
1009 qfp->qf_lnum = lnum;
1010 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001011 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001012 if (pattern == NULL || *pattern == NUL)
1013 qfp->qf_pattern = NULL;
1014 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1015 {
1016 vim_free(qfp->qf_text);
1017 vim_free(qfp);
1018 return FAIL;
1019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 qfp->qf_nr = nr;
1021 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1022 type = 0;
1023 qfp->qf_type = type;
1024 qfp->qf_valid = valid;
1025
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001026 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1027 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001029 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 qfp->qf_prev = qfp; /* first element points to itself */
1031 }
1032 else
1033 {
1034 qfp->qf_prev = *prevp;
1035 (*prevp)->qf_next = qfp;
1036 }
1037 qfp->qf_next = qfp; /* last element points to itself */
1038 qfp->qf_cleared = FALSE;
1039 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001040 ++qi->qf_lists[qi->qf_curlist].qf_count;
1041 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1042 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001044 qi->qf_lists[qi->qf_curlist].qf_index =
1045 qi->qf_lists[qi->qf_curlist].qf_count;
1046 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048
1049 return OK;
1050}
1051
1052/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001053 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001054 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001055 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001056ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001057{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001058 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001059
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001060 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1061 if (qi != NULL)
1062 {
1063 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1064 qi->qf_refcount++;
1065 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001066
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001067 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001068}
1069
1070/*
1071 * Return the location list for window 'wp'.
1072 * If not present, allocate a location list
1073 */
1074 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001075ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001076{
1077 if (IS_LL_WINDOW(wp))
1078 /* For a location list window, use the referenced location list */
1079 return wp->w_llist_ref;
1080
1081 /*
1082 * For a non-location list window, w_llist_ref should not point to a
1083 * location list.
1084 */
1085 ll_free_all(&wp->w_llist_ref);
1086
1087 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001088 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001089 return wp->w_llist;
1090}
1091
1092/*
1093 * Copy the location list from window "from" to window "to".
1094 */
1095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001096copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001097{
1098 qf_info_T *qi;
1099 int idx;
1100 int i;
1101
1102 /*
1103 * When copying from a location list window, copy the referenced
1104 * location list. For other windows, copy the location list for
1105 * that window.
1106 */
1107 if (IS_LL_WINDOW(from))
1108 qi = from->w_llist_ref;
1109 else
1110 qi = from->w_llist;
1111
1112 if (qi == NULL) /* no location list to copy */
1113 return;
1114
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001115 /* allocate a new location list */
1116 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001117 return;
1118
1119 to->w_llist->qf_listcount = qi->qf_listcount;
1120
1121 /* Copy the location lists one at a time */
1122 for (idx = 0; idx < qi->qf_listcount; idx++)
1123 {
1124 qf_list_T *from_qfl;
1125 qf_list_T *to_qfl;
1126
1127 to->w_llist->qf_curlist = idx;
1128
1129 from_qfl = &qi->qf_lists[idx];
1130 to_qfl = &to->w_llist->qf_lists[idx];
1131
1132 /* Some of the fields are populated by qf_add_entry() */
1133 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1134 to_qfl->qf_count = 0;
1135 to_qfl->qf_index = 0;
1136 to_qfl->qf_start = NULL;
1137 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001138 if (from_qfl->qf_title != NULL)
1139 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1140 else
1141 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001142
1143 if (from_qfl->qf_count)
1144 {
1145 qfline_T *from_qfp;
1146 qfline_T *prevp = NULL;
1147
1148 /* copy all the location entries in this list */
1149 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1150 ++i, from_qfp = from_qfp->qf_next)
1151 {
1152 if (qf_add_entry(to->w_llist, &prevp,
1153 NULL,
1154 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001155 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001156 from_qfp->qf_text,
1157 from_qfp->qf_lnum,
1158 from_qfp->qf_col,
1159 from_qfp->qf_viscol,
1160 from_qfp->qf_pattern,
1161 from_qfp->qf_nr,
1162 0,
1163 from_qfp->qf_valid) == FAIL)
1164 {
1165 qf_free_all(to);
1166 return;
1167 }
1168 /*
1169 * qf_add_entry() will not set the qf_num field, as the
1170 * directory and file names are not supplied. So the qf_fnum
1171 * field is copied here.
1172 */
1173 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1174 prevp->qf_type = from_qfp->qf_type; /* error type */
1175 if (from_qfl->qf_ptr == from_qfp)
1176 to_qfl->qf_ptr = prevp; /* current location */
1177 }
1178 }
1179
1180 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1181
1182 /* When no valid entries are present in the list, qf_ptr points to
1183 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001184 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001185 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001186 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001187 to_qfl->qf_index = 1;
1188 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001189 }
1190
1191 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1192}
1193
1194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 * get buffer number for file "dir.name"
1196 */
1197 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001198qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
1200 if (fname == NULL || *fname == NUL) /* no file name */
1201 return 0;
1202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 char_u *ptr;
1204 int fnum;
1205
Bram Moolenaare60acc12011-05-10 16:41:25 +02001206#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001208#endif
1209#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (directory != NULL)
1211 slash_adjust(directory);
1212 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (directory != NULL && !vim_isAbsName(fname)
1215 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1216 {
1217 /*
1218 * Here we check if the file really exists.
1219 * This should normally be true, but if make works without
1220 * "leaving directory"-messages we might have missed a
1221 * directory change.
1222 */
1223 if (mch_getperm(ptr) < 0)
1224 {
1225 vim_free(ptr);
1226 directory = qf_guess_filepath(fname);
1227 if (directory)
1228 ptr = concat_fnames(directory, fname, TRUE);
1229 else
1230 ptr = vim_strsave(fname);
1231 }
1232 /* Use concatenated directory name and file name */
1233 fnum = buflist_add(ptr, 0);
1234 vim_free(ptr);
1235 return fnum;
1236 }
1237 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239}
1240
1241/*
1242 * push dirbuf onto the directory stack and return pointer to actual dir or
1243 * NULL on error
1244 */
1245 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001246qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 struct dir_stack_T *ds_new;
1249 struct dir_stack_T *ds_ptr;
1250
1251 /* allocate new stack element and hook it in */
1252 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1253 if (ds_new == NULL)
1254 return NULL;
1255
1256 ds_new->next = *stackptr;
1257 *stackptr = ds_new;
1258
1259 /* store directory on the stack */
1260 if (vim_isAbsName(dirbuf)
1261 || (*stackptr)->next == NULL
1262 || (*stackptr && dir_stack != *stackptr))
1263 (*stackptr)->dirname = vim_strsave(dirbuf);
1264 else
1265 {
1266 /* Okay we don't have an absolute path.
1267 * dirbuf must be a subdir of one of the directories on the stack.
1268 * Let's search...
1269 */
1270 ds_new = (*stackptr)->next;
1271 (*stackptr)->dirname = NULL;
1272 while (ds_new)
1273 {
1274 vim_free((*stackptr)->dirname);
1275 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1276 TRUE);
1277 if (mch_isdir((*stackptr)->dirname) == TRUE)
1278 break;
1279
1280 ds_new = ds_new->next;
1281 }
1282
1283 /* clean up all dirs we already left */
1284 while ((*stackptr)->next != ds_new)
1285 {
1286 ds_ptr = (*stackptr)->next;
1287 (*stackptr)->next = (*stackptr)->next->next;
1288 vim_free(ds_ptr->dirname);
1289 vim_free(ds_ptr);
1290 }
1291
1292 /* Nothing found -> it must be on top level */
1293 if (ds_new == NULL)
1294 {
1295 vim_free((*stackptr)->dirname);
1296 (*stackptr)->dirname = vim_strsave(dirbuf);
1297 }
1298 }
1299
1300 if ((*stackptr)->dirname != NULL)
1301 return (*stackptr)->dirname;
1302 else
1303 {
1304 ds_ptr = *stackptr;
1305 *stackptr = (*stackptr)->next;
1306 vim_free(ds_ptr);
1307 return NULL;
1308 }
1309}
1310
1311
1312/*
1313 * pop dirbuf from the directory stack and return previous directory or NULL if
1314 * stack is empty
1315 */
1316 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001317qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318{
1319 struct dir_stack_T *ds_ptr;
1320
1321 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1322 * What to do if it isn't? */
1323
1324 /* pop top element and free it */
1325 if (*stackptr != NULL)
1326 {
1327 ds_ptr = *stackptr;
1328 *stackptr = (*stackptr)->next;
1329 vim_free(ds_ptr->dirname);
1330 vim_free(ds_ptr);
1331 }
1332
1333 /* return NEW top element as current dir or NULL if stack is empty*/
1334 return *stackptr ? (*stackptr)->dirname : NULL;
1335}
1336
1337/*
1338 * clean up directory stack
1339 */
1340 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001341qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 struct dir_stack_T *ds_ptr;
1344
1345 while ((ds_ptr = *stackptr) != NULL)
1346 {
1347 *stackptr = (*stackptr)->next;
1348 vim_free(ds_ptr->dirname);
1349 vim_free(ds_ptr);
1350 }
1351}
1352
1353/*
1354 * Check in which directory of the directory stack the given file can be
1355 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001356 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 * Cleans up intermediate directory entries.
1358 *
1359 * TODO: How to solve the following problem?
1360 * If we have the this directory tree:
1361 * ./
1362 * ./aa
1363 * ./aa/bb
1364 * ./bb
1365 * ./bb/x.c
1366 * and make says:
1367 * making all in aa
1368 * making all in bb
1369 * x.c:9: Error
1370 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1371 * qf_guess_filepath will return NULL.
1372 */
1373 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001374qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
1376 struct dir_stack_T *ds_ptr;
1377 struct dir_stack_T *ds_tmp;
1378 char_u *fullname;
1379
1380 /* no dirs on the stack - there's nothing we can do */
1381 if (dir_stack == NULL)
1382 return NULL;
1383
1384 ds_ptr = dir_stack->next;
1385 fullname = NULL;
1386 while (ds_ptr)
1387 {
1388 vim_free(fullname);
1389 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1390
1391 /* If concat_fnames failed, just go on. The worst thing that can happen
1392 * is that we delete the entire stack.
1393 */
1394 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1395 break;
1396
1397 ds_ptr = ds_ptr->next;
1398 }
1399
1400 vim_free(fullname);
1401
1402 /* clean up all dirs we already left */
1403 while (dir_stack->next != ds_ptr)
1404 {
1405 ds_tmp = dir_stack->next;
1406 dir_stack->next = dir_stack->next->next;
1407 vim_free(ds_tmp->dirname);
1408 vim_free(ds_tmp);
1409 }
1410
1411 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1412
1413}
1414
1415/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001416 * When loading a file from the quickfix, the auto commands may modify it.
1417 * This may invalidate the current quickfix entry. This function checks
1418 * whether a entry is still present in the quickfix.
1419 * Similar to location list.
1420 */
1421 static int
1422is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1423{
1424 qf_list_T *qfl;
1425 qfline_T *qfp;
1426 int i;
1427
1428 qfl = &qi->qf_lists[qi->qf_curlist];
1429
1430 /* Search for the entry in the current list */
1431 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1432 ++i, qfp = qfp->qf_next)
1433 if (qfp == qf_ptr)
1434 break;
1435
1436 if (i == qfl->qf_count) /* Entry is not found */
1437 return FALSE;
1438
1439 return TRUE;
1440}
1441
1442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 * jump to a quickfix line
1444 * if dir == FORWARD go "errornr" valid entries forward
1445 * if dir == BACKWARD go "errornr" valid entries backward
1446 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1447 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1448 * else if "errornr" is zero, redisplay the same line
1449 * else go to entry "errornr"
1450 */
1451 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001452qf_jump(
1453 qf_info_T *qi,
1454 int dir,
1455 int errornr,
1456 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001458 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001459 qfline_T *qf_ptr;
1460 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 int qf_index;
1462 int old_qf_fnum;
1463 int old_qf_index;
1464 int prev_index;
1465 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1466 char_u *err = e_no_more_items;
1467 linenr_T i;
1468 buf_T *old_curbuf;
1469 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 colnr_T screen_col;
1471 colnr_T char_col;
1472 char_u *line;
1473#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001474 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001475 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 int opened_window = FALSE;
1477 win_T *win;
1478 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001479 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001481 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 int print_message = TRUE;
1483 int len;
1484#ifdef FEAT_FOLDING
1485 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1486#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001487 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001488 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001490 if (qi == NULL)
1491 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001492
1493 if (qi->qf_curlist >= qi->qf_listcount
1494 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 EMSG(_(e_quickfix));
1497 return;
1498 }
1499
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001500 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001502 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 old_qf_index = qf_index;
1504 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1505 {
1506 while (errornr--)
1507 {
1508 old_qf_ptr = qf_ptr;
1509 prev_index = qf_index;
1510 old_qf_fnum = qf_ptr->qf_fnum;
1511 do
1512 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001513 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 || qf_ptr->qf_next == NULL)
1515 {
1516 qf_ptr = old_qf_ptr;
1517 qf_index = prev_index;
1518 if (err != NULL)
1519 {
1520 EMSG(_(err));
1521 goto theend;
1522 }
1523 errornr = 0;
1524 break;
1525 }
1526 ++qf_index;
1527 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001528 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1529 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1531 err = NULL;
1532 }
1533 }
1534 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1535 {
1536 while (errornr--)
1537 {
1538 old_qf_ptr = qf_ptr;
1539 prev_index = qf_index;
1540 old_qf_fnum = qf_ptr->qf_fnum;
1541 do
1542 {
1543 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1544 {
1545 qf_ptr = old_qf_ptr;
1546 qf_index = prev_index;
1547 if (err != NULL)
1548 {
1549 EMSG(_(err));
1550 goto theend;
1551 }
1552 errornr = 0;
1553 break;
1554 }
1555 --qf_index;
1556 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001557 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1558 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1560 err = NULL;
1561 }
1562 }
1563 else if (errornr != 0) /* go to specified number */
1564 {
1565 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1566 {
1567 --qf_index;
1568 qf_ptr = qf_ptr->qf_prev;
1569 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001570 while (errornr > qf_index && qf_index <
1571 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 && qf_ptr->qf_next != NULL)
1573 {
1574 ++qf_index;
1575 qf_ptr = qf_ptr->qf_next;
1576 }
1577 }
1578
1579#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001580 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1581 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 /* No need to print the error message if it's visible in the error
1583 * window */
1584 print_message = FALSE;
1585
1586 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001587 * For ":helpgrep" find a help window or open one.
1588 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001589 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001590 {
1591 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001592
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001593 if (cmdmod.tab != 0)
1594 wp = NULL;
1595 else
1596 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1597 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1598 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001599 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1600 win_enter(wp, TRUE);
1601 else
1602 {
1603 /*
1604 * Split off help window; put it at far top if no position
1605 * specified, the current window is vertically split and narrow.
1606 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001607 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001608 if (cmdmod.split == 0 && curwin->w_width != Columns
1609 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001610 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001611 if (qi != &ql_info)
1612 flags |= WSP_NEWLOC; /* don't copy the location list */
1613
1614 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001615 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001616 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001617
1618 if (curwin->w_height < p_hh)
1619 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001620
1621 if (qi != &ql_info) /* not a quickfix list */
1622 {
1623 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001624 curwin->w_llist = qi;
1625 qi->qf_refcount++;
1626 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001627 }
1628
1629 if (!p_im)
1630 restart_edit = 0; /* don't want insert mode in help file */
1631 }
1632
1633 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 * If currently in the quickfix window, find another window to show the
1635 * file in.
1636 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001637 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001639 win_T *usable_win_ptr = NULL;
1640
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 /*
1642 * If there is no file specified, we don't know where to go.
1643 * But do advance, otherwise ":cn" gets stuck.
1644 */
1645 if (qf_ptr->qf_fnum == 0)
1646 goto theend;
1647
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001648 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001649
1650 ll_ref = curwin->w_llist_ref;
1651 if (ll_ref != NULL)
1652 {
1653 /* Find a window using the same location list that is not a
1654 * quickfix window. */
1655 FOR_ALL_WINDOWS(usable_win_ptr)
1656 if (usable_win_ptr->w_llist == ll_ref
1657 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001658 {
1659 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001660 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001661 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001662 }
1663
1664 if (!usable_win)
1665 {
1666 /* Locate a window showing a normal buffer */
1667 FOR_ALL_WINDOWS(win)
1668 if (win->w_buffer->b_p_bt[0] == NUL)
1669 {
1670 usable_win = 1;
1671 break;
1672 }
1673 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001676 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001677 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001679 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001680 {
1681 tabpage_T *tp;
1682 win_T *wp;
1683
1684 FOR_ALL_TAB_WINDOWS(tp, wp)
1685 {
1686 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1687 {
1688 goto_tabpage_win(tp, wp);
1689 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001690 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001691 }
1692 }
1693 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001694win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001695
1696 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001697 * If there is only one window and it is the quickfix window, create a
1698 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001699 */
1700 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001702 flags = WSP_ABOVE;
1703 if (ll_ref != NULL)
1704 flags |= WSP_NEWLOC;
1705 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 goto failed; /* not enough room for window */
1707 opened_window = TRUE; /* close it when fail */
1708 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001709 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001710 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001711 if (ll_ref != NULL)
1712 {
1713 /* The new window should use the location list from the
1714 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001715 curwin->w_llist = ll_ref;
1716 ll_ref->qf_refcount++;
1717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719 else
1720 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001721 if (curwin->w_llist_ref != NULL)
1722 {
1723 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001724 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001725 if (win == NULL)
1726 {
1727 /* Find the window showing the selected file */
1728 FOR_ALL_WINDOWS(win)
1729 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1730 break;
1731 if (win == NULL)
1732 {
1733 /* Find a previous usable window */
1734 win = curwin;
1735 do
1736 {
1737 if (win->w_buffer->b_p_bt[0] == NUL)
1738 break;
1739 if (win->w_prev == NULL)
1740 win = lastwin; /* wrap around the top */
1741 else
1742 win = win->w_prev; /* go to previous window */
1743 } while (win != curwin);
1744 }
1745 }
1746 win_goto(win);
1747
1748 /* If the location list for the window is not set, then set it
1749 * to the location list from the location window */
1750 if (win->w_llist == NULL)
1751 {
1752 win->w_llist = ll_ref;
1753 ll_ref->qf_refcount++;
1754 }
1755 }
1756 else
1757 {
1758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 /*
1760 * Try to find a window that shows the right buffer.
1761 * Default to the window just above the quickfix buffer.
1762 */
1763 win = curwin;
1764 altwin = NULL;
1765 for (;;)
1766 {
1767 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1768 break;
1769 if (win->w_prev == NULL)
1770 win = lastwin; /* wrap around the top */
1771 else
1772 win = win->w_prev; /* go to previous window */
1773
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001774 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
1776 /* Didn't find it, go to the window before the quickfix
1777 * window. */
1778 if (altwin != NULL)
1779 win = altwin;
1780 else if (curwin->w_prev != NULL)
1781 win = curwin->w_prev;
1782 else
1783 win = curwin->w_next;
1784 break;
1785 }
1786
1787 /* Remember a usable window. */
1788 if (altwin == NULL && !win->w_p_pvw
1789 && win->w_buffer->b_p_bt[0] == NUL)
1790 altwin = win;
1791 }
1792
1793 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
1796 }
1797#endif
1798
1799 /*
1800 * If there is a file name,
1801 * read the wanted file if needed, and check autowrite etc.
1802 */
1803 old_curbuf = curbuf;
1804 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001805
1806 if (qf_ptr->qf_fnum != 0)
1807 {
1808 if (qf_ptr->qf_type == 1)
1809 {
1810 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1811 * set b_p_ro flag). */
1812 if (!can_abandon(curbuf, forceit))
1813 {
1814 EMSG(_(e_nowrtmsg));
1815 ok = FALSE;
1816 }
1817 else
1818 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001819 ECMD_HIDE + ECMD_SET_HELP,
1820 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001821 }
1822 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01001823 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001824 int old_qf_curlist = qi->qf_curlist;
1825 int is_abort = FALSE;
1826
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001827 ok = buflist_getfile(qf_ptr->qf_fnum,
1828 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01001829 if (qi != &ql_info && !win_valid(oldwin))
1830 {
1831 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001832 is_abort = TRUE;
1833 opened_window = FALSE;
1834 }
1835 else if (old_qf_curlist != qi->qf_curlist
1836 || !is_qf_entry_present(qi, qf_ptr))
1837 {
1838 if (qi == &ql_info)
1839 EMSG(_("E925: Current quickfix was changed"));
1840 else
1841 EMSG(_("E926: Current location list was changed"));
1842 is_abort = TRUE;
1843 }
1844
1845 if (is_abort)
1846 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01001847 ok = FALSE;
1848 qi = NULL;
1849 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01001850 }
1851 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001852 }
1853
1854 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
1856 /* When not switched to another buffer, still need to set pc mark */
1857 if (curbuf == old_curbuf)
1858 setpcmark();
1859
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001860 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001862 /*
1863 * Go to line with error, unless qf_lnum is 0.
1864 */
1865 i = qf_ptr->qf_lnum;
1866 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001868 if (i > curbuf->b_ml.ml_line_count)
1869 i = curbuf->b_ml.ml_line_count;
1870 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001872 if (qf_ptr->qf_col > 0)
1873 {
1874 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02001875#ifdef FEAT_VIRTUALEDIT
1876 curwin->w_cursor.coladd = 0;
1877#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001878 if (qf_ptr->qf_viscol == TRUE)
1879 {
1880 /*
1881 * Check each character from the beginning of the error
1882 * line up to the error column. For each tab character
1883 * found, reduce the error column value by the length of
1884 * a tab character.
1885 */
1886 line = ml_get_curline();
1887 screen_col = 0;
1888 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1889 {
1890 if (*line == NUL)
1891 break;
1892 if (*line++ == '\t')
1893 {
1894 curwin->w_cursor.col -= 7 - (screen_col % 8);
1895 screen_col += 8 - (screen_col % 8);
1896 }
1897 else
1898 ++screen_col;
1899 }
1900 }
1901 check_cursor();
1902 }
1903 else
1904 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001907 {
1908 pos_T save_cursor;
1909
1910 /* Move the cursor to the first line in the buffer */
1911 save_cursor = curwin->w_cursor;
1912 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001913 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1914 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001915 curwin->w_cursor = save_cursor;
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
1918#ifdef FEAT_FOLDING
1919 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1920 foldOpenCursor();
1921#endif
1922 if (print_message)
1923 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001924 /* Update the screen before showing the message, unless the screen
1925 * scrolled up. */
1926 if (!msg_scrolled)
1927 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001929 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1931 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1932 /* Add the message, skipping leading whitespace and newlines. */
1933 len = (int)STRLEN(IObuff);
1934 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1935
1936 /* Output the message. Overwrite to avoid scrolling when the 'O'
1937 * flag is present in 'shortmess'; But when not jumping, print the
1938 * whole message. */
1939 i = msg_scroll;
1940 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1941 msg_scroll = TRUE;
1942 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1943 msg_scroll = FALSE;
1944 msg_attr_keep(IObuff, 0, TRUE);
1945 msg_scroll = i;
1946 }
1947 }
1948 else
1949 {
1950#ifdef FEAT_WINDOWS
1951 if (opened_window)
1952 win_close(curwin, TRUE); /* Close opened window */
1953#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01001954 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 /*
1957 * Couldn't open file, so put index back where it was. This could
1958 * happen if the file was readonly and we changed something.
1959 */
1960#ifdef FEAT_WINDOWS
1961failed:
1962#endif
1963 qf_ptr = old_qf_ptr;
1964 qf_index = old_qf_index;
1965 }
1966 }
1967theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01001968 if (qi != NULL)
1969 {
1970 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1971 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973#ifdef FEAT_WINDOWS
1974 if (p_swb != old_swb && opened_window)
1975 {
1976 /* Restore old 'switchbuf' value, but not when an autocommand or
1977 * modeline has changed the value. */
1978 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001981 swb_flags = old_swb_flags;
1982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 else
1984 free_string_option(old_swb);
1985 }
1986#endif
1987}
1988
1989/*
1990 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001991 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 */
1993 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001994qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001996 buf_T *buf;
1997 char_u *fname;
1998 qfline_T *qfp;
1999 int i;
2000 int idx1 = 1;
2001 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002002 char_u *arg = eap->arg;
2003 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002005 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002007 if (eap->cmdidx == CMD_llist)
2008 {
2009 qi = GET_LOC_LIST(curwin);
2010 if (qi == NULL)
2011 {
2012 EMSG(_(e_loclist));
2013 return;
2014 }
2015 }
2016
2017 if (qi->qf_curlist >= qi->qf_listcount
2018 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
2020 EMSG(_(e_quickfix));
2021 return;
2022 }
2023 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2024 {
2025 EMSG(_(e_trailing));
2026 return;
2027 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002028 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (idx1 < 0)
2030 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2031 if (idx2 < 0)
2032 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2033
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002034 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002036 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2037 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2040 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002041 msg_putchar('\n');
2042 if (got_int)
2043 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002044
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002045 fname = NULL;
2046 if (qfp->qf_fnum != 0
2047 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2048 {
2049 fname = buf->b_fname;
2050 if (qfp->qf_type == 1) /* :helpgrep */
2051 fname = gettail(fname);
2052 }
2053 if (fname == NULL)
2054 sprintf((char *)IObuff, "%2d", i);
2055 else
2056 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2057 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002059 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2060 if (qfp->qf_lnum == 0)
2061 IObuff[0] = NUL;
2062 else if (qfp->qf_col == 0)
2063 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2064 else
2065 sprintf((char *)IObuff, ":%ld col %d",
2066 qfp->qf_lnum, qfp->qf_col);
2067 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2068 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2069 msg_puts_attr(IObuff, hl_attr(HLF_N));
2070 if (qfp->qf_pattern != NULL)
2071 {
2072 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2073 STRCAT(IObuff, ":");
2074 msg_puts(IObuff);
2075 }
2076 msg_puts((char_u *)" ");
2077
2078 /* Remove newlines and leading whitespace from the text. For an
2079 * unrecognized line keep the indent, the compiler may mark a word
2080 * with ^^^^. */
2081 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2083 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002084 msg_prt_line(IObuff, FALSE);
2085 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002087
2088 qfp = qfp->qf_next;
2089 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 ui_breakcheck();
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092}
2093
2094/*
2095 * Remove newlines and leading whitespace from an error message.
2096 * Put the result in "buf[bufsize]".
2097 */
2098 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002099qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100{
2101 int i;
2102 char_u *p = text;
2103
2104 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2105 {
2106 if (*p == '\n')
2107 {
2108 buf[i] = ' ';
2109 while (*++p != NUL)
2110 if (!vim_iswhite(*p) && *p != '\n')
2111 break;
2112 }
2113 else
2114 buf[i] = *p++;
2115 }
2116 buf[i] = NUL;
2117}
2118
2119/*
2120 * ":colder [count]": Up in the quickfix stack.
2121 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122 * ":lolder [count]": Up in the location list stack.
2123 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 */
2125 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002126qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002128 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 int count;
2130
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2132 {
2133 qi = GET_LOC_LIST(curwin);
2134 if (qi == NULL)
2135 {
2136 EMSG(_(e_loclist));
2137 return;
2138 }
2139 }
2140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (eap->addr_count != 0)
2142 count = eap->line2;
2143 else
2144 count = 1;
2145 while (count--)
2146 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002147 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002152 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002154 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 else
2157 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
2160 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002161 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002166 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167}
2168
2169 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002170qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002173 qi->qf_curlist + 1, qi->qf_listcount,
2174 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002176 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#endif
2178}
2179
2180/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002181 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 */
2183 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002184qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002186 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002187 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002189 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002191 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002192 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002193 {
2194 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002195 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002196 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2197 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002198 if (stop)
2199 /* Somehow qf_count may have an incorrect value, set it to 1
2200 * to avoid crashing when it's wrong.
2201 * TODO: Avoid qf_count being incorrect. */
2202 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002203 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002204 qi->qf_lists[idx].qf_start = qfp;
2205 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002207 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002208 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002209 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210}
2211
2212/*
2213 * qf_mark_adjust: adjust marks
2214 */
2215 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002216qf_mark_adjust(
2217 win_T *wp,
2218 linenr_T line1,
2219 linenr_T line2,
2220 long amount,
2221 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002223 int i;
2224 qfline_T *qfp;
2225 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002226 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002228 if (wp != NULL)
2229 {
2230 if (wp->w_llist == NULL)
2231 return;
2232 qi = wp->w_llist;
2233 }
2234
2235 for (idx = 0; idx < qi->qf_listcount; ++idx)
2236 if (qi->qf_lists[idx].qf_count)
2237 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2238 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (qfp->qf_fnum == curbuf->b_fnum)
2240 {
2241 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2242 {
2243 if (amount == MAXLNUM)
2244 qfp->qf_cleared = TRUE;
2245 else
2246 qfp->qf_lnum += amount;
2247 }
2248 else if (amount_after && qfp->qf_lnum > line2)
2249 qfp->qf_lnum += amount_after;
2250 }
2251}
2252
2253/*
2254 * Make a nice message out of the error character and the error number:
2255 * char number message
2256 * e or E 0 " error"
2257 * w or W 0 " warning"
2258 * i or I 0 " info"
2259 * 0 0 ""
2260 * other 0 " c"
2261 * e or E n " error n"
2262 * w or W n " warning n"
2263 * i or I n " info n"
2264 * 0 n " error n"
2265 * other n " c n"
2266 * 1 x "" :helpgrep
2267 */
2268 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002269qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
2271 static char_u buf[20];
2272 static char_u cc[3];
2273 char_u *p;
2274
2275 if (c == 'W' || c == 'w')
2276 p = (char_u *)" warning";
2277 else if (c == 'I' || c == 'i')
2278 p = (char_u *)" info";
2279 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2280 p = (char_u *)" error";
2281 else if (c == 0 || c == 1)
2282 p = (char_u *)"";
2283 else
2284 {
2285 cc[0] = ' ';
2286 cc[1] = c;
2287 cc[2] = NUL;
2288 p = cc;
2289 }
2290
2291 if (nr <= 0)
2292 return p;
2293
2294 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2295 return buf;
2296}
2297
2298#if defined(FEAT_WINDOWS) || defined(PROTO)
2299/*
2300 * ":cwindow": open the quickfix window if we have errors to display,
2301 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002302 * ":lwindow": open the location list window if we have locations to display,
2303 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 */
2305 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002306ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002308 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 win_T *win;
2310
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002311 if (eap->cmdidx == CMD_lwindow)
2312 {
2313 qi = GET_LOC_LIST(curwin);
2314 if (qi == NULL)
2315 return;
2316 }
2317
2318 /* Look for an existing quickfix window. */
2319 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321 /*
2322 * If a quickfix window is open but we have no errors to display,
2323 * close the window. If a quickfix window is not open, then open
2324 * it if we have errors; otherwise, leave it closed.
2325 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002326 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002327 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002328 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 if (win != NULL)
2331 ex_cclose(eap);
2332 }
2333 else if (win == NULL)
2334 ex_copen(eap);
2335}
2336
2337/*
2338 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002342ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002344 win_T *win = NULL;
2345 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2348 {
2349 qi = GET_LOC_LIST(curwin);
2350 if (qi == NULL)
2351 return;
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002354 /* Find existing quickfix window and close it. */
2355 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 if (win != NULL)
2357 win_close(win, FALSE);
2358}
2359
2360/*
2361 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002362 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 */
2364 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002365ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002370 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002371 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002372 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002374 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2375 {
2376 qi = GET_LOC_LIST(curwin);
2377 if (qi == NULL)
2378 {
2379 EMSG(_(e_loclist));
2380 return;
2381 }
2382 }
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (eap->addr_count != 0)
2385 height = eap->line2;
2386 else
2387 height = QF_WINHEIGHT;
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390#ifdef FEAT_GUI
2391 need_mouse_correct = TRUE;
2392#endif
2393
2394 /*
2395 * Find existing quickfix window, or open a new one.
2396 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002397 win = qf_find_win(qi);
2398
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002399 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002402 if (eap->addr_count != 0)
2403 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002404 if (cmdmod.split & WSP_VERT)
2405 {
2406 if (height != W_WIDTH(win))
2407 win_setwidth(height);
2408 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002409 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002410 win_setheight(height);
2411 }
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 else
2414 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002415 qf_buf = qf_find_buf(qi);
2416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 /* The current window becomes the previous window afterwards. */
2418 win = curwin;
2419
Bram Moolenaar77642c02012-11-20 17:55:10 +01002420 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2421 && cmdmod.split == 0)
2422 /* Create the new window at the very bottom, except when
2423 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002424 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002425 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002427 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002429 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002431 /*
2432 * For the location list window, create a reference to the
2433 * location list from the window 'win'.
2434 */
2435 curwin->w_llist_ref = win->w_llist;
2436 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002438
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002439 if (oldwin != curwin)
2440 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002441 if (qf_buf != NULL)
2442 /* Use the existing quickfix buffer */
2443 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002444 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002445 else
2446 {
2447 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002448 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002449 /* switch off 'swapfile' */
2450 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2451 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002452 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002453 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002454 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002455#ifdef FEAT_DIFF
2456 curwin->w_p_diff = FALSE;
2457#endif
2458#ifdef FEAT_FOLDING
2459 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2460 OPT_LOCAL);
2461#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002464 /* Only set the height when still in the same tab page and there is no
2465 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002466 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 win_setheight(height);
2468 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2469 if (win_valid(win))
2470 prevwin = win;
2471 }
2472
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002473 qf_set_title_var(qi);
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 /*
2476 * Fill the buffer with the quickfix list.
2477 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002478 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002480 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 curwin->w_cursor.col = 0;
2482 check_cursor();
2483 update_topline(); /* scroll to show the line */
2484}
2485
2486/*
2487 * Return the number of the current entry (line number in the quickfix
2488 * window).
2489 */
2490 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002491qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002493 qf_info_T *qi = &ql_info;
2494
2495 if (IS_LL_WINDOW(wp))
2496 /* In the location list window, use the referenced location list */
2497 qi = wp->w_llist_ref;
2498
2499 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501
2502/*
2503 * Update the cursor position in the quickfix window to the current error.
2504 * Return TRUE if there is a quickfix window.
2505 */
2506 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002507qf_win_pos_update(
2508 qf_info_T *qi,
2509 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002512 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
2514 /*
2515 * Put the cursor on the current error in the quickfix window, so that
2516 * it's viewable.
2517 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002518 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (win != NULL
2520 && qf_index <= win->w_buffer->b_ml.ml_line_count
2521 && old_qf_index != qf_index)
2522 {
2523 win_T *old_curwin = curwin;
2524
2525 curwin = win;
2526 curbuf = win->w_buffer;
2527 if (qf_index > old_qf_index)
2528 {
2529 curwin->w_redraw_top = old_qf_index;
2530 curwin->w_redraw_bot = qf_index;
2531 }
2532 else
2533 {
2534 curwin->w_redraw_top = qf_index;
2535 curwin->w_redraw_bot = old_qf_index;
2536 }
2537 curwin->w_cursor.lnum = qf_index;
2538 curwin->w_cursor.col = 0;
2539 update_topline(); /* scroll to show the line */
2540 redraw_later(VALID);
2541 curwin->w_redr_status = TRUE; /* update ruler */
2542 curwin = old_curwin;
2543 curbuf = curwin->w_buffer;
2544 }
2545 return win != NULL;
2546}
2547
2548/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002549 * Check whether the given window is displaying the specified quickfix/location
2550 * list buffer
2551 */
2552 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002553is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002554{
2555 /*
2556 * A window displaying the quickfix buffer will have the w_llist_ref field
2557 * set to NULL.
2558 * A window displaying a location list buffer will have the w_llist_ref
2559 * pointing to the location list.
2560 */
2561 if (bt_quickfix(win->w_buffer))
2562 if ((qi == &ql_info && win->w_llist_ref == NULL)
2563 || (qi != &ql_info && win->w_llist_ref == qi))
2564 return TRUE;
2565
2566 return FALSE;
2567}
2568
2569/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002570 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002571 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002572 */
2573 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002574qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002575{
2576 win_T *win;
2577
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002578 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002579 if (is_qf_win(win, qi))
2580 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002581
2582 return win;
2583}
2584
2585/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002586 * Find a quickfix buffer.
2587 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 */
2589 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002590qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002592 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002593 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
Bram Moolenaar9c102382006-05-03 21:26:49 +00002595 FOR_ALL_TAB_WINDOWS(tp, win)
2596 if (is_qf_win(win, qi))
2597 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002598
Bram Moolenaar9c102382006-05-03 21:26:49 +00002599 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600}
2601
2602/*
2603 * Find the quickfix buffer. If it exists, update the contents.
2604 */
2605 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002606qf_update_buffer(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002609 win_T *win;
2610 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002614 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (buf != NULL)
2616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 /* set curwin/curbuf to buf and save a few things */
2618 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002620 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002621 {
2622 curwin_save = curwin;
2623 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002624 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002625 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002626 }
2627
Bram Moolenaar6920c722016-01-22 22:44:10 +01002628 qf_fill_buffer(qi);
2629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 /* restore curwin/curbuf and a few other things */
2631 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002633 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
2635}
2636
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002637/*
2638 * Set "w:quickfix_title" if "qi" has a title.
2639 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002640 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002641qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002642{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002643 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2644 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002645 qi->qf_lists[qi->qf_curlist].qf_title);
2646}
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648/*
2649 * Fill current buffer with quickfix errors, replacing any previous contents.
2650 * curbuf must be the quickfix buffer!
2651 */
2652 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002653qf_fill_buffer(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002655 linenr_T lnum;
2656 qfline_T *qfp;
2657 buf_T *errbuf;
2658 int len;
2659 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 /* delete all existing lines */
2662 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2663 (void)ml_delete((linenr_T)1, FALSE);
2664
2665 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002666 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
2668 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002669 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2670 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
2672 if (qfp->qf_fnum != 0
2673 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2674 && errbuf->b_fname != NULL)
2675 {
2676 if (qfp->qf_type == 1) /* :helpgrep */
2677 STRCPY(IObuff, gettail(errbuf->b_fname));
2678 else
2679 STRCPY(IObuff, errbuf->b_fname);
2680 len = (int)STRLEN(IObuff);
2681 }
2682 else
2683 len = 0;
2684 IObuff[len++] = '|';
2685
2686 if (qfp->qf_lnum > 0)
2687 {
2688 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2689 len += (int)STRLEN(IObuff + len);
2690
2691 if (qfp->qf_col > 0)
2692 {
2693 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2694 len += (int)STRLEN(IObuff + len);
2695 }
2696
2697 sprintf((char *)IObuff + len, "%s",
2698 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2699 len += (int)STRLEN(IObuff + len);
2700 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002701 else if (qfp->qf_pattern != NULL)
2702 {
2703 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2704 len += (int)STRLEN(IObuff + len);
2705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 IObuff[len++] = '|';
2707 IObuff[len++] = ' ';
2708
2709 /* Remove newlines and leading whitespace from the text.
2710 * For an unrecognized line keep the indent, the compiler may
2711 * mark a word with ^^^^. */
2712 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2713 IObuff + len, IOSIZE - len);
2714
2715 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2716 == FAIL)
2717 break;
2718 qfp = qfp->qf_next;
2719 }
2720 /* Delete the empty line which is now at the end */
2721 (void)ml_delete(lnum + 1, FALSE);
2722 }
2723
2724 /* correct cursor position */
2725 check_lnums(TRUE);
2726
2727 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2728 * resembles reading a file into a buffer, it's more logical when using
2729 * autocommands. */
2730 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2731 curbuf->b_p_ma = FALSE;
2732
2733#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002734 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2736 FALSE, curbuf);
2737 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2738 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002739 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740#endif
2741
2742 /* make sure it will be redrawn */
2743 redraw_curbuf_later(NOT_VALID);
2744
2745 /* Restore KeyTyped, setting 'filetype' may reset it. */
2746 KeyTyped = old_KeyTyped;
2747}
2748
2749#endif /* FEAT_WINDOWS */
2750
2751/*
2752 * Return TRUE if "buf" is the quickfix buffer.
2753 */
2754 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002755bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002757 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758}
2759
2760/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002761 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2762 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 */
2764 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002765bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002767 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2768 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769}
2770
2771/*
2772 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2773 */
2774 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002775bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002777 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778}
2779
2780 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002781bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
2783 if (bt_dontwrite(buf))
2784 {
2785 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2786 return TRUE;
2787 }
2788 return FALSE;
2789}
2790
2791/*
2792 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2793 * and 'bufhidden'.
2794 */
2795 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002796buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2799 switch (buf->b_p_bh[0])
2800 {
2801 case 'u': /* "unload" */
2802 case 'w': /* "wipe" */
2803 case 'd': return FALSE; /* "delete" */
2804 case 'h': return TRUE; /* "hide" */
2805 }
2806 return (p_hid || cmdmod.hide);
2807}
2808
2809/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002810 * Return TRUE when using ":vimgrep" for ":grep".
2811 */
2812 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002813grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002814{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002815 return ((cmdidx == CMD_grep
2816 || cmdidx == CMD_lgrep
2817 || cmdidx == CMD_grepadd
2818 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002819 && STRCMP("internal",
2820 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2821}
2822
2823/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002824 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 */
2826 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002827ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002829 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 char_u *cmd;
2831 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002832 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002833 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002834 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002835#ifdef FEAT_AUTOCMD
2836 char_u *au_name = NULL;
2837
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002838 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2839 if (grep_internal(eap->cmdidx))
2840 {
2841 ex_vimgrep(eap);
2842 return;
2843 }
2844
Bram Moolenaar7c626922005-02-07 22:01:03 +00002845 switch (eap->cmdidx)
2846 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002847 case CMD_make: au_name = (char_u *)"make"; break;
2848 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2849 case CMD_grep: au_name = (char_u *)"grep"; break;
2850 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2851 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2852 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002853 default: break;
2854 }
2855 if (au_name != NULL)
2856 {
2857 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2858 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002859# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002860 if (did_throw || force_abort)
2861 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002862# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002863 }
2864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
Bram Moolenaara6557602006-02-04 22:43:20 +00002866 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2867 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002868 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002869
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002871 fname = get_mef_name();
2872 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002874 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
2876 /*
2877 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2878 */
2879 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2880 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002881 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 cmd = alloc(len);
2883 if (cmd == NULL)
2884 return;
2885 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2886 (char *)p_shq);
2887 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002888 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 /*
2890 * Output a newline if there's something else than the :make command that
2891 * was typed (in which case the cursor is in column 0).
2892 */
2893 if (msg_col == 0)
2894 msg_didout = FALSE;
2895 msg_start();
2896 MSG_PUTS(":!");
2897 msg_outtrans(cmd); /* show what we are doing */
2898
2899 /* let the shell know if we are redirecting output or not */
2900 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2901
2902#ifdef AMIGA
2903 out_flush();
2904 /* read window status report and redraw before message */
2905 (void)char_avail();
2906#endif
2907
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002908 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002909 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2910 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002911 && eap->cmdidx != CMD_lgrepadd),
2912 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002913 if (wp != NULL)
2914 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002915#ifdef FEAT_AUTOCMD
2916 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002917 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002918 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2919 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002920 if (qi->qf_curlist < qi->qf_listcount)
2921 res = qi->qf_lists[qi->qf_curlist].qf_count;
2922 else
2923 res = 0;
2924 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002925#endif
2926 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002927 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaar7c626922005-02-07 22:01:03 +00002929 mch_remove(fname);
2930 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 vim_free(cmd);
2932}
2933
2934/*
2935 * Return the name for the errorfile, in allocated memory.
2936 * Find a new unique name when 'makeef' contains "##".
2937 * Returns NULL for error.
2938 */
2939 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002940get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
2942 char_u *p;
2943 char_u *name;
2944 static int start = -1;
2945 static int off = 0;
2946#ifdef HAVE_LSTAT
2947 struct stat sb;
2948#endif
2949
2950 if (*p_mef == NUL)
2951 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002952 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 if (name == NULL)
2954 EMSG(_(e_notmp));
2955 return name;
2956 }
2957
2958 for (p = p_mef; *p; ++p)
2959 if (p[0] == '#' && p[1] == '#')
2960 break;
2961
2962 if (*p == NUL)
2963 return vim_strsave(p_mef);
2964
2965 /* Keep trying until the name doesn't exist yet. */
2966 for (;;)
2967 {
2968 if (start == -1)
2969 start = mch_get_pid();
2970 else
2971 off += 19;
2972
2973 name = alloc((unsigned)STRLEN(p_mef) + 30);
2974 if (name == NULL)
2975 break;
2976 STRCPY(name, p_mef);
2977 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2978 STRCAT(name, p + 2);
2979 if (mch_getperm(name) < 0
2980#ifdef HAVE_LSTAT
2981 /* Don't accept a symbolic link, its a security risk. */
2982 && mch_lstat((char *)name, &sb) < 0
2983#endif
2984 )
2985 break;
2986 vim_free(name);
2987 }
2988 return name;
2989}
2990
2991/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002992 * Returns the number of valid entries in the current quickfix/location list.
2993 */
2994 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002995qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002996{
2997 qf_info_T *qi = &ql_info;
2998 qfline_T *qfp;
2999 int i, sz = 0;
3000 int prev_fnum = 0;
3001
3002 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3003 {
3004 /* Location list */
3005 qi = GET_LOC_LIST(curwin);
3006 if (qi == NULL)
3007 return 0;
3008 }
3009
3010 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3011 (i < qi->qf_lists[qi->qf_curlist].qf_count) && (qfp != NULL);
3012 ++i, qfp = qfp->qf_next)
3013 {
3014 if (qfp->qf_valid)
3015 {
3016 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3017 sz++; /* Count all valid entries */
3018 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3019 {
3020 /* Count the number of files */
3021 sz++;
3022 prev_fnum = qfp->qf_fnum;
3023 }
3024 }
3025 }
3026
3027 return sz;
3028}
3029
3030/*
3031 * Returns the current index of the quickfix/location list.
3032 * Returns 0 if there is an error.
3033 */
3034 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003035qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003036{
3037 qf_info_T *qi = &ql_info;
3038
3039 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3040 {
3041 /* Location list */
3042 qi = GET_LOC_LIST(curwin);
3043 if (qi == NULL)
3044 return 0;
3045 }
3046
3047 return qi->qf_lists[qi->qf_curlist].qf_index;
3048}
3049
3050/*
3051 * Returns the current index in the quickfix/location list (counting only valid
3052 * entries). If no valid entries are in the list, then returns 1.
3053 */
3054 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003055qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003056{
3057 qf_info_T *qi = &ql_info;
3058 qf_list_T *qfl;
3059 qfline_T *qfp;
3060 int i, eidx = 0;
3061 int prev_fnum = 0;
3062
3063 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3064 {
3065 /* Location list */
3066 qi = GET_LOC_LIST(curwin);
3067 if (qi == NULL)
3068 return 1;
3069 }
3070
3071 qfl = &qi->qf_lists[qi->qf_curlist];
3072 qfp = qfl->qf_start;
3073
3074 /* check if the list has valid errors */
3075 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3076 return 1;
3077
3078 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3079 {
3080 if (qfp->qf_valid)
3081 {
3082 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3083 {
3084 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3085 {
3086 /* Count the number of files */
3087 eidx++;
3088 prev_fnum = qfp->qf_fnum;
3089 }
3090 }
3091 else
3092 eidx++;
3093 }
3094 }
3095
3096 return eidx ? eidx : 1;
3097}
3098
3099/*
3100 * Get the 'n'th valid error entry in the quickfix or location list.
3101 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3102 * For :cdo and :ldo returns the 'n'th valid error entry.
3103 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3104 */
3105 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003106qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003107{
3108 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3109 qfline_T *qfp = qfl->qf_start;
3110 int i, eidx;
3111 int prev_fnum = 0;
3112
3113 /* check if the list has valid errors */
3114 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3115 return 1;
3116
3117 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp!= NULL;
3118 i++, qfp = qfp->qf_next)
3119 {
3120 if (qfp->qf_valid)
3121 {
3122 if (fdo)
3123 {
3124 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3125 {
3126 /* Count the number of files */
3127 eidx++;
3128 prev_fnum = qfp->qf_fnum;
3129 }
3130 }
3131 else
3132 eidx++;
3133 }
3134
3135 if (eidx == n)
3136 break;
3137 }
3138
3139 if (i <= qfl->qf_count)
3140 return i;
3141 else
3142 return 1;
3143}
3144
3145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003147 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003148 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 */
3150 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003151ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003153 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003154 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003155
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003156 if (eap->cmdidx == CMD_ll
3157 || eap->cmdidx == CMD_lrewind
3158 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003159 || eap->cmdidx == CMD_llast
3160 || eap->cmdidx == CMD_ldo
3161 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003162 {
3163 qi = GET_LOC_LIST(curwin);
3164 if (qi == NULL)
3165 {
3166 EMSG(_(e_loclist));
3167 return;
3168 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003169 }
3170
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003171 if (eap->addr_count > 0)
3172 errornr = (int)eap->line2;
3173 else
3174 {
3175 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3176 errornr = 0;
3177 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3178 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3179 errornr = 1;
3180 else
3181 errornr = 32767;
3182 }
3183
3184 /* For cdo and ldo commands, jump to the nth valid error.
3185 * For cfdo and lfdo commands, jump to the nth valid file entry.
3186 */
3187 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3188 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3189 errornr = qf_get_nth_valid_entry(qi,
3190 eap->addr_count > 0 ? (int)eap->line1 : 1,
3191 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3192
3193 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194}
3195
3196/*
3197 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003198 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003199 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 */
3201 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003202ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003204 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003205 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003206
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003207 if (eap->cmdidx == CMD_lnext
3208 || eap->cmdidx == CMD_lNext
3209 || eap->cmdidx == CMD_lprevious
3210 || eap->cmdidx == CMD_lnfile
3211 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003212 || eap->cmdidx == CMD_lpfile
3213 || eap->cmdidx == CMD_ldo
3214 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003215 {
3216 qi = GET_LOC_LIST(curwin);
3217 if (qi == NULL)
3218 {
3219 EMSG(_(e_loclist));
3220 return;
3221 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003222 }
3223
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003224 if (eap->addr_count > 0 &&
3225 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3226 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3227 errornr = (int)eap->line2;
3228 else
3229 errornr = 1;
3230
3231 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3232 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003234 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3235 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003237 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3238 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 ? BACKWARD_FILE
3240 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003241 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242}
3243
3244/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003245 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003246 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 */
3248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003249ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003251 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003252 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003253#ifdef FEAT_AUTOCMD
3254 char_u *au_name = NULL;
3255#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003256
3257 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003258 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003259 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003260
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003261#ifdef FEAT_AUTOCMD
3262 switch (eap->cmdidx)
3263 {
3264 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3265 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3266 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3267 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3268 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3269 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3270 default: break;
3271 }
3272 if (au_name != NULL)
3273 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3274#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003275#ifdef FEAT_BROWSE
3276 if (cmdmod.browse)
3277 {
3278 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3279 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3280 if (browse_file == NULL)
3281 return;
3282 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3283 vim_free(browse_file);
3284 }
3285 else
3286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003288 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003289
3290 /*
3291 * This function is used by the :cfile, :cgetfile and :caddfile
3292 * commands.
3293 * :cfile always creates a new quickfix list and jumps to the
3294 * first error.
3295 * :cgetfile creates a new quickfix list but doesn't jump to the
3296 * first error.
3297 * :caddfile adds to an existing quickfix list. If there is no
3298 * quickfix list then a new list is created.
3299 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003300 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003301 && eap->cmdidx != CMD_laddfile),
3302 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003303 && (eap->cmdidx == CMD_cfile
3304 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003305 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003306#ifdef FEAT_AUTOCMD
3307 if (au_name != NULL)
3308 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3309#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003310 if (wp != NULL)
3311 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003312 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003313 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003314
3315 else
3316 {
3317#ifdef FEAT_AUTOCMD
3318 if (au_name != NULL)
3319 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3320#endif
3321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322}
3323
3324/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003325 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003326 * ":vimgrepadd {pattern} file(s)"
3327 * ":lvimgrep {pattern} file(s)"
3328 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003329 */
3330 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003331ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003332{
Bram Moolenaar81695252004-12-29 20:58:21 +00003333 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003334 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003335 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003336 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003337 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003338 char_u *s;
3339 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003340 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003341 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003342#ifdef FEAT_AUTOCMD
3343 qfline_T *cur_qf_start;
3344#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003345 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003346 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003347 buf_T *buf;
3348 int duplicate_name = FALSE;
3349 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003350 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003351 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003352 buf_T *first_match_buf = NULL;
3353 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003354 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003355#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3356 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003357#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003358 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003359 int flags = 0;
3360 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003361 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003362 char_u *dirname_start = NULL;
3363 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003364 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003365#ifdef FEAT_AUTOCMD
3366 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003367
3368 switch (eap->cmdidx)
3369 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003370 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3371 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3372 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003373 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003374 case CMD_grep: au_name = (char_u *)"grep"; break;
3375 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3376 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3377 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003378 default: break;
3379 }
3380 if (au_name != NULL)
3381 {
3382 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3383 curbuf->b_fname, TRUE, curbuf);
3384 if (did_throw || force_abort)
3385 return;
3386 }
3387#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003388
Bram Moolenaar754b5602006-02-09 23:53:20 +00003389 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003390 || eap->cmdidx == CMD_lvimgrep
3391 || eap->cmdidx == CMD_lgrepadd
3392 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003393 {
3394 qi = ll_get_or_alloc_list(curwin);
3395 if (qi == NULL)
3396 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003397 }
3398
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003399 if (eap->addr_count > 0)
3400 tomatch = eap->line2;
3401 else
3402 tomatch = MAXLNUM;
3403
Bram Moolenaar81695252004-12-29 20:58:21 +00003404 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003405 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003406 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003407 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003408 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003409 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003410 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003411 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003412 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003413
3414 if (s != NULL && *s == NUL)
3415 {
3416 /* Pattern is empty, use last search pattern. */
3417 if (last_search_pat() == NULL)
3418 {
3419 EMSG(_(e_noprevre));
3420 goto theend;
3421 }
3422 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3423 }
3424 else
3425 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3426
Bram Moolenaar86b68352004-12-27 21:59:20 +00003427 if (regmatch.regprog == NULL)
3428 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003429 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003430 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003431
3432 p = skipwhite(p);
3433 if (*p == NUL)
3434 {
3435 EMSG(_("E683: File name missing or invalid pattern"));
3436 goto theend;
3437 }
3438
Bram Moolenaar754b5602006-02-09 23:53:20 +00003439 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003440 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003441 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003442 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003443 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003444 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003445 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003446 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003447 prevp->qf_next != prevp; prevp = prevp->qf_next)
3448 ;
3449
3450 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003451 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003452 goto theend;
3453 if (fcount == 0)
3454 {
3455 EMSG(_(e_nomatch));
3456 goto theend;
3457 }
3458
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003459 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3460 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003461 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003462 {
3463 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003464 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003465 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003466
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003467 /* Remember the current directory, because a BufRead autocommand that does
3468 * ":lcd %:p:h" changes the meaning of short path names. */
3469 mch_dirname(dirname_start, MAXPATHL);
3470
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003471#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003472 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003473 * changing the current quickfix list. */
3474 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3475#endif
3476
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003477 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003478 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003479 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003480 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003481 if (time(NULL) > seconds)
3482 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003483 /* Display the file name every second or so, show the user we are
3484 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003485 seconds = time(NULL);
3486 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003487 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003488 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003489 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003490 else
3491 {
3492 msg_outtrans(p);
3493 vim_free(p);
3494 }
3495 msg_clr_eos();
3496 msg_didout = FALSE; /* overwrite this message */
3497 msg_nowait = TRUE; /* don't wait for this message */
3498 msg_col = 0;
3499 out_flush();
3500 }
3501
Bram Moolenaar81695252004-12-29 20:58:21 +00003502 buf = buflist_findname_exp(fnames[fi]);
3503 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3504 {
3505 /* Remember that a buffer with this name already exists. */
3506 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003507 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003508 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003509
3510#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3511 /* Don't do Filetype autocommands to avoid loading syntax and
3512 * indent scripts, a great speed improvement. */
3513 save_ei = au_event_disable(",Filetype");
3514#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003515 /* Don't use modelines here, it's useless. */
3516 save_mls = p_mls;
3517 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003518
3519 /* Load file into a buffer, so that 'fileencoding' is detected,
3520 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003521 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003522
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003523 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003524#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3525 au_event_restore(save_ei);
3526#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003527 }
3528 else
3529 /* Use existing, loaded buffer. */
3530 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003531
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003532#ifdef FEAT_AUTOCMD
3533 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3534 {
3535 int idx;
3536
3537 /* Autocommands changed the quickfix list. Find the one we were
3538 * using and restore it. */
3539 for (idx = 0; idx < LISTCOUNT; ++idx)
3540 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3541 {
3542 qi->qf_curlist = idx;
3543 break;
3544 }
3545 if (idx == LISTCOUNT)
3546 {
3547 /* List cannot be found, create a new one. */
3548 qf_new_list(qi, *eap->cmdlinep);
3549 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3550 }
3551 }
3552#endif
3553
Bram Moolenaar81695252004-12-29 20:58:21 +00003554 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003555 {
3556 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003557 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003558 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003559 else
3560 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003561 /* Try for a match in all lines of the buffer.
3562 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003563 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003564 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3565 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003566 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003567 col = 0;
3568 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003569 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003570 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003571 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003572 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003573 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003574 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003575 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003576 ml_get_buf(buf,
3577 regmatch.startpos[0].lnum + lnum, FALSE),
3578 regmatch.startpos[0].lnum + lnum,
3579 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003580 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003581 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003582 0, /* nr */
3583 0, /* type */
3584 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003585 ) == FAIL)
3586 {
3587 got_int = TRUE;
3588 break;
3589 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003590 found_match = TRUE;
3591 if (--tomatch == 0)
3592 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003593 if ((flags & VGR_GLOBAL) == 0
3594 || regmatch.endpos[0].lnum > 0)
3595 break;
3596 col = regmatch.endpos[0].col
3597 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003598 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003599 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003600 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003601 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003602 if (got_int)
3603 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003604 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003605#ifdef FEAT_AUTOCMD
3606 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3607#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003608
3609 if (using_dummy)
3610 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003611 if (found_match && first_match_buf == NULL)
3612 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003613 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003614 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003615 /* Never keep a dummy buffer if there is another buffer
3616 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003617 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003618 buf = NULL;
3619 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003620 else if (!cmdmod.hide
3621 || buf->b_p_bh[0] == 'u' /* "unload" */
3622 || buf->b_p_bh[0] == 'w' /* "wipe" */
3623 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003624 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003625 /* When no match was found we don't need to remember the
3626 * buffer, wipe it out. If there was a match and it
3627 * wasn't the first one or we won't jump there: only
3628 * unload the buffer.
3629 * Ignore 'hidden' here, because it may lead to having too
3630 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003631 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003632 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003633 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003634 buf = NULL;
3635 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003636 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003637 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003638 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003639 buf = NULL;
3640 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003641 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003642
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003643 if (buf != NULL)
3644 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003645 /* If the buffer is still loaded we need to use the
3646 * directory we jumped to below. */
3647 if (buf == first_match_buf
3648 && target_dir == NULL
3649 && STRCMP(dirname_start, dirname_now) != 0)
3650 target_dir = vim_strsave(dirname_now);
3651
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003652 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003653 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003654 * need to be done (again). But not the window-local
3655 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003656 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003657#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003658 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3659 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003660#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003661 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003662 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003663 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003664 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003665 }
3666 }
3667
3668 FreeWild(fcount, fnames);
3669
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003670 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3671 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3672 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003673
3674#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003675 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003676#endif
3677
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003678#ifdef FEAT_AUTOCMD
3679 if (au_name != NULL)
3680 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3681 curbuf->b_fname, TRUE, curbuf);
3682#endif
3683
Bram Moolenaar86b68352004-12-27 21:59:20 +00003684 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003685 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003686 {
3687 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003688 {
3689 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003690 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003691 if (buf != curbuf)
3692 /* If we jumped to another buffer redrawing will already be
3693 * taken care of. */
3694 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003695
3696 /* Jump to the directory used after loading the buffer. */
3697 if (curbuf == first_match_buf && target_dir != NULL)
3698 {
3699 exarg_T ea;
3700
3701 ea.arg = target_dir;
3702 ea.cmdidx = CMD_lcd;
3703 ex_cd(&ea);
3704 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003705 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003706 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003707 else
3708 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003709
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003710 /* If we loaded a dummy buffer into the current window, the autocommands
3711 * may have messed up things, need to redraw and recompute folds. */
3712 if (redraw_for_dummy)
3713 {
3714#ifdef FEAT_FOLDING
3715 foldUpdateAll(curwin);
3716#else
3717 redraw_later(NOT_VALID);
3718#endif
3719 }
3720
Bram Moolenaar86b68352004-12-27 21:59:20 +00003721theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003722 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003723 vim_free(dirname_now);
3724 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003725 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003726 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003727}
3728
3729/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003730 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003731 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003732 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3733 * If "s" is not NULL terminate the pattern with a NUL.
3734 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003735 */
3736 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003737skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003738{
3739 int c;
3740
3741 if (vim_isIDc(*p))
3742 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003743 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003744 if (s != NULL)
3745 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003746 p = skiptowhite(p);
3747 if (s != NULL && *p != NUL)
3748 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003749 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003750 else
3751 {
3752 /* ":vimgrep /pattern/[g][j] fname" */
3753 if (s != NULL)
3754 *s = p + 1;
3755 c = *p;
3756 p = skip_regexp(p + 1, c, TRUE, NULL);
3757 if (*p != c)
3758 return NULL;
3759
3760 /* Truncate the pattern. */
3761 if (s != NULL)
3762 *p = NUL;
3763 ++p;
3764
3765 /* Find the flags */
3766 while (*p == 'g' || *p == 'j')
3767 {
3768 if (flags != NULL)
3769 {
3770 if (*p == 'g')
3771 *flags |= VGR_GLOBAL;
3772 else
3773 *flags |= VGR_NOJUMP;
3774 }
3775 ++p;
3776 }
3777 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003778 return p;
3779}
3780
3781/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003782 * Restore current working directory to "dirname_start" if they differ, taking
3783 * into account whether it is set locally or globally.
3784 */
3785 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003786restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003787{
3788 char_u *dirname_now = alloc(MAXPATHL);
3789
3790 if (NULL != dirname_now)
3791 {
3792 mch_dirname(dirname_now, MAXPATHL);
3793 if (STRCMP(dirname_start, dirname_now) != 0)
3794 {
3795 /* If the directory has changed, change it back by building up an
3796 * appropriate ex command and executing it. */
3797 exarg_T ea;
3798
3799 ea.arg = dirname_start;
3800 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3801 ex_cd(&ea);
3802 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003803 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003804 }
3805}
3806
3807/*
3808 * Load file "fname" into a dummy buffer and return the buffer pointer,
3809 * placing the directory resulting from the buffer load into the
3810 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3811 * prior to calling this function. Restores directory to "dirname_start" prior
3812 * to returning, if autocmds or the 'autochdir' option have changed it.
3813 *
3814 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3815 * or wipe_dummy_buffer() later!
3816 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003817 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003818 */
3819 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003820load_dummy_buffer(
3821 char_u *fname,
3822 char_u *dirname_start, /* in: old directory */
3823 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003824{
3825 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003826 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003827 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003828 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003829
3830 /* Allocate a buffer without putting it in the buffer list. */
3831 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3832 if (newbuf == NULL)
3833 return NULL;
3834
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003835 /* Init the options. */
3836 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3837
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003838 /* need to open the memfile before putting the buffer in a window */
3839 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003840 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003841 /* set curwin/curbuf to buf and save a few things */
3842 aucmd_prepbuf(&aco, newbuf);
3843
3844 /* Need to set the filename for autocommands. */
3845 (void)setfname(curbuf, fname, NULL, FALSE);
3846
Bram Moolenaar81695252004-12-29 20:58:21 +00003847 /* Create swap file now to avoid the ATTENTION message. */
3848 check_need_swap(TRUE);
3849
3850 /* Remove the "dummy" flag, otherwise autocommands may not
3851 * work. */
3852 curbuf->b_flags &= ~BF_DUMMY;
3853
3854 if (readfile(fname, NULL,
3855 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3856 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003857 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003858 && !(curbuf->b_flags & BF_NEW))
3859 {
3860 failed = FALSE;
3861 if (curbuf != newbuf)
3862 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003863 /* Bloody autocommands changed the buffer! Can happen when
3864 * using netrw and editing a remote file. Use the current
3865 * buffer instead, delete the dummy one after restoring the
3866 * window stuff. */
3867 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003868 newbuf = curbuf;
3869 }
3870 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003871
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003872 /* restore curwin/curbuf and a few other things */
3873 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003874 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3875 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003876 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003877
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003878 /*
3879 * When autocommands/'autochdir' option changed directory: go back.
3880 * Let the caller know what the resulting dir was first, in case it is
3881 * important.
3882 */
3883 mch_dirname(resulting_dir, MAXPATHL);
3884 restore_start_dir(dirname_start);
3885
Bram Moolenaar81695252004-12-29 20:58:21 +00003886 if (!buf_valid(newbuf))
3887 return NULL;
3888 if (failed)
3889 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003890 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003891 return NULL;
3892 }
3893 return newbuf;
3894}
3895
3896/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003897 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3898 * directory to "dirname_start" prior to returning, if autocmds or the
3899 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003900 */
3901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003902wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003903{
3904 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003905 {
3906#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3907 cleanup_T cs;
3908
3909 /* Reset the error/interrupt/exception state here so that aborting()
3910 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3911 * work when got_int is set. */
3912 enter_cleanup(&cs);
3913#endif
3914
Bram Moolenaar81695252004-12-29 20:58:21 +00003915 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003916
3917#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3918 /* Restore the error/interrupt/exception state if not discarded by a
3919 * new aborting error, interrupt, or uncaught exception. */
3920 leave_cleanup(&cs);
3921#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003922 /* When autocommands/'autochdir' option changed directory: go back. */
3923 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003924 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003925}
3926
3927/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003928 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3929 * directory to "dirname_start" prior to returning, if autocmds or the
3930 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003931 */
3932 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003933unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003934{
3935 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003936 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003937 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003938
3939 /* When autocommands/'autochdir' option changed directory: go back. */
3940 restore_start_dir(dirname_start);
3941 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003942}
3943
Bram Moolenaar05159a02005-02-26 23:04:13 +00003944#if defined(FEAT_EVAL) || defined(PROTO)
3945/*
3946 * Add each quickfix error to list "list" as a dictionary.
3947 */
3948 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003949get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003950{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003951 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003952 dict_T *dict;
3953 char_u buf[2];
3954 qfline_T *qfp;
3955 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003956 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003957
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003958 if (wp != NULL)
3959 {
3960 qi = GET_LOC_LIST(wp);
3961 if (qi == NULL)
3962 return FAIL;
3963 }
3964
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003965 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003966 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003967 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003968
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003969 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3970 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003971 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003972 /* Handle entries with a non-existing buffer number. */
3973 bufnum = qfp->qf_fnum;
3974 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3975 bufnum = 0;
3976
Bram Moolenaar05159a02005-02-26 23:04:13 +00003977 if ((dict = dict_alloc()) == NULL)
3978 return FAIL;
3979 if (list_append_dict(list, dict) == FAIL)
3980 return FAIL;
3981
3982 buf[0] = qfp->qf_type;
3983 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003984 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003985 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3986 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3987 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3988 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003989 || dict_add_nr_str(dict, "pattern", 0L,
3990 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3991 || dict_add_nr_str(dict, "text", 0L,
3992 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003993 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3994 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3995 return FAIL;
3996
3997 qfp = qfp->qf_next;
3998 }
3999 return OK;
4000}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004001
4002/*
4003 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02004004 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004005 */
4006 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004007set_errorlist(
4008 win_T *wp,
4009 list_T *list,
4010 int action,
4011 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004012{
4013 listitem_T *li;
4014 dict_T *d;
4015 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004016 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004017 long lnum;
4018 int col, nr;
4019 int vcol;
4020 qfline_T *prevp = NULL;
4021 int valid, status;
4022 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004023 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004024 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004025
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004026 if (wp != NULL)
4027 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004028 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004029 if (qi == NULL)
4030 return FAIL;
4031 }
4032
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004033 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004034 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004035 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004036 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004037 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004038 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004039 prevp->qf_next != prevp; prevp = prevp->qf_next)
4040 ;
4041 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004042 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004043 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004044 qf_store_title(qi, title);
4045 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004046
4047 for (li = list->lv_first; li != NULL; li = li->li_next)
4048 {
4049 if (li->li_tv.v_type != VAR_DICT)
4050 continue; /* Skip non-dict items */
4051
4052 d = li->li_tv.vval.v_dict;
4053 if (d == NULL)
4054 continue;
4055
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004056 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004057 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004058 lnum = get_dict_number(d, (char_u *)"lnum");
4059 col = get_dict_number(d, (char_u *)"col");
4060 vcol = get_dict_number(d, (char_u *)"vcol");
4061 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004062 type = get_dict_string(d, (char_u *)"type", TRUE);
4063 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4064 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004065 if (text == NULL)
4066 text = vim_strsave((char_u *)"");
4067
4068 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004069 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004070 valid = FALSE;
4071
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004072 /* Mark entries with non-existing buffer number as not valid. Give the
4073 * error message only once. */
4074 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4075 {
4076 if (!did_bufnr_emsg)
4077 {
4078 did_bufnr_emsg = TRUE;
4079 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4080 }
4081 valid = FALSE;
4082 bufnum = 0;
4083 }
4084
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004085 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004086 NULL, /* dir */
4087 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004088 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004089 text,
4090 lnum,
4091 col,
4092 vcol, /* vis_col */
4093 pattern, /* search pattern */
4094 nr,
4095 type == NULL ? NUL : *type,
4096 valid);
4097
4098 vim_free(filename);
4099 vim_free(pattern);
4100 vim_free(text);
4101 vim_free(type);
4102
4103 if (status == FAIL)
4104 {
4105 retval = FAIL;
4106 break;
4107 }
4108 }
4109
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004110 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004111 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004112 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4113 else
4114 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004115 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4116 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004117
4118#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004119 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004120#endif
4121
4122 return retval;
4123}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004124#endif
4125
Bram Moolenaar81695252004-12-29 20:58:21 +00004126/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004127 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004128 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004129 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004130 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004131 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004132 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004133 */
4134 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004135ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004136{
4137 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004138 qf_info_T *qi = &ql_info;
4139
Bram Moolenaardb552d602006-03-23 22:59:57 +00004140 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4141 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004142 {
4143 qi = ll_get_or_alloc_list(curwin);
4144 if (qi == NULL)
4145 return;
4146 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004147
4148 if (*eap->arg == NUL)
4149 buf = curbuf;
4150 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4151 buf = buflist_findnr(atoi((char *)eap->arg));
4152 if (buf == NULL)
4153 EMSG(_(e_invarg));
4154 else if (buf->b_ml.ml_mfp == NULL)
4155 EMSG(_("E681: Buffer is not loaded"));
4156 else
4157 {
4158 if (eap->addr_count == 0)
4159 {
4160 eap->line1 = 1;
4161 eap->line2 = buf->b_ml.ml_line_count;
4162 }
4163 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4164 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4165 EMSG(_(e_invrange));
4166 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004167 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004168 char_u *qf_title = *eap->cmdlinep;
4169
4170 if (buf->b_sfname)
4171 {
4172 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4173 (char *)qf_title, (char *)buf->b_sfname);
4174 qf_title = IObuff;
4175 }
4176
Bram Moolenaardb552d602006-03-23 22:59:57 +00004177 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4178 (eap->cmdidx != CMD_caddbuffer
4179 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004180 eap->line1, eap->line2,
4181 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004182 && (eap->cmdidx == CMD_cbuffer
4183 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004184 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4185 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004186 }
4187}
4188
Bram Moolenaar1e015462005-09-25 22:16:38 +00004189#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004190/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004191 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4192 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004193 */
4194 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004195ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004196{
4197 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004198 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004199
Bram Moolenaardb552d602006-03-23 22:59:57 +00004200 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4201 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004202 {
4203 qi = ll_get_or_alloc_list(curwin);
4204 if (qi == NULL)
4205 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004206 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004207
Bram Moolenaar4770d092006-01-12 23:22:24 +00004208 /* Evaluate the expression. When the result is a string or a list we can
4209 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004210 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004211 if (tv != NULL)
4212 {
4213 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4214 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4215 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004216 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004217 (eap->cmdidx != CMD_caddexpr
4218 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004219 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004220 && (eap->cmdidx == CMD_cexpr
4221 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004222 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004223 }
4224 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004225 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004226 free_tv(tv);
4227 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004228}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004229#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004230
4231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 * ":helpgrep {pattern}"
4233 */
4234 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004235ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 regmatch_T regmatch;
4238 char_u *save_cpo;
4239 char_u *p;
4240 int fcount;
4241 char_u **fnames;
4242 FILE *fd;
4243 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004244 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004246#ifdef FEAT_MULTI_LANG
4247 char_u *lang;
4248#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004249 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004250 int new_qi = FALSE;
4251 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004252#ifdef FEAT_AUTOCMD
4253 char_u *au_name = NULL;
4254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004256#ifdef FEAT_MULTI_LANG
4257 /* Check for a specified language */
4258 lang = check_help_lang(eap->arg);
4259#endif
4260
Bram Moolenaar73633f82012-01-20 13:39:07 +01004261#ifdef FEAT_AUTOCMD
4262 switch (eap->cmdidx)
4263 {
4264 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4265 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4266 default: break;
4267 }
4268 if (au_name != NULL)
4269 {
4270 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4271 curbuf->b_fname, TRUE, curbuf);
4272 if (did_throw || force_abort)
4273 return;
4274 }
4275#endif
4276
4277 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4278 save_cpo = p_cpo;
4279 p_cpo = empty_option;
4280
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004281 if (eap->cmdidx == CMD_lhelpgrep)
4282 {
4283 /* Find an existing help window */
4284 FOR_ALL_WINDOWS(wp)
4285 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4286 break;
4287
4288 if (wp == NULL) /* Help window not found */
4289 qi = NULL;
4290 else
4291 qi = wp->w_llist;
4292
4293 if (qi == NULL)
4294 {
4295 /* Allocate a new location list for help text matches */
4296 if ((qi = ll_new_list()) == NULL)
4297 return;
4298 new_qi = TRUE;
4299 }
4300 }
4301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4303 regmatch.rm_ic = FALSE;
4304 if (regmatch.regprog != NULL)
4305 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004306#ifdef FEAT_MBYTE
4307 vimconv_T vc;
4308
4309 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4310 * differs. */
4311 vc.vc_type = CONV_NONE;
4312 if (!enc_utf8)
4313 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4314#endif
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004317 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 /* Go through all directories in 'runtimepath' */
4320 p = p_rtp;
4321 while (*p != NUL && !got_int)
4322 {
4323 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4324
4325 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4326 add_pathsep(NameBuff);
4327 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4328 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4329 &fnames, EW_FILE|EW_SILENT) == OK
4330 && fcount > 0)
4331 {
4332 for (fi = 0; fi < fcount && !got_int; ++fi)
4333 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004334#ifdef FEAT_MULTI_LANG
4335 /* Skip files for a different language. */
4336 if (lang != NULL
4337 && STRNICMP(lang, fnames[fi]
4338 + STRLEN(fnames[fi]) - 3, 2) != 0
4339 && !(STRNICMP(lang, "en", 2) == 0
4340 && STRNICMP("txt", fnames[fi]
4341 + STRLEN(fnames[fi]) - 3, 3) == 0))
4342 continue;
4343#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004344 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (fd != NULL)
4346 {
4347 lnum = 1;
4348 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4349 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004350 char_u *line = IObuff;
4351#ifdef FEAT_MBYTE
4352 /* Convert a line if 'encoding' is not utf-8 and
4353 * the line contains a non-ASCII character. */
4354 if (vc.vc_type != CONV_NONE
4355 && has_non_ascii(IObuff)) {
4356 line = string_convert(&vc, IObuff, NULL);
4357 if (line == NULL)
4358 line = IObuff;
4359 }
4360#endif
4361
4362 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004364 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
4366 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004367 while (l > 0 && line[l - 1] <= ' ')
4368 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004370 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 NULL, /* dir */
4372 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004373 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004374 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004376 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004377 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004378 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004379 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 0, /* nr */
4381 1, /* type */
4382 TRUE /* valid */
4383 ) == FAIL)
4384 {
4385 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004386#ifdef FEAT_MBYTE
4387 if (line != IObuff)
4388 vim_free(line);
4389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 break;
4391 }
4392 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004393#ifdef FEAT_MBYTE
4394 if (line != IObuff)
4395 vim_free(line);
4396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 ++lnum;
4398 line_breakcheck();
4399 }
4400 fclose(fd);
4401 }
4402 }
4403 FreeWild(fcount, fnames);
4404 }
4405 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004406
Bram Moolenaar473de612013-06-08 18:19:48 +02004407 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004408#ifdef FEAT_MBYTE
4409 if (vc.vc_type != CONV_NONE)
4410 convert_setup(&vc, NULL, NULL);
4411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004413 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4414 qi->qf_lists[qi->qf_curlist].qf_ptr =
4415 qi->qf_lists[qi->qf_curlist].qf_start;
4416 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004419 if (p_cpo == empty_option)
4420 p_cpo = save_cpo;
4421 else
4422 /* Darn, some plugin changed the value. */
4423 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004426 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427#endif
4428
Bram Moolenaar73633f82012-01-20 13:39:07 +01004429#ifdef FEAT_AUTOCMD
4430 if (au_name != NULL)
4431 {
4432 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4433 curbuf->b_fname, TRUE, curbuf);
4434 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4435 /* autocommands made "qi" invalid */
4436 return;
4437 }
4438#endif
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004441 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004442 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004443 else
4444 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004445
4446 if (eap->cmdidx == CMD_lhelpgrep)
4447 {
4448 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004449 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004450 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4451 {
4452 if (new_qi)
4453 ll_free_all(&qi);
4454 }
4455 else if (curwin->w_llist == NULL)
4456 curwin->w_llist = qi;
4457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458}
4459
4460#endif /* FEAT_QUICKFIX */