blob: 30a58458b44a4cbc918a433ca8609d9b5ff75263 [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)
901 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
903 /*
904 * When the stack is full, remove to oldest entry
905 * Otherwise, add a new entry.
906 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000907 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000909 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000911 qi->qf_lists[i - 1] = qi->qf_lists[i];
912 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000915 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100916 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200917 if (qf_title != NULL)
918 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200919 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200920
921 qi->qf_lists[qi->qf_curlist].qf_title = p;
922 if (p != NULL)
923 sprintf((char *)p, ":%s", (char *)qf_title);
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925}
926
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000927/*
928 * Free a location list
929 */
930 static void
931ll_free_all(pqi)
932 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000933{
934 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000935 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000936
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000937 qi = *pqi;
938 if (qi == NULL)
939 return;
940 *pqi = NULL; /* Remove reference to this list */
941
942 qi->qf_refcount--;
943 if (qi->qf_refcount < 1)
944 {
945 /* No references to this location list */
946 for (i = 0; i < qi->qf_listcount; ++i)
947 qf_free(qi, i);
948 vim_free(qi);
949 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000950}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000951
952 void
953qf_free_all(wp)
954 win_T *wp;
955{
956 int i;
957 qf_info_T *qi = &ql_info;
958
959 if (wp != NULL)
960 {
961 /* location list */
962 ll_free_all(&wp->w_llist);
963 ll_free_all(&wp->w_llist_ref);
964 }
965 else
966 /* quickfix list */
967 for (i = 0; i < qi->qf_listcount; ++i)
968 qf_free(qi, i);
969}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Add an entry to the end of the list of errors.
973 * Returns OK or FAIL.
974 */
975 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000976qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
977 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000978 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000979 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 char_u *dir; /* optional directory name */
981 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000982 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 char_u *mesg; /* message */
984 long lnum; /* line number */
985 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000986 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000987 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 int nr; /* error number */
989 int type; /* type character */
990 int valid; /* valid entry */
991{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000992 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000996 if (bufnum != 0)
997 qfp->qf_fnum = bufnum;
998 else
999 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1001 {
1002 vim_free(qfp);
1003 return FAIL;
1004 }
1005 qfp->qf_lnum = lnum;
1006 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001007 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001008 if (pattern == NULL || *pattern == NUL)
1009 qfp->qf_pattern = NULL;
1010 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1011 {
1012 vim_free(qfp->qf_text);
1013 vim_free(qfp);
1014 return FAIL;
1015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 qfp->qf_nr = nr;
1017 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1018 type = 0;
1019 qfp->qf_type = type;
1020 qfp->qf_valid = valid;
1021
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001022 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1023 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001025 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 qfp->qf_prev = qfp; /* first element points to itself */
1027 }
1028 else
1029 {
1030 qfp->qf_prev = *prevp;
1031 (*prevp)->qf_next = qfp;
1032 }
1033 qfp->qf_next = qfp; /* last element points to itself */
1034 qfp->qf_cleared = FALSE;
1035 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001036 ++qi->qf_lists[qi->qf_curlist].qf_count;
1037 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1038 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001040 qi->qf_lists[qi->qf_curlist].qf_index =
1041 qi->qf_lists[qi->qf_curlist].qf_count;
1042 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
1044
1045 return OK;
1046}
1047
1048/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001049 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001050 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001051 static qf_info_T *
1052ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001053{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001054 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001055
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001056 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1057 if (qi != NULL)
1058 {
1059 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1060 qi->qf_refcount++;
1061 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001062
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001063 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001064}
1065
1066/*
1067 * Return the location list for window 'wp'.
1068 * If not present, allocate a location list
1069 */
1070 static qf_info_T *
1071ll_get_or_alloc_list(wp)
1072 win_T *wp;
1073{
1074 if (IS_LL_WINDOW(wp))
1075 /* For a location list window, use the referenced location list */
1076 return wp->w_llist_ref;
1077
1078 /*
1079 * For a non-location list window, w_llist_ref should not point to a
1080 * location list.
1081 */
1082 ll_free_all(&wp->w_llist_ref);
1083
1084 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001085 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001086 return wp->w_llist;
1087}
1088
1089/*
1090 * Copy the location list from window "from" to window "to".
1091 */
1092 void
1093copy_loclist(from, to)
1094 win_T *from;
1095 win_T *to;
1096{
1097 qf_info_T *qi;
1098 int idx;
1099 int i;
1100
1101 /*
1102 * When copying from a location list window, copy the referenced
1103 * location list. For other windows, copy the location list for
1104 * that window.
1105 */
1106 if (IS_LL_WINDOW(from))
1107 qi = from->w_llist_ref;
1108 else
1109 qi = from->w_llist;
1110
1111 if (qi == NULL) /* no location list to copy */
1112 return;
1113
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001114 /* allocate a new location list */
1115 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001116 return;
1117
1118 to->w_llist->qf_listcount = qi->qf_listcount;
1119
1120 /* Copy the location lists one at a time */
1121 for (idx = 0; idx < qi->qf_listcount; idx++)
1122 {
1123 qf_list_T *from_qfl;
1124 qf_list_T *to_qfl;
1125
1126 to->w_llist->qf_curlist = idx;
1127
1128 from_qfl = &qi->qf_lists[idx];
1129 to_qfl = &to->w_llist->qf_lists[idx];
1130
1131 /* Some of the fields are populated by qf_add_entry() */
1132 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1133 to_qfl->qf_count = 0;
1134 to_qfl->qf_index = 0;
1135 to_qfl->qf_start = NULL;
1136 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001137 if (from_qfl->qf_title != NULL)
1138 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1139 else
1140 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001141
1142 if (from_qfl->qf_count)
1143 {
1144 qfline_T *from_qfp;
1145 qfline_T *prevp = NULL;
1146
1147 /* copy all the location entries in this list */
1148 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1149 ++i, from_qfp = from_qfp->qf_next)
1150 {
1151 if (qf_add_entry(to->w_llist, &prevp,
1152 NULL,
1153 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001154 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001155 from_qfp->qf_text,
1156 from_qfp->qf_lnum,
1157 from_qfp->qf_col,
1158 from_qfp->qf_viscol,
1159 from_qfp->qf_pattern,
1160 from_qfp->qf_nr,
1161 0,
1162 from_qfp->qf_valid) == FAIL)
1163 {
1164 qf_free_all(to);
1165 return;
1166 }
1167 /*
1168 * qf_add_entry() will not set the qf_num field, as the
1169 * directory and file names are not supplied. So the qf_fnum
1170 * field is copied here.
1171 */
1172 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1173 prevp->qf_type = from_qfp->qf_type; /* error type */
1174 if (from_qfl->qf_ptr == from_qfp)
1175 to_qfl->qf_ptr = prevp; /* current location */
1176 }
1177 }
1178
1179 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1180
1181 /* When no valid entries are present in the list, qf_ptr points to
1182 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001183 if (to_qfl->qf_nonevalid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001184 to_qfl->qf_ptr = to_qfl->qf_start;
1185 }
1186
1187 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1188}
1189
1190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 * get buffer number for file "dir.name"
1192 */
1193 static int
1194qf_get_fnum(directory, fname)
1195 char_u *directory;
1196 char_u *fname;
1197{
1198 if (fname == NULL || *fname == NUL) /* no file name */
1199 return 0;
1200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 char_u *ptr;
1202 int fnum;
1203
Bram Moolenaare60acc12011-05-10 16:41:25 +02001204#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001206#endif
1207#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 if (directory != NULL)
1209 slash_adjust(directory);
1210 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (directory != NULL && !vim_isAbsName(fname)
1213 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1214 {
1215 /*
1216 * Here we check if the file really exists.
1217 * This should normally be true, but if make works without
1218 * "leaving directory"-messages we might have missed a
1219 * directory change.
1220 */
1221 if (mch_getperm(ptr) < 0)
1222 {
1223 vim_free(ptr);
1224 directory = qf_guess_filepath(fname);
1225 if (directory)
1226 ptr = concat_fnames(directory, fname, TRUE);
1227 else
1228 ptr = vim_strsave(fname);
1229 }
1230 /* Use concatenated directory name and file name */
1231 fnum = buflist_add(ptr, 0);
1232 vim_free(ptr);
1233 return fnum;
1234 }
1235 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
1237}
1238
1239/*
1240 * push dirbuf onto the directory stack and return pointer to actual dir or
1241 * NULL on error
1242 */
1243 static char_u *
1244qf_push_dir(dirbuf, stackptr)
1245 char_u *dirbuf;
1246 struct dir_stack_T **stackptr;
1247{
1248 struct dir_stack_T *ds_new;
1249 struct dir_stack_T *ds_ptr;
1250
1251 /* allocate new stack element and hook it in */
1252 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1253 if (ds_new == NULL)
1254 return NULL;
1255
1256 ds_new->next = *stackptr;
1257 *stackptr = ds_new;
1258
1259 /* store directory on the stack */
1260 if (vim_isAbsName(dirbuf)
1261 || (*stackptr)->next == NULL
1262 || (*stackptr && dir_stack != *stackptr))
1263 (*stackptr)->dirname = vim_strsave(dirbuf);
1264 else
1265 {
1266 /* Okay we don't have an absolute path.
1267 * dirbuf must be a subdir of one of the directories on the stack.
1268 * Let's search...
1269 */
1270 ds_new = (*stackptr)->next;
1271 (*stackptr)->dirname = NULL;
1272 while (ds_new)
1273 {
1274 vim_free((*stackptr)->dirname);
1275 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1276 TRUE);
1277 if (mch_isdir((*stackptr)->dirname) == TRUE)
1278 break;
1279
1280 ds_new = ds_new->next;
1281 }
1282
1283 /* clean up all dirs we already left */
1284 while ((*stackptr)->next != ds_new)
1285 {
1286 ds_ptr = (*stackptr)->next;
1287 (*stackptr)->next = (*stackptr)->next->next;
1288 vim_free(ds_ptr->dirname);
1289 vim_free(ds_ptr);
1290 }
1291
1292 /* Nothing found -> it must be on top level */
1293 if (ds_new == NULL)
1294 {
1295 vim_free((*stackptr)->dirname);
1296 (*stackptr)->dirname = vim_strsave(dirbuf);
1297 }
1298 }
1299
1300 if ((*stackptr)->dirname != NULL)
1301 return (*stackptr)->dirname;
1302 else
1303 {
1304 ds_ptr = *stackptr;
1305 *stackptr = (*stackptr)->next;
1306 vim_free(ds_ptr);
1307 return NULL;
1308 }
1309}
1310
1311
1312/*
1313 * pop dirbuf from the directory stack and return previous directory or NULL if
1314 * stack is empty
1315 */
1316 static char_u *
1317qf_pop_dir(stackptr)
1318 struct dir_stack_T **stackptr;
1319{
1320 struct dir_stack_T *ds_ptr;
1321
1322 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1323 * What to do if it isn't? */
1324
1325 /* pop top element and free it */
1326 if (*stackptr != NULL)
1327 {
1328 ds_ptr = *stackptr;
1329 *stackptr = (*stackptr)->next;
1330 vim_free(ds_ptr->dirname);
1331 vim_free(ds_ptr);
1332 }
1333
1334 /* return NEW top element as current dir or NULL if stack is empty*/
1335 return *stackptr ? (*stackptr)->dirname : NULL;
1336}
1337
1338/*
1339 * clean up directory stack
1340 */
1341 static void
1342qf_clean_dir_stack(stackptr)
1343 struct dir_stack_T **stackptr;
1344{
1345 struct dir_stack_T *ds_ptr;
1346
1347 while ((ds_ptr = *stackptr) != NULL)
1348 {
1349 *stackptr = (*stackptr)->next;
1350 vim_free(ds_ptr->dirname);
1351 vim_free(ds_ptr);
1352 }
1353}
1354
1355/*
1356 * Check in which directory of the directory stack the given file can be
1357 * found.
1358 * Returns a pointer to the directory name or NULL if not found
1359 * Cleans up intermediate directory entries.
1360 *
1361 * TODO: How to solve the following problem?
1362 * If we have the this directory tree:
1363 * ./
1364 * ./aa
1365 * ./aa/bb
1366 * ./bb
1367 * ./bb/x.c
1368 * and make says:
1369 * making all in aa
1370 * making all in bb
1371 * x.c:9: Error
1372 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1373 * qf_guess_filepath will return NULL.
1374 */
1375 static char_u *
1376qf_guess_filepath(filename)
1377 char_u *filename;
1378{
1379 struct dir_stack_T *ds_ptr;
1380 struct dir_stack_T *ds_tmp;
1381 char_u *fullname;
1382
1383 /* no dirs on the stack - there's nothing we can do */
1384 if (dir_stack == NULL)
1385 return NULL;
1386
1387 ds_ptr = dir_stack->next;
1388 fullname = NULL;
1389 while (ds_ptr)
1390 {
1391 vim_free(fullname);
1392 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1393
1394 /* If concat_fnames failed, just go on. The worst thing that can happen
1395 * is that we delete the entire stack.
1396 */
1397 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1398 break;
1399
1400 ds_ptr = ds_ptr->next;
1401 }
1402
1403 vim_free(fullname);
1404
1405 /* clean up all dirs we already left */
1406 while (dir_stack->next != ds_ptr)
1407 {
1408 ds_tmp = dir_stack->next;
1409 dir_stack->next = dir_stack->next->next;
1410 vim_free(ds_tmp->dirname);
1411 vim_free(ds_tmp);
1412 }
1413
1414 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1415
1416}
1417
1418/*
1419 * jump to a quickfix line
1420 * if dir == FORWARD go "errornr" valid entries forward
1421 * if dir == BACKWARD go "errornr" valid entries backward
1422 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1423 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1424 * else if "errornr" is zero, redisplay the same line
1425 * else go to entry "errornr"
1426 */
1427 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001428qf_jump(qi, dir, errornr, forceit)
1429 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 int dir;
1431 int errornr;
1432 int forceit;
1433{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001434 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001435 qfline_T *qf_ptr;
1436 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 int qf_index;
1438 int old_qf_fnum;
1439 int old_qf_index;
1440 int prev_index;
1441 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1442 char_u *err = e_no_more_items;
1443 linenr_T i;
1444 buf_T *old_curbuf;
1445 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 colnr_T screen_col;
1447 colnr_T char_col;
1448 char_u *line;
1449#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001450 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001451 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 int opened_window = FALSE;
1453 win_T *win;
1454 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001455 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001457 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 int print_message = TRUE;
1459 int len;
1460#ifdef FEAT_FOLDING
1461 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1462#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001463 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001464 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001466 if (qi == NULL)
1467 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001468
1469 if (qi->qf_curlist >= qi->qf_listcount
1470 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
1472 EMSG(_(e_quickfix));
1473 return;
1474 }
1475
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001476 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001478 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 old_qf_index = qf_index;
1480 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1481 {
1482 while (errornr--)
1483 {
1484 old_qf_ptr = qf_ptr;
1485 prev_index = qf_index;
1486 old_qf_fnum = qf_ptr->qf_fnum;
1487 do
1488 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001489 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 || qf_ptr->qf_next == NULL)
1491 {
1492 qf_ptr = old_qf_ptr;
1493 qf_index = prev_index;
1494 if (err != NULL)
1495 {
1496 EMSG(_(err));
1497 goto theend;
1498 }
1499 errornr = 0;
1500 break;
1501 }
1502 ++qf_index;
1503 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001504 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1505 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1507 err = NULL;
1508 }
1509 }
1510 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1511 {
1512 while (errornr--)
1513 {
1514 old_qf_ptr = qf_ptr;
1515 prev_index = qf_index;
1516 old_qf_fnum = qf_ptr->qf_fnum;
1517 do
1518 {
1519 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1520 {
1521 qf_ptr = old_qf_ptr;
1522 qf_index = prev_index;
1523 if (err != NULL)
1524 {
1525 EMSG(_(err));
1526 goto theend;
1527 }
1528 errornr = 0;
1529 break;
1530 }
1531 --qf_index;
1532 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001533 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1534 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1536 err = NULL;
1537 }
1538 }
1539 else if (errornr != 0) /* go to specified number */
1540 {
1541 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1542 {
1543 --qf_index;
1544 qf_ptr = qf_ptr->qf_prev;
1545 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546 while (errornr > qf_index && qf_index <
1547 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 && qf_ptr->qf_next != NULL)
1549 {
1550 ++qf_index;
1551 qf_ptr = qf_ptr->qf_next;
1552 }
1553 }
1554
1555#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001556 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1557 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 /* No need to print the error message if it's visible in the error
1559 * window */
1560 print_message = FALSE;
1561
1562 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001563 * For ":helpgrep" find a help window or open one.
1564 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001565 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001566 {
1567 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001568
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001569 if (cmdmod.tab != 0)
1570 wp = NULL;
1571 else
1572 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1573 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1574 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001575 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1576 win_enter(wp, TRUE);
1577 else
1578 {
1579 /*
1580 * Split off help window; put it at far top if no position
1581 * specified, the current window is vertically split and narrow.
1582 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001583 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001584# ifdef FEAT_VERTSPLIT
1585 if (cmdmod.split == 0 && curwin->w_width != Columns
1586 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001587 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001588# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001589 if (qi != &ql_info)
1590 flags |= WSP_NEWLOC; /* don't copy the location list */
1591
1592 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001593 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001594 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001595
1596 if (curwin->w_height < p_hh)
1597 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001598
1599 if (qi != &ql_info) /* not a quickfix list */
1600 {
1601 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001602 curwin->w_llist = qi;
1603 qi->qf_refcount++;
1604 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001605 }
1606
1607 if (!p_im)
1608 restart_edit = 0; /* don't want insert mode in help file */
1609 }
1610
1611 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 * If currently in the quickfix window, find another window to show the
1613 * file in.
1614 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001615 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 /*
1618 * If there is no file specified, we don't know where to go.
1619 * But do advance, otherwise ":cn" gets stuck.
1620 */
1621 if (qf_ptr->qf_fnum == 0)
1622 goto theend;
1623
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001624 /* Locate a window showing a normal buffer */
1625 usable_win = 0;
1626 FOR_ALL_WINDOWS(win)
1627 if (win->w_buffer->b_p_bt[0] == NUL)
1628 {
1629 usable_win = 1;
1630 break;
1631 }
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001634 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001635 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001637 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001638 {
1639 tabpage_T *tp;
1640 win_T *wp;
1641
1642 FOR_ALL_TAB_WINDOWS(tp, wp)
1643 {
1644 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1645 {
1646 goto_tabpage_win(tp, wp);
1647 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001648 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001649 }
1650 }
1651 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001652win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001653
1654 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001655 * If there is only one window and it is the quickfix window, create a
1656 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001657 */
1658 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001660 ll_ref = curwin->w_llist_ref;
1661
Bram Moolenaar884ae642009-02-22 01:37:59 +00001662 flags = WSP_ABOVE;
1663 if (ll_ref != NULL)
1664 flags |= WSP_NEWLOC;
1665 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 goto failed; /* not enough room for window */
1667 opened_window = TRUE; /* close it when fail */
1668 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001669 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001670 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001671 if (ll_ref != NULL)
1672 {
1673 /* The new window should use the location list from the
1674 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001675 curwin->w_llist = ll_ref;
1676 ll_ref->qf_refcount++;
1677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679 else
1680 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001681 if (curwin->w_llist_ref != NULL)
1682 {
1683 /* In a location window */
1684 ll_ref = curwin->w_llist_ref;
1685
1686 /* Find the window with the same location list */
1687 FOR_ALL_WINDOWS(win)
1688 if (win->w_llist == ll_ref)
1689 break;
1690 if (win == NULL)
1691 {
1692 /* Find the window showing the selected file */
1693 FOR_ALL_WINDOWS(win)
1694 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1695 break;
1696 if (win == NULL)
1697 {
1698 /* Find a previous usable window */
1699 win = curwin;
1700 do
1701 {
1702 if (win->w_buffer->b_p_bt[0] == NUL)
1703 break;
1704 if (win->w_prev == NULL)
1705 win = lastwin; /* wrap around the top */
1706 else
1707 win = win->w_prev; /* go to previous window */
1708 } while (win != curwin);
1709 }
1710 }
1711 win_goto(win);
1712
1713 /* If the location list for the window is not set, then set it
1714 * to the location list from the location window */
1715 if (win->w_llist == NULL)
1716 {
1717 win->w_llist = ll_ref;
1718 ll_ref->qf_refcount++;
1719 }
1720 }
1721 else
1722 {
1723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 /*
1725 * Try to find a window that shows the right buffer.
1726 * Default to the window just above the quickfix buffer.
1727 */
1728 win = curwin;
1729 altwin = NULL;
1730 for (;;)
1731 {
1732 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1733 break;
1734 if (win->w_prev == NULL)
1735 win = lastwin; /* wrap around the top */
1736 else
1737 win = win->w_prev; /* go to previous window */
1738
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001739 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 {
1741 /* Didn't find it, go to the window before the quickfix
1742 * window. */
1743 if (altwin != NULL)
1744 win = altwin;
1745 else if (curwin->w_prev != NULL)
1746 win = curwin->w_prev;
1747 else
1748 win = curwin->w_next;
1749 break;
1750 }
1751
1752 /* Remember a usable window. */
1753 if (altwin == NULL && !win->w_p_pvw
1754 && win->w_buffer->b_p_bt[0] == NUL)
1755 altwin = win;
1756 }
1757
1758 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 }
1762#endif
1763
1764 /*
1765 * If there is a file name,
1766 * read the wanted file if needed, and check autowrite etc.
1767 */
1768 old_curbuf = curbuf;
1769 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001770
1771 if (qf_ptr->qf_fnum != 0)
1772 {
1773 if (qf_ptr->qf_type == 1)
1774 {
1775 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1776 * set b_p_ro flag). */
1777 if (!can_abandon(curbuf, forceit))
1778 {
1779 EMSG(_(e_nowrtmsg));
1780 ok = FALSE;
1781 }
1782 else
1783 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001784 ECMD_HIDE + ECMD_SET_HELP,
1785 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001786 }
1787 else
1788 ok = buflist_getfile(qf_ptr->qf_fnum,
1789 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1790 }
1791
1792 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
1794 /* When not switched to another buffer, still need to set pc mark */
1795 if (curbuf == old_curbuf)
1796 setpcmark();
1797
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001798 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001800 /*
1801 * Go to line with error, unless qf_lnum is 0.
1802 */
1803 i = qf_ptr->qf_lnum;
1804 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001806 if (i > curbuf->b_ml.ml_line_count)
1807 i = curbuf->b_ml.ml_line_count;
1808 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001810 if (qf_ptr->qf_col > 0)
1811 {
1812 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1813 if (qf_ptr->qf_viscol == TRUE)
1814 {
1815 /*
1816 * Check each character from the beginning of the error
1817 * line up to the error column. For each tab character
1818 * found, reduce the error column value by the length of
1819 * a tab character.
1820 */
1821 line = ml_get_curline();
1822 screen_col = 0;
1823 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1824 {
1825 if (*line == NUL)
1826 break;
1827 if (*line++ == '\t')
1828 {
1829 curwin->w_cursor.col -= 7 - (screen_col % 8);
1830 screen_col += 8 - (screen_col % 8);
1831 }
1832 else
1833 ++screen_col;
1834 }
1835 }
1836 check_cursor();
1837 }
1838 else
1839 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
1841 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001842 {
1843 pos_T save_cursor;
1844
1845 /* Move the cursor to the first line in the buffer */
1846 save_cursor = curwin->w_cursor;
1847 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001848 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1849 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001850 curwin->w_cursor = save_cursor;
1851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853#ifdef FEAT_FOLDING
1854 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1855 foldOpenCursor();
1856#endif
1857 if (print_message)
1858 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001859 /* Update the screen before showing the message, unless the screen
1860 * scrolled up. */
1861 if (!msg_scrolled)
1862 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1866 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1867 /* Add the message, skipping leading whitespace and newlines. */
1868 len = (int)STRLEN(IObuff);
1869 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1870
1871 /* Output the message. Overwrite to avoid scrolling when the 'O'
1872 * flag is present in 'shortmess'; But when not jumping, print the
1873 * whole message. */
1874 i = msg_scroll;
1875 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1876 msg_scroll = TRUE;
1877 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1878 msg_scroll = FALSE;
1879 msg_attr_keep(IObuff, 0, TRUE);
1880 msg_scroll = i;
1881 }
1882 }
1883 else
1884 {
1885#ifdef FEAT_WINDOWS
1886 if (opened_window)
1887 win_close(curwin, TRUE); /* Close opened window */
1888#endif
1889 if (qf_ptr->qf_fnum != 0)
1890 {
1891 /*
1892 * Couldn't open file, so put index back where it was. This could
1893 * happen if the file was readonly and we changed something.
1894 */
1895#ifdef FEAT_WINDOWS
1896failed:
1897#endif
1898 qf_ptr = old_qf_ptr;
1899 qf_index = old_qf_index;
1900 }
1901 }
1902theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001903 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1904 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#ifdef FEAT_WINDOWS
1906 if (p_swb != old_swb && opened_window)
1907 {
1908 /* Restore old 'switchbuf' value, but not when an autocommand or
1909 * modeline has changed the value. */
1910 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001913 swb_flags = old_swb_flags;
1914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 else
1916 free_string_option(old_swb);
1917 }
1918#endif
1919}
1920
1921/*
1922 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001923 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
1925 void
1926qf_list(eap)
1927 exarg_T *eap;
1928{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001929 buf_T *buf;
1930 char_u *fname;
1931 qfline_T *qfp;
1932 int i;
1933 int idx1 = 1;
1934 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001935 char_u *arg = eap->arg;
1936 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001938 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001940 if (eap->cmdidx == CMD_llist)
1941 {
1942 qi = GET_LOC_LIST(curwin);
1943 if (qi == NULL)
1944 {
1945 EMSG(_(e_loclist));
1946 return;
1947 }
1948 }
1949
1950 if (qi->qf_curlist >= qi->qf_listcount
1951 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 EMSG(_(e_quickfix));
1954 return;
1955 }
1956 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1957 {
1958 EMSG(_(e_trailing));
1959 return;
1960 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001961 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (idx1 < 0)
1963 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1964 if (idx2 < 0)
1965 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1966
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001967 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1970 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1973 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001974 msg_putchar('\n');
1975 if (got_int)
1976 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001977
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001978 fname = NULL;
1979 if (qfp->qf_fnum != 0
1980 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1981 {
1982 fname = buf->b_fname;
1983 if (qfp->qf_type == 1) /* :helpgrep */
1984 fname = gettail(fname);
1985 }
1986 if (fname == NULL)
1987 sprintf((char *)IObuff, "%2d", i);
1988 else
1989 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1990 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001991 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1993 if (qfp->qf_lnum == 0)
1994 IObuff[0] = NUL;
1995 else if (qfp->qf_col == 0)
1996 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1997 else
1998 sprintf((char *)IObuff, ":%ld col %d",
1999 qfp->qf_lnum, qfp->qf_col);
2000 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2001 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2002 msg_puts_attr(IObuff, hl_attr(HLF_N));
2003 if (qfp->qf_pattern != NULL)
2004 {
2005 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2006 STRCAT(IObuff, ":");
2007 msg_puts(IObuff);
2008 }
2009 msg_puts((char_u *)" ");
2010
2011 /* Remove newlines and leading whitespace from the text. For an
2012 * unrecognized line keep the indent, the compiler may mark a word
2013 * with ^^^^. */
2014 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2016 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002017 msg_prt_line(IObuff, FALSE);
2018 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002020
2021 qfp = qfp->qf_next;
2022 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 ui_breakcheck();
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025}
2026
2027/*
2028 * Remove newlines and leading whitespace from an error message.
2029 * Put the result in "buf[bufsize]".
2030 */
2031 static void
2032qf_fmt_text(text, buf, bufsize)
2033 char_u *text;
2034 char_u *buf;
2035 int bufsize;
2036{
2037 int i;
2038 char_u *p = text;
2039
2040 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2041 {
2042 if (*p == '\n')
2043 {
2044 buf[i] = ' ';
2045 while (*++p != NUL)
2046 if (!vim_iswhite(*p) && *p != '\n')
2047 break;
2048 }
2049 else
2050 buf[i] = *p++;
2051 }
2052 buf[i] = NUL;
2053}
2054
2055/*
2056 * ":colder [count]": Up in the quickfix stack.
2057 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058 * ":lolder [count]": Up in the location list stack.
2059 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 */
2061 void
2062qf_age(eap)
2063 exarg_T *eap;
2064{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002065 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 int count;
2067
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002068 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2069 {
2070 qi = GET_LOC_LIST(curwin);
2071 if (qi == NULL)
2072 {
2073 EMSG(_(e_loclist));
2074 return;
2075 }
2076 }
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (eap->addr_count != 0)
2079 count = eap->line2;
2080 else
2081 count = 1;
2082 while (count--)
2083 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002084 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002086 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
2088 EMSG(_("E380: At bottom of quickfix stack"));
2089 return;
2090 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002091 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 else
2094 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002095 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 EMSG(_("E381: At top of quickfix stack"));
2098 return;
2099 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002100 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002103 qf_msg(qi);
2104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105}
2106
2107 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002108qf_msg(qi)
2109 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002112 qi->qf_curlist + 1, qi->qf_listcount,
2113 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#endif
2117}
2118
2119/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002120 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 */
2122 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002123qf_free(qi, idx)
2124 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 int idx;
2126{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002127 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002129 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002132 if (qi->qf_lists[idx].qf_title != NULL)
2133 {
2134 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2135 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2136 vim_free(qi->qf_lists[idx].qf_start);
2137 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138 qi->qf_lists[idx].qf_start = qfp;
2139 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002141 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002142 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143}
2144
2145/*
2146 * qf_mark_adjust: adjust marks
2147 */
2148 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149qf_mark_adjust(wp, line1, line2, amount, amount_after)
2150 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 linenr_T line1;
2152 linenr_T line2;
2153 long amount;
2154 long amount_after;
2155{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002156 int i;
2157 qfline_T *qfp;
2158 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002159 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002161 if (wp != NULL)
2162 {
2163 if (wp->w_llist == NULL)
2164 return;
2165 qi = wp->w_llist;
2166 }
2167
2168 for (idx = 0; idx < qi->qf_listcount; ++idx)
2169 if (qi->qf_lists[idx].qf_count)
2170 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2171 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (qfp->qf_fnum == curbuf->b_fnum)
2173 {
2174 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2175 {
2176 if (amount == MAXLNUM)
2177 qfp->qf_cleared = TRUE;
2178 else
2179 qfp->qf_lnum += amount;
2180 }
2181 else if (amount_after && qfp->qf_lnum > line2)
2182 qfp->qf_lnum += amount_after;
2183 }
2184}
2185
2186/*
2187 * Make a nice message out of the error character and the error number:
2188 * char number message
2189 * e or E 0 " error"
2190 * w or W 0 " warning"
2191 * i or I 0 " info"
2192 * 0 0 ""
2193 * other 0 " c"
2194 * e or E n " error n"
2195 * w or W n " warning n"
2196 * i or I n " info n"
2197 * 0 n " error n"
2198 * other n " c n"
2199 * 1 x "" :helpgrep
2200 */
2201 static char_u *
2202qf_types(c, nr)
2203 int c, nr;
2204{
2205 static char_u buf[20];
2206 static char_u cc[3];
2207 char_u *p;
2208
2209 if (c == 'W' || c == 'w')
2210 p = (char_u *)" warning";
2211 else if (c == 'I' || c == 'i')
2212 p = (char_u *)" info";
2213 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2214 p = (char_u *)" error";
2215 else if (c == 0 || c == 1)
2216 p = (char_u *)"";
2217 else
2218 {
2219 cc[0] = ' ';
2220 cc[1] = c;
2221 cc[2] = NUL;
2222 p = cc;
2223 }
2224
2225 if (nr <= 0)
2226 return p;
2227
2228 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2229 return buf;
2230}
2231
2232#if defined(FEAT_WINDOWS) || defined(PROTO)
2233/*
2234 * ":cwindow": open the quickfix window if we have errors to display,
2235 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236 * ":lwindow": open the location list window if we have locations to display,
2237 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
2239 void
2240ex_cwindow(eap)
2241 exarg_T *eap;
2242{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002243 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 win_T *win;
2245
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002246 if (eap->cmdidx == CMD_lwindow)
2247 {
2248 qi = GET_LOC_LIST(curwin);
2249 if (qi == NULL)
2250 return;
2251 }
2252
2253 /* Look for an existing quickfix window. */
2254 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256 /*
2257 * If a quickfix window is open but we have no errors to display,
2258 * close the window. If a quickfix window is not open, then open
2259 * it if we have errors; otherwise, leave it closed.
2260 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002261 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002262 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002263 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 if (win != NULL)
2266 ex_cclose(eap);
2267 }
2268 else if (win == NULL)
2269 ex_copen(eap);
2270}
2271
2272/*
2273 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002274 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 void
2277ex_cclose(eap)
2278 exarg_T *eap;
2279{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002280 win_T *win = NULL;
2281 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002283 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2284 {
2285 qi = GET_LOC_LIST(curwin);
2286 if (qi == NULL)
2287 return;
2288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002290 /* Find existing quickfix window and close it. */
2291 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (win != NULL)
2293 win_close(win, FALSE);
2294}
2295
2296/*
2297 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002298 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 */
2300 void
2301ex_copen(eap)
2302 exarg_T *eap;
2303{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002304 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002307 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002308 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002309 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002311 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2312 {
2313 qi = GET_LOC_LIST(curwin);
2314 if (qi == NULL)
2315 {
2316 EMSG(_(e_loclist));
2317 return;
2318 }
2319 }
2320
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (eap->addr_count != 0)
2322 height = eap->line2;
2323 else
2324 height = QF_WINHEIGHT;
2325
2326#ifdef FEAT_VISUAL
2327 reset_VIsual_and_resel(); /* stop Visual mode */
2328#endif
2329#ifdef FEAT_GUI
2330 need_mouse_correct = TRUE;
2331#endif
2332
2333 /*
2334 * Find existing quickfix window, or open a new one.
2335 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002336 win = qf_find_win(qi);
2337
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002338 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 win_goto(win);
2340 else
2341 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002342 qf_buf = qf_find_buf(qi);
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 /* The current window becomes the previous window afterwards. */
2345 win = curwin;
2346
Bram Moolenaar77642c02012-11-20 17:55:10 +01002347 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2348 && cmdmod.split == 0)
2349 /* Create the new window at the very bottom, except when
2350 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002351 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002352 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002354 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002356 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002358 /*
2359 * For the location list window, create a reference to the
2360 * location list from the window 'win'.
2361 */
2362 curwin->w_llist_ref = win->w_llist;
2363 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002365
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002366 if (oldwin != curwin)
2367 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002368 if (qf_buf != NULL)
2369 /* Use the existing quickfix buffer */
2370 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002371 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002372 else
2373 {
2374 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002375 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002376 /* switch off 'swapfile' */
2377 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2378 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002379 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002380 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002381 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002382#ifdef FEAT_DIFF
2383 curwin->w_p_diff = FALSE;
2384#endif
2385#ifdef FEAT_FOLDING
2386 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2387 OPT_LOCAL);
2388#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002391 /* Only set the height when still in the same tab page and there is no
2392 * window to the side. */
2393 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002394#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002395 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002396#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002397 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 win_setheight(height);
2399 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2400 if (win_valid(win))
2401 prevwin = win;
2402 }
2403
2404 /*
2405 * Fill the buffer with the quickfix list.
2406 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002407 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002409 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002410 qf_set_title(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002411
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002412 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 curwin->w_cursor.col = 0;
2414 check_cursor();
2415 update_topline(); /* scroll to show the line */
2416}
2417
2418/*
2419 * Return the number of the current entry (line number in the quickfix
2420 * window).
2421 */
2422 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002423qf_current_entry(wp)
2424 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426 qf_info_T *qi = &ql_info;
2427
2428 if (IS_LL_WINDOW(wp))
2429 /* In the location list window, use the referenced location list */
2430 qi = wp->w_llist_ref;
2431
2432 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433}
2434
2435/*
2436 * Update the cursor position in the quickfix window to the current error.
2437 * Return TRUE if there is a quickfix window.
2438 */
2439 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002440qf_win_pos_update(qi, old_qf_index)
2441 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 int old_qf_index; /* previous qf_index or zero */
2443{
2444 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002445 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
2447 /*
2448 * Put the cursor on the current error in the quickfix window, so that
2449 * it's viewable.
2450 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002451 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (win != NULL
2453 && qf_index <= win->w_buffer->b_ml.ml_line_count
2454 && old_qf_index != qf_index)
2455 {
2456 win_T *old_curwin = curwin;
2457
2458 curwin = win;
2459 curbuf = win->w_buffer;
2460 if (qf_index > old_qf_index)
2461 {
2462 curwin->w_redraw_top = old_qf_index;
2463 curwin->w_redraw_bot = qf_index;
2464 }
2465 else
2466 {
2467 curwin->w_redraw_top = qf_index;
2468 curwin->w_redraw_bot = old_qf_index;
2469 }
2470 curwin->w_cursor.lnum = qf_index;
2471 curwin->w_cursor.col = 0;
2472 update_topline(); /* scroll to show the line */
2473 redraw_later(VALID);
2474 curwin->w_redr_status = TRUE; /* update ruler */
2475 curwin = old_curwin;
2476 curbuf = curwin->w_buffer;
2477 }
2478 return win != NULL;
2479}
2480
2481/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002482 * Check whether the given window is displaying the specified quickfix/location
2483 * list buffer
2484 */
2485 static int
2486is_qf_win(win, qi)
2487 win_T *win;
2488 qf_info_T *qi;
2489{
2490 /*
2491 * A window displaying the quickfix buffer will have the w_llist_ref field
2492 * set to NULL.
2493 * A window displaying a location list buffer will have the w_llist_ref
2494 * pointing to the location list.
2495 */
2496 if (bt_quickfix(win->w_buffer))
2497 if ((qi == &ql_info && win->w_llist_ref == NULL)
2498 || (qi != &ql_info && win->w_llist_ref == qi))
2499 return TRUE;
2500
2501 return FALSE;
2502}
2503
2504/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002505 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002506 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002507 */
2508 static win_T *
2509qf_find_win(qi)
2510 qf_info_T *qi;
2511{
2512 win_T *win;
2513
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002514 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002515 if (is_qf_win(win, qi))
2516 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002517
2518 return win;
2519}
2520
2521/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002522 * Find a quickfix buffer.
2523 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 */
2525 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002526qf_find_buf(qi)
2527 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002529 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002530 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaar9c102382006-05-03 21:26:49 +00002532 FOR_ALL_TAB_WINDOWS(tp, win)
2533 if (is_qf_win(win, qi))
2534 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002535
Bram Moolenaar9c102382006-05-03 21:26:49 +00002536 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537}
2538
2539/*
2540 * Find the quickfix buffer. If it exists, update the contents.
2541 */
2542 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002543qf_update_buffer(qi)
2544 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002547 win_T *win;
2548 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
2551 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002552 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (buf != NULL)
2554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 /* set curwin/curbuf to buf and save a few things */
2556 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002558 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002560 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2561 && (win = qf_find_win(qi)) != NULL)
2562 {
2563 curwin_save = curwin;
2564 curwin = win;
2565 qf_set_title(qi);
2566 curwin = curwin_save;
2567
2568 }
2569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 /* restore curwin/curbuf and a few other things */
2571 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002573 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575}
2576
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002577 static void
2578qf_set_title(qi)
2579 qf_info_T *qi;
2580{
2581 set_internal_string_var((char_u *)"w:quickfix_title",
2582 qi->qf_lists[qi->qf_curlist].qf_title);
2583}
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585/*
2586 * Fill current buffer with quickfix errors, replacing any previous contents.
2587 * curbuf must be the quickfix buffer!
2588 */
2589 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002590qf_fill_buffer(qi)
2591 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002593 linenr_T lnum;
2594 qfline_T *qfp;
2595 buf_T *errbuf;
2596 int len;
2597 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 /* delete all existing lines */
2600 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2601 (void)ml_delete((linenr_T)1, FALSE);
2602
2603 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002604 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002607 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2608 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 if (qfp->qf_fnum != 0
2611 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2612 && errbuf->b_fname != NULL)
2613 {
2614 if (qfp->qf_type == 1) /* :helpgrep */
2615 STRCPY(IObuff, gettail(errbuf->b_fname));
2616 else
2617 STRCPY(IObuff, errbuf->b_fname);
2618 len = (int)STRLEN(IObuff);
2619 }
2620 else
2621 len = 0;
2622 IObuff[len++] = '|';
2623
2624 if (qfp->qf_lnum > 0)
2625 {
2626 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2627 len += (int)STRLEN(IObuff + len);
2628
2629 if (qfp->qf_col > 0)
2630 {
2631 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2632 len += (int)STRLEN(IObuff + len);
2633 }
2634
2635 sprintf((char *)IObuff + len, "%s",
2636 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2637 len += (int)STRLEN(IObuff + len);
2638 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002639 else if (qfp->qf_pattern != NULL)
2640 {
2641 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2642 len += (int)STRLEN(IObuff + len);
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 IObuff[len++] = '|';
2645 IObuff[len++] = ' ';
2646
2647 /* Remove newlines and leading whitespace from the text.
2648 * For an unrecognized line keep the indent, the compiler may
2649 * mark a word with ^^^^. */
2650 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2651 IObuff + len, IOSIZE - len);
2652
2653 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2654 == FAIL)
2655 break;
2656 qfp = qfp->qf_next;
2657 }
2658 /* Delete the empty line which is now at the end */
2659 (void)ml_delete(lnum + 1, FALSE);
2660 }
2661
2662 /* correct cursor position */
2663 check_lnums(TRUE);
2664
2665 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2666 * resembles reading a file into a buffer, it's more logical when using
2667 * autocommands. */
2668 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2669 curbuf->b_p_ma = FALSE;
2670
2671#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002672 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2674 FALSE, curbuf);
2675 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2676 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002677 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678#endif
2679
2680 /* make sure it will be redrawn */
2681 redraw_curbuf_later(NOT_VALID);
2682
2683 /* Restore KeyTyped, setting 'filetype' may reset it. */
2684 KeyTyped = old_KeyTyped;
2685}
2686
2687#endif /* FEAT_WINDOWS */
2688
2689/*
2690 * Return TRUE if "buf" is the quickfix buffer.
2691 */
2692 int
2693bt_quickfix(buf)
2694 buf_T *buf;
2695{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002696 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697}
2698
2699/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002700 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2701 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 */
2703 int
2704bt_nofile(buf)
2705 buf_T *buf;
2706{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002707 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2708 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709}
2710
2711/*
2712 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2713 */
2714 int
2715bt_dontwrite(buf)
2716 buf_T *buf;
2717{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002718 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
2720
2721 int
2722bt_dontwrite_msg(buf)
2723 buf_T *buf;
2724{
2725 if (bt_dontwrite(buf))
2726 {
2727 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2728 return TRUE;
2729 }
2730 return FALSE;
2731}
2732
2733/*
2734 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2735 * and 'bufhidden'.
2736 */
2737 int
2738buf_hide(buf)
2739 buf_T *buf;
2740{
2741 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2742 switch (buf->b_p_bh[0])
2743 {
2744 case 'u': /* "unload" */
2745 case 'w': /* "wipe" */
2746 case 'd': return FALSE; /* "delete" */
2747 case 'h': return TRUE; /* "hide" */
2748 }
2749 return (p_hid || cmdmod.hide);
2750}
2751
2752/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002753 * Return TRUE when using ":vimgrep" for ":grep".
2754 */
2755 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002756grep_internal(cmdidx)
2757 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002758{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002759 return ((cmdidx == CMD_grep
2760 || cmdidx == CMD_lgrep
2761 || cmdidx == CMD_grepadd
2762 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002763 && STRCMP("internal",
2764 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2765}
2766
2767/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002768 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 */
2770 void
2771ex_make(eap)
2772 exarg_T *eap;
2773{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002774 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 char_u *cmd;
2776 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002777 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002778 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002779 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002780#ifdef FEAT_AUTOCMD
2781 char_u *au_name = NULL;
2782
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002783 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2784 if (grep_internal(eap->cmdidx))
2785 {
2786 ex_vimgrep(eap);
2787 return;
2788 }
2789
Bram Moolenaar7c626922005-02-07 22:01:03 +00002790 switch (eap->cmdidx)
2791 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002792 case CMD_make: au_name = (char_u *)"make"; break;
2793 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2794 case CMD_grep: au_name = (char_u *)"grep"; break;
2795 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2796 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2797 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002798 default: break;
2799 }
2800 if (au_name != NULL)
2801 {
2802 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2803 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002804# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002805 if (did_throw || force_abort)
2806 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002807# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002808 }
2809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
Bram Moolenaara6557602006-02-04 22:43:20 +00002811 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2812 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002813 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002816 fname = get_mef_name();
2817 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002819 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
2821 /*
2822 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2823 */
2824 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2825 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002826 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 cmd = alloc(len);
2828 if (cmd == NULL)
2829 return;
2830 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2831 (char *)p_shq);
2832 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002833 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 /*
2835 * Output a newline if there's something else than the :make command that
2836 * was typed (in which case the cursor is in column 0).
2837 */
2838 if (msg_col == 0)
2839 msg_didout = FALSE;
2840 msg_start();
2841 MSG_PUTS(":!");
2842 msg_outtrans(cmd); /* show what we are doing */
2843
2844 /* let the shell know if we are redirecting output or not */
2845 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2846
2847#ifdef AMIGA
2848 out_flush();
2849 /* read window status report and redraw before message */
2850 (void)char_avail();
2851#endif
2852
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002853 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002854 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2855 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002856 && eap->cmdidx != CMD_lgrepadd),
2857 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002858 if (wp != NULL)
2859 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002860#ifdef FEAT_AUTOCMD
2861 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002862 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002863 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2864 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002865 if (qi->qf_curlist < qi->qf_listcount)
2866 res = qi->qf_lists[qi->qf_curlist].qf_count;
2867 else
2868 res = 0;
2869 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002870#endif
2871 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002872 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar7c626922005-02-07 22:01:03 +00002874 mch_remove(fname);
2875 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 vim_free(cmd);
2877}
2878
2879/*
2880 * Return the name for the errorfile, in allocated memory.
2881 * Find a new unique name when 'makeef' contains "##".
2882 * Returns NULL for error.
2883 */
2884 static char_u *
2885get_mef_name()
2886{
2887 char_u *p;
2888 char_u *name;
2889 static int start = -1;
2890 static int off = 0;
2891#ifdef HAVE_LSTAT
2892 struct stat sb;
2893#endif
2894
2895 if (*p_mef == NUL)
2896 {
2897 name = vim_tempname('e');
2898 if (name == NULL)
2899 EMSG(_(e_notmp));
2900 return name;
2901 }
2902
2903 for (p = p_mef; *p; ++p)
2904 if (p[0] == '#' && p[1] == '#')
2905 break;
2906
2907 if (*p == NUL)
2908 return vim_strsave(p_mef);
2909
2910 /* Keep trying until the name doesn't exist yet. */
2911 for (;;)
2912 {
2913 if (start == -1)
2914 start = mch_get_pid();
2915 else
2916 off += 19;
2917
2918 name = alloc((unsigned)STRLEN(p_mef) + 30);
2919 if (name == NULL)
2920 break;
2921 STRCPY(name, p_mef);
2922 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2923 STRCAT(name, p + 2);
2924 if (mch_getperm(name) < 0
2925#ifdef HAVE_LSTAT
2926 /* Don't accept a symbolic link, its a security risk. */
2927 && mch_lstat((char *)name, &sb) < 0
2928#endif
2929 )
2930 break;
2931 vim_free(name);
2932 }
2933 return name;
2934}
2935
2936/*
2937 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 */
2940 void
2941ex_cc(eap)
2942 exarg_T *eap;
2943{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002944 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002945
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002946 if (eap->cmdidx == CMD_ll
2947 || eap->cmdidx == CMD_lrewind
2948 || eap->cmdidx == CMD_lfirst
2949 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002950 {
2951 qi = GET_LOC_LIST(curwin);
2952 if (qi == NULL)
2953 {
2954 EMSG(_(e_loclist));
2955 return;
2956 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002957 }
2958
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002959 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 eap->addr_count > 0
2961 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002962 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002964 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2965 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 ? 1
2967 : 32767,
2968 eap->forceit);
2969}
2970
2971/*
2972 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002973 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 */
2975 void
2976ex_cnext(eap)
2977 exarg_T *eap;
2978{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002979 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002980
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002981 if (eap->cmdidx == CMD_lnext
2982 || eap->cmdidx == CMD_lNext
2983 || eap->cmdidx == CMD_lprevious
2984 || eap->cmdidx == CMD_lnfile
2985 || eap->cmdidx == CMD_lNfile
2986 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002987 {
2988 qi = GET_LOC_LIST(curwin);
2989 if (qi == NULL)
2990 {
2991 EMSG(_(e_loclist));
2992 return;
2993 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002994 }
2995
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002996 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002998 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003000 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3001 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 ? BACKWARD_FILE
3003 : BACKWARD,
3004 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
3005}
3006
3007/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003008 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003009 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 */
3011 void
3012ex_cfile(eap)
3013 exarg_T *eap;
3014{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003015 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003016 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003017#ifdef FEAT_AUTOCMD
3018 char_u *au_name = NULL;
3019#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003020
3021 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003022 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003023 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003024
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003025#ifdef FEAT_AUTOCMD
3026 switch (eap->cmdidx)
3027 {
3028 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3029 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3030 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3031 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3032 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3033 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3034 default: break;
3035 }
3036 if (au_name != NULL)
3037 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3038#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003039#ifdef FEAT_BROWSE
3040 if (cmdmod.browse)
3041 {
3042 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3043 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3044 if (browse_file == NULL)
3045 return;
3046 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3047 vim_free(browse_file);
3048 }
3049 else
3050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003052 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003053
3054 /*
3055 * This function is used by the :cfile, :cgetfile and :caddfile
3056 * commands.
3057 * :cfile always creates a new quickfix list and jumps to the
3058 * first error.
3059 * :cgetfile creates a new quickfix list but doesn't jump to the
3060 * first error.
3061 * :caddfile adds to an existing quickfix list. If there is no
3062 * quickfix list then a new list is created.
3063 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003064 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003065 && eap->cmdidx != CMD_laddfile),
3066 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003067 && (eap->cmdidx == CMD_cfile
3068 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003069 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003070#ifdef FEAT_AUTOCMD
3071 if (au_name != NULL)
3072 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3073#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003074 if (wp != NULL)
3075 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003076 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003077 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003078
3079 else
3080 {
3081#ifdef FEAT_AUTOCMD
3082 if (au_name != NULL)
3083 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3084#endif
3085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086}
3087
3088/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003089 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003090 * ":vimgrepadd {pattern} file(s)"
3091 * ":lvimgrep {pattern} file(s)"
3092 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003093 */
3094 void
3095ex_vimgrep(eap)
3096 exarg_T *eap;
3097{
Bram Moolenaar81695252004-12-29 20:58:21 +00003098 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003099 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003100 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003101 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003102 char_u *s;
3103 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003104 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003105 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003106 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003107 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003108 buf_T *buf;
3109 int duplicate_name = FALSE;
3110 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003111 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003112 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003113 buf_T *first_match_buf = NULL;
3114 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003115 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003116#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3117 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003118#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003119 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003120 int flags = 0;
3121 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003122 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003123 char_u *dirname_start = NULL;
3124 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003125 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003126#ifdef FEAT_AUTOCMD
3127 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003128
3129 switch (eap->cmdidx)
3130 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003131 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3132 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3133 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003134 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003135 case CMD_grep: au_name = (char_u *)"grep"; break;
3136 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3137 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3138 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003139 default: break;
3140 }
3141 if (au_name != NULL)
3142 {
3143 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3144 curbuf->b_fname, TRUE, curbuf);
3145 if (did_throw || force_abort)
3146 return;
3147 }
3148#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003149
Bram Moolenaar754b5602006-02-09 23:53:20 +00003150 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003151 || eap->cmdidx == CMD_lvimgrep
3152 || eap->cmdidx == CMD_lgrepadd
3153 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003154 {
3155 qi = ll_get_or_alloc_list(curwin);
3156 if (qi == NULL)
3157 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003158 }
3159
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003160 if (eap->addr_count > 0)
3161 tomatch = eap->line2;
3162 else
3163 tomatch = MAXLNUM;
3164
Bram Moolenaar81695252004-12-29 20:58:21 +00003165 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003166 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003167 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003168 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003169 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003170 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003171 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003172 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003173 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003174 if (regmatch.regprog == NULL)
3175 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003176 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003177 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003178
3179 p = skipwhite(p);
3180 if (*p == NUL)
3181 {
3182 EMSG(_("E683: File name missing or invalid pattern"));
3183 goto theend;
3184 }
3185
Bram Moolenaar754b5602006-02-09 23:53:20 +00003186 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003187 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003188 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003189 /* make place for a new list */
Bram Moolenaar41b884b2012-11-14 22:38:08 +01003190 qf_new_list(qi, *eap->cmdlinep, curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003191 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003192 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003193 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003194 prevp->qf_next != prevp; prevp = prevp->qf_next)
3195 ;
3196
3197 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003198 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003199 goto theend;
3200 if (fcount == 0)
3201 {
3202 EMSG(_(e_nomatch));
3203 goto theend;
3204 }
3205
Bram Moolenaard9462e32011-04-11 21:35:11 +02003206 dirname_start = alloc(MAXPATHL);
3207 dirname_now = alloc(MAXPATHL);
3208 if (dirname_start == NULL || dirname_now == NULL)
3209 goto theend;
3210
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003211 /* Remember the current directory, because a BufRead autocommand that does
3212 * ":lcd %:p:h" changes the meaning of short path names. */
3213 mch_dirname(dirname_start, MAXPATHL);
3214
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003215 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003216 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003217 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003218 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003219 if (time(NULL) > seconds)
3220 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003221 /* Display the file name every second or so, show the user we are
3222 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003223 seconds = time(NULL);
3224 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003225 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003226 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003227 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003228 else
3229 {
3230 msg_outtrans(p);
3231 vim_free(p);
3232 }
3233 msg_clr_eos();
3234 msg_didout = FALSE; /* overwrite this message */
3235 msg_nowait = TRUE; /* don't wait for this message */
3236 msg_col = 0;
3237 out_flush();
3238 }
3239
Bram Moolenaar81695252004-12-29 20:58:21 +00003240 buf = buflist_findname_exp(fnames[fi]);
3241 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3242 {
3243 /* Remember that a buffer with this name already exists. */
3244 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003245 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003246 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003247
3248#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3249 /* Don't do Filetype autocommands to avoid loading syntax and
3250 * indent scripts, a great speed improvement. */
3251 save_ei = au_event_disable(",Filetype");
3252#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003253 /* Don't use modelines here, it's useless. */
3254 save_mls = p_mls;
3255 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003256
3257 /* Load file into a buffer, so that 'fileencoding' is detected,
3258 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003259 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003260
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003261 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003262#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3263 au_event_restore(save_ei);
3264#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003265 }
3266 else
3267 /* Use existing, loaded buffer. */
3268 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003269
Bram Moolenaar81695252004-12-29 20:58:21 +00003270 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003271 {
3272 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003273 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003274 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003275 else
3276 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003277 /* Try for a match in all lines of the buffer.
3278 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003279 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003280 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3281 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003282 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003283 col = 0;
3284 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003285 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003286 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003287 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003288 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003289 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003290 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003291 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003292 ml_get_buf(buf,
3293 regmatch.startpos[0].lnum + lnum, FALSE),
3294 regmatch.startpos[0].lnum + lnum,
3295 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003296 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003297 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003298 0, /* nr */
3299 0, /* type */
3300 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003301 ) == FAIL)
3302 {
3303 got_int = TRUE;
3304 break;
3305 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003306 found_match = TRUE;
3307 if (--tomatch == 0)
3308 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003309 if ((flags & VGR_GLOBAL) == 0
3310 || regmatch.endpos[0].lnum > 0)
3311 break;
3312 col = regmatch.endpos[0].col
3313 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003314 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003315 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003316 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003317 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003318 if (got_int)
3319 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003320 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003321
3322 if (using_dummy)
3323 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003324 if (found_match && first_match_buf == NULL)
3325 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003326 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003327 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003328 /* Never keep a dummy buffer if there is another buffer
3329 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003330 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003331 buf = NULL;
3332 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003333 else if (!cmdmod.hide
3334 || buf->b_p_bh[0] == 'u' /* "unload" */
3335 || buf->b_p_bh[0] == 'w' /* "wipe" */
3336 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003337 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003338 /* When no match was found we don't need to remember the
3339 * buffer, wipe it out. If there was a match and it
3340 * wasn't the first one or we won't jump there: only
3341 * unload the buffer.
3342 * Ignore 'hidden' here, because it may lead to having too
3343 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003344 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003345 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003346 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003347 buf = NULL;
3348 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003349 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003350 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003351 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003352 buf = NULL;
3353 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003354 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003355
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003356 if (buf != NULL)
3357 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003358 /* If the buffer is still loaded we need to use the
3359 * directory we jumped to below. */
3360 if (buf == first_match_buf
3361 && target_dir == NULL
3362 && STRCMP(dirname_start, dirname_now) != 0)
3363 target_dir = vim_strsave(dirname_now);
3364
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003365 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003366 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003367 * need to be done (again). But not the window-local
3368 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003369 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003370#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003371 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3372 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003373#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003374 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003375 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003376 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003377 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003378 }
3379 }
3380
3381 FreeWild(fcount, fnames);
3382
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003383 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3384 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3385 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003386
3387#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003388 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003389#endif
3390
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003391#ifdef FEAT_AUTOCMD
3392 if (au_name != NULL)
3393 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3394 curbuf->b_fname, TRUE, curbuf);
3395#endif
3396
Bram Moolenaar86b68352004-12-27 21:59:20 +00003397 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003398 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003399 {
3400 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003401 {
3402 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003403 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003404 if (buf != curbuf)
3405 /* If we jumped to another buffer redrawing will already be
3406 * taken care of. */
3407 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003408
3409 /* Jump to the directory used after loading the buffer. */
3410 if (curbuf == first_match_buf && target_dir != NULL)
3411 {
3412 exarg_T ea;
3413
3414 ea.arg = target_dir;
3415 ea.cmdidx = CMD_lcd;
3416 ex_cd(&ea);
3417 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003418 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003419 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003420 else
3421 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003422
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003423 /* If we loaded a dummy buffer into the current window, the autocommands
3424 * may have messed up things, need to redraw and recompute folds. */
3425 if (redraw_for_dummy)
3426 {
3427#ifdef FEAT_FOLDING
3428 foldUpdateAll(curwin);
3429#else
3430 redraw_later(NOT_VALID);
3431#endif
3432 }
3433
Bram Moolenaar86b68352004-12-27 21:59:20 +00003434theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003435 vim_free(dirname_now);
3436 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003437 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003438 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003439}
3440
3441/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003442 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003443 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003444 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3445 * If "s" is not NULL terminate the pattern with a NUL.
3446 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003447 */
3448 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003449skip_vimgrep_pat(p, s, flags)
3450 char_u *p;
3451 char_u **s;
3452 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003453{
3454 int c;
3455
3456 if (vim_isIDc(*p))
3457 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003458 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003459 if (s != NULL)
3460 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003461 p = skiptowhite(p);
3462 if (s != NULL && *p != NUL)
3463 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003464 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003465 else
3466 {
3467 /* ":vimgrep /pattern/[g][j] fname" */
3468 if (s != NULL)
3469 *s = p + 1;
3470 c = *p;
3471 p = skip_regexp(p + 1, c, TRUE, NULL);
3472 if (*p != c)
3473 return NULL;
3474
3475 /* Truncate the pattern. */
3476 if (s != NULL)
3477 *p = NUL;
3478 ++p;
3479
3480 /* Find the flags */
3481 while (*p == 'g' || *p == 'j')
3482 {
3483 if (flags != NULL)
3484 {
3485 if (*p == 'g')
3486 *flags |= VGR_GLOBAL;
3487 else
3488 *flags |= VGR_NOJUMP;
3489 }
3490 ++p;
3491 }
3492 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003493 return p;
3494}
3495
3496/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003497 * Restore current working directory to "dirname_start" if they differ, taking
3498 * into account whether it is set locally or globally.
3499 */
3500 static void
3501restore_start_dir(dirname_start)
3502 char_u *dirname_start;
3503{
3504 char_u *dirname_now = alloc(MAXPATHL);
3505
3506 if (NULL != dirname_now)
3507 {
3508 mch_dirname(dirname_now, MAXPATHL);
3509 if (STRCMP(dirname_start, dirname_now) != 0)
3510 {
3511 /* If the directory has changed, change it back by building up an
3512 * appropriate ex command and executing it. */
3513 exarg_T ea;
3514
3515 ea.arg = dirname_start;
3516 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3517 ex_cd(&ea);
3518 }
3519 }
3520}
3521
3522/*
3523 * Load file "fname" into a dummy buffer and return the buffer pointer,
3524 * placing the directory resulting from the buffer load into the
3525 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3526 * prior to calling this function. Restores directory to "dirname_start" prior
3527 * to returning, if autocmds or the 'autochdir' option have changed it.
3528 *
3529 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3530 * or wipe_dummy_buffer() later!
3531 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003532 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003533 */
3534 static buf_T *
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003535load_dummy_buffer(fname, dirname_start, resulting_dir)
Bram Moolenaar81695252004-12-29 20:58:21 +00003536 char_u *fname;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003537 char_u *dirname_start; /* in: old directory */
3538 char_u *resulting_dir; /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003539{
3540 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003541 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003542 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003543 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003544
3545 /* Allocate a buffer without putting it in the buffer list. */
3546 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3547 if (newbuf == NULL)
3548 return NULL;
3549
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003550 /* Init the options. */
3551 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3552
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003553 /* need to open the memfile before putting the buffer in a window */
3554 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003555 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003556 /* set curwin/curbuf to buf and save a few things */
3557 aucmd_prepbuf(&aco, newbuf);
3558
3559 /* Need to set the filename for autocommands. */
3560 (void)setfname(curbuf, fname, NULL, FALSE);
3561
Bram Moolenaar81695252004-12-29 20:58:21 +00003562 /* Create swap file now to avoid the ATTENTION message. */
3563 check_need_swap(TRUE);
3564
3565 /* Remove the "dummy" flag, otherwise autocommands may not
3566 * work. */
3567 curbuf->b_flags &= ~BF_DUMMY;
3568
3569 if (readfile(fname, NULL,
3570 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3571 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003572 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003573 && !(curbuf->b_flags & BF_NEW))
3574 {
3575 failed = FALSE;
3576 if (curbuf != newbuf)
3577 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003578 /* Bloody autocommands changed the buffer! Can happen when
3579 * using netrw and editing a remote file. Use the current
3580 * buffer instead, delete the dummy one after restoring the
3581 * window stuff. */
3582 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003583 newbuf = curbuf;
3584 }
3585 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003586
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003587 /* restore curwin/curbuf and a few other things */
3588 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003589 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3590 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003591 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003592
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003593 /*
3594 * When autocommands/'autochdir' option changed directory: go back.
3595 * Let the caller know what the resulting dir was first, in case it is
3596 * important.
3597 */
3598 mch_dirname(resulting_dir, MAXPATHL);
3599 restore_start_dir(dirname_start);
3600
Bram Moolenaar81695252004-12-29 20:58:21 +00003601 if (!buf_valid(newbuf))
3602 return NULL;
3603 if (failed)
3604 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003605 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003606 return NULL;
3607 }
3608 return newbuf;
3609}
3610
3611/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003612 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3613 * directory to "dirname_start" prior to returning, if autocmds or the
3614 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003615 */
3616 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003617wipe_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003618 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003619 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003620{
3621 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003622 {
3623#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3624 cleanup_T cs;
3625
3626 /* Reset the error/interrupt/exception state here so that aborting()
3627 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3628 * work when got_int is set. */
3629 enter_cleanup(&cs);
3630#endif
3631
Bram Moolenaar81695252004-12-29 20:58:21 +00003632 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003633
3634#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3635 /* Restore the error/interrupt/exception state if not discarded by a
3636 * new aborting error, interrupt, or uncaught exception. */
3637 leave_cleanup(&cs);
3638#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003639 /* When autocommands/'autochdir' option changed directory: go back. */
3640 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003641 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003642}
3643
3644/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003645 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3646 * directory to "dirname_start" prior to returning, if autocmds or the
3647 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003648 */
3649 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003650unload_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003651 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003652 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003653{
3654 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003655 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003656 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003657
3658 /* When autocommands/'autochdir' option changed directory: go back. */
3659 restore_start_dir(dirname_start);
3660 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003661}
3662
Bram Moolenaar05159a02005-02-26 23:04:13 +00003663#if defined(FEAT_EVAL) || defined(PROTO)
3664/*
3665 * Add each quickfix error to list "list" as a dictionary.
3666 */
3667 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003668get_errorlist(wp, list)
3669 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003670 list_T *list;
3671{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003672 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003673 dict_T *dict;
3674 char_u buf[2];
3675 qfline_T *qfp;
3676 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003677 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003678
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003679 if (wp != NULL)
3680 {
3681 qi = GET_LOC_LIST(wp);
3682 if (qi == NULL)
3683 return FAIL;
3684 }
3685
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003686 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003687 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003688 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003689
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003690 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3691 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003692 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003693 /* Handle entries with a non-existing buffer number. */
3694 bufnum = qfp->qf_fnum;
3695 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3696 bufnum = 0;
3697
Bram Moolenaar05159a02005-02-26 23:04:13 +00003698 if ((dict = dict_alloc()) == NULL)
3699 return FAIL;
3700 if (list_append_dict(list, dict) == FAIL)
3701 return FAIL;
3702
3703 buf[0] = qfp->qf_type;
3704 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003705 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003706 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3707 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3708 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3709 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003710 || dict_add_nr_str(dict, "pattern", 0L,
3711 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3712 || dict_add_nr_str(dict, "text", 0L,
3713 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003714 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3715 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3716 return FAIL;
3717
3718 qfp = qfp->qf_next;
3719 }
3720 return OK;
3721}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003722
3723/*
3724 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003725 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003726 */
3727 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003728set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003729 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003730 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003731 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003732 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003733{
3734 listitem_T *li;
3735 dict_T *d;
3736 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003737 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003738 long lnum;
3739 int col, nr;
3740 int vcol;
3741 qfline_T *prevp = NULL;
3742 int valid, status;
3743 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003744 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003745 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003746
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003747 if (wp != NULL)
3748 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003749 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003750 if (qi == NULL)
3751 return FAIL;
3752 }
3753
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003754 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003755 /* make place for a new list */
Bram Moolenaar41b884b2012-11-14 22:38:08 +01003756 qf_new_list(qi, title, wp);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003757 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003758 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003759 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003760 prevp->qf_next != prevp; prevp = prevp->qf_next)
3761 ;
3762 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003763 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003764
3765 for (li = list->lv_first; li != NULL; li = li->li_next)
3766 {
3767 if (li->li_tv.v_type != VAR_DICT)
3768 continue; /* Skip non-dict items */
3769
3770 d = li->li_tv.vval.v_dict;
3771 if (d == NULL)
3772 continue;
3773
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003774 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003775 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003776 lnum = get_dict_number(d, (char_u *)"lnum");
3777 col = get_dict_number(d, (char_u *)"col");
3778 vcol = get_dict_number(d, (char_u *)"vcol");
3779 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003780 type = get_dict_string(d, (char_u *)"type", TRUE);
3781 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3782 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003783 if (text == NULL)
3784 text = vim_strsave((char_u *)"");
3785
3786 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003787 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003788 valid = FALSE;
3789
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003790 /* Mark entries with non-existing buffer number as not valid. Give the
3791 * error message only once. */
3792 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3793 {
3794 if (!did_bufnr_emsg)
3795 {
3796 did_bufnr_emsg = TRUE;
3797 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3798 }
3799 valid = FALSE;
3800 bufnum = 0;
3801 }
3802
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003803 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003804 NULL, /* dir */
3805 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003806 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003807 text,
3808 lnum,
3809 col,
3810 vcol, /* vis_col */
3811 pattern, /* search pattern */
3812 nr,
3813 type == NULL ? NUL : *type,
3814 valid);
3815
3816 vim_free(filename);
3817 vim_free(pattern);
3818 vim_free(text);
3819 vim_free(type);
3820
3821 if (status == FAIL)
3822 {
3823 retval = FAIL;
3824 break;
3825 }
3826 }
3827
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003828 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003829 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003830 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3831 else
3832 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003833 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3834 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003835
3836#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003837 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003838#endif
3839
3840 return retval;
3841}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003842#endif
3843
Bram Moolenaar81695252004-12-29 20:58:21 +00003844/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003845 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003846 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003847 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003848 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003849 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003850 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003851 */
3852 void
3853ex_cbuffer(eap)
3854 exarg_T *eap;
3855{
3856 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003857 qf_info_T *qi = &ql_info;
3858
Bram Moolenaardb552d602006-03-23 22:59:57 +00003859 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3860 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003861 {
3862 qi = ll_get_or_alloc_list(curwin);
3863 if (qi == NULL)
3864 return;
3865 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003866
3867 if (*eap->arg == NUL)
3868 buf = curbuf;
3869 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3870 buf = buflist_findnr(atoi((char *)eap->arg));
3871 if (buf == NULL)
3872 EMSG(_(e_invarg));
3873 else if (buf->b_ml.ml_mfp == NULL)
3874 EMSG(_("E681: Buffer is not loaded"));
3875 else
3876 {
3877 if (eap->addr_count == 0)
3878 {
3879 eap->line1 = 1;
3880 eap->line2 = buf->b_ml.ml_line_count;
3881 }
3882 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3883 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3884 EMSG(_(e_invrange));
3885 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003886 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003887 char_u *qf_title = *eap->cmdlinep;
3888
3889 if (buf->b_sfname)
3890 {
3891 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3892 (char *)qf_title, (char *)buf->b_sfname);
3893 qf_title = IObuff;
3894 }
3895
Bram Moolenaardb552d602006-03-23 22:59:57 +00003896 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3897 (eap->cmdidx != CMD_caddbuffer
3898 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003899 eap->line1, eap->line2,
3900 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003901 && (eap->cmdidx == CMD_cbuffer
3902 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003903 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3904 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003905 }
3906}
3907
Bram Moolenaar1e015462005-09-25 22:16:38 +00003908#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003909/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003910 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3911 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003912 */
3913 void
3914ex_cexpr(eap)
3915 exarg_T *eap;
3916{
3917 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003918 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003919
Bram Moolenaardb552d602006-03-23 22:59:57 +00003920 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3921 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003922 {
3923 qi = ll_get_or_alloc_list(curwin);
3924 if (qi == NULL)
3925 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003926 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003927
Bram Moolenaar4770d092006-01-12 23:22:24 +00003928 /* Evaluate the expression. When the result is a string or a list we can
3929 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003930 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003931 if (tv != NULL)
3932 {
3933 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3934 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3935 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003936 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3937 (eap->cmdidx != CMD_caddexpr
3938 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003939 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003940 && (eap->cmdidx == CMD_cexpr
3941 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003942 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003943 }
3944 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003945 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003946 free_tv(tv);
3947 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003948}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003949#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003950
3951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 * ":helpgrep {pattern}"
3953 */
3954 void
3955ex_helpgrep(eap)
3956 exarg_T *eap;
3957{
3958 regmatch_T regmatch;
3959 char_u *save_cpo;
3960 char_u *p;
3961 int fcount;
3962 char_u **fnames;
3963 FILE *fd;
3964 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003965 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003967#ifdef FEAT_MULTI_LANG
3968 char_u *lang;
3969#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003970 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003971 int new_qi = FALSE;
3972 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01003973#ifdef FEAT_AUTOCMD
3974 char_u *au_name = NULL;
3975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003977#ifdef FEAT_MULTI_LANG
3978 /* Check for a specified language */
3979 lang = check_help_lang(eap->arg);
3980#endif
3981
Bram Moolenaar73633f82012-01-20 13:39:07 +01003982#ifdef FEAT_AUTOCMD
3983 switch (eap->cmdidx)
3984 {
3985 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
3986 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
3987 default: break;
3988 }
3989 if (au_name != NULL)
3990 {
3991 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3992 curbuf->b_fname, TRUE, curbuf);
3993 if (did_throw || force_abort)
3994 return;
3995 }
3996#endif
3997
3998 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3999 save_cpo = p_cpo;
4000 p_cpo = empty_option;
4001
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004002 if (eap->cmdidx == CMD_lhelpgrep)
4003 {
4004 /* Find an existing help window */
4005 FOR_ALL_WINDOWS(wp)
4006 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4007 break;
4008
4009 if (wp == NULL) /* Help window not found */
4010 qi = NULL;
4011 else
4012 qi = wp->w_llist;
4013
4014 if (qi == NULL)
4015 {
4016 /* Allocate a new location list for help text matches */
4017 if ((qi = ll_new_list()) == NULL)
4018 return;
4019 new_qi = TRUE;
4020 }
4021 }
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4024 regmatch.rm_ic = FALSE;
4025 if (regmatch.regprog != NULL)
4026 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004027#ifdef FEAT_MBYTE
4028 vimconv_T vc;
4029
4030 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4031 * differs. */
4032 vc.vc_type = CONV_NONE;
4033 if (!enc_utf8)
4034 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4035#endif
4036
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 /* create a new quickfix list */
Bram Moolenaar41b884b2012-11-14 22:38:08 +01004038 qf_new_list(qi, *eap->cmdlinep, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040 /* Go through all directories in 'runtimepath' */
4041 p = p_rtp;
4042 while (*p != NUL && !got_int)
4043 {
4044 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4045
4046 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4047 add_pathsep(NameBuff);
4048 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4049 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4050 &fnames, EW_FILE|EW_SILENT) == OK
4051 && fcount > 0)
4052 {
4053 for (fi = 0; fi < fcount && !got_int; ++fi)
4054 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004055#ifdef FEAT_MULTI_LANG
4056 /* Skip files for a different language. */
4057 if (lang != NULL
4058 && STRNICMP(lang, fnames[fi]
4059 + STRLEN(fnames[fi]) - 3, 2) != 0
4060 && !(STRNICMP(lang, "en", 2) == 0
4061 && STRNICMP("txt", fnames[fi]
4062 + STRLEN(fnames[fi]) - 3, 3) == 0))
4063 continue;
4064#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004065 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (fd != NULL)
4067 {
4068 lnum = 1;
4069 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4070 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004071 char_u *line = IObuff;
4072#ifdef FEAT_MBYTE
4073 /* Convert a line if 'encoding' is not utf-8 and
4074 * the line contains a non-ASCII character. */
4075 if (vc.vc_type != CONV_NONE
4076 && has_non_ascii(IObuff)) {
4077 line = string_convert(&vc, IObuff, NULL);
4078 if (line == NULL)
4079 line = IObuff;
4080 }
4081#endif
4082
4083 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004085 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004088 while (l > 0 && line[l - 1] <= ' ')
4089 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004091 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 NULL, /* dir */
4093 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004094 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004095 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004097 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004098 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004099 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004100 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 0, /* nr */
4102 1, /* type */
4103 TRUE /* valid */
4104 ) == FAIL)
4105 {
4106 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004107#ifdef FEAT_MBYTE
4108 if (line != IObuff)
4109 vim_free(line);
4110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 break;
4112 }
4113 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004114#ifdef FEAT_MBYTE
4115 if (line != IObuff)
4116 vim_free(line);
4117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 ++lnum;
4119 line_breakcheck();
4120 }
4121 fclose(fd);
4122 }
4123 }
4124 FreeWild(fcount, fnames);
4125 }
4126 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 vim_free(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004129#ifdef FEAT_MBYTE
4130 if (vc.vc_type != CONV_NONE)
4131 convert_setup(&vc, NULL, NULL);
4132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004134 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4135 qi->qf_lists[qi->qf_curlist].qf_ptr =
4136 qi->qf_lists[qi->qf_curlist].qf_start;
4137 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004140 if (p_cpo == empty_option)
4141 p_cpo = save_cpo;
4142 else
4143 /* Darn, some plugin changed the value. */
4144 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004147 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#endif
4149
Bram Moolenaar73633f82012-01-20 13:39:07 +01004150#ifdef FEAT_AUTOCMD
4151 if (au_name != NULL)
4152 {
4153 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4154 curbuf->b_fname, TRUE, curbuf);
4155 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4156 /* autocommands made "qi" invalid */
4157 return;
4158 }
4159#endif
4160
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004162 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004163 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004164 else
4165 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004166
4167 if (eap->cmdidx == CMD_lhelpgrep)
4168 {
4169 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004170 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004171 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4172 {
4173 if (new_qi)
4174 ll_free_all(&qi);
4175 }
4176 else if (curwin->w_llist == NULL)
4177 curwin->w_llist = qi;
4178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179}
4180
4181#endif /* FEAT_QUICKFIX */