blob: 85da690fb3c2d2eb3313bc7e56d0c6995eed1e53 [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 Moolenaar7fd73202010-07-25 16:58:46 +0200109static int qf_init_ext __ARGS((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));
Bram Moolenaar41b884b2012-11-14 22:38:08 +0100110static void qf_new_list __ARGS((qf_info_T *qi, char_u *qf_title, win_T *wp));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000111static void ll_free_all __ARGS((qf_info_T **pqi));
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000112static int qf_add_entry __ARGS((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));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000113static qf_info_T *ll_new_list __ARGS((void));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000114static void qf_msg __ARGS((qf_info_T *qi));
115static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static char_u *qf_types __ARGS((int, int));
117static int qf_get_fnum __ARGS((char_u *, char_u *));
118static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
119static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
120static char_u *qf_guess_filepath __ARGS((char_u *));
121static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
122static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
123#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000124static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000125static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000126static win_T *qf_find_win __ARGS((qf_info_T *qi));
127static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
128static void qf_update_buffer __ARGS((qf_info_T *qi));
Bram Moolenaarc95e3262011-08-10 18:36:54 +0200129static void qf_set_title __ARGS((qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132static char_u *get_mef_name __ARGS((void));
Bram Moolenaar7f51a822012-04-25 18:57:21 +0200133static void restore_start_dir __ARGS((char_u *dirname_start));
134static buf_T *load_dummy_buffer __ARGS((char_u *fname, char_u *dirname_start, char_u *resulting_dir));
135static void wipe_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
136static void unload_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000137static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000139/* Quickfix window check helper macro */
140#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
141/* Location list window check helper macro */
142#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
143/*
144 * Return location list for window 'wp'
145 * For location list window, return the referenced location list
146 */
147#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000150 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200151 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 * Return -1 for error, number of errors for success.
153 */
154 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200155qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000156 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 char_u *efile;
158 char_u *errorformat;
159 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200160 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162 qf_info_T *qi = &ql_info;
163
Bram Moolenaar86b68352004-12-27 21:59:20 +0000164 if (efile == NULL)
165 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166
167 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000168 {
169 qi = ll_get_or_alloc_list(wp);
170 if (qi == NULL)
171 return FAIL;
172 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000173
174 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200175 (linenr_T)0, (linenr_T)0,
176 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000177}
178
179/*
180 * Read the errorfile "efile" into memory, line by line, building the error
181 * list.
182 * Alternative: when "efile" is null read errors from buffer "buf".
183 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200184 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
185 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000186 * Return -1 for error, number of errors for success.
187 */
188 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200189qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
190 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000191 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000192 char_u *efile;
193 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000194 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000195 char_u *errorformat;
196 int newlist; /* TRUE: start a new error list */
197 linenr_T lnumfirst; /* first line number to use */
198 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200199 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000200{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 char_u *namebuf;
202 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000203 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 char_u *fmtstr = NULL;
205 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000206 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 int type = 0;
208 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000209 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 long lnum = 0L;
211 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000212 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000213 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000215 efm_T *fmt_first = NULL;
216 efm_T *fmt_last = NULL;
217 efm_T *fmt_ptr;
218 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 char_u *efm;
220 char_u *ptr;
221 char_u *srcptr;
222 int len;
223 int i;
224 int round;
225 int idx = 0;
226 int multiline = FALSE;
227 int multiignore = FALSE;
228 int multiscan = FALSE;
229 int retval = -1; /* default: return error flag */
230 char_u *directory = NULL;
231 char_u *currfile = NULL;
232 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000233 char_u *p_str = NULL;
234 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 struct dir_stack_T *file_stack = NULL;
236 regmatch_T regmatch;
237 static struct fmtpattern
238 {
239 char_u convchar;
240 char *pattern;
241 } fmt_pat[FMT_PATTERNS] =
242 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000243 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {'n', "\\d\\+"},
245 {'l', "\\d\\+"},
246 {'c', "\\d\\+"},
247 {'t', "."},
248 {'m', ".\\+"},
249 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200250 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000251 {'v', "\\d\\+"},
252 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 };
254
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 namebuf = alloc(CMDBUFFSIZE + 1);
256 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000257 pattern = alloc(CMDBUFFSIZE + 1);
258 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 goto qf_init_end;
260
Bram Moolenaar86b68352004-12-27 21:59:20 +0000261 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 EMSG2(_(e_openerrf), efile);
264 goto qf_init_end;
265 }
266
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000267 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 /* make place for a new list */
Bram Moolenaar41b884b2012-11-14 22:38:08 +0100269 qf_new_list(qi, qf_title, curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000270 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000272 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200273 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 ;
275
276/*
277 * Each part of the format string is copied and modified from errorformat to
278 * regex prog. Only a few % characters are allowed.
279 */
280 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000281 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000282 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 else
284 efm = errorformat;
285 /*
286 * Get some space to modify the format string into.
287 */
288 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
289 for (round = FMT_PATTERNS; round > 0; )
290 i += (int)STRLEN(fmt_pat[--round].pattern);
291#ifdef COLON_IN_FILENAME
292 i += 12; /* "%f" can become twelve chars longer */
293#else
294 i += 2; /* "%f" can become two chars longer */
295#endif
296 if ((fmtstr = alloc(i)) == NULL)
297 goto error2;
298
Bram Moolenaar01265852006-03-20 21:50:15 +0000299 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 {
301 /*
302 * Allocate a new eformat structure and put it at the end of the list
303 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000304 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 if (fmt_ptr == NULL)
306 goto error2;
307 if (fmt_first == NULL) /* first one */
308 fmt_first = fmt_ptr;
309 else
310 fmt_last->next = fmt_ptr;
311 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
313 /*
314 * Isolate one part in the 'errorformat' option
315 */
316 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
317 if (efm[len] == '\\' && efm[len + 1] != NUL)
318 ++len;
319
320 /*
321 * Build regexp pattern from current 'errorformat' option
322 */
323 ptr = fmtstr;
324 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000325 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 for (efmp = efm; efmp < efm + len; ++efmp)
327 {
328 if (*efmp == '%')
329 {
330 ++efmp;
331 for (idx = 0; idx < FMT_PATTERNS; ++idx)
332 if (fmt_pat[idx].convchar == *efmp)
333 break;
334 if (idx < FMT_PATTERNS)
335 {
336 if (fmt_ptr->addr[idx])
337 {
338 sprintf((char *)errmsg,
339 _("E372: Too many %%%c in format string"), *efmp);
340 EMSG(errmsg);
341 goto error2;
342 }
343 if ((idx
344 && idx < 6
345 && vim_strchr((char_u *)"DXOPQ",
346 fmt_ptr->prefix) != NULL)
347 || (idx == 6
348 && vim_strchr((char_u *)"OPQ",
349 fmt_ptr->prefix) == NULL))
350 {
351 sprintf((char *)errmsg,
352 _("E373: Unexpected %%%c in format string"), *efmp);
353 EMSG(errmsg);
354 goto error2;
355 }
356 fmt_ptr->addr[idx] = (char_u)++round;
357 *ptr++ = '\\';
358 *ptr++ = '(';
359#ifdef BACKSLASH_IN_FILENAME
360 if (*efmp == 'f')
361 {
362 /* Also match "c:" in the file name, even when
363 * checking for a colon next: "%f:".
364 * "\%(\a:\)\=" */
365 STRCPY(ptr, "\\%(\\a:\\)\\=");
366 ptr += 10;
367 }
368#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000369 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000371 if (efmp[1] != '\\' && efmp[1] != '%')
372 {
373 /* A file name may contain spaces, but this isn't
374 * in "\f". For "%f:%l:%m" there may be a ":" in
375 * the file name. Use ".\{-1,}x" instead (x is
376 * the next character), the requirement that :999:
377 * follows should work. */
378 STRCPY(ptr, ".\\{-1,}");
379 ptr += 7;
380 }
381 else
382 {
383 /* File name followed by '\\' or '%': include as
384 * many file name chars as possible. */
385 STRCPY(ptr, "\\f\\+");
386 ptr += 4;
387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
389 else
390 {
391 srcptr = (char_u *)fmt_pat[idx].pattern;
392 while ((*ptr = *srcptr++) != NUL)
393 ++ptr;
394 }
395 *ptr++ = '\\';
396 *ptr++ = ')';
397 }
398 else if (*efmp == '*')
399 {
400 if (*++efmp == '[' || *efmp == '\\')
401 {
402 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
403 {
404 if (efmp[1] == '^')
405 *ptr++ = *++efmp;
406 if (efmp < efm + len)
407 {
408 *ptr++ = *++efmp; /* could be ']' */
409 while (efmp < efm + len
410 && (*ptr++ = *++efmp) != ']')
411 /* skip */;
412 if (efmp == efm + len)
413 {
414 EMSG(_("E374: Missing ] in format string"));
415 goto error2;
416 }
417 }
418 }
419 else if (efmp < efm + len) /* %*\D, %*\s etc. */
420 *ptr++ = *++efmp;
421 *ptr++ = '\\';
422 *ptr++ = '+';
423 }
424 else
425 {
426 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
427 sprintf((char *)errmsg,
428 _("E375: Unsupported %%%c in format string"), *efmp);
429 EMSG(errmsg);
430 goto error2;
431 }
432 }
433 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
434 *ptr++ = *efmp; /* regexp magic characters */
435 else if (*efmp == '#')
436 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000437 else if (*efmp == '>')
438 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 else if (efmp == efm + 1) /* analyse prefix */
440 {
441 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
442 fmt_ptr->flags = *efmp++;
443 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
444 fmt_ptr->prefix = *efmp;
445 else
446 {
447 sprintf((char *)errmsg,
448 _("E376: Invalid %%%c in format string prefix"), *efmp);
449 EMSG(errmsg);
450 goto error2;
451 }
452 }
453 else
454 {
455 sprintf((char *)errmsg,
456 _("E377: Invalid %%%c in format string"), *efmp);
457 EMSG(errmsg);
458 goto error2;
459 }
460 }
461 else /* copy normal character */
462 {
463 if (*efmp == '\\' && efmp + 1 < efm + len)
464 ++efmp;
465 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
466 *ptr++ = '\\'; /* escape regexp atoms */
467 if (*efmp)
468 *ptr++ = *efmp;
469 }
470 }
471 *ptr++ = '$';
472 *ptr = NUL;
473 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
474 goto error2;
475 /*
476 * Advance to next part
477 */
478 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
479 }
480 if (fmt_first == NULL) /* nothing found */
481 {
482 EMSG(_("E378: 'errorformat' contains no pattern"));
483 goto error2;
484 }
485
486 /*
487 * got_int is reset here, because it was probably set when killing the
488 * ":make" command, but we still want to read the errorfile then.
489 */
490 got_int = FALSE;
491
492 /* Always ignore case when looking for a matching error. */
493 regmatch.rm_ic = TRUE;
494
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000495 if (tv != NULL)
496 {
497 if (tv->v_type == VAR_STRING)
498 p_str = tv->vval.v_string;
499 else if (tv->v_type == VAR_LIST)
500 p_li = tv->vval.v_list->lv_first;
501 }
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 /*
504 * Read the lines in the error file one by one.
505 * Try to recognize one of the error formats in each line.
506 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000507 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000509 /* Get the next line. */
510 if (fd == NULL)
511 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000512 if (tv != NULL)
513 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000514 if (tv->v_type == VAR_STRING)
515 {
516 /* Get the next line from the supplied string */
517 char_u *p;
518
519 if (!*p_str) /* Reached the end of the string */
520 break;
521
522 p = vim_strchr(p_str, '\n');
523 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000524 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000525 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000526 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000527
528 if (len > CMDBUFFSIZE - 2)
529 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
530 else
531 vim_strncpy(IObuff, p_str, len);
532
533 p_str += len;
534 }
535 else if (tv->v_type == VAR_LIST)
536 {
537 /* Get the next line from the supplied list */
538 while (p_li && p_li->li_tv.v_type != VAR_STRING)
539 p_li = p_li->li_next; /* Skip non-string items */
540
541 if (!p_li) /* End of the list */
542 break;
543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000544 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000545 if (len > CMDBUFFSIZE - 2)
546 len = CMDBUFFSIZE - 2;
547
548 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
549
550 p_li = p_li->li_next; /* next item */
551 }
552 }
553 else
554 {
555 /* Get the next line from the supplied buffer */
556 if (buflnum > lnumlast)
557 break;
558 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
559 CMDBUFFSIZE - 2);
560 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000561 }
562 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
563 break;
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200566#ifdef FEAT_MBYTE
567 remove_bom(IObuff);
568#endif
569
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
571 *efmp = NUL;
572#ifdef USE_CRNL
573 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
574 *efmp = NUL;
575#endif
576
Bram Moolenaar01265852006-03-20 21:50:15 +0000577 /* If there was no %> item start at the first pattern */
578 if (fmt_start == NULL)
579 fmt_ptr = fmt_first;
580 else
581 {
582 fmt_ptr = fmt_start;
583 fmt_start = NULL;
584 }
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 /*
587 * Try to match each part of 'errorformat' until we find a complete
588 * match or no match.
589 */
590 valid = TRUE;
591restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000592 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {
594 idx = fmt_ptr->prefix;
595 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
596 continue;
597 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000598 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!multiscan)
600 errmsg[0] = NUL;
601 lnum = 0;
602 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000603 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 enr = -1;
605 type = 0;
606 tail = NULL;
607
608 regmatch.regprog = fmt_ptr->prog;
609 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
610 {
611 if ((idx == 'C' || idx == 'Z') && !multiline)
612 continue;
613 if (vim_strchr((char_u *)"EWI", idx) != NULL)
614 type = idx;
615 else
616 type = 0;
617 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000618 * Extract error message data from matched line.
619 * We check for an actual submatch, because "\[" and "\]" in
620 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 */
622 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
623 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000624 int c;
625
626 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
627 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000628
629 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000630 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000631 *regmatch.endp[i] = NUL;
632 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
633 *regmatch.endp[i] = c;
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000636 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 continue;
638 }
639 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000640 {
641 if (regmatch.startp[i] == NULL)
642 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000646 {
647 if (regmatch.startp[i] == NULL)
648 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000652 {
653 if (regmatch.startp[i] == NULL)
654 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000658 {
659 if (regmatch.startp[i] == NULL)
660 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000662 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000663 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 STRCPY(errmsg, IObuff);
665 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
666 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000667 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
668 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000670 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
672 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000673 {
674 if (regmatch.startp[i] == NULL)
675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
679 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200680 char_u *match_ptr;
681
Bram Moolenaar4169da72006-06-20 18:49:32 +0000682 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
683 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200684 col = 0;
685 for (match_ptr = regmatch.startp[i];
686 match_ptr != regmatch.endp[i]; ++match_ptr)
687 {
688 ++col;
689 if (*match_ptr == TAB)
690 {
691 col += 7;
692 col -= col % 8;
693 }
694 }
695 ++col;
696 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
699 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000700 if (regmatch.startp[i] == NULL)
701 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000705 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
706 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000707 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
708 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000709 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
710 if (len > CMDBUFFSIZE - 5)
711 len = CMDBUFFSIZE - 5;
712 STRCPY(pattern, "^\\V");
713 STRNCAT(pattern, regmatch.startp[i], len);
714 pattern[len + 3] = '\\';
715 pattern[len + 4] = '$';
716 pattern[len + 5] = NUL;
717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 break;
719 }
720 }
721 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000722
Bram Moolenaar4770d092006-01-12 23:22:24 +0000723 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000725 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
727 if (idx == 'D') /* enter directory */
728 {
729 if (*namebuf == NUL)
730 {
731 EMSG(_("E379: Missing or empty directory name"));
732 goto error2;
733 }
734 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
735 goto error2;
736 }
737 else if (idx == 'X') /* leave directory */
738 directory = qf_pop_dir(&dir_stack);
739 }
740 namebuf[0] = NUL; /* no match found, remove file name */
741 lnum = 0; /* don't jump to this line */
742 valid = FALSE;
743 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000744 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 multiline = multiignore = FALSE;
746 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000747 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000749 /* honor %> item */
750 if (fmt_ptr->conthere)
751 fmt_start = fmt_ptr;
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
754 multiline = TRUE; /* start of a multi-line message */
755 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
756 { /* continuation of multi-line msg */
757 if (qfprev == NULL)
758 goto error2;
759 if (*errmsg && !multiignore)
760 {
761 len = (int)STRLEN(qfprev->qf_text);
762 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
763 == NULL)
764 goto error2;
765 STRCPY(ptr, qfprev->qf_text);
766 vim_free(qfprev->qf_text);
767 qfprev->qf_text = ptr;
768 *(ptr += len) = '\n';
769 STRCPY(++ptr, errmsg);
770 }
771 if (qfprev->qf_nr == -1)
772 qfprev->qf_nr = enr;
773 if (vim_isprintc(type) && !qfprev->qf_type)
774 qfprev->qf_type = type; /* only printable chars allowed */
775 if (!qfprev->qf_lnum)
776 qfprev->qf_lnum = lnum;
777 if (!qfprev->qf_col)
778 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (!qfprev->qf_fnum)
781 qfprev->qf_fnum = qf_get_fnum(directory,
782 *namebuf || directory ? namebuf
783 : currfile && valid ? currfile : 0);
784 if (idx == 'Z')
785 multiline = multiignore = FALSE;
786 line_breakcheck();
787 continue;
788 }
789 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
790 {
791 /* global file names */
792 valid = FALSE;
793 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
794 {
795 if (*namebuf && idx == 'P')
796 currfile = qf_push_dir(namebuf, &file_stack);
797 else if (idx == 'Q')
798 currfile = qf_pop_dir(&file_stack);
799 *namebuf = NUL;
800 if (tail && *tail)
801 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000802 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 multiscan = TRUE;
804 goto restofline;
805 }
806 }
807 }
808 if (fmt_ptr->flags == '-') /* generally exclude this line */
809 {
810 if (multiline)
811 multiignore = TRUE; /* also exclude continuation lines */
812 continue;
813 }
814 }
815
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000816 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000818 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000820 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000821 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 errmsg,
823 lnum,
824 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000826 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 enr,
828 type,
829 valid) == FAIL)
830 goto error2;
831 line_breakcheck();
832 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000833 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000835 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000837 /* no valid entry found */
838 qi->qf_lists[qi->qf_curlist].qf_ptr =
839 qi->qf_lists[qi->qf_curlist].qf_start;
840 qi->qf_lists[qi->qf_curlist].qf_index = 1;
841 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 else
844 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000845 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
846 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
847 qi->qf_lists[qi->qf_curlist].qf_ptr =
848 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000850 /* return number of matches */
851 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 goto qf_init_ok;
853 }
854 EMSG(_(e_readerrf));
855error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000856 qf_free(qi, qi->qf_curlist);
857 qi->qf_listcount--;
858 if (qi->qf_curlist > 0)
859 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000861 if (fd != NULL)
862 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
864 {
865 fmt_first = fmt_ptr->next;
866 vim_free(fmt_ptr->prog);
867 vim_free(fmt_ptr);
868 }
869 qf_clean_dir_stack(&dir_stack);
870 qf_clean_dir_stack(&file_stack);
871qf_init_end:
872 vim_free(namebuf);
873 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000874 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 vim_free(fmtstr);
876
877#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000878 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
880
881 return retval;
882}
883
884/*
885 * Prepare for adding a new quickfix list.
886 */
887 static void
Bram Moolenaar41b884b2012-11-14 22:38:08 +0100888qf_new_list(qi, qf_title, wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000889 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200890 char_u *qf_title;
Bram Moolenaar41b884b2012-11-14 22:38:08 +0100891 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 int i;
894
895 /*
896 * If the current entry is not the last entry, delete entries below
897 * the current entry. This makes it possible to browse in a tree-like
898 * way with ":grep'.
899 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000900 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaar41b884b2012-11-14 22:38:08 +0100901 {
902 if (wp != NULL && wp->w_llist == qi)
903 wp->w_llist = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000904 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar41b884b2012-11-14 22:38:08 +0100905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
907 /*
908 * When the stack is full, remove to oldest entry
909 * Otherwise, add a new entry.
910 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000911 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 {
Bram Moolenaar41b884b2012-11-14 22:38:08 +0100913 if (wp != NULL && wp->w_llist == qi)
914 wp->w_llist = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000915 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000917 qi->qf_lists[i - 1] = qi->qf_lists[i];
918 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000921 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100922 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200923 if (qf_title != NULL)
924 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200925 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200926
927 qi->qf_lists[qi->qf_curlist].qf_title = p;
928 if (p != NULL)
929 sprintf((char *)p, ":%s", (char *)qf_title);
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931}
932
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000933/*
934 * Free a location list
935 */
936 static void
937ll_free_all(pqi)
938 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000939{
940 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000941 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000942
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000943 qi = *pqi;
944 if (qi == NULL)
945 return;
946 *pqi = NULL; /* Remove reference to this list */
947
948 qi->qf_refcount--;
949 if (qi->qf_refcount < 1)
950 {
951 /* No references to this location list */
952 for (i = 0; i < qi->qf_listcount; ++i)
953 qf_free(qi, i);
954 vim_free(qi);
955 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000956}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000957
958 void
959qf_free_all(wp)
960 win_T *wp;
961{
962 int i;
963 qf_info_T *qi = &ql_info;
964
965 if (wp != NULL)
966 {
967 /* location list */
968 ll_free_all(&wp->w_llist);
969 ll_free_all(&wp->w_llist_ref);
970 }
971 else
972 /* quickfix list */
973 for (i = 0; i < qi->qf_listcount; ++i)
974 qf_free(qi, i);
975}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977/*
978 * Add an entry to the end of the list of errors.
979 * Returns OK or FAIL.
980 */
981 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000982qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
983 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000984 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000985 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 char_u *dir; /* optional directory name */
987 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000988 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 char_u *mesg; /* message */
990 long lnum; /* line number */
991 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000992 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000993 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 int nr; /* error number */
995 int type; /* type character */
996 int valid; /* valid entry */
997{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000998 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001000 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001002 if (bufnum != 0)
1003 qfp->qf_fnum = bufnum;
1004 else
1005 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1007 {
1008 vim_free(qfp);
1009 return FAIL;
1010 }
1011 qfp->qf_lnum = lnum;
1012 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001013 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001014 if (pattern == NULL || *pattern == NUL)
1015 qfp->qf_pattern = NULL;
1016 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1017 {
1018 vim_free(qfp->qf_text);
1019 vim_free(qfp);
1020 return FAIL;
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 qfp->qf_nr = nr;
1023 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1024 type = 0;
1025 qfp->qf_type = type;
1026 qfp->qf_valid = valid;
1027
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001028 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1029 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001031 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 qfp->qf_prev = qfp; /* first element points to itself */
1033 }
1034 else
1035 {
1036 qfp->qf_prev = *prevp;
1037 (*prevp)->qf_next = qfp;
1038 }
1039 qfp->qf_next = qfp; /* last element points to itself */
1040 qfp->qf_cleared = FALSE;
1041 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001042 ++qi->qf_lists[qi->qf_curlist].qf_count;
1043 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1044 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001046 qi->qf_lists[qi->qf_curlist].qf_index =
1047 qi->qf_lists[qi->qf_curlist].qf_count;
1048 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050
1051 return OK;
1052}
1053
1054/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001055 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001056 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001057 static qf_info_T *
1058ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001059{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001060 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001061
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001062 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1063 if (qi != NULL)
1064 {
1065 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1066 qi->qf_refcount++;
1067 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001068
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001069 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001070}
1071
1072/*
1073 * Return the location list for window 'wp'.
1074 * If not present, allocate a location list
1075 */
1076 static qf_info_T *
1077ll_get_or_alloc_list(wp)
1078 win_T *wp;
1079{
1080 if (IS_LL_WINDOW(wp))
1081 /* For a location list window, use the referenced location list */
1082 return wp->w_llist_ref;
1083
1084 /*
1085 * For a non-location list window, w_llist_ref should not point to a
1086 * location list.
1087 */
1088 ll_free_all(&wp->w_llist_ref);
1089
1090 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001091 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001092 return wp->w_llist;
1093}
1094
1095/*
1096 * Copy the location list from window "from" to window "to".
1097 */
1098 void
1099copy_loclist(from, to)
1100 win_T *from;
1101 win_T *to;
1102{
1103 qf_info_T *qi;
1104 int idx;
1105 int i;
1106
1107 /*
1108 * When copying from a location list window, copy the referenced
1109 * location list. For other windows, copy the location list for
1110 * that window.
1111 */
1112 if (IS_LL_WINDOW(from))
1113 qi = from->w_llist_ref;
1114 else
1115 qi = from->w_llist;
1116
1117 if (qi == NULL) /* no location list to copy */
1118 return;
1119
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001120 /* allocate a new location list */
1121 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001122 return;
1123
1124 to->w_llist->qf_listcount = qi->qf_listcount;
1125
1126 /* Copy the location lists one at a time */
1127 for (idx = 0; idx < qi->qf_listcount; idx++)
1128 {
1129 qf_list_T *from_qfl;
1130 qf_list_T *to_qfl;
1131
1132 to->w_llist->qf_curlist = idx;
1133
1134 from_qfl = &qi->qf_lists[idx];
1135 to_qfl = &to->w_llist->qf_lists[idx];
1136
1137 /* Some of the fields are populated by qf_add_entry() */
1138 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1139 to_qfl->qf_count = 0;
1140 to_qfl->qf_index = 0;
1141 to_qfl->qf_start = NULL;
1142 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001143 if (from_qfl->qf_title != NULL)
1144 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1145 else
1146 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001147
1148 if (from_qfl->qf_count)
1149 {
1150 qfline_T *from_qfp;
1151 qfline_T *prevp = NULL;
1152
1153 /* copy all the location entries in this list */
1154 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1155 ++i, from_qfp = from_qfp->qf_next)
1156 {
1157 if (qf_add_entry(to->w_llist, &prevp,
1158 NULL,
1159 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001160 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001161 from_qfp->qf_text,
1162 from_qfp->qf_lnum,
1163 from_qfp->qf_col,
1164 from_qfp->qf_viscol,
1165 from_qfp->qf_pattern,
1166 from_qfp->qf_nr,
1167 0,
1168 from_qfp->qf_valid) == FAIL)
1169 {
1170 qf_free_all(to);
1171 return;
1172 }
1173 /*
1174 * qf_add_entry() will not set the qf_num field, as the
1175 * directory and file names are not supplied. So the qf_fnum
1176 * field is copied here.
1177 */
1178 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1179 prevp->qf_type = from_qfp->qf_type; /* error type */
1180 if (from_qfl->qf_ptr == from_qfp)
1181 to_qfl->qf_ptr = prevp; /* current location */
1182 }
1183 }
1184
1185 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1186
1187 /* When no valid entries are present in the list, qf_ptr points to
1188 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001189 if (to_qfl->qf_nonevalid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001190 to_qfl->qf_ptr = to_qfl->qf_start;
1191 }
1192
1193 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1194}
1195
1196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 * get buffer number for file "dir.name"
1198 */
1199 static int
1200qf_get_fnum(directory, fname)
1201 char_u *directory;
1202 char_u *fname;
1203{
1204 if (fname == NULL || *fname == NUL) /* no file name */
1205 return 0;
1206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 char_u *ptr;
1208 int fnum;
1209
Bram Moolenaare60acc12011-05-10 16:41:25 +02001210#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001212#endif
1213#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (directory != NULL)
1215 slash_adjust(directory);
1216 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (directory != NULL && !vim_isAbsName(fname)
1219 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1220 {
1221 /*
1222 * Here we check if the file really exists.
1223 * This should normally be true, but if make works without
1224 * "leaving directory"-messages we might have missed a
1225 * directory change.
1226 */
1227 if (mch_getperm(ptr) < 0)
1228 {
1229 vim_free(ptr);
1230 directory = qf_guess_filepath(fname);
1231 if (directory)
1232 ptr = concat_fnames(directory, fname, TRUE);
1233 else
1234 ptr = vim_strsave(fname);
1235 }
1236 /* Use concatenated directory name and file name */
1237 fnum = buflist_add(ptr, 0);
1238 vim_free(ptr);
1239 return fnum;
1240 }
1241 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243}
1244
1245/*
1246 * push dirbuf onto the directory stack and return pointer to actual dir or
1247 * NULL on error
1248 */
1249 static char_u *
1250qf_push_dir(dirbuf, stackptr)
1251 char_u *dirbuf;
1252 struct dir_stack_T **stackptr;
1253{
1254 struct dir_stack_T *ds_new;
1255 struct dir_stack_T *ds_ptr;
1256
1257 /* allocate new stack element and hook it in */
1258 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1259 if (ds_new == NULL)
1260 return NULL;
1261
1262 ds_new->next = *stackptr;
1263 *stackptr = ds_new;
1264
1265 /* store directory on the stack */
1266 if (vim_isAbsName(dirbuf)
1267 || (*stackptr)->next == NULL
1268 || (*stackptr && dir_stack != *stackptr))
1269 (*stackptr)->dirname = vim_strsave(dirbuf);
1270 else
1271 {
1272 /* Okay we don't have an absolute path.
1273 * dirbuf must be a subdir of one of the directories on the stack.
1274 * Let's search...
1275 */
1276 ds_new = (*stackptr)->next;
1277 (*stackptr)->dirname = NULL;
1278 while (ds_new)
1279 {
1280 vim_free((*stackptr)->dirname);
1281 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1282 TRUE);
1283 if (mch_isdir((*stackptr)->dirname) == TRUE)
1284 break;
1285
1286 ds_new = ds_new->next;
1287 }
1288
1289 /* clean up all dirs we already left */
1290 while ((*stackptr)->next != ds_new)
1291 {
1292 ds_ptr = (*stackptr)->next;
1293 (*stackptr)->next = (*stackptr)->next->next;
1294 vim_free(ds_ptr->dirname);
1295 vim_free(ds_ptr);
1296 }
1297
1298 /* Nothing found -> it must be on top level */
1299 if (ds_new == NULL)
1300 {
1301 vim_free((*stackptr)->dirname);
1302 (*stackptr)->dirname = vim_strsave(dirbuf);
1303 }
1304 }
1305
1306 if ((*stackptr)->dirname != NULL)
1307 return (*stackptr)->dirname;
1308 else
1309 {
1310 ds_ptr = *stackptr;
1311 *stackptr = (*stackptr)->next;
1312 vim_free(ds_ptr);
1313 return NULL;
1314 }
1315}
1316
1317
1318/*
1319 * pop dirbuf from the directory stack and return previous directory or NULL if
1320 * stack is empty
1321 */
1322 static char_u *
1323qf_pop_dir(stackptr)
1324 struct dir_stack_T **stackptr;
1325{
1326 struct dir_stack_T *ds_ptr;
1327
1328 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1329 * What to do if it isn't? */
1330
1331 /* pop top element and free it */
1332 if (*stackptr != NULL)
1333 {
1334 ds_ptr = *stackptr;
1335 *stackptr = (*stackptr)->next;
1336 vim_free(ds_ptr->dirname);
1337 vim_free(ds_ptr);
1338 }
1339
1340 /* return NEW top element as current dir or NULL if stack is empty*/
1341 return *stackptr ? (*stackptr)->dirname : NULL;
1342}
1343
1344/*
1345 * clean up directory stack
1346 */
1347 static void
1348qf_clean_dir_stack(stackptr)
1349 struct dir_stack_T **stackptr;
1350{
1351 struct dir_stack_T *ds_ptr;
1352
1353 while ((ds_ptr = *stackptr) != NULL)
1354 {
1355 *stackptr = (*stackptr)->next;
1356 vim_free(ds_ptr->dirname);
1357 vim_free(ds_ptr);
1358 }
1359}
1360
1361/*
1362 * Check in which directory of the directory stack the given file can be
1363 * found.
1364 * Returns a pointer to the directory name or NULL if not found
1365 * Cleans up intermediate directory entries.
1366 *
1367 * TODO: How to solve the following problem?
1368 * If we have the this directory tree:
1369 * ./
1370 * ./aa
1371 * ./aa/bb
1372 * ./bb
1373 * ./bb/x.c
1374 * and make says:
1375 * making all in aa
1376 * making all in bb
1377 * x.c:9: Error
1378 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1379 * qf_guess_filepath will return NULL.
1380 */
1381 static char_u *
1382qf_guess_filepath(filename)
1383 char_u *filename;
1384{
1385 struct dir_stack_T *ds_ptr;
1386 struct dir_stack_T *ds_tmp;
1387 char_u *fullname;
1388
1389 /* no dirs on the stack - there's nothing we can do */
1390 if (dir_stack == NULL)
1391 return NULL;
1392
1393 ds_ptr = dir_stack->next;
1394 fullname = NULL;
1395 while (ds_ptr)
1396 {
1397 vim_free(fullname);
1398 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1399
1400 /* If concat_fnames failed, just go on. The worst thing that can happen
1401 * is that we delete the entire stack.
1402 */
1403 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1404 break;
1405
1406 ds_ptr = ds_ptr->next;
1407 }
1408
1409 vim_free(fullname);
1410
1411 /* clean up all dirs we already left */
1412 while (dir_stack->next != ds_ptr)
1413 {
1414 ds_tmp = dir_stack->next;
1415 dir_stack->next = dir_stack->next->next;
1416 vim_free(ds_tmp->dirname);
1417 vim_free(ds_tmp);
1418 }
1419
1420 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1421
1422}
1423
1424/*
1425 * jump to a quickfix line
1426 * if dir == FORWARD go "errornr" valid entries forward
1427 * if dir == BACKWARD go "errornr" valid entries backward
1428 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1429 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1430 * else if "errornr" is zero, redisplay the same line
1431 * else go to entry "errornr"
1432 */
1433 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001434qf_jump(qi, dir, errornr, forceit)
1435 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 int dir;
1437 int errornr;
1438 int forceit;
1439{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001440 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001441 qfline_T *qf_ptr;
1442 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 int qf_index;
1444 int old_qf_fnum;
1445 int old_qf_index;
1446 int prev_index;
1447 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1448 char_u *err = e_no_more_items;
1449 linenr_T i;
1450 buf_T *old_curbuf;
1451 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 colnr_T screen_col;
1453 colnr_T char_col;
1454 char_u *line;
1455#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001456 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001457 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 int opened_window = FALSE;
1459 win_T *win;
1460 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001461 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001463 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 int print_message = TRUE;
1465 int len;
1466#ifdef FEAT_FOLDING
1467 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1468#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001469 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001470 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001472 if (qi == NULL)
1473 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001474
1475 if (qi->qf_curlist >= qi->qf_listcount
1476 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
1478 EMSG(_(e_quickfix));
1479 return;
1480 }
1481
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001482 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001484 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 old_qf_index = qf_index;
1486 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1487 {
1488 while (errornr--)
1489 {
1490 old_qf_ptr = qf_ptr;
1491 prev_index = qf_index;
1492 old_qf_fnum = qf_ptr->qf_fnum;
1493 do
1494 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001495 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 || qf_ptr->qf_next == NULL)
1497 {
1498 qf_ptr = old_qf_ptr;
1499 qf_index = prev_index;
1500 if (err != NULL)
1501 {
1502 EMSG(_(err));
1503 goto theend;
1504 }
1505 errornr = 0;
1506 break;
1507 }
1508 ++qf_index;
1509 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001510 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1511 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1513 err = NULL;
1514 }
1515 }
1516 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1517 {
1518 while (errornr--)
1519 {
1520 old_qf_ptr = qf_ptr;
1521 prev_index = qf_index;
1522 old_qf_fnum = qf_ptr->qf_fnum;
1523 do
1524 {
1525 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1526 {
1527 qf_ptr = old_qf_ptr;
1528 qf_index = prev_index;
1529 if (err != NULL)
1530 {
1531 EMSG(_(err));
1532 goto theend;
1533 }
1534 errornr = 0;
1535 break;
1536 }
1537 --qf_index;
1538 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001539 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1540 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1542 err = NULL;
1543 }
1544 }
1545 else if (errornr != 0) /* go to specified number */
1546 {
1547 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1548 {
1549 --qf_index;
1550 qf_ptr = qf_ptr->qf_prev;
1551 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001552 while (errornr > qf_index && qf_index <
1553 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 && qf_ptr->qf_next != NULL)
1555 {
1556 ++qf_index;
1557 qf_ptr = qf_ptr->qf_next;
1558 }
1559 }
1560
1561#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001562 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1563 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 /* No need to print the error message if it's visible in the error
1565 * window */
1566 print_message = FALSE;
1567
1568 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001569 * For ":helpgrep" find a help window or open one.
1570 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001571 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001572 {
1573 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001574
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001575 if (cmdmod.tab != 0)
1576 wp = NULL;
1577 else
1578 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1579 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1580 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001581 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1582 win_enter(wp, TRUE);
1583 else
1584 {
1585 /*
1586 * Split off help window; put it at far top if no position
1587 * specified, the current window is vertically split and narrow.
1588 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001589 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001590# ifdef FEAT_VERTSPLIT
1591 if (cmdmod.split == 0 && curwin->w_width != Columns
1592 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001593 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001594# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001595 if (qi != &ql_info)
1596 flags |= WSP_NEWLOC; /* don't copy the location list */
1597
1598 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001599 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001600 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001601
1602 if (curwin->w_height < p_hh)
1603 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001604
1605 if (qi != &ql_info) /* not a quickfix list */
1606 {
1607 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001608 curwin->w_llist = qi;
1609 qi->qf_refcount++;
1610 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001611 }
1612
1613 if (!p_im)
1614 restart_edit = 0; /* don't want insert mode in help file */
1615 }
1616
1617 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 * If currently in the quickfix window, find another window to show the
1619 * file in.
1620 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001621 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 /*
1624 * If there is no file specified, we don't know where to go.
1625 * But do advance, otherwise ":cn" gets stuck.
1626 */
1627 if (qf_ptr->qf_fnum == 0)
1628 goto theend;
1629
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001630 /* Locate a window showing a normal buffer */
1631 usable_win = 0;
1632 FOR_ALL_WINDOWS(win)
1633 if (win->w_buffer->b_p_bt[0] == NUL)
1634 {
1635 usable_win = 1;
1636 break;
1637 }
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001640 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001641 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001643 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001644 {
1645 tabpage_T *tp;
1646 win_T *wp;
1647
1648 FOR_ALL_TAB_WINDOWS(tp, wp)
1649 {
1650 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1651 {
1652 goto_tabpage_win(tp, wp);
1653 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001654 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001655 }
1656 }
1657 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001658win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001659
1660 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001661 * If there is only one window and it is the quickfix window, create a
1662 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001663 */
1664 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001666 ll_ref = curwin->w_llist_ref;
1667
Bram Moolenaar884ae642009-02-22 01:37:59 +00001668 flags = WSP_ABOVE;
1669 if (ll_ref != NULL)
1670 flags |= WSP_NEWLOC;
1671 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 goto failed; /* not enough room for window */
1673 opened_window = TRUE; /* close it when fail */
1674 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001675 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001676 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001677 if (ll_ref != NULL)
1678 {
1679 /* The new window should use the location list from the
1680 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001681 curwin->w_llist = ll_ref;
1682 ll_ref->qf_refcount++;
1683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 }
1685 else
1686 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001687 if (curwin->w_llist_ref != NULL)
1688 {
1689 /* In a location window */
1690 ll_ref = curwin->w_llist_ref;
1691
1692 /* Find the window with the same location list */
1693 FOR_ALL_WINDOWS(win)
1694 if (win->w_llist == ll_ref)
1695 break;
1696 if (win == NULL)
1697 {
1698 /* Find the window showing the selected file */
1699 FOR_ALL_WINDOWS(win)
1700 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1701 break;
1702 if (win == NULL)
1703 {
1704 /* Find a previous usable window */
1705 win = curwin;
1706 do
1707 {
1708 if (win->w_buffer->b_p_bt[0] == NUL)
1709 break;
1710 if (win->w_prev == NULL)
1711 win = lastwin; /* wrap around the top */
1712 else
1713 win = win->w_prev; /* go to previous window */
1714 } while (win != curwin);
1715 }
1716 }
1717 win_goto(win);
1718
1719 /* If the location list for the window is not set, then set it
1720 * to the location list from the location window */
1721 if (win->w_llist == NULL)
1722 {
1723 win->w_llist = ll_ref;
1724 ll_ref->qf_refcount++;
1725 }
1726 }
1727 else
1728 {
1729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 /*
1731 * Try to find a window that shows the right buffer.
1732 * Default to the window just above the quickfix buffer.
1733 */
1734 win = curwin;
1735 altwin = NULL;
1736 for (;;)
1737 {
1738 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1739 break;
1740 if (win->w_prev == NULL)
1741 win = lastwin; /* wrap around the top */
1742 else
1743 win = win->w_prev; /* go to previous window */
1744
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001745 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
1747 /* Didn't find it, go to the window before the quickfix
1748 * window. */
1749 if (altwin != NULL)
1750 win = altwin;
1751 else if (curwin->w_prev != NULL)
1752 win = curwin->w_prev;
1753 else
1754 win = curwin->w_next;
1755 break;
1756 }
1757
1758 /* Remember a usable window. */
1759 if (altwin == NULL && !win->w_p_pvw
1760 && win->w_buffer->b_p_bt[0] == NUL)
1761 altwin = win;
1762 }
1763
1764 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767 }
1768#endif
1769
1770 /*
1771 * If there is a file name,
1772 * read the wanted file if needed, and check autowrite etc.
1773 */
1774 old_curbuf = curbuf;
1775 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001776
1777 if (qf_ptr->qf_fnum != 0)
1778 {
1779 if (qf_ptr->qf_type == 1)
1780 {
1781 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1782 * set b_p_ro flag). */
1783 if (!can_abandon(curbuf, forceit))
1784 {
1785 EMSG(_(e_nowrtmsg));
1786 ok = FALSE;
1787 }
1788 else
1789 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001790 ECMD_HIDE + ECMD_SET_HELP,
1791 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001792 }
1793 else
1794 ok = buflist_getfile(qf_ptr->qf_fnum,
1795 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1796 }
1797
1798 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
1800 /* When not switched to another buffer, still need to set pc mark */
1801 if (curbuf == old_curbuf)
1802 setpcmark();
1803
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001804 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001806 /*
1807 * Go to line with error, unless qf_lnum is 0.
1808 */
1809 i = qf_ptr->qf_lnum;
1810 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001812 if (i > curbuf->b_ml.ml_line_count)
1813 i = curbuf->b_ml.ml_line_count;
1814 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001816 if (qf_ptr->qf_col > 0)
1817 {
1818 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1819 if (qf_ptr->qf_viscol == TRUE)
1820 {
1821 /*
1822 * Check each character from the beginning of the error
1823 * line up to the error column. For each tab character
1824 * found, reduce the error column value by the length of
1825 * a tab character.
1826 */
1827 line = ml_get_curline();
1828 screen_col = 0;
1829 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1830 {
1831 if (*line == NUL)
1832 break;
1833 if (*line++ == '\t')
1834 {
1835 curwin->w_cursor.col -= 7 - (screen_col % 8);
1836 screen_col += 8 - (screen_col % 8);
1837 }
1838 else
1839 ++screen_col;
1840 }
1841 }
1842 check_cursor();
1843 }
1844 else
1845 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001848 {
1849 pos_T save_cursor;
1850
1851 /* Move the cursor to the first line in the buffer */
1852 save_cursor = curwin->w_cursor;
1853 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001854 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1855 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001856 curwin->w_cursor = save_cursor;
1857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
1859#ifdef FEAT_FOLDING
1860 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1861 foldOpenCursor();
1862#endif
1863 if (print_message)
1864 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001865 /* Update the screen before showing the message, unless the screen
1866 * scrolled up. */
1867 if (!msg_scrolled)
1868 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001870 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1872 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1873 /* Add the message, skipping leading whitespace and newlines. */
1874 len = (int)STRLEN(IObuff);
1875 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1876
1877 /* Output the message. Overwrite to avoid scrolling when the 'O'
1878 * flag is present in 'shortmess'; But when not jumping, print the
1879 * whole message. */
1880 i = msg_scroll;
1881 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1882 msg_scroll = TRUE;
1883 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1884 msg_scroll = FALSE;
1885 msg_attr_keep(IObuff, 0, TRUE);
1886 msg_scroll = i;
1887 }
1888 }
1889 else
1890 {
1891#ifdef FEAT_WINDOWS
1892 if (opened_window)
1893 win_close(curwin, TRUE); /* Close opened window */
1894#endif
1895 if (qf_ptr->qf_fnum != 0)
1896 {
1897 /*
1898 * Couldn't open file, so put index back where it was. This could
1899 * happen if the file was readonly and we changed something.
1900 */
1901#ifdef FEAT_WINDOWS
1902failed:
1903#endif
1904 qf_ptr = old_qf_ptr;
1905 qf_index = old_qf_index;
1906 }
1907 }
1908theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001909 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1910 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911#ifdef FEAT_WINDOWS
1912 if (p_swb != old_swb && opened_window)
1913 {
1914 /* Restore old 'switchbuf' value, but not when an autocommand or
1915 * modeline has changed the value. */
1916 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001919 swb_flags = old_swb_flags;
1920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 else
1922 free_string_option(old_swb);
1923 }
1924#endif
1925}
1926
1927/*
1928 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001929 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931 void
1932qf_list(eap)
1933 exarg_T *eap;
1934{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001935 buf_T *buf;
1936 char_u *fname;
1937 qfline_T *qfp;
1938 int i;
1939 int idx1 = 1;
1940 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001941 char_u *arg = eap->arg;
1942 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001944 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946 if (eap->cmdidx == CMD_llist)
1947 {
1948 qi = GET_LOC_LIST(curwin);
1949 if (qi == NULL)
1950 {
1951 EMSG(_(e_loclist));
1952 return;
1953 }
1954 }
1955
1956 if (qi->qf_curlist >= qi->qf_listcount
1957 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 EMSG(_(e_quickfix));
1960 return;
1961 }
1962 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1963 {
1964 EMSG(_(e_trailing));
1965 return;
1966 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001967 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (idx1 < 0)
1969 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1970 if (idx2 < 0)
1971 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1972
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001975 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1976 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1979 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001980 msg_putchar('\n');
1981 if (got_int)
1982 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001983
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001984 fname = NULL;
1985 if (qfp->qf_fnum != 0
1986 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1987 {
1988 fname = buf->b_fname;
1989 if (qfp->qf_type == 1) /* :helpgrep */
1990 fname = gettail(fname);
1991 }
1992 if (fname == NULL)
1993 sprintf((char *)IObuff, "%2d", i);
1994 else
1995 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1996 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001997 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001998 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1999 if (qfp->qf_lnum == 0)
2000 IObuff[0] = NUL;
2001 else if (qfp->qf_col == 0)
2002 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2003 else
2004 sprintf((char *)IObuff, ":%ld col %d",
2005 qfp->qf_lnum, qfp->qf_col);
2006 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2007 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2008 msg_puts_attr(IObuff, hl_attr(HLF_N));
2009 if (qfp->qf_pattern != NULL)
2010 {
2011 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2012 STRCAT(IObuff, ":");
2013 msg_puts(IObuff);
2014 }
2015 msg_puts((char_u *)" ");
2016
2017 /* Remove newlines and leading whitespace from the text. For an
2018 * unrecognized line keep the indent, the compiler may mark a word
2019 * with ^^^^. */
2020 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2022 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002023 msg_prt_line(IObuff, FALSE);
2024 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002026
2027 qfp = qfp->qf_next;
2028 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 ui_breakcheck();
2030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031}
2032
2033/*
2034 * Remove newlines and leading whitespace from an error message.
2035 * Put the result in "buf[bufsize]".
2036 */
2037 static void
2038qf_fmt_text(text, buf, bufsize)
2039 char_u *text;
2040 char_u *buf;
2041 int bufsize;
2042{
2043 int i;
2044 char_u *p = text;
2045
2046 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2047 {
2048 if (*p == '\n')
2049 {
2050 buf[i] = ' ';
2051 while (*++p != NUL)
2052 if (!vim_iswhite(*p) && *p != '\n')
2053 break;
2054 }
2055 else
2056 buf[i] = *p++;
2057 }
2058 buf[i] = NUL;
2059}
2060
2061/*
2062 * ":colder [count]": Up in the quickfix stack.
2063 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002064 * ":lolder [count]": Up in the location list stack.
2065 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 */
2067 void
2068qf_age(eap)
2069 exarg_T *eap;
2070{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 int count;
2073
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002074 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2075 {
2076 qi = GET_LOC_LIST(curwin);
2077 if (qi == NULL)
2078 {
2079 EMSG(_(e_loclist));
2080 return;
2081 }
2082 }
2083
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (eap->addr_count != 0)
2085 count = eap->line2;
2086 else
2087 count = 1;
2088 while (count--)
2089 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 EMSG(_("E380: At bottom of quickfix stack"));
2095 return;
2096 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002097 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
2099 else
2100 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002101 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 EMSG(_("E381: At top of quickfix stack"));
2104 return;
2105 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109 qf_msg(qi);
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112
2113 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002114qf_msg(qi)
2115 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 qi->qf_curlist + 1, qi->qf_listcount,
2119 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002121 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#endif
2123}
2124
2125/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002126 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 */
2128 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002129qf_free(qi, idx)
2130 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 int idx;
2132{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002133 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002135 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002137 qfp = qi->qf_lists[idx].qf_start->qf_next;
2138 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2139 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2140 vim_free(qi->qf_lists[idx].qf_start);
2141 qi->qf_lists[idx].qf_start = qfp;
2142 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002144 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002145 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146}
2147
2148/*
2149 * qf_mark_adjust: adjust marks
2150 */
2151 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152qf_mark_adjust(wp, line1, line2, amount, amount_after)
2153 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 linenr_T line1;
2155 linenr_T line2;
2156 long amount;
2157 long amount_after;
2158{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002159 int i;
2160 qfline_T *qfp;
2161 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002164 if (wp != NULL)
2165 {
2166 if (wp->w_llist == NULL)
2167 return;
2168 qi = wp->w_llist;
2169 }
2170
2171 for (idx = 0; idx < qi->qf_listcount; ++idx)
2172 if (qi->qf_lists[idx].qf_count)
2173 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2174 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (qfp->qf_fnum == curbuf->b_fnum)
2176 {
2177 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2178 {
2179 if (amount == MAXLNUM)
2180 qfp->qf_cleared = TRUE;
2181 else
2182 qfp->qf_lnum += amount;
2183 }
2184 else if (amount_after && qfp->qf_lnum > line2)
2185 qfp->qf_lnum += amount_after;
2186 }
2187}
2188
2189/*
2190 * Make a nice message out of the error character and the error number:
2191 * char number message
2192 * e or E 0 " error"
2193 * w or W 0 " warning"
2194 * i or I 0 " info"
2195 * 0 0 ""
2196 * other 0 " c"
2197 * e or E n " error n"
2198 * w or W n " warning n"
2199 * i or I n " info n"
2200 * 0 n " error n"
2201 * other n " c n"
2202 * 1 x "" :helpgrep
2203 */
2204 static char_u *
2205qf_types(c, nr)
2206 int c, nr;
2207{
2208 static char_u buf[20];
2209 static char_u cc[3];
2210 char_u *p;
2211
2212 if (c == 'W' || c == 'w')
2213 p = (char_u *)" warning";
2214 else if (c == 'I' || c == 'i')
2215 p = (char_u *)" info";
2216 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2217 p = (char_u *)" error";
2218 else if (c == 0 || c == 1)
2219 p = (char_u *)"";
2220 else
2221 {
2222 cc[0] = ' ';
2223 cc[1] = c;
2224 cc[2] = NUL;
2225 p = cc;
2226 }
2227
2228 if (nr <= 0)
2229 return p;
2230
2231 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2232 return buf;
2233}
2234
2235#if defined(FEAT_WINDOWS) || defined(PROTO)
2236/*
2237 * ":cwindow": open the quickfix window if we have errors to display,
2238 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002239 * ":lwindow": open the location list window if we have locations to display,
2240 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 */
2242 void
2243ex_cwindow(eap)
2244 exarg_T *eap;
2245{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002246 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 win_T *win;
2248
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002249 if (eap->cmdidx == CMD_lwindow)
2250 {
2251 qi = GET_LOC_LIST(curwin);
2252 if (qi == NULL)
2253 return;
2254 }
2255
2256 /* Look for an existing quickfix window. */
2257 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 /*
2260 * If a quickfix window is open but we have no errors to display,
2261 * close the window. If a quickfix window is not open, then open
2262 * it if we have errors; otherwise, leave it closed.
2263 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002264 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002265 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002266 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
2268 if (win != NULL)
2269 ex_cclose(eap);
2270 }
2271 else if (win == NULL)
2272 ex_copen(eap);
2273}
2274
2275/*
2276 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002277 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 void
2280ex_cclose(eap)
2281 exarg_T *eap;
2282{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002283 win_T *win = NULL;
2284 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002286 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2287 {
2288 qi = GET_LOC_LIST(curwin);
2289 if (qi == NULL)
2290 return;
2291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002293 /* Find existing quickfix window and close it. */
2294 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (win != NULL)
2296 win_close(win, FALSE);
2297}
2298
2299/*
2300 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002301 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 */
2303 void
2304ex_copen(eap)
2305 exarg_T *eap;
2306{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002310 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002311 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002312 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002314 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2315 {
2316 qi = GET_LOC_LIST(curwin);
2317 if (qi == NULL)
2318 {
2319 EMSG(_(e_loclist));
2320 return;
2321 }
2322 }
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (eap->addr_count != 0)
2325 height = eap->line2;
2326 else
2327 height = QF_WINHEIGHT;
2328
2329#ifdef FEAT_VISUAL
2330 reset_VIsual_and_resel(); /* stop Visual mode */
2331#endif
2332#ifdef FEAT_GUI
2333 need_mouse_correct = TRUE;
2334#endif
2335
2336 /*
2337 * Find existing quickfix window, or open a new one.
2338 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 win = qf_find_win(qi);
2340
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002341 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 win_goto(win);
2343 else
2344 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002345 qf_buf = qf_find_buf(qi);
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 /* The current window becomes the previous window afterwards. */
2348 win = curwin;
2349
Bram Moolenaar77642c02012-11-20 17:55:10 +01002350 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2351 && cmdmod.split == 0)
2352 /* Create the new window at the very bottom, except when
2353 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002354 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002355 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002357 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002359 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002361 /*
2362 * For the location list window, create a reference to the
2363 * location list from the window 'win'.
2364 */
2365 curwin->w_llist_ref = win->w_llist;
2366 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002368
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002369 if (oldwin != curwin)
2370 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002371 if (qf_buf != NULL)
2372 /* Use the existing quickfix buffer */
2373 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002374 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002375 else
2376 {
2377 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002378 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002379 /* switch off 'swapfile' */
2380 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2381 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002382 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002383 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002384 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002385#ifdef FEAT_DIFF
2386 curwin->w_p_diff = FALSE;
2387#endif
2388#ifdef FEAT_FOLDING
2389 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2390 OPT_LOCAL);
2391#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002394 /* Only set the height when still in the same tab page and there is no
2395 * window to the side. */
2396 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002397#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002398 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002399#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002400 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 win_setheight(height);
2402 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2403 if (win_valid(win))
2404 prevwin = win;
2405 }
2406
2407 /*
2408 * Fill the buffer with the quickfix list.
2409 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002410 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002412 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002413 qf_set_title(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002414
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002415 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 curwin->w_cursor.col = 0;
2417 check_cursor();
2418 update_topline(); /* scroll to show the line */
2419}
2420
2421/*
2422 * Return the number of the current entry (line number in the quickfix
2423 * window).
2424 */
2425 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426qf_current_entry(wp)
2427 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002429 qf_info_T *qi = &ql_info;
2430
2431 if (IS_LL_WINDOW(wp))
2432 /* In the location list window, use the referenced location list */
2433 qi = wp->w_llist_ref;
2434
2435 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436}
2437
2438/*
2439 * Update the cursor position in the quickfix window to the current error.
2440 * Return TRUE if there is a quickfix window.
2441 */
2442 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002443qf_win_pos_update(qi, old_qf_index)
2444 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 int old_qf_index; /* previous qf_index or zero */
2446{
2447 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002448 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450 /*
2451 * Put the cursor on the current error in the quickfix window, so that
2452 * it's viewable.
2453 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002454 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (win != NULL
2456 && qf_index <= win->w_buffer->b_ml.ml_line_count
2457 && old_qf_index != qf_index)
2458 {
2459 win_T *old_curwin = curwin;
2460
2461 curwin = win;
2462 curbuf = win->w_buffer;
2463 if (qf_index > old_qf_index)
2464 {
2465 curwin->w_redraw_top = old_qf_index;
2466 curwin->w_redraw_bot = qf_index;
2467 }
2468 else
2469 {
2470 curwin->w_redraw_top = qf_index;
2471 curwin->w_redraw_bot = old_qf_index;
2472 }
2473 curwin->w_cursor.lnum = qf_index;
2474 curwin->w_cursor.col = 0;
2475 update_topline(); /* scroll to show the line */
2476 redraw_later(VALID);
2477 curwin->w_redr_status = TRUE; /* update ruler */
2478 curwin = old_curwin;
2479 curbuf = curwin->w_buffer;
2480 }
2481 return win != NULL;
2482}
2483
2484/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002485 * Check whether the given window is displaying the specified quickfix/location
2486 * list buffer
2487 */
2488 static int
2489is_qf_win(win, qi)
2490 win_T *win;
2491 qf_info_T *qi;
2492{
2493 /*
2494 * A window displaying the quickfix buffer will have the w_llist_ref field
2495 * set to NULL.
2496 * A window displaying a location list buffer will have the w_llist_ref
2497 * pointing to the location list.
2498 */
2499 if (bt_quickfix(win->w_buffer))
2500 if ((qi == &ql_info && win->w_llist_ref == NULL)
2501 || (qi != &ql_info && win->w_llist_ref == qi))
2502 return TRUE;
2503
2504 return FALSE;
2505}
2506
2507/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002508 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002509 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002510 */
2511 static win_T *
2512qf_find_win(qi)
2513 qf_info_T *qi;
2514{
2515 win_T *win;
2516
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002517 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002518 if (is_qf_win(win, qi))
2519 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002520
2521 return win;
2522}
2523
2524/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002525 * Find a quickfix buffer.
2526 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 */
2528 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002529qf_find_buf(qi)
2530 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002532 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002533 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaar9c102382006-05-03 21:26:49 +00002535 FOR_ALL_TAB_WINDOWS(tp, win)
2536 if (is_qf_win(win, qi))
2537 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002538
Bram Moolenaar9c102382006-05-03 21:26:49 +00002539 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540}
2541
2542/*
2543 * Find the quickfix buffer. If it exists, update the contents.
2544 */
2545 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002546qf_update_buffer(qi)
2547 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
2549 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002550 win_T *win;
2551 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002555 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 if (buf != NULL)
2557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 /* set curwin/curbuf to buf and save a few things */
2559 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002561 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002563 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2564 && (win = qf_find_win(qi)) != NULL)
2565 {
2566 curwin_save = curwin;
2567 curwin = win;
2568 qf_set_title(qi);
2569 curwin = curwin_save;
2570
2571 }
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 /* restore curwin/curbuf and a few other things */
2574 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002576 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
2578}
2579
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002580 static void
2581qf_set_title(qi)
2582 qf_info_T *qi;
2583{
2584 set_internal_string_var((char_u *)"w:quickfix_title",
2585 qi->qf_lists[qi->qf_curlist].qf_title);
2586}
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
2589 * Fill current buffer with quickfix errors, replacing any previous contents.
2590 * curbuf must be the quickfix buffer!
2591 */
2592 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002593qf_fill_buffer(qi)
2594 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002596 linenr_T lnum;
2597 qfline_T *qfp;
2598 buf_T *errbuf;
2599 int len;
2600 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
2602 /* delete all existing lines */
2603 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2604 (void)ml_delete((linenr_T)1, FALSE);
2605
2606 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002607 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002610 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2611 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 if (qfp->qf_fnum != 0
2614 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2615 && errbuf->b_fname != NULL)
2616 {
2617 if (qfp->qf_type == 1) /* :helpgrep */
2618 STRCPY(IObuff, gettail(errbuf->b_fname));
2619 else
2620 STRCPY(IObuff, errbuf->b_fname);
2621 len = (int)STRLEN(IObuff);
2622 }
2623 else
2624 len = 0;
2625 IObuff[len++] = '|';
2626
2627 if (qfp->qf_lnum > 0)
2628 {
2629 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2630 len += (int)STRLEN(IObuff + len);
2631
2632 if (qfp->qf_col > 0)
2633 {
2634 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2635 len += (int)STRLEN(IObuff + len);
2636 }
2637
2638 sprintf((char *)IObuff + len, "%s",
2639 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2640 len += (int)STRLEN(IObuff + len);
2641 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002642 else if (qfp->qf_pattern != NULL)
2643 {
2644 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2645 len += (int)STRLEN(IObuff + len);
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 IObuff[len++] = '|';
2648 IObuff[len++] = ' ';
2649
2650 /* Remove newlines and leading whitespace from the text.
2651 * For an unrecognized line keep the indent, the compiler may
2652 * mark a word with ^^^^. */
2653 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2654 IObuff + len, IOSIZE - len);
2655
2656 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2657 == FAIL)
2658 break;
2659 qfp = qfp->qf_next;
2660 }
2661 /* Delete the empty line which is now at the end */
2662 (void)ml_delete(lnum + 1, FALSE);
2663 }
2664
2665 /* correct cursor position */
2666 check_lnums(TRUE);
2667
2668 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2669 * resembles reading a file into a buffer, it's more logical when using
2670 * autocommands. */
2671 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2672 curbuf->b_p_ma = FALSE;
2673
2674#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002675 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2677 FALSE, curbuf);
2678 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2679 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002680 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681#endif
2682
2683 /* make sure it will be redrawn */
2684 redraw_curbuf_later(NOT_VALID);
2685
2686 /* Restore KeyTyped, setting 'filetype' may reset it. */
2687 KeyTyped = old_KeyTyped;
2688}
2689
2690#endif /* FEAT_WINDOWS */
2691
2692/*
2693 * Return TRUE if "buf" is the quickfix buffer.
2694 */
2695 int
2696bt_quickfix(buf)
2697 buf_T *buf;
2698{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002699 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700}
2701
2702/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002703 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2704 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 */
2706 int
2707bt_nofile(buf)
2708 buf_T *buf;
2709{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002710 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2711 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712}
2713
2714/*
2715 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2716 */
2717 int
2718bt_dontwrite(buf)
2719 buf_T *buf;
2720{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002721 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722}
2723
2724 int
2725bt_dontwrite_msg(buf)
2726 buf_T *buf;
2727{
2728 if (bt_dontwrite(buf))
2729 {
2730 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2731 return TRUE;
2732 }
2733 return FALSE;
2734}
2735
2736/*
2737 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2738 * and 'bufhidden'.
2739 */
2740 int
2741buf_hide(buf)
2742 buf_T *buf;
2743{
2744 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2745 switch (buf->b_p_bh[0])
2746 {
2747 case 'u': /* "unload" */
2748 case 'w': /* "wipe" */
2749 case 'd': return FALSE; /* "delete" */
2750 case 'h': return TRUE; /* "hide" */
2751 }
2752 return (p_hid || cmdmod.hide);
2753}
2754
2755/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002756 * Return TRUE when using ":vimgrep" for ":grep".
2757 */
2758 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002759grep_internal(cmdidx)
2760 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002761{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002762 return ((cmdidx == CMD_grep
2763 || cmdidx == CMD_lgrep
2764 || cmdidx == CMD_grepadd
2765 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002766 && STRCMP("internal",
2767 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2768}
2769
2770/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002771 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 */
2773 void
2774ex_make(eap)
2775 exarg_T *eap;
2776{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002777 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 char_u *cmd;
2779 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002780 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002781 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002782 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002783#ifdef FEAT_AUTOCMD
2784 char_u *au_name = NULL;
2785
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002786 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2787 if (grep_internal(eap->cmdidx))
2788 {
2789 ex_vimgrep(eap);
2790 return;
2791 }
2792
Bram Moolenaar7c626922005-02-07 22:01:03 +00002793 switch (eap->cmdidx)
2794 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002795 case CMD_make: au_name = (char_u *)"make"; break;
2796 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2797 case CMD_grep: au_name = (char_u *)"grep"; break;
2798 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2799 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2800 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002801 default: break;
2802 }
2803 if (au_name != NULL)
2804 {
2805 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2806 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002807# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002808 if (did_throw || force_abort)
2809 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002810# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002811 }
2812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
Bram Moolenaara6557602006-02-04 22:43:20 +00002814 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2815 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002816 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002817
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002819 fname = get_mef_name();
2820 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002822 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824 /*
2825 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2826 */
2827 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2828 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002829 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 cmd = alloc(len);
2831 if (cmd == NULL)
2832 return;
2833 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2834 (char *)p_shq);
2835 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002836 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 /*
2838 * Output a newline if there's something else than the :make command that
2839 * was typed (in which case the cursor is in column 0).
2840 */
2841 if (msg_col == 0)
2842 msg_didout = FALSE;
2843 msg_start();
2844 MSG_PUTS(":!");
2845 msg_outtrans(cmd); /* show what we are doing */
2846
2847 /* let the shell know if we are redirecting output or not */
2848 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2849
2850#ifdef AMIGA
2851 out_flush();
2852 /* read window status report and redraw before message */
2853 (void)char_avail();
2854#endif
2855
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002856 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002857 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2858 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002859 && eap->cmdidx != CMD_lgrepadd),
2860 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002861 if (wp != NULL)
2862 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002863#ifdef FEAT_AUTOCMD
2864 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002865 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002866 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2867 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002868 if (qi->qf_curlist < qi->qf_listcount)
2869 res = qi->qf_lists[qi->qf_curlist].qf_count;
2870 else
2871 res = 0;
2872 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002873#endif
2874 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002875 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaar7c626922005-02-07 22:01:03 +00002877 mch_remove(fname);
2878 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 vim_free(cmd);
2880}
2881
2882/*
2883 * Return the name for the errorfile, in allocated memory.
2884 * Find a new unique name when 'makeef' contains "##".
2885 * Returns NULL for error.
2886 */
2887 static char_u *
2888get_mef_name()
2889{
2890 char_u *p;
2891 char_u *name;
2892 static int start = -1;
2893 static int off = 0;
2894#ifdef HAVE_LSTAT
2895 struct stat sb;
2896#endif
2897
2898 if (*p_mef == NUL)
2899 {
2900 name = vim_tempname('e');
2901 if (name == NULL)
2902 EMSG(_(e_notmp));
2903 return name;
2904 }
2905
2906 for (p = p_mef; *p; ++p)
2907 if (p[0] == '#' && p[1] == '#')
2908 break;
2909
2910 if (*p == NUL)
2911 return vim_strsave(p_mef);
2912
2913 /* Keep trying until the name doesn't exist yet. */
2914 for (;;)
2915 {
2916 if (start == -1)
2917 start = mch_get_pid();
2918 else
2919 off += 19;
2920
2921 name = alloc((unsigned)STRLEN(p_mef) + 30);
2922 if (name == NULL)
2923 break;
2924 STRCPY(name, p_mef);
2925 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2926 STRCAT(name, p + 2);
2927 if (mch_getperm(name) < 0
2928#ifdef HAVE_LSTAT
2929 /* Don't accept a symbolic link, its a security risk. */
2930 && mch_lstat((char *)name, &sb) < 0
2931#endif
2932 )
2933 break;
2934 vim_free(name);
2935 }
2936 return name;
2937}
2938
2939/*
2940 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002941 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 */
2943 void
2944ex_cc(eap)
2945 exarg_T *eap;
2946{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002947 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002948
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002949 if (eap->cmdidx == CMD_ll
2950 || eap->cmdidx == CMD_lrewind
2951 || eap->cmdidx == CMD_lfirst
2952 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002953 {
2954 qi = GET_LOC_LIST(curwin);
2955 if (qi == NULL)
2956 {
2957 EMSG(_(e_loclist));
2958 return;
2959 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002960 }
2961
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002962 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 eap->addr_count > 0
2964 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002965 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002967 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2968 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 ? 1
2970 : 32767,
2971 eap->forceit);
2972}
2973
2974/*
2975 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002976 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 */
2978 void
2979ex_cnext(eap)
2980 exarg_T *eap;
2981{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002982 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002983
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002984 if (eap->cmdidx == CMD_lnext
2985 || eap->cmdidx == CMD_lNext
2986 || eap->cmdidx == CMD_lprevious
2987 || eap->cmdidx == CMD_lnfile
2988 || eap->cmdidx == CMD_lNfile
2989 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002990 {
2991 qi = GET_LOC_LIST(curwin);
2992 if (qi == NULL)
2993 {
2994 EMSG(_(e_loclist));
2995 return;
2996 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002997 }
2998
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002999 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003001 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003003 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3004 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 ? BACKWARD_FILE
3006 : BACKWARD,
3007 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
3008}
3009
3010/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003011 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003012 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 */
3014 void
3015ex_cfile(eap)
3016 exarg_T *eap;
3017{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003018 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003019 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003020#ifdef FEAT_AUTOCMD
3021 char_u *au_name = NULL;
3022#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003023
3024 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003025 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003026 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003027
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003028#ifdef FEAT_AUTOCMD
3029 switch (eap->cmdidx)
3030 {
3031 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3032 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3033 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3034 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3035 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3036 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3037 default: break;
3038 }
3039 if (au_name != NULL)
3040 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3041#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003042#ifdef FEAT_BROWSE
3043 if (cmdmod.browse)
3044 {
3045 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3046 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3047 if (browse_file == NULL)
3048 return;
3049 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3050 vim_free(browse_file);
3051 }
3052 else
3053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003055 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003056
3057 /*
3058 * This function is used by the :cfile, :cgetfile and :caddfile
3059 * commands.
3060 * :cfile always creates a new quickfix list and jumps to the
3061 * first error.
3062 * :cgetfile creates a new quickfix list but doesn't jump to the
3063 * first error.
3064 * :caddfile adds to an existing quickfix list. If there is no
3065 * quickfix list then a new list is created.
3066 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003067 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003068 && eap->cmdidx != CMD_laddfile),
3069 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003070 && (eap->cmdidx == CMD_cfile
3071 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003072 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003073#ifdef FEAT_AUTOCMD
3074 if (au_name != NULL)
3075 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3076#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003077 if (wp != NULL)
3078 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003079 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003080 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003081
3082 else
3083 {
3084#ifdef FEAT_AUTOCMD
3085 if (au_name != NULL)
3086 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3087#endif
3088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089}
3090
3091/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003092 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003093 * ":vimgrepadd {pattern} file(s)"
3094 * ":lvimgrep {pattern} file(s)"
3095 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003096 */
3097 void
3098ex_vimgrep(eap)
3099 exarg_T *eap;
3100{
Bram Moolenaar81695252004-12-29 20:58:21 +00003101 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003102 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003103 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003104 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003105 char_u *s;
3106 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003107 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003108 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003109 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003110 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003111 buf_T *buf;
3112 int duplicate_name = FALSE;
3113 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003114 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003115 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003116 buf_T *first_match_buf = NULL;
3117 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003118 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003119#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3120 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003121#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003122 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003123 int flags = 0;
3124 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003125 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003126 char_u *dirname_start = NULL;
3127 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003128 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003129#ifdef FEAT_AUTOCMD
3130 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003131
3132 switch (eap->cmdidx)
3133 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003134 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3135 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3136 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003137 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003138 case CMD_grep: au_name = (char_u *)"grep"; break;
3139 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3140 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3141 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003142 default: break;
3143 }
3144 if (au_name != NULL)
3145 {
3146 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3147 curbuf->b_fname, TRUE, curbuf);
3148 if (did_throw || force_abort)
3149 return;
3150 }
3151#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003152
Bram Moolenaar754b5602006-02-09 23:53:20 +00003153 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003154 || eap->cmdidx == CMD_lvimgrep
3155 || eap->cmdidx == CMD_lgrepadd
3156 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003157 {
3158 qi = ll_get_or_alloc_list(curwin);
3159 if (qi == NULL)
3160 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003161 }
3162
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003163 if (eap->addr_count > 0)
3164 tomatch = eap->line2;
3165 else
3166 tomatch = MAXLNUM;
3167
Bram Moolenaar81695252004-12-29 20:58:21 +00003168 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003169 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003170 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003171 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003172 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003173 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003174 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003175 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003176 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003177 if (regmatch.regprog == NULL)
3178 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003179 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003180 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003181
3182 p = skipwhite(p);
3183 if (*p == NUL)
3184 {
3185 EMSG(_("E683: File name missing or invalid pattern"));
3186 goto theend;
3187 }
3188
Bram Moolenaar754b5602006-02-09 23:53:20 +00003189 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003190 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003191 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003192 /* make place for a new list */
Bram Moolenaar41b884b2012-11-14 22:38:08 +01003193 qf_new_list(qi, *eap->cmdlinep, curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003194 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003195 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003196 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003197 prevp->qf_next != prevp; prevp = prevp->qf_next)
3198 ;
3199
3200 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003201 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003202 goto theend;
3203 if (fcount == 0)
3204 {
3205 EMSG(_(e_nomatch));
3206 goto theend;
3207 }
3208
Bram Moolenaard9462e32011-04-11 21:35:11 +02003209 dirname_start = alloc(MAXPATHL);
3210 dirname_now = alloc(MAXPATHL);
3211 if (dirname_start == NULL || dirname_now == NULL)
3212 goto theend;
3213
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003214 /* Remember the current directory, because a BufRead autocommand that does
3215 * ":lcd %:p:h" changes the meaning of short path names. */
3216 mch_dirname(dirname_start, MAXPATHL);
3217
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003218 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003219 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003220 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003221 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003222 if (time(NULL) > seconds)
3223 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003224 /* Display the file name every second or so, show the user we are
3225 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003226 seconds = time(NULL);
3227 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003228 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003229 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003230 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003231 else
3232 {
3233 msg_outtrans(p);
3234 vim_free(p);
3235 }
3236 msg_clr_eos();
3237 msg_didout = FALSE; /* overwrite this message */
3238 msg_nowait = TRUE; /* don't wait for this message */
3239 msg_col = 0;
3240 out_flush();
3241 }
3242
Bram Moolenaar81695252004-12-29 20:58:21 +00003243 buf = buflist_findname_exp(fnames[fi]);
3244 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3245 {
3246 /* Remember that a buffer with this name already exists. */
3247 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003248 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003249 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003250
3251#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3252 /* Don't do Filetype autocommands to avoid loading syntax and
3253 * indent scripts, a great speed improvement. */
3254 save_ei = au_event_disable(",Filetype");
3255#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003256 /* Don't use modelines here, it's useless. */
3257 save_mls = p_mls;
3258 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003259
3260 /* Load file into a buffer, so that 'fileencoding' is detected,
3261 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003262 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003263
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003264 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003265#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3266 au_event_restore(save_ei);
3267#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003268 }
3269 else
3270 /* Use existing, loaded buffer. */
3271 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003272
Bram Moolenaar81695252004-12-29 20:58:21 +00003273 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003274 {
3275 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003276 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003277 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003278 else
3279 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003280 /* Try for a match in all lines of the buffer.
3281 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003282 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003283 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3284 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003285 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003286 col = 0;
3287 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003288 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003289 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003290 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003291 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003292 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003293 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003294 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003295 ml_get_buf(buf,
3296 regmatch.startpos[0].lnum + lnum, FALSE),
3297 regmatch.startpos[0].lnum + lnum,
3298 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003299 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003300 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003301 0, /* nr */
3302 0, /* type */
3303 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003304 ) == FAIL)
3305 {
3306 got_int = TRUE;
3307 break;
3308 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003309 found_match = TRUE;
3310 if (--tomatch == 0)
3311 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003312 if ((flags & VGR_GLOBAL) == 0
3313 || regmatch.endpos[0].lnum > 0)
3314 break;
3315 col = regmatch.endpos[0].col
3316 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003317 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003318 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003319 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003320 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003321 if (got_int)
3322 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003323 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003324
3325 if (using_dummy)
3326 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003327 if (found_match && first_match_buf == NULL)
3328 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003329 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003330 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003331 /* Never keep a dummy buffer if there is another buffer
3332 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003333 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003334 buf = NULL;
3335 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003336 else if (!cmdmod.hide
3337 || buf->b_p_bh[0] == 'u' /* "unload" */
3338 || buf->b_p_bh[0] == 'w' /* "wipe" */
3339 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003340 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003341 /* When no match was found we don't need to remember the
3342 * buffer, wipe it out. If there was a match and it
3343 * wasn't the first one or we won't jump there: only
3344 * unload the buffer.
3345 * Ignore 'hidden' here, because it may lead to having too
3346 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003347 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003348 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003349 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003350 buf = NULL;
3351 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003352 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003353 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003354 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003355 buf = NULL;
3356 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003357 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003358
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003359 if (buf != NULL)
3360 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003361 /* If the buffer is still loaded we need to use the
3362 * directory we jumped to below. */
3363 if (buf == first_match_buf
3364 && target_dir == NULL
3365 && STRCMP(dirname_start, dirname_now) != 0)
3366 target_dir = vim_strsave(dirname_now);
3367
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003368 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003369 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003370 * need to be done (again). But not the window-local
3371 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003372 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003373#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003374 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3375 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003376#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003377 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003378 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003379 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003380 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003381 }
3382 }
3383
3384 FreeWild(fcount, fnames);
3385
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003386 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3387 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3388 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003389
3390#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003391 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003392#endif
3393
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003394#ifdef FEAT_AUTOCMD
3395 if (au_name != NULL)
3396 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3397 curbuf->b_fname, TRUE, curbuf);
3398#endif
3399
Bram Moolenaar86b68352004-12-27 21:59:20 +00003400 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003401 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003402 {
3403 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003404 {
3405 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003406 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003407 if (buf != curbuf)
3408 /* If we jumped to another buffer redrawing will already be
3409 * taken care of. */
3410 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003411
3412 /* Jump to the directory used after loading the buffer. */
3413 if (curbuf == first_match_buf && target_dir != NULL)
3414 {
3415 exarg_T ea;
3416
3417 ea.arg = target_dir;
3418 ea.cmdidx = CMD_lcd;
3419 ex_cd(&ea);
3420 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003421 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003422 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003423 else
3424 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003425
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003426 /* If we loaded a dummy buffer into the current window, the autocommands
3427 * may have messed up things, need to redraw and recompute folds. */
3428 if (redraw_for_dummy)
3429 {
3430#ifdef FEAT_FOLDING
3431 foldUpdateAll(curwin);
3432#else
3433 redraw_later(NOT_VALID);
3434#endif
3435 }
3436
Bram Moolenaar86b68352004-12-27 21:59:20 +00003437theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003438 vim_free(dirname_now);
3439 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003440 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003441 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003442}
3443
3444/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003445 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003446 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003447 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3448 * If "s" is not NULL terminate the pattern with a NUL.
3449 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003450 */
3451 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003452skip_vimgrep_pat(p, s, flags)
3453 char_u *p;
3454 char_u **s;
3455 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003456{
3457 int c;
3458
3459 if (vim_isIDc(*p))
3460 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003461 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003462 if (s != NULL)
3463 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003464 p = skiptowhite(p);
3465 if (s != NULL && *p != NUL)
3466 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003467 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003468 else
3469 {
3470 /* ":vimgrep /pattern/[g][j] fname" */
3471 if (s != NULL)
3472 *s = p + 1;
3473 c = *p;
3474 p = skip_regexp(p + 1, c, TRUE, NULL);
3475 if (*p != c)
3476 return NULL;
3477
3478 /* Truncate the pattern. */
3479 if (s != NULL)
3480 *p = NUL;
3481 ++p;
3482
3483 /* Find the flags */
3484 while (*p == 'g' || *p == 'j')
3485 {
3486 if (flags != NULL)
3487 {
3488 if (*p == 'g')
3489 *flags |= VGR_GLOBAL;
3490 else
3491 *flags |= VGR_NOJUMP;
3492 }
3493 ++p;
3494 }
3495 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003496 return p;
3497}
3498
3499/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003500 * Restore current working directory to "dirname_start" if they differ, taking
3501 * into account whether it is set locally or globally.
3502 */
3503 static void
3504restore_start_dir(dirname_start)
3505 char_u *dirname_start;
3506{
3507 char_u *dirname_now = alloc(MAXPATHL);
3508
3509 if (NULL != dirname_now)
3510 {
3511 mch_dirname(dirname_now, MAXPATHL);
3512 if (STRCMP(dirname_start, dirname_now) != 0)
3513 {
3514 /* If the directory has changed, change it back by building up an
3515 * appropriate ex command and executing it. */
3516 exarg_T ea;
3517
3518 ea.arg = dirname_start;
3519 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3520 ex_cd(&ea);
3521 }
3522 }
3523}
3524
3525/*
3526 * Load file "fname" into a dummy buffer and return the buffer pointer,
3527 * placing the directory resulting from the buffer load into the
3528 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3529 * prior to calling this function. Restores directory to "dirname_start" prior
3530 * to returning, if autocmds or the 'autochdir' option have changed it.
3531 *
3532 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3533 * or wipe_dummy_buffer() later!
3534 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003535 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003536 */
3537 static buf_T *
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003538load_dummy_buffer(fname, dirname_start, resulting_dir)
Bram Moolenaar81695252004-12-29 20:58:21 +00003539 char_u *fname;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003540 char_u *dirname_start; /* in: old directory */
3541 char_u *resulting_dir; /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003542{
3543 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003544 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003545 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003546 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003547
3548 /* Allocate a buffer without putting it in the buffer list. */
3549 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3550 if (newbuf == NULL)
3551 return NULL;
3552
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003553 /* Init the options. */
3554 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3555
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003556 /* need to open the memfile before putting the buffer in a window */
3557 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003558 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003559 /* set curwin/curbuf to buf and save a few things */
3560 aucmd_prepbuf(&aco, newbuf);
3561
3562 /* Need to set the filename for autocommands. */
3563 (void)setfname(curbuf, fname, NULL, FALSE);
3564
Bram Moolenaar81695252004-12-29 20:58:21 +00003565 /* Create swap file now to avoid the ATTENTION message. */
3566 check_need_swap(TRUE);
3567
3568 /* Remove the "dummy" flag, otherwise autocommands may not
3569 * work. */
3570 curbuf->b_flags &= ~BF_DUMMY;
3571
3572 if (readfile(fname, NULL,
3573 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3574 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003575 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003576 && !(curbuf->b_flags & BF_NEW))
3577 {
3578 failed = FALSE;
3579 if (curbuf != newbuf)
3580 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003581 /* Bloody autocommands changed the buffer! Can happen when
3582 * using netrw and editing a remote file. Use the current
3583 * buffer instead, delete the dummy one after restoring the
3584 * window stuff. */
3585 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003586 newbuf = curbuf;
3587 }
3588 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003589
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003590 /* restore curwin/curbuf and a few other things */
3591 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003592 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3593 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003594 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003595
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003596 /*
3597 * When autocommands/'autochdir' option changed directory: go back.
3598 * Let the caller know what the resulting dir was first, in case it is
3599 * important.
3600 */
3601 mch_dirname(resulting_dir, MAXPATHL);
3602 restore_start_dir(dirname_start);
3603
Bram Moolenaar81695252004-12-29 20:58:21 +00003604 if (!buf_valid(newbuf))
3605 return NULL;
3606 if (failed)
3607 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003608 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003609 return NULL;
3610 }
3611 return newbuf;
3612}
3613
3614/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003615 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3616 * directory to "dirname_start" prior to returning, if autocmds or the
3617 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003618 */
3619 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003620wipe_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003621 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003622 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003623{
3624 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003625 {
3626#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3627 cleanup_T cs;
3628
3629 /* Reset the error/interrupt/exception state here so that aborting()
3630 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3631 * work when got_int is set. */
3632 enter_cleanup(&cs);
3633#endif
3634
Bram Moolenaar81695252004-12-29 20:58:21 +00003635 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003636
3637#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3638 /* Restore the error/interrupt/exception state if not discarded by a
3639 * new aborting error, interrupt, or uncaught exception. */
3640 leave_cleanup(&cs);
3641#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003642 /* When autocommands/'autochdir' option changed directory: go back. */
3643 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003644 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003645}
3646
3647/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003648 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3649 * directory to "dirname_start" prior to returning, if autocmds or the
3650 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003651 */
3652 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003653unload_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003654 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003655 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003656{
3657 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003658 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003659 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003660
3661 /* When autocommands/'autochdir' option changed directory: go back. */
3662 restore_start_dir(dirname_start);
3663 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003664}
3665
Bram Moolenaar05159a02005-02-26 23:04:13 +00003666#if defined(FEAT_EVAL) || defined(PROTO)
3667/*
3668 * Add each quickfix error to list "list" as a dictionary.
3669 */
3670 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003671get_errorlist(wp, list)
3672 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003673 list_T *list;
3674{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003675 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003676 dict_T *dict;
3677 char_u buf[2];
3678 qfline_T *qfp;
3679 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003680 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003681
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003682 if (wp != NULL)
3683 {
3684 qi = GET_LOC_LIST(wp);
3685 if (qi == NULL)
3686 return FAIL;
3687 }
3688
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003689 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003690 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003691 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003692
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003693 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3694 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003695 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003696 /* Handle entries with a non-existing buffer number. */
3697 bufnum = qfp->qf_fnum;
3698 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3699 bufnum = 0;
3700
Bram Moolenaar05159a02005-02-26 23:04:13 +00003701 if ((dict = dict_alloc()) == NULL)
3702 return FAIL;
3703 if (list_append_dict(list, dict) == FAIL)
3704 return FAIL;
3705
3706 buf[0] = qfp->qf_type;
3707 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003708 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003709 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3710 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3711 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3712 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003713 || dict_add_nr_str(dict, "pattern", 0L,
3714 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3715 || dict_add_nr_str(dict, "text", 0L,
3716 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003717 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3718 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3719 return FAIL;
3720
3721 qfp = qfp->qf_next;
3722 }
3723 return OK;
3724}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003725
3726/*
3727 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003728 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003729 */
3730 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003731set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003732 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003733 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003734 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003735 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003736{
3737 listitem_T *li;
3738 dict_T *d;
3739 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003740 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003741 long lnum;
3742 int col, nr;
3743 int vcol;
3744 qfline_T *prevp = NULL;
3745 int valid, status;
3746 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003747 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003748 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003749
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003750 if (wp != NULL)
3751 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003752 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003753 if (qi == NULL)
3754 return FAIL;
3755 }
3756
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003757 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003758 /* make place for a new list */
Bram Moolenaar41b884b2012-11-14 22:38:08 +01003759 qf_new_list(qi, title, wp);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003760 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003761 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003762 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003763 prevp->qf_next != prevp; prevp = prevp->qf_next)
3764 ;
3765 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003766 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003767
3768 for (li = list->lv_first; li != NULL; li = li->li_next)
3769 {
3770 if (li->li_tv.v_type != VAR_DICT)
3771 continue; /* Skip non-dict items */
3772
3773 d = li->li_tv.vval.v_dict;
3774 if (d == NULL)
3775 continue;
3776
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003777 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003778 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003779 lnum = get_dict_number(d, (char_u *)"lnum");
3780 col = get_dict_number(d, (char_u *)"col");
3781 vcol = get_dict_number(d, (char_u *)"vcol");
3782 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003783 type = get_dict_string(d, (char_u *)"type", TRUE);
3784 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3785 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003786 if (text == NULL)
3787 text = vim_strsave((char_u *)"");
3788
3789 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003790 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003791 valid = FALSE;
3792
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003793 /* Mark entries with non-existing buffer number as not valid. Give the
3794 * error message only once. */
3795 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3796 {
3797 if (!did_bufnr_emsg)
3798 {
3799 did_bufnr_emsg = TRUE;
3800 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3801 }
3802 valid = FALSE;
3803 bufnum = 0;
3804 }
3805
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003806 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003807 NULL, /* dir */
3808 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003809 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003810 text,
3811 lnum,
3812 col,
3813 vcol, /* vis_col */
3814 pattern, /* search pattern */
3815 nr,
3816 type == NULL ? NUL : *type,
3817 valid);
3818
3819 vim_free(filename);
3820 vim_free(pattern);
3821 vim_free(text);
3822 vim_free(type);
3823
3824 if (status == FAIL)
3825 {
3826 retval = FAIL;
3827 break;
3828 }
3829 }
3830
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003831 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003832 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003833 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3834 else
3835 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003836 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3837 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003838
3839#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003840 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003841#endif
3842
3843 return retval;
3844}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003845#endif
3846
Bram Moolenaar81695252004-12-29 20:58:21 +00003847/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003848 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003849 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003850 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003852 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003853 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003854 */
3855 void
3856ex_cbuffer(eap)
3857 exarg_T *eap;
3858{
3859 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860 qf_info_T *qi = &ql_info;
3861
Bram Moolenaardb552d602006-03-23 22:59:57 +00003862 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3863 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003864 {
3865 qi = ll_get_or_alloc_list(curwin);
3866 if (qi == NULL)
3867 return;
3868 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003869
3870 if (*eap->arg == NUL)
3871 buf = curbuf;
3872 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3873 buf = buflist_findnr(atoi((char *)eap->arg));
3874 if (buf == NULL)
3875 EMSG(_(e_invarg));
3876 else if (buf->b_ml.ml_mfp == NULL)
3877 EMSG(_("E681: Buffer is not loaded"));
3878 else
3879 {
3880 if (eap->addr_count == 0)
3881 {
3882 eap->line1 = 1;
3883 eap->line2 = buf->b_ml.ml_line_count;
3884 }
3885 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3886 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3887 EMSG(_(e_invrange));
3888 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003889 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003890 char_u *qf_title = *eap->cmdlinep;
3891
3892 if (buf->b_sfname)
3893 {
3894 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3895 (char *)qf_title, (char *)buf->b_sfname);
3896 qf_title = IObuff;
3897 }
3898
Bram Moolenaardb552d602006-03-23 22:59:57 +00003899 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3900 (eap->cmdidx != CMD_caddbuffer
3901 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003902 eap->line1, eap->line2,
3903 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003904 && (eap->cmdidx == CMD_cbuffer
3905 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003906 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3907 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003908 }
3909}
3910
Bram Moolenaar1e015462005-09-25 22:16:38 +00003911#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003912/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003913 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3914 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003915 */
3916 void
3917ex_cexpr(eap)
3918 exarg_T *eap;
3919{
3920 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003921 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003922
Bram Moolenaardb552d602006-03-23 22:59:57 +00003923 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3924 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003925 {
3926 qi = ll_get_or_alloc_list(curwin);
3927 if (qi == NULL)
3928 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003929 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003930
Bram Moolenaar4770d092006-01-12 23:22:24 +00003931 /* Evaluate the expression. When the result is a string or a list we can
3932 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003933 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003934 if (tv != NULL)
3935 {
3936 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3937 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3938 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003939 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3940 (eap->cmdidx != CMD_caddexpr
3941 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003942 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003943 && (eap->cmdidx == CMD_cexpr
3944 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003945 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003946 }
3947 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003948 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003949 free_tv(tv);
3950 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003951}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003952#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003953
3954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 * ":helpgrep {pattern}"
3956 */
3957 void
3958ex_helpgrep(eap)
3959 exarg_T *eap;
3960{
3961 regmatch_T regmatch;
3962 char_u *save_cpo;
3963 char_u *p;
3964 int fcount;
3965 char_u **fnames;
3966 FILE *fd;
3967 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003968 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003970#ifdef FEAT_MULTI_LANG
3971 char_u *lang;
3972#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003973 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003974 int new_qi = FALSE;
3975 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01003976#ifdef FEAT_AUTOCMD
3977 char_u *au_name = NULL;
3978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003980#ifdef FEAT_MULTI_LANG
3981 /* Check for a specified language */
3982 lang = check_help_lang(eap->arg);
3983#endif
3984
Bram Moolenaar73633f82012-01-20 13:39:07 +01003985#ifdef FEAT_AUTOCMD
3986 switch (eap->cmdidx)
3987 {
3988 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
3989 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
3990 default: break;
3991 }
3992 if (au_name != NULL)
3993 {
3994 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3995 curbuf->b_fname, TRUE, curbuf);
3996 if (did_throw || force_abort)
3997 return;
3998 }
3999#endif
4000
4001 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4002 save_cpo = p_cpo;
4003 p_cpo = empty_option;
4004
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004005 if (eap->cmdidx == CMD_lhelpgrep)
4006 {
4007 /* Find an existing help window */
4008 FOR_ALL_WINDOWS(wp)
4009 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4010 break;
4011
4012 if (wp == NULL) /* Help window not found */
4013 qi = NULL;
4014 else
4015 qi = wp->w_llist;
4016
4017 if (qi == NULL)
4018 {
4019 /* Allocate a new location list for help text matches */
4020 if ((qi = ll_new_list()) == NULL)
4021 return;
4022 new_qi = TRUE;
4023 }
4024 }
4025
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4027 regmatch.rm_ic = FALSE;
4028 if (regmatch.regprog != NULL)
4029 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004030#ifdef FEAT_MBYTE
4031 vimconv_T vc;
4032
4033 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4034 * differs. */
4035 vc.vc_type = CONV_NONE;
4036 if (!enc_utf8)
4037 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4038#endif
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 /* create a new quickfix list */
Bram Moolenaar41b884b2012-11-14 22:38:08 +01004041 qf_new_list(qi, *eap->cmdlinep, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
4043 /* Go through all directories in 'runtimepath' */
4044 p = p_rtp;
4045 while (*p != NUL && !got_int)
4046 {
4047 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4048
4049 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4050 add_pathsep(NameBuff);
4051 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4052 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4053 &fnames, EW_FILE|EW_SILENT) == OK
4054 && fcount > 0)
4055 {
4056 for (fi = 0; fi < fcount && !got_int; ++fi)
4057 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004058#ifdef FEAT_MULTI_LANG
4059 /* Skip files for a different language. */
4060 if (lang != NULL
4061 && STRNICMP(lang, fnames[fi]
4062 + STRLEN(fnames[fi]) - 3, 2) != 0
4063 && !(STRNICMP(lang, "en", 2) == 0
4064 && STRNICMP("txt", fnames[fi]
4065 + STRLEN(fnames[fi]) - 3, 3) == 0))
4066 continue;
4067#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004068 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (fd != NULL)
4070 {
4071 lnum = 1;
4072 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4073 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004074 char_u *line = IObuff;
4075#ifdef FEAT_MBYTE
4076 /* Convert a line if 'encoding' is not utf-8 and
4077 * the line contains a non-ASCII character. */
4078 if (vc.vc_type != CONV_NONE
4079 && has_non_ascii(IObuff)) {
4080 line = string_convert(&vc, IObuff, NULL);
4081 if (line == NULL)
4082 line = IObuff;
4083 }
4084#endif
4085
4086 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004088 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004091 while (l > 0 && line[l - 1] <= ' ')
4092 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004094 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 NULL, /* dir */
4096 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004097 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004098 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004100 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004101 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004102 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004103 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 0, /* nr */
4105 1, /* type */
4106 TRUE /* valid */
4107 ) == FAIL)
4108 {
4109 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004110#ifdef FEAT_MBYTE
4111 if (line != IObuff)
4112 vim_free(line);
4113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 break;
4115 }
4116 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004117#ifdef FEAT_MBYTE
4118 if (line != IObuff)
4119 vim_free(line);
4120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 ++lnum;
4122 line_breakcheck();
4123 }
4124 fclose(fd);
4125 }
4126 }
4127 FreeWild(fcount, fnames);
4128 }
4129 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 vim_free(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004132#ifdef FEAT_MBYTE
4133 if (vc.vc_type != CONV_NONE)
4134 convert_setup(&vc, NULL, NULL);
4135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004137 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4138 qi->qf_lists[qi->qf_curlist].qf_ptr =
4139 qi->qf_lists[qi->qf_curlist].qf_start;
4140 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004143 if (p_cpo == empty_option)
4144 p_cpo = save_cpo;
4145 else
4146 /* Darn, some plugin changed the value. */
4147 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004150 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#endif
4152
Bram Moolenaar73633f82012-01-20 13:39:07 +01004153#ifdef FEAT_AUTOCMD
4154 if (au_name != NULL)
4155 {
4156 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4157 curbuf->b_fname, TRUE, curbuf);
4158 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4159 /* autocommands made "qi" invalid */
4160 return;
4161 }
4162#endif
4163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004165 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004166 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004167 else
4168 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004169
4170 if (eap->cmdidx == CMD_lhelpgrep)
4171 {
4172 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004173 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004174 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4175 {
4176 if (new_qi)
4177 ll_free_all(&qi);
4178 }
4179 else if (curwin->w_llist == NULL)
4180 curwin->w_llist = qi;
4181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182}
4183
4184#endif /* FEAT_QUICKFIX */