blob: 17d85837cb82f081b0ddcf7a608c16c14c76e4f1 [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));
110static 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));
129static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
131static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000132static buf_T *load_dummy_buffer __ARGS((char_u *fname));
133static void wipe_dummy_buffer __ARGS((buf_T *buf));
134static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000135static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000137/* Quickfix window check helper macro */
138#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
139/* Location list window check helper macro */
140#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
141/*
142 * Return location list for window 'wp'
143 * For location list window, return the referenced location list
144 */
145#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000148 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200149 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 * Return -1 for error, number of errors for success.
151 */
152 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200153qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000154 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 char_u *efile;
156 char_u *errorformat;
157 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200158 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000160 qf_info_T *qi = &ql_info;
161
Bram Moolenaar86b68352004-12-27 21:59:20 +0000162 if (efile == NULL)
163 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164
165 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166 {
167 qi = ll_get_or_alloc_list(wp);
168 if (qi == NULL)
169 return FAIL;
170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171
172 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200173 (linenr_T)0, (linenr_T)0,
174 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000175}
176
177/*
178 * Read the errorfile "efile" into memory, line by line, building the error
179 * list.
180 * Alternative: when "efile" is null read errors from buffer "buf".
181 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200182 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
183 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000184 * Return -1 for error, number of errors for success.
185 */
186 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200187qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
188 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000189 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000190 char_u *efile;
191 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000192 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000193 char_u *errorformat;
194 int newlist; /* TRUE: start a new error list */
195 linenr_T lnumfirst; /* first line number to use */
196 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200197 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000198{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 char_u *namebuf;
200 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000201 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 char_u *fmtstr = NULL;
203 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000204 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 int type = 0;
206 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000207 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 long lnum = 0L;
209 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000210 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000211 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000213 efm_T *fmt_first = NULL;
214 efm_T *fmt_last = NULL;
215 efm_T *fmt_ptr;
216 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 char_u *efm;
218 char_u *ptr;
219 char_u *srcptr;
220 int len;
221 int i;
222 int round;
223 int idx = 0;
224 int multiline = FALSE;
225 int multiignore = FALSE;
226 int multiscan = FALSE;
227 int retval = -1; /* default: return error flag */
228 char_u *directory = NULL;
229 char_u *currfile = NULL;
230 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000231 char_u *p_str = NULL;
232 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 struct dir_stack_T *file_stack = NULL;
234 regmatch_T regmatch;
235 static struct fmtpattern
236 {
237 char_u convchar;
238 char *pattern;
239 } fmt_pat[FMT_PATTERNS] =
240 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000241 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {'n', "\\d\\+"},
243 {'l', "\\d\\+"},
244 {'c', "\\d\\+"},
245 {'t', "."},
246 {'m', ".\\+"},
247 {'r', ".*"},
248 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000249 {'v', "\\d\\+"},
250 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 };
252
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 namebuf = alloc(CMDBUFFSIZE + 1);
254 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000255 pattern = alloc(CMDBUFFSIZE + 1);
256 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 goto qf_init_end;
258
Bram Moolenaar86b68352004-12-27 21:59:20 +0000259 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 {
261 EMSG2(_(e_openerrf), efile);
262 goto qf_init_end;
263 }
264
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000265 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200267 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000268 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000270 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200271 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 ;
273
274/*
275 * Each part of the format string is copied and modified from errorformat to
276 * regex prog. Only a few % characters are allowed.
277 */
278 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000279 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000280 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 else
282 efm = errorformat;
283 /*
284 * Get some space to modify the format string into.
285 */
286 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
287 for (round = FMT_PATTERNS; round > 0; )
288 i += (int)STRLEN(fmt_pat[--round].pattern);
289#ifdef COLON_IN_FILENAME
290 i += 12; /* "%f" can become twelve chars longer */
291#else
292 i += 2; /* "%f" can become two chars longer */
293#endif
294 if ((fmtstr = alloc(i)) == NULL)
295 goto error2;
296
Bram Moolenaar01265852006-03-20 21:50:15 +0000297 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {
299 /*
300 * Allocate a new eformat structure and put it at the end of the list
301 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000302 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 if (fmt_ptr == NULL)
304 goto error2;
305 if (fmt_first == NULL) /* first one */
306 fmt_first = fmt_ptr;
307 else
308 fmt_last->next = fmt_ptr;
309 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310
311 /*
312 * Isolate one part in the 'errorformat' option
313 */
314 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
315 if (efm[len] == '\\' && efm[len + 1] != NUL)
316 ++len;
317
318 /*
319 * Build regexp pattern from current 'errorformat' option
320 */
321 ptr = fmtstr;
322 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000323 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 for (efmp = efm; efmp < efm + len; ++efmp)
325 {
326 if (*efmp == '%')
327 {
328 ++efmp;
329 for (idx = 0; idx < FMT_PATTERNS; ++idx)
330 if (fmt_pat[idx].convchar == *efmp)
331 break;
332 if (idx < FMT_PATTERNS)
333 {
334 if (fmt_ptr->addr[idx])
335 {
336 sprintf((char *)errmsg,
337 _("E372: Too many %%%c in format string"), *efmp);
338 EMSG(errmsg);
339 goto error2;
340 }
341 if ((idx
342 && idx < 6
343 && vim_strchr((char_u *)"DXOPQ",
344 fmt_ptr->prefix) != NULL)
345 || (idx == 6
346 && vim_strchr((char_u *)"OPQ",
347 fmt_ptr->prefix) == NULL))
348 {
349 sprintf((char *)errmsg,
350 _("E373: Unexpected %%%c in format string"), *efmp);
351 EMSG(errmsg);
352 goto error2;
353 }
354 fmt_ptr->addr[idx] = (char_u)++round;
355 *ptr++ = '\\';
356 *ptr++ = '(';
357#ifdef BACKSLASH_IN_FILENAME
358 if (*efmp == 'f')
359 {
360 /* Also match "c:" in the file name, even when
361 * checking for a colon next: "%f:".
362 * "\%(\a:\)\=" */
363 STRCPY(ptr, "\\%(\\a:\\)\\=");
364 ptr += 10;
365 }
366#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000367 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000369 if (efmp[1] != '\\' && efmp[1] != '%')
370 {
371 /* A file name may contain spaces, but this isn't
372 * in "\f". For "%f:%l:%m" there may be a ":" in
373 * the file name. Use ".\{-1,}x" instead (x is
374 * the next character), the requirement that :999:
375 * follows should work. */
376 STRCPY(ptr, ".\\{-1,}");
377 ptr += 7;
378 }
379 else
380 {
381 /* File name followed by '\\' or '%': include as
382 * many file name chars as possible. */
383 STRCPY(ptr, "\\f\\+");
384 ptr += 4;
385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 }
387 else
388 {
389 srcptr = (char_u *)fmt_pat[idx].pattern;
390 while ((*ptr = *srcptr++) != NUL)
391 ++ptr;
392 }
393 *ptr++ = '\\';
394 *ptr++ = ')';
395 }
396 else if (*efmp == '*')
397 {
398 if (*++efmp == '[' || *efmp == '\\')
399 {
400 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
401 {
402 if (efmp[1] == '^')
403 *ptr++ = *++efmp;
404 if (efmp < efm + len)
405 {
406 *ptr++ = *++efmp; /* could be ']' */
407 while (efmp < efm + len
408 && (*ptr++ = *++efmp) != ']')
409 /* skip */;
410 if (efmp == efm + len)
411 {
412 EMSG(_("E374: Missing ] in format string"));
413 goto error2;
414 }
415 }
416 }
417 else if (efmp < efm + len) /* %*\D, %*\s etc. */
418 *ptr++ = *++efmp;
419 *ptr++ = '\\';
420 *ptr++ = '+';
421 }
422 else
423 {
424 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
425 sprintf((char *)errmsg,
426 _("E375: Unsupported %%%c in format string"), *efmp);
427 EMSG(errmsg);
428 goto error2;
429 }
430 }
431 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
432 *ptr++ = *efmp; /* regexp magic characters */
433 else if (*efmp == '#')
434 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000435 else if (*efmp == '>')
436 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 else if (efmp == efm + 1) /* analyse prefix */
438 {
439 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
440 fmt_ptr->flags = *efmp++;
441 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
442 fmt_ptr->prefix = *efmp;
443 else
444 {
445 sprintf((char *)errmsg,
446 _("E376: Invalid %%%c in format string prefix"), *efmp);
447 EMSG(errmsg);
448 goto error2;
449 }
450 }
451 else
452 {
453 sprintf((char *)errmsg,
454 _("E377: Invalid %%%c in format string"), *efmp);
455 EMSG(errmsg);
456 goto error2;
457 }
458 }
459 else /* copy normal character */
460 {
461 if (*efmp == '\\' && efmp + 1 < efm + len)
462 ++efmp;
463 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
464 *ptr++ = '\\'; /* escape regexp atoms */
465 if (*efmp)
466 *ptr++ = *efmp;
467 }
468 }
469 *ptr++ = '$';
470 *ptr = NUL;
471 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
472 goto error2;
473 /*
474 * Advance to next part
475 */
476 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
477 }
478 if (fmt_first == NULL) /* nothing found */
479 {
480 EMSG(_("E378: 'errorformat' contains no pattern"));
481 goto error2;
482 }
483
484 /*
485 * got_int is reset here, because it was probably set when killing the
486 * ":make" command, but we still want to read the errorfile then.
487 */
488 got_int = FALSE;
489
490 /* Always ignore case when looking for a matching error. */
491 regmatch.rm_ic = TRUE;
492
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000493 if (tv != NULL)
494 {
495 if (tv->v_type == VAR_STRING)
496 p_str = tv->vval.v_string;
497 else if (tv->v_type == VAR_LIST)
498 p_li = tv->vval.v_list->lv_first;
499 }
500
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 /*
502 * Read the lines in the error file one by one.
503 * Try to recognize one of the error formats in each line.
504 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000505 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000507 /* Get the next line. */
508 if (fd == NULL)
509 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000510 if (tv != NULL)
511 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000512 if (tv->v_type == VAR_STRING)
513 {
514 /* Get the next line from the supplied string */
515 char_u *p;
516
517 if (!*p_str) /* Reached the end of the string */
518 break;
519
520 p = vim_strchr(p_str, '\n');
521 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000522 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000523 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000524 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000525
526 if (len > CMDBUFFSIZE - 2)
527 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
528 else
529 vim_strncpy(IObuff, p_str, len);
530
531 p_str += len;
532 }
533 else if (tv->v_type == VAR_LIST)
534 {
535 /* Get the next line from the supplied list */
536 while (p_li && p_li->li_tv.v_type != VAR_STRING)
537 p_li = p_li->li_next; /* Skip non-string items */
538
539 if (!p_li) /* End of the list */
540 break;
541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000542 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000543 if (len > CMDBUFFSIZE - 2)
544 len = CMDBUFFSIZE - 2;
545
546 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
547
548 p_li = p_li->li_next; /* next item */
549 }
550 }
551 else
552 {
553 /* Get the next line from the supplied buffer */
554 if (buflnum > lnumlast)
555 break;
556 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
557 CMDBUFFSIZE - 2);
558 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000559 }
560 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
561 break;
562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200564#ifdef FEAT_MBYTE
565 remove_bom(IObuff);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
569 *efmp = NUL;
570#ifdef USE_CRNL
571 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
572 *efmp = NUL;
573#endif
574
Bram Moolenaar01265852006-03-20 21:50:15 +0000575 /* If there was no %> item start at the first pattern */
576 if (fmt_start == NULL)
577 fmt_ptr = fmt_first;
578 else
579 {
580 fmt_ptr = fmt_start;
581 fmt_start = NULL;
582 }
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 /*
585 * Try to match each part of 'errorformat' until we find a complete
586 * match or no match.
587 */
588 valid = TRUE;
589restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000590 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {
592 idx = fmt_ptr->prefix;
593 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
594 continue;
595 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000596 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 if (!multiscan)
598 errmsg[0] = NUL;
599 lnum = 0;
600 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000601 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 enr = -1;
603 type = 0;
604 tail = NULL;
605
606 regmatch.regprog = fmt_ptr->prog;
607 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
608 {
609 if ((idx == 'C' || idx == 'Z') && !multiline)
610 continue;
611 if (vim_strchr((char_u *)"EWI", idx) != NULL)
612 type = idx;
613 else
614 type = 0;
615 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000616 * Extract error message data from matched line.
617 * We check for an actual submatch, because "\[" and "\]" in
618 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 */
620 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
621 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000622 int c;
623
624 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
625 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000626
627 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000628 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000629 *regmatch.endp[i] = NUL;
630 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
631 *regmatch.endp[i] = c;
632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000634 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 continue;
636 }
637 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000638 {
639 if (regmatch.startp[i] == NULL)
640 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000644 {
645 if (regmatch.startp[i] == NULL)
646 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000650 {
651 if (regmatch.startp[i] == NULL)
652 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000656 {
657 if (regmatch.startp[i] == NULL)
658 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000660 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 STRCPY(errmsg, IObuff);
663 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
664 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000665 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
666 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000668 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 }
670 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000671 {
672 if (regmatch.startp[i] == NULL)
673 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
677 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000678 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
679 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
681 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000682 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
685 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000686 if (regmatch.startp[i] == NULL)
687 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000691 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
692 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000693 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
694 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000695 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
696 if (len > CMDBUFFSIZE - 5)
697 len = CMDBUFFSIZE - 5;
698 STRCPY(pattern, "^\\V");
699 STRNCAT(pattern, regmatch.startp[i], len);
700 pattern[len + 3] = '\\';
701 pattern[len + 4] = '$';
702 pattern[len + 5] = NUL;
703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 break;
705 }
706 }
707 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000708
Bram Moolenaar4770d092006-01-12 23:22:24 +0000709 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000711 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 if (idx == 'D') /* enter directory */
714 {
715 if (*namebuf == NUL)
716 {
717 EMSG(_("E379: Missing or empty directory name"));
718 goto error2;
719 }
720 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
721 goto error2;
722 }
723 else if (idx == 'X') /* leave directory */
724 directory = qf_pop_dir(&dir_stack);
725 }
726 namebuf[0] = NUL; /* no match found, remove file name */
727 lnum = 0; /* don't jump to this line */
728 valid = FALSE;
729 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000730 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 multiline = multiignore = FALSE;
732 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000733 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000735 /* honor %> item */
736 if (fmt_ptr->conthere)
737 fmt_start = fmt_ptr;
738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
740 multiline = TRUE; /* start of a multi-line message */
741 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
742 { /* continuation of multi-line msg */
743 if (qfprev == NULL)
744 goto error2;
745 if (*errmsg && !multiignore)
746 {
747 len = (int)STRLEN(qfprev->qf_text);
748 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
749 == NULL)
750 goto error2;
751 STRCPY(ptr, qfprev->qf_text);
752 vim_free(qfprev->qf_text);
753 qfprev->qf_text = ptr;
754 *(ptr += len) = '\n';
755 STRCPY(++ptr, errmsg);
756 }
757 if (qfprev->qf_nr == -1)
758 qfprev->qf_nr = enr;
759 if (vim_isprintc(type) && !qfprev->qf_type)
760 qfprev->qf_type = type; /* only printable chars allowed */
761 if (!qfprev->qf_lnum)
762 qfprev->qf_lnum = lnum;
763 if (!qfprev->qf_col)
764 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (!qfprev->qf_fnum)
767 qfprev->qf_fnum = qf_get_fnum(directory,
768 *namebuf || directory ? namebuf
769 : currfile && valid ? currfile : 0);
770 if (idx == 'Z')
771 multiline = multiignore = FALSE;
772 line_breakcheck();
773 continue;
774 }
775 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
776 {
777 /* global file names */
778 valid = FALSE;
779 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
780 {
781 if (*namebuf && idx == 'P')
782 currfile = qf_push_dir(namebuf, &file_stack);
783 else if (idx == 'Q')
784 currfile = qf_pop_dir(&file_stack);
785 *namebuf = NUL;
786 if (tail && *tail)
787 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000788 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 multiscan = TRUE;
790 goto restofline;
791 }
792 }
793 }
794 if (fmt_ptr->flags == '-') /* generally exclude this line */
795 {
796 if (multiline)
797 multiignore = TRUE; /* also exclude continuation lines */
798 continue;
799 }
800 }
801
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000802 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000804 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000806 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000807 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 errmsg,
809 lnum,
810 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000812 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 enr,
814 type,
815 valid) == FAIL)
816 goto error2;
817 line_breakcheck();
818 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000819 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000821 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000823 /* no valid entry found */
824 qi->qf_lists[qi->qf_curlist].qf_ptr =
825 qi->qf_lists[qi->qf_curlist].qf_start;
826 qi->qf_lists[qi->qf_curlist].qf_index = 1;
827 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 }
829 else
830 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000831 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
832 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
833 qi->qf_lists[qi->qf_curlist].qf_ptr =
834 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000836 /* return number of matches */
837 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 goto qf_init_ok;
839 }
840 EMSG(_(e_readerrf));
841error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000842 qf_free(qi, qi->qf_curlist);
843 qi->qf_listcount--;
844 if (qi->qf_curlist > 0)
845 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000847 if (fd != NULL)
848 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
850 {
851 fmt_first = fmt_ptr->next;
852 vim_free(fmt_ptr->prog);
853 vim_free(fmt_ptr);
854 }
855 qf_clean_dir_stack(&dir_stack);
856 qf_clean_dir_stack(&file_stack);
857qf_init_end:
858 vim_free(namebuf);
859 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000860 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 vim_free(fmtstr);
862
863#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000864 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#endif
866
867 return retval;
868}
869
870/*
871 * Prepare for adding a new quickfix list.
872 */
873 static void
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200874qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000875 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200876 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
878 int i;
879
880 /*
881 * If the current entry is not the last entry, delete entries below
882 * the current entry. This makes it possible to browse in a tree-like
883 * way with ":grep'.
884 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000885 while (qi->qf_listcount > qi->qf_curlist + 1)
886 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 /*
889 * When the stack is full, remove to oldest entry
890 * Otherwise, add a new entry.
891 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000892 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000894 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000896 qi->qf_lists[i - 1] = qi->qf_lists[i];
897 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000900 qi->qf_curlist = qi->qf_listcount++;
901 qi->qf_lists[qi->qf_curlist].qf_index = 0;
902 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200903 if (qf_title != NULL)
904 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200905 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200906
907 qi->qf_lists[qi->qf_curlist].qf_title = p;
908 if (p != NULL)
909 sprintf((char *)p, ":%s", (char *)qf_title);
910 }
911 else
912 qi->qf_lists[qi->qf_curlist].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913}
914
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000915/*
916 * Free a location list
917 */
918 static void
919ll_free_all(pqi)
920 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000921{
922 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000923 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000924
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000925 qi = *pqi;
926 if (qi == NULL)
927 return;
928 *pqi = NULL; /* Remove reference to this list */
929
930 qi->qf_refcount--;
931 if (qi->qf_refcount < 1)
932 {
933 /* No references to this location list */
934 for (i = 0; i < qi->qf_listcount; ++i)
935 qf_free(qi, i);
936 vim_free(qi);
937 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000938}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000939
940 void
941qf_free_all(wp)
942 win_T *wp;
943{
944 int i;
945 qf_info_T *qi = &ql_info;
946
947 if (wp != NULL)
948 {
949 /* location list */
950 ll_free_all(&wp->w_llist);
951 ll_free_all(&wp->w_llist_ref);
952 }
953 else
954 /* quickfix list */
955 for (i = 0; i < qi->qf_listcount; ++i)
956 qf_free(qi, i);
957}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959/*
960 * Add an entry to the end of the list of errors.
961 * Returns OK or FAIL.
962 */
963 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000964qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
965 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000966 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000967 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 char_u *dir; /* optional directory name */
969 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000970 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 char_u *mesg; /* message */
972 long lnum; /* line number */
973 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000974 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000975 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 int nr; /* error number */
977 int type; /* type character */
978 int valid; /* valid entry */
979{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000980 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000982 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000984 if (bufnum != 0)
985 qfp->qf_fnum = bufnum;
986 else
987 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
989 {
990 vim_free(qfp);
991 return FAIL;
992 }
993 qfp->qf_lnum = lnum;
994 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000995 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000996 if (pattern == NULL || *pattern == NUL)
997 qfp->qf_pattern = NULL;
998 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
999 {
1000 vim_free(qfp->qf_text);
1001 vim_free(qfp);
1002 return FAIL;
1003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 qfp->qf_nr = nr;
1005 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1006 type = 0;
1007 qfp->qf_type = type;
1008 qfp->qf_valid = valid;
1009
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001010 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1011 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001013 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 qfp->qf_prev = qfp; /* first element points to itself */
1015 }
1016 else
1017 {
1018 qfp->qf_prev = *prevp;
1019 (*prevp)->qf_next = qfp;
1020 }
1021 qfp->qf_next = qfp; /* last element points to itself */
1022 qfp->qf_cleared = FALSE;
1023 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001024 ++qi->qf_lists[qi->qf_curlist].qf_count;
1025 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1026 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001028 qi->qf_lists[qi->qf_curlist].qf_index =
1029 qi->qf_lists[qi->qf_curlist].qf_count;
1030 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032
1033 return OK;
1034}
1035
1036/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001037 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001038 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001039 static qf_info_T *
1040ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001041{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001042 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001043
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001044 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1045 if (qi != NULL)
1046 {
1047 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1048 qi->qf_refcount++;
1049 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001050
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001051 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001052}
1053
1054/*
1055 * Return the location list for window 'wp'.
1056 * If not present, allocate a location list
1057 */
1058 static qf_info_T *
1059ll_get_or_alloc_list(wp)
1060 win_T *wp;
1061{
1062 if (IS_LL_WINDOW(wp))
1063 /* For a location list window, use the referenced location list */
1064 return wp->w_llist_ref;
1065
1066 /*
1067 * For a non-location list window, w_llist_ref should not point to a
1068 * location list.
1069 */
1070 ll_free_all(&wp->w_llist_ref);
1071
1072 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001073 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001074 return wp->w_llist;
1075}
1076
1077/*
1078 * Copy the location list from window "from" to window "to".
1079 */
1080 void
1081copy_loclist(from, to)
1082 win_T *from;
1083 win_T *to;
1084{
1085 qf_info_T *qi;
1086 int idx;
1087 int i;
1088
1089 /*
1090 * When copying from a location list window, copy the referenced
1091 * location list. For other windows, copy the location list for
1092 * that window.
1093 */
1094 if (IS_LL_WINDOW(from))
1095 qi = from->w_llist_ref;
1096 else
1097 qi = from->w_llist;
1098
1099 if (qi == NULL) /* no location list to copy */
1100 return;
1101
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001102 /* allocate a new location list */
1103 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001104 return;
1105
1106 to->w_llist->qf_listcount = qi->qf_listcount;
1107
1108 /* Copy the location lists one at a time */
1109 for (idx = 0; idx < qi->qf_listcount; idx++)
1110 {
1111 qf_list_T *from_qfl;
1112 qf_list_T *to_qfl;
1113
1114 to->w_llist->qf_curlist = idx;
1115
1116 from_qfl = &qi->qf_lists[idx];
1117 to_qfl = &to->w_llist->qf_lists[idx];
1118
1119 /* Some of the fields are populated by qf_add_entry() */
1120 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1121 to_qfl->qf_count = 0;
1122 to_qfl->qf_index = 0;
1123 to_qfl->qf_start = NULL;
1124 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001125 if (from_qfl->qf_title != NULL)
1126 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1127 else
1128 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001129
1130 if (from_qfl->qf_count)
1131 {
1132 qfline_T *from_qfp;
1133 qfline_T *prevp = NULL;
1134
1135 /* copy all the location entries in this list */
1136 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1137 ++i, from_qfp = from_qfp->qf_next)
1138 {
1139 if (qf_add_entry(to->w_llist, &prevp,
1140 NULL,
1141 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001142 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001143 from_qfp->qf_text,
1144 from_qfp->qf_lnum,
1145 from_qfp->qf_col,
1146 from_qfp->qf_viscol,
1147 from_qfp->qf_pattern,
1148 from_qfp->qf_nr,
1149 0,
1150 from_qfp->qf_valid) == FAIL)
1151 {
1152 qf_free_all(to);
1153 return;
1154 }
1155 /*
1156 * qf_add_entry() will not set the qf_num field, as the
1157 * directory and file names are not supplied. So the qf_fnum
1158 * field is copied here.
1159 */
1160 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1161 prevp->qf_type = from_qfp->qf_type; /* error type */
1162 if (from_qfl->qf_ptr == from_qfp)
1163 to_qfl->qf_ptr = prevp; /* current location */
1164 }
1165 }
1166
1167 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1168
1169 /* When no valid entries are present in the list, qf_ptr points to
1170 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001171 if (to_qfl->qf_nonevalid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001172 to_qfl->qf_ptr = to_qfl->qf_start;
1173 }
1174
1175 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1176}
1177
1178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 * get buffer number for file "dir.name"
1180 */
1181 static int
1182qf_get_fnum(directory, fname)
1183 char_u *directory;
1184 char_u *fname;
1185{
1186 if (fname == NULL || *fname == NUL) /* no file name */
1187 return 0;
1188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 char_u *ptr;
1190 int fnum;
1191
Bram Moolenaare60acc12011-05-10 16:41:25 +02001192#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001194#endif
1195#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (directory != NULL)
1197 slash_adjust(directory);
1198 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (directory != NULL && !vim_isAbsName(fname)
1201 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1202 {
1203 /*
1204 * Here we check if the file really exists.
1205 * This should normally be true, but if make works without
1206 * "leaving directory"-messages we might have missed a
1207 * directory change.
1208 */
1209 if (mch_getperm(ptr) < 0)
1210 {
1211 vim_free(ptr);
1212 directory = qf_guess_filepath(fname);
1213 if (directory)
1214 ptr = concat_fnames(directory, fname, TRUE);
1215 else
1216 ptr = vim_strsave(fname);
1217 }
1218 /* Use concatenated directory name and file name */
1219 fnum = buflist_add(ptr, 0);
1220 vim_free(ptr);
1221 return fnum;
1222 }
1223 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225}
1226
1227/*
1228 * push dirbuf onto the directory stack and return pointer to actual dir or
1229 * NULL on error
1230 */
1231 static char_u *
1232qf_push_dir(dirbuf, stackptr)
1233 char_u *dirbuf;
1234 struct dir_stack_T **stackptr;
1235{
1236 struct dir_stack_T *ds_new;
1237 struct dir_stack_T *ds_ptr;
1238
1239 /* allocate new stack element and hook it in */
1240 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1241 if (ds_new == NULL)
1242 return NULL;
1243
1244 ds_new->next = *stackptr;
1245 *stackptr = ds_new;
1246
1247 /* store directory on the stack */
1248 if (vim_isAbsName(dirbuf)
1249 || (*stackptr)->next == NULL
1250 || (*stackptr && dir_stack != *stackptr))
1251 (*stackptr)->dirname = vim_strsave(dirbuf);
1252 else
1253 {
1254 /* Okay we don't have an absolute path.
1255 * dirbuf must be a subdir of one of the directories on the stack.
1256 * Let's search...
1257 */
1258 ds_new = (*stackptr)->next;
1259 (*stackptr)->dirname = NULL;
1260 while (ds_new)
1261 {
1262 vim_free((*stackptr)->dirname);
1263 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1264 TRUE);
1265 if (mch_isdir((*stackptr)->dirname) == TRUE)
1266 break;
1267
1268 ds_new = ds_new->next;
1269 }
1270
1271 /* clean up all dirs we already left */
1272 while ((*stackptr)->next != ds_new)
1273 {
1274 ds_ptr = (*stackptr)->next;
1275 (*stackptr)->next = (*stackptr)->next->next;
1276 vim_free(ds_ptr->dirname);
1277 vim_free(ds_ptr);
1278 }
1279
1280 /* Nothing found -> it must be on top level */
1281 if (ds_new == NULL)
1282 {
1283 vim_free((*stackptr)->dirname);
1284 (*stackptr)->dirname = vim_strsave(dirbuf);
1285 }
1286 }
1287
1288 if ((*stackptr)->dirname != NULL)
1289 return (*stackptr)->dirname;
1290 else
1291 {
1292 ds_ptr = *stackptr;
1293 *stackptr = (*stackptr)->next;
1294 vim_free(ds_ptr);
1295 return NULL;
1296 }
1297}
1298
1299
1300/*
1301 * pop dirbuf from the directory stack and return previous directory or NULL if
1302 * stack is empty
1303 */
1304 static char_u *
1305qf_pop_dir(stackptr)
1306 struct dir_stack_T **stackptr;
1307{
1308 struct dir_stack_T *ds_ptr;
1309
1310 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1311 * What to do if it isn't? */
1312
1313 /* pop top element and free it */
1314 if (*stackptr != NULL)
1315 {
1316 ds_ptr = *stackptr;
1317 *stackptr = (*stackptr)->next;
1318 vim_free(ds_ptr->dirname);
1319 vim_free(ds_ptr);
1320 }
1321
1322 /* return NEW top element as current dir or NULL if stack is empty*/
1323 return *stackptr ? (*stackptr)->dirname : NULL;
1324}
1325
1326/*
1327 * clean up directory stack
1328 */
1329 static void
1330qf_clean_dir_stack(stackptr)
1331 struct dir_stack_T **stackptr;
1332{
1333 struct dir_stack_T *ds_ptr;
1334
1335 while ((ds_ptr = *stackptr) != NULL)
1336 {
1337 *stackptr = (*stackptr)->next;
1338 vim_free(ds_ptr->dirname);
1339 vim_free(ds_ptr);
1340 }
1341}
1342
1343/*
1344 * Check in which directory of the directory stack the given file can be
1345 * found.
1346 * Returns a pointer to the directory name or NULL if not found
1347 * Cleans up intermediate directory entries.
1348 *
1349 * TODO: How to solve the following problem?
1350 * If we have the this directory tree:
1351 * ./
1352 * ./aa
1353 * ./aa/bb
1354 * ./bb
1355 * ./bb/x.c
1356 * and make says:
1357 * making all in aa
1358 * making all in bb
1359 * x.c:9: Error
1360 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1361 * qf_guess_filepath will return NULL.
1362 */
1363 static char_u *
1364qf_guess_filepath(filename)
1365 char_u *filename;
1366{
1367 struct dir_stack_T *ds_ptr;
1368 struct dir_stack_T *ds_tmp;
1369 char_u *fullname;
1370
1371 /* no dirs on the stack - there's nothing we can do */
1372 if (dir_stack == NULL)
1373 return NULL;
1374
1375 ds_ptr = dir_stack->next;
1376 fullname = NULL;
1377 while (ds_ptr)
1378 {
1379 vim_free(fullname);
1380 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1381
1382 /* If concat_fnames failed, just go on. The worst thing that can happen
1383 * is that we delete the entire stack.
1384 */
1385 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1386 break;
1387
1388 ds_ptr = ds_ptr->next;
1389 }
1390
1391 vim_free(fullname);
1392
1393 /* clean up all dirs we already left */
1394 while (dir_stack->next != ds_ptr)
1395 {
1396 ds_tmp = dir_stack->next;
1397 dir_stack->next = dir_stack->next->next;
1398 vim_free(ds_tmp->dirname);
1399 vim_free(ds_tmp);
1400 }
1401
1402 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1403
1404}
1405
1406/*
1407 * jump to a quickfix line
1408 * if dir == FORWARD go "errornr" valid entries forward
1409 * if dir == BACKWARD go "errornr" valid entries backward
1410 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1411 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1412 * else if "errornr" is zero, redisplay the same line
1413 * else go to entry "errornr"
1414 */
1415 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001416qf_jump(qi, dir, errornr, forceit)
1417 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 int dir;
1419 int errornr;
1420 int forceit;
1421{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001422 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001423 qfline_T *qf_ptr;
1424 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 int qf_index;
1426 int old_qf_fnum;
1427 int old_qf_index;
1428 int prev_index;
1429 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1430 char_u *err = e_no_more_items;
1431 linenr_T i;
1432 buf_T *old_curbuf;
1433 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 colnr_T screen_col;
1435 colnr_T char_col;
1436 char_u *line;
1437#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001438 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001439 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 int opened_window = FALSE;
1441 win_T *win;
1442 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001443 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001445 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 int print_message = TRUE;
1447 int len;
1448#ifdef FEAT_FOLDING
1449 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1450#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001451 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001452 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001454 if (qi == NULL)
1455 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001456
1457 if (qi->qf_curlist >= qi->qf_listcount
1458 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
1460 EMSG(_(e_quickfix));
1461 return;
1462 }
1463
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001464 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001466 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 old_qf_index = qf_index;
1468 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1469 {
1470 while (errornr--)
1471 {
1472 old_qf_ptr = qf_ptr;
1473 prev_index = qf_index;
1474 old_qf_fnum = qf_ptr->qf_fnum;
1475 do
1476 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001477 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 || qf_ptr->qf_next == NULL)
1479 {
1480 qf_ptr = old_qf_ptr;
1481 qf_index = prev_index;
1482 if (err != NULL)
1483 {
1484 EMSG(_(err));
1485 goto theend;
1486 }
1487 errornr = 0;
1488 break;
1489 }
1490 ++qf_index;
1491 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001492 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1493 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1495 err = NULL;
1496 }
1497 }
1498 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1499 {
1500 while (errornr--)
1501 {
1502 old_qf_ptr = qf_ptr;
1503 prev_index = qf_index;
1504 old_qf_fnum = qf_ptr->qf_fnum;
1505 do
1506 {
1507 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1508 {
1509 qf_ptr = old_qf_ptr;
1510 qf_index = prev_index;
1511 if (err != NULL)
1512 {
1513 EMSG(_(err));
1514 goto theend;
1515 }
1516 errornr = 0;
1517 break;
1518 }
1519 --qf_index;
1520 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001521 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1522 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1524 err = NULL;
1525 }
1526 }
1527 else if (errornr != 0) /* go to specified number */
1528 {
1529 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1530 {
1531 --qf_index;
1532 qf_ptr = qf_ptr->qf_prev;
1533 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534 while (errornr > qf_index && qf_index <
1535 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 && qf_ptr->qf_next != NULL)
1537 {
1538 ++qf_index;
1539 qf_ptr = qf_ptr->qf_next;
1540 }
1541 }
1542
1543#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001544 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1545 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 /* No need to print the error message if it's visible in the error
1547 * window */
1548 print_message = FALSE;
1549
1550 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001551 * For ":helpgrep" find a help window or open one.
1552 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001553 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001554 {
1555 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001556
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001557 if (cmdmod.tab != 0)
1558 wp = NULL;
1559 else
1560 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1561 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1562 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001563 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1564 win_enter(wp, TRUE);
1565 else
1566 {
1567 /*
1568 * Split off help window; put it at far top if no position
1569 * specified, the current window is vertically split and narrow.
1570 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001571 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001572# ifdef FEAT_VERTSPLIT
1573 if (cmdmod.split == 0 && curwin->w_width != Columns
1574 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001575 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001576# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001577 if (qi != &ql_info)
1578 flags |= WSP_NEWLOC; /* don't copy the location list */
1579
1580 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001581 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001582 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001583
1584 if (curwin->w_height < p_hh)
1585 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001586
1587 if (qi != &ql_info) /* not a quickfix list */
1588 {
1589 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001590 curwin->w_llist = qi;
1591 qi->qf_refcount++;
1592 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001593 }
1594
1595 if (!p_im)
1596 restart_edit = 0; /* don't want insert mode in help file */
1597 }
1598
1599 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 * If currently in the quickfix window, find another window to show the
1601 * file in.
1602 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001603 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
1605 /*
1606 * If there is no file specified, we don't know where to go.
1607 * But do advance, otherwise ":cn" gets stuck.
1608 */
1609 if (qf_ptr->qf_fnum == 0)
1610 goto theend;
1611
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001612 /* Locate a window showing a normal buffer */
1613 usable_win = 0;
1614 FOR_ALL_WINDOWS(win)
1615 if (win->w_buffer->b_p_bt[0] == NUL)
1616 {
1617 usable_win = 1;
1618 break;
1619 }
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001622 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001623 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001625 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001626 {
1627 tabpage_T *tp;
1628 win_T *wp;
1629
1630 FOR_ALL_TAB_WINDOWS(tp, wp)
1631 {
1632 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1633 {
1634 goto_tabpage_win(tp, wp);
1635 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001636 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001637 }
1638 }
1639 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001640win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001641
1642 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001643 * If there is only one window and it is the quickfix window, create a
1644 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001645 */
1646 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001648 ll_ref = curwin->w_llist_ref;
1649
Bram Moolenaar884ae642009-02-22 01:37:59 +00001650 flags = WSP_ABOVE;
1651 if (ll_ref != NULL)
1652 flags |= WSP_NEWLOC;
1653 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 goto failed; /* not enough room for window */
1655 opened_window = TRUE; /* close it when fail */
1656 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001657 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001658 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001659 if (ll_ref != NULL)
1660 {
1661 /* The new window should use the location list from the
1662 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001663 curwin->w_llist = ll_ref;
1664 ll_ref->qf_refcount++;
1665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667 else
1668 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001669 if (curwin->w_llist_ref != NULL)
1670 {
1671 /* In a location window */
1672 ll_ref = curwin->w_llist_ref;
1673
1674 /* Find the window with the same location list */
1675 FOR_ALL_WINDOWS(win)
1676 if (win->w_llist == ll_ref)
1677 break;
1678 if (win == NULL)
1679 {
1680 /* Find the window showing the selected file */
1681 FOR_ALL_WINDOWS(win)
1682 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1683 break;
1684 if (win == NULL)
1685 {
1686 /* Find a previous usable window */
1687 win = curwin;
1688 do
1689 {
1690 if (win->w_buffer->b_p_bt[0] == NUL)
1691 break;
1692 if (win->w_prev == NULL)
1693 win = lastwin; /* wrap around the top */
1694 else
1695 win = win->w_prev; /* go to previous window */
1696 } while (win != curwin);
1697 }
1698 }
1699 win_goto(win);
1700
1701 /* If the location list for the window is not set, then set it
1702 * to the location list from the location window */
1703 if (win->w_llist == NULL)
1704 {
1705 win->w_llist = ll_ref;
1706 ll_ref->qf_refcount++;
1707 }
1708 }
1709 else
1710 {
1711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 /*
1713 * Try to find a window that shows the right buffer.
1714 * Default to the window just above the quickfix buffer.
1715 */
1716 win = curwin;
1717 altwin = NULL;
1718 for (;;)
1719 {
1720 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1721 break;
1722 if (win->w_prev == NULL)
1723 win = lastwin; /* wrap around the top */
1724 else
1725 win = win->w_prev; /* go to previous window */
1726
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001727 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 {
1729 /* Didn't find it, go to the window before the quickfix
1730 * window. */
1731 if (altwin != NULL)
1732 win = altwin;
1733 else if (curwin->w_prev != NULL)
1734 win = curwin->w_prev;
1735 else
1736 win = curwin->w_next;
1737 break;
1738 }
1739
1740 /* Remember a usable window. */
1741 if (altwin == NULL && !win->w_p_pvw
1742 && win->w_buffer->b_p_bt[0] == NUL)
1743 altwin = win;
1744 }
1745
1746 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749 }
1750#endif
1751
1752 /*
1753 * If there is a file name,
1754 * read the wanted file if needed, and check autowrite etc.
1755 */
1756 old_curbuf = curbuf;
1757 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001758
1759 if (qf_ptr->qf_fnum != 0)
1760 {
1761 if (qf_ptr->qf_type == 1)
1762 {
1763 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1764 * set b_p_ro flag). */
1765 if (!can_abandon(curbuf, forceit))
1766 {
1767 EMSG(_(e_nowrtmsg));
1768 ok = FALSE;
1769 }
1770 else
1771 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001772 ECMD_HIDE + ECMD_SET_HELP,
1773 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001774 }
1775 else
1776 ok = buflist_getfile(qf_ptr->qf_fnum,
1777 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1778 }
1779
1780 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 {
1782 /* When not switched to another buffer, still need to set pc mark */
1783 if (curbuf == old_curbuf)
1784 setpcmark();
1785
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001786 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001788 /*
1789 * Go to line with error, unless qf_lnum is 0.
1790 */
1791 i = qf_ptr->qf_lnum;
1792 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001794 if (i > curbuf->b_ml.ml_line_count)
1795 i = curbuf->b_ml.ml_line_count;
1796 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001798 if (qf_ptr->qf_col > 0)
1799 {
1800 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1801 if (qf_ptr->qf_viscol == TRUE)
1802 {
1803 /*
1804 * Check each character from the beginning of the error
1805 * line up to the error column. For each tab character
1806 * found, reduce the error column value by the length of
1807 * a tab character.
1808 */
1809 line = ml_get_curline();
1810 screen_col = 0;
1811 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1812 {
1813 if (*line == NUL)
1814 break;
1815 if (*line++ == '\t')
1816 {
1817 curwin->w_cursor.col -= 7 - (screen_col % 8);
1818 screen_col += 8 - (screen_col % 8);
1819 }
1820 else
1821 ++screen_col;
1822 }
1823 }
1824 check_cursor();
1825 }
1826 else
1827 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001830 {
1831 pos_T save_cursor;
1832
1833 /* Move the cursor to the first line in the buffer */
1834 save_cursor = curwin->w_cursor;
1835 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001836 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1837 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001838 curwin->w_cursor = save_cursor;
1839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841#ifdef FEAT_FOLDING
1842 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1843 foldOpenCursor();
1844#endif
1845 if (print_message)
1846 {
1847 /* Update the screen before showing the message */
1848 update_topline_redraw();
1849 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001850 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1852 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1853 /* Add the message, skipping leading whitespace and newlines. */
1854 len = (int)STRLEN(IObuff);
1855 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1856
1857 /* Output the message. Overwrite to avoid scrolling when the 'O'
1858 * flag is present in 'shortmess'; But when not jumping, print the
1859 * whole message. */
1860 i = msg_scroll;
1861 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1862 msg_scroll = TRUE;
1863 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1864 msg_scroll = FALSE;
1865 msg_attr_keep(IObuff, 0, TRUE);
1866 msg_scroll = i;
1867 }
1868 }
1869 else
1870 {
1871#ifdef FEAT_WINDOWS
1872 if (opened_window)
1873 win_close(curwin, TRUE); /* Close opened window */
1874#endif
1875 if (qf_ptr->qf_fnum != 0)
1876 {
1877 /*
1878 * Couldn't open file, so put index back where it was. This could
1879 * happen if the file was readonly and we changed something.
1880 */
1881#ifdef FEAT_WINDOWS
1882failed:
1883#endif
1884 qf_ptr = old_qf_ptr;
1885 qf_index = old_qf_index;
1886 }
1887 }
1888theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001889 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1890 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#ifdef FEAT_WINDOWS
1892 if (p_swb != old_swb && opened_window)
1893 {
1894 /* Restore old 'switchbuf' value, but not when an autocommand or
1895 * modeline has changed the value. */
1896 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001899 swb_flags = old_swb_flags;
1900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 else
1902 free_string_option(old_swb);
1903 }
1904#endif
1905}
1906
1907/*
1908 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001909 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 */
1911 void
1912qf_list(eap)
1913 exarg_T *eap;
1914{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001915 buf_T *buf;
1916 char_u *fname;
1917 qfline_T *qfp;
1918 int i;
1919 int idx1 = 1;
1920 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001921 char_u *arg = eap->arg;
1922 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001924 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001926 if (eap->cmdidx == CMD_llist)
1927 {
1928 qi = GET_LOC_LIST(curwin);
1929 if (qi == NULL)
1930 {
1931 EMSG(_(e_loclist));
1932 return;
1933 }
1934 }
1935
1936 if (qi->qf_curlist >= qi->qf_listcount
1937 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
1939 EMSG(_(e_quickfix));
1940 return;
1941 }
1942 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1943 {
1944 EMSG(_(e_trailing));
1945 return;
1946 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001947 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (idx1 < 0)
1949 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1950 if (idx2 < 0)
1951 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1952
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001953 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1956 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1959 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001960 msg_putchar('\n');
1961 if (got_int)
1962 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001963
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001964 fname = NULL;
1965 if (qfp->qf_fnum != 0
1966 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1967 {
1968 fname = buf->b_fname;
1969 if (qfp->qf_type == 1) /* :helpgrep */
1970 fname = gettail(fname);
1971 }
1972 if (fname == NULL)
1973 sprintf((char *)IObuff, "%2d", i);
1974 else
1975 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1976 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001977 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001978 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1979 if (qfp->qf_lnum == 0)
1980 IObuff[0] = NUL;
1981 else if (qfp->qf_col == 0)
1982 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1983 else
1984 sprintf((char *)IObuff, ":%ld col %d",
1985 qfp->qf_lnum, qfp->qf_col);
1986 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1987 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1988 msg_puts_attr(IObuff, hl_attr(HLF_N));
1989 if (qfp->qf_pattern != NULL)
1990 {
1991 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1992 STRCAT(IObuff, ":");
1993 msg_puts(IObuff);
1994 }
1995 msg_puts((char_u *)" ");
1996
1997 /* Remove newlines and leading whitespace from the text. For an
1998 * unrecognized line keep the indent, the compiler may mark a word
1999 * with ^^^^. */
2000 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2002 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002003 msg_prt_line(IObuff, FALSE);
2004 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002006
2007 qfp = qfp->qf_next;
2008 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 ui_breakcheck();
2010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011}
2012
2013/*
2014 * Remove newlines and leading whitespace from an error message.
2015 * Put the result in "buf[bufsize]".
2016 */
2017 static void
2018qf_fmt_text(text, buf, bufsize)
2019 char_u *text;
2020 char_u *buf;
2021 int bufsize;
2022{
2023 int i;
2024 char_u *p = text;
2025
2026 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2027 {
2028 if (*p == '\n')
2029 {
2030 buf[i] = ' ';
2031 while (*++p != NUL)
2032 if (!vim_iswhite(*p) && *p != '\n')
2033 break;
2034 }
2035 else
2036 buf[i] = *p++;
2037 }
2038 buf[i] = NUL;
2039}
2040
2041/*
2042 * ":colder [count]": Up in the quickfix stack.
2043 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002044 * ":lolder [count]": Up in the location list stack.
2045 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 */
2047 void
2048qf_age(eap)
2049 exarg_T *eap;
2050{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002051 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 int count;
2053
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002054 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2055 {
2056 qi = GET_LOC_LIST(curwin);
2057 if (qi == NULL)
2058 {
2059 EMSG(_(e_loclist));
2060 return;
2061 }
2062 }
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (eap->addr_count != 0)
2065 count = eap->line2;
2066 else
2067 count = 1;
2068 while (count--)
2069 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002070 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002072 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
2074 EMSG(_("E380: At bottom of quickfix stack"));
2075 return;
2076 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079 else
2080 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002081 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 EMSG(_("E381: At top of quickfix stack"));
2084 return;
2085 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002086 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002089 qf_msg(qi);
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091}
2092
2093 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002094qf_msg(qi)
2095 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
2097 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002098 qi->qf_curlist + 1, qi->qf_listcount,
2099 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002101 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102#endif
2103}
2104
2105/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002106 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 */
2108 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109qf_free(qi, idx)
2110 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 int idx;
2112{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002113 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002117 qfp = qi->qf_lists[idx].qf_start->qf_next;
2118 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2119 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2120 vim_free(qi->qf_lists[idx].qf_start);
2121 qi->qf_lists[idx].qf_start = qfp;
2122 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002124 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002125 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126}
2127
2128/*
2129 * qf_mark_adjust: adjust marks
2130 */
2131 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002132qf_mark_adjust(wp, line1, line2, amount, amount_after)
2133 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 linenr_T line1;
2135 linenr_T line2;
2136 long amount;
2137 long amount_after;
2138{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002139 int i;
2140 qfline_T *qfp;
2141 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002142 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002144 if (wp != NULL)
2145 {
2146 if (wp->w_llist == NULL)
2147 return;
2148 qi = wp->w_llist;
2149 }
2150
2151 for (idx = 0; idx < qi->qf_listcount; ++idx)
2152 if (qi->qf_lists[idx].qf_count)
2153 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2154 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (qfp->qf_fnum == curbuf->b_fnum)
2156 {
2157 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2158 {
2159 if (amount == MAXLNUM)
2160 qfp->qf_cleared = TRUE;
2161 else
2162 qfp->qf_lnum += amount;
2163 }
2164 else if (amount_after && qfp->qf_lnum > line2)
2165 qfp->qf_lnum += amount_after;
2166 }
2167}
2168
2169/*
2170 * Make a nice message out of the error character and the error number:
2171 * char number message
2172 * e or E 0 " error"
2173 * w or W 0 " warning"
2174 * i or I 0 " info"
2175 * 0 0 ""
2176 * other 0 " c"
2177 * e or E n " error n"
2178 * w or W n " warning n"
2179 * i or I n " info n"
2180 * 0 n " error n"
2181 * other n " c n"
2182 * 1 x "" :helpgrep
2183 */
2184 static char_u *
2185qf_types(c, nr)
2186 int c, nr;
2187{
2188 static char_u buf[20];
2189 static char_u cc[3];
2190 char_u *p;
2191
2192 if (c == 'W' || c == 'w')
2193 p = (char_u *)" warning";
2194 else if (c == 'I' || c == 'i')
2195 p = (char_u *)" info";
2196 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2197 p = (char_u *)" error";
2198 else if (c == 0 || c == 1)
2199 p = (char_u *)"";
2200 else
2201 {
2202 cc[0] = ' ';
2203 cc[1] = c;
2204 cc[2] = NUL;
2205 p = cc;
2206 }
2207
2208 if (nr <= 0)
2209 return p;
2210
2211 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2212 return buf;
2213}
2214
2215#if defined(FEAT_WINDOWS) || defined(PROTO)
2216/*
2217 * ":cwindow": open the quickfix window if we have errors to display,
2218 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002219 * ":lwindow": open the location list window if we have locations to display,
2220 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 */
2222 void
2223ex_cwindow(eap)
2224 exarg_T *eap;
2225{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002226 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 win_T *win;
2228
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002229 if (eap->cmdidx == CMD_lwindow)
2230 {
2231 qi = GET_LOC_LIST(curwin);
2232 if (qi == NULL)
2233 return;
2234 }
2235
2236 /* Look for an existing quickfix window. */
2237 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239 /*
2240 * If a quickfix window is open but we have no errors to display,
2241 * close the window. If a quickfix window is not open, then open
2242 * it if we have errors; otherwise, leave it closed.
2243 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002245 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002246 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
2248 if (win != NULL)
2249 ex_cclose(eap);
2250 }
2251 else if (win == NULL)
2252 ex_copen(eap);
2253}
2254
2255/*
2256 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002257 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 void
2260ex_cclose(eap)
2261 exarg_T *eap;
2262{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002263 win_T *win = NULL;
2264 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002266 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2267 {
2268 qi = GET_LOC_LIST(curwin);
2269 if (qi == NULL)
2270 return;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002273 /* Find existing quickfix window and close it. */
2274 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (win != NULL)
2276 win_close(win, FALSE);
2277}
2278
2279/*
2280 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002281 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 */
2283 void
2284ex_copen(eap)
2285 exarg_T *eap;
2286{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002287 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002290 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002291 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002292 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002294 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2295 {
2296 qi = GET_LOC_LIST(curwin);
2297 if (qi == NULL)
2298 {
2299 EMSG(_(e_loclist));
2300 return;
2301 }
2302 }
2303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (eap->addr_count != 0)
2305 height = eap->line2;
2306 else
2307 height = QF_WINHEIGHT;
2308
2309#ifdef FEAT_VISUAL
2310 reset_VIsual_and_resel(); /* stop Visual mode */
2311#endif
2312#ifdef FEAT_GUI
2313 need_mouse_correct = TRUE;
2314#endif
2315
2316 /*
2317 * Find existing quickfix window, or open a new one.
2318 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002319 win = qf_find_win(qi);
2320
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002321 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 win_goto(win);
2323 else
2324 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002325 qf_buf = qf_find_buf(qi);
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 /* The current window becomes the previous window afterwards. */
2328 win = curwin;
2329
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002330 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2331 /* Create the new window at the very bottom. */
2332 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002333 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002335 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 /*
2340 * For the location list window, create a reference to the
2341 * location list from the window 'win'.
2342 */
2343 curwin->w_llist_ref = win->w_llist;
2344 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002346
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002347 if (oldwin != curwin)
2348 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002349 if (qf_buf != NULL)
2350 /* Use the existing quickfix buffer */
2351 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002352 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002353 else
2354 {
2355 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002356 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002357 /* switch off 'swapfile' */
2358 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2359 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002360 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002361 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002362 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002363#ifdef FEAT_DIFF
2364 curwin->w_p_diff = FALSE;
2365#endif
2366#ifdef FEAT_FOLDING
2367 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2368 OPT_LOCAL);
2369#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002372 /* Only set the height when still in the same tab page and there is no
2373 * window to the side. */
2374 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002375#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002376 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002377#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002378 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 win_setheight(height);
2380 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2381 if (win_valid(win))
2382 prevwin = win;
2383 }
2384
2385 /*
2386 * Fill the buffer with the quickfix list.
2387 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002388 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002390 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2391 set_internal_string_var((char_u *)"w:quickfix_title",
2392 qi->qf_lists[qi->qf_curlist].qf_title);
2393
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002394 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 curwin->w_cursor.col = 0;
2396 check_cursor();
2397 update_topline(); /* scroll to show the line */
2398}
2399
2400/*
2401 * Return the number of the current entry (line number in the quickfix
2402 * window).
2403 */
2404 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405qf_current_entry(wp)
2406 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002408 qf_info_T *qi = &ql_info;
2409
2410 if (IS_LL_WINDOW(wp))
2411 /* In the location list window, use the referenced location list */
2412 qi = wp->w_llist_ref;
2413
2414 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415}
2416
2417/*
2418 * Update the cursor position in the quickfix window to the current error.
2419 * Return TRUE if there is a quickfix window.
2420 */
2421 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002422qf_win_pos_update(qi, old_qf_index)
2423 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 int old_qf_index; /* previous qf_index or zero */
2425{
2426 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002427 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 /*
2430 * Put the cursor on the current error in the quickfix window, so that
2431 * it's viewable.
2432 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (win != NULL
2435 && qf_index <= win->w_buffer->b_ml.ml_line_count
2436 && old_qf_index != qf_index)
2437 {
2438 win_T *old_curwin = curwin;
2439
2440 curwin = win;
2441 curbuf = win->w_buffer;
2442 if (qf_index > old_qf_index)
2443 {
2444 curwin->w_redraw_top = old_qf_index;
2445 curwin->w_redraw_bot = qf_index;
2446 }
2447 else
2448 {
2449 curwin->w_redraw_top = qf_index;
2450 curwin->w_redraw_bot = old_qf_index;
2451 }
2452 curwin->w_cursor.lnum = qf_index;
2453 curwin->w_cursor.col = 0;
2454 update_topline(); /* scroll to show the line */
2455 redraw_later(VALID);
2456 curwin->w_redr_status = TRUE; /* update ruler */
2457 curwin = old_curwin;
2458 curbuf = curwin->w_buffer;
2459 }
2460 return win != NULL;
2461}
2462
2463/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002464 * Check whether the given window is displaying the specified quickfix/location
2465 * list buffer
2466 */
2467 static int
2468is_qf_win(win, qi)
2469 win_T *win;
2470 qf_info_T *qi;
2471{
2472 /*
2473 * A window displaying the quickfix buffer will have the w_llist_ref field
2474 * set to NULL.
2475 * A window displaying a location list buffer will have the w_llist_ref
2476 * pointing to the location list.
2477 */
2478 if (bt_quickfix(win->w_buffer))
2479 if ((qi == &ql_info && win->w_llist_ref == NULL)
2480 || (qi != &ql_info && win->w_llist_ref == qi))
2481 return TRUE;
2482
2483 return FALSE;
2484}
2485
2486/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002487 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002488 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002489 */
2490 static win_T *
2491qf_find_win(qi)
2492 qf_info_T *qi;
2493{
2494 win_T *win;
2495
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002496 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002497 if (is_qf_win(win, qi))
2498 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002499
2500 return win;
2501}
2502
2503/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002504 * Find a quickfix buffer.
2505 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
2507 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002508qf_find_buf(qi)
2509 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002511 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002512 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
Bram Moolenaar9c102382006-05-03 21:26:49 +00002514 FOR_ALL_TAB_WINDOWS(tp, win)
2515 if (is_qf_win(win, qi))
2516 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002517
Bram Moolenaar9c102382006-05-03 21:26:49 +00002518 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519}
2520
2521/*
2522 * Find the quickfix buffer. If it exists, update the contents.
2523 */
2524 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002525qf_update_buffer(qi)
2526 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002532 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (buf != NULL)
2534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 /* set curwin/curbuf to buf and save a few things */
2536 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002538 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 /* restore curwin/curbuf and a few other things */
2541 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002543 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545}
2546
2547/*
2548 * Fill current buffer with quickfix errors, replacing any previous contents.
2549 * curbuf must be the quickfix buffer!
2550 */
2551 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002552qf_fill_buffer(qi)
2553 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002555 linenr_T lnum;
2556 qfline_T *qfp;
2557 buf_T *errbuf;
2558 int len;
2559 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
2561 /* delete all existing lines */
2562 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2563 (void)ml_delete((linenr_T)1, FALSE);
2564
2565 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002566 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002569 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2570 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
2572 if (qfp->qf_fnum != 0
2573 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2574 && errbuf->b_fname != NULL)
2575 {
2576 if (qfp->qf_type == 1) /* :helpgrep */
2577 STRCPY(IObuff, gettail(errbuf->b_fname));
2578 else
2579 STRCPY(IObuff, errbuf->b_fname);
2580 len = (int)STRLEN(IObuff);
2581 }
2582 else
2583 len = 0;
2584 IObuff[len++] = '|';
2585
2586 if (qfp->qf_lnum > 0)
2587 {
2588 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2589 len += (int)STRLEN(IObuff + len);
2590
2591 if (qfp->qf_col > 0)
2592 {
2593 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2594 len += (int)STRLEN(IObuff + len);
2595 }
2596
2597 sprintf((char *)IObuff + len, "%s",
2598 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2599 len += (int)STRLEN(IObuff + len);
2600 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002601 else if (qfp->qf_pattern != NULL)
2602 {
2603 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2604 len += (int)STRLEN(IObuff + len);
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 IObuff[len++] = '|';
2607 IObuff[len++] = ' ';
2608
2609 /* Remove newlines and leading whitespace from the text.
2610 * For an unrecognized line keep the indent, the compiler may
2611 * mark a word with ^^^^. */
2612 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2613 IObuff + len, IOSIZE - len);
2614
2615 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2616 == FAIL)
2617 break;
2618 qfp = qfp->qf_next;
2619 }
2620 /* Delete the empty line which is now at the end */
2621 (void)ml_delete(lnum + 1, FALSE);
2622 }
2623
2624 /* correct cursor position */
2625 check_lnums(TRUE);
2626
2627 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2628 * resembles reading a file into a buffer, it's more logical when using
2629 * autocommands. */
2630 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2631 curbuf->b_p_ma = FALSE;
2632
2633#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002634 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2636 FALSE, curbuf);
2637 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2638 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002639 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#endif
2641
2642 /* make sure it will be redrawn */
2643 redraw_curbuf_later(NOT_VALID);
2644
2645 /* Restore KeyTyped, setting 'filetype' may reset it. */
2646 KeyTyped = old_KeyTyped;
2647}
2648
2649#endif /* FEAT_WINDOWS */
2650
2651/*
2652 * Return TRUE if "buf" is the quickfix buffer.
2653 */
2654 int
2655bt_quickfix(buf)
2656 buf_T *buf;
2657{
2658 return (buf->b_p_bt[0] == 'q');
2659}
2660
2661/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002662 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2663 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 */
2665 int
2666bt_nofile(buf)
2667 buf_T *buf;
2668{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002669 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2670 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671}
2672
2673/*
2674 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2675 */
2676 int
2677bt_dontwrite(buf)
2678 buf_T *buf;
2679{
2680 return (buf->b_p_bt[0] == 'n');
2681}
2682
2683 int
2684bt_dontwrite_msg(buf)
2685 buf_T *buf;
2686{
2687 if (bt_dontwrite(buf))
2688 {
2689 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2690 return TRUE;
2691 }
2692 return FALSE;
2693}
2694
2695/*
2696 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2697 * and 'bufhidden'.
2698 */
2699 int
2700buf_hide(buf)
2701 buf_T *buf;
2702{
2703 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2704 switch (buf->b_p_bh[0])
2705 {
2706 case 'u': /* "unload" */
2707 case 'w': /* "wipe" */
2708 case 'd': return FALSE; /* "delete" */
2709 case 'h': return TRUE; /* "hide" */
2710 }
2711 return (p_hid || cmdmod.hide);
2712}
2713
2714/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002715 * Return TRUE when using ":vimgrep" for ":grep".
2716 */
2717 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002718grep_internal(cmdidx)
2719 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002720{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002721 return ((cmdidx == CMD_grep
2722 || cmdidx == CMD_lgrep
2723 || cmdidx == CMD_grepadd
2724 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002725 && STRCMP("internal",
2726 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2727}
2728
2729/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002730 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 */
2732 void
2733ex_make(eap)
2734 exarg_T *eap;
2735{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002736 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 char_u *cmd;
2738 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002739 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002740 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002741 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002742#ifdef FEAT_AUTOCMD
2743 char_u *au_name = NULL;
2744
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002745 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2746 if (grep_internal(eap->cmdidx))
2747 {
2748 ex_vimgrep(eap);
2749 return;
2750 }
2751
Bram Moolenaar7c626922005-02-07 22:01:03 +00002752 switch (eap->cmdidx)
2753 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002754 case CMD_make: au_name = (char_u *)"make"; break;
2755 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2756 case CMD_grep: au_name = (char_u *)"grep"; break;
2757 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2758 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2759 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002760 default: break;
2761 }
2762 if (au_name != NULL)
2763 {
2764 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2765 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002766# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002767 if (did_throw || force_abort)
2768 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002769# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002770 }
2771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
Bram Moolenaara6557602006-02-04 22:43:20 +00002773 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2774 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002775 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002778 fname = get_mef_name();
2779 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002781 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /*
2784 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2785 */
2786 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2787 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002788 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 cmd = alloc(len);
2790 if (cmd == NULL)
2791 return;
2792 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2793 (char *)p_shq);
2794 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002795 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 /*
2797 * Output a newline if there's something else than the :make command that
2798 * was typed (in which case the cursor is in column 0).
2799 */
2800 if (msg_col == 0)
2801 msg_didout = FALSE;
2802 msg_start();
2803 MSG_PUTS(":!");
2804 msg_outtrans(cmd); /* show what we are doing */
2805
2806 /* let the shell know if we are redirecting output or not */
2807 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2808
2809#ifdef AMIGA
2810 out_flush();
2811 /* read window status report and redraw before message */
2812 (void)char_avail();
2813#endif
2814
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002815 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002816 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2817 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002818 && eap->cmdidx != CMD_lgrepadd),
2819 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002820 if (wp != NULL)
2821 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002822#ifdef FEAT_AUTOCMD
2823 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002824 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002825 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2826 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002827 if (qi->qf_curlist < qi->qf_listcount)
2828 res = qi->qf_lists[qi->qf_curlist].qf_count;
2829 else
2830 res = 0;
2831 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002832#endif
2833 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002834 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
Bram Moolenaar7c626922005-02-07 22:01:03 +00002836 mch_remove(fname);
2837 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 vim_free(cmd);
2839}
2840
2841/*
2842 * Return the name for the errorfile, in allocated memory.
2843 * Find a new unique name when 'makeef' contains "##".
2844 * Returns NULL for error.
2845 */
2846 static char_u *
2847get_mef_name()
2848{
2849 char_u *p;
2850 char_u *name;
2851 static int start = -1;
2852 static int off = 0;
2853#ifdef HAVE_LSTAT
2854 struct stat sb;
2855#endif
2856
2857 if (*p_mef == NUL)
2858 {
2859 name = vim_tempname('e');
2860 if (name == NULL)
2861 EMSG(_(e_notmp));
2862 return name;
2863 }
2864
2865 for (p = p_mef; *p; ++p)
2866 if (p[0] == '#' && p[1] == '#')
2867 break;
2868
2869 if (*p == NUL)
2870 return vim_strsave(p_mef);
2871
2872 /* Keep trying until the name doesn't exist yet. */
2873 for (;;)
2874 {
2875 if (start == -1)
2876 start = mch_get_pid();
2877 else
2878 off += 19;
2879
2880 name = alloc((unsigned)STRLEN(p_mef) + 30);
2881 if (name == NULL)
2882 break;
2883 STRCPY(name, p_mef);
2884 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2885 STRCAT(name, p + 2);
2886 if (mch_getperm(name) < 0
2887#ifdef HAVE_LSTAT
2888 /* Don't accept a symbolic link, its a security risk. */
2889 && mch_lstat((char *)name, &sb) < 0
2890#endif
2891 )
2892 break;
2893 vim_free(name);
2894 }
2895 return name;
2896}
2897
2898/*
2899 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002900 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
2902 void
2903ex_cc(eap)
2904 exarg_T *eap;
2905{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002906 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002907
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002908 if (eap->cmdidx == CMD_ll
2909 || eap->cmdidx == CMD_lrewind
2910 || eap->cmdidx == CMD_lfirst
2911 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002912 {
2913 qi = GET_LOC_LIST(curwin);
2914 if (qi == NULL)
2915 {
2916 EMSG(_(e_loclist));
2917 return;
2918 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 }
2920
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002921 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 eap->addr_count > 0
2923 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002924 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002926 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2927 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 ? 1
2929 : 32767,
2930 eap->forceit);
2931}
2932
2933/*
2934 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 */
2937 void
2938ex_cnext(eap)
2939 exarg_T *eap;
2940{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002941 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002943 if (eap->cmdidx == CMD_lnext
2944 || eap->cmdidx == CMD_lNext
2945 || eap->cmdidx == CMD_lprevious
2946 || eap->cmdidx == CMD_lnfile
2947 || eap->cmdidx == CMD_lNfile
2948 || eap->cmdidx == CMD_lpfile)
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, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002960 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002962 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2963 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 ? BACKWARD_FILE
2965 : BACKWARD,
2966 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2967}
2968
2969/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002970 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002971 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 */
2973 void
2974ex_cfile(eap)
2975 exarg_T *eap;
2976{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002977 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002978 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979
2980 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2981 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002982 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002983
Bram Moolenaar9028b102010-07-11 16:58:51 +02002984#ifdef FEAT_BROWSE
2985 if (cmdmod.browse)
2986 {
2987 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
2988 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
2989 if (browse_file == NULL)
2990 return;
2991 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
2992 vim_free(browse_file);
2993 }
2994 else
2995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002997 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002998
2999 /*
3000 * This function is used by the :cfile, :cgetfile and :caddfile
3001 * commands.
3002 * :cfile always creates a new quickfix list and jumps to the
3003 * first error.
3004 * :cgetfile creates a new quickfix list but doesn't jump to the
3005 * first error.
3006 * :caddfile adds to an existing quickfix list. If there is no
3007 * quickfix list then a new list is created.
3008 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003009 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003010 && eap->cmdidx != CMD_laddfile),
3011 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003012 && (eap->cmdidx == CMD_cfile
3013 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003014 {
3015 if (wp != NULL)
3016 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003017 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020
3021/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003022 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003023 * ":vimgrepadd {pattern} file(s)"
3024 * ":lvimgrep {pattern} file(s)"
3025 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003026 */
3027 void
3028ex_vimgrep(eap)
3029 exarg_T *eap;
3030{
Bram Moolenaar81695252004-12-29 20:58:21 +00003031 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003032 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003033 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003034 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003035 char_u *s;
3036 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003037 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003038 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003039 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003040 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003041 buf_T *buf;
3042 int duplicate_name = FALSE;
3043 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003044 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003045 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003046 buf_T *first_match_buf = NULL;
3047 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003048 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003049#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3050 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003051#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003052 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003053 int flags = 0;
3054 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003055 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003056 char_u *dirname_start = NULL;
3057 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003058 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003059#ifdef FEAT_AUTOCMD
3060 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003061
3062 switch (eap->cmdidx)
3063 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003064 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3065 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3066 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003067 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003068 case CMD_grep: au_name = (char_u *)"grep"; break;
3069 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3070 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3071 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003072 default: break;
3073 }
3074 if (au_name != NULL)
3075 {
3076 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3077 curbuf->b_fname, TRUE, curbuf);
3078 if (did_throw || force_abort)
3079 return;
3080 }
3081#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003082
Bram Moolenaar754b5602006-02-09 23:53:20 +00003083 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003084 || eap->cmdidx == CMD_lvimgrep
3085 || eap->cmdidx == CMD_lgrepadd
3086 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003087 {
3088 qi = ll_get_or_alloc_list(curwin);
3089 if (qi == NULL)
3090 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003091 }
3092
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003093 if (eap->addr_count > 0)
3094 tomatch = eap->line2;
3095 else
3096 tomatch = MAXLNUM;
3097
Bram Moolenaar81695252004-12-29 20:58:21 +00003098 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003099 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003100 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003101 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003102 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003103 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003104 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003105 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003106 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003107 if (regmatch.regprog == NULL)
3108 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003109 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003110 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003111
3112 p = skipwhite(p);
3113 if (*p == NUL)
3114 {
3115 EMSG(_("E683: File name missing or invalid pattern"));
3116 goto theend;
3117 }
3118
Bram Moolenaar754b5602006-02-09 23:53:20 +00003119 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003120 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003121 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003122 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003123 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003124 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003125 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003126 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003127 prevp->qf_next != prevp; prevp = prevp->qf_next)
3128 ;
3129
3130 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003131 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003132 goto theend;
3133 if (fcount == 0)
3134 {
3135 EMSG(_(e_nomatch));
3136 goto theend;
3137 }
3138
Bram Moolenaard9462e32011-04-11 21:35:11 +02003139 dirname_start = alloc(MAXPATHL);
3140 dirname_now = alloc(MAXPATHL);
3141 if (dirname_start == NULL || dirname_now == NULL)
3142 goto theend;
3143
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003144 /* Remember the current directory, because a BufRead autocommand that does
3145 * ":lcd %:p:h" changes the meaning of short path names. */
3146 mch_dirname(dirname_start, MAXPATHL);
3147
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003148 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003149 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003150 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003151 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003152 if (time(NULL) > seconds)
3153 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003154 /* Display the file name every second or so, show the user we are
3155 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003156 seconds = time(NULL);
3157 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003158 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003159 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003160 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003161 else
3162 {
3163 msg_outtrans(p);
3164 vim_free(p);
3165 }
3166 msg_clr_eos();
3167 msg_didout = FALSE; /* overwrite this message */
3168 msg_nowait = TRUE; /* don't wait for this message */
3169 msg_col = 0;
3170 out_flush();
3171 }
3172
Bram Moolenaar81695252004-12-29 20:58:21 +00003173 buf = buflist_findname_exp(fnames[fi]);
3174 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3175 {
3176 /* Remember that a buffer with this name already exists. */
3177 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003178 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003179 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003180
3181#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3182 /* Don't do Filetype autocommands to avoid loading syntax and
3183 * indent scripts, a great speed improvement. */
3184 save_ei = au_event_disable(",Filetype");
3185#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003186 /* Don't use modelines here, it's useless. */
3187 save_mls = p_mls;
3188 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003189
3190 /* Load file into a buffer, so that 'fileencoding' is detected,
3191 * autocommands applied, etc. */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003192 buf = load_dummy_buffer(fname);
3193
3194 /* When autocommands changed directory: go back. We assume it was
3195 * ":lcd %:p:h". */
3196 mch_dirname(dirname_now, MAXPATHL);
3197 if (STRCMP(dirname_start, dirname_now) != 0)
3198 {
3199 exarg_T ea;
3200
3201 ea.arg = dirname_start;
3202 ea.cmdidx = CMD_lcd;
3203 ex_cd(&ea);
3204 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003205
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003206 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003207#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3208 au_event_restore(save_ei);
3209#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003210 }
3211 else
3212 /* Use existing, loaded buffer. */
3213 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003214
Bram Moolenaar81695252004-12-29 20:58:21 +00003215 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003216 {
3217 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003218 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003219 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003220 else
3221 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003222 /* Try for a match in all lines of the buffer.
3223 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003224 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003225 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3226 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003227 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003228 col = 0;
3229 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003230 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003231 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003232 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003233 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003234 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003235 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003236 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003237 ml_get_buf(buf,
3238 regmatch.startpos[0].lnum + lnum, FALSE),
3239 regmatch.startpos[0].lnum + lnum,
3240 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003241 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003242 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003243 0, /* nr */
3244 0, /* type */
3245 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003246 ) == FAIL)
3247 {
3248 got_int = TRUE;
3249 break;
3250 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003251 found_match = TRUE;
3252 if (--tomatch == 0)
3253 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003254 if ((flags & VGR_GLOBAL) == 0
3255 || regmatch.endpos[0].lnum > 0)
3256 break;
3257 col = regmatch.endpos[0].col
3258 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003259 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003260 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003261 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003262 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003263 if (got_int)
3264 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003265 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003266
3267 if (using_dummy)
3268 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003269 if (found_match && first_match_buf == NULL)
3270 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003271 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003272 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003273 /* Never keep a dummy buffer if there is another buffer
3274 * with the same name. */
3275 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003276 buf = NULL;
3277 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003278 else if (!cmdmod.hide
3279 || buf->b_p_bh[0] == 'u' /* "unload" */
3280 || buf->b_p_bh[0] == 'w' /* "wipe" */
3281 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003282 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003283 /* When no match was found we don't need to remember the
3284 * buffer, wipe it out. If there was a match and it
3285 * wasn't the first one or we won't jump there: only
3286 * unload the buffer.
3287 * Ignore 'hidden' here, because it may lead to having too
3288 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003289 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003290 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003291 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003292 buf = NULL;
3293 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003294 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003295 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003296 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003297 buf = NULL;
3298 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003299 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003300
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003301 if (buf != NULL)
3302 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003303 /* If the buffer is still loaded we need to use the
3304 * directory we jumped to below. */
3305 if (buf == first_match_buf
3306 && target_dir == NULL
3307 && STRCMP(dirname_start, dirname_now) != 0)
3308 target_dir = vim_strsave(dirname_now);
3309
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003310 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003311 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003312 * need to be done (again). But not the window-local
3313 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003314 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003315#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003316 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3317 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003318#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003319 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003320 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003321 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003322 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003323 }
3324 }
3325
3326 FreeWild(fcount, fnames);
3327
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003328 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3329 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3330 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003331
3332#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003333 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003334#endif
3335
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003336#ifdef FEAT_AUTOCMD
3337 if (au_name != NULL)
3338 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3339 curbuf->b_fname, TRUE, curbuf);
3340#endif
3341
Bram Moolenaar86b68352004-12-27 21:59:20 +00003342 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003343 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003344 {
3345 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003346 {
3347 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003348 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003349 if (buf != curbuf)
3350 /* If we jumped to another buffer redrawing will already be
3351 * taken care of. */
3352 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003353
3354 /* Jump to the directory used after loading the buffer. */
3355 if (curbuf == first_match_buf && target_dir != NULL)
3356 {
3357 exarg_T ea;
3358
3359 ea.arg = target_dir;
3360 ea.cmdidx = CMD_lcd;
3361 ex_cd(&ea);
3362 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003363 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003364 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003365 else
3366 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003367
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003368 /* If we loaded a dummy buffer into the current window, the autocommands
3369 * may have messed up things, need to redraw and recompute folds. */
3370 if (redraw_for_dummy)
3371 {
3372#ifdef FEAT_FOLDING
3373 foldUpdateAll(curwin);
3374#else
3375 redraw_later(NOT_VALID);
3376#endif
3377 }
3378
Bram Moolenaar86b68352004-12-27 21:59:20 +00003379theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003380 vim_free(dirname_now);
3381 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003382 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003383 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003384}
3385
3386/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003387 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003388 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003389 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3390 * If "s" is not NULL terminate the pattern with a NUL.
3391 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003392 */
3393 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003394skip_vimgrep_pat(p, s, flags)
3395 char_u *p;
3396 char_u **s;
3397 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003398{
3399 int c;
3400
3401 if (vim_isIDc(*p))
3402 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003403 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003404 if (s != NULL)
3405 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003406 p = skiptowhite(p);
3407 if (s != NULL && *p != NUL)
3408 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003409 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003410 else
3411 {
3412 /* ":vimgrep /pattern/[g][j] fname" */
3413 if (s != NULL)
3414 *s = p + 1;
3415 c = *p;
3416 p = skip_regexp(p + 1, c, TRUE, NULL);
3417 if (*p != c)
3418 return NULL;
3419
3420 /* Truncate the pattern. */
3421 if (s != NULL)
3422 *p = NUL;
3423 ++p;
3424
3425 /* Find the flags */
3426 while (*p == 'g' || *p == 'j')
3427 {
3428 if (flags != NULL)
3429 {
3430 if (*p == 'g')
3431 *flags |= VGR_GLOBAL;
3432 else
3433 *flags |= VGR_NOJUMP;
3434 }
3435 ++p;
3436 }
3437 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003438 return p;
3439}
3440
3441/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003442 * Load file "fname" into a dummy buffer and return the buffer pointer.
3443 * Returns NULL if it fails.
3444 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3445 */
3446 static buf_T *
3447load_dummy_buffer(fname)
3448 char_u *fname;
3449{
3450 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003451 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003452 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003453 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003454
3455 /* Allocate a buffer without putting it in the buffer list. */
3456 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3457 if (newbuf == NULL)
3458 return NULL;
3459
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003460 /* Init the options. */
3461 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3462
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003463 /* need to open the memfile before putting the buffer in a window */
3464 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003465 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003466 /* set curwin/curbuf to buf and save a few things */
3467 aucmd_prepbuf(&aco, newbuf);
3468
3469 /* Need to set the filename for autocommands. */
3470 (void)setfname(curbuf, fname, NULL, FALSE);
3471
Bram Moolenaar81695252004-12-29 20:58:21 +00003472 /* Create swap file now to avoid the ATTENTION message. */
3473 check_need_swap(TRUE);
3474
3475 /* Remove the "dummy" flag, otherwise autocommands may not
3476 * work. */
3477 curbuf->b_flags &= ~BF_DUMMY;
3478
3479 if (readfile(fname, NULL,
3480 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3481 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003482 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003483 && !(curbuf->b_flags & BF_NEW))
3484 {
3485 failed = FALSE;
3486 if (curbuf != newbuf)
3487 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003488 /* Bloody autocommands changed the buffer! Can happen when
3489 * using netrw and editing a remote file. Use the current
3490 * buffer instead, delete the dummy one after restoring the
3491 * window stuff. */
3492 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003493 newbuf = curbuf;
3494 }
3495 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003496
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003497 /* restore curwin/curbuf and a few other things */
3498 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003499 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3500 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003501 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003502
3503 if (!buf_valid(newbuf))
3504 return NULL;
3505 if (failed)
3506 {
3507 wipe_dummy_buffer(newbuf);
3508 return NULL;
3509 }
3510 return newbuf;
3511}
3512
3513/*
3514 * Wipe out the dummy buffer that load_dummy_buffer() created.
3515 */
3516 static void
3517wipe_dummy_buffer(buf)
3518 buf_T *buf;
3519{
3520 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003521 {
3522#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3523 cleanup_T cs;
3524
3525 /* Reset the error/interrupt/exception state here so that aborting()
3526 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3527 * work when got_int is set. */
3528 enter_cleanup(&cs);
3529#endif
3530
Bram Moolenaar81695252004-12-29 20:58:21 +00003531 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003532
3533#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3534 /* Restore the error/interrupt/exception state if not discarded by a
3535 * new aborting error, interrupt, or uncaught exception. */
3536 leave_cleanup(&cs);
3537#endif
3538 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003539}
3540
3541/*
3542 * Unload the dummy buffer that load_dummy_buffer() created.
3543 */
3544 static void
3545unload_dummy_buffer(buf)
3546 buf_T *buf;
3547{
3548 if (curbuf != buf) /* safety check */
3549 close_buffer(NULL, buf, DOBUF_UNLOAD);
3550}
3551
Bram Moolenaar05159a02005-02-26 23:04:13 +00003552#if defined(FEAT_EVAL) || defined(PROTO)
3553/*
3554 * Add each quickfix error to list "list" as a dictionary.
3555 */
3556 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003557get_errorlist(wp, list)
3558 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003559 list_T *list;
3560{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003561 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003562 dict_T *dict;
3563 char_u buf[2];
3564 qfline_T *qfp;
3565 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003566 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003567
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003568 if (wp != NULL)
3569 {
3570 qi = GET_LOC_LIST(wp);
3571 if (qi == NULL)
3572 return FAIL;
3573 }
3574
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003575 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003576 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003577 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003578
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003579 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3580 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003581 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003582 /* Handle entries with a non-existing buffer number. */
3583 bufnum = qfp->qf_fnum;
3584 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3585 bufnum = 0;
3586
Bram Moolenaar05159a02005-02-26 23:04:13 +00003587 if ((dict = dict_alloc()) == NULL)
3588 return FAIL;
3589 if (list_append_dict(list, dict) == FAIL)
3590 return FAIL;
3591
3592 buf[0] = qfp->qf_type;
3593 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003594 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003595 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3596 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3597 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3598 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003599 || dict_add_nr_str(dict, "pattern", 0L,
3600 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3601 || dict_add_nr_str(dict, "text", 0L,
3602 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003603 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3604 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3605 return FAIL;
3606
3607 qfp = qfp->qf_next;
3608 }
3609 return OK;
3610}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003611
3612/*
3613 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003614 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003615 */
3616 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003617set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003618 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003619 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003620 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003621 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003622{
3623 listitem_T *li;
3624 dict_T *d;
3625 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003626 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003627 long lnum;
3628 int col, nr;
3629 int vcol;
3630 qfline_T *prevp = NULL;
3631 int valid, status;
3632 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003633 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003634 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003635
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003636 if (wp != NULL)
3637 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003638 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003639 if (qi == NULL)
3640 return FAIL;
3641 }
3642
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003643 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003644 /* make place for a new list */
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003645 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003646 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003647 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003648 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003649 prevp->qf_next != prevp; prevp = prevp->qf_next)
3650 ;
3651 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003652 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003653
3654 for (li = list->lv_first; li != NULL; li = li->li_next)
3655 {
3656 if (li->li_tv.v_type != VAR_DICT)
3657 continue; /* Skip non-dict items */
3658
3659 d = li->li_tv.vval.v_dict;
3660 if (d == NULL)
3661 continue;
3662
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003663 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003664 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003665 lnum = get_dict_number(d, (char_u *)"lnum");
3666 col = get_dict_number(d, (char_u *)"col");
3667 vcol = get_dict_number(d, (char_u *)"vcol");
3668 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003669 type = get_dict_string(d, (char_u *)"type", TRUE);
3670 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3671 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003672 if (text == NULL)
3673 text = vim_strsave((char_u *)"");
3674
3675 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003676 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003677 valid = FALSE;
3678
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003679 /* Mark entries with non-existing buffer number as not valid. Give the
3680 * error message only once. */
3681 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3682 {
3683 if (!did_bufnr_emsg)
3684 {
3685 did_bufnr_emsg = TRUE;
3686 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3687 }
3688 valid = FALSE;
3689 bufnum = 0;
3690 }
3691
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003692 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003693 NULL, /* dir */
3694 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003695 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003696 text,
3697 lnum,
3698 col,
3699 vcol, /* vis_col */
3700 pattern, /* search pattern */
3701 nr,
3702 type == NULL ? NUL : *type,
3703 valid);
3704
3705 vim_free(filename);
3706 vim_free(pattern);
3707 vim_free(text);
3708 vim_free(type);
3709
3710 if (status == FAIL)
3711 {
3712 retval = FAIL;
3713 break;
3714 }
3715 }
3716
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003717 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003718 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003719 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3720 else
3721 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003722 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3723 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003724
3725#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003726 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003727#endif
3728
3729 return retval;
3730}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003731#endif
3732
Bram Moolenaar81695252004-12-29 20:58:21 +00003733/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003734 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003735 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003736 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003737 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003738 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003739 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003740 */
3741 void
3742ex_cbuffer(eap)
3743 exarg_T *eap;
3744{
3745 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003746 qf_info_T *qi = &ql_info;
3747
Bram Moolenaardb552d602006-03-23 22:59:57 +00003748 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3749 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003750 {
3751 qi = ll_get_or_alloc_list(curwin);
3752 if (qi == NULL)
3753 return;
3754 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003755
3756 if (*eap->arg == NUL)
3757 buf = curbuf;
3758 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3759 buf = buflist_findnr(atoi((char *)eap->arg));
3760 if (buf == NULL)
3761 EMSG(_(e_invarg));
3762 else if (buf->b_ml.ml_mfp == NULL)
3763 EMSG(_("E681: Buffer is not loaded"));
3764 else
3765 {
3766 if (eap->addr_count == 0)
3767 {
3768 eap->line1 = 1;
3769 eap->line2 = buf->b_ml.ml_line_count;
3770 }
3771 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3772 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3773 EMSG(_(e_invrange));
3774 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003775 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003776 char_u *qf_title = *eap->cmdlinep;
3777
3778 if (buf->b_sfname)
3779 {
3780 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3781 (char *)qf_title, (char *)buf->b_sfname);
3782 qf_title = IObuff;
3783 }
3784
Bram Moolenaardb552d602006-03-23 22:59:57 +00003785 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3786 (eap->cmdidx != CMD_caddbuffer
3787 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003788 eap->line1, eap->line2,
3789 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003790 && (eap->cmdidx == CMD_cbuffer
3791 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003792 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3793 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003794 }
3795}
3796
Bram Moolenaar1e015462005-09-25 22:16:38 +00003797#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003798/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003799 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3800 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003801 */
3802 void
3803ex_cexpr(eap)
3804 exarg_T *eap;
3805{
3806 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003807 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003808
Bram Moolenaardb552d602006-03-23 22:59:57 +00003809 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3810 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003811 {
3812 qi = ll_get_or_alloc_list(curwin);
3813 if (qi == NULL)
3814 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003816
Bram Moolenaar4770d092006-01-12 23:22:24 +00003817 /* Evaluate the expression. When the result is a string or a list we can
3818 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003819 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003820 if (tv != NULL)
3821 {
3822 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3823 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3824 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003825 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3826 (eap->cmdidx != CMD_caddexpr
3827 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003828 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003829 && (eap->cmdidx == CMD_cexpr
3830 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003831 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003832 }
3833 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003834 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003835 free_tv(tv);
3836 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003837}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003838#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003839
3840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 * ":helpgrep {pattern}"
3842 */
3843 void
3844ex_helpgrep(eap)
3845 exarg_T *eap;
3846{
3847 regmatch_T regmatch;
3848 char_u *save_cpo;
3849 char_u *p;
3850 int fcount;
3851 char_u **fnames;
3852 FILE *fd;
3853 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003854 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003856#ifdef FEAT_MULTI_LANG
3857 char_u *lang;
3858#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003859 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003860 int new_qi = FALSE;
3861 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3864 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00003865 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003867#ifdef FEAT_MULTI_LANG
3868 /* Check for a specified language */
3869 lang = check_help_lang(eap->arg);
3870#endif
3871
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003872 if (eap->cmdidx == CMD_lhelpgrep)
3873 {
3874 /* Find an existing help window */
3875 FOR_ALL_WINDOWS(wp)
3876 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3877 break;
3878
3879 if (wp == NULL) /* Help window not found */
3880 qi = NULL;
3881 else
3882 qi = wp->w_llist;
3883
3884 if (qi == NULL)
3885 {
3886 /* Allocate a new location list for help text matches */
3887 if ((qi = ll_new_list()) == NULL)
3888 return;
3889 new_qi = TRUE;
3890 }
3891 }
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3894 regmatch.rm_ic = FALSE;
3895 if (regmatch.regprog != NULL)
3896 {
3897 /* create a new quickfix list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003898 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
3900 /* Go through all directories in 'runtimepath' */
3901 p = p_rtp;
3902 while (*p != NUL && !got_int)
3903 {
3904 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3905
3906 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3907 add_pathsep(NameBuff);
3908 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3909 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3910 &fnames, EW_FILE|EW_SILENT) == OK
3911 && fcount > 0)
3912 {
3913 for (fi = 0; fi < fcount && !got_int; ++fi)
3914 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003915#ifdef FEAT_MULTI_LANG
3916 /* Skip files for a different language. */
3917 if (lang != NULL
3918 && STRNICMP(lang, fnames[fi]
3919 + STRLEN(fnames[fi]) - 3, 2) != 0
3920 && !(STRNICMP(lang, "en", 2) == 0
3921 && STRNICMP("txt", fnames[fi]
3922 + STRLEN(fnames[fi]) - 3, 3) == 0))
3923 continue;
3924#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003925 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (fd != NULL)
3927 {
3928 lnum = 1;
3929 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3930 {
3931 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3932 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003933 int l = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935 /* remove trailing CR, LF, spaces, etc. */
3936 while (l > 0 && IObuff[l - 1] <= ' ')
3937 IObuff[--l] = NUL;
3938
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003939 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 NULL, /* dir */
3941 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003942 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 IObuff,
3944 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003945 (int)(regmatch.startp[0] - IObuff)
3946 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003947 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003948 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 0, /* nr */
3950 1, /* type */
3951 TRUE /* valid */
3952 ) == FAIL)
3953 {
3954 got_int = TRUE;
3955 break;
3956 }
3957 }
3958 ++lnum;
3959 line_breakcheck();
3960 }
3961 fclose(fd);
3962 }
3963 }
3964 FreeWild(fcount, fnames);
3965 }
3966 }
3967 vim_free(regmatch.regprog);
3968
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003969 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3970 qi->qf_lists[qi->qf_curlist].qf_ptr =
3971 qi->qf_lists[qi->qf_curlist].qf_start;
3972 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00003975 if (p_cpo == empty_option)
3976 p_cpo = save_cpo;
3977 else
3978 /* Darn, some plugin changed the value. */
3979 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003982 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983#endif
3984
3985 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003986 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003987 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003988 else
3989 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003990
3991 if (eap->cmdidx == CMD_lhelpgrep)
3992 {
3993 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003994 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003995 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3996 {
3997 if (new_qi)
3998 ll_free_all(&qi);
3999 }
4000 else if (curwin->w_llist == NULL)
4001 curwin->w_llist = qi;
4002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003}
4004
4005#endif /* FEAT_QUICKFIX */