blob: d23b03e2e0f8896a78463a91023b9faf146c790c [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));
Bram Moolenaarc95e3262011-08-10 18:36:54 +0200129static void qf_set_title __ARGS((qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000133static buf_T *load_dummy_buffer __ARGS((char_u *fname));
134static void wipe_dummy_buffer __ARGS((buf_T *buf));
135static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000136static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000138/* Quickfix window check helper macro */
139#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
140/* Location list window check helper macro */
141#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
142/*
143 * Return location list for window 'wp'
144 * For location list window, return the referenced location list
145 */
146#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
147
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000149 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200150 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 * Return -1 for error, number of errors for success.
152 */
153 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200154qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000155 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 char_u *efile;
157 char_u *errorformat;
158 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200159 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000161 qf_info_T *qi = &ql_info;
162
Bram Moolenaar86b68352004-12-27 21:59:20 +0000163 if (efile == NULL)
164 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000165
166 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000167 {
168 qi = ll_get_or_alloc_list(wp);
169 if (qi == NULL)
170 return FAIL;
171 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172
173 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200174 (linenr_T)0, (linenr_T)0,
175 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000176}
177
178/*
179 * Read the errorfile "efile" into memory, line by line, building the error
180 * list.
181 * Alternative: when "efile" is null read errors from buffer "buf".
182 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200183 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
184 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185 * Return -1 for error, number of errors for success.
186 */
187 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200188qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
189 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000190 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000191 char_u *efile;
192 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000193 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000194 char_u *errorformat;
195 int newlist; /* TRUE: start a new error list */
196 linenr_T lnumfirst; /* first line number to use */
197 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200198 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000199{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 char_u *namebuf;
201 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000202 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 char_u *fmtstr = NULL;
204 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000205 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 int type = 0;
207 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 long lnum = 0L;
210 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000211 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000212 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000214 efm_T *fmt_first = NULL;
215 efm_T *fmt_last = NULL;
216 efm_T *fmt_ptr;
217 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 char_u *efm;
219 char_u *ptr;
220 char_u *srcptr;
221 int len;
222 int i;
223 int round;
224 int idx = 0;
225 int multiline = FALSE;
226 int multiignore = FALSE;
227 int multiscan = FALSE;
228 int retval = -1; /* default: return error flag */
229 char_u *directory = NULL;
230 char_u *currfile = NULL;
231 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000232 char_u *p_str = NULL;
233 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 struct dir_stack_T *file_stack = NULL;
235 regmatch_T regmatch;
236 static struct fmtpattern
237 {
238 char_u convchar;
239 char *pattern;
240 } fmt_pat[FMT_PATTERNS] =
241 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000242 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {'n', "\\d\\+"},
244 {'l', "\\d\\+"},
245 {'c', "\\d\\+"},
246 {'t', "."},
247 {'m', ".\\+"},
248 {'r', ".*"},
249 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000250 {'v', "\\d\\+"},
251 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 };
253
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 namebuf = alloc(CMDBUFFSIZE + 1);
255 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000256 pattern = alloc(CMDBUFFSIZE + 1);
257 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 goto qf_init_end;
259
Bram Moolenaar86b68352004-12-27 21:59:20 +0000260 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
262 EMSG2(_(e_openerrf), efile);
263 goto qf_init_end;
264 }
265
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000266 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200268 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000269 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000271 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200272 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 ;
274
275/*
276 * Each part of the format string is copied and modified from errorformat to
277 * regex prog. Only a few % characters are allowed.
278 */
279 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000280 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000281 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 else
283 efm = errorformat;
284 /*
285 * Get some space to modify the format string into.
286 */
287 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
288 for (round = FMT_PATTERNS; round > 0; )
289 i += (int)STRLEN(fmt_pat[--round].pattern);
290#ifdef COLON_IN_FILENAME
291 i += 12; /* "%f" can become twelve chars longer */
292#else
293 i += 2; /* "%f" can become two chars longer */
294#endif
295 if ((fmtstr = alloc(i)) == NULL)
296 goto error2;
297
Bram Moolenaar01265852006-03-20 21:50:15 +0000298 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 {
300 /*
301 * Allocate a new eformat structure and put it at the end of the list
302 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000303 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 if (fmt_ptr == NULL)
305 goto error2;
306 if (fmt_first == NULL) /* first one */
307 fmt_first = fmt_ptr;
308 else
309 fmt_last->next = fmt_ptr;
310 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312 /*
313 * Isolate one part in the 'errorformat' option
314 */
315 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
316 if (efm[len] == '\\' && efm[len + 1] != NUL)
317 ++len;
318
319 /*
320 * Build regexp pattern from current 'errorformat' option
321 */
322 ptr = fmtstr;
323 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000324 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 for (efmp = efm; efmp < efm + len; ++efmp)
326 {
327 if (*efmp == '%')
328 {
329 ++efmp;
330 for (idx = 0; idx < FMT_PATTERNS; ++idx)
331 if (fmt_pat[idx].convchar == *efmp)
332 break;
333 if (idx < FMT_PATTERNS)
334 {
335 if (fmt_ptr->addr[idx])
336 {
337 sprintf((char *)errmsg,
338 _("E372: Too many %%%c in format string"), *efmp);
339 EMSG(errmsg);
340 goto error2;
341 }
342 if ((idx
343 && idx < 6
344 && vim_strchr((char_u *)"DXOPQ",
345 fmt_ptr->prefix) != NULL)
346 || (idx == 6
347 && vim_strchr((char_u *)"OPQ",
348 fmt_ptr->prefix) == NULL))
349 {
350 sprintf((char *)errmsg,
351 _("E373: Unexpected %%%c in format string"), *efmp);
352 EMSG(errmsg);
353 goto error2;
354 }
355 fmt_ptr->addr[idx] = (char_u)++round;
356 *ptr++ = '\\';
357 *ptr++ = '(';
358#ifdef BACKSLASH_IN_FILENAME
359 if (*efmp == 'f')
360 {
361 /* Also match "c:" in the file name, even when
362 * checking for a colon next: "%f:".
363 * "\%(\a:\)\=" */
364 STRCPY(ptr, "\\%(\\a:\\)\\=");
365 ptr += 10;
366 }
367#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000368 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000370 if (efmp[1] != '\\' && efmp[1] != '%')
371 {
372 /* A file name may contain spaces, but this isn't
373 * in "\f". For "%f:%l:%m" there may be a ":" in
374 * the file name. Use ".\{-1,}x" instead (x is
375 * the next character), the requirement that :999:
376 * follows should work. */
377 STRCPY(ptr, ".\\{-1,}");
378 ptr += 7;
379 }
380 else
381 {
382 /* File name followed by '\\' or '%': include as
383 * many file name chars as possible. */
384 STRCPY(ptr, "\\f\\+");
385 ptr += 4;
386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 else
389 {
390 srcptr = (char_u *)fmt_pat[idx].pattern;
391 while ((*ptr = *srcptr++) != NUL)
392 ++ptr;
393 }
394 *ptr++ = '\\';
395 *ptr++ = ')';
396 }
397 else if (*efmp == '*')
398 {
399 if (*++efmp == '[' || *efmp == '\\')
400 {
401 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
402 {
403 if (efmp[1] == '^')
404 *ptr++ = *++efmp;
405 if (efmp < efm + len)
406 {
407 *ptr++ = *++efmp; /* could be ']' */
408 while (efmp < efm + len
409 && (*ptr++ = *++efmp) != ']')
410 /* skip */;
411 if (efmp == efm + len)
412 {
413 EMSG(_("E374: Missing ] in format string"));
414 goto error2;
415 }
416 }
417 }
418 else if (efmp < efm + len) /* %*\D, %*\s etc. */
419 *ptr++ = *++efmp;
420 *ptr++ = '\\';
421 *ptr++ = '+';
422 }
423 else
424 {
425 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
426 sprintf((char *)errmsg,
427 _("E375: Unsupported %%%c in format string"), *efmp);
428 EMSG(errmsg);
429 goto error2;
430 }
431 }
432 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
433 *ptr++ = *efmp; /* regexp magic characters */
434 else if (*efmp == '#')
435 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000436 else if (*efmp == '>')
437 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 else if (efmp == efm + 1) /* analyse prefix */
439 {
440 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
441 fmt_ptr->flags = *efmp++;
442 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
443 fmt_ptr->prefix = *efmp;
444 else
445 {
446 sprintf((char *)errmsg,
447 _("E376: Invalid %%%c in format string prefix"), *efmp);
448 EMSG(errmsg);
449 goto error2;
450 }
451 }
452 else
453 {
454 sprintf((char *)errmsg,
455 _("E377: Invalid %%%c in format string"), *efmp);
456 EMSG(errmsg);
457 goto error2;
458 }
459 }
460 else /* copy normal character */
461 {
462 if (*efmp == '\\' && efmp + 1 < efm + len)
463 ++efmp;
464 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
465 *ptr++ = '\\'; /* escape regexp atoms */
466 if (*efmp)
467 *ptr++ = *efmp;
468 }
469 }
470 *ptr++ = '$';
471 *ptr = NUL;
472 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
473 goto error2;
474 /*
475 * Advance to next part
476 */
477 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
478 }
479 if (fmt_first == NULL) /* nothing found */
480 {
481 EMSG(_("E378: 'errorformat' contains no pattern"));
482 goto error2;
483 }
484
485 /*
486 * got_int is reset here, because it was probably set when killing the
487 * ":make" command, but we still want to read the errorfile then.
488 */
489 got_int = FALSE;
490
491 /* Always ignore case when looking for a matching error. */
492 regmatch.rm_ic = TRUE;
493
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000494 if (tv != NULL)
495 {
496 if (tv->v_type == VAR_STRING)
497 p_str = tv->vval.v_string;
498 else if (tv->v_type == VAR_LIST)
499 p_li = tv->vval.v_list->lv_first;
500 }
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 /*
503 * Read the lines in the error file one by one.
504 * Try to recognize one of the error formats in each line.
505 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000506 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000508 /* Get the next line. */
509 if (fd == NULL)
510 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000511 if (tv != NULL)
512 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000513 if (tv->v_type == VAR_STRING)
514 {
515 /* Get the next line from the supplied string */
516 char_u *p;
517
518 if (!*p_str) /* Reached the end of the string */
519 break;
520
521 p = vim_strchr(p_str, '\n');
522 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000523 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000524 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000525 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000526
527 if (len > CMDBUFFSIZE - 2)
528 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
529 else
530 vim_strncpy(IObuff, p_str, len);
531
532 p_str += len;
533 }
534 else if (tv->v_type == VAR_LIST)
535 {
536 /* Get the next line from the supplied list */
537 while (p_li && p_li->li_tv.v_type != VAR_STRING)
538 p_li = p_li->li_next; /* Skip non-string items */
539
540 if (!p_li) /* End of the list */
541 break;
542
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000543 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000544 if (len > CMDBUFFSIZE - 2)
545 len = CMDBUFFSIZE - 2;
546
547 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
548
549 p_li = p_li->li_next; /* next item */
550 }
551 }
552 else
553 {
554 /* Get the next line from the supplied buffer */
555 if (buflnum > lnumlast)
556 break;
557 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
558 CMDBUFFSIZE - 2);
559 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000560 }
561 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
562 break;
563
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200565#ifdef FEAT_MBYTE
566 remove_bom(IObuff);
567#endif
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
570 *efmp = NUL;
571#ifdef USE_CRNL
572 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
573 *efmp = NUL;
574#endif
575
Bram Moolenaar01265852006-03-20 21:50:15 +0000576 /* If there was no %> item start at the first pattern */
577 if (fmt_start == NULL)
578 fmt_ptr = fmt_first;
579 else
580 {
581 fmt_ptr = fmt_start;
582 fmt_start = NULL;
583 }
584
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 /*
586 * Try to match each part of 'errorformat' until we find a complete
587 * match or no match.
588 */
589 valid = TRUE;
590restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000591 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {
593 idx = fmt_ptr->prefix;
594 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
595 continue;
596 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000597 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (!multiscan)
599 errmsg[0] = NUL;
600 lnum = 0;
601 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000602 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 enr = -1;
604 type = 0;
605 tail = NULL;
606
607 regmatch.regprog = fmt_ptr->prog;
608 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
609 {
610 if ((idx == 'C' || idx == 'Z') && !multiline)
611 continue;
612 if (vim_strchr((char_u *)"EWI", idx) != NULL)
613 type = idx;
614 else
615 type = 0;
616 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000617 * Extract error message data from matched line.
618 * We check for an actual submatch, because "\[" and "\]" in
619 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 */
621 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
622 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000623 int c;
624
625 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
626 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000627
628 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000629 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000630 *regmatch.endp[i] = NUL;
631 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
632 *regmatch.endp[i] = c;
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000635 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 continue;
637 }
638 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000639 {
640 if (regmatch.startp[i] == NULL)
641 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000645 {
646 if (regmatch.startp[i] == NULL)
647 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000651 {
652 if (regmatch.startp[i] == NULL)
653 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000657 {
658 if (regmatch.startp[i] == NULL)
659 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000661 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000662 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 STRCPY(errmsg, IObuff);
664 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
665 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000666 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
667 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000669 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000672 {
673 if (regmatch.startp[i] == NULL)
674 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
678 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000679 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
680 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
682 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000683 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
686 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000687 if (regmatch.startp[i] == NULL)
688 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000690 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000692 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
693 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000694 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
695 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000696 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
697 if (len > CMDBUFFSIZE - 5)
698 len = CMDBUFFSIZE - 5;
699 STRCPY(pattern, "^\\V");
700 STRNCAT(pattern, regmatch.startp[i], len);
701 pattern[len + 3] = '\\';
702 pattern[len + 4] = '$';
703 pattern[len + 5] = NUL;
704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 break;
706 }
707 }
708 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000709
Bram Moolenaar4770d092006-01-12 23:22:24 +0000710 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000712 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 if (idx == 'D') /* enter directory */
715 {
716 if (*namebuf == NUL)
717 {
718 EMSG(_("E379: Missing or empty directory name"));
719 goto error2;
720 }
721 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
722 goto error2;
723 }
724 else if (idx == 'X') /* leave directory */
725 directory = qf_pop_dir(&dir_stack);
726 }
727 namebuf[0] = NUL; /* no match found, remove file name */
728 lnum = 0; /* don't jump to this line */
729 valid = FALSE;
730 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000731 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 multiline = multiignore = FALSE;
733 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000734 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000736 /* honor %> item */
737 if (fmt_ptr->conthere)
738 fmt_start = fmt_ptr;
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
741 multiline = TRUE; /* start of a multi-line message */
742 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
743 { /* continuation of multi-line msg */
744 if (qfprev == NULL)
745 goto error2;
746 if (*errmsg && !multiignore)
747 {
748 len = (int)STRLEN(qfprev->qf_text);
749 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
750 == NULL)
751 goto error2;
752 STRCPY(ptr, qfprev->qf_text);
753 vim_free(qfprev->qf_text);
754 qfprev->qf_text = ptr;
755 *(ptr += len) = '\n';
756 STRCPY(++ptr, errmsg);
757 }
758 if (qfprev->qf_nr == -1)
759 qfprev->qf_nr = enr;
760 if (vim_isprintc(type) && !qfprev->qf_type)
761 qfprev->qf_type = type; /* only printable chars allowed */
762 if (!qfprev->qf_lnum)
763 qfprev->qf_lnum = lnum;
764 if (!qfprev->qf_col)
765 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (!qfprev->qf_fnum)
768 qfprev->qf_fnum = qf_get_fnum(directory,
769 *namebuf || directory ? namebuf
770 : currfile && valid ? currfile : 0);
771 if (idx == 'Z')
772 multiline = multiignore = FALSE;
773 line_breakcheck();
774 continue;
775 }
776 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
777 {
778 /* global file names */
779 valid = FALSE;
780 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
781 {
782 if (*namebuf && idx == 'P')
783 currfile = qf_push_dir(namebuf, &file_stack);
784 else if (idx == 'Q')
785 currfile = qf_pop_dir(&file_stack);
786 *namebuf = NUL;
787 if (tail && *tail)
788 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000789 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 multiscan = TRUE;
791 goto restofline;
792 }
793 }
794 }
795 if (fmt_ptr->flags == '-') /* generally exclude this line */
796 {
797 if (multiline)
798 multiignore = TRUE; /* also exclude continuation lines */
799 continue;
800 }
801 }
802
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000803 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000805 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000807 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000808 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 errmsg,
810 lnum,
811 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000813 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 enr,
815 type,
816 valid) == FAIL)
817 goto error2;
818 line_breakcheck();
819 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000820 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000822 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000824 /* no valid entry found */
825 qi->qf_lists[qi->qf_curlist].qf_ptr =
826 qi->qf_lists[qi->qf_curlist].qf_start;
827 qi->qf_lists[qi->qf_curlist].qf_index = 1;
828 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 }
830 else
831 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000832 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
833 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
834 qi->qf_lists[qi->qf_curlist].qf_ptr =
835 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000837 /* return number of matches */
838 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 goto qf_init_ok;
840 }
841 EMSG(_(e_readerrf));
842error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000843 qf_free(qi, qi->qf_curlist);
844 qi->qf_listcount--;
845 if (qi->qf_curlist > 0)
846 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000848 if (fd != NULL)
849 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
851 {
852 fmt_first = fmt_ptr->next;
853 vim_free(fmt_ptr->prog);
854 vim_free(fmt_ptr);
855 }
856 qf_clean_dir_stack(&dir_stack);
857 qf_clean_dir_stack(&file_stack);
858qf_init_end:
859 vim_free(namebuf);
860 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000861 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 vim_free(fmtstr);
863
864#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000865 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#endif
867
868 return retval;
869}
870
871/*
872 * Prepare for adding a new quickfix list.
873 */
874 static void
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200875qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000876 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200877 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 int i;
880
881 /*
882 * If the current entry is not the last entry, delete entries below
883 * the current entry. This makes it possible to browse in a tree-like
884 * way with ":grep'.
885 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000886 while (qi->qf_listcount > qi->qf_curlist + 1)
887 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 /*
890 * When the stack is full, remove to oldest entry
891 * Otherwise, add a new entry.
892 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000893 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000895 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000897 qi->qf_lists[i - 1] = qi->qf_lists[i];
898 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000901 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100902 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
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 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911}
912
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000913/*
914 * Free a location list
915 */
916 static void
917ll_free_all(pqi)
918 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000919{
920 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000921 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000922
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000923 qi = *pqi;
924 if (qi == NULL)
925 return;
926 *pqi = NULL; /* Remove reference to this list */
927
928 qi->qf_refcount--;
929 if (qi->qf_refcount < 1)
930 {
931 /* No references to this location list */
932 for (i = 0; i < qi->qf_listcount; ++i)
933 qf_free(qi, i);
934 vim_free(qi);
935 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000936}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000937
938 void
939qf_free_all(wp)
940 win_T *wp;
941{
942 int i;
943 qf_info_T *qi = &ql_info;
944
945 if (wp != NULL)
946 {
947 /* location list */
948 ll_free_all(&wp->w_llist);
949 ll_free_all(&wp->w_llist_ref);
950 }
951 else
952 /* quickfix list */
953 for (i = 0; i < qi->qf_listcount; ++i)
954 qf_free(qi, i);
955}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957/*
958 * Add an entry to the end of the list of errors.
959 * Returns OK or FAIL.
960 */
961 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000962qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
963 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000964 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000965 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 char_u *dir; /* optional directory name */
967 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000968 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 char_u *mesg; /* message */
970 long lnum; /* line number */
971 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000972 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000973 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 int nr; /* error number */
975 int type; /* type character */
976 int valid; /* valid entry */
977{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000980 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000982 if (bufnum != 0)
983 qfp->qf_fnum = bufnum;
984 else
985 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
987 {
988 vim_free(qfp);
989 return FAIL;
990 }
991 qfp->qf_lnum = lnum;
992 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000993 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 if (pattern == NULL || *pattern == NUL)
995 qfp->qf_pattern = NULL;
996 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
997 {
998 vim_free(qfp->qf_text);
999 vim_free(qfp);
1000 return FAIL;
1001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 qfp->qf_nr = nr;
1003 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1004 type = 0;
1005 qfp->qf_type = type;
1006 qfp->qf_valid = valid;
1007
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001008 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1009 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001011 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 qfp->qf_prev = qfp; /* first element points to itself */
1013 }
1014 else
1015 {
1016 qfp->qf_prev = *prevp;
1017 (*prevp)->qf_next = qfp;
1018 }
1019 qfp->qf_next = qfp; /* last element points to itself */
1020 qfp->qf_cleared = FALSE;
1021 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001022 ++qi->qf_lists[qi->qf_curlist].qf_count;
1023 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1024 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001026 qi->qf_lists[qi->qf_curlist].qf_index =
1027 qi->qf_lists[qi->qf_curlist].qf_count;
1028 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001035 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001036 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001037 static qf_info_T *
1038ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001039{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001040 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001041
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001042 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1043 if (qi != NULL)
1044 {
1045 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1046 qi->qf_refcount++;
1047 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001048
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001049 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001050}
1051
1052/*
1053 * Return the location list for window 'wp'.
1054 * If not present, allocate a location list
1055 */
1056 static qf_info_T *
1057ll_get_or_alloc_list(wp)
1058 win_T *wp;
1059{
1060 if (IS_LL_WINDOW(wp))
1061 /* For a location list window, use the referenced location list */
1062 return wp->w_llist_ref;
1063
1064 /*
1065 * For a non-location list window, w_llist_ref should not point to a
1066 * location list.
1067 */
1068 ll_free_all(&wp->w_llist_ref);
1069
1070 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001071 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001072 return wp->w_llist;
1073}
1074
1075/*
1076 * Copy the location list from window "from" to window "to".
1077 */
1078 void
1079copy_loclist(from, to)
1080 win_T *from;
1081 win_T *to;
1082{
1083 qf_info_T *qi;
1084 int idx;
1085 int i;
1086
1087 /*
1088 * When copying from a location list window, copy the referenced
1089 * location list. For other windows, copy the location list for
1090 * that window.
1091 */
1092 if (IS_LL_WINDOW(from))
1093 qi = from->w_llist_ref;
1094 else
1095 qi = from->w_llist;
1096
1097 if (qi == NULL) /* no location list to copy */
1098 return;
1099
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001100 /* allocate a new location list */
1101 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001102 return;
1103
1104 to->w_llist->qf_listcount = qi->qf_listcount;
1105
1106 /* Copy the location lists one at a time */
1107 for (idx = 0; idx < qi->qf_listcount; idx++)
1108 {
1109 qf_list_T *from_qfl;
1110 qf_list_T *to_qfl;
1111
1112 to->w_llist->qf_curlist = idx;
1113
1114 from_qfl = &qi->qf_lists[idx];
1115 to_qfl = &to->w_llist->qf_lists[idx];
1116
1117 /* Some of the fields are populated by qf_add_entry() */
1118 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1119 to_qfl->qf_count = 0;
1120 to_qfl->qf_index = 0;
1121 to_qfl->qf_start = NULL;
1122 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001123 if (from_qfl->qf_title != NULL)
1124 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1125 else
1126 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001127
1128 if (from_qfl->qf_count)
1129 {
1130 qfline_T *from_qfp;
1131 qfline_T *prevp = NULL;
1132
1133 /* copy all the location entries in this list */
1134 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1135 ++i, from_qfp = from_qfp->qf_next)
1136 {
1137 if (qf_add_entry(to->w_llist, &prevp,
1138 NULL,
1139 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001140 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001141 from_qfp->qf_text,
1142 from_qfp->qf_lnum,
1143 from_qfp->qf_col,
1144 from_qfp->qf_viscol,
1145 from_qfp->qf_pattern,
1146 from_qfp->qf_nr,
1147 0,
1148 from_qfp->qf_valid) == FAIL)
1149 {
1150 qf_free_all(to);
1151 return;
1152 }
1153 /*
1154 * qf_add_entry() will not set the qf_num field, as the
1155 * directory and file names are not supplied. So the qf_fnum
1156 * field is copied here.
1157 */
1158 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1159 prevp->qf_type = from_qfp->qf_type; /* error type */
1160 if (from_qfl->qf_ptr == from_qfp)
1161 to_qfl->qf_ptr = prevp; /* current location */
1162 }
1163 }
1164
1165 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1166
1167 /* When no valid entries are present in the list, qf_ptr points to
1168 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001169 if (to_qfl->qf_nonevalid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001170 to_qfl->qf_ptr = to_qfl->qf_start;
1171 }
1172
1173 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1174}
1175
1176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 * get buffer number for file "dir.name"
1178 */
1179 static int
1180qf_get_fnum(directory, fname)
1181 char_u *directory;
1182 char_u *fname;
1183{
1184 if (fname == NULL || *fname == NUL) /* no file name */
1185 return 0;
1186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 char_u *ptr;
1188 int fnum;
1189
Bram Moolenaare60acc12011-05-10 16:41:25 +02001190#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001192#endif
1193#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (directory != NULL)
1195 slash_adjust(directory);
1196 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (directory != NULL && !vim_isAbsName(fname)
1199 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1200 {
1201 /*
1202 * Here we check if the file really exists.
1203 * This should normally be true, but if make works without
1204 * "leaving directory"-messages we might have missed a
1205 * directory change.
1206 */
1207 if (mch_getperm(ptr) < 0)
1208 {
1209 vim_free(ptr);
1210 directory = qf_guess_filepath(fname);
1211 if (directory)
1212 ptr = concat_fnames(directory, fname, TRUE);
1213 else
1214 ptr = vim_strsave(fname);
1215 }
1216 /* Use concatenated directory name and file name */
1217 fnum = buflist_add(ptr, 0);
1218 vim_free(ptr);
1219 return fnum;
1220 }
1221 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223}
1224
1225/*
1226 * push dirbuf onto the directory stack and return pointer to actual dir or
1227 * NULL on error
1228 */
1229 static char_u *
1230qf_push_dir(dirbuf, stackptr)
1231 char_u *dirbuf;
1232 struct dir_stack_T **stackptr;
1233{
1234 struct dir_stack_T *ds_new;
1235 struct dir_stack_T *ds_ptr;
1236
1237 /* allocate new stack element and hook it in */
1238 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1239 if (ds_new == NULL)
1240 return NULL;
1241
1242 ds_new->next = *stackptr;
1243 *stackptr = ds_new;
1244
1245 /* store directory on the stack */
1246 if (vim_isAbsName(dirbuf)
1247 || (*stackptr)->next == NULL
1248 || (*stackptr && dir_stack != *stackptr))
1249 (*stackptr)->dirname = vim_strsave(dirbuf);
1250 else
1251 {
1252 /* Okay we don't have an absolute path.
1253 * dirbuf must be a subdir of one of the directories on the stack.
1254 * Let's search...
1255 */
1256 ds_new = (*stackptr)->next;
1257 (*stackptr)->dirname = NULL;
1258 while (ds_new)
1259 {
1260 vim_free((*stackptr)->dirname);
1261 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1262 TRUE);
1263 if (mch_isdir((*stackptr)->dirname) == TRUE)
1264 break;
1265
1266 ds_new = ds_new->next;
1267 }
1268
1269 /* clean up all dirs we already left */
1270 while ((*stackptr)->next != ds_new)
1271 {
1272 ds_ptr = (*stackptr)->next;
1273 (*stackptr)->next = (*stackptr)->next->next;
1274 vim_free(ds_ptr->dirname);
1275 vim_free(ds_ptr);
1276 }
1277
1278 /* Nothing found -> it must be on top level */
1279 if (ds_new == NULL)
1280 {
1281 vim_free((*stackptr)->dirname);
1282 (*stackptr)->dirname = vim_strsave(dirbuf);
1283 }
1284 }
1285
1286 if ((*stackptr)->dirname != NULL)
1287 return (*stackptr)->dirname;
1288 else
1289 {
1290 ds_ptr = *stackptr;
1291 *stackptr = (*stackptr)->next;
1292 vim_free(ds_ptr);
1293 return NULL;
1294 }
1295}
1296
1297
1298/*
1299 * pop dirbuf from the directory stack and return previous directory or NULL if
1300 * stack is empty
1301 */
1302 static char_u *
1303qf_pop_dir(stackptr)
1304 struct dir_stack_T **stackptr;
1305{
1306 struct dir_stack_T *ds_ptr;
1307
1308 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1309 * What to do if it isn't? */
1310
1311 /* pop top element and free it */
1312 if (*stackptr != NULL)
1313 {
1314 ds_ptr = *stackptr;
1315 *stackptr = (*stackptr)->next;
1316 vim_free(ds_ptr->dirname);
1317 vim_free(ds_ptr);
1318 }
1319
1320 /* return NEW top element as current dir or NULL if stack is empty*/
1321 return *stackptr ? (*stackptr)->dirname : NULL;
1322}
1323
1324/*
1325 * clean up directory stack
1326 */
1327 static void
1328qf_clean_dir_stack(stackptr)
1329 struct dir_stack_T **stackptr;
1330{
1331 struct dir_stack_T *ds_ptr;
1332
1333 while ((ds_ptr = *stackptr) != NULL)
1334 {
1335 *stackptr = (*stackptr)->next;
1336 vim_free(ds_ptr->dirname);
1337 vim_free(ds_ptr);
1338 }
1339}
1340
1341/*
1342 * Check in which directory of the directory stack the given file can be
1343 * found.
1344 * Returns a pointer to the directory name or NULL if not found
1345 * Cleans up intermediate directory entries.
1346 *
1347 * TODO: How to solve the following problem?
1348 * If we have the this directory tree:
1349 * ./
1350 * ./aa
1351 * ./aa/bb
1352 * ./bb
1353 * ./bb/x.c
1354 * and make says:
1355 * making all in aa
1356 * making all in bb
1357 * x.c:9: Error
1358 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1359 * qf_guess_filepath will return NULL.
1360 */
1361 static char_u *
1362qf_guess_filepath(filename)
1363 char_u *filename;
1364{
1365 struct dir_stack_T *ds_ptr;
1366 struct dir_stack_T *ds_tmp;
1367 char_u *fullname;
1368
1369 /* no dirs on the stack - there's nothing we can do */
1370 if (dir_stack == NULL)
1371 return NULL;
1372
1373 ds_ptr = dir_stack->next;
1374 fullname = NULL;
1375 while (ds_ptr)
1376 {
1377 vim_free(fullname);
1378 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1379
1380 /* If concat_fnames failed, just go on. The worst thing that can happen
1381 * is that we delete the entire stack.
1382 */
1383 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1384 break;
1385
1386 ds_ptr = ds_ptr->next;
1387 }
1388
1389 vim_free(fullname);
1390
1391 /* clean up all dirs we already left */
1392 while (dir_stack->next != ds_ptr)
1393 {
1394 ds_tmp = dir_stack->next;
1395 dir_stack->next = dir_stack->next->next;
1396 vim_free(ds_tmp->dirname);
1397 vim_free(ds_tmp);
1398 }
1399
1400 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1401
1402}
1403
1404/*
1405 * jump to a quickfix line
1406 * if dir == FORWARD go "errornr" valid entries forward
1407 * if dir == BACKWARD go "errornr" valid entries backward
1408 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1409 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1410 * else if "errornr" is zero, redisplay the same line
1411 * else go to entry "errornr"
1412 */
1413 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001414qf_jump(qi, dir, errornr, forceit)
1415 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 int dir;
1417 int errornr;
1418 int forceit;
1419{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001420 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001421 qfline_T *qf_ptr;
1422 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 int qf_index;
1424 int old_qf_fnum;
1425 int old_qf_index;
1426 int prev_index;
1427 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1428 char_u *err = e_no_more_items;
1429 linenr_T i;
1430 buf_T *old_curbuf;
1431 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 colnr_T screen_col;
1433 colnr_T char_col;
1434 char_u *line;
1435#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001436 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001437 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 int opened_window = FALSE;
1439 win_T *win;
1440 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001441 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001443 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 int print_message = TRUE;
1445 int len;
1446#ifdef FEAT_FOLDING
1447 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1448#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001449 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001450 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001452 if (qi == NULL)
1453 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001454
1455 if (qi->qf_curlist >= qi->qf_listcount
1456 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
1458 EMSG(_(e_quickfix));
1459 return;
1460 }
1461
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001462 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001464 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 old_qf_index = qf_index;
1466 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1467 {
1468 while (errornr--)
1469 {
1470 old_qf_ptr = qf_ptr;
1471 prev_index = qf_index;
1472 old_qf_fnum = qf_ptr->qf_fnum;
1473 do
1474 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001475 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 || qf_ptr->qf_next == NULL)
1477 {
1478 qf_ptr = old_qf_ptr;
1479 qf_index = prev_index;
1480 if (err != NULL)
1481 {
1482 EMSG(_(err));
1483 goto theend;
1484 }
1485 errornr = 0;
1486 break;
1487 }
1488 ++qf_index;
1489 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001490 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1491 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1493 err = NULL;
1494 }
1495 }
1496 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1497 {
1498 while (errornr--)
1499 {
1500 old_qf_ptr = qf_ptr;
1501 prev_index = qf_index;
1502 old_qf_fnum = qf_ptr->qf_fnum;
1503 do
1504 {
1505 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1506 {
1507 qf_ptr = old_qf_ptr;
1508 qf_index = prev_index;
1509 if (err != NULL)
1510 {
1511 EMSG(_(err));
1512 goto theend;
1513 }
1514 errornr = 0;
1515 break;
1516 }
1517 --qf_index;
1518 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001519 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1520 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1522 err = NULL;
1523 }
1524 }
1525 else if (errornr != 0) /* go to specified number */
1526 {
1527 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1528 {
1529 --qf_index;
1530 qf_ptr = qf_ptr->qf_prev;
1531 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532 while (errornr > qf_index && qf_index <
1533 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 && qf_ptr->qf_next != NULL)
1535 {
1536 ++qf_index;
1537 qf_ptr = qf_ptr->qf_next;
1538 }
1539 }
1540
1541#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001542 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1543 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 /* No need to print the error message if it's visible in the error
1545 * window */
1546 print_message = FALSE;
1547
1548 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001549 * For ":helpgrep" find a help window or open one.
1550 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001551 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001552 {
1553 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001554
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001555 if (cmdmod.tab != 0)
1556 wp = NULL;
1557 else
1558 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1559 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1560 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001561 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1562 win_enter(wp, TRUE);
1563 else
1564 {
1565 /*
1566 * Split off help window; put it at far top if no position
1567 * specified, the current window is vertically split and narrow.
1568 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001569 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001570# ifdef FEAT_VERTSPLIT
1571 if (cmdmod.split == 0 && curwin->w_width != Columns
1572 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001573 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001574# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001575 if (qi != &ql_info)
1576 flags |= WSP_NEWLOC; /* don't copy the location list */
1577
1578 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001579 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001580 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001581
1582 if (curwin->w_height < p_hh)
1583 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001584
1585 if (qi != &ql_info) /* not a quickfix list */
1586 {
1587 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001588 curwin->w_llist = qi;
1589 qi->qf_refcount++;
1590 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001591 }
1592
1593 if (!p_im)
1594 restart_edit = 0; /* don't want insert mode in help file */
1595 }
1596
1597 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 * If currently in the quickfix window, find another window to show the
1599 * file in.
1600 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001601 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 /*
1604 * If there is no file specified, we don't know where to go.
1605 * But do advance, otherwise ":cn" gets stuck.
1606 */
1607 if (qf_ptr->qf_fnum == 0)
1608 goto theend;
1609
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001610 /* Locate a window showing a normal buffer */
1611 usable_win = 0;
1612 FOR_ALL_WINDOWS(win)
1613 if (win->w_buffer->b_p_bt[0] == NUL)
1614 {
1615 usable_win = 1;
1616 break;
1617 }
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001620 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001621 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001623 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001624 {
1625 tabpage_T *tp;
1626 win_T *wp;
1627
1628 FOR_ALL_TAB_WINDOWS(tp, wp)
1629 {
1630 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1631 {
1632 goto_tabpage_win(tp, wp);
1633 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001634 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001635 }
1636 }
1637 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001638win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001639
1640 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001641 * If there is only one window and it is the quickfix window, create a
1642 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001643 */
1644 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001646 ll_ref = curwin->w_llist_ref;
1647
Bram Moolenaar884ae642009-02-22 01:37:59 +00001648 flags = WSP_ABOVE;
1649 if (ll_ref != NULL)
1650 flags |= WSP_NEWLOC;
1651 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 goto failed; /* not enough room for window */
1653 opened_window = TRUE; /* close it when fail */
1654 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001655 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001656 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001657 if (ll_ref != NULL)
1658 {
1659 /* The new window should use the location list from the
1660 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001661 curwin->w_llist = ll_ref;
1662 ll_ref->qf_refcount++;
1663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
1665 else
1666 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001667 if (curwin->w_llist_ref != NULL)
1668 {
1669 /* In a location window */
1670 ll_ref = curwin->w_llist_ref;
1671
1672 /* Find the window with the same location list */
1673 FOR_ALL_WINDOWS(win)
1674 if (win->w_llist == ll_ref)
1675 break;
1676 if (win == NULL)
1677 {
1678 /* Find the window showing the selected file */
1679 FOR_ALL_WINDOWS(win)
1680 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1681 break;
1682 if (win == NULL)
1683 {
1684 /* Find a previous usable window */
1685 win = curwin;
1686 do
1687 {
1688 if (win->w_buffer->b_p_bt[0] == NUL)
1689 break;
1690 if (win->w_prev == NULL)
1691 win = lastwin; /* wrap around the top */
1692 else
1693 win = win->w_prev; /* go to previous window */
1694 } while (win != curwin);
1695 }
1696 }
1697 win_goto(win);
1698
1699 /* If the location list for the window is not set, then set it
1700 * to the location list from the location window */
1701 if (win->w_llist == NULL)
1702 {
1703 win->w_llist = ll_ref;
1704 ll_ref->qf_refcount++;
1705 }
1706 }
1707 else
1708 {
1709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 /*
1711 * Try to find a window that shows the right buffer.
1712 * Default to the window just above the quickfix buffer.
1713 */
1714 win = curwin;
1715 altwin = NULL;
1716 for (;;)
1717 {
1718 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1719 break;
1720 if (win->w_prev == NULL)
1721 win = lastwin; /* wrap around the top */
1722 else
1723 win = win->w_prev; /* go to previous window */
1724
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001725 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
1727 /* Didn't find it, go to the window before the quickfix
1728 * window. */
1729 if (altwin != NULL)
1730 win = altwin;
1731 else if (curwin->w_prev != NULL)
1732 win = curwin->w_prev;
1733 else
1734 win = curwin->w_next;
1735 break;
1736 }
1737
1738 /* Remember a usable window. */
1739 if (altwin == NULL && !win->w_p_pvw
1740 && win->w_buffer->b_p_bt[0] == NUL)
1741 altwin = win;
1742 }
1743
1744 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
1747 }
1748#endif
1749
1750 /*
1751 * If there is a file name,
1752 * read the wanted file if needed, and check autowrite etc.
1753 */
1754 old_curbuf = curbuf;
1755 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001756
1757 if (qf_ptr->qf_fnum != 0)
1758 {
1759 if (qf_ptr->qf_type == 1)
1760 {
1761 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1762 * set b_p_ro flag). */
1763 if (!can_abandon(curbuf, forceit))
1764 {
1765 EMSG(_(e_nowrtmsg));
1766 ok = FALSE;
1767 }
1768 else
1769 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001770 ECMD_HIDE + ECMD_SET_HELP,
1771 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001772 }
1773 else
1774 ok = buflist_getfile(qf_ptr->qf_fnum,
1775 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1776 }
1777
1778 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
1780 /* When not switched to another buffer, still need to set pc mark */
1781 if (curbuf == old_curbuf)
1782 setpcmark();
1783
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001784 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001786 /*
1787 * Go to line with error, unless qf_lnum is 0.
1788 */
1789 i = qf_ptr->qf_lnum;
1790 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001792 if (i > curbuf->b_ml.ml_line_count)
1793 i = curbuf->b_ml.ml_line_count;
1794 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001796 if (qf_ptr->qf_col > 0)
1797 {
1798 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1799 if (qf_ptr->qf_viscol == TRUE)
1800 {
1801 /*
1802 * Check each character from the beginning of the error
1803 * line up to the error column. For each tab character
1804 * found, reduce the error column value by the length of
1805 * a tab character.
1806 */
1807 line = ml_get_curline();
1808 screen_col = 0;
1809 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1810 {
1811 if (*line == NUL)
1812 break;
1813 if (*line++ == '\t')
1814 {
1815 curwin->w_cursor.col -= 7 - (screen_col % 8);
1816 screen_col += 8 - (screen_col % 8);
1817 }
1818 else
1819 ++screen_col;
1820 }
1821 }
1822 check_cursor();
1823 }
1824 else
1825 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001828 {
1829 pos_T save_cursor;
1830
1831 /* Move the cursor to the first line in the buffer */
1832 save_cursor = curwin->w_cursor;
1833 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001834 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1835 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001836 curwin->w_cursor = save_cursor;
1837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839#ifdef FEAT_FOLDING
1840 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1841 foldOpenCursor();
1842#endif
1843 if (print_message)
1844 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001845 /* Update the screen before showing the message, unless the screen
1846 * scrolled up. */
1847 if (!msg_scrolled)
1848 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 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)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002391 qf_set_title(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002392
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002393 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 curwin->w_cursor.col = 0;
2395 check_cursor();
2396 update_topline(); /* scroll to show the line */
2397}
2398
2399/*
2400 * Return the number of the current entry (line number in the quickfix
2401 * window).
2402 */
2403 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002404qf_current_entry(wp)
2405 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002407 qf_info_T *qi = &ql_info;
2408
2409 if (IS_LL_WINDOW(wp))
2410 /* In the location list window, use the referenced location list */
2411 qi = wp->w_llist_ref;
2412
2413 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414}
2415
2416/*
2417 * Update the cursor position in the quickfix window to the current error.
2418 * Return TRUE if there is a quickfix window.
2419 */
2420 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002421qf_win_pos_update(qi, old_qf_index)
2422 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 int old_qf_index; /* previous qf_index or zero */
2424{
2425 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
2428 /*
2429 * Put the cursor on the current error in the quickfix window, so that
2430 * it's viewable.
2431 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002432 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (win != NULL
2434 && qf_index <= win->w_buffer->b_ml.ml_line_count
2435 && old_qf_index != qf_index)
2436 {
2437 win_T *old_curwin = curwin;
2438
2439 curwin = win;
2440 curbuf = win->w_buffer;
2441 if (qf_index > old_qf_index)
2442 {
2443 curwin->w_redraw_top = old_qf_index;
2444 curwin->w_redraw_bot = qf_index;
2445 }
2446 else
2447 {
2448 curwin->w_redraw_top = qf_index;
2449 curwin->w_redraw_bot = old_qf_index;
2450 }
2451 curwin->w_cursor.lnum = qf_index;
2452 curwin->w_cursor.col = 0;
2453 update_topline(); /* scroll to show the line */
2454 redraw_later(VALID);
2455 curwin->w_redr_status = TRUE; /* update ruler */
2456 curwin = old_curwin;
2457 curbuf = curwin->w_buffer;
2458 }
2459 return win != NULL;
2460}
2461
2462/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002463 * Check whether the given window is displaying the specified quickfix/location
2464 * list buffer
2465 */
2466 static int
2467is_qf_win(win, qi)
2468 win_T *win;
2469 qf_info_T *qi;
2470{
2471 /*
2472 * A window displaying the quickfix buffer will have the w_llist_ref field
2473 * set to NULL.
2474 * A window displaying a location list buffer will have the w_llist_ref
2475 * pointing to the location list.
2476 */
2477 if (bt_quickfix(win->w_buffer))
2478 if ((qi == &ql_info && win->w_llist_ref == NULL)
2479 || (qi != &ql_info && win->w_llist_ref == qi))
2480 return TRUE;
2481
2482 return FALSE;
2483}
2484
2485/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002486 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002487 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002488 */
2489 static win_T *
2490qf_find_win(qi)
2491 qf_info_T *qi;
2492{
2493 win_T *win;
2494
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002495 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002496 if (is_qf_win(win, qi))
2497 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002498
2499 return win;
2500}
2501
2502/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002503 * Find a quickfix buffer.
2504 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 */
2506 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002507qf_find_buf(qi)
2508 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002510 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002511 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
Bram Moolenaar9c102382006-05-03 21:26:49 +00002513 FOR_ALL_TAB_WINDOWS(tp, win)
2514 if (is_qf_win(win, qi))
2515 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002516
Bram Moolenaar9c102382006-05-03 21:26:49 +00002517 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518}
2519
2520/*
2521 * Find the quickfix buffer. If it exists, update the contents.
2522 */
2523 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002524qf_update_buffer(qi)
2525 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
2527 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002528 win_T *win;
2529 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002533 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (buf != NULL)
2535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 /* set curwin/curbuf to buf and save a few things */
2537 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002539 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002541 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2542 && (win = qf_find_win(qi)) != NULL)
2543 {
2544 curwin_save = curwin;
2545 curwin = win;
2546 qf_set_title(qi);
2547 curwin = curwin_save;
2548
2549 }
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 /* restore curwin/curbuf and a few other things */
2552 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002554 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
2556}
2557
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002558 static void
2559qf_set_title(qi)
2560 qf_info_T *qi;
2561{
2562 set_internal_string_var((char_u *)"w:quickfix_title",
2563 qi->qf_lists[qi->qf_curlist].qf_title);
2564}
2565
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566/*
2567 * Fill current buffer with quickfix errors, replacing any previous contents.
2568 * curbuf must be the quickfix buffer!
2569 */
2570 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002571qf_fill_buffer(qi)
2572 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002574 linenr_T lnum;
2575 qfline_T *qfp;
2576 buf_T *errbuf;
2577 int len;
2578 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580 /* delete all existing lines */
2581 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2582 (void)ml_delete((linenr_T)1, FALSE);
2583
2584 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002585 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002588 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2589 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
2591 if (qfp->qf_fnum != 0
2592 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2593 && errbuf->b_fname != NULL)
2594 {
2595 if (qfp->qf_type == 1) /* :helpgrep */
2596 STRCPY(IObuff, gettail(errbuf->b_fname));
2597 else
2598 STRCPY(IObuff, errbuf->b_fname);
2599 len = (int)STRLEN(IObuff);
2600 }
2601 else
2602 len = 0;
2603 IObuff[len++] = '|';
2604
2605 if (qfp->qf_lnum > 0)
2606 {
2607 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2608 len += (int)STRLEN(IObuff + len);
2609
2610 if (qfp->qf_col > 0)
2611 {
2612 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2613 len += (int)STRLEN(IObuff + len);
2614 }
2615
2616 sprintf((char *)IObuff + len, "%s",
2617 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2618 len += (int)STRLEN(IObuff + len);
2619 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002620 else if (qfp->qf_pattern != NULL)
2621 {
2622 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2623 len += (int)STRLEN(IObuff + len);
2624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 IObuff[len++] = '|';
2626 IObuff[len++] = ' ';
2627
2628 /* Remove newlines and leading whitespace from the text.
2629 * For an unrecognized line keep the indent, the compiler may
2630 * mark a word with ^^^^. */
2631 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2632 IObuff + len, IOSIZE - len);
2633
2634 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2635 == FAIL)
2636 break;
2637 qfp = qfp->qf_next;
2638 }
2639 /* Delete the empty line which is now at the end */
2640 (void)ml_delete(lnum + 1, FALSE);
2641 }
2642
2643 /* correct cursor position */
2644 check_lnums(TRUE);
2645
2646 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2647 * resembles reading a file into a buffer, it's more logical when using
2648 * autocommands. */
2649 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2650 curbuf->b_p_ma = FALSE;
2651
2652#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002653 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2655 FALSE, curbuf);
2656 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2657 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002658 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#endif
2660
2661 /* make sure it will be redrawn */
2662 redraw_curbuf_later(NOT_VALID);
2663
2664 /* Restore KeyTyped, setting 'filetype' may reset it. */
2665 KeyTyped = old_KeyTyped;
2666}
2667
2668#endif /* FEAT_WINDOWS */
2669
2670/*
2671 * Return TRUE if "buf" is the quickfix buffer.
2672 */
2673 int
2674bt_quickfix(buf)
2675 buf_T *buf;
2676{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002677 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678}
2679
2680/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002681 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2682 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 */
2684 int
2685bt_nofile(buf)
2686 buf_T *buf;
2687{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002688 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2689 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690}
2691
2692/*
2693 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2694 */
2695 int
2696bt_dontwrite(buf)
2697 buf_T *buf;
2698{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002699 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700}
2701
2702 int
2703bt_dontwrite_msg(buf)
2704 buf_T *buf;
2705{
2706 if (bt_dontwrite(buf))
2707 {
2708 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2709 return TRUE;
2710 }
2711 return FALSE;
2712}
2713
2714/*
2715 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2716 * and 'bufhidden'.
2717 */
2718 int
2719buf_hide(buf)
2720 buf_T *buf;
2721{
2722 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2723 switch (buf->b_p_bh[0])
2724 {
2725 case 'u': /* "unload" */
2726 case 'w': /* "wipe" */
2727 case 'd': return FALSE; /* "delete" */
2728 case 'h': return TRUE; /* "hide" */
2729 }
2730 return (p_hid || cmdmod.hide);
2731}
2732
2733/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002734 * Return TRUE when using ":vimgrep" for ":grep".
2735 */
2736 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002737grep_internal(cmdidx)
2738 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002739{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002740 return ((cmdidx == CMD_grep
2741 || cmdidx == CMD_lgrep
2742 || cmdidx == CMD_grepadd
2743 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002744 && STRCMP("internal",
2745 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2746}
2747
2748/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002749 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 */
2751 void
2752ex_make(eap)
2753 exarg_T *eap;
2754{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002755 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 char_u *cmd;
2757 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002758 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002759 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002760 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002761#ifdef FEAT_AUTOCMD
2762 char_u *au_name = NULL;
2763
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002764 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2765 if (grep_internal(eap->cmdidx))
2766 {
2767 ex_vimgrep(eap);
2768 return;
2769 }
2770
Bram Moolenaar7c626922005-02-07 22:01:03 +00002771 switch (eap->cmdidx)
2772 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002773 case CMD_make: au_name = (char_u *)"make"; break;
2774 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2775 case CMD_grep: au_name = (char_u *)"grep"; break;
2776 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2777 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2778 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002779 default: break;
2780 }
2781 if (au_name != NULL)
2782 {
2783 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2784 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002785# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002786 if (did_throw || force_abort)
2787 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002788# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002789 }
2790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
Bram Moolenaara6557602006-02-04 22:43:20 +00002792 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2793 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002794 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002795
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002797 fname = get_mef_name();
2798 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002800 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 /*
2803 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2804 */
2805 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2806 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002807 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 cmd = alloc(len);
2809 if (cmd == NULL)
2810 return;
2811 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2812 (char *)p_shq);
2813 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002814 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 /*
2816 * Output a newline if there's something else than the :make command that
2817 * was typed (in which case the cursor is in column 0).
2818 */
2819 if (msg_col == 0)
2820 msg_didout = FALSE;
2821 msg_start();
2822 MSG_PUTS(":!");
2823 msg_outtrans(cmd); /* show what we are doing */
2824
2825 /* let the shell know if we are redirecting output or not */
2826 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2827
2828#ifdef AMIGA
2829 out_flush();
2830 /* read window status report and redraw before message */
2831 (void)char_avail();
2832#endif
2833
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002834 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002835 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2836 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002837 && eap->cmdidx != CMD_lgrepadd),
2838 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002839 if (wp != NULL)
2840 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002841#ifdef FEAT_AUTOCMD
2842 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002843 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002844 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2845 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002846 if (qi->qf_curlist < qi->qf_listcount)
2847 res = qi->qf_lists[qi->qf_curlist].qf_count;
2848 else
2849 res = 0;
2850 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002851#endif
2852 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002853 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
Bram Moolenaar7c626922005-02-07 22:01:03 +00002855 mch_remove(fname);
2856 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 vim_free(cmd);
2858}
2859
2860/*
2861 * Return the name for the errorfile, in allocated memory.
2862 * Find a new unique name when 'makeef' contains "##".
2863 * Returns NULL for error.
2864 */
2865 static char_u *
2866get_mef_name()
2867{
2868 char_u *p;
2869 char_u *name;
2870 static int start = -1;
2871 static int off = 0;
2872#ifdef HAVE_LSTAT
2873 struct stat sb;
2874#endif
2875
2876 if (*p_mef == NUL)
2877 {
2878 name = vim_tempname('e');
2879 if (name == NULL)
2880 EMSG(_(e_notmp));
2881 return name;
2882 }
2883
2884 for (p = p_mef; *p; ++p)
2885 if (p[0] == '#' && p[1] == '#')
2886 break;
2887
2888 if (*p == NUL)
2889 return vim_strsave(p_mef);
2890
2891 /* Keep trying until the name doesn't exist yet. */
2892 for (;;)
2893 {
2894 if (start == -1)
2895 start = mch_get_pid();
2896 else
2897 off += 19;
2898
2899 name = alloc((unsigned)STRLEN(p_mef) + 30);
2900 if (name == NULL)
2901 break;
2902 STRCPY(name, p_mef);
2903 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2904 STRCAT(name, p + 2);
2905 if (mch_getperm(name) < 0
2906#ifdef HAVE_LSTAT
2907 /* Don't accept a symbolic link, its a security risk. */
2908 && mch_lstat((char *)name, &sb) < 0
2909#endif
2910 )
2911 break;
2912 vim_free(name);
2913 }
2914 return name;
2915}
2916
2917/*
2918 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
2921 void
2922ex_cc(eap)
2923 exarg_T *eap;
2924{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002925 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002926
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002927 if (eap->cmdidx == CMD_ll
2928 || eap->cmdidx == CMD_lrewind
2929 || eap->cmdidx == CMD_lfirst
2930 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 {
2932 qi = GET_LOC_LIST(curwin);
2933 if (qi == NULL)
2934 {
2935 EMSG(_(e_loclist));
2936 return;
2937 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938 }
2939
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002940 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 eap->addr_count > 0
2942 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002943 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002945 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2946 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 ? 1
2948 : 32767,
2949 eap->forceit);
2950}
2951
2952/*
2953 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002954 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
2956 void
2957ex_cnext(eap)
2958 exarg_T *eap;
2959{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002960 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002961
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002962 if (eap->cmdidx == CMD_lnext
2963 || eap->cmdidx == CMD_lNext
2964 || eap->cmdidx == CMD_lprevious
2965 || eap->cmdidx == CMD_lnfile
2966 || eap->cmdidx == CMD_lNfile
2967 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002968 {
2969 qi = GET_LOC_LIST(curwin);
2970 if (qi == NULL)
2971 {
2972 EMSG(_(e_loclist));
2973 return;
2974 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002975 }
2976
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002977 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002981 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2982 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 ? BACKWARD_FILE
2984 : BACKWARD,
2985 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2986}
2987
2988/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002989 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002990 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 */
2992 void
2993ex_cfile(eap)
2994 exarg_T *eap;
2995{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002996 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002997 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002998
2999 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
3000 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003001 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003002
Bram Moolenaar9028b102010-07-11 16:58:51 +02003003#ifdef FEAT_BROWSE
3004 if (cmdmod.browse)
3005 {
3006 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3007 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3008 if (browse_file == NULL)
3009 return;
3010 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3011 vim_free(browse_file);
3012 }
3013 else
3014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003016 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003017
3018 /*
3019 * This function is used by the :cfile, :cgetfile and :caddfile
3020 * commands.
3021 * :cfile always creates a new quickfix list and jumps to the
3022 * first error.
3023 * :cgetfile creates a new quickfix list but doesn't jump to the
3024 * first error.
3025 * :caddfile adds to an existing quickfix list. If there is no
3026 * quickfix list then a new list is created.
3027 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003028 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003029 && eap->cmdidx != CMD_laddfile),
3030 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003031 && (eap->cmdidx == CMD_cfile
3032 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003033 {
3034 if (wp != NULL)
3035 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003036 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038}
3039
3040/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003041 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003042 * ":vimgrepadd {pattern} file(s)"
3043 * ":lvimgrep {pattern} file(s)"
3044 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003045 */
3046 void
3047ex_vimgrep(eap)
3048 exarg_T *eap;
3049{
Bram Moolenaar81695252004-12-29 20:58:21 +00003050 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003051 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003052 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003053 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003054 char_u *s;
3055 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003056 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003057 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003058 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003059 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003060 buf_T *buf;
3061 int duplicate_name = FALSE;
3062 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003063 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003064 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003065 buf_T *first_match_buf = NULL;
3066 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003067 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003068#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3069 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003070#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003071 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003072 int flags = 0;
3073 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003074 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003075 char_u *dirname_start = NULL;
3076 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003077 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003078#ifdef FEAT_AUTOCMD
3079 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003080
3081 switch (eap->cmdidx)
3082 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003083 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3084 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3085 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003086 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003087 case CMD_grep: au_name = (char_u *)"grep"; break;
3088 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3089 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3090 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003091 default: break;
3092 }
3093 if (au_name != NULL)
3094 {
3095 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3096 curbuf->b_fname, TRUE, curbuf);
3097 if (did_throw || force_abort)
3098 return;
3099 }
3100#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003101
Bram Moolenaar754b5602006-02-09 23:53:20 +00003102 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003103 || eap->cmdidx == CMD_lvimgrep
3104 || eap->cmdidx == CMD_lgrepadd
3105 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003106 {
3107 qi = ll_get_or_alloc_list(curwin);
3108 if (qi == NULL)
3109 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003110 }
3111
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003112 if (eap->addr_count > 0)
3113 tomatch = eap->line2;
3114 else
3115 tomatch = MAXLNUM;
3116
Bram Moolenaar81695252004-12-29 20:58:21 +00003117 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003118 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003119 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003120 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003121 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003122 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003123 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003124 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003125 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003126 if (regmatch.regprog == NULL)
3127 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003128 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003129 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003130
3131 p = skipwhite(p);
3132 if (*p == NUL)
3133 {
3134 EMSG(_("E683: File name missing or invalid pattern"));
3135 goto theend;
3136 }
3137
Bram Moolenaar754b5602006-02-09 23:53:20 +00003138 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003139 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003140 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003141 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003142 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003143 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003144 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003145 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003146 prevp->qf_next != prevp; prevp = prevp->qf_next)
3147 ;
3148
3149 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003150 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003151 goto theend;
3152 if (fcount == 0)
3153 {
3154 EMSG(_(e_nomatch));
3155 goto theend;
3156 }
3157
Bram Moolenaard9462e32011-04-11 21:35:11 +02003158 dirname_start = alloc(MAXPATHL);
3159 dirname_now = alloc(MAXPATHL);
3160 if (dirname_start == NULL || dirname_now == NULL)
3161 goto theend;
3162
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003163 /* Remember the current directory, because a BufRead autocommand that does
3164 * ":lcd %:p:h" changes the meaning of short path names. */
3165 mch_dirname(dirname_start, MAXPATHL);
3166
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003167 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003168 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003169 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003170 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003171 if (time(NULL) > seconds)
3172 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003173 /* Display the file name every second or so, show the user we are
3174 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003175 seconds = time(NULL);
3176 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003177 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003178 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003179 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003180 else
3181 {
3182 msg_outtrans(p);
3183 vim_free(p);
3184 }
3185 msg_clr_eos();
3186 msg_didout = FALSE; /* overwrite this message */
3187 msg_nowait = TRUE; /* don't wait for this message */
3188 msg_col = 0;
3189 out_flush();
3190 }
3191
Bram Moolenaar81695252004-12-29 20:58:21 +00003192 buf = buflist_findname_exp(fnames[fi]);
3193 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3194 {
3195 /* Remember that a buffer with this name already exists. */
3196 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003197 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003198 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003199
3200#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3201 /* Don't do Filetype autocommands to avoid loading syntax and
3202 * indent scripts, a great speed improvement. */
3203 save_ei = au_event_disable(",Filetype");
3204#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003205 /* Don't use modelines here, it's useless. */
3206 save_mls = p_mls;
3207 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003208
3209 /* Load file into a buffer, so that 'fileencoding' is detected,
3210 * autocommands applied, etc. */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003211 buf = load_dummy_buffer(fname);
3212
3213 /* When autocommands changed directory: go back. We assume it was
3214 * ":lcd %:p:h". */
3215 mch_dirname(dirname_now, MAXPATHL);
3216 if (STRCMP(dirname_start, dirname_now) != 0)
3217 {
3218 exarg_T ea;
3219
3220 ea.arg = dirname_start;
3221 ea.cmdidx = CMD_lcd;
3222 ex_cd(&ea);
3223 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003224
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003225 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003226#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3227 au_event_restore(save_ei);
3228#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003229 }
3230 else
3231 /* Use existing, loaded buffer. */
3232 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003233
Bram Moolenaar81695252004-12-29 20:58:21 +00003234 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003235 {
3236 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003237 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003238 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003239 else
3240 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003241 /* Try for a match in all lines of the buffer.
3242 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003243 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003244 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3245 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003246 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003247 col = 0;
3248 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003249 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003250 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003251 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003252 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003253 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003254 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003255 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003256 ml_get_buf(buf,
3257 regmatch.startpos[0].lnum + lnum, FALSE),
3258 regmatch.startpos[0].lnum + lnum,
3259 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003260 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003261 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003262 0, /* nr */
3263 0, /* type */
3264 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003265 ) == FAIL)
3266 {
3267 got_int = TRUE;
3268 break;
3269 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003270 found_match = TRUE;
3271 if (--tomatch == 0)
3272 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003273 if ((flags & VGR_GLOBAL) == 0
3274 || regmatch.endpos[0].lnum > 0)
3275 break;
3276 col = regmatch.endpos[0].col
3277 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003278 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003279 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003280 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003281 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003282 if (got_int)
3283 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003284 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003285
3286 if (using_dummy)
3287 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003288 if (found_match && first_match_buf == NULL)
3289 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003290 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003291 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003292 /* Never keep a dummy buffer if there is another buffer
3293 * with the same name. */
3294 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003295 buf = NULL;
3296 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003297 else if (!cmdmod.hide
3298 || buf->b_p_bh[0] == 'u' /* "unload" */
3299 || buf->b_p_bh[0] == 'w' /* "wipe" */
3300 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003301 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003302 /* When no match was found we don't need to remember the
3303 * buffer, wipe it out. If there was a match and it
3304 * wasn't the first one or we won't jump there: only
3305 * unload the buffer.
3306 * Ignore 'hidden' here, because it may lead to having too
3307 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003308 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003309 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003310 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003311 buf = NULL;
3312 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003313 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003314 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003315 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003316 buf = NULL;
3317 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003318 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003319
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003320 if (buf != NULL)
3321 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003322 /* If the buffer is still loaded we need to use the
3323 * directory we jumped to below. */
3324 if (buf == first_match_buf
3325 && target_dir == NULL
3326 && STRCMP(dirname_start, dirname_now) != 0)
3327 target_dir = vim_strsave(dirname_now);
3328
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003329 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003330 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003331 * need to be done (again). But not the window-local
3332 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003333 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003334#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003335 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3336 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003337#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003338 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003339 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003340 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003341 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003342 }
3343 }
3344
3345 FreeWild(fcount, fnames);
3346
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003347 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3348 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3349 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003350
3351#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003352 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003353#endif
3354
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003355#ifdef FEAT_AUTOCMD
3356 if (au_name != NULL)
3357 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3358 curbuf->b_fname, TRUE, curbuf);
3359#endif
3360
Bram Moolenaar86b68352004-12-27 21:59:20 +00003361 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003362 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003363 {
3364 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003365 {
3366 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003367 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003368 if (buf != curbuf)
3369 /* If we jumped to another buffer redrawing will already be
3370 * taken care of. */
3371 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003372
3373 /* Jump to the directory used after loading the buffer. */
3374 if (curbuf == first_match_buf && target_dir != NULL)
3375 {
3376 exarg_T ea;
3377
3378 ea.arg = target_dir;
3379 ea.cmdidx = CMD_lcd;
3380 ex_cd(&ea);
3381 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003382 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003383 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003384 else
3385 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003386
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003387 /* If we loaded a dummy buffer into the current window, the autocommands
3388 * may have messed up things, need to redraw and recompute folds. */
3389 if (redraw_for_dummy)
3390 {
3391#ifdef FEAT_FOLDING
3392 foldUpdateAll(curwin);
3393#else
3394 redraw_later(NOT_VALID);
3395#endif
3396 }
3397
Bram Moolenaar86b68352004-12-27 21:59:20 +00003398theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003399 vim_free(dirname_now);
3400 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003401 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003402 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003403}
3404
3405/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003406 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003407 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003408 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3409 * If "s" is not NULL terminate the pattern with a NUL.
3410 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003411 */
3412 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003413skip_vimgrep_pat(p, s, flags)
3414 char_u *p;
3415 char_u **s;
3416 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003417{
3418 int c;
3419
3420 if (vim_isIDc(*p))
3421 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003422 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003423 if (s != NULL)
3424 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003425 p = skiptowhite(p);
3426 if (s != NULL && *p != NUL)
3427 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003428 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003429 else
3430 {
3431 /* ":vimgrep /pattern/[g][j] fname" */
3432 if (s != NULL)
3433 *s = p + 1;
3434 c = *p;
3435 p = skip_regexp(p + 1, c, TRUE, NULL);
3436 if (*p != c)
3437 return NULL;
3438
3439 /* Truncate the pattern. */
3440 if (s != NULL)
3441 *p = NUL;
3442 ++p;
3443
3444 /* Find the flags */
3445 while (*p == 'g' || *p == 'j')
3446 {
3447 if (flags != NULL)
3448 {
3449 if (*p == 'g')
3450 *flags |= VGR_GLOBAL;
3451 else
3452 *flags |= VGR_NOJUMP;
3453 }
3454 ++p;
3455 }
3456 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003457 return p;
3458}
3459
3460/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003461 * Load file "fname" into a dummy buffer and return the buffer pointer.
3462 * Returns NULL if it fails.
3463 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3464 */
3465 static buf_T *
3466load_dummy_buffer(fname)
3467 char_u *fname;
3468{
3469 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003470 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003471 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003472 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003473
3474 /* Allocate a buffer without putting it in the buffer list. */
3475 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3476 if (newbuf == NULL)
3477 return NULL;
3478
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003479 /* Init the options. */
3480 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3481
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003482 /* need to open the memfile before putting the buffer in a window */
3483 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003484 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003485 /* set curwin/curbuf to buf and save a few things */
3486 aucmd_prepbuf(&aco, newbuf);
3487
3488 /* Need to set the filename for autocommands. */
3489 (void)setfname(curbuf, fname, NULL, FALSE);
3490
Bram Moolenaar81695252004-12-29 20:58:21 +00003491 /* Create swap file now to avoid the ATTENTION message. */
3492 check_need_swap(TRUE);
3493
3494 /* Remove the "dummy" flag, otherwise autocommands may not
3495 * work. */
3496 curbuf->b_flags &= ~BF_DUMMY;
3497
3498 if (readfile(fname, NULL,
3499 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3500 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003501 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003502 && !(curbuf->b_flags & BF_NEW))
3503 {
3504 failed = FALSE;
3505 if (curbuf != newbuf)
3506 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003507 /* Bloody autocommands changed the buffer! Can happen when
3508 * using netrw and editing a remote file. Use the current
3509 * buffer instead, delete the dummy one after restoring the
3510 * window stuff. */
3511 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003512 newbuf = curbuf;
3513 }
3514 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003515
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003516 /* restore curwin/curbuf and a few other things */
3517 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003518 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3519 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003520 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003521
3522 if (!buf_valid(newbuf))
3523 return NULL;
3524 if (failed)
3525 {
3526 wipe_dummy_buffer(newbuf);
3527 return NULL;
3528 }
3529 return newbuf;
3530}
3531
3532/*
3533 * Wipe out the dummy buffer that load_dummy_buffer() created.
3534 */
3535 static void
3536wipe_dummy_buffer(buf)
3537 buf_T *buf;
3538{
3539 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003540 {
3541#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3542 cleanup_T cs;
3543
3544 /* Reset the error/interrupt/exception state here so that aborting()
3545 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3546 * work when got_int is set. */
3547 enter_cleanup(&cs);
3548#endif
3549
Bram Moolenaar81695252004-12-29 20:58:21 +00003550 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003551
3552#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3553 /* Restore the error/interrupt/exception state if not discarded by a
3554 * new aborting error, interrupt, or uncaught exception. */
3555 leave_cleanup(&cs);
3556#endif
3557 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003558}
3559
3560/*
3561 * Unload the dummy buffer that load_dummy_buffer() created.
3562 */
3563 static void
3564unload_dummy_buffer(buf)
3565 buf_T *buf;
3566{
3567 if (curbuf != buf) /* safety check */
3568 close_buffer(NULL, buf, DOBUF_UNLOAD);
3569}
3570
Bram Moolenaar05159a02005-02-26 23:04:13 +00003571#if defined(FEAT_EVAL) || defined(PROTO)
3572/*
3573 * Add each quickfix error to list "list" as a dictionary.
3574 */
3575 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003576get_errorlist(wp, list)
3577 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003578 list_T *list;
3579{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003580 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003581 dict_T *dict;
3582 char_u buf[2];
3583 qfline_T *qfp;
3584 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003585 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003586
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003587 if (wp != NULL)
3588 {
3589 qi = GET_LOC_LIST(wp);
3590 if (qi == NULL)
3591 return FAIL;
3592 }
3593
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003594 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003595 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003596 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003597
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003598 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3599 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003600 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003601 /* Handle entries with a non-existing buffer number. */
3602 bufnum = qfp->qf_fnum;
3603 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3604 bufnum = 0;
3605
Bram Moolenaar05159a02005-02-26 23:04:13 +00003606 if ((dict = dict_alloc()) == NULL)
3607 return FAIL;
3608 if (list_append_dict(list, dict) == FAIL)
3609 return FAIL;
3610
3611 buf[0] = qfp->qf_type;
3612 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003613 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003614 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3615 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3616 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3617 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003618 || dict_add_nr_str(dict, "pattern", 0L,
3619 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3620 || dict_add_nr_str(dict, "text", 0L,
3621 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003622 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3623 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3624 return FAIL;
3625
3626 qfp = qfp->qf_next;
3627 }
3628 return OK;
3629}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003630
3631/*
3632 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003633 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003634 */
3635 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003636set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003637 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003638 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003639 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003640 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003641{
3642 listitem_T *li;
3643 dict_T *d;
3644 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003645 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003646 long lnum;
3647 int col, nr;
3648 int vcol;
3649 qfline_T *prevp = NULL;
3650 int valid, status;
3651 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003652 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003653 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003654
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003655 if (wp != NULL)
3656 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003657 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003658 if (qi == NULL)
3659 return FAIL;
3660 }
3661
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003662 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003663 /* make place for a new list */
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003664 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003665 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003666 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003667 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003668 prevp->qf_next != prevp; prevp = prevp->qf_next)
3669 ;
3670 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003671 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003672
3673 for (li = list->lv_first; li != NULL; li = li->li_next)
3674 {
3675 if (li->li_tv.v_type != VAR_DICT)
3676 continue; /* Skip non-dict items */
3677
3678 d = li->li_tv.vval.v_dict;
3679 if (d == NULL)
3680 continue;
3681
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003682 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003683 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003684 lnum = get_dict_number(d, (char_u *)"lnum");
3685 col = get_dict_number(d, (char_u *)"col");
3686 vcol = get_dict_number(d, (char_u *)"vcol");
3687 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003688 type = get_dict_string(d, (char_u *)"type", TRUE);
3689 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3690 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003691 if (text == NULL)
3692 text = vim_strsave((char_u *)"");
3693
3694 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003695 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003696 valid = FALSE;
3697
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003698 /* Mark entries with non-existing buffer number as not valid. Give the
3699 * error message only once. */
3700 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3701 {
3702 if (!did_bufnr_emsg)
3703 {
3704 did_bufnr_emsg = TRUE;
3705 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3706 }
3707 valid = FALSE;
3708 bufnum = 0;
3709 }
3710
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003711 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003712 NULL, /* dir */
3713 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003714 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003715 text,
3716 lnum,
3717 col,
3718 vcol, /* vis_col */
3719 pattern, /* search pattern */
3720 nr,
3721 type == NULL ? NUL : *type,
3722 valid);
3723
3724 vim_free(filename);
3725 vim_free(pattern);
3726 vim_free(text);
3727 vim_free(type);
3728
3729 if (status == FAIL)
3730 {
3731 retval = FAIL;
3732 break;
3733 }
3734 }
3735
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003736 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003737 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003738 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3739 else
3740 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003741 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3742 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003743
3744#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003745 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003746#endif
3747
3748 return retval;
3749}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003750#endif
3751
Bram Moolenaar81695252004-12-29 20:58:21 +00003752/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003753 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003754 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003755 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003756 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003757 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003758 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003759 */
3760 void
3761ex_cbuffer(eap)
3762 exarg_T *eap;
3763{
3764 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003765 qf_info_T *qi = &ql_info;
3766
Bram Moolenaardb552d602006-03-23 22:59:57 +00003767 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3768 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003769 {
3770 qi = ll_get_or_alloc_list(curwin);
3771 if (qi == NULL)
3772 return;
3773 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003774
3775 if (*eap->arg == NUL)
3776 buf = curbuf;
3777 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3778 buf = buflist_findnr(atoi((char *)eap->arg));
3779 if (buf == NULL)
3780 EMSG(_(e_invarg));
3781 else if (buf->b_ml.ml_mfp == NULL)
3782 EMSG(_("E681: Buffer is not loaded"));
3783 else
3784 {
3785 if (eap->addr_count == 0)
3786 {
3787 eap->line1 = 1;
3788 eap->line2 = buf->b_ml.ml_line_count;
3789 }
3790 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3791 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3792 EMSG(_(e_invrange));
3793 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003794 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003795 char_u *qf_title = *eap->cmdlinep;
3796
3797 if (buf->b_sfname)
3798 {
3799 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3800 (char *)qf_title, (char *)buf->b_sfname);
3801 qf_title = IObuff;
3802 }
3803
Bram Moolenaardb552d602006-03-23 22:59:57 +00003804 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3805 (eap->cmdidx != CMD_caddbuffer
3806 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003807 eap->line1, eap->line2,
3808 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003809 && (eap->cmdidx == CMD_cbuffer
3810 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003811 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3812 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003813 }
3814}
3815
Bram Moolenaar1e015462005-09-25 22:16:38 +00003816#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003817/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003818 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3819 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003820 */
3821 void
3822ex_cexpr(eap)
3823 exarg_T *eap;
3824{
3825 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003826 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003827
Bram Moolenaardb552d602006-03-23 22:59:57 +00003828 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3829 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003830 {
3831 qi = ll_get_or_alloc_list(curwin);
3832 if (qi == NULL)
3833 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003834 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003835
Bram Moolenaar4770d092006-01-12 23:22:24 +00003836 /* Evaluate the expression. When the result is a string or a list we can
3837 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003838 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003839 if (tv != NULL)
3840 {
3841 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3842 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3843 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003844 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3845 (eap->cmdidx != CMD_caddexpr
3846 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003847 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003848 && (eap->cmdidx == CMD_cexpr
3849 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003850 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003851 }
3852 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003853 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003854 free_tv(tv);
3855 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003856}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003857#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003858
3859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 * ":helpgrep {pattern}"
3861 */
3862 void
3863ex_helpgrep(eap)
3864 exarg_T *eap;
3865{
3866 regmatch_T regmatch;
3867 char_u *save_cpo;
3868 char_u *p;
3869 int fcount;
3870 char_u **fnames;
3871 FILE *fd;
3872 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003873 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003875#ifdef FEAT_MULTI_LANG
3876 char_u *lang;
3877#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003878 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003879 int new_qi = FALSE;
3880 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01003881#ifdef FEAT_AUTOCMD
3882 char_u *au_name = NULL;
3883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003885#ifdef FEAT_MULTI_LANG
3886 /* Check for a specified language */
3887 lang = check_help_lang(eap->arg);
3888#endif
3889
Bram Moolenaar73633f82012-01-20 13:39:07 +01003890#ifdef FEAT_AUTOCMD
3891 switch (eap->cmdidx)
3892 {
3893 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
3894 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
3895 default: break;
3896 }
3897 if (au_name != NULL)
3898 {
3899 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3900 curbuf->b_fname, TRUE, curbuf);
3901 if (did_throw || force_abort)
3902 return;
3903 }
3904#endif
3905
3906 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3907 save_cpo = p_cpo;
3908 p_cpo = empty_option;
3909
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003910 if (eap->cmdidx == CMD_lhelpgrep)
3911 {
3912 /* Find an existing help window */
3913 FOR_ALL_WINDOWS(wp)
3914 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3915 break;
3916
3917 if (wp == NULL) /* Help window not found */
3918 qi = NULL;
3919 else
3920 qi = wp->w_llist;
3921
3922 if (qi == NULL)
3923 {
3924 /* Allocate a new location list for help text matches */
3925 if ((qi = ll_new_list()) == NULL)
3926 return;
3927 new_qi = TRUE;
3928 }
3929 }
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3932 regmatch.rm_ic = FALSE;
3933 if (regmatch.regprog != NULL)
3934 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003935#ifdef FEAT_MBYTE
3936 vimconv_T vc;
3937
3938 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
3939 * differs. */
3940 vc.vc_type = CONV_NONE;
3941 if (!enc_utf8)
3942 convert_setup(&vc, (char_u *)"utf-8", p_enc);
3943#endif
3944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 /* create a new quickfix list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003946 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
3948 /* Go through all directories in 'runtimepath' */
3949 p = p_rtp;
3950 while (*p != NUL && !got_int)
3951 {
3952 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3953
3954 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3955 add_pathsep(NameBuff);
3956 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3957 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3958 &fnames, EW_FILE|EW_SILENT) == OK
3959 && fcount > 0)
3960 {
3961 for (fi = 0; fi < fcount && !got_int; ++fi)
3962 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003963#ifdef FEAT_MULTI_LANG
3964 /* Skip files for a different language. */
3965 if (lang != NULL
3966 && STRNICMP(lang, fnames[fi]
3967 + STRLEN(fnames[fi]) - 3, 2) != 0
3968 && !(STRNICMP(lang, "en", 2) == 0
3969 && STRNICMP("txt", fnames[fi]
3970 + STRLEN(fnames[fi]) - 3, 3) == 0))
3971 continue;
3972#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003973 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (fd != NULL)
3975 {
3976 lnum = 1;
3977 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3978 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003979 char_u *line = IObuff;
3980#ifdef FEAT_MBYTE
3981 /* Convert a line if 'encoding' is not utf-8 and
3982 * the line contains a non-ASCII character. */
3983 if (vc.vc_type != CONV_NONE
3984 && has_non_ascii(IObuff)) {
3985 line = string_convert(&vc, IObuff, NULL);
3986 if (line == NULL)
3987 line = IObuff;
3988 }
3989#endif
3990
3991 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003993 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003996 while (l > 0 && line[l - 1] <= ' ')
3997 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003999 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 NULL, /* dir */
4001 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004002 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004003 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004005 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004006 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004007 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004008 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 0, /* nr */
4010 1, /* type */
4011 TRUE /* valid */
4012 ) == FAIL)
4013 {
4014 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004015#ifdef FEAT_MBYTE
4016 if (line != IObuff)
4017 vim_free(line);
4018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 break;
4020 }
4021 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004022#ifdef FEAT_MBYTE
4023 if (line != IObuff)
4024 vim_free(line);
4025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 ++lnum;
4027 line_breakcheck();
4028 }
4029 fclose(fd);
4030 }
4031 }
4032 FreeWild(fcount, fnames);
4033 }
4034 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004035
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 vim_free(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004037#ifdef FEAT_MBYTE
4038 if (vc.vc_type != CONV_NONE)
4039 convert_setup(&vc, NULL, NULL);
4040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004042 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4043 qi->qf_lists[qi->qf_curlist].qf_ptr =
4044 qi->qf_lists[qi->qf_curlist].qf_start;
4045 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004048 if (p_cpo == empty_option)
4049 p_cpo = save_cpo;
4050 else
4051 /* Darn, some plugin changed the value. */
4052 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004055 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#endif
4057
Bram Moolenaar73633f82012-01-20 13:39:07 +01004058#ifdef FEAT_AUTOCMD
4059 if (au_name != NULL)
4060 {
4061 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4062 curbuf->b_fname, TRUE, curbuf);
4063 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4064 /* autocommands made "qi" invalid */
4065 return;
4066 }
4067#endif
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004070 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004071 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004072 else
4073 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004074
4075 if (eap->cmdidx == CMD_lhelpgrep)
4076 {
4077 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004078 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004079 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4080 {
4081 if (new_qi)
4082 ll_free_all(&qi);
4083 }
4084 else if (curwin->w_llist == NULL)
4085 curwin->w_llist = qi;
4086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087}
4088
4089#endif /* FEAT_QUICKFIX */