blob: c2ff55a8c0e9ec79a7e735ee44271b95ccf69939 [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 Moolenaar8b201792016-03-25 15:01:10 +01001030 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1031 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 qfp->qf_prev = qfp; /* first element points to itself */
1033 }
1034 else
1035 {
1036 qfp->qf_prev = *prevp;
1037 (*prevp)->qf_next = qfp;
1038 }
1039 qfp->qf_next = qfp; /* last element points to itself */
1040 qfp->qf_cleared = FALSE;
1041 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001042 ++qi->qf_lists[qi->qf_curlist].qf_count;
1043 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1044 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001046 qi->qf_lists[qi->qf_curlist].qf_index =
1047 qi->qf_lists[qi->qf_curlist].qf_count;
1048 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050
1051 return OK;
1052}
1053
1054/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001055 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001056 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001057 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001058ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001059{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001060 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001061
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001062 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1063 if (qi != NULL)
1064 {
1065 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1066 qi->qf_refcount++;
1067 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001068
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001069 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001070}
1071
1072/*
1073 * Return the location list for window 'wp'.
1074 * If not present, allocate a location list
1075 */
1076 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001077ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001078{
1079 if (IS_LL_WINDOW(wp))
1080 /* For a location list window, use the referenced location list */
1081 return wp->w_llist_ref;
1082
1083 /*
1084 * For a non-location list window, w_llist_ref should not point to a
1085 * location list.
1086 */
1087 ll_free_all(&wp->w_llist_ref);
1088
1089 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001090 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001091 return wp->w_llist;
1092}
1093
1094/*
1095 * Copy the location list from window "from" to window "to".
1096 */
1097 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001098copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001099{
1100 qf_info_T *qi;
1101 int idx;
1102 int i;
1103
1104 /*
1105 * When copying from a location list window, copy the referenced
1106 * location list. For other windows, copy the location list for
1107 * that window.
1108 */
1109 if (IS_LL_WINDOW(from))
1110 qi = from->w_llist_ref;
1111 else
1112 qi = from->w_llist;
1113
1114 if (qi == NULL) /* no location list to copy */
1115 return;
1116
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001117 /* allocate a new location list */
1118 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001119 return;
1120
1121 to->w_llist->qf_listcount = qi->qf_listcount;
1122
1123 /* Copy the location lists one at a time */
1124 for (idx = 0; idx < qi->qf_listcount; idx++)
1125 {
1126 qf_list_T *from_qfl;
1127 qf_list_T *to_qfl;
1128
1129 to->w_llist->qf_curlist = idx;
1130
1131 from_qfl = &qi->qf_lists[idx];
1132 to_qfl = &to->w_llist->qf_lists[idx];
1133
1134 /* Some of the fields are populated by qf_add_entry() */
1135 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1136 to_qfl->qf_count = 0;
1137 to_qfl->qf_index = 0;
1138 to_qfl->qf_start = NULL;
1139 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001140 if (from_qfl->qf_title != NULL)
1141 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1142 else
1143 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001144
1145 if (from_qfl->qf_count)
1146 {
1147 qfline_T *from_qfp;
1148 qfline_T *prevp = NULL;
1149
1150 /* copy all the location entries in this list */
1151 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1152 ++i, from_qfp = from_qfp->qf_next)
1153 {
1154 if (qf_add_entry(to->w_llist, &prevp,
1155 NULL,
1156 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001157 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001158 from_qfp->qf_text,
1159 from_qfp->qf_lnum,
1160 from_qfp->qf_col,
1161 from_qfp->qf_viscol,
1162 from_qfp->qf_pattern,
1163 from_qfp->qf_nr,
1164 0,
1165 from_qfp->qf_valid) == FAIL)
1166 {
1167 qf_free_all(to);
1168 return;
1169 }
1170 /*
1171 * qf_add_entry() will not set the qf_num field, as the
1172 * directory and file names are not supplied. So the qf_fnum
1173 * field is copied here.
1174 */
1175 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1176 prevp->qf_type = from_qfp->qf_type; /* error type */
1177 if (from_qfl->qf_ptr == from_qfp)
1178 to_qfl->qf_ptr = prevp; /* current location */
1179 }
1180 }
1181
1182 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1183
1184 /* When no valid entries are present in the list, qf_ptr points to
1185 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001186 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001187 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001188 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001189 to_qfl->qf_index = 1;
1190 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001191 }
1192
1193 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1194}
1195
1196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 * get buffer number for file "dir.name"
1198 */
1199 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001200qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
1202 if (fname == NULL || *fname == NUL) /* no file name */
1203 return 0;
1204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 char_u *ptr;
1206 int fnum;
1207
Bram Moolenaare60acc12011-05-10 16:41:25 +02001208#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001210#endif
1211#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (directory != NULL)
1213 slash_adjust(directory);
1214 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (directory != NULL && !vim_isAbsName(fname)
1217 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1218 {
1219 /*
1220 * Here we check if the file really exists.
1221 * This should normally be true, but if make works without
1222 * "leaving directory"-messages we might have missed a
1223 * directory change.
1224 */
1225 if (mch_getperm(ptr) < 0)
1226 {
1227 vim_free(ptr);
1228 directory = qf_guess_filepath(fname);
1229 if (directory)
1230 ptr = concat_fnames(directory, fname, TRUE);
1231 else
1232 ptr = vim_strsave(fname);
1233 }
1234 /* Use concatenated directory name and file name */
1235 fnum = buflist_add(ptr, 0);
1236 vim_free(ptr);
1237 return fnum;
1238 }
1239 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241}
1242
1243/*
1244 * push dirbuf onto the directory stack and return pointer to actual dir or
1245 * NULL on error
1246 */
1247 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001248qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 struct dir_stack_T *ds_new;
1251 struct dir_stack_T *ds_ptr;
1252
1253 /* allocate new stack element and hook it in */
1254 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1255 if (ds_new == NULL)
1256 return NULL;
1257
1258 ds_new->next = *stackptr;
1259 *stackptr = ds_new;
1260
1261 /* store directory on the stack */
1262 if (vim_isAbsName(dirbuf)
1263 || (*stackptr)->next == NULL
1264 || (*stackptr && dir_stack != *stackptr))
1265 (*stackptr)->dirname = vim_strsave(dirbuf);
1266 else
1267 {
1268 /* Okay we don't have an absolute path.
1269 * dirbuf must be a subdir of one of the directories on the stack.
1270 * Let's search...
1271 */
1272 ds_new = (*stackptr)->next;
1273 (*stackptr)->dirname = NULL;
1274 while (ds_new)
1275 {
1276 vim_free((*stackptr)->dirname);
1277 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1278 TRUE);
1279 if (mch_isdir((*stackptr)->dirname) == TRUE)
1280 break;
1281
1282 ds_new = ds_new->next;
1283 }
1284
1285 /* clean up all dirs we already left */
1286 while ((*stackptr)->next != ds_new)
1287 {
1288 ds_ptr = (*stackptr)->next;
1289 (*stackptr)->next = (*stackptr)->next->next;
1290 vim_free(ds_ptr->dirname);
1291 vim_free(ds_ptr);
1292 }
1293
1294 /* Nothing found -> it must be on top level */
1295 if (ds_new == NULL)
1296 {
1297 vim_free((*stackptr)->dirname);
1298 (*stackptr)->dirname = vim_strsave(dirbuf);
1299 }
1300 }
1301
1302 if ((*stackptr)->dirname != NULL)
1303 return (*stackptr)->dirname;
1304 else
1305 {
1306 ds_ptr = *stackptr;
1307 *stackptr = (*stackptr)->next;
1308 vim_free(ds_ptr);
1309 return NULL;
1310 }
1311}
1312
1313
1314/*
1315 * pop dirbuf from the directory stack and return previous directory or NULL if
1316 * stack is empty
1317 */
1318 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001319qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 struct dir_stack_T *ds_ptr;
1322
1323 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1324 * What to do if it isn't? */
1325
1326 /* pop top element and free it */
1327 if (*stackptr != NULL)
1328 {
1329 ds_ptr = *stackptr;
1330 *stackptr = (*stackptr)->next;
1331 vim_free(ds_ptr->dirname);
1332 vim_free(ds_ptr);
1333 }
1334
1335 /* return NEW top element as current dir or NULL if stack is empty*/
1336 return *stackptr ? (*stackptr)->dirname : NULL;
1337}
1338
1339/*
1340 * clean up directory stack
1341 */
1342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001343qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 struct dir_stack_T *ds_ptr;
1346
1347 while ((ds_ptr = *stackptr) != NULL)
1348 {
1349 *stackptr = (*stackptr)->next;
1350 vim_free(ds_ptr->dirname);
1351 vim_free(ds_ptr);
1352 }
1353}
1354
1355/*
1356 * Check in which directory of the directory stack the given file can be
1357 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001358 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * Cleans up intermediate directory entries.
1360 *
1361 * TODO: How to solve the following problem?
1362 * If we have the this directory tree:
1363 * ./
1364 * ./aa
1365 * ./aa/bb
1366 * ./bb
1367 * ./bb/x.c
1368 * and make says:
1369 * making all in aa
1370 * making all in bb
1371 * x.c:9: Error
1372 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1373 * qf_guess_filepath will return NULL.
1374 */
1375 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001376qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 struct dir_stack_T *ds_ptr;
1379 struct dir_stack_T *ds_tmp;
1380 char_u *fullname;
1381
1382 /* no dirs on the stack - there's nothing we can do */
1383 if (dir_stack == NULL)
1384 return NULL;
1385
1386 ds_ptr = dir_stack->next;
1387 fullname = NULL;
1388 while (ds_ptr)
1389 {
1390 vim_free(fullname);
1391 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1392
1393 /* If concat_fnames failed, just go on. The worst thing that can happen
1394 * is that we delete the entire stack.
1395 */
1396 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1397 break;
1398
1399 ds_ptr = ds_ptr->next;
1400 }
1401
1402 vim_free(fullname);
1403
1404 /* clean up all dirs we already left */
1405 while (dir_stack->next != ds_ptr)
1406 {
1407 ds_tmp = dir_stack->next;
1408 dir_stack->next = dir_stack->next->next;
1409 vim_free(ds_tmp->dirname);
1410 vim_free(ds_tmp);
1411 }
1412
1413 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1414
1415}
1416
1417/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001418 * When loading a file from the quickfix, the auto commands may modify it.
1419 * This may invalidate the current quickfix entry. This function checks
1420 * whether a entry is still present in the quickfix.
1421 * Similar to location list.
1422 */
1423 static int
1424is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1425{
1426 qf_list_T *qfl;
1427 qfline_T *qfp;
1428 int i;
1429
1430 qfl = &qi->qf_lists[qi->qf_curlist];
1431
1432 /* Search for the entry in the current list */
1433 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1434 ++i, qfp = qfp->qf_next)
1435 if (qfp == qf_ptr)
1436 break;
1437
1438 if (i == qfl->qf_count) /* Entry is not found */
1439 return FALSE;
1440
1441 return TRUE;
1442}
1443
1444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 * jump to a quickfix line
1446 * if dir == FORWARD go "errornr" valid entries forward
1447 * if dir == BACKWARD go "errornr" valid entries backward
1448 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1449 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1450 * else if "errornr" is zero, redisplay the same line
1451 * else go to entry "errornr"
1452 */
1453 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001454qf_jump(
1455 qf_info_T *qi,
1456 int dir,
1457 int errornr,
1458 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001460 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001461 qfline_T *qf_ptr;
1462 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 int qf_index;
1464 int old_qf_fnum;
1465 int old_qf_index;
1466 int prev_index;
1467 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1468 char_u *err = e_no_more_items;
1469 linenr_T i;
1470 buf_T *old_curbuf;
1471 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 colnr_T screen_col;
1473 colnr_T char_col;
1474 char_u *line;
1475#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001476 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001477 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 int opened_window = FALSE;
1479 win_T *win;
1480 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001481 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001483 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 int print_message = TRUE;
1485 int len;
1486#ifdef FEAT_FOLDING
1487 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1488#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001489 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001490 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001492 if (qi == NULL)
1493 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001494
1495 if (qi->qf_curlist >= qi->qf_listcount
1496 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 EMSG(_(e_quickfix));
1499 return;
1500 }
1501
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001502 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001504 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 old_qf_index = qf_index;
1506 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1507 {
1508 while (errornr--)
1509 {
1510 old_qf_ptr = qf_ptr;
1511 prev_index = qf_index;
1512 old_qf_fnum = qf_ptr->qf_fnum;
1513 do
1514 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001515 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 || qf_ptr->qf_next == NULL)
1517 {
1518 qf_ptr = old_qf_ptr;
1519 qf_index = prev_index;
1520 if (err != NULL)
1521 {
1522 EMSG(_(err));
1523 goto theend;
1524 }
1525 errornr = 0;
1526 break;
1527 }
1528 ++qf_index;
1529 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001530 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1531 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1533 err = NULL;
1534 }
1535 }
1536 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1537 {
1538 while (errornr--)
1539 {
1540 old_qf_ptr = qf_ptr;
1541 prev_index = qf_index;
1542 old_qf_fnum = qf_ptr->qf_fnum;
1543 do
1544 {
1545 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1546 {
1547 qf_ptr = old_qf_ptr;
1548 qf_index = prev_index;
1549 if (err != NULL)
1550 {
1551 EMSG(_(err));
1552 goto theend;
1553 }
1554 errornr = 0;
1555 break;
1556 }
1557 --qf_index;
1558 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001559 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1560 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1562 err = NULL;
1563 }
1564 }
1565 else if (errornr != 0) /* go to specified number */
1566 {
1567 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1568 {
1569 --qf_index;
1570 qf_ptr = qf_ptr->qf_prev;
1571 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001572 while (errornr > qf_index && qf_index <
1573 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 && qf_ptr->qf_next != NULL)
1575 {
1576 ++qf_index;
1577 qf_ptr = qf_ptr->qf_next;
1578 }
1579 }
1580
1581#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001582 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1583 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 /* No need to print the error message if it's visible in the error
1585 * window */
1586 print_message = FALSE;
1587
1588 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001589 * For ":helpgrep" find a help window or open one.
1590 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001591 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001592 {
1593 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001594
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001595 if (cmdmod.tab != 0)
1596 wp = NULL;
1597 else
1598 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1599 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1600 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001601 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1602 win_enter(wp, TRUE);
1603 else
1604 {
1605 /*
1606 * Split off help window; put it at far top if no position
1607 * specified, the current window is vertically split and narrow.
1608 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001609 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001610 if (cmdmod.split == 0 && curwin->w_width != Columns
1611 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001612 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001613 if (qi != &ql_info)
1614 flags |= WSP_NEWLOC; /* don't copy the location list */
1615
1616 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001617 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001618 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001619
1620 if (curwin->w_height < p_hh)
1621 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001622
1623 if (qi != &ql_info) /* not a quickfix list */
1624 {
1625 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001626 curwin->w_llist = qi;
1627 qi->qf_refcount++;
1628 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001629 }
1630
1631 if (!p_im)
1632 restart_edit = 0; /* don't want insert mode in help file */
1633 }
1634
1635 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * If currently in the quickfix window, find another window to show the
1637 * file in.
1638 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001639 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001641 win_T *usable_win_ptr = NULL;
1642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 /*
1644 * If there is no file specified, we don't know where to go.
1645 * But do advance, otherwise ":cn" gets stuck.
1646 */
1647 if (qf_ptr->qf_fnum == 0)
1648 goto theend;
1649
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001650 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001651
1652 ll_ref = curwin->w_llist_ref;
1653 if (ll_ref != NULL)
1654 {
1655 /* Find a window using the same location list that is not a
1656 * quickfix window. */
1657 FOR_ALL_WINDOWS(usable_win_ptr)
1658 if (usable_win_ptr->w_llist == ll_ref
1659 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001660 {
1661 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001662 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001663 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001664 }
1665
1666 if (!usable_win)
1667 {
1668 /* Locate a window showing a normal buffer */
1669 FOR_ALL_WINDOWS(win)
1670 if (win->w_buffer->b_p_bt[0] == NUL)
1671 {
1672 usable_win = 1;
1673 break;
1674 }
1675 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001678 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001679 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001681 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001682 {
1683 tabpage_T *tp;
1684 win_T *wp;
1685
1686 FOR_ALL_TAB_WINDOWS(tp, wp)
1687 {
1688 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1689 {
1690 goto_tabpage_win(tp, wp);
1691 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001692 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001693 }
1694 }
1695 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001696win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001697
1698 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001699 * If there is only one window and it is the quickfix window, create a
1700 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001701 */
1702 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001704 flags = WSP_ABOVE;
1705 if (ll_ref != NULL)
1706 flags |= WSP_NEWLOC;
1707 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 goto failed; /* not enough room for window */
1709 opened_window = TRUE; /* close it when fail */
1710 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001711 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001712 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001713 if (ll_ref != NULL)
1714 {
1715 /* The new window should use the location list from the
1716 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001717 curwin->w_llist = ll_ref;
1718 ll_ref->qf_refcount++;
1719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
1721 else
1722 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001723 if (curwin->w_llist_ref != NULL)
1724 {
1725 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001726 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001727 if (win == NULL)
1728 {
1729 /* Find the window showing the selected file */
1730 FOR_ALL_WINDOWS(win)
1731 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1732 break;
1733 if (win == NULL)
1734 {
1735 /* Find a previous usable window */
1736 win = curwin;
1737 do
1738 {
1739 if (win->w_buffer->b_p_bt[0] == NUL)
1740 break;
1741 if (win->w_prev == NULL)
1742 win = lastwin; /* wrap around the top */
1743 else
1744 win = win->w_prev; /* go to previous window */
1745 } while (win != curwin);
1746 }
1747 }
1748 win_goto(win);
1749
1750 /* If the location list for the window is not set, then set it
1751 * to the location list from the location window */
1752 if (win->w_llist == NULL)
1753 {
1754 win->w_llist = ll_ref;
1755 ll_ref->qf_refcount++;
1756 }
1757 }
1758 else
1759 {
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 /*
1762 * Try to find a window that shows the right buffer.
1763 * Default to the window just above the quickfix buffer.
1764 */
1765 win = curwin;
1766 altwin = NULL;
1767 for (;;)
1768 {
1769 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1770 break;
1771 if (win->w_prev == NULL)
1772 win = lastwin; /* wrap around the top */
1773 else
1774 win = win->w_prev; /* go to previous window */
1775
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001776 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
1778 /* Didn't find it, go to the window before the quickfix
1779 * window. */
1780 if (altwin != NULL)
1781 win = altwin;
1782 else if (curwin->w_prev != NULL)
1783 win = curwin->w_prev;
1784 else
1785 win = curwin->w_next;
1786 break;
1787 }
1788
1789 /* Remember a usable window. */
1790 if (altwin == NULL && !win->w_p_pvw
1791 && win->w_buffer->b_p_bt[0] == NUL)
1792 altwin = win;
1793 }
1794
1795 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
1798 }
1799#endif
1800
1801 /*
1802 * If there is a file name,
1803 * read the wanted file if needed, and check autowrite etc.
1804 */
1805 old_curbuf = curbuf;
1806 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001807
1808 if (qf_ptr->qf_fnum != 0)
1809 {
1810 if (qf_ptr->qf_type == 1)
1811 {
1812 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1813 * set b_p_ro flag). */
1814 if (!can_abandon(curbuf, forceit))
1815 {
1816 EMSG(_(e_nowrtmsg));
1817 ok = FALSE;
1818 }
1819 else
1820 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001821 ECMD_HIDE + ECMD_SET_HELP,
1822 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001823 }
1824 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01001825 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001826 int old_qf_curlist = qi->qf_curlist;
1827 int is_abort = FALSE;
1828
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001829 ok = buflist_getfile(qf_ptr->qf_fnum,
1830 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01001831 if (qi != &ql_info && !win_valid(oldwin))
1832 {
1833 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001834 is_abort = TRUE;
1835 opened_window = FALSE;
1836 }
1837 else if (old_qf_curlist != qi->qf_curlist
1838 || !is_qf_entry_present(qi, qf_ptr))
1839 {
1840 if (qi == &ql_info)
1841 EMSG(_("E925: Current quickfix was changed"));
1842 else
1843 EMSG(_("E926: Current location list was changed"));
1844 is_abort = TRUE;
1845 }
1846
1847 if (is_abort)
1848 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01001849 ok = FALSE;
1850 qi = NULL;
1851 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01001852 }
1853 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001854 }
1855
1856 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 /* When not switched to another buffer, still need to set pc mark */
1859 if (curbuf == old_curbuf)
1860 setpcmark();
1861
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001862 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001864 /*
1865 * Go to line with error, unless qf_lnum is 0.
1866 */
1867 i = qf_ptr->qf_lnum;
1868 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001870 if (i > curbuf->b_ml.ml_line_count)
1871 i = curbuf->b_ml.ml_line_count;
1872 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001874 if (qf_ptr->qf_col > 0)
1875 {
1876 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02001877#ifdef FEAT_VIRTUALEDIT
1878 curwin->w_cursor.coladd = 0;
1879#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001880 if (qf_ptr->qf_viscol == TRUE)
1881 {
1882 /*
1883 * Check each character from the beginning of the error
1884 * line up to the error column. For each tab character
1885 * found, reduce the error column value by the length of
1886 * a tab character.
1887 */
1888 line = ml_get_curline();
1889 screen_col = 0;
1890 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1891 {
1892 if (*line == NUL)
1893 break;
1894 if (*line++ == '\t')
1895 {
1896 curwin->w_cursor.col -= 7 - (screen_col % 8);
1897 screen_col += 8 - (screen_col % 8);
1898 }
1899 else
1900 ++screen_col;
1901 }
1902 }
1903 check_cursor();
1904 }
1905 else
1906 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001909 {
1910 pos_T save_cursor;
1911
1912 /* Move the cursor to the first line in the buffer */
1913 save_cursor = curwin->w_cursor;
1914 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001915 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1916 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001917 curwin->w_cursor = save_cursor;
1918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920#ifdef FEAT_FOLDING
1921 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1922 foldOpenCursor();
1923#endif
1924 if (print_message)
1925 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001926 /* Update the screen before showing the message, unless the screen
1927 * scrolled up. */
1928 if (!msg_scrolled)
1929 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001931 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1933 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1934 /* Add the message, skipping leading whitespace and newlines. */
1935 len = (int)STRLEN(IObuff);
1936 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1937
1938 /* Output the message. Overwrite to avoid scrolling when the 'O'
1939 * flag is present in 'shortmess'; But when not jumping, print the
1940 * whole message. */
1941 i = msg_scroll;
1942 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1943 msg_scroll = TRUE;
1944 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1945 msg_scroll = FALSE;
1946 msg_attr_keep(IObuff, 0, TRUE);
1947 msg_scroll = i;
1948 }
1949 }
1950 else
1951 {
1952#ifdef FEAT_WINDOWS
1953 if (opened_window)
1954 win_close(curwin, TRUE); /* Close opened window */
1955#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01001956 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 /*
1959 * Couldn't open file, so put index back where it was. This could
1960 * happen if the file was readonly and we changed something.
1961 */
1962#ifdef FEAT_WINDOWS
1963failed:
1964#endif
1965 qf_ptr = old_qf_ptr;
1966 qf_index = old_qf_index;
1967 }
1968 }
1969theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01001970 if (qi != NULL)
1971 {
1972 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1973 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975#ifdef FEAT_WINDOWS
1976 if (p_swb != old_swb && opened_window)
1977 {
1978 /* Restore old 'switchbuf' value, but not when an autocommand or
1979 * modeline has changed the value. */
1980 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001983 swb_flags = old_swb_flags;
1984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 else
1986 free_string_option(old_swb);
1987 }
1988#endif
1989}
1990
1991/*
1992 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001993 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 */
1995 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001996qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001998 buf_T *buf;
1999 char_u *fname;
2000 qfline_T *qfp;
2001 int i;
2002 int idx1 = 1;
2003 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002004 char_u *arg = eap->arg;
2005 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002007 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 if (eap->cmdidx == CMD_llist)
2010 {
2011 qi = GET_LOC_LIST(curwin);
2012 if (qi == NULL)
2013 {
2014 EMSG(_(e_loclist));
2015 return;
2016 }
2017 }
2018
2019 if (qi->qf_curlist >= qi->qf_listcount
2020 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 EMSG(_(e_quickfix));
2023 return;
2024 }
2025 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2026 {
2027 EMSG(_(e_trailing));
2028 return;
2029 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 if (idx1 < 0)
2032 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2033 if (idx2 < 0)
2034 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2035
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002036 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002038 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2039 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2042 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002043 msg_putchar('\n');
2044 if (got_int)
2045 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002046
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002047 fname = NULL;
2048 if (qfp->qf_fnum != 0
2049 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2050 {
2051 fname = buf->b_fname;
2052 if (qfp->qf_type == 1) /* :helpgrep */
2053 fname = gettail(fname);
2054 }
2055 if (fname == NULL)
2056 sprintf((char *)IObuff, "%2d", i);
2057 else
2058 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2059 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002060 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002061 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2062 if (qfp->qf_lnum == 0)
2063 IObuff[0] = NUL;
2064 else if (qfp->qf_col == 0)
2065 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2066 else
2067 sprintf((char *)IObuff, ":%ld col %d",
2068 qfp->qf_lnum, qfp->qf_col);
2069 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2070 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2071 msg_puts_attr(IObuff, hl_attr(HLF_N));
2072 if (qfp->qf_pattern != NULL)
2073 {
2074 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2075 STRCAT(IObuff, ":");
2076 msg_puts(IObuff);
2077 }
2078 msg_puts((char_u *)" ");
2079
2080 /* Remove newlines and leading whitespace from the text. For an
2081 * unrecognized line keep the indent, the compiler may mark a word
2082 * with ^^^^. */
2083 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2085 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002086 msg_prt_line(IObuff, FALSE);
2087 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002089
2090 qfp = qfp->qf_next;
2091 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 ui_breakcheck();
2093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094}
2095
2096/*
2097 * Remove newlines and leading whitespace from an error message.
2098 * Put the result in "buf[bufsize]".
2099 */
2100 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002101qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 int i;
2104 char_u *p = text;
2105
2106 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2107 {
2108 if (*p == '\n')
2109 {
2110 buf[i] = ' ';
2111 while (*++p != NUL)
2112 if (!vim_iswhite(*p) && *p != '\n')
2113 break;
2114 }
2115 else
2116 buf[i] = *p++;
2117 }
2118 buf[i] = NUL;
2119}
2120
2121/*
2122 * ":colder [count]": Up in the quickfix stack.
2123 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002124 * ":lolder [count]": Up in the location list stack.
2125 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 */
2127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002128qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002130 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 int count;
2132
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002133 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2134 {
2135 qi = GET_LOC_LIST(curwin);
2136 if (qi == NULL)
2137 {
2138 EMSG(_(e_loclist));
2139 return;
2140 }
2141 }
2142
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (eap->addr_count != 0)
2144 count = eap->line2;
2145 else
2146 count = 1;
2147 while (count--)
2148 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002151 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002154 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 else
2159 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002165 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002168 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169}
2170
2171 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002172qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002175 qi->qf_curlist + 1, qi->qf_listcount,
2176 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002178 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#endif
2180}
2181
2182/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002183 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 */
2185 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002186qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002188 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002189 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002191 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002193 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002194 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002195 {
2196 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002197 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002198 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2199 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002200 if (stop)
2201 /* Somehow qf_count may have an incorrect value, set it to 1
2202 * to avoid crashing when it's wrong.
2203 * TODO: Avoid qf_count being incorrect. */
2204 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002205 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002206 qi->qf_lists[idx].qf_start = qfp;
2207 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002209 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002210 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002211 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212}
2213
2214/*
2215 * qf_mark_adjust: adjust marks
2216 */
2217 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002218qf_mark_adjust(
2219 win_T *wp,
2220 linenr_T line1,
2221 linenr_T line2,
2222 long amount,
2223 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002225 int i;
2226 qfline_T *qfp;
2227 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002228 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230 if (wp != NULL)
2231 {
2232 if (wp->w_llist == NULL)
2233 return;
2234 qi = wp->w_llist;
2235 }
2236
2237 for (idx = 0; idx < qi->qf_listcount; ++idx)
2238 if (qi->qf_lists[idx].qf_count)
2239 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2240 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (qfp->qf_fnum == curbuf->b_fnum)
2242 {
2243 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2244 {
2245 if (amount == MAXLNUM)
2246 qfp->qf_cleared = TRUE;
2247 else
2248 qfp->qf_lnum += amount;
2249 }
2250 else if (amount_after && qfp->qf_lnum > line2)
2251 qfp->qf_lnum += amount_after;
2252 }
2253}
2254
2255/*
2256 * Make a nice message out of the error character and the error number:
2257 * char number message
2258 * e or E 0 " error"
2259 * w or W 0 " warning"
2260 * i or I 0 " info"
2261 * 0 0 ""
2262 * other 0 " c"
2263 * e or E n " error n"
2264 * w or W n " warning n"
2265 * i or I n " info n"
2266 * 0 n " error n"
2267 * other n " c n"
2268 * 1 x "" :helpgrep
2269 */
2270 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002271qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 static char_u buf[20];
2274 static char_u cc[3];
2275 char_u *p;
2276
2277 if (c == 'W' || c == 'w')
2278 p = (char_u *)" warning";
2279 else if (c == 'I' || c == 'i')
2280 p = (char_u *)" info";
2281 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2282 p = (char_u *)" error";
2283 else if (c == 0 || c == 1)
2284 p = (char_u *)"";
2285 else
2286 {
2287 cc[0] = ' ';
2288 cc[1] = c;
2289 cc[2] = NUL;
2290 p = cc;
2291 }
2292
2293 if (nr <= 0)
2294 return p;
2295
2296 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2297 return buf;
2298}
2299
2300#if defined(FEAT_WINDOWS) || defined(PROTO)
2301/*
2302 * ":cwindow": open the quickfix window if we have errors to display,
2303 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002304 * ":lwindow": open the location list window if we have locations to display,
2305 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 */
2307 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002308ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002310 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 win_T *win;
2312
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313 if (eap->cmdidx == CMD_lwindow)
2314 {
2315 qi = GET_LOC_LIST(curwin);
2316 if (qi == NULL)
2317 return;
2318 }
2319
2320 /* Look for an existing quickfix window. */
2321 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 /*
2324 * If a quickfix window is open but we have no errors to display,
2325 * close the window. If a quickfix window is not open, then open
2326 * it if we have errors; otherwise, leave it closed.
2327 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002328 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002329 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002330 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332 if (win != NULL)
2333 ex_cclose(eap);
2334 }
2335 else if (win == NULL)
2336 ex_copen(eap);
2337}
2338
2339/*
2340 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002341 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002344ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002346 win_T *win = NULL;
2347 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002349 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2350 {
2351 qi = GET_LOC_LIST(curwin);
2352 if (qi == NULL)
2353 return;
2354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002356 /* Find existing quickfix window and close it. */
2357 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (win != NULL)
2359 win_close(win, FALSE);
2360}
2361
2362/*
2363 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002364 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 */
2366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002367ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002369 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002372 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002373 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002374 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002376 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2377 {
2378 qi = GET_LOC_LIST(curwin);
2379 if (qi == NULL)
2380 {
2381 EMSG(_(e_loclist));
2382 return;
2383 }
2384 }
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 if (eap->addr_count != 0)
2387 height = eap->line2;
2388 else
2389 height = QF_WINHEIGHT;
2390
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef FEAT_GUI
2393 need_mouse_correct = TRUE;
2394#endif
2395
2396 /*
2397 * Find existing quickfix window, or open a new one.
2398 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002399 win = qf_find_win(qi);
2400
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002401 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002404 if (eap->addr_count != 0)
2405 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002406 if (cmdmod.split & WSP_VERT)
2407 {
2408 if (height != W_WIDTH(win))
2409 win_setwidth(height);
2410 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002411 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002412 win_setheight(height);
2413 }
2414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 else
2416 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002417 qf_buf = qf_find_buf(qi);
2418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 /* The current window becomes the previous window afterwards. */
2420 win = curwin;
2421
Bram Moolenaar77642c02012-11-20 17:55:10 +01002422 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2423 && cmdmod.split == 0)
2424 /* Create the new window at the very bottom, except when
2425 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002427 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002429 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002431 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 /*
2434 * For the location list window, create a reference to the
2435 * location list from the window 'win'.
2436 */
2437 curwin->w_llist_ref = win->w_llist;
2438 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002440
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002441 if (oldwin != curwin)
2442 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002443 if (qf_buf != NULL)
2444 /* Use the existing quickfix buffer */
2445 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002446 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002447 else
2448 {
2449 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002450 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002451 /* switch off 'swapfile' */
2452 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2453 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002454 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002455 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002456 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002457#ifdef FEAT_DIFF
2458 curwin->w_p_diff = FALSE;
2459#endif
2460#ifdef FEAT_FOLDING
2461 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2462 OPT_LOCAL);
2463#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002466 /* Only set the height when still in the same tab page and there is no
2467 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002468 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 win_setheight(height);
2470 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2471 if (win_valid(win))
2472 prevwin = win;
2473 }
2474
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002475 qf_set_title_var(qi);
2476
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 /*
2478 * Fill the buffer with the quickfix list.
2479 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002480 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002482 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 curwin->w_cursor.col = 0;
2484 check_cursor();
2485 update_topline(); /* scroll to show the line */
2486}
2487
2488/*
2489 * Return the number of the current entry (line number in the quickfix
2490 * window).
2491 */
2492 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002493qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002495 qf_info_T *qi = &ql_info;
2496
2497 if (IS_LL_WINDOW(wp))
2498 /* In the location list window, use the referenced location list */
2499 qi = wp->w_llist_ref;
2500
2501 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502}
2503
2504/*
2505 * Update the cursor position in the quickfix window to the current error.
2506 * Return TRUE if there is a quickfix window.
2507 */
2508 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002509qf_win_pos_update(
2510 qf_info_T *qi,
2511 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002514 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516 /*
2517 * Put the cursor on the current error in the quickfix window, so that
2518 * it's viewable.
2519 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002520 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (win != NULL
2522 && qf_index <= win->w_buffer->b_ml.ml_line_count
2523 && old_qf_index != qf_index)
2524 {
2525 win_T *old_curwin = curwin;
2526
2527 curwin = win;
2528 curbuf = win->w_buffer;
2529 if (qf_index > old_qf_index)
2530 {
2531 curwin->w_redraw_top = old_qf_index;
2532 curwin->w_redraw_bot = qf_index;
2533 }
2534 else
2535 {
2536 curwin->w_redraw_top = qf_index;
2537 curwin->w_redraw_bot = old_qf_index;
2538 }
2539 curwin->w_cursor.lnum = qf_index;
2540 curwin->w_cursor.col = 0;
2541 update_topline(); /* scroll to show the line */
2542 redraw_later(VALID);
2543 curwin->w_redr_status = TRUE; /* update ruler */
2544 curwin = old_curwin;
2545 curbuf = curwin->w_buffer;
2546 }
2547 return win != NULL;
2548}
2549
2550/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002551 * Check whether the given window is displaying the specified quickfix/location
2552 * list buffer
2553 */
2554 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002555is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002556{
2557 /*
2558 * A window displaying the quickfix buffer will have the w_llist_ref field
2559 * set to NULL.
2560 * A window displaying a location list buffer will have the w_llist_ref
2561 * pointing to the location list.
2562 */
2563 if (bt_quickfix(win->w_buffer))
2564 if ((qi == &ql_info && win->w_llist_ref == NULL)
2565 || (qi != &ql_info && win->w_llist_ref == qi))
2566 return TRUE;
2567
2568 return FALSE;
2569}
2570
2571/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002572 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002573 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002574 */
2575 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002576qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002577{
2578 win_T *win;
2579
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002580 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002581 if (is_qf_win(win, qi))
2582 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002583
2584 return win;
2585}
2586
2587/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002588 * Find a quickfix buffer.
2589 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 */
2591 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002592qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002594 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002595 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Bram Moolenaar9c102382006-05-03 21:26:49 +00002597 FOR_ALL_TAB_WINDOWS(tp, win)
2598 if (is_qf_win(win, qi))
2599 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002600
Bram Moolenaar9c102382006-05-03 21:26:49 +00002601 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602}
2603
2604/*
2605 * Find the quickfix buffer. If it exists, update the contents.
2606 */
2607 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002608qf_update_buffer(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002611 win_T *win;
2612 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002616 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 if (buf != NULL)
2618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 /* set curwin/curbuf to buf and save a few things */
2620 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002622 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002623 {
2624 curwin_save = curwin;
2625 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002626 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002627 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002628 }
2629
Bram Moolenaar6920c722016-01-22 22:44:10 +01002630 qf_fill_buffer(qi);
2631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 /* restore curwin/curbuf and a few other things */
2633 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002635 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637}
2638
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002639/*
2640 * Set "w:quickfix_title" if "qi" has a title.
2641 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002642 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002643qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002644{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002645 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2646 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002647 qi->qf_lists[qi->qf_curlist].qf_title);
2648}
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650/*
2651 * Fill current buffer with quickfix errors, replacing any previous contents.
2652 * curbuf must be the quickfix buffer!
2653 */
2654 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002655qf_fill_buffer(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002657 linenr_T lnum;
2658 qfline_T *qfp;
2659 buf_T *errbuf;
2660 int len;
2661 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
2663 /* delete all existing lines */
2664 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2665 (void)ml_delete((linenr_T)1, FALSE);
2666
2667 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002668 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002671 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2672 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
2674 if (qfp->qf_fnum != 0
2675 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2676 && errbuf->b_fname != NULL)
2677 {
2678 if (qfp->qf_type == 1) /* :helpgrep */
2679 STRCPY(IObuff, gettail(errbuf->b_fname));
2680 else
2681 STRCPY(IObuff, errbuf->b_fname);
2682 len = (int)STRLEN(IObuff);
2683 }
2684 else
2685 len = 0;
2686 IObuff[len++] = '|';
2687
2688 if (qfp->qf_lnum > 0)
2689 {
2690 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2691 len += (int)STRLEN(IObuff + len);
2692
2693 if (qfp->qf_col > 0)
2694 {
2695 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2696 len += (int)STRLEN(IObuff + len);
2697 }
2698
2699 sprintf((char *)IObuff + len, "%s",
2700 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2701 len += (int)STRLEN(IObuff + len);
2702 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002703 else if (qfp->qf_pattern != NULL)
2704 {
2705 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2706 len += (int)STRLEN(IObuff + len);
2707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 IObuff[len++] = '|';
2709 IObuff[len++] = ' ';
2710
2711 /* Remove newlines and leading whitespace from the text.
2712 * For an unrecognized line keep the indent, the compiler may
2713 * mark a word with ^^^^. */
2714 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2715 IObuff + len, IOSIZE - len);
2716
2717 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2718 == FAIL)
2719 break;
2720 qfp = qfp->qf_next;
2721 }
2722 /* Delete the empty line which is now at the end */
2723 (void)ml_delete(lnum + 1, FALSE);
2724 }
2725
2726 /* correct cursor position */
2727 check_lnums(TRUE);
2728
2729 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2730 * resembles reading a file into a buffer, it's more logical when using
2731 * autocommands. */
2732 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2733 curbuf->b_p_ma = FALSE;
2734
2735#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002736 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2738 FALSE, curbuf);
2739 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2740 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002741 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742#endif
2743
2744 /* make sure it will be redrawn */
2745 redraw_curbuf_later(NOT_VALID);
2746
2747 /* Restore KeyTyped, setting 'filetype' may reset it. */
2748 KeyTyped = old_KeyTyped;
2749}
2750
2751#endif /* FEAT_WINDOWS */
2752
2753/*
2754 * Return TRUE if "buf" is the quickfix buffer.
2755 */
2756 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002757bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002759 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760}
2761
2762/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002763 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2764 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
2766 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002767bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002769 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2770 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
2773/*
2774 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2775 */
2776 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002777bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002779 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780}
2781
2782 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002783bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 if (bt_dontwrite(buf))
2786 {
2787 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2788 return TRUE;
2789 }
2790 return FALSE;
2791}
2792
2793/*
2794 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2795 * and 'bufhidden'.
2796 */
2797 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002798buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2801 switch (buf->b_p_bh[0])
2802 {
2803 case 'u': /* "unload" */
2804 case 'w': /* "wipe" */
2805 case 'd': return FALSE; /* "delete" */
2806 case 'h': return TRUE; /* "hide" */
2807 }
2808 return (p_hid || cmdmod.hide);
2809}
2810
2811/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002812 * Return TRUE when using ":vimgrep" for ":grep".
2813 */
2814 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002815grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002816{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002817 return ((cmdidx == CMD_grep
2818 || cmdidx == CMD_lgrep
2819 || cmdidx == CMD_grepadd
2820 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002821 && STRCMP("internal",
2822 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2823}
2824
2825/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002826 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 */
2828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002829ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002831 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 char_u *cmd;
2833 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002834 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002835 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002836 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002837#ifdef FEAT_AUTOCMD
2838 char_u *au_name = NULL;
2839
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002840 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2841 if (grep_internal(eap->cmdidx))
2842 {
2843 ex_vimgrep(eap);
2844 return;
2845 }
2846
Bram Moolenaar7c626922005-02-07 22:01:03 +00002847 switch (eap->cmdidx)
2848 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002849 case CMD_make: au_name = (char_u *)"make"; break;
2850 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2851 case CMD_grep: au_name = (char_u *)"grep"; break;
2852 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2853 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2854 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002855 default: break;
2856 }
2857 if (au_name != NULL)
2858 {
2859 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2860 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002861# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002862 if (did_throw || force_abort)
2863 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002864# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002865 }
2866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaara6557602006-02-04 22:43:20 +00002868 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2869 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002870 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002871
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002873 fname = get_mef_name();
2874 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002876 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878 /*
2879 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2880 */
2881 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2882 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002883 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 cmd = alloc(len);
2885 if (cmd == NULL)
2886 return;
2887 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2888 (char *)p_shq);
2889 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002890 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 /*
2892 * Output a newline if there's something else than the :make command that
2893 * was typed (in which case the cursor is in column 0).
2894 */
2895 if (msg_col == 0)
2896 msg_didout = FALSE;
2897 msg_start();
2898 MSG_PUTS(":!");
2899 msg_outtrans(cmd); /* show what we are doing */
2900
2901 /* let the shell know if we are redirecting output or not */
2902 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2903
2904#ifdef AMIGA
2905 out_flush();
2906 /* read window status report and redraw before message */
2907 (void)char_avail();
2908#endif
2909
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002910 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002911 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2912 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002913 && eap->cmdidx != CMD_lgrepadd),
2914 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002915 if (wp != NULL)
2916 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002917#ifdef FEAT_AUTOCMD
2918 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002919 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002920 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2921 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002922 if (qi->qf_curlist < qi->qf_listcount)
2923 res = qi->qf_lists[qi->qf_curlist].qf_count;
2924 else
2925 res = 0;
2926 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002927#endif
2928 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002929 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
Bram Moolenaar7c626922005-02-07 22:01:03 +00002931 mch_remove(fname);
2932 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 vim_free(cmd);
2934}
2935
2936/*
2937 * Return the name for the errorfile, in allocated memory.
2938 * Find a new unique name when 'makeef' contains "##".
2939 * Returns NULL for error.
2940 */
2941 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002942get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 char_u *p;
2945 char_u *name;
2946 static int start = -1;
2947 static int off = 0;
2948#ifdef HAVE_LSTAT
2949 struct stat sb;
2950#endif
2951
2952 if (*p_mef == NUL)
2953 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002954 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (name == NULL)
2956 EMSG(_(e_notmp));
2957 return name;
2958 }
2959
2960 for (p = p_mef; *p; ++p)
2961 if (p[0] == '#' && p[1] == '#')
2962 break;
2963
2964 if (*p == NUL)
2965 return vim_strsave(p_mef);
2966
2967 /* Keep trying until the name doesn't exist yet. */
2968 for (;;)
2969 {
2970 if (start == -1)
2971 start = mch_get_pid();
2972 else
2973 off += 19;
2974
2975 name = alloc((unsigned)STRLEN(p_mef) + 30);
2976 if (name == NULL)
2977 break;
2978 STRCPY(name, p_mef);
2979 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2980 STRCAT(name, p + 2);
2981 if (mch_getperm(name) < 0
2982#ifdef HAVE_LSTAT
2983 /* Don't accept a symbolic link, its a security risk. */
2984 && mch_lstat((char *)name, &sb) < 0
2985#endif
2986 )
2987 break;
2988 vim_free(name);
2989 }
2990 return name;
2991}
2992
2993/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002994 * Returns the number of valid entries in the current quickfix/location list.
2995 */
2996 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002997qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002998{
2999 qf_info_T *qi = &ql_info;
3000 qfline_T *qfp;
3001 int i, sz = 0;
3002 int prev_fnum = 0;
3003
3004 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3005 {
3006 /* Location list */
3007 qi = GET_LOC_LIST(curwin);
3008 if (qi == NULL)
3009 return 0;
3010 }
3011
3012 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3013 (i < qi->qf_lists[qi->qf_curlist].qf_count) && (qfp != NULL);
3014 ++i, qfp = qfp->qf_next)
3015 {
3016 if (qfp->qf_valid)
3017 {
3018 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3019 sz++; /* Count all valid entries */
3020 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3021 {
3022 /* Count the number of files */
3023 sz++;
3024 prev_fnum = qfp->qf_fnum;
3025 }
3026 }
3027 }
3028
3029 return sz;
3030}
3031
3032/*
3033 * Returns the current index of the quickfix/location list.
3034 * Returns 0 if there is an error.
3035 */
3036 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003037qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003038{
3039 qf_info_T *qi = &ql_info;
3040
3041 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3042 {
3043 /* Location list */
3044 qi = GET_LOC_LIST(curwin);
3045 if (qi == NULL)
3046 return 0;
3047 }
3048
3049 return qi->qf_lists[qi->qf_curlist].qf_index;
3050}
3051
3052/*
3053 * Returns the current index in the quickfix/location list (counting only valid
3054 * entries). If no valid entries are in the list, then returns 1.
3055 */
3056 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003057qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003058{
3059 qf_info_T *qi = &ql_info;
3060 qf_list_T *qfl;
3061 qfline_T *qfp;
3062 int i, eidx = 0;
3063 int prev_fnum = 0;
3064
3065 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3066 {
3067 /* Location list */
3068 qi = GET_LOC_LIST(curwin);
3069 if (qi == NULL)
3070 return 1;
3071 }
3072
3073 qfl = &qi->qf_lists[qi->qf_curlist];
3074 qfp = qfl->qf_start;
3075
3076 /* check if the list has valid errors */
3077 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3078 return 1;
3079
3080 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3081 {
3082 if (qfp->qf_valid)
3083 {
3084 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3085 {
3086 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3087 {
3088 /* Count the number of files */
3089 eidx++;
3090 prev_fnum = qfp->qf_fnum;
3091 }
3092 }
3093 else
3094 eidx++;
3095 }
3096 }
3097
3098 return eidx ? eidx : 1;
3099}
3100
3101/*
3102 * Get the 'n'th valid error entry in the quickfix or location list.
3103 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3104 * For :cdo and :ldo returns the 'n'th valid error entry.
3105 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3106 */
3107 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003108qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003109{
3110 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3111 qfline_T *qfp = qfl->qf_start;
3112 int i, eidx;
3113 int prev_fnum = 0;
3114
3115 /* check if the list has valid errors */
3116 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3117 return 1;
3118
3119 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp!= NULL;
3120 i++, qfp = qfp->qf_next)
3121 {
3122 if (qfp->qf_valid)
3123 {
3124 if (fdo)
3125 {
3126 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3127 {
3128 /* Count the number of files */
3129 eidx++;
3130 prev_fnum = qfp->qf_fnum;
3131 }
3132 }
3133 else
3134 eidx++;
3135 }
3136
3137 if (eidx == n)
3138 break;
3139 }
3140
3141 if (i <= qfl->qf_count)
3142 return i;
3143 else
3144 return 1;
3145}
3146
3147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003149 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003150 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 */
3152 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003153ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003155 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003156 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003157
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003158 if (eap->cmdidx == CMD_ll
3159 || eap->cmdidx == CMD_lrewind
3160 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003161 || eap->cmdidx == CMD_llast
3162 || eap->cmdidx == CMD_ldo
3163 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003164 {
3165 qi = GET_LOC_LIST(curwin);
3166 if (qi == NULL)
3167 {
3168 EMSG(_(e_loclist));
3169 return;
3170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003171 }
3172
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003173 if (eap->addr_count > 0)
3174 errornr = (int)eap->line2;
3175 else
3176 {
3177 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3178 errornr = 0;
3179 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3180 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3181 errornr = 1;
3182 else
3183 errornr = 32767;
3184 }
3185
3186 /* For cdo and ldo commands, jump to the nth valid error.
3187 * For cfdo and lfdo commands, jump to the nth valid file entry.
3188 */
3189 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3190 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3191 errornr = qf_get_nth_valid_entry(qi,
3192 eap->addr_count > 0 ? (int)eap->line1 : 1,
3193 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3194
3195 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196}
3197
3198/*
3199 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003200 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003201 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 */
3203 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003204ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003206 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003207 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003208
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003209 if (eap->cmdidx == CMD_lnext
3210 || eap->cmdidx == CMD_lNext
3211 || eap->cmdidx == CMD_lprevious
3212 || eap->cmdidx == CMD_lnfile
3213 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003214 || eap->cmdidx == CMD_lpfile
3215 || eap->cmdidx == CMD_ldo
3216 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003217 {
3218 qi = GET_LOC_LIST(curwin);
3219 if (qi == NULL)
3220 {
3221 EMSG(_(e_loclist));
3222 return;
3223 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003224 }
3225
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003226 if (eap->addr_count > 0 &&
3227 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3228 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3229 errornr = (int)eap->line2;
3230 else
3231 errornr = 1;
3232
3233 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3234 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003236 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3237 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003239 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3240 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 ? BACKWARD_FILE
3242 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003243 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
3245
3246/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003247 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003248 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 */
3250 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003251ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003253 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003254 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003255#ifdef FEAT_AUTOCMD
3256 char_u *au_name = NULL;
3257#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003258
3259 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003260 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003261 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003262
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003263#ifdef FEAT_AUTOCMD
3264 switch (eap->cmdidx)
3265 {
3266 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3267 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3268 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3269 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3270 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3271 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3272 default: break;
3273 }
3274 if (au_name != NULL)
3275 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3276#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003277#ifdef FEAT_BROWSE
3278 if (cmdmod.browse)
3279 {
3280 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3281 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3282 if (browse_file == NULL)
3283 return;
3284 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3285 vim_free(browse_file);
3286 }
3287 else
3288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003290 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003291
3292 /*
3293 * This function is used by the :cfile, :cgetfile and :caddfile
3294 * commands.
3295 * :cfile always creates a new quickfix list and jumps to the
3296 * first error.
3297 * :cgetfile creates a new quickfix list but doesn't jump to the
3298 * first error.
3299 * :caddfile adds to an existing quickfix list. If there is no
3300 * quickfix list then a new list is created.
3301 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003302 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003303 && eap->cmdidx != CMD_laddfile),
3304 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003305 && (eap->cmdidx == CMD_cfile
3306 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003307 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003308#ifdef FEAT_AUTOCMD
3309 if (au_name != NULL)
3310 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3311#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003312 if (wp != NULL)
3313 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003314 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003315 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003316
3317 else
3318 {
3319#ifdef FEAT_AUTOCMD
3320 if (au_name != NULL)
3321 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3322#endif
3323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324}
3325
3326/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003327 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003328 * ":vimgrepadd {pattern} file(s)"
3329 * ":lvimgrep {pattern} file(s)"
3330 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003331 */
3332 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003333ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003334{
Bram Moolenaar81695252004-12-29 20:58:21 +00003335 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003336 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003337 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003338 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003339 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003340 char_u *s;
3341 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003342 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003343 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003344#ifdef FEAT_AUTOCMD
3345 qfline_T *cur_qf_start;
3346#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003347 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003348 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003349 buf_T *buf;
3350 int duplicate_name = FALSE;
3351 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003352 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003353 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003354 buf_T *first_match_buf = NULL;
3355 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003356 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003357#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3358 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003359#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003360 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003361 int flags = 0;
3362 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003363 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003364 char_u *dirname_start = NULL;
3365 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003366 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003367#ifdef FEAT_AUTOCMD
3368 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003369
3370 switch (eap->cmdidx)
3371 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003372 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3373 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3374 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003375 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003376 case CMD_grep: au_name = (char_u *)"grep"; break;
3377 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3378 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3379 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003380 default: break;
3381 }
3382 if (au_name != NULL)
3383 {
3384 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3385 curbuf->b_fname, TRUE, curbuf);
3386 if (did_throw || force_abort)
3387 return;
3388 }
3389#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003390
Bram Moolenaar754b5602006-02-09 23:53:20 +00003391 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003392 || eap->cmdidx == CMD_lvimgrep
3393 || eap->cmdidx == CMD_lgrepadd
3394 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003395 {
3396 qi = ll_get_or_alloc_list(curwin);
3397 if (qi == NULL)
3398 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003399 }
3400
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003401 if (eap->addr_count > 0)
3402 tomatch = eap->line2;
3403 else
3404 tomatch = MAXLNUM;
3405
Bram Moolenaar81695252004-12-29 20:58:21 +00003406 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003407 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003408 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003409 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003410 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003411 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003412 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003413 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003414 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003415
3416 if (s != NULL && *s == NUL)
3417 {
3418 /* Pattern is empty, use last search pattern. */
3419 if (last_search_pat() == NULL)
3420 {
3421 EMSG(_(e_noprevre));
3422 goto theend;
3423 }
3424 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3425 }
3426 else
3427 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3428
Bram Moolenaar86b68352004-12-27 21:59:20 +00003429 if (regmatch.regprog == NULL)
3430 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003431 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003432 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003433
3434 p = skipwhite(p);
3435 if (*p == NUL)
3436 {
3437 EMSG(_("E683: File name missing or invalid pattern"));
3438 goto theend;
3439 }
3440
Bram Moolenaar754b5602006-02-09 23:53:20 +00003441 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003442 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003443 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003444 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003445 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003446 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003447 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003448 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003449 prevp->qf_next != prevp; prevp = prevp->qf_next)
3450 ;
3451
3452 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003453 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003454 goto theend;
3455 if (fcount == 0)
3456 {
3457 EMSG(_(e_nomatch));
3458 goto theend;
3459 }
3460
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003461 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3462 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003463 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003464 {
3465 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003466 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003467 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003468
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003469 /* Remember the current directory, because a BufRead autocommand that does
3470 * ":lcd %:p:h" changes the meaning of short path names. */
3471 mch_dirname(dirname_start, MAXPATHL);
3472
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003473#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003474 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003475 * changing the current quickfix list. */
3476 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3477#endif
3478
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003479 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003480 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003481 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003482 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003483 if (time(NULL) > seconds)
3484 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003485 /* Display the file name every second or so, show the user we are
3486 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003487 seconds = time(NULL);
3488 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003489 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003490 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003491 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003492 else
3493 {
3494 msg_outtrans(p);
3495 vim_free(p);
3496 }
3497 msg_clr_eos();
3498 msg_didout = FALSE; /* overwrite this message */
3499 msg_nowait = TRUE; /* don't wait for this message */
3500 msg_col = 0;
3501 out_flush();
3502 }
3503
Bram Moolenaar81695252004-12-29 20:58:21 +00003504 buf = buflist_findname_exp(fnames[fi]);
3505 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3506 {
3507 /* Remember that a buffer with this name already exists. */
3508 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003509 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003510 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003511
3512#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3513 /* Don't do Filetype autocommands to avoid loading syntax and
3514 * indent scripts, a great speed improvement. */
3515 save_ei = au_event_disable(",Filetype");
3516#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003517 /* Don't use modelines here, it's useless. */
3518 save_mls = p_mls;
3519 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003520
3521 /* Load file into a buffer, so that 'fileencoding' is detected,
3522 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003523 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003524
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003525 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003526#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3527 au_event_restore(save_ei);
3528#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003529 }
3530 else
3531 /* Use existing, loaded buffer. */
3532 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003533
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003534#ifdef FEAT_AUTOCMD
3535 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3536 {
3537 int idx;
3538
3539 /* Autocommands changed the quickfix list. Find the one we were
3540 * using and restore it. */
3541 for (idx = 0; idx < LISTCOUNT; ++idx)
3542 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3543 {
3544 qi->qf_curlist = idx;
3545 break;
3546 }
3547 if (idx == LISTCOUNT)
3548 {
3549 /* List cannot be found, create a new one. */
3550 qf_new_list(qi, *eap->cmdlinep);
3551 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3552 }
3553 }
3554#endif
3555
Bram Moolenaar81695252004-12-29 20:58:21 +00003556 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003557 {
3558 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003559 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003560 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003561 else
3562 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003563 /* Try for a match in all lines of the buffer.
3564 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003565 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003566 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3567 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003568 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003569 col = 0;
3570 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003571 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003572 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003573 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003574 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003575 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003576 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003577 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003578 ml_get_buf(buf,
3579 regmatch.startpos[0].lnum + lnum, FALSE),
3580 regmatch.startpos[0].lnum + lnum,
3581 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003582 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003583 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003584 0, /* nr */
3585 0, /* type */
3586 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003587 ) == FAIL)
3588 {
3589 got_int = TRUE;
3590 break;
3591 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003592 found_match = TRUE;
3593 if (--tomatch == 0)
3594 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003595 if ((flags & VGR_GLOBAL) == 0
3596 || regmatch.endpos[0].lnum > 0)
3597 break;
3598 col = regmatch.endpos[0].col
3599 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003600 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003601 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003602 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003603 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003604 if (got_int)
3605 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003606 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003607#ifdef FEAT_AUTOCMD
3608 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3609#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003610
3611 if (using_dummy)
3612 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003613 if (found_match && first_match_buf == NULL)
3614 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003615 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003616 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003617 /* Never keep a dummy buffer if there is another buffer
3618 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003619 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003620 buf = NULL;
3621 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003622 else if (!cmdmod.hide
3623 || buf->b_p_bh[0] == 'u' /* "unload" */
3624 || buf->b_p_bh[0] == 'w' /* "wipe" */
3625 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003626 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003627 /* When no match was found we don't need to remember the
3628 * buffer, wipe it out. If there was a match and it
3629 * wasn't the first one or we won't jump there: only
3630 * unload the buffer.
3631 * Ignore 'hidden' here, because it may lead to having too
3632 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003633 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003634 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003635 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003636 buf = NULL;
3637 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003638 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003639 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003640 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003641 buf = NULL;
3642 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003643 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003644
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003645 if (buf != NULL)
3646 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003647 /* If the buffer is still loaded we need to use the
3648 * directory we jumped to below. */
3649 if (buf == first_match_buf
3650 && target_dir == NULL
3651 && STRCMP(dirname_start, dirname_now) != 0)
3652 target_dir = vim_strsave(dirname_now);
3653
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003654 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003655 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003656 * need to be done (again). But not the window-local
3657 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003658 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003659#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003660 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3661 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003662#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003663 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003664 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003665 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003666 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003667 }
3668 }
3669
3670 FreeWild(fcount, fnames);
3671
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003672 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3673 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3674 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003675
3676#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003677 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003678#endif
3679
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003680#ifdef FEAT_AUTOCMD
3681 if (au_name != NULL)
3682 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3683 curbuf->b_fname, TRUE, curbuf);
3684#endif
3685
Bram Moolenaar86b68352004-12-27 21:59:20 +00003686 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003687 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003688 {
3689 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003690 {
3691 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003692 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003693 if (buf != curbuf)
3694 /* If we jumped to another buffer redrawing will already be
3695 * taken care of. */
3696 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003697
3698 /* Jump to the directory used after loading the buffer. */
3699 if (curbuf == first_match_buf && target_dir != NULL)
3700 {
3701 exarg_T ea;
3702
3703 ea.arg = target_dir;
3704 ea.cmdidx = CMD_lcd;
3705 ex_cd(&ea);
3706 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003707 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003708 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003709 else
3710 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003711
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003712 /* If we loaded a dummy buffer into the current window, the autocommands
3713 * may have messed up things, need to redraw and recompute folds. */
3714 if (redraw_for_dummy)
3715 {
3716#ifdef FEAT_FOLDING
3717 foldUpdateAll(curwin);
3718#else
3719 redraw_later(NOT_VALID);
3720#endif
3721 }
3722
Bram Moolenaar86b68352004-12-27 21:59:20 +00003723theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003724 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003725 vim_free(dirname_now);
3726 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003727 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003728 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003729}
3730
3731/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003732 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003733 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003734 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3735 * If "s" is not NULL terminate the pattern with a NUL.
3736 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003737 */
3738 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003739skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003740{
3741 int c;
3742
3743 if (vim_isIDc(*p))
3744 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003745 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003746 if (s != NULL)
3747 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003748 p = skiptowhite(p);
3749 if (s != NULL && *p != NUL)
3750 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003751 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003752 else
3753 {
3754 /* ":vimgrep /pattern/[g][j] fname" */
3755 if (s != NULL)
3756 *s = p + 1;
3757 c = *p;
3758 p = skip_regexp(p + 1, c, TRUE, NULL);
3759 if (*p != c)
3760 return NULL;
3761
3762 /* Truncate the pattern. */
3763 if (s != NULL)
3764 *p = NUL;
3765 ++p;
3766
3767 /* Find the flags */
3768 while (*p == 'g' || *p == 'j')
3769 {
3770 if (flags != NULL)
3771 {
3772 if (*p == 'g')
3773 *flags |= VGR_GLOBAL;
3774 else
3775 *flags |= VGR_NOJUMP;
3776 }
3777 ++p;
3778 }
3779 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003780 return p;
3781}
3782
3783/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003784 * Restore current working directory to "dirname_start" if they differ, taking
3785 * into account whether it is set locally or globally.
3786 */
3787 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003788restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003789{
3790 char_u *dirname_now = alloc(MAXPATHL);
3791
3792 if (NULL != dirname_now)
3793 {
3794 mch_dirname(dirname_now, MAXPATHL);
3795 if (STRCMP(dirname_start, dirname_now) != 0)
3796 {
3797 /* If the directory has changed, change it back by building up an
3798 * appropriate ex command and executing it. */
3799 exarg_T ea;
3800
3801 ea.arg = dirname_start;
3802 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3803 ex_cd(&ea);
3804 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003805 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003806 }
3807}
3808
3809/*
3810 * Load file "fname" into a dummy buffer and return the buffer pointer,
3811 * placing the directory resulting from the buffer load into the
3812 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3813 * prior to calling this function. Restores directory to "dirname_start" prior
3814 * to returning, if autocmds or the 'autochdir' option have changed it.
3815 *
3816 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3817 * or wipe_dummy_buffer() later!
3818 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003819 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003820 */
3821 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003822load_dummy_buffer(
3823 char_u *fname,
3824 char_u *dirname_start, /* in: old directory */
3825 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003826{
3827 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003828 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003829 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003830 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003831
3832 /* Allocate a buffer without putting it in the buffer list. */
3833 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3834 if (newbuf == NULL)
3835 return NULL;
3836
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003837 /* Init the options. */
3838 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3839
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003840 /* need to open the memfile before putting the buffer in a window */
3841 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003842 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003843 /* set curwin/curbuf to buf and save a few things */
3844 aucmd_prepbuf(&aco, newbuf);
3845
3846 /* Need to set the filename for autocommands. */
3847 (void)setfname(curbuf, fname, NULL, FALSE);
3848
Bram Moolenaar81695252004-12-29 20:58:21 +00003849 /* Create swap file now to avoid the ATTENTION message. */
3850 check_need_swap(TRUE);
3851
3852 /* Remove the "dummy" flag, otherwise autocommands may not
3853 * work. */
3854 curbuf->b_flags &= ~BF_DUMMY;
3855
3856 if (readfile(fname, NULL,
3857 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3858 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003859 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003860 && !(curbuf->b_flags & BF_NEW))
3861 {
3862 failed = FALSE;
3863 if (curbuf != newbuf)
3864 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003865 /* Bloody autocommands changed the buffer! Can happen when
3866 * using netrw and editing a remote file. Use the current
3867 * buffer instead, delete the dummy one after restoring the
3868 * window stuff. */
3869 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003870 newbuf = curbuf;
3871 }
3872 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003873
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003874 /* restore curwin/curbuf and a few other things */
3875 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003876 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3877 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003878 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003879
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003880 /*
3881 * When autocommands/'autochdir' option changed directory: go back.
3882 * Let the caller know what the resulting dir was first, in case it is
3883 * important.
3884 */
3885 mch_dirname(resulting_dir, MAXPATHL);
3886 restore_start_dir(dirname_start);
3887
Bram Moolenaar81695252004-12-29 20:58:21 +00003888 if (!buf_valid(newbuf))
3889 return NULL;
3890 if (failed)
3891 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003892 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003893 return NULL;
3894 }
3895 return newbuf;
3896}
3897
3898/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003899 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3900 * directory to "dirname_start" prior to returning, if autocmds or the
3901 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003902 */
3903 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003904wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003905{
3906 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003907 {
3908#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3909 cleanup_T cs;
3910
3911 /* Reset the error/interrupt/exception state here so that aborting()
3912 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3913 * work when got_int is set. */
3914 enter_cleanup(&cs);
3915#endif
3916
Bram Moolenaar81695252004-12-29 20:58:21 +00003917 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003918
3919#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3920 /* Restore the error/interrupt/exception state if not discarded by a
3921 * new aborting error, interrupt, or uncaught exception. */
3922 leave_cleanup(&cs);
3923#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003924 /* When autocommands/'autochdir' option changed directory: go back. */
3925 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003926 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003927}
3928
3929/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003930 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3931 * directory to "dirname_start" prior to returning, if autocmds or the
3932 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003933 */
3934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003935unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003936{
3937 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003938 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003939 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003940
3941 /* When autocommands/'autochdir' option changed directory: go back. */
3942 restore_start_dir(dirname_start);
3943 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003944}
3945
Bram Moolenaar05159a02005-02-26 23:04:13 +00003946#if defined(FEAT_EVAL) || defined(PROTO)
3947/*
3948 * Add each quickfix error to list "list" as a dictionary.
3949 */
3950 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003951get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003952{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003953 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003954 dict_T *dict;
3955 char_u buf[2];
3956 qfline_T *qfp;
3957 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003958 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003959
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003960 if (wp != NULL)
3961 {
3962 qi = GET_LOC_LIST(wp);
3963 if (qi == NULL)
3964 return FAIL;
3965 }
3966
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003967 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003968 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003969 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003970
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003971 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3972 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003973 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003974 /* Handle entries with a non-existing buffer number. */
3975 bufnum = qfp->qf_fnum;
3976 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3977 bufnum = 0;
3978
Bram Moolenaar05159a02005-02-26 23:04:13 +00003979 if ((dict = dict_alloc()) == NULL)
3980 return FAIL;
3981 if (list_append_dict(list, dict) == FAIL)
3982 return FAIL;
3983
3984 buf[0] = qfp->qf_type;
3985 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003986 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003987 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3988 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3989 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3990 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003991 || dict_add_nr_str(dict, "pattern", 0L,
3992 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3993 || dict_add_nr_str(dict, "text", 0L,
3994 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003995 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3996 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3997 return FAIL;
3998
3999 qfp = qfp->qf_next;
4000 }
4001 return OK;
4002}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004003
4004/*
4005 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02004006 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004007 */
4008 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004009set_errorlist(
4010 win_T *wp,
4011 list_T *list,
4012 int action,
4013 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004014{
4015 listitem_T *li;
4016 dict_T *d;
4017 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004018 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004019 long lnum;
4020 int col, nr;
4021 int vcol;
4022 qfline_T *prevp = NULL;
4023 int valid, status;
4024 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004025 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004026 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004027
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004028 if (wp != NULL)
4029 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004030 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004031 if (qi == NULL)
4032 return FAIL;
4033 }
4034
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004035 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004036 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004037 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004038 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004039 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004040 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004041 prevp->qf_next != prevp; prevp = prevp->qf_next)
4042 ;
4043 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004044 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004045 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004046 qf_store_title(qi, title);
4047 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004048
4049 for (li = list->lv_first; li != NULL; li = li->li_next)
4050 {
4051 if (li->li_tv.v_type != VAR_DICT)
4052 continue; /* Skip non-dict items */
4053
4054 d = li->li_tv.vval.v_dict;
4055 if (d == NULL)
4056 continue;
4057
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004058 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004059 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004060 lnum = get_dict_number(d, (char_u *)"lnum");
4061 col = get_dict_number(d, (char_u *)"col");
4062 vcol = get_dict_number(d, (char_u *)"vcol");
4063 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004064 type = get_dict_string(d, (char_u *)"type", TRUE);
4065 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4066 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004067 if (text == NULL)
4068 text = vim_strsave((char_u *)"");
4069
4070 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004071 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004072 valid = FALSE;
4073
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004074 /* Mark entries with non-existing buffer number as not valid. Give the
4075 * error message only once. */
4076 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4077 {
4078 if (!did_bufnr_emsg)
4079 {
4080 did_bufnr_emsg = TRUE;
4081 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4082 }
4083 valid = FALSE;
4084 bufnum = 0;
4085 }
4086
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004087 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004088 NULL, /* dir */
4089 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004090 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004091 text,
4092 lnum,
4093 col,
4094 vcol, /* vis_col */
4095 pattern, /* search pattern */
4096 nr,
4097 type == NULL ? NUL : *type,
4098 valid);
4099
4100 vim_free(filename);
4101 vim_free(pattern);
4102 vim_free(text);
4103 vim_free(type);
4104
4105 if (status == FAIL)
4106 {
4107 retval = FAIL;
4108 break;
4109 }
4110 }
4111
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004112 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004113 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004114 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4115 else
4116 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004117 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar8b201792016-03-25 15:01:10 +01004118 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4119 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004120
4121#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004122 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004123#endif
4124
4125 return retval;
4126}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004127#endif
4128
Bram Moolenaar81695252004-12-29 20:58:21 +00004129/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004130 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004131 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004132 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004133 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004134 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004135 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004136 */
4137 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004138ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004139{
4140 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004141 qf_info_T *qi = &ql_info;
4142
Bram Moolenaardb552d602006-03-23 22:59:57 +00004143 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4144 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004145 {
4146 qi = ll_get_or_alloc_list(curwin);
4147 if (qi == NULL)
4148 return;
4149 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004150
4151 if (*eap->arg == NUL)
4152 buf = curbuf;
4153 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4154 buf = buflist_findnr(atoi((char *)eap->arg));
4155 if (buf == NULL)
4156 EMSG(_(e_invarg));
4157 else if (buf->b_ml.ml_mfp == NULL)
4158 EMSG(_("E681: Buffer is not loaded"));
4159 else
4160 {
4161 if (eap->addr_count == 0)
4162 {
4163 eap->line1 = 1;
4164 eap->line2 = buf->b_ml.ml_line_count;
4165 }
4166 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4167 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4168 EMSG(_(e_invrange));
4169 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004170 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004171 char_u *qf_title = *eap->cmdlinep;
4172
4173 if (buf->b_sfname)
4174 {
4175 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4176 (char *)qf_title, (char *)buf->b_sfname);
4177 qf_title = IObuff;
4178 }
4179
Bram Moolenaardb552d602006-03-23 22:59:57 +00004180 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4181 (eap->cmdidx != CMD_caddbuffer
4182 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004183 eap->line1, eap->line2,
4184 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004185 && (eap->cmdidx == CMD_cbuffer
4186 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004187 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4188 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004189 }
4190}
4191
Bram Moolenaar1e015462005-09-25 22:16:38 +00004192#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004193/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004194 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4195 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004196 */
4197 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004198ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004199{
4200 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004201 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004202
Bram Moolenaardb552d602006-03-23 22:59:57 +00004203 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4204 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004205 {
4206 qi = ll_get_or_alloc_list(curwin);
4207 if (qi == NULL)
4208 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004209 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004210
Bram Moolenaar4770d092006-01-12 23:22:24 +00004211 /* Evaluate the expression. When the result is a string or a list we can
4212 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004213 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004214 if (tv != NULL)
4215 {
4216 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4217 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4218 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004219 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004220 (eap->cmdidx != CMD_caddexpr
4221 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004222 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004223 && (eap->cmdidx == CMD_cexpr
4224 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004225 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004226 }
4227 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004228 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004229 free_tv(tv);
4230 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004231}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004232#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004233
4234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 * ":helpgrep {pattern}"
4236 */
4237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004238ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
4240 regmatch_T regmatch;
4241 char_u *save_cpo;
4242 char_u *p;
4243 int fcount;
4244 char_u **fnames;
4245 FILE *fd;
4246 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004247 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004249#ifdef FEAT_MULTI_LANG
4250 char_u *lang;
4251#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004252 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004253 int new_qi = FALSE;
4254 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004255#ifdef FEAT_AUTOCMD
4256 char_u *au_name = NULL;
4257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004259#ifdef FEAT_MULTI_LANG
4260 /* Check for a specified language */
4261 lang = check_help_lang(eap->arg);
4262#endif
4263
Bram Moolenaar73633f82012-01-20 13:39:07 +01004264#ifdef FEAT_AUTOCMD
4265 switch (eap->cmdidx)
4266 {
4267 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4268 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4269 default: break;
4270 }
4271 if (au_name != NULL)
4272 {
4273 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4274 curbuf->b_fname, TRUE, curbuf);
4275 if (did_throw || force_abort)
4276 return;
4277 }
4278#endif
4279
4280 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4281 save_cpo = p_cpo;
4282 p_cpo = empty_option;
4283
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004284 if (eap->cmdidx == CMD_lhelpgrep)
4285 {
4286 /* Find an existing help window */
4287 FOR_ALL_WINDOWS(wp)
4288 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4289 break;
4290
4291 if (wp == NULL) /* Help window not found */
4292 qi = NULL;
4293 else
4294 qi = wp->w_llist;
4295
4296 if (qi == NULL)
4297 {
4298 /* Allocate a new location list for help text matches */
4299 if ((qi = ll_new_list()) == NULL)
4300 return;
4301 new_qi = TRUE;
4302 }
4303 }
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4306 regmatch.rm_ic = FALSE;
4307 if (regmatch.regprog != NULL)
4308 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004309#ifdef FEAT_MBYTE
4310 vimconv_T vc;
4311
4312 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4313 * differs. */
4314 vc.vc_type = CONV_NONE;
4315 if (!enc_utf8)
4316 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4317#endif
4318
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004320 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 /* Go through all directories in 'runtimepath' */
4323 p = p_rtp;
4324 while (*p != NUL && !got_int)
4325 {
4326 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4327
4328 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4329 add_pathsep(NameBuff);
4330 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4331 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4332 &fnames, EW_FILE|EW_SILENT) == OK
4333 && fcount > 0)
4334 {
4335 for (fi = 0; fi < fcount && !got_int; ++fi)
4336 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004337#ifdef FEAT_MULTI_LANG
4338 /* Skip files for a different language. */
4339 if (lang != NULL
4340 && STRNICMP(lang, fnames[fi]
4341 + STRLEN(fnames[fi]) - 3, 2) != 0
4342 && !(STRNICMP(lang, "en", 2) == 0
4343 && STRNICMP("txt", fnames[fi]
4344 + STRLEN(fnames[fi]) - 3, 3) == 0))
4345 continue;
4346#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004347 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 if (fd != NULL)
4349 {
4350 lnum = 1;
4351 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4352 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004353 char_u *line = IObuff;
4354#ifdef FEAT_MBYTE
4355 /* Convert a line if 'encoding' is not utf-8 and
4356 * the line contains a non-ASCII character. */
4357 if (vc.vc_type != CONV_NONE
4358 && has_non_ascii(IObuff)) {
4359 line = string_convert(&vc, IObuff, NULL);
4360 if (line == NULL)
4361 line = IObuff;
4362 }
4363#endif
4364
4365 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004367 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
4369 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004370 while (l > 0 && line[l - 1] <= ' ')
4371 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004373 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 NULL, /* dir */
4375 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004376 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004377 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004379 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004380 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004381 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004382 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 0, /* nr */
4384 1, /* type */
4385 TRUE /* valid */
4386 ) == FAIL)
4387 {
4388 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004389#ifdef FEAT_MBYTE
4390 if (line != IObuff)
4391 vim_free(line);
4392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 break;
4394 }
4395 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004396#ifdef FEAT_MBYTE
4397 if (line != IObuff)
4398 vim_free(line);
4399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 ++lnum;
4401 line_breakcheck();
4402 }
4403 fclose(fd);
4404 }
4405 }
4406 FreeWild(fcount, fnames);
4407 }
4408 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004409
Bram Moolenaar473de612013-06-08 18:19:48 +02004410 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004411#ifdef FEAT_MBYTE
4412 if (vc.vc_type != CONV_NONE)
4413 convert_setup(&vc, NULL, NULL);
4414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004416 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4417 qi->qf_lists[qi->qf_curlist].qf_ptr =
4418 qi->qf_lists[qi->qf_curlist].qf_start;
4419 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004422 if (p_cpo == empty_option)
4423 p_cpo = save_cpo;
4424 else
4425 /* Darn, some plugin changed the value. */
4426 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004429 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#endif
4431
Bram Moolenaar73633f82012-01-20 13:39:07 +01004432#ifdef FEAT_AUTOCMD
4433 if (au_name != NULL)
4434 {
4435 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4436 curbuf->b_fname, TRUE, curbuf);
4437 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4438 /* autocommands made "qi" invalid */
4439 return;
4440 }
4441#endif
4442
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004444 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004445 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004446 else
4447 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004448
4449 if (eap->cmdidx == CMD_lhelpgrep)
4450 {
4451 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004452 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004453 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4454 {
4455 if (new_qi)
4456 ll_free_all(&qi);
4457 }
4458 else if (curwin->w_llist == NULL)
4459 curwin->w_llist = qi;
4460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461}
4462
4463#endif /* FEAT_QUICKFIX */