blob: 6a38c789a300f8b9d645ac0d48ee8354d2cfb22d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
55 qfline_T *qf_ptr; /* pointer to the current error */
56 int qf_count; /* number of errors (0 means no error list) */
57 int qf_index; /* current index in the error list */
58 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059 char_u *qf_title; /* title derived from the command that created
60 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
75};
76
77static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar68b76a62005-03-25 21:53:48 +000079#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * Structure used to hold the info of one part of 'errorformat'
83 */
Bram Moolenaar01265852006-03-20 21:50:15 +000084typedef struct efm_S efm_T;
85struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000088 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
90 char_u prefix; /* prefix of this format line: */
91 /* 'D' enter directory */
92 /* 'X' leave directory */
93 /* 'A' start of multi-line message */
94 /* 'E' error message */
95 /* 'W' warning message */
96 /* 'I' informational message */
97 /* 'C' continuation line */
98 /* 'Z' end of multi-line message */
99 /* 'G' general, unspecific message */
100 /* 'P' push file (partial) message */
101 /* 'Q' pop/quit file (partial) message */
102 /* 'O' overread (partial) message */
103 char_u flags; /* additional flags given in prefix */
104 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000105 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000106 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107};
108
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100109static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
110static void qf_store_title(qf_info_T *qi, char_u *title);
111static void qf_new_list(qf_info_T *qi, char_u *qf_title);
112static void ll_free_all(qf_info_T **pqi);
113static int qf_add_entry(qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
114static qf_info_T *ll_new_list(void);
115static void qf_msg(qf_info_T *qi);
116static void qf_free(qf_info_T *qi, int idx);
117static char_u *qf_types(int, int);
118static int qf_get_fnum(char_u *, char_u *);
119static char_u *qf_push_dir(char_u *, struct dir_stack_T **);
120static char_u *qf_pop_dir(struct dir_stack_T **);
121static char_u *qf_guess_filepath(char_u *);
122static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
123static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100125static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
126static int is_qf_win(win_T *win, qf_info_T *qi);
127static win_T *qf_find_win(qf_info_T *qi);
128static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200129static void qf_update_buffer(qf_info_T *qi, int update_cursor);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void qf_set_title_var(qf_info_T *qi);
131static void qf_fill_buffer(qf_info_T *qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static char_u *get_mef_name(void);
134static void restore_start_dir(char_u *dirname_start);
135static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
136static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
137static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
138static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000140/* Quickfix window check helper macro */
141#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
142/* Location list window check helper macro */
143#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
144/*
145 * Return location list for window 'wp'
146 * For location list window, return the referenced location list
147 */
148#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000151 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200152 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 * Return -1 for error, number of errors for success.
154 */
155 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100156qf_init(
157 win_T *wp,
158 char_u *efile,
159 char_u *errorformat,
160 int newlist, /* TRUE: start a new error list */
161 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163 qf_info_T *qi = &ql_info;
164
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000165 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166 {
167 qi = ll_get_or_alloc_list(wp);
168 if (qi == NULL)
169 return FAIL;
170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171
172 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200173 (linenr_T)0, (linenr_T)0,
174 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000175}
176
177/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200178 * Maximum number of bytes allowed per line while reading a errorfile.
179 */
180#define LINE_MAXLEN 4096
181
182/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000183 * Read the errorfile "efile" into memory, line by line, building the error
184 * list.
185 * Alternative: when "efile" is null read errors from buffer "buf".
186 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200187 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
188 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000189 * Return -1 for error, number of errors for success.
190 */
191 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100192qf_init_ext(
193 qf_info_T *qi,
194 char_u *efile,
195 buf_T *buf,
196 typval_T *tv,
197 char_u *errorformat,
198 int newlist, /* TRUE: start a new error list */
199 linenr_T lnumfirst, /* first line number to use */
200 linenr_T lnumlast, /* last line number to use */
201 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000202{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 char_u *namebuf;
204 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200205 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000206 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 char_u *fmtstr = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200208 char_u *growbuf = NULL;
209 int growbuflen;
210 int growbufsiz;
211 char_u *linebuf;
212 int linelen;
213 int discard;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000215 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 int type = 0;
217 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000218 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 long lnum = 0L;
220 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000221 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000222 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000224 efm_T *fmt_first = NULL;
225 efm_T *fmt_last = NULL;
226 efm_T *fmt_ptr;
227 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 char_u *efm;
229 char_u *ptr;
230 char_u *srcptr;
231 int len;
232 int i;
233 int round;
234 int idx = 0;
235 int multiline = FALSE;
236 int multiignore = FALSE;
237 int multiscan = FALSE;
238 int retval = -1; /* default: return error flag */
239 char_u *directory = NULL;
240 char_u *currfile = NULL;
241 char_u *tail = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200242 char_u *p_buf = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000243 char_u *p_str = NULL;
244 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 struct dir_stack_T *file_stack = NULL;
246 regmatch_T regmatch;
247 static struct fmtpattern
248 {
249 char_u convchar;
250 char *pattern;
251 } fmt_pat[FMT_PATTERNS] =
252 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000253 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {'n', "\\d\\+"},
255 {'l', "\\d\\+"},
256 {'c', "\\d\\+"},
257 {'t', "."},
258 {'m', ".\\+"},
259 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200260 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000261 {'v', "\\d\\+"},
262 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 };
264
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100265 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200266 errmsglen = CMDBUFFSIZE + 1;
267 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100268 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000269 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 goto qf_init_end;
271
Bram Moolenaar86b68352004-12-27 21:59:20 +0000272 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 {
274 EMSG2(_(e_openerrf), efile);
275 goto qf_init_end;
276 }
277
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000278 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100280 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000281 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000283 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200284 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 ;
286
287/*
288 * Each part of the format string is copied and modified from errorformat to
289 * regex prog. Only a few % characters are allowed.
290 */
291 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000292 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000293 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 else
295 efm = errorformat;
296 /*
297 * Get some space to modify the format string into.
298 */
299 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
300 for (round = FMT_PATTERNS; round > 0; )
301 i += (int)STRLEN(fmt_pat[--round].pattern);
302#ifdef COLON_IN_FILENAME
303 i += 12; /* "%f" can become twelve chars longer */
304#else
305 i += 2; /* "%f" can become two chars longer */
306#endif
307 if ((fmtstr = alloc(i)) == NULL)
308 goto error2;
309
Bram Moolenaar01265852006-03-20 21:50:15 +0000310 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 {
312 /*
313 * Allocate a new eformat structure and put it at the end of the list
314 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000315 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 if (fmt_ptr == NULL)
317 goto error2;
318 if (fmt_first == NULL) /* first one */
319 fmt_first = fmt_ptr;
320 else
321 fmt_last->next = fmt_ptr;
322 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323
324 /*
325 * Isolate one part in the 'errorformat' option
326 */
327 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
328 if (efm[len] == '\\' && efm[len + 1] != NUL)
329 ++len;
330
331 /*
332 * Build regexp pattern from current 'errorformat' option
333 */
334 ptr = fmtstr;
335 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000336 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 for (efmp = efm; efmp < efm + len; ++efmp)
338 {
339 if (*efmp == '%')
340 {
341 ++efmp;
342 for (idx = 0; idx < FMT_PATTERNS; ++idx)
343 if (fmt_pat[idx].convchar == *efmp)
344 break;
345 if (idx < FMT_PATTERNS)
346 {
347 if (fmt_ptr->addr[idx])
348 {
349 sprintf((char *)errmsg,
350 _("E372: Too many %%%c in format string"), *efmp);
351 EMSG(errmsg);
352 goto error2;
353 }
354 if ((idx
355 && idx < 6
356 && vim_strchr((char_u *)"DXOPQ",
357 fmt_ptr->prefix) != NULL)
358 || (idx == 6
359 && vim_strchr((char_u *)"OPQ",
360 fmt_ptr->prefix) == NULL))
361 {
362 sprintf((char *)errmsg,
363 _("E373: Unexpected %%%c in format string"), *efmp);
364 EMSG(errmsg);
365 goto error2;
366 }
367 fmt_ptr->addr[idx] = (char_u)++round;
368 *ptr++ = '\\';
369 *ptr++ = '(';
370#ifdef BACKSLASH_IN_FILENAME
371 if (*efmp == 'f')
372 {
373 /* Also match "c:" in the file name, even when
374 * checking for a colon next: "%f:".
375 * "\%(\a:\)\=" */
376 STRCPY(ptr, "\\%(\\a:\\)\\=");
377 ptr += 10;
378 }
379#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000380 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000382 if (efmp[1] != '\\' && efmp[1] != '%')
383 {
384 /* A file name may contain spaces, but this isn't
385 * in "\f". For "%f:%l:%m" there may be a ":" in
386 * the file name. Use ".\{-1,}x" instead (x is
387 * the next character), the requirement that :999:
388 * follows should work. */
389 STRCPY(ptr, ".\\{-1,}");
390 ptr += 7;
391 }
392 else
393 {
394 /* File name followed by '\\' or '%': include as
395 * many file name chars as possible. */
396 STRCPY(ptr, "\\f\\+");
397 ptr += 4;
398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400 else
401 {
402 srcptr = (char_u *)fmt_pat[idx].pattern;
403 while ((*ptr = *srcptr++) != NUL)
404 ++ptr;
405 }
406 *ptr++ = '\\';
407 *ptr++ = ')';
408 }
409 else if (*efmp == '*')
410 {
411 if (*++efmp == '[' || *efmp == '\\')
412 {
413 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
414 {
415 if (efmp[1] == '^')
416 *ptr++ = *++efmp;
417 if (efmp < efm + len)
418 {
419 *ptr++ = *++efmp; /* could be ']' */
420 while (efmp < efm + len
421 && (*ptr++ = *++efmp) != ']')
422 /* skip */;
423 if (efmp == efm + len)
424 {
425 EMSG(_("E374: Missing ] in format string"));
426 goto error2;
427 }
428 }
429 }
430 else if (efmp < efm + len) /* %*\D, %*\s etc. */
431 *ptr++ = *++efmp;
432 *ptr++ = '\\';
433 *ptr++ = '+';
434 }
435 else
436 {
437 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
438 sprintf((char *)errmsg,
439 _("E375: Unsupported %%%c in format string"), *efmp);
440 EMSG(errmsg);
441 goto error2;
442 }
443 }
444 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
445 *ptr++ = *efmp; /* regexp magic characters */
446 else if (*efmp == '#')
447 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000448 else if (*efmp == '>')
449 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 else if (efmp == efm + 1) /* analyse prefix */
451 {
452 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
453 fmt_ptr->flags = *efmp++;
454 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
455 fmt_ptr->prefix = *efmp;
456 else
457 {
458 sprintf((char *)errmsg,
459 _("E376: Invalid %%%c in format string prefix"), *efmp);
460 EMSG(errmsg);
461 goto error2;
462 }
463 }
464 else
465 {
466 sprintf((char *)errmsg,
467 _("E377: Invalid %%%c in format string"), *efmp);
468 EMSG(errmsg);
469 goto error2;
470 }
471 }
472 else /* copy normal character */
473 {
474 if (*efmp == '\\' && efmp + 1 < efm + len)
475 ++efmp;
476 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
477 *ptr++ = '\\'; /* escape regexp atoms */
478 if (*efmp)
479 *ptr++ = *efmp;
480 }
481 }
482 *ptr++ = '$';
483 *ptr = NUL;
484 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
485 goto error2;
486 /*
487 * Advance to next part
488 */
489 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
490 }
491 if (fmt_first == NULL) /* nothing found */
492 {
493 EMSG(_("E378: 'errorformat' contains no pattern"));
494 goto error2;
495 }
496
497 /*
498 * got_int is reset here, because it was probably set when killing the
499 * ":make" command, but we still want to read the errorfile then.
500 */
501 got_int = FALSE;
502
503 /* Always ignore case when looking for a matching error. */
504 regmatch.rm_ic = TRUE;
505
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000506 if (tv != NULL)
507 {
508 if (tv->v_type == VAR_STRING)
509 p_str = tv->vval.v_string;
510 else if (tv->v_type == VAR_LIST)
511 p_li = tv->vval.v_list->lv_first;
512 }
513
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 /*
515 * Read the lines in the error file one by one.
516 * Try to recognize one of the error formats in each line.
517 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000518 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000520 /* Get the next line. */
521 if (fd == NULL)
522 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000523 if (tv != NULL)
524 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000525 if (tv->v_type == VAR_STRING)
526 {
527 /* Get the next line from the supplied string */
528 char_u *p;
529
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200530 if (*p_str == NUL) /* Reached the end of the string */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000531 break;
532
533 p = vim_strchr(p_str, '\n');
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200534 if (p != NULL)
535 len = (int)(p - p_str) + 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000536 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000537 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000538
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200539 if (len > IOSIZE - 2)
540 {
541 /*
542 * If the line exceeds LINE_MAXLEN exclude the last
543 * byte since it's not a NL character.
544 */
545 linelen = len > LINE_MAXLEN ? LINE_MAXLEN - 1 : len;
546 if (growbuf == NULL)
547 {
Bram Moolenaar9b4ebc62016-05-01 13:28:38 +0200548 growbuf = alloc(linelen + 1);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200549 growbufsiz = linelen;
550 }
551 else if (linelen > growbufsiz)
552 {
Bram Moolenaar9b4ebc62016-05-01 13:28:38 +0200553 growbuf = vim_realloc(growbuf, linelen + 1);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200554 if (growbuf == NULL)
555 goto qf_init_end;
556 growbufsiz = linelen;
557 }
558 linebuf = growbuf;
559 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000560 else
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200561 {
562 linebuf = IObuff;
563 linelen = len;
564 }
565 vim_strncpy(linebuf, p_str, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000566
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200567 /*
568 * Increment using len in order to discard the rest of the
569 * line if it exceeds LINE_MAXLEN.
570 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000571 p_str += len;
572 }
573 else if (tv->v_type == VAR_LIST)
574 {
575 /* Get the next line from the supplied list */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200576 while (p_li != NULL
577 && (p_li->li_tv.v_type != VAR_STRING
578 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000579 p_li = p_li->li_next; /* Skip non-string items */
580
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200581 if (p_li == NULL) /* End of the list */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000582 break;
583
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000584 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200585 if (len > IOSIZE - 2)
586 {
587 linelen = len;
588 if (linelen > LINE_MAXLEN)
589 linelen = LINE_MAXLEN - 1;
590 if (growbuf == NULL)
591 {
Bram Moolenaar9b4ebc62016-05-01 13:28:38 +0200592 growbuf = alloc(linelen + 1);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200593 growbufsiz = linelen;
594 }
595 else if (linelen > growbufsiz)
596 {
597 if ((growbuf = vim_realloc(growbuf,
Bram Moolenaar9b4ebc62016-05-01 13:28:38 +0200598 linelen + 1)) == NULL)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200599 goto qf_init_end;
600 growbufsiz = linelen;
601 }
602 linebuf = growbuf;
603 }
604 else
605 {
606 linebuf = IObuff;
607 linelen = len;
608 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200610 vim_strncpy(linebuf, p_li->li_tv.vval.v_string, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000611
612 p_li = p_li->li_next; /* next item */
613 }
614 }
615 else
616 {
617 /* Get the next line from the supplied buffer */
618 if (buflnum > lnumlast)
619 break;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200620 p_buf = ml_get_buf(buf, buflnum++, FALSE);
621 linelen = (int)STRLEN(p_buf);
622 if (linelen > IOSIZE - 2)
623 {
624 if (growbuf == NULL)
625 {
Bram Moolenaar9b4ebc62016-05-01 13:28:38 +0200626 growbuf = alloc(linelen + 1);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200627 growbufsiz = linelen;
628 }
629 else if (linelen > growbufsiz)
630 {
631 if (linelen > LINE_MAXLEN)
632 linelen = LINE_MAXLEN - 1;
Bram Moolenaar9b4ebc62016-05-01 13:28:38 +0200633 if ((growbuf = vim_realloc(growbuf, linelen + 1)) == NULL)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200634 goto qf_init_end;
635 growbufsiz = linelen;
636 }
637 linebuf = growbuf;
638 }
639 else
640 linebuf = IObuff;
641 vim_strncpy(linebuf, p_buf, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000642 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000643 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200644 else
645 {
646 if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
647 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000648
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200649 discard = FALSE;
650 linelen = (int)STRLEN(IObuff);
651 if (linelen == IOSIZE - 1 && (IObuff[linelen - 1] != '\n'
652#ifdef USE_CRNL
653 || IObuff[linelen - 1] != '\r'
654#endif
655 ))
656 {
657 /*
658 * The current line exceeds IObuff, continue reading using
659 * growbuf until EOL or LINE_MAXLEN bytes is read.
660 */
661 if (growbuf == NULL)
662 {
663 growbufsiz = 2 * (IOSIZE - 1);
664 growbuf = alloc(growbufsiz);
665 if (growbuf == NULL)
666 goto qf_init_end;
667 }
668
669 /* Copy the read part of the line, excluding null-terminator */
670 memcpy(growbuf, IObuff, IOSIZE - 1);
671 growbuflen = linelen;
672
673 for (;;)
674 {
675 if (fgets((char *)growbuf + growbuflen,
676 growbufsiz - growbuflen, fd) == NULL)
677 break;
678 linelen = STRLEN(growbuf + growbuflen);
679 growbuflen += linelen;
680 if (growbuf[growbuflen - 1] == '\n'
681#ifdef USE_CRNL
682 || growbuf[growbuflen - 1] == '\r'
683#endif
684 )
685 break;
686 if (growbufsiz == LINE_MAXLEN)
687 {
688 discard = TRUE;
689 break;
690 }
691
692 growbufsiz = 2 * growbufsiz < LINE_MAXLEN
693 ? 2 * growbufsiz : LINE_MAXLEN;
694 growbuf = vim_realloc(growbuf, 2 * growbufsiz);
695 if (growbuf == NULL)
696 goto qf_init_end;
697 }
698
699 while (discard)
700 {
701 /*
702 * The current line is longer than LINE_MAXLEN, continue
703 * reading but discard everything until EOL or EOF is
704 * reached.
705 */
706 if (fgets((char *)IObuff, IOSIZE, fd) == NULL
707 || (int)STRLEN(IObuff) < IOSIZE - 1
708 || IObuff[IOSIZE - 1] == '\n'
709#ifdef USE_CRNL
710 || IObuff[IOSIZE - 1] == '\r'
711#endif
712 )
713 break;
714 }
715
716 linebuf = growbuf;
717 linelen = growbuflen;
718 }
719 else
720 linebuf = IObuff;
721 }
722
723 if (linelen > 0 && linebuf[linelen - 1] == '\n')
724 linebuf[linelen - 1] = NUL;
725#ifdef USE_CRNL
726 if (linelen > 0 && linebuf[linelen - 1] == '\r')
727 linebuf[linelen - 1] = NUL;
Bram Moolenaar836082d2011-08-10 13:21:46 +0200728#endif
729
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200730#ifdef FEAT_MBYTE
731 remove_bom(linebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733
Bram Moolenaar01265852006-03-20 21:50:15 +0000734 /* If there was no %> item start at the first pattern */
735 if (fmt_start == NULL)
736 fmt_ptr = fmt_first;
737 else
738 {
739 fmt_ptr = fmt_start;
740 fmt_start = NULL;
741 }
742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 /*
744 * Try to match each part of 'errorformat' until we find a complete
745 * match or no match.
746 */
747 valid = TRUE;
748restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000749 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100751 int r;
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 idx = fmt_ptr->prefix;
754 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
755 continue;
756 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000757 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (!multiscan)
759 errmsg[0] = NUL;
760 lnum = 0;
761 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000762 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 enr = -1;
764 type = 0;
765 tail = NULL;
766
767 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200768 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100769 fmt_ptr->prog = regmatch.regprog;
770 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
772 if ((idx == 'C' || idx == 'Z') && !multiline)
773 continue;
774 if (vim_strchr((char_u *)"EWI", idx) != NULL)
775 type = idx;
776 else
777 type = 0;
778 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000779 * Extract error message data from matched line.
780 * We check for an actual submatch, because "\[" and "\]" in
781 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 */
783 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
784 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000785 int c;
786
787 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
788 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000789
790 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000791 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000792 *regmatch.endp[i] = NUL;
793 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
794 *regmatch.endp[i] = c;
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000797 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 continue;
799 }
800 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000801 {
802 if (regmatch.startp[i] == NULL)
803 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000807 {
808 if (regmatch.startp[i] == NULL)
809 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000813 {
814 if (regmatch.startp[i] == NULL)
815 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000819 {
820 if (regmatch.startp[i] == NULL)
821 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000823 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000824 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200825 {
826 if (linelen > errmsglen) {
827 /* linelen + null terminator */
828 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
829 goto qf_init_end;
830 errmsglen = linelen + 1;
831 }
832 vim_strncpy(errmsg, linebuf, linelen);
833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
835 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000836 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
837 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200839 if (len > errmsglen) {
840 /* len + null terminator */
841 if ((errmsg = vim_realloc(errmsg, len + 1))
842 == NULL)
843 goto qf_init_end;
844 errmsglen = len + 1;
845 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000846 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000849 {
850 if (regmatch.startp[i] == NULL)
851 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
855 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200856 char_u *match_ptr;
857
Bram Moolenaar4169da72006-06-20 18:49:32 +0000858 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
859 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200860 col = 0;
861 for (match_ptr = regmatch.startp[i];
862 match_ptr != regmatch.endp[i]; ++match_ptr)
863 {
864 ++col;
865 if (*match_ptr == TAB)
866 {
867 col += 7;
868 col -= col % 8;
869 }
870 }
871 ++col;
872 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
875 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000876 if (regmatch.startp[i] == NULL)
877 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000879 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000881 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
882 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000883 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
884 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000885 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
886 if (len > CMDBUFFSIZE - 5)
887 len = CMDBUFFSIZE - 5;
888 STRCPY(pattern, "^\\V");
889 STRNCAT(pattern, regmatch.startp[i], len);
890 pattern[len + 3] = '\\';
891 pattern[len + 4] = '$';
892 pattern[len + 5] = NUL;
893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 break;
895 }
896 }
897 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000898
Bram Moolenaar4770d092006-01-12 23:22:24 +0000899 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000901 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
903 if (idx == 'D') /* enter directory */
904 {
905 if (*namebuf == NUL)
906 {
907 EMSG(_("E379: Missing or empty directory name"));
908 goto error2;
909 }
910 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
911 goto error2;
912 }
913 else if (idx == 'X') /* leave directory */
914 directory = qf_pop_dir(&dir_stack);
915 }
916 namebuf[0] = NUL; /* no match found, remove file name */
917 lnum = 0; /* don't jump to this line */
918 valid = FALSE;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200919 if (linelen > errmsglen) {
920 /* linelen + null terminator */
921 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
922 goto qf_init_end;
923 errmsglen = linelen + 1;
924 }
925 /* copy whole line to error message */
926 vim_strncpy(errmsg, linebuf, linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000927 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 multiline = multiignore = FALSE;
929 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000930 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000932 /* honor %> item */
933 if (fmt_ptr->conthere)
934 fmt_start = fmt_ptr;
935
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100939 multiignore = FALSE; /* reset continuation */
940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
942 { /* continuation of multi-line msg */
943 if (qfprev == NULL)
944 goto error2;
945 if (*errmsg && !multiignore)
946 {
947 len = (int)STRLEN(qfprev->qf_text);
948 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
949 == NULL)
950 goto error2;
951 STRCPY(ptr, qfprev->qf_text);
952 vim_free(qfprev->qf_text);
953 qfprev->qf_text = ptr;
954 *(ptr += len) = '\n';
955 STRCPY(++ptr, errmsg);
956 }
957 if (qfprev->qf_nr == -1)
958 qfprev->qf_nr = enr;
959 if (vim_isprintc(type) && !qfprev->qf_type)
960 qfprev->qf_type = type; /* only printable chars allowed */
961 if (!qfprev->qf_lnum)
962 qfprev->qf_lnum = lnum;
963 if (!qfprev->qf_col)
964 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000965 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (!qfprev->qf_fnum)
967 qfprev->qf_fnum = qf_get_fnum(directory,
968 *namebuf || directory ? namebuf
969 : currfile && valid ? currfile : 0);
970 if (idx == 'Z')
971 multiline = multiignore = FALSE;
972 line_breakcheck();
973 continue;
974 }
975 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
976 {
977 /* global file names */
978 valid = FALSE;
979 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
980 {
981 if (*namebuf && idx == 'P')
982 currfile = qf_push_dir(namebuf, &file_stack);
983 else if (idx == 'Q')
984 currfile = qf_pop_dir(&file_stack);
985 *namebuf = NUL;
986 if (tail && *tail)
987 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000988 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 multiscan = TRUE;
990 goto restofline;
991 }
992 }
993 }
994 if (fmt_ptr->flags == '-') /* generally exclude this line */
995 {
996 if (multiline)
997 multiignore = TRUE; /* also exclude continuation lines */
998 continue;
999 }
1000 }
1001
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001002 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001004 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001006 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001007 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 errmsg,
1009 lnum,
1010 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001011 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001012 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 enr,
1014 type,
1015 valid) == FAIL)
1016 goto error2;
1017 line_breakcheck();
1018 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00001019 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001021 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001023 /* no valid entry found */
1024 qi->qf_lists[qi->qf_curlist].qf_ptr =
1025 qi->qf_lists[qi->qf_curlist].qf_start;
1026 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1027 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 else
1030 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001031 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1032 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1033 qi->qf_lists[qi->qf_curlist].qf_ptr =
1034 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001036 /* return number of matches */
1037 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 goto qf_init_ok;
1039 }
1040 EMSG(_(e_readerrf));
1041error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001042 qf_free(qi, qi->qf_curlist);
1043 qi->qf_listcount--;
1044 if (qi->qf_curlist > 0)
1045 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +00001047 if (fd != NULL)
1048 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
1050 {
1051 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02001052 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 vim_free(fmt_ptr);
1054 }
1055 qf_clean_dir_stack(&dir_stack);
1056 qf_clean_dir_stack(&file_stack);
1057qf_init_end:
1058 vim_free(namebuf);
1059 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001060 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 vim_free(fmtstr);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001062 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001065 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#endif
1067
1068 return retval;
1069}
1070
Bram Moolenaarfb604092014-07-23 15:55:00 +02001071 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001072qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001073{
1074 if (title != NULL)
1075 {
1076 char_u *p = alloc((int)STRLEN(title) + 2);
1077
1078 qi->qf_lists[qi->qf_curlist].qf_title = p;
1079 if (p != NULL)
1080 sprintf((char *)p, ":%s", (char *)title);
1081 }
1082}
1083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084/*
1085 * Prepare for adding a new quickfix list.
1086 */
1087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001088qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 int i;
1091
1092 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001093 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 * the current entry. This makes it possible to browse in a tree-like
1095 * way with ":grep'.
1096 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001097 while (qi->qf_listcount > qi->qf_curlist + 1)
1098 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
1100 /*
1101 * When the stack is full, remove to oldest entry
1102 * Otherwise, add a new entry.
1103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001104 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001106 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001108 qi->qf_lists[i - 1] = qi->qf_lists[i];
1109 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001112 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001113 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001114 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001117/*
1118 * Free a location list
1119 */
1120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001121ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001122{
1123 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001124 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001125
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001126 qi = *pqi;
1127 if (qi == NULL)
1128 return;
1129 *pqi = NULL; /* Remove reference to this list */
1130
1131 qi->qf_refcount--;
1132 if (qi->qf_refcount < 1)
1133 {
1134 /* No references to this location list */
1135 for (i = 0; i < qi->qf_listcount; ++i)
1136 qf_free(qi, i);
1137 vim_free(qi);
1138 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001139}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001140
1141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001142qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001143{
1144 int i;
1145 qf_info_T *qi = &ql_info;
1146
1147 if (wp != NULL)
1148 {
1149 /* location list */
1150 ll_free_all(&wp->w_llist);
1151 ll_free_all(&wp->w_llist_ref);
1152 }
1153 else
1154 /* quickfix list */
1155 for (i = 0; i < qi->qf_listcount; ++i)
1156 qf_free(qi, i);
1157}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159/*
1160 * Add an entry to the end of the list of errors.
1161 * Returns OK or FAIL.
1162 */
1163 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001164qf_add_entry(
1165 qf_info_T *qi, /* quickfix list */
1166 qfline_T **prevp, /* pointer to previously added entry or NULL */
1167 char_u *dir, /* optional directory name */
1168 char_u *fname, /* file name or NULL */
1169 int bufnum, /* buffer number or zero */
1170 char_u *mesg, /* message */
1171 long lnum, /* line number */
1172 int col, /* column */
1173 int vis_col, /* using visual column */
1174 char_u *pattern, /* search pattern */
1175 int nr, /* error number */
1176 int type, /* type character */
1177 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001179 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001181 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001183 if (bufnum != 0)
1184 qfp->qf_fnum = bufnum;
1185 else
1186 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1188 {
1189 vim_free(qfp);
1190 return FAIL;
1191 }
1192 qfp->qf_lnum = lnum;
1193 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001194 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001195 if (pattern == NULL || *pattern == NUL)
1196 qfp->qf_pattern = NULL;
1197 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1198 {
1199 vim_free(qfp->qf_text);
1200 vim_free(qfp);
1201 return FAIL;
1202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 qfp->qf_nr = nr;
1204 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1205 type = 0;
1206 qfp->qf_type = type;
1207 qfp->qf_valid = valid;
1208
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001209 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1210 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001212 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001213 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1214 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 qfp->qf_prev = qfp; /* first element points to itself */
1216 }
1217 else
1218 {
1219 qfp->qf_prev = *prevp;
1220 (*prevp)->qf_next = qfp;
1221 }
1222 qfp->qf_next = qfp; /* last element points to itself */
1223 qfp->qf_cleared = FALSE;
1224 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001225 ++qi->qf_lists[qi->qf_curlist].qf_count;
1226 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1227 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001229 qi->qf_lists[qi->qf_curlist].qf_index =
1230 qi->qf_lists[qi->qf_curlist].qf_count;
1231 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233
1234 return OK;
1235}
1236
1237/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001238 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001239 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001240 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001241ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001242{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001243 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001244
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001245 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1246 if (qi != NULL)
1247 {
1248 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1249 qi->qf_refcount++;
1250 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001251
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001252 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001253}
1254
1255/*
1256 * Return the location list for window 'wp'.
1257 * If not present, allocate a location list
1258 */
1259 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001260ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001261{
1262 if (IS_LL_WINDOW(wp))
1263 /* For a location list window, use the referenced location list */
1264 return wp->w_llist_ref;
1265
1266 /*
1267 * For a non-location list window, w_llist_ref should not point to a
1268 * location list.
1269 */
1270 ll_free_all(&wp->w_llist_ref);
1271
1272 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001273 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001274 return wp->w_llist;
1275}
1276
1277/*
1278 * Copy the location list from window "from" to window "to".
1279 */
1280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001281copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001282{
1283 qf_info_T *qi;
1284 int idx;
1285 int i;
1286
1287 /*
1288 * When copying from a location list window, copy the referenced
1289 * location list. For other windows, copy the location list for
1290 * that window.
1291 */
1292 if (IS_LL_WINDOW(from))
1293 qi = from->w_llist_ref;
1294 else
1295 qi = from->w_llist;
1296
1297 if (qi == NULL) /* no location list to copy */
1298 return;
1299
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001300 /* allocate a new location list */
1301 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001302 return;
1303
1304 to->w_llist->qf_listcount = qi->qf_listcount;
1305
1306 /* Copy the location lists one at a time */
1307 for (idx = 0; idx < qi->qf_listcount; idx++)
1308 {
1309 qf_list_T *from_qfl;
1310 qf_list_T *to_qfl;
1311
1312 to->w_llist->qf_curlist = idx;
1313
1314 from_qfl = &qi->qf_lists[idx];
1315 to_qfl = &to->w_llist->qf_lists[idx];
1316
1317 /* Some of the fields are populated by qf_add_entry() */
1318 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1319 to_qfl->qf_count = 0;
1320 to_qfl->qf_index = 0;
1321 to_qfl->qf_start = NULL;
1322 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001323 if (from_qfl->qf_title != NULL)
1324 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1325 else
1326 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001327
1328 if (from_qfl->qf_count)
1329 {
1330 qfline_T *from_qfp;
1331 qfline_T *prevp = NULL;
1332
1333 /* copy all the location entries in this list */
1334 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1335 ++i, from_qfp = from_qfp->qf_next)
1336 {
1337 if (qf_add_entry(to->w_llist, &prevp,
1338 NULL,
1339 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001340 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001341 from_qfp->qf_text,
1342 from_qfp->qf_lnum,
1343 from_qfp->qf_col,
1344 from_qfp->qf_viscol,
1345 from_qfp->qf_pattern,
1346 from_qfp->qf_nr,
1347 0,
1348 from_qfp->qf_valid) == FAIL)
1349 {
1350 qf_free_all(to);
1351 return;
1352 }
1353 /*
1354 * qf_add_entry() will not set the qf_num field, as the
1355 * directory and file names are not supplied. So the qf_fnum
1356 * field is copied here.
1357 */
1358 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1359 prevp->qf_type = from_qfp->qf_type; /* error type */
1360 if (from_qfl->qf_ptr == from_qfp)
1361 to_qfl->qf_ptr = prevp; /* current location */
1362 }
1363 }
1364
1365 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1366
1367 /* When no valid entries are present in the list, qf_ptr points to
1368 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001369 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001370 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001371 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001372 to_qfl->qf_index = 1;
1373 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001374 }
1375
1376 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1377}
1378
1379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 * get buffer number for file "dir.name"
1381 */
1382 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001383qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 if (fname == NULL || *fname == NUL) /* no file name */
1386 return 0;
1387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 char_u *ptr;
1389 int fnum;
1390
Bram Moolenaare60acc12011-05-10 16:41:25 +02001391#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001393#endif
1394#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (directory != NULL)
1396 slash_adjust(directory);
1397 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (directory != NULL && !vim_isAbsName(fname)
1400 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1401 {
1402 /*
1403 * Here we check if the file really exists.
1404 * This should normally be true, but if make works without
1405 * "leaving directory"-messages we might have missed a
1406 * directory change.
1407 */
1408 if (mch_getperm(ptr) < 0)
1409 {
1410 vim_free(ptr);
1411 directory = qf_guess_filepath(fname);
1412 if (directory)
1413 ptr = concat_fnames(directory, fname, TRUE);
1414 else
1415 ptr = vim_strsave(fname);
1416 }
1417 /* Use concatenated directory name and file name */
1418 fnum = buflist_add(ptr, 0);
1419 vim_free(ptr);
1420 return fnum;
1421 }
1422 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424}
1425
1426/*
1427 * push dirbuf onto the directory stack and return pointer to actual dir or
1428 * NULL on error
1429 */
1430 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001431qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 struct dir_stack_T *ds_new;
1434 struct dir_stack_T *ds_ptr;
1435
1436 /* allocate new stack element and hook it in */
1437 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1438 if (ds_new == NULL)
1439 return NULL;
1440
1441 ds_new->next = *stackptr;
1442 *stackptr = ds_new;
1443
1444 /* store directory on the stack */
1445 if (vim_isAbsName(dirbuf)
1446 || (*stackptr)->next == NULL
1447 || (*stackptr && dir_stack != *stackptr))
1448 (*stackptr)->dirname = vim_strsave(dirbuf);
1449 else
1450 {
1451 /* Okay we don't have an absolute path.
1452 * dirbuf must be a subdir of one of the directories on the stack.
1453 * Let's search...
1454 */
1455 ds_new = (*stackptr)->next;
1456 (*stackptr)->dirname = NULL;
1457 while (ds_new)
1458 {
1459 vim_free((*stackptr)->dirname);
1460 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1461 TRUE);
1462 if (mch_isdir((*stackptr)->dirname) == TRUE)
1463 break;
1464
1465 ds_new = ds_new->next;
1466 }
1467
1468 /* clean up all dirs we already left */
1469 while ((*stackptr)->next != ds_new)
1470 {
1471 ds_ptr = (*stackptr)->next;
1472 (*stackptr)->next = (*stackptr)->next->next;
1473 vim_free(ds_ptr->dirname);
1474 vim_free(ds_ptr);
1475 }
1476
1477 /* Nothing found -> it must be on top level */
1478 if (ds_new == NULL)
1479 {
1480 vim_free((*stackptr)->dirname);
1481 (*stackptr)->dirname = vim_strsave(dirbuf);
1482 }
1483 }
1484
1485 if ((*stackptr)->dirname != NULL)
1486 return (*stackptr)->dirname;
1487 else
1488 {
1489 ds_ptr = *stackptr;
1490 *stackptr = (*stackptr)->next;
1491 vim_free(ds_ptr);
1492 return NULL;
1493 }
1494}
1495
1496
1497/*
1498 * pop dirbuf from the directory stack and return previous directory or NULL if
1499 * stack is empty
1500 */
1501 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001502qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 struct dir_stack_T *ds_ptr;
1505
1506 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1507 * What to do if it isn't? */
1508
1509 /* pop top element and free it */
1510 if (*stackptr != NULL)
1511 {
1512 ds_ptr = *stackptr;
1513 *stackptr = (*stackptr)->next;
1514 vim_free(ds_ptr->dirname);
1515 vim_free(ds_ptr);
1516 }
1517
1518 /* return NEW top element as current dir or NULL if stack is empty*/
1519 return *stackptr ? (*stackptr)->dirname : NULL;
1520}
1521
1522/*
1523 * clean up directory stack
1524 */
1525 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001526qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 struct dir_stack_T *ds_ptr;
1529
1530 while ((ds_ptr = *stackptr) != NULL)
1531 {
1532 *stackptr = (*stackptr)->next;
1533 vim_free(ds_ptr->dirname);
1534 vim_free(ds_ptr);
1535 }
1536}
1537
1538/*
1539 * Check in which directory of the directory stack the given file can be
1540 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001541 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 * Cleans up intermediate directory entries.
1543 *
1544 * TODO: How to solve the following problem?
1545 * If we have the this directory tree:
1546 * ./
1547 * ./aa
1548 * ./aa/bb
1549 * ./bb
1550 * ./bb/x.c
1551 * and make says:
1552 * making all in aa
1553 * making all in bb
1554 * x.c:9: Error
1555 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1556 * qf_guess_filepath will return NULL.
1557 */
1558 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001559qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 struct dir_stack_T *ds_ptr;
1562 struct dir_stack_T *ds_tmp;
1563 char_u *fullname;
1564
1565 /* no dirs on the stack - there's nothing we can do */
1566 if (dir_stack == NULL)
1567 return NULL;
1568
1569 ds_ptr = dir_stack->next;
1570 fullname = NULL;
1571 while (ds_ptr)
1572 {
1573 vim_free(fullname);
1574 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1575
1576 /* If concat_fnames failed, just go on. The worst thing that can happen
1577 * is that we delete the entire stack.
1578 */
1579 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1580 break;
1581
1582 ds_ptr = ds_ptr->next;
1583 }
1584
1585 vim_free(fullname);
1586
1587 /* clean up all dirs we already left */
1588 while (dir_stack->next != ds_ptr)
1589 {
1590 ds_tmp = dir_stack->next;
1591 dir_stack->next = dir_stack->next->next;
1592 vim_free(ds_tmp->dirname);
1593 vim_free(ds_tmp);
1594 }
1595
1596 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1597
1598}
1599
1600/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001601 * When loading a file from the quickfix, the auto commands may modify it.
1602 * This may invalidate the current quickfix entry. This function checks
1603 * whether a entry is still present in the quickfix.
1604 * Similar to location list.
1605 */
1606 static int
1607is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1608{
1609 qf_list_T *qfl;
1610 qfline_T *qfp;
1611 int i;
1612
1613 qfl = &qi->qf_lists[qi->qf_curlist];
1614
1615 /* Search for the entry in the current list */
1616 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1617 ++i, qfp = qfp->qf_next)
1618 if (qfp == qf_ptr)
1619 break;
1620
1621 if (i == qfl->qf_count) /* Entry is not found */
1622 return FALSE;
1623
1624 return TRUE;
1625}
1626
1627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 * jump to a quickfix line
1629 * if dir == FORWARD go "errornr" valid entries forward
1630 * if dir == BACKWARD go "errornr" valid entries backward
1631 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1632 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1633 * else if "errornr" is zero, redisplay the same line
1634 * else go to entry "errornr"
1635 */
1636 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001637qf_jump(
1638 qf_info_T *qi,
1639 int dir,
1640 int errornr,
1641 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001643 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001644 qfline_T *qf_ptr;
1645 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 int qf_index;
1647 int old_qf_fnum;
1648 int old_qf_index;
1649 int prev_index;
1650 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1651 char_u *err = e_no_more_items;
1652 linenr_T i;
1653 buf_T *old_curbuf;
1654 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 colnr_T screen_col;
1656 colnr_T char_col;
1657 char_u *line;
1658#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001659 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001660 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 int opened_window = FALSE;
1662 win_T *win;
1663 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001664 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001666 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 int print_message = TRUE;
1668 int len;
1669#ifdef FEAT_FOLDING
1670 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1671#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001672 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001673 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001675 if (qi == NULL)
1676 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001677
1678 if (qi->qf_curlist >= qi->qf_listcount
1679 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 EMSG(_(e_quickfix));
1682 return;
1683 }
1684
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001685 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001687 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 old_qf_index = qf_index;
1689 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1690 {
1691 while (errornr--)
1692 {
1693 old_qf_ptr = qf_ptr;
1694 prev_index = qf_index;
1695 old_qf_fnum = qf_ptr->qf_fnum;
1696 do
1697 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001698 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 || qf_ptr->qf_next == NULL)
1700 {
1701 qf_ptr = old_qf_ptr;
1702 qf_index = prev_index;
1703 if (err != NULL)
1704 {
1705 EMSG(_(err));
1706 goto theend;
1707 }
1708 errornr = 0;
1709 break;
1710 }
1711 ++qf_index;
1712 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001713 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1714 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1716 err = NULL;
1717 }
1718 }
1719 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1720 {
1721 while (errornr--)
1722 {
1723 old_qf_ptr = qf_ptr;
1724 prev_index = qf_index;
1725 old_qf_fnum = qf_ptr->qf_fnum;
1726 do
1727 {
1728 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1729 {
1730 qf_ptr = old_qf_ptr;
1731 qf_index = prev_index;
1732 if (err != NULL)
1733 {
1734 EMSG(_(err));
1735 goto theend;
1736 }
1737 errornr = 0;
1738 break;
1739 }
1740 --qf_index;
1741 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001742 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1743 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1745 err = NULL;
1746 }
1747 }
1748 else if (errornr != 0) /* go to specified number */
1749 {
1750 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1751 {
1752 --qf_index;
1753 qf_ptr = qf_ptr->qf_prev;
1754 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001755 while (errornr > qf_index && qf_index <
1756 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 && qf_ptr->qf_next != NULL)
1758 {
1759 ++qf_index;
1760 qf_ptr = qf_ptr->qf_next;
1761 }
1762 }
1763
1764#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001765 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1766 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 /* No need to print the error message if it's visible in the error
1768 * window */
1769 print_message = FALSE;
1770
1771 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001772 * For ":helpgrep" find a help window or open one.
1773 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001774 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001775 {
1776 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001777
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001778 if (cmdmod.tab != 0)
1779 wp = NULL;
1780 else
1781 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1782 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1783 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001784 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1785 win_enter(wp, TRUE);
1786 else
1787 {
1788 /*
1789 * Split off help window; put it at far top if no position
1790 * specified, the current window is vertically split and narrow.
1791 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001792 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001793 if (cmdmod.split == 0 && curwin->w_width != Columns
1794 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001795 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001796 if (qi != &ql_info)
1797 flags |= WSP_NEWLOC; /* don't copy the location list */
1798
1799 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001800 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001801 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001802
1803 if (curwin->w_height < p_hh)
1804 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001805
1806 if (qi != &ql_info) /* not a quickfix list */
1807 {
1808 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001809 curwin->w_llist = qi;
1810 qi->qf_refcount++;
1811 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001812 }
1813
1814 if (!p_im)
1815 restart_edit = 0; /* don't want insert mode in help file */
1816 }
1817
1818 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 * If currently in the quickfix window, find another window to show the
1820 * file in.
1821 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001822 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001824 win_T *usable_win_ptr = NULL;
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 /*
1827 * If there is no file specified, we don't know where to go.
1828 * But do advance, otherwise ":cn" gets stuck.
1829 */
1830 if (qf_ptr->qf_fnum == 0)
1831 goto theend;
1832
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001833 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001834
1835 ll_ref = curwin->w_llist_ref;
1836 if (ll_ref != NULL)
1837 {
1838 /* Find a window using the same location list that is not a
1839 * quickfix window. */
1840 FOR_ALL_WINDOWS(usable_win_ptr)
1841 if (usable_win_ptr->w_llist == ll_ref
1842 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001843 {
1844 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001845 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001846 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001847 }
1848
1849 if (!usable_win)
1850 {
1851 /* Locate a window showing a normal buffer */
1852 FOR_ALL_WINDOWS(win)
1853 if (win->w_buffer->b_p_bt[0] == NUL)
1854 {
1855 usable_win = 1;
1856 break;
1857 }
1858 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001861 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001862 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001864 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001865 {
1866 tabpage_T *tp;
1867 win_T *wp;
1868
1869 FOR_ALL_TAB_WINDOWS(tp, wp)
1870 {
1871 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1872 {
1873 goto_tabpage_win(tp, wp);
1874 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001875 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001876 }
1877 }
1878 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001879win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001880
1881 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001882 * If there is only one window and it is the quickfix window, create a
1883 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001884 */
1885 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001887 flags = WSP_ABOVE;
1888 if (ll_ref != NULL)
1889 flags |= WSP_NEWLOC;
1890 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 goto failed; /* not enough room for window */
1892 opened_window = TRUE; /* close it when fail */
1893 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001894 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001895 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001896 if (ll_ref != NULL)
1897 {
1898 /* The new window should use the location list from the
1899 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001900 curwin->w_llist = ll_ref;
1901 ll_ref->qf_refcount++;
1902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 else
1905 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906 if (curwin->w_llist_ref != NULL)
1907 {
1908 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001909 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001910 if (win == NULL)
1911 {
1912 /* Find the window showing the selected file */
1913 FOR_ALL_WINDOWS(win)
1914 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1915 break;
1916 if (win == NULL)
1917 {
1918 /* Find a previous usable window */
1919 win = curwin;
1920 do
1921 {
1922 if (win->w_buffer->b_p_bt[0] == NUL)
1923 break;
1924 if (win->w_prev == NULL)
1925 win = lastwin; /* wrap around the top */
1926 else
1927 win = win->w_prev; /* go to previous window */
1928 } while (win != curwin);
1929 }
1930 }
1931 win_goto(win);
1932
1933 /* If the location list for the window is not set, then set it
1934 * to the location list from the location window */
1935 if (win->w_llist == NULL)
1936 {
1937 win->w_llist = ll_ref;
1938 ll_ref->qf_refcount++;
1939 }
1940 }
1941 else
1942 {
1943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 /*
1945 * Try to find a window that shows the right buffer.
1946 * Default to the window just above the quickfix buffer.
1947 */
1948 win = curwin;
1949 altwin = NULL;
1950 for (;;)
1951 {
1952 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1953 break;
1954 if (win->w_prev == NULL)
1955 win = lastwin; /* wrap around the top */
1956 else
1957 win = win->w_prev; /* go to previous window */
1958
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001959 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961 /* Didn't find it, go to the window before the quickfix
1962 * window. */
1963 if (altwin != NULL)
1964 win = altwin;
1965 else if (curwin->w_prev != NULL)
1966 win = curwin->w_prev;
1967 else
1968 win = curwin->w_next;
1969 break;
1970 }
1971
1972 /* Remember a usable window. */
1973 if (altwin == NULL && !win->w_p_pvw
1974 && win->w_buffer->b_p_bt[0] == NUL)
1975 altwin = win;
1976 }
1977
1978 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 }
1981 }
1982#endif
1983
1984 /*
1985 * If there is a file name,
1986 * read the wanted file if needed, and check autowrite etc.
1987 */
1988 old_curbuf = curbuf;
1989 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001990
1991 if (qf_ptr->qf_fnum != 0)
1992 {
1993 if (qf_ptr->qf_type == 1)
1994 {
1995 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1996 * set b_p_ro flag). */
1997 if (!can_abandon(curbuf, forceit))
1998 {
1999 EMSG(_(e_nowrtmsg));
2000 ok = FALSE;
2001 }
2002 else
2003 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002004 ECMD_HIDE + ECMD_SET_HELP,
2005 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002006 }
2007 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002008 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002009 int old_qf_curlist = qi->qf_curlist;
2010 int is_abort = FALSE;
2011
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002012 ok = buflist_getfile(qf_ptr->qf_fnum,
2013 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002014 if (qi != &ql_info && !win_valid(oldwin))
2015 {
2016 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002017 is_abort = TRUE;
2018 opened_window = FALSE;
2019 }
2020 else if (old_qf_curlist != qi->qf_curlist
2021 || !is_qf_entry_present(qi, qf_ptr))
2022 {
2023 if (qi == &ql_info)
2024 EMSG(_("E925: Current quickfix was changed"));
2025 else
2026 EMSG(_("E926: Current location list was changed"));
2027 is_abort = TRUE;
2028 }
2029
2030 if (is_abort)
2031 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002032 ok = FALSE;
2033 qi = NULL;
2034 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002035 }
2036 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002037 }
2038
2039 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 /* When not switched to another buffer, still need to set pc mark */
2042 if (curbuf == old_curbuf)
2043 setpcmark();
2044
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002045 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002047 /*
2048 * Go to line with error, unless qf_lnum is 0.
2049 */
2050 i = qf_ptr->qf_lnum;
2051 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002053 if (i > curbuf->b_ml.ml_line_count)
2054 i = curbuf->b_ml.ml_line_count;
2055 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002057 if (qf_ptr->qf_col > 0)
2058 {
2059 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002060#ifdef FEAT_VIRTUALEDIT
2061 curwin->w_cursor.coladd = 0;
2062#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002063 if (qf_ptr->qf_viscol == TRUE)
2064 {
2065 /*
2066 * Check each character from the beginning of the error
2067 * line up to the error column. For each tab character
2068 * found, reduce the error column value by the length of
2069 * a tab character.
2070 */
2071 line = ml_get_curline();
2072 screen_col = 0;
2073 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2074 {
2075 if (*line == NUL)
2076 break;
2077 if (*line++ == '\t')
2078 {
2079 curwin->w_cursor.col -= 7 - (screen_col % 8);
2080 screen_col += 8 - (screen_col % 8);
2081 }
2082 else
2083 ++screen_col;
2084 }
2085 }
2086 check_cursor();
2087 }
2088 else
2089 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
2091 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002092 {
2093 pos_T save_cursor;
2094
2095 /* Move the cursor to the first line in the buffer */
2096 save_cursor = curwin->w_cursor;
2097 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002098 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2099 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002100 curwin->w_cursor = save_cursor;
2101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
2103#ifdef FEAT_FOLDING
2104 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2105 foldOpenCursor();
2106#endif
2107 if (print_message)
2108 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002109 /* Update the screen before showing the message, unless the screen
2110 * scrolled up. */
2111 if (!msg_scrolled)
2112 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002114 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2116 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2117 /* Add the message, skipping leading whitespace and newlines. */
2118 len = (int)STRLEN(IObuff);
2119 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2120
2121 /* Output the message. Overwrite to avoid scrolling when the 'O'
2122 * flag is present in 'shortmess'; But when not jumping, print the
2123 * whole message. */
2124 i = msg_scroll;
2125 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2126 msg_scroll = TRUE;
2127 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2128 msg_scroll = FALSE;
2129 msg_attr_keep(IObuff, 0, TRUE);
2130 msg_scroll = i;
2131 }
2132 }
2133 else
2134 {
2135#ifdef FEAT_WINDOWS
2136 if (opened_window)
2137 win_close(curwin, TRUE); /* Close opened window */
2138#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002139 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
2141 /*
2142 * Couldn't open file, so put index back where it was. This could
2143 * happen if the file was readonly and we changed something.
2144 */
2145#ifdef FEAT_WINDOWS
2146failed:
2147#endif
2148 qf_ptr = old_qf_ptr;
2149 qf_index = old_qf_index;
2150 }
2151 }
2152theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002153 if (qi != NULL)
2154 {
2155 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2156 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158#ifdef FEAT_WINDOWS
2159 if (p_swb != old_swb && opened_window)
2160 {
2161 /* Restore old 'switchbuf' value, but not when an autocommand or
2162 * modeline has changed the value. */
2163 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002166 swb_flags = old_swb_flags;
2167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 else
2169 free_string_option(old_swb);
2170 }
2171#endif
2172}
2173
2174/*
2175 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002176 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
2178 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002179qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002181 buf_T *buf;
2182 char_u *fname;
2183 qfline_T *qfp;
2184 int i;
2185 int idx1 = 1;
2186 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002187 char_u *arg = eap->arg;
2188 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002192 if (eap->cmdidx == CMD_llist)
2193 {
2194 qi = GET_LOC_LIST(curwin);
2195 if (qi == NULL)
2196 {
2197 EMSG(_(e_loclist));
2198 return;
2199 }
2200 }
2201
2202 if (qi->qf_curlist >= qi->qf_listcount
2203 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
2205 EMSG(_(e_quickfix));
2206 return;
2207 }
2208 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2209 {
2210 EMSG(_(e_trailing));
2211 return;
2212 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002213 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (idx1 < 0)
2215 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2216 if (idx2 < 0)
2217 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2218
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002219 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002221 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2222 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2225 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002226 msg_putchar('\n');
2227 if (got_int)
2228 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002229
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002230 fname = NULL;
2231 if (qfp->qf_fnum != 0
2232 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2233 {
2234 fname = buf->b_fname;
2235 if (qfp->qf_type == 1) /* :helpgrep */
2236 fname = gettail(fname);
2237 }
2238 if (fname == NULL)
2239 sprintf((char *)IObuff, "%2d", i);
2240 else
2241 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2242 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002243 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002244 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2245 if (qfp->qf_lnum == 0)
2246 IObuff[0] = NUL;
2247 else if (qfp->qf_col == 0)
2248 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2249 else
2250 sprintf((char *)IObuff, ":%ld col %d",
2251 qfp->qf_lnum, qfp->qf_col);
2252 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2253 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2254 msg_puts_attr(IObuff, hl_attr(HLF_N));
2255 if (qfp->qf_pattern != NULL)
2256 {
2257 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2258 STRCAT(IObuff, ":");
2259 msg_puts(IObuff);
2260 }
2261 msg_puts((char_u *)" ");
2262
2263 /* Remove newlines and leading whitespace from the text. For an
2264 * unrecognized line keep the indent, the compiler may mark a word
2265 * with ^^^^. */
2266 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2268 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002269 msg_prt_line(IObuff, FALSE);
2270 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002272
2273 qfp = qfp->qf_next;
2274 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 ui_breakcheck();
2276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277}
2278
2279/*
2280 * Remove newlines and leading whitespace from an error message.
2281 * Put the result in "buf[bufsize]".
2282 */
2283 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002284qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 int i;
2287 char_u *p = text;
2288
2289 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2290 {
2291 if (*p == '\n')
2292 {
2293 buf[i] = ' ';
2294 while (*++p != NUL)
2295 if (!vim_iswhite(*p) && *p != '\n')
2296 break;
2297 }
2298 else
2299 buf[i] = *p++;
2300 }
2301 buf[i] = NUL;
2302}
2303
2304/*
2305 * ":colder [count]": Up in the quickfix stack.
2306 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307 * ":lolder [count]": Up in the location list stack.
2308 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 */
2310 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002311qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 int count;
2315
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002316 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2317 {
2318 qi = GET_LOC_LIST(curwin);
2319 if (qi == NULL)
2320 {
2321 EMSG(_(e_loclist));
2322 return;
2323 }
2324 }
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 if (eap->addr_count != 0)
2327 count = eap->line2;
2328 else
2329 count = 1;
2330 while (count--)
2331 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002332 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002334 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
2336 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002337 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341 else
2342 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002343 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002348 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002351 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352}
2353
2354 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002355qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
2357 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002358 qi->qf_curlist + 1, qi->qf_listcount,
2359 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002361 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362#endif
2363}
2364
2365/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002366 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 */
2368 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002369qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002371 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002372 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002374 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002376 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002377 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002378 {
2379 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002380 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002381 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2382 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002383 if (stop)
2384 /* Somehow qf_count may have an incorrect value, set it to 1
2385 * to avoid crashing when it's wrong.
2386 * TODO: Avoid qf_count being incorrect. */
2387 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002388 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002389 qi->qf_lists[idx].qf_start = qfp;
2390 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002392 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002393 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002394 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395}
2396
2397/*
2398 * qf_mark_adjust: adjust marks
2399 */
2400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002401qf_mark_adjust(
2402 win_T *wp,
2403 linenr_T line1,
2404 linenr_T line2,
2405 long amount,
2406 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002408 int i;
2409 qfline_T *qfp;
2410 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002411 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002413 if (wp != NULL)
2414 {
2415 if (wp->w_llist == NULL)
2416 return;
2417 qi = wp->w_llist;
2418 }
2419
2420 for (idx = 0; idx < qi->qf_listcount; ++idx)
2421 if (qi->qf_lists[idx].qf_count)
2422 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2423 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (qfp->qf_fnum == curbuf->b_fnum)
2425 {
2426 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2427 {
2428 if (amount == MAXLNUM)
2429 qfp->qf_cleared = TRUE;
2430 else
2431 qfp->qf_lnum += amount;
2432 }
2433 else if (amount_after && qfp->qf_lnum > line2)
2434 qfp->qf_lnum += amount_after;
2435 }
2436}
2437
2438/*
2439 * Make a nice message out of the error character and the error number:
2440 * char number message
2441 * e or E 0 " error"
2442 * w or W 0 " warning"
2443 * i or I 0 " info"
2444 * 0 0 ""
2445 * other 0 " c"
2446 * e or E n " error n"
2447 * w or W n " warning n"
2448 * i or I n " info n"
2449 * 0 n " error n"
2450 * other n " c n"
2451 * 1 x "" :helpgrep
2452 */
2453 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002454qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
2456 static char_u buf[20];
2457 static char_u cc[3];
2458 char_u *p;
2459
2460 if (c == 'W' || c == 'w')
2461 p = (char_u *)" warning";
2462 else if (c == 'I' || c == 'i')
2463 p = (char_u *)" info";
2464 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2465 p = (char_u *)" error";
2466 else if (c == 0 || c == 1)
2467 p = (char_u *)"";
2468 else
2469 {
2470 cc[0] = ' ';
2471 cc[1] = c;
2472 cc[2] = NUL;
2473 p = cc;
2474 }
2475
2476 if (nr <= 0)
2477 return p;
2478
2479 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2480 return buf;
2481}
2482
2483#if defined(FEAT_WINDOWS) || defined(PROTO)
2484/*
2485 * ":cwindow": open the quickfix window if we have errors to display,
2486 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002487 * ":lwindow": open the location list window if we have locations to display,
2488 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
2490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002491ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002493 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 win_T *win;
2495
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002496 if (eap->cmdidx == CMD_lwindow)
2497 {
2498 qi = GET_LOC_LIST(curwin);
2499 if (qi == NULL)
2500 return;
2501 }
2502
2503 /* Look for an existing quickfix window. */
2504 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
2506 /*
2507 * If a quickfix window is open but we have no errors to display,
2508 * close the window. If a quickfix window is not open, then open
2509 * it if we have errors; otherwise, leave it closed.
2510 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002511 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002512 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002513 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 if (win != NULL)
2516 ex_cclose(eap);
2517 }
2518 else if (win == NULL)
2519 ex_copen(eap);
2520}
2521
2522/*
2523 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002524 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002527ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002529 win_T *win = NULL;
2530 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002532 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2533 {
2534 qi = GET_LOC_LIST(curwin);
2535 if (qi == NULL)
2536 return;
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002539 /* Find existing quickfix window and close it. */
2540 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (win != NULL)
2542 win_close(win, FALSE);
2543}
2544
2545/*
2546 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002547 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 */
2549 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002550ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002552 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002555 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002556 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002557 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002559 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2560 {
2561 qi = GET_LOC_LIST(curwin);
2562 if (qi == NULL)
2563 {
2564 EMSG(_(e_loclist));
2565 return;
2566 }
2567 }
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (eap->addr_count != 0)
2570 height = eap->line2;
2571 else
2572 height = QF_WINHEIGHT;
2573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575#ifdef FEAT_GUI
2576 need_mouse_correct = TRUE;
2577#endif
2578
2579 /*
2580 * Find existing quickfix window, or open a new one.
2581 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002582 win = qf_find_win(qi);
2583
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002584 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002587 if (eap->addr_count != 0)
2588 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002589 if (cmdmod.split & WSP_VERT)
2590 {
2591 if (height != W_WIDTH(win))
2592 win_setwidth(height);
2593 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002594 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002595 win_setheight(height);
2596 }
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 else
2599 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002600 qf_buf = qf_find_buf(qi);
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 /* The current window becomes the previous window afterwards. */
2603 win = curwin;
2604
Bram Moolenaar77642c02012-11-20 17:55:10 +01002605 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2606 && cmdmod.split == 0)
2607 /* Create the new window at the very bottom, except when
2608 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002609 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002610 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002612 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002614 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002616 /*
2617 * For the location list window, create a reference to the
2618 * location list from the window 'win'.
2619 */
2620 curwin->w_llist_ref = win->w_llist;
2621 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002623
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002624 if (oldwin != curwin)
2625 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002626 if (qf_buf != NULL)
2627 /* Use the existing quickfix buffer */
2628 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002629 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002630 else
2631 {
2632 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002633 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002634 /* switch off 'swapfile' */
2635 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2636 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002637 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002638 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002639 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002640#ifdef FEAT_DIFF
2641 curwin->w_p_diff = FALSE;
2642#endif
2643#ifdef FEAT_FOLDING
2644 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2645 OPT_LOCAL);
2646#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002649 /* Only set the height when still in the same tab page and there is no
2650 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002651 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 win_setheight(height);
2653 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2654 if (win_valid(win))
2655 prevwin = win;
2656 }
2657
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002658 qf_set_title_var(qi);
2659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 /*
2661 * Fill the buffer with the quickfix list.
2662 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002663 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002665 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 curwin->w_cursor.col = 0;
2667 check_cursor();
2668 update_topline(); /* scroll to show the line */
2669}
2670
2671/*
2672 * Return the number of the current entry (line number in the quickfix
2673 * window).
2674 */
2675 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002676qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002678 qf_info_T *qi = &ql_info;
2679
2680 if (IS_LL_WINDOW(wp))
2681 /* In the location list window, use the referenced location list */
2682 qi = wp->w_llist_ref;
2683
2684 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685}
2686
2687/*
2688 * Update the cursor position in the quickfix window to the current error.
2689 * Return TRUE if there is a quickfix window.
2690 */
2691 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002692qf_win_pos_update(
2693 qf_info_T *qi,
2694 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002697 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 /*
2700 * Put the cursor on the current error in the quickfix window, so that
2701 * it's viewable.
2702 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002703 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (win != NULL
2705 && qf_index <= win->w_buffer->b_ml.ml_line_count
2706 && old_qf_index != qf_index)
2707 {
2708 win_T *old_curwin = curwin;
2709
2710 curwin = win;
2711 curbuf = win->w_buffer;
2712 if (qf_index > old_qf_index)
2713 {
2714 curwin->w_redraw_top = old_qf_index;
2715 curwin->w_redraw_bot = qf_index;
2716 }
2717 else
2718 {
2719 curwin->w_redraw_top = qf_index;
2720 curwin->w_redraw_bot = old_qf_index;
2721 }
2722 curwin->w_cursor.lnum = qf_index;
2723 curwin->w_cursor.col = 0;
2724 update_topline(); /* scroll to show the line */
2725 redraw_later(VALID);
2726 curwin->w_redr_status = TRUE; /* update ruler */
2727 curwin = old_curwin;
2728 curbuf = curwin->w_buffer;
2729 }
2730 return win != NULL;
2731}
2732
2733/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002734 * Check whether the given window is displaying the specified quickfix/location
2735 * list buffer
2736 */
2737 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002738is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002739{
2740 /*
2741 * A window displaying the quickfix buffer will have the w_llist_ref field
2742 * set to NULL.
2743 * A window displaying a location list buffer will have the w_llist_ref
2744 * pointing to the location list.
2745 */
2746 if (bt_quickfix(win->w_buffer))
2747 if ((qi == &ql_info && win->w_llist_ref == NULL)
2748 || (qi != &ql_info && win->w_llist_ref == qi))
2749 return TRUE;
2750
2751 return FALSE;
2752}
2753
2754/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002755 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002756 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002757 */
2758 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002759qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002760{
2761 win_T *win;
2762
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002763 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002764 if (is_qf_win(win, qi))
2765 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002766
2767 return win;
2768}
2769
2770/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002771 * Find a quickfix buffer.
2772 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 */
2774 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002775qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002777 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002778 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
Bram Moolenaar9c102382006-05-03 21:26:49 +00002780 FOR_ALL_TAB_WINDOWS(tp, win)
2781 if (is_qf_win(win, qi))
2782 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783
Bram Moolenaar9c102382006-05-03 21:26:49 +00002784 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785}
2786
2787/*
2788 * Find the quickfix buffer. If it exists, update the contents.
2789 */
2790 static void
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002791qf_update_buffer(qf_info_T *qi, int update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
2793 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002794 win_T *win;
2795 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002799 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (buf != NULL)
2801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 /* set curwin/curbuf to buf and save a few things */
2803 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002805 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002806 {
2807 curwin_save = curwin;
2808 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002809 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002810 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002811 }
2812
Bram Moolenaar6920c722016-01-22 22:44:10 +01002813 qf_fill_buffer(qi);
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 /* restore curwin/curbuf and a few other things */
2816 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002818 if (update_cursor)
2819 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
2821}
2822
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002823/*
2824 * Set "w:quickfix_title" if "qi" has a title.
2825 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002826 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002827qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002828{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002829 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2830 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002831 qi->qf_lists[qi->qf_curlist].qf_title);
2832}
2833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834/*
2835 * Fill current buffer with quickfix errors, replacing any previous contents.
2836 * curbuf must be the quickfix buffer!
2837 */
2838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002839qf_fill_buffer(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002841 linenr_T lnum;
2842 qfline_T *qfp;
2843 buf_T *errbuf;
2844 int len;
2845 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
2847 /* delete all existing lines */
2848 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2849 (void)ml_delete((linenr_T)1, FALSE);
2850
2851 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002852 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
2854 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002855 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2856 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 if (qfp->qf_fnum != 0
2859 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2860 && errbuf->b_fname != NULL)
2861 {
2862 if (qfp->qf_type == 1) /* :helpgrep */
2863 STRCPY(IObuff, gettail(errbuf->b_fname));
2864 else
2865 STRCPY(IObuff, errbuf->b_fname);
2866 len = (int)STRLEN(IObuff);
2867 }
2868 else
2869 len = 0;
2870 IObuff[len++] = '|';
2871
2872 if (qfp->qf_lnum > 0)
2873 {
2874 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2875 len += (int)STRLEN(IObuff + len);
2876
2877 if (qfp->qf_col > 0)
2878 {
2879 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2880 len += (int)STRLEN(IObuff + len);
2881 }
2882
2883 sprintf((char *)IObuff + len, "%s",
2884 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2885 len += (int)STRLEN(IObuff + len);
2886 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002887 else if (qfp->qf_pattern != NULL)
2888 {
2889 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2890 len += (int)STRLEN(IObuff + len);
2891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 IObuff[len++] = '|';
2893 IObuff[len++] = ' ';
2894
2895 /* Remove newlines and leading whitespace from the text.
2896 * For an unrecognized line keep the indent, the compiler may
2897 * mark a word with ^^^^. */
2898 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2899 IObuff + len, IOSIZE - len);
2900
2901 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2902 == FAIL)
2903 break;
2904 qfp = qfp->qf_next;
2905 }
2906 /* Delete the empty line which is now at the end */
2907 (void)ml_delete(lnum + 1, FALSE);
2908 }
2909
2910 /* correct cursor position */
2911 check_lnums(TRUE);
2912
2913 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2914 * resembles reading a file into a buffer, it's more logical when using
2915 * autocommands. */
2916 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2917 curbuf->b_p_ma = FALSE;
2918
2919#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002920 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2922 FALSE, curbuf);
2923 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2924 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002925 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#endif
2927
2928 /* make sure it will be redrawn */
2929 redraw_curbuf_later(NOT_VALID);
2930
2931 /* Restore KeyTyped, setting 'filetype' may reset it. */
2932 KeyTyped = old_KeyTyped;
2933}
2934
2935#endif /* FEAT_WINDOWS */
2936
2937/*
2938 * Return TRUE if "buf" is the quickfix buffer.
2939 */
2940 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002941bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002943 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945
2946/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002947 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2948 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
2950 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002951bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002953 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2954 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955}
2956
2957/*
2958 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2959 */
2960 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002961bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002963 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964}
2965
2966 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002967bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 if (bt_dontwrite(buf))
2970 {
2971 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2972 return TRUE;
2973 }
2974 return FALSE;
2975}
2976
2977/*
2978 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2979 * and 'bufhidden'.
2980 */
2981 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002982buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2985 switch (buf->b_p_bh[0])
2986 {
2987 case 'u': /* "unload" */
2988 case 'w': /* "wipe" */
2989 case 'd': return FALSE; /* "delete" */
2990 case 'h': return TRUE; /* "hide" */
2991 }
2992 return (p_hid || cmdmod.hide);
2993}
2994
2995/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002996 * Return TRUE when using ":vimgrep" for ":grep".
2997 */
2998 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002999grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003000{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003001 return ((cmdidx == CMD_grep
3002 || cmdidx == CMD_lgrep
3003 || cmdidx == CMD_grepadd
3004 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003005 && STRCMP("internal",
3006 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3007}
3008
3009/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003010 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 */
3012 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003013ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003015 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 char_u *cmd;
3017 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003018 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003019 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003020 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003021#ifdef FEAT_AUTOCMD
3022 char_u *au_name = NULL;
3023
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003024 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3025 if (grep_internal(eap->cmdidx))
3026 {
3027 ex_vimgrep(eap);
3028 return;
3029 }
3030
Bram Moolenaar7c626922005-02-07 22:01:03 +00003031 switch (eap->cmdidx)
3032 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003033 case CMD_make: au_name = (char_u *)"make"; break;
3034 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3035 case CMD_grep: au_name = (char_u *)"grep"; break;
3036 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3037 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3038 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003039 default: break;
3040 }
3041 if (au_name != NULL)
3042 {
3043 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3044 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003045# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003046 if (did_throw || force_abort)
3047 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003048# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003049 }
3050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051
Bram Moolenaara6557602006-02-04 22:43:20 +00003052 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3053 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003054 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003055
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003057 fname = get_mef_name();
3058 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003060 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 /*
3063 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3064 */
3065 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3066 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003067 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 cmd = alloc(len);
3069 if (cmd == NULL)
3070 return;
3071 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3072 (char *)p_shq);
3073 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003074 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 /*
3076 * Output a newline if there's something else than the :make command that
3077 * was typed (in which case the cursor is in column 0).
3078 */
3079 if (msg_col == 0)
3080 msg_didout = FALSE;
3081 msg_start();
3082 MSG_PUTS(":!");
3083 msg_outtrans(cmd); /* show what we are doing */
3084
3085 /* let the shell know if we are redirecting output or not */
3086 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3087
3088#ifdef AMIGA
3089 out_flush();
3090 /* read window status report and redraw before message */
3091 (void)char_avail();
3092#endif
3093
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003094 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003095 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3096 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003097 && eap->cmdidx != CMD_lgrepadd),
3098 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003099 if (wp != NULL)
3100 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003101#ifdef FEAT_AUTOCMD
3102 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003103 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003104 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3105 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003106 if (qi->qf_curlist < qi->qf_listcount)
3107 res = qi->qf_lists[qi->qf_curlist].qf_count;
3108 else
3109 res = 0;
3110 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003111#endif
3112 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003113 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaar7c626922005-02-07 22:01:03 +00003115 mch_remove(fname);
3116 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 vim_free(cmd);
3118}
3119
3120/*
3121 * Return the name for the errorfile, in allocated memory.
3122 * Find a new unique name when 'makeef' contains "##".
3123 * Returns NULL for error.
3124 */
3125 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003126get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 char_u *p;
3129 char_u *name;
3130 static int start = -1;
3131 static int off = 0;
3132#ifdef HAVE_LSTAT
3133 struct stat sb;
3134#endif
3135
3136 if (*p_mef == NUL)
3137 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003138 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (name == NULL)
3140 EMSG(_(e_notmp));
3141 return name;
3142 }
3143
3144 for (p = p_mef; *p; ++p)
3145 if (p[0] == '#' && p[1] == '#')
3146 break;
3147
3148 if (*p == NUL)
3149 return vim_strsave(p_mef);
3150
3151 /* Keep trying until the name doesn't exist yet. */
3152 for (;;)
3153 {
3154 if (start == -1)
3155 start = mch_get_pid();
3156 else
3157 off += 19;
3158
3159 name = alloc((unsigned)STRLEN(p_mef) + 30);
3160 if (name == NULL)
3161 break;
3162 STRCPY(name, p_mef);
3163 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3164 STRCAT(name, p + 2);
3165 if (mch_getperm(name) < 0
3166#ifdef HAVE_LSTAT
3167 /* Don't accept a symbolic link, its a security risk. */
3168 && mch_lstat((char *)name, &sb) < 0
3169#endif
3170 )
3171 break;
3172 vim_free(name);
3173 }
3174 return name;
3175}
3176
3177/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003178 * Returns the number of valid entries in the current quickfix/location list.
3179 */
3180 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003181qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003182{
3183 qf_info_T *qi = &ql_info;
3184 qfline_T *qfp;
3185 int i, sz = 0;
3186 int prev_fnum = 0;
3187
3188 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3189 {
3190 /* Location list */
3191 qi = GET_LOC_LIST(curwin);
3192 if (qi == NULL)
3193 return 0;
3194 }
3195
3196 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3197 (i < qi->qf_lists[qi->qf_curlist].qf_count) && (qfp != NULL);
3198 ++i, qfp = qfp->qf_next)
3199 {
3200 if (qfp->qf_valid)
3201 {
3202 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3203 sz++; /* Count all valid entries */
3204 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3205 {
3206 /* Count the number of files */
3207 sz++;
3208 prev_fnum = qfp->qf_fnum;
3209 }
3210 }
3211 }
3212
3213 return sz;
3214}
3215
3216/*
3217 * Returns the current index of the quickfix/location list.
3218 * Returns 0 if there is an error.
3219 */
3220 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003221qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003222{
3223 qf_info_T *qi = &ql_info;
3224
3225 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3226 {
3227 /* Location list */
3228 qi = GET_LOC_LIST(curwin);
3229 if (qi == NULL)
3230 return 0;
3231 }
3232
3233 return qi->qf_lists[qi->qf_curlist].qf_index;
3234}
3235
3236/*
3237 * Returns the current index in the quickfix/location list (counting only valid
3238 * entries). If no valid entries are in the list, then returns 1.
3239 */
3240 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003241qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003242{
3243 qf_info_T *qi = &ql_info;
3244 qf_list_T *qfl;
3245 qfline_T *qfp;
3246 int i, eidx = 0;
3247 int prev_fnum = 0;
3248
3249 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3250 {
3251 /* Location list */
3252 qi = GET_LOC_LIST(curwin);
3253 if (qi == NULL)
3254 return 1;
3255 }
3256
3257 qfl = &qi->qf_lists[qi->qf_curlist];
3258 qfp = qfl->qf_start;
3259
3260 /* check if the list has valid errors */
3261 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3262 return 1;
3263
3264 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3265 {
3266 if (qfp->qf_valid)
3267 {
3268 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3269 {
3270 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3271 {
3272 /* Count the number of files */
3273 eidx++;
3274 prev_fnum = qfp->qf_fnum;
3275 }
3276 }
3277 else
3278 eidx++;
3279 }
3280 }
3281
3282 return eidx ? eidx : 1;
3283}
3284
3285/*
3286 * Get the 'n'th valid error entry in the quickfix or location list.
3287 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3288 * For :cdo and :ldo returns the 'n'th valid error entry.
3289 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3290 */
3291 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003292qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003293{
3294 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3295 qfline_T *qfp = qfl->qf_start;
3296 int i, eidx;
3297 int prev_fnum = 0;
3298
3299 /* check if the list has valid errors */
3300 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3301 return 1;
3302
3303 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp!= NULL;
3304 i++, qfp = qfp->qf_next)
3305 {
3306 if (qfp->qf_valid)
3307 {
3308 if (fdo)
3309 {
3310 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3311 {
3312 /* Count the number of files */
3313 eidx++;
3314 prev_fnum = qfp->qf_fnum;
3315 }
3316 }
3317 else
3318 eidx++;
3319 }
3320
3321 if (eidx == n)
3322 break;
3323 }
3324
3325 if (i <= qfl->qf_count)
3326 return i;
3327 else
3328 return 1;
3329}
3330
3331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003333 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003334 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 */
3336 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003337ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003339 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003340 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003341
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003342 if (eap->cmdidx == CMD_ll
3343 || eap->cmdidx == CMD_lrewind
3344 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003345 || eap->cmdidx == CMD_llast
3346 || eap->cmdidx == CMD_ldo
3347 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003348 {
3349 qi = GET_LOC_LIST(curwin);
3350 if (qi == NULL)
3351 {
3352 EMSG(_(e_loclist));
3353 return;
3354 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003355 }
3356
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003357 if (eap->addr_count > 0)
3358 errornr = (int)eap->line2;
3359 else
3360 {
3361 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3362 errornr = 0;
3363 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3364 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3365 errornr = 1;
3366 else
3367 errornr = 32767;
3368 }
3369
3370 /* For cdo and ldo commands, jump to the nth valid error.
3371 * For cfdo and lfdo commands, jump to the nth valid file entry.
3372 */
3373 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3374 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3375 errornr = qf_get_nth_valid_entry(qi,
3376 eap->addr_count > 0 ? (int)eap->line1 : 1,
3377 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3378
3379 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380}
3381
3382/*
3383 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003384 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003385 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 */
3387 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003388ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003390 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003391 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003392
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003393 if (eap->cmdidx == CMD_lnext
3394 || eap->cmdidx == CMD_lNext
3395 || eap->cmdidx == CMD_lprevious
3396 || eap->cmdidx == CMD_lnfile
3397 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003398 || eap->cmdidx == CMD_lpfile
3399 || eap->cmdidx == CMD_ldo
3400 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003401 {
3402 qi = GET_LOC_LIST(curwin);
3403 if (qi == NULL)
3404 {
3405 EMSG(_(e_loclist));
3406 return;
3407 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003408 }
3409
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003410 if (eap->addr_count > 0 &&
3411 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3412 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3413 errornr = (int)eap->line2;
3414 else
3415 errornr = 1;
3416
3417 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3418 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003420 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3421 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003423 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3424 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 ? BACKWARD_FILE
3426 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003427 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428}
3429
3430/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003431 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003432 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 */
3434 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003435ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003437 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003438 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003439#ifdef FEAT_AUTOCMD
3440 char_u *au_name = NULL;
3441#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003442
3443 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003444 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003446
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003447#ifdef FEAT_AUTOCMD
3448 switch (eap->cmdidx)
3449 {
3450 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3451 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3452 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3453 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3454 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3455 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3456 default: break;
3457 }
3458 if (au_name != NULL)
3459 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3460#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003461#ifdef FEAT_BROWSE
3462 if (cmdmod.browse)
3463 {
3464 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3465 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3466 if (browse_file == NULL)
3467 return;
3468 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3469 vim_free(browse_file);
3470 }
3471 else
3472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003474 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003475
3476 /*
3477 * This function is used by the :cfile, :cgetfile and :caddfile
3478 * commands.
3479 * :cfile always creates a new quickfix list and jumps to the
3480 * first error.
3481 * :cgetfile creates a new quickfix list but doesn't jump to the
3482 * first error.
3483 * :caddfile adds to an existing quickfix list. If there is no
3484 * quickfix list then a new list is created.
3485 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003486 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003487 && eap->cmdidx != CMD_laddfile),
3488 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003489 && (eap->cmdidx == CMD_cfile
3490 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003491 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003492#ifdef FEAT_AUTOCMD
3493 if (au_name != NULL)
3494 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3495#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003496 if (wp != NULL)
3497 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003498 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003499 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003500
3501 else
3502 {
3503#ifdef FEAT_AUTOCMD
3504 if (au_name != NULL)
3505 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3506#endif
3507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508}
3509
3510/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003511 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003512 * ":vimgrepadd {pattern} file(s)"
3513 * ":lvimgrep {pattern} file(s)"
3514 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003515 */
3516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003517ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003518{
Bram Moolenaar81695252004-12-29 20:58:21 +00003519 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003520 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003521 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003522 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003523 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003524 char_u *s;
3525 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003526 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003527 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003528#ifdef FEAT_AUTOCMD
3529 qfline_T *cur_qf_start;
3530#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003531 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003532 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003533 buf_T *buf;
3534 int duplicate_name = FALSE;
3535 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003536 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003537 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003538 buf_T *first_match_buf = NULL;
3539 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003540 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003541#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3542 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003543#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003544 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003545 int flags = 0;
3546 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003547 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003548 char_u *dirname_start = NULL;
3549 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003550 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003551#ifdef FEAT_AUTOCMD
3552 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003553
3554 switch (eap->cmdidx)
3555 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003556 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3557 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3558 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003559 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003560 case CMD_grep: au_name = (char_u *)"grep"; break;
3561 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3562 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3563 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003564 default: break;
3565 }
3566 if (au_name != NULL)
3567 {
3568 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3569 curbuf->b_fname, TRUE, curbuf);
3570 if (did_throw || force_abort)
3571 return;
3572 }
3573#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003574
Bram Moolenaar754b5602006-02-09 23:53:20 +00003575 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003576 || eap->cmdidx == CMD_lvimgrep
3577 || eap->cmdidx == CMD_lgrepadd
3578 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003579 {
3580 qi = ll_get_or_alloc_list(curwin);
3581 if (qi == NULL)
3582 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003583 }
3584
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003585 if (eap->addr_count > 0)
3586 tomatch = eap->line2;
3587 else
3588 tomatch = MAXLNUM;
3589
Bram Moolenaar81695252004-12-29 20:58:21 +00003590 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003591 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003592 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003593 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003594 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003595 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003596 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003597 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003598 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003599
3600 if (s != NULL && *s == NUL)
3601 {
3602 /* Pattern is empty, use last search pattern. */
3603 if (last_search_pat() == NULL)
3604 {
3605 EMSG(_(e_noprevre));
3606 goto theend;
3607 }
3608 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3609 }
3610 else
3611 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3612
Bram Moolenaar86b68352004-12-27 21:59:20 +00003613 if (regmatch.regprog == NULL)
3614 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003615 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003616 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003617
3618 p = skipwhite(p);
3619 if (*p == NUL)
3620 {
3621 EMSG(_("E683: File name missing or invalid pattern"));
3622 goto theend;
3623 }
3624
Bram Moolenaar754b5602006-02-09 23:53:20 +00003625 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003626 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003627 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003628 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003629 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003630 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003631 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003632 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003633 prevp->qf_next != prevp; prevp = prevp->qf_next)
3634 ;
3635
3636 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003637 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003638 goto theend;
3639 if (fcount == 0)
3640 {
3641 EMSG(_(e_nomatch));
3642 goto theend;
3643 }
3644
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003645 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3646 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003647 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003648 {
3649 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003650 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003651 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003652
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003653 /* Remember the current directory, because a BufRead autocommand that does
3654 * ":lcd %:p:h" changes the meaning of short path names. */
3655 mch_dirname(dirname_start, MAXPATHL);
3656
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003657#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003658 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003659 * changing the current quickfix list. */
3660 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3661#endif
3662
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003663 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003664 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003665 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003666 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003667 if (time(NULL) > seconds)
3668 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003669 /* Display the file name every second or so, show the user we are
3670 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003671 seconds = time(NULL);
3672 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003673 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003674 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003675 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003676 else
3677 {
3678 msg_outtrans(p);
3679 vim_free(p);
3680 }
3681 msg_clr_eos();
3682 msg_didout = FALSE; /* overwrite this message */
3683 msg_nowait = TRUE; /* don't wait for this message */
3684 msg_col = 0;
3685 out_flush();
3686 }
3687
Bram Moolenaar81695252004-12-29 20:58:21 +00003688 buf = buflist_findname_exp(fnames[fi]);
3689 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3690 {
3691 /* Remember that a buffer with this name already exists. */
3692 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003693 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003694 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003695
3696#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3697 /* Don't do Filetype autocommands to avoid loading syntax and
3698 * indent scripts, a great speed improvement. */
3699 save_ei = au_event_disable(",Filetype");
3700#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003701 /* Don't use modelines here, it's useless. */
3702 save_mls = p_mls;
3703 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003704
3705 /* Load file into a buffer, so that 'fileencoding' is detected,
3706 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003707 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003708
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003709 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003710#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3711 au_event_restore(save_ei);
3712#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003713 }
3714 else
3715 /* Use existing, loaded buffer. */
3716 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003717
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003718#ifdef FEAT_AUTOCMD
3719 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3720 {
3721 int idx;
3722
3723 /* Autocommands changed the quickfix list. Find the one we were
3724 * using and restore it. */
3725 for (idx = 0; idx < LISTCOUNT; ++idx)
3726 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3727 {
3728 qi->qf_curlist = idx;
3729 break;
3730 }
3731 if (idx == LISTCOUNT)
3732 {
3733 /* List cannot be found, create a new one. */
3734 qf_new_list(qi, *eap->cmdlinep);
3735 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3736 }
3737 }
3738#endif
3739
Bram Moolenaar81695252004-12-29 20:58:21 +00003740 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003741 {
3742 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003743 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003744 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003745 else
3746 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003747 /* Try for a match in all lines of the buffer.
3748 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003749 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003750 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3751 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003752 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003753 col = 0;
3754 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003755 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003756 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003757 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003758 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003759 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003760 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003761 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003762 ml_get_buf(buf,
3763 regmatch.startpos[0].lnum + lnum, FALSE),
3764 regmatch.startpos[0].lnum + lnum,
3765 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003766 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003767 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003768 0, /* nr */
3769 0, /* type */
3770 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003771 ) == FAIL)
3772 {
3773 got_int = TRUE;
3774 break;
3775 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003776 found_match = TRUE;
3777 if (--tomatch == 0)
3778 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003779 if ((flags & VGR_GLOBAL) == 0
3780 || regmatch.endpos[0].lnum > 0)
3781 break;
3782 col = regmatch.endpos[0].col
3783 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003784 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003785 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003786 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003787 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003788 if (got_int)
3789 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003790 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003791#ifdef FEAT_AUTOCMD
3792 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3793#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003794
3795 if (using_dummy)
3796 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003797 if (found_match && first_match_buf == NULL)
3798 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003799 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003800 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003801 /* Never keep a dummy buffer if there is another buffer
3802 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003803 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003804 buf = NULL;
3805 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003806 else if (!cmdmod.hide
3807 || buf->b_p_bh[0] == 'u' /* "unload" */
3808 || buf->b_p_bh[0] == 'w' /* "wipe" */
3809 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003810 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003811 /* When no match was found we don't need to remember the
3812 * buffer, wipe it out. If there was a match and it
3813 * wasn't the first one or we won't jump there: only
3814 * unload the buffer.
3815 * Ignore 'hidden' here, because it may lead to having too
3816 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003817 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003818 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003819 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003820 buf = NULL;
3821 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003822 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003823 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003824 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003825 buf = NULL;
3826 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003827 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003828
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003829 if (buf != NULL)
3830 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003831 /* If the buffer is still loaded we need to use the
3832 * directory we jumped to below. */
3833 if (buf == first_match_buf
3834 && target_dir == NULL
3835 && STRCMP(dirname_start, dirname_now) != 0)
3836 target_dir = vim_strsave(dirname_now);
3837
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003838 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003839 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003840 * need to be done (again). But not the window-local
3841 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003842 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003843#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003844 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3845 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003846#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003847 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003848 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003849 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003850 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003851 }
3852 }
3853
3854 FreeWild(fcount, fnames);
3855
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3857 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3858 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003859
3860#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003861 qf_update_buffer(qi, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003862#endif
3863
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003864#ifdef FEAT_AUTOCMD
3865 if (au_name != NULL)
3866 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3867 curbuf->b_fname, TRUE, curbuf);
3868#endif
3869
Bram Moolenaar86b68352004-12-27 21:59:20 +00003870 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003871 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003872 {
3873 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003874 {
3875 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003876 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003877 if (buf != curbuf)
3878 /* If we jumped to another buffer redrawing will already be
3879 * taken care of. */
3880 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003881
3882 /* Jump to the directory used after loading the buffer. */
3883 if (curbuf == first_match_buf && target_dir != NULL)
3884 {
3885 exarg_T ea;
3886
3887 ea.arg = target_dir;
3888 ea.cmdidx = CMD_lcd;
3889 ex_cd(&ea);
3890 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003891 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003892 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003893 else
3894 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003895
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003896 /* If we loaded a dummy buffer into the current window, the autocommands
3897 * may have messed up things, need to redraw and recompute folds. */
3898 if (redraw_for_dummy)
3899 {
3900#ifdef FEAT_FOLDING
3901 foldUpdateAll(curwin);
3902#else
3903 redraw_later(NOT_VALID);
3904#endif
3905 }
3906
Bram Moolenaar86b68352004-12-27 21:59:20 +00003907theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003908 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003909 vim_free(dirname_now);
3910 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003911 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003912 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003913}
3914
3915/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003916 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003917 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003918 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3919 * If "s" is not NULL terminate the pattern with a NUL.
3920 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003921 */
3922 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003923skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003924{
3925 int c;
3926
3927 if (vim_isIDc(*p))
3928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003929 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003930 if (s != NULL)
3931 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003932 p = skiptowhite(p);
3933 if (s != NULL && *p != NUL)
3934 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003935 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003936 else
3937 {
3938 /* ":vimgrep /pattern/[g][j] fname" */
3939 if (s != NULL)
3940 *s = p + 1;
3941 c = *p;
3942 p = skip_regexp(p + 1, c, TRUE, NULL);
3943 if (*p != c)
3944 return NULL;
3945
3946 /* Truncate the pattern. */
3947 if (s != NULL)
3948 *p = NUL;
3949 ++p;
3950
3951 /* Find the flags */
3952 while (*p == 'g' || *p == 'j')
3953 {
3954 if (flags != NULL)
3955 {
3956 if (*p == 'g')
3957 *flags |= VGR_GLOBAL;
3958 else
3959 *flags |= VGR_NOJUMP;
3960 }
3961 ++p;
3962 }
3963 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003964 return p;
3965}
3966
3967/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003968 * Restore current working directory to "dirname_start" if they differ, taking
3969 * into account whether it is set locally or globally.
3970 */
3971 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003972restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003973{
3974 char_u *dirname_now = alloc(MAXPATHL);
3975
3976 if (NULL != dirname_now)
3977 {
3978 mch_dirname(dirname_now, MAXPATHL);
3979 if (STRCMP(dirname_start, dirname_now) != 0)
3980 {
3981 /* If the directory has changed, change it back by building up an
3982 * appropriate ex command and executing it. */
3983 exarg_T ea;
3984
3985 ea.arg = dirname_start;
3986 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3987 ex_cd(&ea);
3988 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003989 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003990 }
3991}
3992
3993/*
3994 * Load file "fname" into a dummy buffer and return the buffer pointer,
3995 * placing the directory resulting from the buffer load into the
3996 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3997 * prior to calling this function. Restores directory to "dirname_start" prior
3998 * to returning, if autocmds or the 'autochdir' option have changed it.
3999 *
4000 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4001 * or wipe_dummy_buffer() later!
4002 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004003 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004004 */
4005 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004006load_dummy_buffer(
4007 char_u *fname,
4008 char_u *dirname_start, /* in: old directory */
4009 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004010{
4011 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004012 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004013 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004014 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004015
4016 /* Allocate a buffer without putting it in the buffer list. */
4017 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4018 if (newbuf == NULL)
4019 return NULL;
4020
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004021 /* Init the options. */
4022 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4023
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004024 /* need to open the memfile before putting the buffer in a window */
4025 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004026 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004027 /* set curwin/curbuf to buf and save a few things */
4028 aucmd_prepbuf(&aco, newbuf);
4029
4030 /* Need to set the filename for autocommands. */
4031 (void)setfname(curbuf, fname, NULL, FALSE);
4032
Bram Moolenaar81695252004-12-29 20:58:21 +00004033 /* Create swap file now to avoid the ATTENTION message. */
4034 check_need_swap(TRUE);
4035
4036 /* Remove the "dummy" flag, otherwise autocommands may not
4037 * work. */
4038 curbuf->b_flags &= ~BF_DUMMY;
4039
4040 if (readfile(fname, NULL,
4041 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4042 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004043 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004044 && !(curbuf->b_flags & BF_NEW))
4045 {
4046 failed = FALSE;
4047 if (curbuf != newbuf)
4048 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004049 /* Bloody autocommands changed the buffer! Can happen when
4050 * using netrw and editing a remote file. Use the current
4051 * buffer instead, delete the dummy one after restoring the
4052 * window stuff. */
4053 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004054 newbuf = curbuf;
4055 }
4056 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004057
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004058 /* restore curwin/curbuf and a few other things */
4059 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004060 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4061 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004062 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004063
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004064 /*
4065 * When autocommands/'autochdir' option changed directory: go back.
4066 * Let the caller know what the resulting dir was first, in case it is
4067 * important.
4068 */
4069 mch_dirname(resulting_dir, MAXPATHL);
4070 restore_start_dir(dirname_start);
4071
Bram Moolenaar81695252004-12-29 20:58:21 +00004072 if (!buf_valid(newbuf))
4073 return NULL;
4074 if (failed)
4075 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004076 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004077 return NULL;
4078 }
4079 return newbuf;
4080}
4081
4082/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004083 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4084 * directory to "dirname_start" prior to returning, if autocmds or the
4085 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004086 */
4087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004088wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004089{
4090 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004091 {
4092#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4093 cleanup_T cs;
4094
4095 /* Reset the error/interrupt/exception state here so that aborting()
4096 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4097 * work when got_int is set. */
4098 enter_cleanup(&cs);
4099#endif
4100
Bram Moolenaar81695252004-12-29 20:58:21 +00004101 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004102
4103#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4104 /* Restore the error/interrupt/exception state if not discarded by a
4105 * new aborting error, interrupt, or uncaught exception. */
4106 leave_cleanup(&cs);
4107#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004108 /* When autocommands/'autochdir' option changed directory: go back. */
4109 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004110 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004111}
4112
4113/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004114 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4115 * directory to "dirname_start" prior to returning, if autocmds or the
4116 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004117 */
4118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004119unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004120{
4121 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004122 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004123 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004124
4125 /* When autocommands/'autochdir' option changed directory: go back. */
4126 restore_start_dir(dirname_start);
4127 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004128}
4129
Bram Moolenaar05159a02005-02-26 23:04:13 +00004130#if defined(FEAT_EVAL) || defined(PROTO)
4131/*
4132 * Add each quickfix error to list "list" as a dictionary.
4133 */
4134 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004135get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004136{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004137 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004138 dict_T *dict;
4139 char_u buf[2];
4140 qfline_T *qfp;
4141 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004142 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004143
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004144 if (wp != NULL)
4145 {
4146 qi = GET_LOC_LIST(wp);
4147 if (qi == NULL)
4148 return FAIL;
4149 }
4150
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004151 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004152 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004153 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004154
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004155 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4156 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004157 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004158 /* Handle entries with a non-existing buffer number. */
4159 bufnum = qfp->qf_fnum;
4160 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4161 bufnum = 0;
4162
Bram Moolenaar05159a02005-02-26 23:04:13 +00004163 if ((dict = dict_alloc()) == NULL)
4164 return FAIL;
4165 if (list_append_dict(list, dict) == FAIL)
4166 return FAIL;
4167
4168 buf[0] = qfp->qf_type;
4169 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004170 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004171 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4172 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4173 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4174 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004175 || dict_add_nr_str(dict, "pattern", 0L,
4176 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4177 || dict_add_nr_str(dict, "text", 0L,
4178 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004179 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4180 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4181 return FAIL;
4182
4183 qfp = qfp->qf_next;
4184 }
4185 return OK;
4186}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004187
4188/*
4189 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02004190 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004191 */
4192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004193set_errorlist(
4194 win_T *wp,
4195 list_T *list,
4196 int action,
4197 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004198{
4199 listitem_T *li;
4200 dict_T *d;
4201 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004202 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004203 long lnum;
4204 int col, nr;
4205 int vcol;
4206 qfline_T *prevp = NULL;
4207 int valid, status;
4208 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004209 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004210 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004211
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004212 if (wp != NULL)
4213 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004214 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004215 if (qi == NULL)
4216 return FAIL;
4217 }
4218
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004219 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004220 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004221 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004222 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004223 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004224 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004225 prevp->qf_next != prevp; prevp = prevp->qf_next)
4226 ;
4227 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004228 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004229 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004230 qf_store_title(qi, title);
4231 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004232
4233 for (li = list->lv_first; li != NULL; li = li->li_next)
4234 {
4235 if (li->li_tv.v_type != VAR_DICT)
4236 continue; /* Skip non-dict items */
4237
4238 d = li->li_tv.vval.v_dict;
4239 if (d == NULL)
4240 continue;
4241
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004242 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004243 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004244 lnum = get_dict_number(d, (char_u *)"lnum");
4245 col = get_dict_number(d, (char_u *)"col");
4246 vcol = get_dict_number(d, (char_u *)"vcol");
4247 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004248 type = get_dict_string(d, (char_u *)"type", TRUE);
4249 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4250 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004251 if (text == NULL)
4252 text = vim_strsave((char_u *)"");
4253
4254 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004255 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004256 valid = FALSE;
4257
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004258 /* Mark entries with non-existing buffer number as not valid. Give the
4259 * error message only once. */
4260 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4261 {
4262 if (!did_bufnr_emsg)
4263 {
4264 did_bufnr_emsg = TRUE;
4265 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4266 }
4267 valid = FALSE;
4268 bufnum = 0;
4269 }
4270
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004271 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004272 NULL, /* dir */
4273 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004274 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004275 text,
4276 lnum,
4277 col,
4278 vcol, /* vis_col */
4279 pattern, /* search pattern */
4280 nr,
4281 type == NULL ? NUL : *type,
4282 valid);
4283
4284 vim_free(filename);
4285 vim_free(pattern);
4286 vim_free(text);
4287 vim_free(type);
4288
4289 if (status == FAIL)
4290 {
4291 retval = FAIL;
4292 break;
4293 }
4294 }
4295
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004296 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004297 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004298 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4299 else
4300 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004301 if (action != 'a') {
4302 qi->qf_lists[qi->qf_curlist].qf_ptr =
4303 qi->qf_lists[qi->qf_curlist].qf_start;
4304 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4305 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4306 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004307
4308#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004309 /* Don't update the cursor in quickfix window when appending entries */
4310 qf_update_buffer(qi, (action != 'a'));
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004311#endif
4312
4313 return retval;
4314}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004315#endif
4316
Bram Moolenaar81695252004-12-29 20:58:21 +00004317/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004318 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004319 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004320 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004321 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004322 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004323 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004324 */
4325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004326ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004327{
4328 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004329 qf_info_T *qi = &ql_info;
4330
Bram Moolenaardb552d602006-03-23 22:59:57 +00004331 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4332 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004333 {
4334 qi = ll_get_or_alloc_list(curwin);
4335 if (qi == NULL)
4336 return;
4337 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004338
4339 if (*eap->arg == NUL)
4340 buf = curbuf;
4341 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4342 buf = buflist_findnr(atoi((char *)eap->arg));
4343 if (buf == NULL)
4344 EMSG(_(e_invarg));
4345 else if (buf->b_ml.ml_mfp == NULL)
4346 EMSG(_("E681: Buffer is not loaded"));
4347 else
4348 {
4349 if (eap->addr_count == 0)
4350 {
4351 eap->line1 = 1;
4352 eap->line2 = buf->b_ml.ml_line_count;
4353 }
4354 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4355 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4356 EMSG(_(e_invrange));
4357 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004358 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004359 char_u *qf_title = *eap->cmdlinep;
4360
4361 if (buf->b_sfname)
4362 {
4363 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4364 (char *)qf_title, (char *)buf->b_sfname);
4365 qf_title = IObuff;
4366 }
4367
Bram Moolenaardb552d602006-03-23 22:59:57 +00004368 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4369 (eap->cmdidx != CMD_caddbuffer
4370 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004371 eap->line1, eap->line2,
4372 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004373 && (eap->cmdidx == CMD_cbuffer
4374 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004375 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4376 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004377 }
4378}
4379
Bram Moolenaar1e015462005-09-25 22:16:38 +00004380#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004381/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004382 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4383 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004384 */
4385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004386ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004387{
4388 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004389 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004390
Bram Moolenaardb552d602006-03-23 22:59:57 +00004391 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4392 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004393 {
4394 qi = ll_get_or_alloc_list(curwin);
4395 if (qi == NULL)
4396 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004397 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004398
Bram Moolenaar4770d092006-01-12 23:22:24 +00004399 /* Evaluate the expression. When the result is a string or a list we can
4400 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004401 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004402 if (tv != NULL)
4403 {
4404 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4405 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4406 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004407 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004408 (eap->cmdidx != CMD_caddexpr
4409 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004410 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004411 && (eap->cmdidx == CMD_cexpr
4412 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004413 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004414 }
4415 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004416 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004417 free_tv(tv);
4418 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004419}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004420#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004421
4422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 * ":helpgrep {pattern}"
4424 */
4425 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004426ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427{
4428 regmatch_T regmatch;
4429 char_u *save_cpo;
4430 char_u *p;
4431 int fcount;
4432 char_u **fnames;
4433 FILE *fd;
4434 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004435 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004437#ifdef FEAT_MULTI_LANG
4438 char_u *lang;
4439#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004440 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004441 int new_qi = FALSE;
4442 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004443#ifdef FEAT_AUTOCMD
4444 char_u *au_name = NULL;
4445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004447#ifdef FEAT_MULTI_LANG
4448 /* Check for a specified language */
4449 lang = check_help_lang(eap->arg);
4450#endif
4451
Bram Moolenaar73633f82012-01-20 13:39:07 +01004452#ifdef FEAT_AUTOCMD
4453 switch (eap->cmdidx)
4454 {
4455 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4456 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4457 default: break;
4458 }
4459 if (au_name != NULL)
4460 {
4461 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4462 curbuf->b_fname, TRUE, curbuf);
4463 if (did_throw || force_abort)
4464 return;
4465 }
4466#endif
4467
4468 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4469 save_cpo = p_cpo;
4470 p_cpo = empty_option;
4471
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004472 if (eap->cmdidx == CMD_lhelpgrep)
4473 {
4474 /* Find an existing help window */
4475 FOR_ALL_WINDOWS(wp)
4476 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4477 break;
4478
4479 if (wp == NULL) /* Help window not found */
4480 qi = NULL;
4481 else
4482 qi = wp->w_llist;
4483
4484 if (qi == NULL)
4485 {
4486 /* Allocate a new location list for help text matches */
4487 if ((qi = ll_new_list()) == NULL)
4488 return;
4489 new_qi = TRUE;
4490 }
4491 }
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4494 regmatch.rm_ic = FALSE;
4495 if (regmatch.regprog != NULL)
4496 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004497#ifdef FEAT_MBYTE
4498 vimconv_T vc;
4499
4500 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4501 * differs. */
4502 vc.vc_type = CONV_NONE;
4503 if (!enc_utf8)
4504 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4505#endif
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004508 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 /* Go through all directories in 'runtimepath' */
4511 p = p_rtp;
4512 while (*p != NUL && !got_int)
4513 {
4514 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4515
4516 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4517 add_pathsep(NameBuff);
4518 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4519 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4520 &fnames, EW_FILE|EW_SILENT) == OK
4521 && fcount > 0)
4522 {
4523 for (fi = 0; fi < fcount && !got_int; ++fi)
4524 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004525#ifdef FEAT_MULTI_LANG
4526 /* Skip files for a different language. */
4527 if (lang != NULL
4528 && STRNICMP(lang, fnames[fi]
4529 + STRLEN(fnames[fi]) - 3, 2) != 0
4530 && !(STRNICMP(lang, "en", 2) == 0
4531 && STRNICMP("txt", fnames[fi]
4532 + STRLEN(fnames[fi]) - 3, 3) == 0))
4533 continue;
4534#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004535 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 if (fd != NULL)
4537 {
4538 lnum = 1;
4539 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4540 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004541 char_u *line = IObuff;
4542#ifdef FEAT_MBYTE
4543 /* Convert a line if 'encoding' is not utf-8 and
4544 * the line contains a non-ASCII character. */
4545 if (vc.vc_type != CONV_NONE
4546 && has_non_ascii(IObuff)) {
4547 line = string_convert(&vc, IObuff, NULL);
4548 if (line == NULL)
4549 line = IObuff;
4550 }
4551#endif
4552
4553 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004555 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004558 while (l > 0 && line[l - 1] <= ' ')
4559 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004561 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 NULL, /* dir */
4563 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004564 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004565 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004567 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004568 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004569 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004570 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 0, /* nr */
4572 1, /* type */
4573 TRUE /* valid */
4574 ) == FAIL)
4575 {
4576 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004577#ifdef FEAT_MBYTE
4578 if (line != IObuff)
4579 vim_free(line);
4580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 break;
4582 }
4583 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004584#ifdef FEAT_MBYTE
4585 if (line != IObuff)
4586 vim_free(line);
4587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 ++lnum;
4589 line_breakcheck();
4590 }
4591 fclose(fd);
4592 }
4593 }
4594 FreeWild(fcount, fnames);
4595 }
4596 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004597
Bram Moolenaar473de612013-06-08 18:19:48 +02004598 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004599#ifdef FEAT_MBYTE
4600 if (vc.vc_type != CONV_NONE)
4601 convert_setup(&vc, NULL, NULL);
4602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004604 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4605 qi->qf_lists[qi->qf_curlist].qf_ptr =
4606 qi->qf_lists[qi->qf_curlist].qf_start;
4607 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004610 if (p_cpo == empty_option)
4611 p_cpo = save_cpo;
4612 else
4613 /* Darn, some plugin changed the value. */
4614 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004617 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618#endif
4619
Bram Moolenaar73633f82012-01-20 13:39:07 +01004620#ifdef FEAT_AUTOCMD
4621 if (au_name != NULL)
4622 {
4623 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4624 curbuf->b_fname, TRUE, curbuf);
4625 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4626 /* autocommands made "qi" invalid */
4627 return;
4628 }
4629#endif
4630
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004632 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004633 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004634 else
4635 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004636
4637 if (eap->cmdidx == CMD_lhelpgrep)
4638 {
4639 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004640 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004641 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4642 {
4643 if (new_qi)
4644 ll_free_all(&qi);
4645 }
4646 else if (curwin->w_llist == NULL)
4647 curwin->w_llist = qi;
4648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649}
4650
4651#endif /* FEAT_QUICKFIX */