blob: 7dbdb965724aa89d2f979cdb11b61f0f40d1614b [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 Moolenaar94116152012-11-28 17:41:59 +0100110static void qf_new_list __ARGS((qf_info_T *qi, char_u *qf_title));
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 Moolenaar94116152012-11-28 17:41:59 +0100269 qf_new_list(qi, qf_title);
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 Moolenaar94116152012-11-28 17:41:59 +0100888qf_new_list(qi, qf_title)
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 Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 int i;
893
894 /*
895 * If the current entry is not the last entry, delete entries below
896 * the current entry. This makes it possible to browse in a tree-like
897 * way with ":grep'.
898 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000899 while (qi->qf_listcount > qi->qf_curlist + 1)
900 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 /*
903 * When the stack is full, remove to oldest entry
904 * Otherwise, add a new entry.
905 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000906 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000908 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000910 qi->qf_lists[i - 1] = qi->qf_lists[i];
911 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000914 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100915 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200916 if (qf_title != NULL)
917 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200918 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200919
920 qi->qf_lists[qi->qf_curlist].qf_title = p;
921 if (p != NULL)
922 sprintf((char *)p, ":%s", (char *)qf_title);
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924}
925
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000926/*
927 * Free a location list
928 */
929 static void
930ll_free_all(pqi)
931 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000932{
933 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000934 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000935
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000936 qi = *pqi;
937 if (qi == NULL)
938 return;
939 *pqi = NULL; /* Remove reference to this list */
940
941 qi->qf_refcount--;
942 if (qi->qf_refcount < 1)
943 {
944 /* No references to this location list */
945 for (i = 0; i < qi->qf_listcount; ++i)
946 qf_free(qi, i);
947 vim_free(qi);
948 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000949}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000950
951 void
952qf_free_all(wp)
953 win_T *wp;
954{
955 int i;
956 qf_info_T *qi = &ql_info;
957
958 if (wp != NULL)
959 {
960 /* location list */
961 ll_free_all(&wp->w_llist);
962 ll_free_all(&wp->w_llist_ref);
963 }
964 else
965 /* quickfix list */
966 for (i = 0; i < qi->qf_listcount; ++i)
967 qf_free(qi, i);
968}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970/*
971 * Add an entry to the end of the list of errors.
972 * Returns OK or FAIL.
973 */
974 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000975qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
976 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000977 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 char_u *dir; /* optional directory name */
980 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000981 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 char_u *mesg; /* message */
983 long lnum; /* line number */
984 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000985 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000986 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 int nr; /* error number */
988 int type; /* type character */
989 int valid; /* valid entry */
990{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000991 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000993 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000995 if (bufnum != 0)
996 qfp->qf_fnum = bufnum;
997 else
998 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1000 {
1001 vim_free(qfp);
1002 return FAIL;
1003 }
1004 qfp->qf_lnum = lnum;
1005 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001006 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001007 if (pattern == NULL || *pattern == NUL)
1008 qfp->qf_pattern = NULL;
1009 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1010 {
1011 vim_free(qfp->qf_text);
1012 vim_free(qfp);
1013 return FAIL;
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 qfp->qf_nr = nr;
1016 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1017 type = 0;
1018 qfp->qf_type = type;
1019 qfp->qf_valid = valid;
1020
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001021 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1022 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001024 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 qfp->qf_prev = qfp; /* first element points to itself */
1026 }
1027 else
1028 {
1029 qfp->qf_prev = *prevp;
1030 (*prevp)->qf_next = qfp;
1031 }
1032 qfp->qf_next = qfp; /* last element points to itself */
1033 qfp->qf_cleared = FALSE;
1034 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001035 ++qi->qf_lists[qi->qf_curlist].qf_count;
1036 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1037 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001039 qi->qf_lists[qi->qf_curlist].qf_index =
1040 qi->qf_lists[qi->qf_curlist].qf_count;
1041 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043
1044 return OK;
1045}
1046
1047/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001048 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001049 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001050 static qf_info_T *
1051ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001052{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001053 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001054
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001055 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1056 if (qi != NULL)
1057 {
1058 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1059 qi->qf_refcount++;
1060 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001061
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001062 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001063}
1064
1065/*
1066 * Return the location list for window 'wp'.
1067 * If not present, allocate a location list
1068 */
1069 static qf_info_T *
1070ll_get_or_alloc_list(wp)
1071 win_T *wp;
1072{
1073 if (IS_LL_WINDOW(wp))
1074 /* For a location list window, use the referenced location list */
1075 return wp->w_llist_ref;
1076
1077 /*
1078 * For a non-location list window, w_llist_ref should not point to a
1079 * location list.
1080 */
1081 ll_free_all(&wp->w_llist_ref);
1082
1083 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001084 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001085 return wp->w_llist;
1086}
1087
1088/*
1089 * Copy the location list from window "from" to window "to".
1090 */
1091 void
1092copy_loclist(from, to)
1093 win_T *from;
1094 win_T *to;
1095{
1096 qf_info_T *qi;
1097 int idx;
1098 int i;
1099
1100 /*
1101 * When copying from a location list window, copy the referenced
1102 * location list. For other windows, copy the location list for
1103 * that window.
1104 */
1105 if (IS_LL_WINDOW(from))
1106 qi = from->w_llist_ref;
1107 else
1108 qi = from->w_llist;
1109
1110 if (qi == NULL) /* no location list to copy */
1111 return;
1112
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001113 /* allocate a new location list */
1114 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001115 return;
1116
1117 to->w_llist->qf_listcount = qi->qf_listcount;
1118
1119 /* Copy the location lists one at a time */
1120 for (idx = 0; idx < qi->qf_listcount; idx++)
1121 {
1122 qf_list_T *from_qfl;
1123 qf_list_T *to_qfl;
1124
1125 to->w_llist->qf_curlist = idx;
1126
1127 from_qfl = &qi->qf_lists[idx];
1128 to_qfl = &to->w_llist->qf_lists[idx];
1129
1130 /* Some of the fields are populated by qf_add_entry() */
1131 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1132 to_qfl->qf_count = 0;
1133 to_qfl->qf_index = 0;
1134 to_qfl->qf_start = NULL;
1135 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001136 if (from_qfl->qf_title != NULL)
1137 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1138 else
1139 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001140
1141 if (from_qfl->qf_count)
1142 {
1143 qfline_T *from_qfp;
1144 qfline_T *prevp = NULL;
1145
1146 /* copy all the location entries in this list */
1147 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1148 ++i, from_qfp = from_qfp->qf_next)
1149 {
1150 if (qf_add_entry(to->w_llist, &prevp,
1151 NULL,
1152 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001153 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001154 from_qfp->qf_text,
1155 from_qfp->qf_lnum,
1156 from_qfp->qf_col,
1157 from_qfp->qf_viscol,
1158 from_qfp->qf_pattern,
1159 from_qfp->qf_nr,
1160 0,
1161 from_qfp->qf_valid) == FAIL)
1162 {
1163 qf_free_all(to);
1164 return;
1165 }
1166 /*
1167 * qf_add_entry() will not set the qf_num field, as the
1168 * directory and file names are not supplied. So the qf_fnum
1169 * field is copied here.
1170 */
1171 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1172 prevp->qf_type = from_qfp->qf_type; /* error type */
1173 if (from_qfl->qf_ptr == from_qfp)
1174 to_qfl->qf_ptr = prevp; /* current location */
1175 }
1176 }
1177
1178 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1179
1180 /* When no valid entries are present in the list, qf_ptr points to
1181 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001182 if (to_qfl->qf_nonevalid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001183 to_qfl->qf_ptr = to_qfl->qf_start;
1184 }
1185
1186 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1187}
1188
1189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 * get buffer number for file "dir.name"
1191 */
1192 static int
1193qf_get_fnum(directory, fname)
1194 char_u *directory;
1195 char_u *fname;
1196{
1197 if (fname == NULL || *fname == NUL) /* no file name */
1198 return 0;
1199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 char_u *ptr;
1201 int fnum;
1202
Bram Moolenaare60acc12011-05-10 16:41:25 +02001203#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001205#endif
1206#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (directory != NULL)
1208 slash_adjust(directory);
1209 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (directory != NULL && !vim_isAbsName(fname)
1212 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1213 {
1214 /*
1215 * Here we check if the file really exists.
1216 * This should normally be true, but if make works without
1217 * "leaving directory"-messages we might have missed a
1218 * directory change.
1219 */
1220 if (mch_getperm(ptr) < 0)
1221 {
1222 vim_free(ptr);
1223 directory = qf_guess_filepath(fname);
1224 if (directory)
1225 ptr = concat_fnames(directory, fname, TRUE);
1226 else
1227 ptr = vim_strsave(fname);
1228 }
1229 /* Use concatenated directory name and file name */
1230 fnum = buflist_add(ptr, 0);
1231 vim_free(ptr);
1232 return fnum;
1233 }
1234 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236}
1237
1238/*
1239 * push dirbuf onto the directory stack and return pointer to actual dir or
1240 * NULL on error
1241 */
1242 static char_u *
1243qf_push_dir(dirbuf, stackptr)
1244 char_u *dirbuf;
1245 struct dir_stack_T **stackptr;
1246{
1247 struct dir_stack_T *ds_new;
1248 struct dir_stack_T *ds_ptr;
1249
1250 /* allocate new stack element and hook it in */
1251 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1252 if (ds_new == NULL)
1253 return NULL;
1254
1255 ds_new->next = *stackptr;
1256 *stackptr = ds_new;
1257
1258 /* store directory on the stack */
1259 if (vim_isAbsName(dirbuf)
1260 || (*stackptr)->next == NULL
1261 || (*stackptr && dir_stack != *stackptr))
1262 (*stackptr)->dirname = vim_strsave(dirbuf);
1263 else
1264 {
1265 /* Okay we don't have an absolute path.
1266 * dirbuf must be a subdir of one of the directories on the stack.
1267 * Let's search...
1268 */
1269 ds_new = (*stackptr)->next;
1270 (*stackptr)->dirname = NULL;
1271 while (ds_new)
1272 {
1273 vim_free((*stackptr)->dirname);
1274 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1275 TRUE);
1276 if (mch_isdir((*stackptr)->dirname) == TRUE)
1277 break;
1278
1279 ds_new = ds_new->next;
1280 }
1281
1282 /* clean up all dirs we already left */
1283 while ((*stackptr)->next != ds_new)
1284 {
1285 ds_ptr = (*stackptr)->next;
1286 (*stackptr)->next = (*stackptr)->next->next;
1287 vim_free(ds_ptr->dirname);
1288 vim_free(ds_ptr);
1289 }
1290
1291 /* Nothing found -> it must be on top level */
1292 if (ds_new == NULL)
1293 {
1294 vim_free((*stackptr)->dirname);
1295 (*stackptr)->dirname = vim_strsave(dirbuf);
1296 }
1297 }
1298
1299 if ((*stackptr)->dirname != NULL)
1300 return (*stackptr)->dirname;
1301 else
1302 {
1303 ds_ptr = *stackptr;
1304 *stackptr = (*stackptr)->next;
1305 vim_free(ds_ptr);
1306 return NULL;
1307 }
1308}
1309
1310
1311/*
1312 * pop dirbuf from the directory stack and return previous directory or NULL if
1313 * stack is empty
1314 */
1315 static char_u *
1316qf_pop_dir(stackptr)
1317 struct dir_stack_T **stackptr;
1318{
1319 struct dir_stack_T *ds_ptr;
1320
1321 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1322 * What to do if it isn't? */
1323
1324 /* pop top element and free it */
1325 if (*stackptr != NULL)
1326 {
1327 ds_ptr = *stackptr;
1328 *stackptr = (*stackptr)->next;
1329 vim_free(ds_ptr->dirname);
1330 vim_free(ds_ptr);
1331 }
1332
1333 /* return NEW top element as current dir or NULL if stack is empty*/
1334 return *stackptr ? (*stackptr)->dirname : NULL;
1335}
1336
1337/*
1338 * clean up directory stack
1339 */
1340 static void
1341qf_clean_dir_stack(stackptr)
1342 struct dir_stack_T **stackptr;
1343{
1344 struct dir_stack_T *ds_ptr;
1345
1346 while ((ds_ptr = *stackptr) != NULL)
1347 {
1348 *stackptr = (*stackptr)->next;
1349 vim_free(ds_ptr->dirname);
1350 vim_free(ds_ptr);
1351 }
1352}
1353
1354/*
1355 * Check in which directory of the directory stack the given file can be
1356 * found.
1357 * Returns a pointer to the directory name or NULL if not found
1358 * Cleans up intermediate directory entries.
1359 *
1360 * TODO: How to solve the following problem?
1361 * If we have the this directory tree:
1362 * ./
1363 * ./aa
1364 * ./aa/bb
1365 * ./bb
1366 * ./bb/x.c
1367 * and make says:
1368 * making all in aa
1369 * making all in bb
1370 * x.c:9: Error
1371 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1372 * qf_guess_filepath will return NULL.
1373 */
1374 static char_u *
1375qf_guess_filepath(filename)
1376 char_u *filename;
1377{
1378 struct dir_stack_T *ds_ptr;
1379 struct dir_stack_T *ds_tmp;
1380 char_u *fullname;
1381
1382 /* no dirs on the stack - there's nothing we can do */
1383 if (dir_stack == NULL)
1384 return NULL;
1385
1386 ds_ptr = dir_stack->next;
1387 fullname = NULL;
1388 while (ds_ptr)
1389 {
1390 vim_free(fullname);
1391 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1392
1393 /* If concat_fnames failed, just go on. The worst thing that can happen
1394 * is that we delete the entire stack.
1395 */
1396 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1397 break;
1398
1399 ds_ptr = ds_ptr->next;
1400 }
1401
1402 vim_free(fullname);
1403
1404 /* clean up all dirs we already left */
1405 while (dir_stack->next != ds_ptr)
1406 {
1407 ds_tmp = dir_stack->next;
1408 dir_stack->next = dir_stack->next->next;
1409 vim_free(ds_tmp->dirname);
1410 vim_free(ds_tmp);
1411 }
1412
1413 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1414
1415}
1416
1417/*
1418 * jump to a quickfix line
1419 * if dir == FORWARD go "errornr" valid entries forward
1420 * if dir == BACKWARD go "errornr" valid entries backward
1421 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1422 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1423 * else if "errornr" is zero, redisplay the same line
1424 * else go to entry "errornr"
1425 */
1426 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001427qf_jump(qi, dir, errornr, forceit)
1428 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 int dir;
1430 int errornr;
1431 int forceit;
1432{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001433 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001434 qfline_T *qf_ptr;
1435 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 int qf_index;
1437 int old_qf_fnum;
1438 int old_qf_index;
1439 int prev_index;
1440 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1441 char_u *err = e_no_more_items;
1442 linenr_T i;
1443 buf_T *old_curbuf;
1444 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 colnr_T screen_col;
1446 colnr_T char_col;
1447 char_u *line;
1448#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001449 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001450 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 int opened_window = FALSE;
1452 win_T *win;
1453 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001454 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001456 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 int print_message = TRUE;
1458 int len;
1459#ifdef FEAT_FOLDING
1460 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1461#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001462 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001463 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001465 if (qi == NULL)
1466 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001467
1468 if (qi->qf_curlist >= qi->qf_listcount
1469 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 {
1471 EMSG(_(e_quickfix));
1472 return;
1473 }
1474
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001475 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001477 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 old_qf_index = qf_index;
1479 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1480 {
1481 while (errornr--)
1482 {
1483 old_qf_ptr = qf_ptr;
1484 prev_index = qf_index;
1485 old_qf_fnum = qf_ptr->qf_fnum;
1486 do
1487 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001488 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 || qf_ptr->qf_next == NULL)
1490 {
1491 qf_ptr = old_qf_ptr;
1492 qf_index = prev_index;
1493 if (err != NULL)
1494 {
1495 EMSG(_(err));
1496 goto theend;
1497 }
1498 errornr = 0;
1499 break;
1500 }
1501 ++qf_index;
1502 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001503 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1504 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1506 err = NULL;
1507 }
1508 }
1509 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1510 {
1511 while (errornr--)
1512 {
1513 old_qf_ptr = qf_ptr;
1514 prev_index = qf_index;
1515 old_qf_fnum = qf_ptr->qf_fnum;
1516 do
1517 {
1518 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1519 {
1520 qf_ptr = old_qf_ptr;
1521 qf_index = prev_index;
1522 if (err != NULL)
1523 {
1524 EMSG(_(err));
1525 goto theend;
1526 }
1527 errornr = 0;
1528 break;
1529 }
1530 --qf_index;
1531 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1533 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1535 err = NULL;
1536 }
1537 }
1538 else if (errornr != 0) /* go to specified number */
1539 {
1540 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1541 {
1542 --qf_index;
1543 qf_ptr = qf_ptr->qf_prev;
1544 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001545 while (errornr > qf_index && qf_index <
1546 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 && qf_ptr->qf_next != NULL)
1548 {
1549 ++qf_index;
1550 qf_ptr = qf_ptr->qf_next;
1551 }
1552 }
1553
1554#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001555 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1556 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 /* No need to print the error message if it's visible in the error
1558 * window */
1559 print_message = FALSE;
1560
1561 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001562 * For ":helpgrep" find a help window or open one.
1563 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001564 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001565 {
1566 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001567
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001568 if (cmdmod.tab != 0)
1569 wp = NULL;
1570 else
1571 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1572 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1573 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001574 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1575 win_enter(wp, TRUE);
1576 else
1577 {
1578 /*
1579 * Split off help window; put it at far top if no position
1580 * specified, the current window is vertically split and narrow.
1581 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001582 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001583# ifdef FEAT_VERTSPLIT
1584 if (cmdmod.split == 0 && curwin->w_width != Columns
1585 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001586 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001587# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001588 if (qi != &ql_info)
1589 flags |= WSP_NEWLOC; /* don't copy the location list */
1590
1591 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001592 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001593 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001594
1595 if (curwin->w_height < p_hh)
1596 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001597
1598 if (qi != &ql_info) /* not a quickfix list */
1599 {
1600 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001601 curwin->w_llist = qi;
1602 qi->qf_refcount++;
1603 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001604 }
1605
1606 if (!p_im)
1607 restart_edit = 0; /* don't want insert mode in help file */
1608 }
1609
1610 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 * If currently in the quickfix window, find another window to show the
1612 * file in.
1613 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001614 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 /*
1617 * If there is no file specified, we don't know where to go.
1618 * But do advance, otherwise ":cn" gets stuck.
1619 */
1620 if (qf_ptr->qf_fnum == 0)
1621 goto theend;
1622
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001623 /* Locate a window showing a normal buffer */
1624 usable_win = 0;
1625 FOR_ALL_WINDOWS(win)
1626 if (win->w_buffer->b_p_bt[0] == NUL)
1627 {
1628 usable_win = 1;
1629 break;
1630 }
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001633 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001634 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001636 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001637 {
1638 tabpage_T *tp;
1639 win_T *wp;
1640
1641 FOR_ALL_TAB_WINDOWS(tp, wp)
1642 {
1643 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1644 {
1645 goto_tabpage_win(tp, wp);
1646 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001647 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001648 }
1649 }
1650 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001651win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001652
1653 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001654 * If there is only one window and it is the quickfix window, create a
1655 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001656 */
1657 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001659 ll_ref = curwin->w_llist_ref;
1660
Bram Moolenaar884ae642009-02-22 01:37:59 +00001661 flags = WSP_ABOVE;
1662 if (ll_ref != NULL)
1663 flags |= WSP_NEWLOC;
1664 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 goto failed; /* not enough room for window */
1666 opened_window = TRUE; /* close it when fail */
1667 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001668 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001669 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001670 if (ll_ref != NULL)
1671 {
1672 /* The new window should use the location list from the
1673 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001674 curwin->w_llist = ll_ref;
1675 ll_ref->qf_refcount++;
1676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
1678 else
1679 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001680 if (curwin->w_llist_ref != NULL)
1681 {
1682 /* In a location window */
1683 ll_ref = curwin->w_llist_ref;
1684
1685 /* Find the window with the same location list */
1686 FOR_ALL_WINDOWS(win)
1687 if (win->w_llist == ll_ref)
1688 break;
1689 if (win == NULL)
1690 {
1691 /* Find the window showing the selected file */
1692 FOR_ALL_WINDOWS(win)
1693 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1694 break;
1695 if (win == NULL)
1696 {
1697 /* Find a previous usable window */
1698 win = curwin;
1699 do
1700 {
1701 if (win->w_buffer->b_p_bt[0] == NUL)
1702 break;
1703 if (win->w_prev == NULL)
1704 win = lastwin; /* wrap around the top */
1705 else
1706 win = win->w_prev; /* go to previous window */
1707 } while (win != curwin);
1708 }
1709 }
1710 win_goto(win);
1711
1712 /* If the location list for the window is not set, then set it
1713 * to the location list from the location window */
1714 if (win->w_llist == NULL)
1715 {
1716 win->w_llist = ll_ref;
1717 ll_ref->qf_refcount++;
1718 }
1719 }
1720 else
1721 {
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 /*
1724 * Try to find a window that shows the right buffer.
1725 * Default to the window just above the quickfix buffer.
1726 */
1727 win = curwin;
1728 altwin = NULL;
1729 for (;;)
1730 {
1731 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1732 break;
1733 if (win->w_prev == NULL)
1734 win = lastwin; /* wrap around the top */
1735 else
1736 win = win->w_prev; /* go to previous window */
1737
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001738 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {
1740 /* Didn't find it, go to the window before the quickfix
1741 * window. */
1742 if (altwin != NULL)
1743 win = altwin;
1744 else if (curwin->w_prev != NULL)
1745 win = curwin->w_prev;
1746 else
1747 win = curwin->w_next;
1748 break;
1749 }
1750
1751 /* Remember a usable window. */
1752 if (altwin == NULL && !win->w_p_pvw
1753 && win->w_buffer->b_p_bt[0] == NUL)
1754 altwin = win;
1755 }
1756
1757 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 }
1761#endif
1762
1763 /*
1764 * If there is a file name,
1765 * read the wanted file if needed, and check autowrite etc.
1766 */
1767 old_curbuf = curbuf;
1768 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001769
1770 if (qf_ptr->qf_fnum != 0)
1771 {
1772 if (qf_ptr->qf_type == 1)
1773 {
1774 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1775 * set b_p_ro flag). */
1776 if (!can_abandon(curbuf, forceit))
1777 {
1778 EMSG(_(e_nowrtmsg));
1779 ok = FALSE;
1780 }
1781 else
1782 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001783 ECMD_HIDE + ECMD_SET_HELP,
1784 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001785 }
1786 else
1787 ok = buflist_getfile(qf_ptr->qf_fnum,
1788 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1789 }
1790
1791 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
1793 /* When not switched to another buffer, still need to set pc mark */
1794 if (curbuf == old_curbuf)
1795 setpcmark();
1796
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001797 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001799 /*
1800 * Go to line with error, unless qf_lnum is 0.
1801 */
1802 i = qf_ptr->qf_lnum;
1803 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001805 if (i > curbuf->b_ml.ml_line_count)
1806 i = curbuf->b_ml.ml_line_count;
1807 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001809 if (qf_ptr->qf_col > 0)
1810 {
1811 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1812 if (qf_ptr->qf_viscol == TRUE)
1813 {
1814 /*
1815 * Check each character from the beginning of the error
1816 * line up to the error column. For each tab character
1817 * found, reduce the error column value by the length of
1818 * a tab character.
1819 */
1820 line = ml_get_curline();
1821 screen_col = 0;
1822 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1823 {
1824 if (*line == NUL)
1825 break;
1826 if (*line++ == '\t')
1827 {
1828 curwin->w_cursor.col -= 7 - (screen_col % 8);
1829 screen_col += 8 - (screen_col % 8);
1830 }
1831 else
1832 ++screen_col;
1833 }
1834 }
1835 check_cursor();
1836 }
1837 else
1838 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001841 {
1842 pos_T save_cursor;
1843
1844 /* Move the cursor to the first line in the buffer */
1845 save_cursor = curwin->w_cursor;
1846 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001847 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1848 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001849 curwin->w_cursor = save_cursor;
1850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852#ifdef FEAT_FOLDING
1853 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1854 foldOpenCursor();
1855#endif
1856 if (print_message)
1857 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001858 /* Update the screen before showing the message, unless the screen
1859 * scrolled up. */
1860 if (!msg_scrolled)
1861 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001863 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1865 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1866 /* Add the message, skipping leading whitespace and newlines. */
1867 len = (int)STRLEN(IObuff);
1868 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1869
1870 /* Output the message. Overwrite to avoid scrolling when the 'O'
1871 * flag is present in 'shortmess'; But when not jumping, print the
1872 * whole message. */
1873 i = msg_scroll;
1874 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1875 msg_scroll = TRUE;
1876 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1877 msg_scroll = FALSE;
1878 msg_attr_keep(IObuff, 0, TRUE);
1879 msg_scroll = i;
1880 }
1881 }
1882 else
1883 {
1884#ifdef FEAT_WINDOWS
1885 if (opened_window)
1886 win_close(curwin, TRUE); /* Close opened window */
1887#endif
1888 if (qf_ptr->qf_fnum != 0)
1889 {
1890 /*
1891 * Couldn't open file, so put index back where it was. This could
1892 * happen if the file was readonly and we changed something.
1893 */
1894#ifdef FEAT_WINDOWS
1895failed:
1896#endif
1897 qf_ptr = old_qf_ptr;
1898 qf_index = old_qf_index;
1899 }
1900 }
1901theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001902 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1903 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904#ifdef FEAT_WINDOWS
1905 if (p_swb != old_swb && opened_window)
1906 {
1907 /* Restore old 'switchbuf' value, but not when an autocommand or
1908 * modeline has changed the value. */
1909 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001912 swb_flags = old_swb_flags;
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 else
1915 free_string_option(old_swb);
1916 }
1917#endif
1918}
1919
1920/*
1921 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001922 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
1924 void
1925qf_list(eap)
1926 exarg_T *eap;
1927{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001928 buf_T *buf;
1929 char_u *fname;
1930 qfline_T *qfp;
1931 int i;
1932 int idx1 = 1;
1933 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001934 char_u *arg = eap->arg;
1935 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001937 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939 if (eap->cmdidx == CMD_llist)
1940 {
1941 qi = GET_LOC_LIST(curwin);
1942 if (qi == NULL)
1943 {
1944 EMSG(_(e_loclist));
1945 return;
1946 }
1947 }
1948
1949 if (qi->qf_curlist >= qi->qf_listcount
1950 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952 EMSG(_(e_quickfix));
1953 return;
1954 }
1955 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1956 {
1957 EMSG(_(e_trailing));
1958 return;
1959 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001960 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if (idx1 < 0)
1962 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1963 if (idx2 < 0)
1964 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1965
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001966 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001968 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1969 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1972 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001973 msg_putchar('\n');
1974 if (got_int)
1975 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001976
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001977 fname = NULL;
1978 if (qfp->qf_fnum != 0
1979 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1980 {
1981 fname = buf->b_fname;
1982 if (qfp->qf_type == 1) /* :helpgrep */
1983 fname = gettail(fname);
1984 }
1985 if (fname == NULL)
1986 sprintf((char *)IObuff, "%2d", i);
1987 else
1988 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1989 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001991 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1992 if (qfp->qf_lnum == 0)
1993 IObuff[0] = NUL;
1994 else if (qfp->qf_col == 0)
1995 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1996 else
1997 sprintf((char *)IObuff, ":%ld col %d",
1998 qfp->qf_lnum, qfp->qf_col);
1999 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2000 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2001 msg_puts_attr(IObuff, hl_attr(HLF_N));
2002 if (qfp->qf_pattern != NULL)
2003 {
2004 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2005 STRCAT(IObuff, ":");
2006 msg_puts(IObuff);
2007 }
2008 msg_puts((char_u *)" ");
2009
2010 /* Remove newlines and leading whitespace from the text. For an
2011 * unrecognized line keep the indent, the compiler may mark a word
2012 * with ^^^^. */
2013 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2015 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002016 msg_prt_line(IObuff, FALSE);
2017 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002019
2020 qfp = qfp->qf_next;
2021 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 ui_breakcheck();
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024}
2025
2026/*
2027 * Remove newlines and leading whitespace from an error message.
2028 * Put the result in "buf[bufsize]".
2029 */
2030 static void
2031qf_fmt_text(text, buf, bufsize)
2032 char_u *text;
2033 char_u *buf;
2034 int bufsize;
2035{
2036 int i;
2037 char_u *p = text;
2038
2039 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2040 {
2041 if (*p == '\n')
2042 {
2043 buf[i] = ' ';
2044 while (*++p != NUL)
2045 if (!vim_iswhite(*p) && *p != '\n')
2046 break;
2047 }
2048 else
2049 buf[i] = *p++;
2050 }
2051 buf[i] = NUL;
2052}
2053
2054/*
2055 * ":colder [count]": Up in the quickfix stack.
2056 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002057 * ":lolder [count]": Up in the location list stack.
2058 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 */
2060 void
2061qf_age(eap)
2062 exarg_T *eap;
2063{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002064 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 int count;
2066
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002067 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2068 {
2069 qi = GET_LOC_LIST(curwin);
2070 if (qi == NULL)
2071 {
2072 EMSG(_(e_loclist));
2073 return;
2074 }
2075 }
2076
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 if (eap->addr_count != 0)
2078 count = eap->line2;
2079 else
2080 count = 1;
2081 while (count--)
2082 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002083 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002085 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
2087 EMSG(_("E380: At bottom of quickfix stack"));
2088 return;
2089 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
2092 else
2093 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002094 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
2096 EMSG(_("E381: At top of quickfix stack"));
2097 return;
2098 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002099 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002102 qf_msg(qi);
2103
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104}
2105
2106 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002107qf_msg(qi)
2108 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109{
2110 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111 qi->qf_curlist + 1, qi->qf_listcount,
2112 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002114 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115#endif
2116}
2117
2118/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002119 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 */
2121 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122qf_free(qi, idx)
2123 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 int idx;
2125{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002126 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002128 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002130 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002131 if (qi->qf_lists[idx].qf_title != NULL)
2132 {
2133 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2134 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2135 vim_free(qi->qf_lists[idx].qf_start);
2136 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002137 qi->qf_lists[idx].qf_start = qfp;
2138 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002140 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002141 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142}
2143
2144/*
2145 * qf_mark_adjust: adjust marks
2146 */
2147 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002148qf_mark_adjust(wp, line1, line2, amount, amount_after)
2149 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 linenr_T line1;
2151 linenr_T line2;
2152 long amount;
2153 long amount_after;
2154{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002155 int i;
2156 qfline_T *qfp;
2157 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 if (wp != NULL)
2161 {
2162 if (wp->w_llist == NULL)
2163 return;
2164 qi = wp->w_llist;
2165 }
2166
2167 for (idx = 0; idx < qi->qf_listcount; ++idx)
2168 if (qi->qf_lists[idx].qf_count)
2169 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2170 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (qfp->qf_fnum == curbuf->b_fnum)
2172 {
2173 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2174 {
2175 if (amount == MAXLNUM)
2176 qfp->qf_cleared = TRUE;
2177 else
2178 qfp->qf_lnum += amount;
2179 }
2180 else if (amount_after && qfp->qf_lnum > line2)
2181 qfp->qf_lnum += amount_after;
2182 }
2183}
2184
2185/*
2186 * Make a nice message out of the error character and the error number:
2187 * char number message
2188 * e or E 0 " error"
2189 * w or W 0 " warning"
2190 * i or I 0 " info"
2191 * 0 0 ""
2192 * other 0 " c"
2193 * e or E n " error n"
2194 * w or W n " warning n"
2195 * i or I n " info n"
2196 * 0 n " error n"
2197 * other n " c n"
2198 * 1 x "" :helpgrep
2199 */
2200 static char_u *
2201qf_types(c, nr)
2202 int c, nr;
2203{
2204 static char_u buf[20];
2205 static char_u cc[3];
2206 char_u *p;
2207
2208 if (c == 'W' || c == 'w')
2209 p = (char_u *)" warning";
2210 else if (c == 'I' || c == 'i')
2211 p = (char_u *)" info";
2212 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2213 p = (char_u *)" error";
2214 else if (c == 0 || c == 1)
2215 p = (char_u *)"";
2216 else
2217 {
2218 cc[0] = ' ';
2219 cc[1] = c;
2220 cc[2] = NUL;
2221 p = cc;
2222 }
2223
2224 if (nr <= 0)
2225 return p;
2226
2227 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2228 return buf;
2229}
2230
2231#if defined(FEAT_WINDOWS) || defined(PROTO)
2232/*
2233 * ":cwindow": open the quickfix window if we have errors to display,
2234 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002235 * ":lwindow": open the location list window if we have locations to display,
2236 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 */
2238 void
2239ex_cwindow(eap)
2240 exarg_T *eap;
2241{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002242 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 win_T *win;
2244
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002245 if (eap->cmdidx == CMD_lwindow)
2246 {
2247 qi = GET_LOC_LIST(curwin);
2248 if (qi == NULL)
2249 return;
2250 }
2251
2252 /* Look for an existing quickfix window. */
2253 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
2255 /*
2256 * If a quickfix window is open but we have no errors to display,
2257 * close the window. If a quickfix window is not open, then open
2258 * it if we have errors; otherwise, leave it closed.
2259 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002260 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002261 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002262 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
2264 if (win != NULL)
2265 ex_cclose(eap);
2266 }
2267 else if (win == NULL)
2268 ex_copen(eap);
2269}
2270
2271/*
2272 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002273 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 void
2276ex_cclose(eap)
2277 exarg_T *eap;
2278{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002279 win_T *win = NULL;
2280 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002282 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2283 {
2284 qi = GET_LOC_LIST(curwin);
2285 if (qi == NULL)
2286 return;
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002289 /* Find existing quickfix window and close it. */
2290 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (win != NULL)
2292 win_close(win, FALSE);
2293}
2294
2295/*
2296 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002297 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 */
2299 void
2300ex_copen(eap)
2301 exarg_T *eap;
2302{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002303 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002306 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002307 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002308 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002310 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2311 {
2312 qi = GET_LOC_LIST(curwin);
2313 if (qi == NULL)
2314 {
2315 EMSG(_(e_loclist));
2316 return;
2317 }
2318 }
2319
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (eap->addr_count != 0)
2321 height = eap->line2;
2322 else
2323 height = QF_WINHEIGHT;
2324
2325#ifdef FEAT_VISUAL
2326 reset_VIsual_and_resel(); /* stop Visual mode */
2327#endif
2328#ifdef FEAT_GUI
2329 need_mouse_correct = TRUE;
2330#endif
2331
2332 /*
2333 * Find existing quickfix window, or open a new one.
2334 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335 win = qf_find_win(qi);
2336
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002337 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 win_goto(win);
2339 else
2340 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002341 qf_buf = qf_find_buf(qi);
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 /* The current window becomes the previous window afterwards. */
2344 win = curwin;
2345
Bram Moolenaar77642c02012-11-20 17:55:10 +01002346 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2347 && cmdmod.split == 0)
2348 /* Create the new window at the very bottom, except when
2349 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002350 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002351 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002353 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002355 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002357 /*
2358 * For the location list window, create a reference to the
2359 * location list from the window 'win'.
2360 */
2361 curwin->w_llist_ref = win->w_llist;
2362 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002364
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002365 if (oldwin != curwin)
2366 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002367 if (qf_buf != NULL)
2368 /* Use the existing quickfix buffer */
2369 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002370 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002371 else
2372 {
2373 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002374 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002375 /* switch off 'swapfile' */
2376 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2377 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002378 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002379 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002380 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002381#ifdef FEAT_DIFF
2382 curwin->w_p_diff = FALSE;
2383#endif
2384#ifdef FEAT_FOLDING
2385 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2386 OPT_LOCAL);
2387#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002390 /* Only set the height when still in the same tab page and there is no
2391 * window to the side. */
2392 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002393#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002394 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002395#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002396 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 win_setheight(height);
2398 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2399 if (win_valid(win))
2400 prevwin = win;
2401 }
2402
2403 /*
2404 * Fill the buffer with the quickfix list.
2405 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002406 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002408 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002409 qf_set_title(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002410
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002411 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 curwin->w_cursor.col = 0;
2413 check_cursor();
2414 update_topline(); /* scroll to show the line */
2415}
2416
2417/*
2418 * Return the number of the current entry (line number in the quickfix
2419 * window).
2420 */
2421 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002422qf_current_entry(wp)
2423 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002425 qf_info_T *qi = &ql_info;
2426
2427 if (IS_LL_WINDOW(wp))
2428 /* In the location list window, use the referenced location list */
2429 qi = wp->w_llist_ref;
2430
2431 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432}
2433
2434/*
2435 * Update the cursor position in the quickfix window to the current error.
2436 * Return TRUE if there is a quickfix window.
2437 */
2438 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002439qf_win_pos_update(qi, old_qf_index)
2440 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 int old_qf_index; /* previous qf_index or zero */
2442{
2443 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002444 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 /*
2447 * Put the cursor on the current error in the quickfix window, so that
2448 * it's viewable.
2449 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002450 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 if (win != NULL
2452 && qf_index <= win->w_buffer->b_ml.ml_line_count
2453 && old_qf_index != qf_index)
2454 {
2455 win_T *old_curwin = curwin;
2456
2457 curwin = win;
2458 curbuf = win->w_buffer;
2459 if (qf_index > old_qf_index)
2460 {
2461 curwin->w_redraw_top = old_qf_index;
2462 curwin->w_redraw_bot = qf_index;
2463 }
2464 else
2465 {
2466 curwin->w_redraw_top = qf_index;
2467 curwin->w_redraw_bot = old_qf_index;
2468 }
2469 curwin->w_cursor.lnum = qf_index;
2470 curwin->w_cursor.col = 0;
2471 update_topline(); /* scroll to show the line */
2472 redraw_later(VALID);
2473 curwin->w_redr_status = TRUE; /* update ruler */
2474 curwin = old_curwin;
2475 curbuf = curwin->w_buffer;
2476 }
2477 return win != NULL;
2478}
2479
2480/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002481 * Check whether the given window is displaying the specified quickfix/location
2482 * list buffer
2483 */
2484 static int
2485is_qf_win(win, qi)
2486 win_T *win;
2487 qf_info_T *qi;
2488{
2489 /*
2490 * A window displaying the quickfix buffer will have the w_llist_ref field
2491 * set to NULL.
2492 * A window displaying a location list buffer will have the w_llist_ref
2493 * pointing to the location list.
2494 */
2495 if (bt_quickfix(win->w_buffer))
2496 if ((qi == &ql_info && win->w_llist_ref == NULL)
2497 || (qi != &ql_info && win->w_llist_ref == qi))
2498 return TRUE;
2499
2500 return FALSE;
2501}
2502
2503/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002504 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002505 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002506 */
2507 static win_T *
2508qf_find_win(qi)
2509 qf_info_T *qi;
2510{
2511 win_T *win;
2512
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002513 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002514 if (is_qf_win(win, qi))
2515 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002516
2517 return win;
2518}
2519
2520/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002521 * Find a quickfix buffer.
2522 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
2524 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002525qf_find_buf(qi)
2526 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002528 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002529 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar9c102382006-05-03 21:26:49 +00002531 FOR_ALL_TAB_WINDOWS(tp, win)
2532 if (is_qf_win(win, qi))
2533 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002534
Bram Moolenaar9c102382006-05-03 21:26:49 +00002535 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536}
2537
2538/*
2539 * Find the quickfix buffer. If it exists, update the contents.
2540 */
2541 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002542qf_update_buffer(qi)
2543 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002546 win_T *win;
2547 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002551 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (buf != NULL)
2553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 /* set curwin/curbuf to buf and save a few things */
2555 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002557 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002559 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2560 && (win = qf_find_win(qi)) != NULL)
2561 {
2562 curwin_save = curwin;
2563 curwin = win;
2564 qf_set_title(qi);
2565 curwin = curwin_save;
2566
2567 }
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 /* restore curwin/curbuf and a few other things */
2570 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002572 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574}
2575
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002576 static void
2577qf_set_title(qi)
2578 qf_info_T *qi;
2579{
2580 set_internal_string_var((char_u *)"w:quickfix_title",
2581 qi->qf_lists[qi->qf_curlist].qf_title);
2582}
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584/*
2585 * Fill current buffer with quickfix errors, replacing any previous contents.
2586 * curbuf must be the quickfix buffer!
2587 */
2588 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002589qf_fill_buffer(qi)
2590 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002592 linenr_T lnum;
2593 qfline_T *qfp;
2594 buf_T *errbuf;
2595 int len;
2596 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598 /* delete all existing lines */
2599 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2600 (void)ml_delete((linenr_T)1, FALSE);
2601
2602 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002603 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
2605 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002606 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2607 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 if (qfp->qf_fnum != 0
2610 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2611 && errbuf->b_fname != NULL)
2612 {
2613 if (qfp->qf_type == 1) /* :helpgrep */
2614 STRCPY(IObuff, gettail(errbuf->b_fname));
2615 else
2616 STRCPY(IObuff, errbuf->b_fname);
2617 len = (int)STRLEN(IObuff);
2618 }
2619 else
2620 len = 0;
2621 IObuff[len++] = '|';
2622
2623 if (qfp->qf_lnum > 0)
2624 {
2625 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2626 len += (int)STRLEN(IObuff + len);
2627
2628 if (qfp->qf_col > 0)
2629 {
2630 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2631 len += (int)STRLEN(IObuff + len);
2632 }
2633
2634 sprintf((char *)IObuff + len, "%s",
2635 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2636 len += (int)STRLEN(IObuff + len);
2637 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002638 else if (qfp->qf_pattern != NULL)
2639 {
2640 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2641 len += (int)STRLEN(IObuff + len);
2642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 IObuff[len++] = '|';
2644 IObuff[len++] = ' ';
2645
2646 /* Remove newlines and leading whitespace from the text.
2647 * For an unrecognized line keep the indent, the compiler may
2648 * mark a word with ^^^^. */
2649 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2650 IObuff + len, IOSIZE - len);
2651
2652 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2653 == FAIL)
2654 break;
2655 qfp = qfp->qf_next;
2656 }
2657 /* Delete the empty line which is now at the end */
2658 (void)ml_delete(lnum + 1, FALSE);
2659 }
2660
2661 /* correct cursor position */
2662 check_lnums(TRUE);
2663
2664 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2665 * resembles reading a file into a buffer, it's more logical when using
2666 * autocommands. */
2667 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2668 curbuf->b_p_ma = FALSE;
2669
2670#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002671 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2673 FALSE, curbuf);
2674 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2675 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002676 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677#endif
2678
2679 /* make sure it will be redrawn */
2680 redraw_curbuf_later(NOT_VALID);
2681
2682 /* Restore KeyTyped, setting 'filetype' may reset it. */
2683 KeyTyped = old_KeyTyped;
2684}
2685
2686#endif /* FEAT_WINDOWS */
2687
2688/*
2689 * Return TRUE if "buf" is the quickfix buffer.
2690 */
2691 int
2692bt_quickfix(buf)
2693 buf_T *buf;
2694{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002695 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696}
2697
2698/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002699 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2700 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 */
2702 int
2703bt_nofile(buf)
2704 buf_T *buf;
2705{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002706 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2707 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708}
2709
2710/*
2711 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2712 */
2713 int
2714bt_dontwrite(buf)
2715 buf_T *buf;
2716{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002717 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718}
2719
2720 int
2721bt_dontwrite_msg(buf)
2722 buf_T *buf;
2723{
2724 if (bt_dontwrite(buf))
2725 {
2726 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2727 return TRUE;
2728 }
2729 return FALSE;
2730}
2731
2732/*
2733 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2734 * and 'bufhidden'.
2735 */
2736 int
2737buf_hide(buf)
2738 buf_T *buf;
2739{
2740 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2741 switch (buf->b_p_bh[0])
2742 {
2743 case 'u': /* "unload" */
2744 case 'w': /* "wipe" */
2745 case 'd': return FALSE; /* "delete" */
2746 case 'h': return TRUE; /* "hide" */
2747 }
2748 return (p_hid || cmdmod.hide);
2749}
2750
2751/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002752 * Return TRUE when using ":vimgrep" for ":grep".
2753 */
2754 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002755grep_internal(cmdidx)
2756 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002757{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002758 return ((cmdidx == CMD_grep
2759 || cmdidx == CMD_lgrep
2760 || cmdidx == CMD_grepadd
2761 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002762 && STRCMP("internal",
2763 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2764}
2765
2766/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002767 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 */
2769 void
2770ex_make(eap)
2771 exarg_T *eap;
2772{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002773 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 char_u *cmd;
2775 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002776 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002777 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002778 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002779#ifdef FEAT_AUTOCMD
2780 char_u *au_name = NULL;
2781
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002782 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2783 if (grep_internal(eap->cmdidx))
2784 {
2785 ex_vimgrep(eap);
2786 return;
2787 }
2788
Bram Moolenaar7c626922005-02-07 22:01:03 +00002789 switch (eap->cmdidx)
2790 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002791 case CMD_make: au_name = (char_u *)"make"; break;
2792 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2793 case CMD_grep: au_name = (char_u *)"grep"; break;
2794 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2795 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2796 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002797 default: break;
2798 }
2799 if (au_name != NULL)
2800 {
2801 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2802 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002803# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002804 if (did_throw || force_abort)
2805 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002806# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002807 }
2808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
Bram Moolenaara6557602006-02-04 22:43:20 +00002810 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2811 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002812 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002813
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002815 fname = get_mef_name();
2816 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002818 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
2820 /*
2821 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2822 */
2823 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2824 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002825 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 cmd = alloc(len);
2827 if (cmd == NULL)
2828 return;
2829 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2830 (char *)p_shq);
2831 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002832 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 /*
2834 * Output a newline if there's something else than the :make command that
2835 * was typed (in which case the cursor is in column 0).
2836 */
2837 if (msg_col == 0)
2838 msg_didout = FALSE;
2839 msg_start();
2840 MSG_PUTS(":!");
2841 msg_outtrans(cmd); /* show what we are doing */
2842
2843 /* let the shell know if we are redirecting output or not */
2844 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2845
2846#ifdef AMIGA
2847 out_flush();
2848 /* read window status report and redraw before message */
2849 (void)char_avail();
2850#endif
2851
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002852 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002853 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2854 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002855 && eap->cmdidx != CMD_lgrepadd),
2856 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002857 if (wp != NULL)
2858 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002859#ifdef FEAT_AUTOCMD
2860 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002861 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002862 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2863 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002864 if (qi->qf_curlist < qi->qf_listcount)
2865 res = qi->qf_lists[qi->qf_curlist].qf_count;
2866 else
2867 res = 0;
2868 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002869#endif
2870 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002871 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar7c626922005-02-07 22:01:03 +00002873 mch_remove(fname);
2874 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 vim_free(cmd);
2876}
2877
2878/*
2879 * Return the name for the errorfile, in allocated memory.
2880 * Find a new unique name when 'makeef' contains "##".
2881 * Returns NULL for error.
2882 */
2883 static char_u *
2884get_mef_name()
2885{
2886 char_u *p;
2887 char_u *name;
2888 static int start = -1;
2889 static int off = 0;
2890#ifdef HAVE_LSTAT
2891 struct stat sb;
2892#endif
2893
2894 if (*p_mef == NUL)
2895 {
2896 name = vim_tempname('e');
2897 if (name == NULL)
2898 EMSG(_(e_notmp));
2899 return name;
2900 }
2901
2902 for (p = p_mef; *p; ++p)
2903 if (p[0] == '#' && p[1] == '#')
2904 break;
2905
2906 if (*p == NUL)
2907 return vim_strsave(p_mef);
2908
2909 /* Keep trying until the name doesn't exist yet. */
2910 for (;;)
2911 {
2912 if (start == -1)
2913 start = mch_get_pid();
2914 else
2915 off += 19;
2916
2917 name = alloc((unsigned)STRLEN(p_mef) + 30);
2918 if (name == NULL)
2919 break;
2920 STRCPY(name, p_mef);
2921 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2922 STRCAT(name, p + 2);
2923 if (mch_getperm(name) < 0
2924#ifdef HAVE_LSTAT
2925 /* Don't accept a symbolic link, its a security risk. */
2926 && mch_lstat((char *)name, &sb) < 0
2927#endif
2928 )
2929 break;
2930 vim_free(name);
2931 }
2932 return name;
2933}
2934
2935/*
2936 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002937 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 */
2939 void
2940ex_cc(eap)
2941 exarg_T *eap;
2942{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002943 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002944
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002945 if (eap->cmdidx == CMD_ll
2946 || eap->cmdidx == CMD_lrewind
2947 || eap->cmdidx == CMD_lfirst
2948 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002949 {
2950 qi = GET_LOC_LIST(curwin);
2951 if (qi == NULL)
2952 {
2953 EMSG(_(e_loclist));
2954 return;
2955 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002956 }
2957
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002958 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 eap->addr_count > 0
2960 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002961 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002963 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2964 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 ? 1
2966 : 32767,
2967 eap->forceit);
2968}
2969
2970/*
2971 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002972 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 */
2974 void
2975ex_cnext(eap)
2976 exarg_T *eap;
2977{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002978 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002980 if (eap->cmdidx == CMD_lnext
2981 || eap->cmdidx == CMD_lNext
2982 || eap->cmdidx == CMD_lprevious
2983 || eap->cmdidx == CMD_lnfile
2984 || eap->cmdidx == CMD_lNfile
2985 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002986 {
2987 qi = GET_LOC_LIST(curwin);
2988 if (qi == NULL)
2989 {
2990 EMSG(_(e_loclist));
2991 return;
2992 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002993 }
2994
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002995 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002997 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002999 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3000 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 ? BACKWARD_FILE
3002 : BACKWARD,
3003 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
3004}
3005
3006/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003007 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003008 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 */
3010 void
3011ex_cfile(eap)
3012 exarg_T *eap;
3013{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003014 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003015 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003016#ifdef FEAT_AUTOCMD
3017 char_u *au_name = NULL;
3018#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003019
3020 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003021 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003022 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003023
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003024#ifdef FEAT_AUTOCMD
3025 switch (eap->cmdidx)
3026 {
3027 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3028 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3029 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3030 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3031 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3032 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3033 default: break;
3034 }
3035 if (au_name != NULL)
3036 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3037#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003038#ifdef FEAT_BROWSE
3039 if (cmdmod.browse)
3040 {
3041 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3042 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3043 if (browse_file == NULL)
3044 return;
3045 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3046 vim_free(browse_file);
3047 }
3048 else
3049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003051 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003052
3053 /*
3054 * This function is used by the :cfile, :cgetfile and :caddfile
3055 * commands.
3056 * :cfile always creates a new quickfix list and jumps to the
3057 * first error.
3058 * :cgetfile creates a new quickfix list but doesn't jump to the
3059 * first error.
3060 * :caddfile adds to an existing quickfix list. If there is no
3061 * quickfix list then a new list is created.
3062 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003063 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003064 && eap->cmdidx != CMD_laddfile),
3065 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003066 && (eap->cmdidx == CMD_cfile
3067 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003068 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003069#ifdef FEAT_AUTOCMD
3070 if (au_name != NULL)
3071 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3072#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003073 if (wp != NULL)
3074 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003075 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003076 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003077
3078 else
3079 {
3080#ifdef FEAT_AUTOCMD
3081 if (au_name != NULL)
3082 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3083#endif
3084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085}
3086
3087/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003088 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003089 * ":vimgrepadd {pattern} file(s)"
3090 * ":lvimgrep {pattern} file(s)"
3091 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003092 */
3093 void
3094ex_vimgrep(eap)
3095 exarg_T *eap;
3096{
Bram Moolenaar81695252004-12-29 20:58:21 +00003097 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003098 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003099 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003100 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003101 char_u *s;
3102 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003103 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003104 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003105 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003106 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003107 buf_T *buf;
3108 int duplicate_name = FALSE;
3109 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003110 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003111 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003112 buf_T *first_match_buf = NULL;
3113 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003114 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003115#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3116 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003117#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003118 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003119 int flags = 0;
3120 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003121 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003122 char_u *dirname_start = NULL;
3123 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003124 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003125#ifdef FEAT_AUTOCMD
3126 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003127
3128 switch (eap->cmdidx)
3129 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003130 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3131 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3132 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003133 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003134 case CMD_grep: au_name = (char_u *)"grep"; break;
3135 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3136 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3137 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003138 default: break;
3139 }
3140 if (au_name != NULL)
3141 {
3142 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3143 curbuf->b_fname, TRUE, curbuf);
3144 if (did_throw || force_abort)
3145 return;
3146 }
3147#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003148
Bram Moolenaar754b5602006-02-09 23:53:20 +00003149 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003150 || eap->cmdidx == CMD_lvimgrep
3151 || eap->cmdidx == CMD_lgrepadd
3152 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003153 {
3154 qi = ll_get_or_alloc_list(curwin);
3155 if (qi == NULL)
3156 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003157 }
3158
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003159 if (eap->addr_count > 0)
3160 tomatch = eap->line2;
3161 else
3162 tomatch = MAXLNUM;
3163
Bram Moolenaar81695252004-12-29 20:58:21 +00003164 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003165 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003166 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003167 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003168 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003169 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003170 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003171 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003172 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003173 if (regmatch.regprog == NULL)
3174 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003175 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003176 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003177
3178 p = skipwhite(p);
3179 if (*p == NUL)
3180 {
3181 EMSG(_("E683: File name missing or invalid pattern"));
3182 goto theend;
3183 }
3184
Bram Moolenaar754b5602006-02-09 23:53:20 +00003185 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003186 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003187 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003188 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003189 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003190 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003191 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003192 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003193 prevp->qf_next != prevp; prevp = prevp->qf_next)
3194 ;
3195
3196 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003197 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003198 goto theend;
3199 if (fcount == 0)
3200 {
3201 EMSG(_(e_nomatch));
3202 goto theend;
3203 }
3204
Bram Moolenaard9462e32011-04-11 21:35:11 +02003205 dirname_start = alloc(MAXPATHL);
3206 dirname_now = alloc(MAXPATHL);
3207 if (dirname_start == NULL || dirname_now == NULL)
3208 goto theend;
3209
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003210 /* Remember the current directory, because a BufRead autocommand that does
3211 * ":lcd %:p:h" changes the meaning of short path names. */
3212 mch_dirname(dirname_start, MAXPATHL);
3213
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003214 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003215 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003216 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003217 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003218 if (time(NULL) > seconds)
3219 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003220 /* Display the file name every second or so, show the user we are
3221 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003222 seconds = time(NULL);
3223 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003224 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003225 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003226 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003227 else
3228 {
3229 msg_outtrans(p);
3230 vim_free(p);
3231 }
3232 msg_clr_eos();
3233 msg_didout = FALSE; /* overwrite this message */
3234 msg_nowait = TRUE; /* don't wait for this message */
3235 msg_col = 0;
3236 out_flush();
3237 }
3238
Bram Moolenaar81695252004-12-29 20:58:21 +00003239 buf = buflist_findname_exp(fnames[fi]);
3240 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3241 {
3242 /* Remember that a buffer with this name already exists. */
3243 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003244 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003245 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003246
3247#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3248 /* Don't do Filetype autocommands to avoid loading syntax and
3249 * indent scripts, a great speed improvement. */
3250 save_ei = au_event_disable(",Filetype");
3251#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003252 /* Don't use modelines here, it's useless. */
3253 save_mls = p_mls;
3254 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003255
3256 /* Load file into a buffer, so that 'fileencoding' is detected,
3257 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003258 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003259
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003260 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003261#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3262 au_event_restore(save_ei);
3263#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003264 }
3265 else
3266 /* Use existing, loaded buffer. */
3267 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003268
Bram Moolenaar81695252004-12-29 20:58:21 +00003269 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003270 {
3271 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003272 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003273 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003274 else
3275 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003276 /* Try for a match in all lines of the buffer.
3277 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003278 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003279 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3280 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003281 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003282 col = 0;
3283 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003284 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003285 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003286 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003287 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003288 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003289 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003290 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003291 ml_get_buf(buf,
3292 regmatch.startpos[0].lnum + lnum, FALSE),
3293 regmatch.startpos[0].lnum + lnum,
3294 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003295 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003296 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003297 0, /* nr */
3298 0, /* type */
3299 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003300 ) == FAIL)
3301 {
3302 got_int = TRUE;
3303 break;
3304 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003305 found_match = TRUE;
3306 if (--tomatch == 0)
3307 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003308 if ((flags & VGR_GLOBAL) == 0
3309 || regmatch.endpos[0].lnum > 0)
3310 break;
3311 col = regmatch.endpos[0].col
3312 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003313 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003314 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003315 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003316 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003317 if (got_int)
3318 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003319 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003320
3321 if (using_dummy)
3322 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003323 if (found_match && first_match_buf == NULL)
3324 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003325 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003326 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003327 /* Never keep a dummy buffer if there is another buffer
3328 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003329 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003330 buf = NULL;
3331 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003332 else if (!cmdmod.hide
3333 || buf->b_p_bh[0] == 'u' /* "unload" */
3334 || buf->b_p_bh[0] == 'w' /* "wipe" */
3335 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003336 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003337 /* When no match was found we don't need to remember the
3338 * buffer, wipe it out. If there was a match and it
3339 * wasn't the first one or we won't jump there: only
3340 * unload the buffer.
3341 * Ignore 'hidden' here, because it may lead to having too
3342 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003343 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003344 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003345 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003346 buf = NULL;
3347 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003348 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003349 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003350 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003351 buf = NULL;
3352 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003353 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003354
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003355 if (buf != NULL)
3356 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003357 /* If the buffer is still loaded we need to use the
3358 * directory we jumped to below. */
3359 if (buf == first_match_buf
3360 && target_dir == NULL
3361 && STRCMP(dirname_start, dirname_now) != 0)
3362 target_dir = vim_strsave(dirname_now);
3363
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003364 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003365 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003366 * need to be done (again). But not the window-local
3367 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003368 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003369#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003370 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3371 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003372#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003373 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003374 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003375 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003376 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003377 }
3378 }
3379
3380 FreeWild(fcount, fnames);
3381
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003382 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3383 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3384 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003385
3386#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003387 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003388#endif
3389
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003390#ifdef FEAT_AUTOCMD
3391 if (au_name != NULL)
3392 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3393 curbuf->b_fname, TRUE, curbuf);
3394#endif
3395
Bram Moolenaar86b68352004-12-27 21:59:20 +00003396 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003397 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003398 {
3399 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003400 {
3401 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003402 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003403 if (buf != curbuf)
3404 /* If we jumped to another buffer redrawing will already be
3405 * taken care of. */
3406 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003407
3408 /* Jump to the directory used after loading the buffer. */
3409 if (curbuf == first_match_buf && target_dir != NULL)
3410 {
3411 exarg_T ea;
3412
3413 ea.arg = target_dir;
3414 ea.cmdidx = CMD_lcd;
3415 ex_cd(&ea);
3416 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003417 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003418 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003419 else
3420 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003421
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003422 /* If we loaded a dummy buffer into the current window, the autocommands
3423 * may have messed up things, need to redraw and recompute folds. */
3424 if (redraw_for_dummy)
3425 {
3426#ifdef FEAT_FOLDING
3427 foldUpdateAll(curwin);
3428#else
3429 redraw_later(NOT_VALID);
3430#endif
3431 }
3432
Bram Moolenaar86b68352004-12-27 21:59:20 +00003433theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003434 vim_free(dirname_now);
3435 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003436 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003437 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003438}
3439
3440/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003441 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003442 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003443 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3444 * If "s" is not NULL terminate the pattern with a NUL.
3445 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003446 */
3447 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003448skip_vimgrep_pat(p, s, flags)
3449 char_u *p;
3450 char_u **s;
3451 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003452{
3453 int c;
3454
3455 if (vim_isIDc(*p))
3456 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003457 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003458 if (s != NULL)
3459 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003460 p = skiptowhite(p);
3461 if (s != NULL && *p != NUL)
3462 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003463 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003464 else
3465 {
3466 /* ":vimgrep /pattern/[g][j] fname" */
3467 if (s != NULL)
3468 *s = p + 1;
3469 c = *p;
3470 p = skip_regexp(p + 1, c, TRUE, NULL);
3471 if (*p != c)
3472 return NULL;
3473
3474 /* Truncate the pattern. */
3475 if (s != NULL)
3476 *p = NUL;
3477 ++p;
3478
3479 /* Find the flags */
3480 while (*p == 'g' || *p == 'j')
3481 {
3482 if (flags != NULL)
3483 {
3484 if (*p == 'g')
3485 *flags |= VGR_GLOBAL;
3486 else
3487 *flags |= VGR_NOJUMP;
3488 }
3489 ++p;
3490 }
3491 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003492 return p;
3493}
3494
3495/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003496 * Restore current working directory to "dirname_start" if they differ, taking
3497 * into account whether it is set locally or globally.
3498 */
3499 static void
3500restore_start_dir(dirname_start)
3501 char_u *dirname_start;
3502{
3503 char_u *dirname_now = alloc(MAXPATHL);
3504
3505 if (NULL != dirname_now)
3506 {
3507 mch_dirname(dirname_now, MAXPATHL);
3508 if (STRCMP(dirname_start, dirname_now) != 0)
3509 {
3510 /* If the directory has changed, change it back by building up an
3511 * appropriate ex command and executing it. */
3512 exarg_T ea;
3513
3514 ea.arg = dirname_start;
3515 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3516 ex_cd(&ea);
3517 }
3518 }
3519}
3520
3521/*
3522 * Load file "fname" into a dummy buffer and return the buffer pointer,
3523 * placing the directory resulting from the buffer load into the
3524 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3525 * prior to calling this function. Restores directory to "dirname_start" prior
3526 * to returning, if autocmds or the 'autochdir' option have changed it.
3527 *
3528 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3529 * or wipe_dummy_buffer() later!
3530 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003531 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003532 */
3533 static buf_T *
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003534load_dummy_buffer(fname, dirname_start, resulting_dir)
Bram Moolenaar81695252004-12-29 20:58:21 +00003535 char_u *fname;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003536 char_u *dirname_start; /* in: old directory */
3537 char_u *resulting_dir; /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003538{
3539 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003540 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003541 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003542 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003543
3544 /* Allocate a buffer without putting it in the buffer list. */
3545 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3546 if (newbuf == NULL)
3547 return NULL;
3548
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003549 /* Init the options. */
3550 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3551
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003552 /* need to open the memfile before putting the buffer in a window */
3553 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003554 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003555 /* set curwin/curbuf to buf and save a few things */
3556 aucmd_prepbuf(&aco, newbuf);
3557
3558 /* Need to set the filename for autocommands. */
3559 (void)setfname(curbuf, fname, NULL, FALSE);
3560
Bram Moolenaar81695252004-12-29 20:58:21 +00003561 /* Create swap file now to avoid the ATTENTION message. */
3562 check_need_swap(TRUE);
3563
3564 /* Remove the "dummy" flag, otherwise autocommands may not
3565 * work. */
3566 curbuf->b_flags &= ~BF_DUMMY;
3567
3568 if (readfile(fname, NULL,
3569 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3570 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003571 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003572 && !(curbuf->b_flags & BF_NEW))
3573 {
3574 failed = FALSE;
3575 if (curbuf != newbuf)
3576 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003577 /* Bloody autocommands changed the buffer! Can happen when
3578 * using netrw and editing a remote file. Use the current
3579 * buffer instead, delete the dummy one after restoring the
3580 * window stuff. */
3581 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003582 newbuf = curbuf;
3583 }
3584 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003585
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003586 /* restore curwin/curbuf and a few other things */
3587 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003588 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3589 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003590 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003591
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003592 /*
3593 * When autocommands/'autochdir' option changed directory: go back.
3594 * Let the caller know what the resulting dir was first, in case it is
3595 * important.
3596 */
3597 mch_dirname(resulting_dir, MAXPATHL);
3598 restore_start_dir(dirname_start);
3599
Bram Moolenaar81695252004-12-29 20:58:21 +00003600 if (!buf_valid(newbuf))
3601 return NULL;
3602 if (failed)
3603 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003604 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003605 return NULL;
3606 }
3607 return newbuf;
3608}
3609
3610/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003611 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3612 * directory to "dirname_start" prior to returning, if autocmds or the
3613 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003614 */
3615 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003616wipe_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003617 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003618 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003619{
3620 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003621 {
3622#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3623 cleanup_T cs;
3624
3625 /* Reset the error/interrupt/exception state here so that aborting()
3626 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3627 * work when got_int is set. */
3628 enter_cleanup(&cs);
3629#endif
3630
Bram Moolenaar81695252004-12-29 20:58:21 +00003631 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003632
3633#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3634 /* Restore the error/interrupt/exception state if not discarded by a
3635 * new aborting error, interrupt, or uncaught exception. */
3636 leave_cleanup(&cs);
3637#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003638 /* When autocommands/'autochdir' option changed directory: go back. */
3639 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003640 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003641}
3642
3643/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003644 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3645 * directory to "dirname_start" prior to returning, if autocmds or the
3646 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003647 */
3648 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003649unload_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003650 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003651 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003652{
3653 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003654 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003655 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003656
3657 /* When autocommands/'autochdir' option changed directory: go back. */
3658 restore_start_dir(dirname_start);
3659 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003660}
3661
Bram Moolenaar05159a02005-02-26 23:04:13 +00003662#if defined(FEAT_EVAL) || defined(PROTO)
3663/*
3664 * Add each quickfix error to list "list" as a dictionary.
3665 */
3666 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003667get_errorlist(wp, list)
3668 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003669 list_T *list;
3670{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003671 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003672 dict_T *dict;
3673 char_u buf[2];
3674 qfline_T *qfp;
3675 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003676 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003677
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003678 if (wp != NULL)
3679 {
3680 qi = GET_LOC_LIST(wp);
3681 if (qi == NULL)
3682 return FAIL;
3683 }
3684
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003685 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003686 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003687 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003688
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003689 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3690 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003691 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003692 /* Handle entries with a non-existing buffer number. */
3693 bufnum = qfp->qf_fnum;
3694 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3695 bufnum = 0;
3696
Bram Moolenaar05159a02005-02-26 23:04:13 +00003697 if ((dict = dict_alloc()) == NULL)
3698 return FAIL;
3699 if (list_append_dict(list, dict) == FAIL)
3700 return FAIL;
3701
3702 buf[0] = qfp->qf_type;
3703 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003704 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003705 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3706 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3707 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3708 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003709 || dict_add_nr_str(dict, "pattern", 0L,
3710 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3711 || dict_add_nr_str(dict, "text", 0L,
3712 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003713 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3714 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3715 return FAIL;
3716
3717 qfp = qfp->qf_next;
3718 }
3719 return OK;
3720}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003721
3722/*
3723 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003724 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003725 */
3726 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003727set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003728 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003729 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003730 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003731 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003732{
3733 listitem_T *li;
3734 dict_T *d;
3735 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003736 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003737 long lnum;
3738 int col, nr;
3739 int vcol;
3740 qfline_T *prevp = NULL;
3741 int valid, status;
3742 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003743 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003744 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003745
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003746 if (wp != NULL)
3747 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003748 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003749 if (qi == NULL)
3750 return FAIL;
3751 }
3752
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003753 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003754 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003755 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003756 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003757 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003758 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003759 prevp->qf_next != prevp; prevp = prevp->qf_next)
3760 ;
3761 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003762 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003763
3764 for (li = list->lv_first; li != NULL; li = li->li_next)
3765 {
3766 if (li->li_tv.v_type != VAR_DICT)
3767 continue; /* Skip non-dict items */
3768
3769 d = li->li_tv.vval.v_dict;
3770 if (d == NULL)
3771 continue;
3772
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003773 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003774 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003775 lnum = get_dict_number(d, (char_u *)"lnum");
3776 col = get_dict_number(d, (char_u *)"col");
3777 vcol = get_dict_number(d, (char_u *)"vcol");
3778 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003779 type = get_dict_string(d, (char_u *)"type", TRUE);
3780 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3781 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003782 if (text == NULL)
3783 text = vim_strsave((char_u *)"");
3784
3785 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003786 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003787 valid = FALSE;
3788
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003789 /* Mark entries with non-existing buffer number as not valid. Give the
3790 * error message only once. */
3791 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3792 {
3793 if (!did_bufnr_emsg)
3794 {
3795 did_bufnr_emsg = TRUE;
3796 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3797 }
3798 valid = FALSE;
3799 bufnum = 0;
3800 }
3801
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003803 NULL, /* dir */
3804 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003805 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003806 text,
3807 lnum,
3808 col,
3809 vcol, /* vis_col */
3810 pattern, /* search pattern */
3811 nr,
3812 type == NULL ? NUL : *type,
3813 valid);
3814
3815 vim_free(filename);
3816 vim_free(pattern);
3817 vim_free(text);
3818 vim_free(type);
3819
3820 if (status == FAIL)
3821 {
3822 retval = FAIL;
3823 break;
3824 }
3825 }
3826
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003827 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003828 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003829 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3830 else
3831 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003832 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3833 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003834
3835#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003836 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003837#endif
3838
3839 return retval;
3840}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003841#endif
3842
Bram Moolenaar81695252004-12-29 20:58:21 +00003843/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003844 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003845 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003846 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003847 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003848 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003849 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003850 */
3851 void
3852ex_cbuffer(eap)
3853 exarg_T *eap;
3854{
3855 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856 qf_info_T *qi = &ql_info;
3857
Bram Moolenaardb552d602006-03-23 22:59:57 +00003858 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3859 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860 {
3861 qi = ll_get_or_alloc_list(curwin);
3862 if (qi == NULL)
3863 return;
3864 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003865
3866 if (*eap->arg == NUL)
3867 buf = curbuf;
3868 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3869 buf = buflist_findnr(atoi((char *)eap->arg));
3870 if (buf == NULL)
3871 EMSG(_(e_invarg));
3872 else if (buf->b_ml.ml_mfp == NULL)
3873 EMSG(_("E681: Buffer is not loaded"));
3874 else
3875 {
3876 if (eap->addr_count == 0)
3877 {
3878 eap->line1 = 1;
3879 eap->line2 = buf->b_ml.ml_line_count;
3880 }
3881 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3882 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3883 EMSG(_(e_invrange));
3884 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003885 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003886 char_u *qf_title = *eap->cmdlinep;
3887
3888 if (buf->b_sfname)
3889 {
3890 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3891 (char *)qf_title, (char *)buf->b_sfname);
3892 qf_title = IObuff;
3893 }
3894
Bram Moolenaardb552d602006-03-23 22:59:57 +00003895 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3896 (eap->cmdidx != CMD_caddbuffer
3897 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003898 eap->line1, eap->line2,
3899 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003900 && (eap->cmdidx == CMD_cbuffer
3901 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003902 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3903 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003904 }
3905}
3906
Bram Moolenaar1e015462005-09-25 22:16:38 +00003907#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003908/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003909 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3910 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003911 */
3912 void
3913ex_cexpr(eap)
3914 exarg_T *eap;
3915{
3916 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003917 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003918
Bram Moolenaardb552d602006-03-23 22:59:57 +00003919 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3920 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003921 {
3922 qi = ll_get_or_alloc_list(curwin);
3923 if (qi == NULL)
3924 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003925 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003926
Bram Moolenaar4770d092006-01-12 23:22:24 +00003927 /* Evaluate the expression. When the result is a string or a list we can
3928 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003929 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003930 if (tv != NULL)
3931 {
3932 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3933 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3934 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003935 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3936 (eap->cmdidx != CMD_caddexpr
3937 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003938 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003939 && (eap->cmdidx == CMD_cexpr
3940 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003941 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003942 }
3943 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003944 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003945 free_tv(tv);
3946 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003947}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003948#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003949
3950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 * ":helpgrep {pattern}"
3952 */
3953 void
3954ex_helpgrep(eap)
3955 exarg_T *eap;
3956{
3957 regmatch_T regmatch;
3958 char_u *save_cpo;
3959 char_u *p;
3960 int fcount;
3961 char_u **fnames;
3962 FILE *fd;
3963 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003964 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003966#ifdef FEAT_MULTI_LANG
3967 char_u *lang;
3968#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003969 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003970 int new_qi = FALSE;
3971 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01003972#ifdef FEAT_AUTOCMD
3973 char_u *au_name = NULL;
3974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003976#ifdef FEAT_MULTI_LANG
3977 /* Check for a specified language */
3978 lang = check_help_lang(eap->arg);
3979#endif
3980
Bram Moolenaar73633f82012-01-20 13:39:07 +01003981#ifdef FEAT_AUTOCMD
3982 switch (eap->cmdidx)
3983 {
3984 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
3985 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
3986 default: break;
3987 }
3988 if (au_name != NULL)
3989 {
3990 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3991 curbuf->b_fname, TRUE, curbuf);
3992 if (did_throw || force_abort)
3993 return;
3994 }
3995#endif
3996
3997 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3998 save_cpo = p_cpo;
3999 p_cpo = empty_option;
4000
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004001 if (eap->cmdidx == CMD_lhelpgrep)
4002 {
4003 /* Find an existing help window */
4004 FOR_ALL_WINDOWS(wp)
4005 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4006 break;
4007
4008 if (wp == NULL) /* Help window not found */
4009 qi = NULL;
4010 else
4011 qi = wp->w_llist;
4012
4013 if (qi == NULL)
4014 {
4015 /* Allocate a new location list for help text matches */
4016 if ((qi = ll_new_list()) == NULL)
4017 return;
4018 new_qi = TRUE;
4019 }
4020 }
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4023 regmatch.rm_ic = FALSE;
4024 if (regmatch.regprog != NULL)
4025 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004026#ifdef FEAT_MBYTE
4027 vimconv_T vc;
4028
4029 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4030 * differs. */
4031 vc.vc_type = CONV_NONE;
4032 if (!enc_utf8)
4033 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4034#endif
4035
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004037 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
4039 /* Go through all directories in 'runtimepath' */
4040 p = p_rtp;
4041 while (*p != NUL && !got_int)
4042 {
4043 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4044
4045 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4046 add_pathsep(NameBuff);
4047 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4048 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4049 &fnames, EW_FILE|EW_SILENT) == OK
4050 && fcount > 0)
4051 {
4052 for (fi = 0; fi < fcount && !got_int; ++fi)
4053 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004054#ifdef FEAT_MULTI_LANG
4055 /* Skip files for a different language. */
4056 if (lang != NULL
4057 && STRNICMP(lang, fnames[fi]
4058 + STRLEN(fnames[fi]) - 3, 2) != 0
4059 && !(STRNICMP(lang, "en", 2) == 0
4060 && STRNICMP("txt", fnames[fi]
4061 + STRLEN(fnames[fi]) - 3, 3) == 0))
4062 continue;
4063#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004064 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (fd != NULL)
4066 {
4067 lnum = 1;
4068 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4069 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004070 char_u *line = IObuff;
4071#ifdef FEAT_MBYTE
4072 /* Convert a line if 'encoding' is not utf-8 and
4073 * the line contains a non-ASCII character. */
4074 if (vc.vc_type != CONV_NONE
4075 && has_non_ascii(IObuff)) {
4076 line = string_convert(&vc, IObuff, NULL);
4077 if (line == NULL)
4078 line = IObuff;
4079 }
4080#endif
4081
4082 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004084 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004087 while (l > 0 && line[l - 1] <= ' ')
4088 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004090 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 NULL, /* dir */
4092 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004093 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004094 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004096 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004097 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004098 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004099 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 0, /* nr */
4101 1, /* type */
4102 TRUE /* valid */
4103 ) == FAIL)
4104 {
4105 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004106#ifdef FEAT_MBYTE
4107 if (line != IObuff)
4108 vim_free(line);
4109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 break;
4111 }
4112 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004113#ifdef FEAT_MBYTE
4114 if (line != IObuff)
4115 vim_free(line);
4116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 ++lnum;
4118 line_breakcheck();
4119 }
4120 fclose(fd);
4121 }
4122 }
4123 FreeWild(fcount, fnames);
4124 }
4125 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 vim_free(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004128#ifdef FEAT_MBYTE
4129 if (vc.vc_type != CONV_NONE)
4130 convert_setup(&vc, NULL, NULL);
4131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004133 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4134 qi->qf_lists[qi->qf_curlist].qf_ptr =
4135 qi->qf_lists[qi->qf_curlist].qf_start;
4136 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004139 if (p_cpo == empty_option)
4140 p_cpo = save_cpo;
4141 else
4142 /* Darn, some plugin changed the value. */
4143 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004146 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147#endif
4148
Bram Moolenaar73633f82012-01-20 13:39:07 +01004149#ifdef FEAT_AUTOCMD
4150 if (au_name != NULL)
4151 {
4152 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4153 curbuf->b_fname, TRUE, curbuf);
4154 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4155 /* autocommands made "qi" invalid */
4156 return;
4157 }
4158#endif
4159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004161 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004162 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004163 else
4164 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004165
4166 if (eap->cmdidx == CMD_lhelpgrep)
4167 {
4168 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004169 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004170 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4171 {
4172 if (new_qi)
4173 ll_free_all(&qi);
4174 }
4175 else if (curwin->w_llist == NULL)
4176 curwin->w_llist = qi;
4177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178}
4179
4180#endif /* FEAT_QUICKFIX */