blob: c6e409c169398e1f2688f8fda04a1133e653f0e2 [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 Moolenaar7f51a822012-04-25 18:57:21 +0200133static void restore_start_dir __ARGS((char_u *dirname_start));
134static buf_T *load_dummy_buffer __ARGS((char_u *fname, char_u *dirname_start, char_u *resulting_dir));
135static void wipe_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
136static void unload_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000137static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000139/* Quickfix window check helper macro */
140#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
141/* Location list window check helper macro */
142#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
143/*
144 * Return location list for window 'wp'
145 * For location list window, return the referenced location list
146 */
147#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000150 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200151 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 * Return -1 for error, number of errors for success.
153 */
154 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200155qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000156 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 char_u *efile;
158 char_u *errorformat;
159 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200160 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162 qf_info_T *qi = &ql_info;
163
Bram Moolenaar86b68352004-12-27 21:59:20 +0000164 if (efile == NULL)
165 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166
167 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000168 {
169 qi = ll_get_or_alloc_list(wp);
170 if (qi == NULL)
171 return FAIL;
172 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000173
174 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200175 (linenr_T)0, (linenr_T)0,
176 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000177}
178
179/*
180 * Read the errorfile "efile" into memory, line by line, building the error
181 * list.
182 * Alternative: when "efile" is null read errors from buffer "buf".
183 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200184 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
185 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000186 * Return -1 for error, number of errors for success.
187 */
188 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200189qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
190 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000191 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000192 char_u *efile;
193 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000194 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000195 char_u *errorformat;
196 int newlist; /* TRUE: start a new error list */
197 linenr_T lnumfirst; /* first line number to use */
198 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200199 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000200{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 char_u *namebuf;
202 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000203 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 char_u *fmtstr = NULL;
205 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000206 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 int type = 0;
208 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000209 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 long lnum = 0L;
211 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000212 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000213 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000215 efm_T *fmt_first = NULL;
216 efm_T *fmt_last = NULL;
217 efm_T *fmt_ptr;
218 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 char_u *efm;
220 char_u *ptr;
221 char_u *srcptr;
222 int len;
223 int i;
224 int round;
225 int idx = 0;
226 int multiline = FALSE;
227 int multiignore = FALSE;
228 int multiscan = FALSE;
229 int retval = -1; /* default: return error flag */
230 char_u *directory = NULL;
231 char_u *currfile = NULL;
232 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000233 char_u *p_str = NULL;
234 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 struct dir_stack_T *file_stack = NULL;
236 regmatch_T regmatch;
237 static struct fmtpattern
238 {
239 char_u convchar;
240 char *pattern;
241 } fmt_pat[FMT_PATTERNS] =
242 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000243 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {'n', "\\d\\+"},
245 {'l', "\\d\\+"},
246 {'c', "\\d\\+"},
247 {'t', "."},
248 {'m', ".\\+"},
249 {'r', ".*"},
250 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000251 {'v', "\\d\\+"},
252 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 };
254
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 namebuf = alloc(CMDBUFFSIZE + 1);
256 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000257 pattern = alloc(CMDBUFFSIZE + 1);
258 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 goto qf_init_end;
260
Bram Moolenaar86b68352004-12-27 21:59:20 +0000261 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 EMSG2(_(e_openerrf), efile);
264 goto qf_init_end;
265 }
266
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000267 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200269 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000270 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000272 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200273 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 ;
275
276/*
277 * Each part of the format string is copied and modified from errorformat to
278 * regex prog. Only a few % characters are allowed.
279 */
280 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000281 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000282 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 else
284 efm = errorformat;
285 /*
286 * Get some space to modify the format string into.
287 */
288 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
289 for (round = FMT_PATTERNS; round > 0; )
290 i += (int)STRLEN(fmt_pat[--round].pattern);
291#ifdef COLON_IN_FILENAME
292 i += 12; /* "%f" can become twelve chars longer */
293#else
294 i += 2; /* "%f" can become two chars longer */
295#endif
296 if ((fmtstr = alloc(i)) == NULL)
297 goto error2;
298
Bram Moolenaar01265852006-03-20 21:50:15 +0000299 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 {
301 /*
302 * Allocate a new eformat structure and put it at the end of the list
303 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000304 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 if (fmt_ptr == NULL)
306 goto error2;
307 if (fmt_first == NULL) /* first one */
308 fmt_first = fmt_ptr;
309 else
310 fmt_last->next = fmt_ptr;
311 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
313 /*
314 * Isolate one part in the 'errorformat' option
315 */
316 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
317 if (efm[len] == '\\' && efm[len + 1] != NUL)
318 ++len;
319
320 /*
321 * Build regexp pattern from current 'errorformat' option
322 */
323 ptr = fmtstr;
324 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000325 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 for (efmp = efm; efmp < efm + len; ++efmp)
327 {
328 if (*efmp == '%')
329 {
330 ++efmp;
331 for (idx = 0; idx < FMT_PATTERNS; ++idx)
332 if (fmt_pat[idx].convchar == *efmp)
333 break;
334 if (idx < FMT_PATTERNS)
335 {
336 if (fmt_ptr->addr[idx])
337 {
338 sprintf((char *)errmsg,
339 _("E372: Too many %%%c in format string"), *efmp);
340 EMSG(errmsg);
341 goto error2;
342 }
343 if ((idx
344 && idx < 6
345 && vim_strchr((char_u *)"DXOPQ",
346 fmt_ptr->prefix) != NULL)
347 || (idx == 6
348 && vim_strchr((char_u *)"OPQ",
349 fmt_ptr->prefix) == NULL))
350 {
351 sprintf((char *)errmsg,
352 _("E373: Unexpected %%%c in format string"), *efmp);
353 EMSG(errmsg);
354 goto error2;
355 }
356 fmt_ptr->addr[idx] = (char_u)++round;
357 *ptr++ = '\\';
358 *ptr++ = '(';
359#ifdef BACKSLASH_IN_FILENAME
360 if (*efmp == 'f')
361 {
362 /* Also match "c:" in the file name, even when
363 * checking for a colon next: "%f:".
364 * "\%(\a:\)\=" */
365 STRCPY(ptr, "\\%(\\a:\\)\\=");
366 ptr += 10;
367 }
368#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000369 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000371 if (efmp[1] != '\\' && efmp[1] != '%')
372 {
373 /* A file name may contain spaces, but this isn't
374 * in "\f". For "%f:%l:%m" there may be a ":" in
375 * the file name. Use ".\{-1,}x" instead (x is
376 * the next character), the requirement that :999:
377 * follows should work. */
378 STRCPY(ptr, ".\\{-1,}");
379 ptr += 7;
380 }
381 else
382 {
383 /* File name followed by '\\' or '%': include as
384 * many file name chars as possible. */
385 STRCPY(ptr, "\\f\\+");
386 ptr += 4;
387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
389 else
390 {
391 srcptr = (char_u *)fmt_pat[idx].pattern;
392 while ((*ptr = *srcptr++) != NUL)
393 ++ptr;
394 }
395 *ptr++ = '\\';
396 *ptr++ = ')';
397 }
398 else if (*efmp == '*')
399 {
400 if (*++efmp == '[' || *efmp == '\\')
401 {
402 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
403 {
404 if (efmp[1] == '^')
405 *ptr++ = *++efmp;
406 if (efmp < efm + len)
407 {
408 *ptr++ = *++efmp; /* could be ']' */
409 while (efmp < efm + len
410 && (*ptr++ = *++efmp) != ']')
411 /* skip */;
412 if (efmp == efm + len)
413 {
414 EMSG(_("E374: Missing ] in format string"));
415 goto error2;
416 }
417 }
418 }
419 else if (efmp < efm + len) /* %*\D, %*\s etc. */
420 *ptr++ = *++efmp;
421 *ptr++ = '\\';
422 *ptr++ = '+';
423 }
424 else
425 {
426 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
427 sprintf((char *)errmsg,
428 _("E375: Unsupported %%%c in format string"), *efmp);
429 EMSG(errmsg);
430 goto error2;
431 }
432 }
433 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
434 *ptr++ = *efmp; /* regexp magic characters */
435 else if (*efmp == '#')
436 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000437 else if (*efmp == '>')
438 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 else if (efmp == efm + 1) /* analyse prefix */
440 {
441 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
442 fmt_ptr->flags = *efmp++;
443 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
444 fmt_ptr->prefix = *efmp;
445 else
446 {
447 sprintf((char *)errmsg,
448 _("E376: Invalid %%%c in format string prefix"), *efmp);
449 EMSG(errmsg);
450 goto error2;
451 }
452 }
453 else
454 {
455 sprintf((char *)errmsg,
456 _("E377: Invalid %%%c in format string"), *efmp);
457 EMSG(errmsg);
458 goto error2;
459 }
460 }
461 else /* copy normal character */
462 {
463 if (*efmp == '\\' && efmp + 1 < efm + len)
464 ++efmp;
465 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
466 *ptr++ = '\\'; /* escape regexp atoms */
467 if (*efmp)
468 *ptr++ = *efmp;
469 }
470 }
471 *ptr++ = '$';
472 *ptr = NUL;
473 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
474 goto error2;
475 /*
476 * Advance to next part
477 */
478 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
479 }
480 if (fmt_first == NULL) /* nothing found */
481 {
482 EMSG(_("E378: 'errorformat' contains no pattern"));
483 goto error2;
484 }
485
486 /*
487 * got_int is reset here, because it was probably set when killing the
488 * ":make" command, but we still want to read the errorfile then.
489 */
490 got_int = FALSE;
491
492 /* Always ignore case when looking for a matching error. */
493 regmatch.rm_ic = TRUE;
494
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000495 if (tv != NULL)
496 {
497 if (tv->v_type == VAR_STRING)
498 p_str = tv->vval.v_string;
499 else if (tv->v_type == VAR_LIST)
500 p_li = tv->vval.v_list->lv_first;
501 }
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 /*
504 * Read the lines in the error file one by one.
505 * Try to recognize one of the error formats in each line.
506 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000507 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000509 /* Get the next line. */
510 if (fd == NULL)
511 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000512 if (tv != NULL)
513 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000514 if (tv->v_type == VAR_STRING)
515 {
516 /* Get the next line from the supplied string */
517 char_u *p;
518
519 if (!*p_str) /* Reached the end of the string */
520 break;
521
522 p = vim_strchr(p_str, '\n');
523 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000524 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000525 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000526 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000527
528 if (len > CMDBUFFSIZE - 2)
529 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
530 else
531 vim_strncpy(IObuff, p_str, len);
532
533 p_str += len;
534 }
535 else if (tv->v_type == VAR_LIST)
536 {
537 /* Get the next line from the supplied list */
538 while (p_li && p_li->li_tv.v_type != VAR_STRING)
539 p_li = p_li->li_next; /* Skip non-string items */
540
541 if (!p_li) /* End of the list */
542 break;
543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000544 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000545 if (len > CMDBUFFSIZE - 2)
546 len = CMDBUFFSIZE - 2;
547
548 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
549
550 p_li = p_li->li_next; /* next item */
551 }
552 }
553 else
554 {
555 /* Get the next line from the supplied buffer */
556 if (buflnum > lnumlast)
557 break;
558 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
559 CMDBUFFSIZE - 2);
560 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000561 }
562 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
563 break;
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200566#ifdef FEAT_MBYTE
567 remove_bom(IObuff);
568#endif
569
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
571 *efmp = NUL;
572#ifdef USE_CRNL
573 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
574 *efmp = NUL;
575#endif
576
Bram Moolenaar01265852006-03-20 21:50:15 +0000577 /* If there was no %> item start at the first pattern */
578 if (fmt_start == NULL)
579 fmt_ptr = fmt_first;
580 else
581 {
582 fmt_ptr = fmt_start;
583 fmt_start = NULL;
584 }
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 /*
587 * Try to match each part of 'errorformat' until we find a complete
588 * match or no match.
589 */
590 valid = TRUE;
591restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000592 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {
594 idx = fmt_ptr->prefix;
595 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
596 continue;
597 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000598 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!multiscan)
600 errmsg[0] = NUL;
601 lnum = 0;
602 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000603 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 enr = -1;
605 type = 0;
606 tail = NULL;
607
608 regmatch.regprog = fmt_ptr->prog;
609 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
610 {
611 if ((idx == 'C' || idx == 'Z') && !multiline)
612 continue;
613 if (vim_strchr((char_u *)"EWI", idx) != NULL)
614 type = idx;
615 else
616 type = 0;
617 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000618 * Extract error message data from matched line.
619 * We check for an actual submatch, because "\[" and "\]" in
620 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 */
622 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
623 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000624 int c;
625
626 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
627 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000628
629 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000630 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000631 *regmatch.endp[i] = NUL;
632 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
633 *regmatch.endp[i] = c;
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000636 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 continue;
638 }
639 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000640 {
641 if (regmatch.startp[i] == NULL)
642 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000646 {
647 if (regmatch.startp[i] == NULL)
648 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000652 {
653 if (regmatch.startp[i] == NULL)
654 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000658 {
659 if (regmatch.startp[i] == NULL)
660 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000662 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000663 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 STRCPY(errmsg, IObuff);
665 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
666 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000667 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
668 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000670 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
672 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000673 {
674 if (regmatch.startp[i] == NULL)
675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
679 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000680 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
681 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
683 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000684 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
687 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000688 if (regmatch.startp[i] == NULL)
689 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000691 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000693 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
694 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000695 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
696 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000697 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
698 if (len > CMDBUFFSIZE - 5)
699 len = CMDBUFFSIZE - 5;
700 STRCPY(pattern, "^\\V");
701 STRNCAT(pattern, regmatch.startp[i], len);
702 pattern[len + 3] = '\\';
703 pattern[len + 4] = '$';
704 pattern[len + 5] = NUL;
705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 break;
707 }
708 }
709 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000710
Bram Moolenaar4770d092006-01-12 23:22:24 +0000711 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
715 if (idx == 'D') /* enter directory */
716 {
717 if (*namebuf == NUL)
718 {
719 EMSG(_("E379: Missing or empty directory name"));
720 goto error2;
721 }
722 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
723 goto error2;
724 }
725 else if (idx == 'X') /* leave directory */
726 directory = qf_pop_dir(&dir_stack);
727 }
728 namebuf[0] = NUL; /* no match found, remove file name */
729 lnum = 0; /* don't jump to this line */
730 valid = FALSE;
731 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000732 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 multiline = multiignore = FALSE;
734 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000735 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000737 /* honor %> item */
738 if (fmt_ptr->conthere)
739 fmt_start = fmt_ptr;
740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
742 multiline = TRUE; /* start of a multi-line message */
743 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
744 { /* continuation of multi-line msg */
745 if (qfprev == NULL)
746 goto error2;
747 if (*errmsg && !multiignore)
748 {
749 len = (int)STRLEN(qfprev->qf_text);
750 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
751 == NULL)
752 goto error2;
753 STRCPY(ptr, qfprev->qf_text);
754 vim_free(qfprev->qf_text);
755 qfprev->qf_text = ptr;
756 *(ptr += len) = '\n';
757 STRCPY(++ptr, errmsg);
758 }
759 if (qfprev->qf_nr == -1)
760 qfprev->qf_nr = enr;
761 if (vim_isprintc(type) && !qfprev->qf_type)
762 qfprev->qf_type = type; /* only printable chars allowed */
763 if (!qfprev->qf_lnum)
764 qfprev->qf_lnum = lnum;
765 if (!qfprev->qf_col)
766 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (!qfprev->qf_fnum)
769 qfprev->qf_fnum = qf_get_fnum(directory,
770 *namebuf || directory ? namebuf
771 : currfile && valid ? currfile : 0);
772 if (idx == 'Z')
773 multiline = multiignore = FALSE;
774 line_breakcheck();
775 continue;
776 }
777 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
778 {
779 /* global file names */
780 valid = FALSE;
781 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
782 {
783 if (*namebuf && idx == 'P')
784 currfile = qf_push_dir(namebuf, &file_stack);
785 else if (idx == 'Q')
786 currfile = qf_pop_dir(&file_stack);
787 *namebuf = NUL;
788 if (tail && *tail)
789 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000790 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 multiscan = TRUE;
792 goto restofline;
793 }
794 }
795 }
796 if (fmt_ptr->flags == '-') /* generally exclude this line */
797 {
798 if (multiline)
799 multiignore = TRUE; /* also exclude continuation lines */
800 continue;
801 }
802 }
803
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000804 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000806 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000808 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000809 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 errmsg,
811 lnum,
812 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000814 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 enr,
816 type,
817 valid) == FAIL)
818 goto error2;
819 line_breakcheck();
820 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000821 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000823 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000825 /* no valid entry found */
826 qi->qf_lists[qi->qf_curlist].qf_ptr =
827 qi->qf_lists[qi->qf_curlist].qf_start;
828 qi->qf_lists[qi->qf_curlist].qf_index = 1;
829 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
831 else
832 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000833 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
834 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
835 qi->qf_lists[qi->qf_curlist].qf_ptr =
836 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000838 /* return number of matches */
839 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 goto qf_init_ok;
841 }
842 EMSG(_(e_readerrf));
843error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000844 qf_free(qi, qi->qf_curlist);
845 qi->qf_listcount--;
846 if (qi->qf_curlist > 0)
847 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000849 if (fd != NULL)
850 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
852 {
853 fmt_first = fmt_ptr->next;
854 vim_free(fmt_ptr->prog);
855 vim_free(fmt_ptr);
856 }
857 qf_clean_dir_stack(&dir_stack);
858 qf_clean_dir_stack(&file_stack);
859qf_init_end:
860 vim_free(namebuf);
861 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000862 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 vim_free(fmtstr);
864
865#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000866 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#endif
868
869 return retval;
870}
871
872/*
873 * Prepare for adding a new quickfix list.
874 */
875 static void
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200876qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000877 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200878 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
880 int i;
881
882 /*
883 * If the current entry is not the last entry, delete entries below
884 * the current entry. This makes it possible to browse in a tree-like
885 * way with ":grep'.
886 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000887 while (qi->qf_listcount > qi->qf_curlist + 1)
888 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 /*
891 * When the stack is full, remove to oldest entry
892 * Otherwise, add a new entry.
893 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000894 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000896 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000898 qi->qf_lists[i - 1] = qi->qf_lists[i];
899 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
901 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000902 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100903 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200904 if (qf_title != NULL)
905 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200906 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200907
908 qi->qf_lists[qi->qf_curlist].qf_title = p;
909 if (p != NULL)
910 sprintf((char *)p, ":%s", (char *)qf_title);
911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912}
913
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000914/*
915 * Free a location list
916 */
917 static void
918ll_free_all(pqi)
919 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000920{
921 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000922 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000923
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000924 qi = *pqi;
925 if (qi == NULL)
926 return;
927 *pqi = NULL; /* Remove reference to this list */
928
929 qi->qf_refcount--;
930 if (qi->qf_refcount < 1)
931 {
932 /* No references to this location list */
933 for (i = 0; i < qi->qf_listcount; ++i)
934 qf_free(qi, i);
935 vim_free(qi);
936 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000937}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000938
939 void
940qf_free_all(wp)
941 win_T *wp;
942{
943 int i;
944 qf_info_T *qi = &ql_info;
945
946 if (wp != NULL)
947 {
948 /* location list */
949 ll_free_all(&wp->w_llist);
950 ll_free_all(&wp->w_llist_ref);
951 }
952 else
953 /* quickfix list */
954 for (i = 0; i < qi->qf_listcount; ++i)
955 qf_free(qi, i);
956}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958/*
959 * Add an entry to the end of the list of errors.
960 * Returns OK or FAIL.
961 */
962 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000963qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
964 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000965 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000966 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 char_u *dir; /* optional directory name */
968 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000969 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 char_u *mesg; /* message */
971 long lnum; /* line number */
972 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000973 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000974 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 int nr; /* error number */
976 int type; /* type character */
977 int valid; /* valid entry */
978{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000979 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000981 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000983 if (bufnum != 0)
984 qfp->qf_fnum = bufnum;
985 else
986 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
988 {
989 vim_free(qfp);
990 return FAIL;
991 }
992 qfp->qf_lnum = lnum;
993 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000994 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000995 if (pattern == NULL || *pattern == NUL)
996 qfp->qf_pattern = NULL;
997 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
998 {
999 vim_free(qfp->qf_text);
1000 vim_free(qfp);
1001 return FAIL;
1002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 qfp->qf_nr = nr;
1004 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1005 type = 0;
1006 qfp->qf_type = type;
1007 qfp->qf_valid = valid;
1008
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001009 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1010 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001012 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 qfp->qf_prev = qfp; /* first element points to itself */
1014 }
1015 else
1016 {
1017 qfp->qf_prev = *prevp;
1018 (*prevp)->qf_next = qfp;
1019 }
1020 qfp->qf_next = qfp; /* last element points to itself */
1021 qfp->qf_cleared = FALSE;
1022 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001023 ++qi->qf_lists[qi->qf_curlist].qf_count;
1024 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1025 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001027 qi->qf_lists[qi->qf_curlist].qf_index =
1028 qi->qf_lists[qi->qf_curlist].qf_count;
1029 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031
1032 return OK;
1033}
1034
1035/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001036 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001037 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001038 static qf_info_T *
1039ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001040{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001041 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001042
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001043 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1044 if (qi != NULL)
1045 {
1046 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1047 qi->qf_refcount++;
1048 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001049
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001050 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001051}
1052
1053/*
1054 * Return the location list for window 'wp'.
1055 * If not present, allocate a location list
1056 */
1057 static qf_info_T *
1058ll_get_or_alloc_list(wp)
1059 win_T *wp;
1060{
1061 if (IS_LL_WINDOW(wp))
1062 /* For a location list window, use the referenced location list */
1063 return wp->w_llist_ref;
1064
1065 /*
1066 * For a non-location list window, w_llist_ref should not point to a
1067 * location list.
1068 */
1069 ll_free_all(&wp->w_llist_ref);
1070
1071 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001072 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001073 return wp->w_llist;
1074}
1075
1076/*
1077 * Copy the location list from window "from" to window "to".
1078 */
1079 void
1080copy_loclist(from, to)
1081 win_T *from;
1082 win_T *to;
1083{
1084 qf_info_T *qi;
1085 int idx;
1086 int i;
1087
1088 /*
1089 * When copying from a location list window, copy the referenced
1090 * location list. For other windows, copy the location list for
1091 * that window.
1092 */
1093 if (IS_LL_WINDOW(from))
1094 qi = from->w_llist_ref;
1095 else
1096 qi = from->w_llist;
1097
1098 if (qi == NULL) /* no location list to copy */
1099 return;
1100
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001101 /* allocate a new location list */
1102 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001103 return;
1104
1105 to->w_llist->qf_listcount = qi->qf_listcount;
1106
1107 /* Copy the location lists one at a time */
1108 for (idx = 0; idx < qi->qf_listcount; idx++)
1109 {
1110 qf_list_T *from_qfl;
1111 qf_list_T *to_qfl;
1112
1113 to->w_llist->qf_curlist = idx;
1114
1115 from_qfl = &qi->qf_lists[idx];
1116 to_qfl = &to->w_llist->qf_lists[idx];
1117
1118 /* Some of the fields are populated by qf_add_entry() */
1119 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1120 to_qfl->qf_count = 0;
1121 to_qfl->qf_index = 0;
1122 to_qfl->qf_start = NULL;
1123 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001124 if (from_qfl->qf_title != NULL)
1125 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1126 else
1127 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001128
1129 if (from_qfl->qf_count)
1130 {
1131 qfline_T *from_qfp;
1132 qfline_T *prevp = NULL;
1133
1134 /* copy all the location entries in this list */
1135 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1136 ++i, from_qfp = from_qfp->qf_next)
1137 {
1138 if (qf_add_entry(to->w_llist, &prevp,
1139 NULL,
1140 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001141 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001142 from_qfp->qf_text,
1143 from_qfp->qf_lnum,
1144 from_qfp->qf_col,
1145 from_qfp->qf_viscol,
1146 from_qfp->qf_pattern,
1147 from_qfp->qf_nr,
1148 0,
1149 from_qfp->qf_valid) == FAIL)
1150 {
1151 qf_free_all(to);
1152 return;
1153 }
1154 /*
1155 * qf_add_entry() will not set the qf_num field, as the
1156 * directory and file names are not supplied. So the qf_fnum
1157 * field is copied here.
1158 */
1159 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1160 prevp->qf_type = from_qfp->qf_type; /* error type */
1161 if (from_qfl->qf_ptr == from_qfp)
1162 to_qfl->qf_ptr = prevp; /* current location */
1163 }
1164 }
1165
1166 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1167
1168 /* When no valid entries are present in the list, qf_ptr points to
1169 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001170 if (to_qfl->qf_nonevalid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001171 to_qfl->qf_ptr = to_qfl->qf_start;
1172 }
1173
1174 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1175}
1176
1177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 * get buffer number for file "dir.name"
1179 */
1180 static int
1181qf_get_fnum(directory, fname)
1182 char_u *directory;
1183 char_u *fname;
1184{
1185 if (fname == NULL || *fname == NUL) /* no file name */
1186 return 0;
1187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 char_u *ptr;
1189 int fnum;
1190
Bram Moolenaare60acc12011-05-10 16:41:25 +02001191#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001193#endif
1194#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (directory != NULL)
1196 slash_adjust(directory);
1197 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (directory != NULL && !vim_isAbsName(fname)
1200 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1201 {
1202 /*
1203 * Here we check if the file really exists.
1204 * This should normally be true, but if make works without
1205 * "leaving directory"-messages we might have missed a
1206 * directory change.
1207 */
1208 if (mch_getperm(ptr) < 0)
1209 {
1210 vim_free(ptr);
1211 directory = qf_guess_filepath(fname);
1212 if (directory)
1213 ptr = concat_fnames(directory, fname, TRUE);
1214 else
1215 ptr = vim_strsave(fname);
1216 }
1217 /* Use concatenated directory name and file name */
1218 fnum = buflist_add(ptr, 0);
1219 vim_free(ptr);
1220 return fnum;
1221 }
1222 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224}
1225
1226/*
1227 * push dirbuf onto the directory stack and return pointer to actual dir or
1228 * NULL on error
1229 */
1230 static char_u *
1231qf_push_dir(dirbuf, stackptr)
1232 char_u *dirbuf;
1233 struct dir_stack_T **stackptr;
1234{
1235 struct dir_stack_T *ds_new;
1236 struct dir_stack_T *ds_ptr;
1237
1238 /* allocate new stack element and hook it in */
1239 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1240 if (ds_new == NULL)
1241 return NULL;
1242
1243 ds_new->next = *stackptr;
1244 *stackptr = ds_new;
1245
1246 /* store directory on the stack */
1247 if (vim_isAbsName(dirbuf)
1248 || (*stackptr)->next == NULL
1249 || (*stackptr && dir_stack != *stackptr))
1250 (*stackptr)->dirname = vim_strsave(dirbuf);
1251 else
1252 {
1253 /* Okay we don't have an absolute path.
1254 * dirbuf must be a subdir of one of the directories on the stack.
1255 * Let's search...
1256 */
1257 ds_new = (*stackptr)->next;
1258 (*stackptr)->dirname = NULL;
1259 while (ds_new)
1260 {
1261 vim_free((*stackptr)->dirname);
1262 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1263 TRUE);
1264 if (mch_isdir((*stackptr)->dirname) == TRUE)
1265 break;
1266
1267 ds_new = ds_new->next;
1268 }
1269
1270 /* clean up all dirs we already left */
1271 while ((*stackptr)->next != ds_new)
1272 {
1273 ds_ptr = (*stackptr)->next;
1274 (*stackptr)->next = (*stackptr)->next->next;
1275 vim_free(ds_ptr->dirname);
1276 vim_free(ds_ptr);
1277 }
1278
1279 /* Nothing found -> it must be on top level */
1280 if (ds_new == NULL)
1281 {
1282 vim_free((*stackptr)->dirname);
1283 (*stackptr)->dirname = vim_strsave(dirbuf);
1284 }
1285 }
1286
1287 if ((*stackptr)->dirname != NULL)
1288 return (*stackptr)->dirname;
1289 else
1290 {
1291 ds_ptr = *stackptr;
1292 *stackptr = (*stackptr)->next;
1293 vim_free(ds_ptr);
1294 return NULL;
1295 }
1296}
1297
1298
1299/*
1300 * pop dirbuf from the directory stack and return previous directory or NULL if
1301 * stack is empty
1302 */
1303 static char_u *
1304qf_pop_dir(stackptr)
1305 struct dir_stack_T **stackptr;
1306{
1307 struct dir_stack_T *ds_ptr;
1308
1309 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1310 * What to do if it isn't? */
1311
1312 /* pop top element and free it */
1313 if (*stackptr != NULL)
1314 {
1315 ds_ptr = *stackptr;
1316 *stackptr = (*stackptr)->next;
1317 vim_free(ds_ptr->dirname);
1318 vim_free(ds_ptr);
1319 }
1320
1321 /* return NEW top element as current dir or NULL if stack is empty*/
1322 return *stackptr ? (*stackptr)->dirname : NULL;
1323}
1324
1325/*
1326 * clean up directory stack
1327 */
1328 static void
1329qf_clean_dir_stack(stackptr)
1330 struct dir_stack_T **stackptr;
1331{
1332 struct dir_stack_T *ds_ptr;
1333
1334 while ((ds_ptr = *stackptr) != NULL)
1335 {
1336 *stackptr = (*stackptr)->next;
1337 vim_free(ds_ptr->dirname);
1338 vim_free(ds_ptr);
1339 }
1340}
1341
1342/*
1343 * Check in which directory of the directory stack the given file can be
1344 * found.
1345 * Returns a pointer to the directory name or NULL if not found
1346 * Cleans up intermediate directory entries.
1347 *
1348 * TODO: How to solve the following problem?
1349 * If we have the this directory tree:
1350 * ./
1351 * ./aa
1352 * ./aa/bb
1353 * ./bb
1354 * ./bb/x.c
1355 * and make says:
1356 * making all in aa
1357 * making all in bb
1358 * x.c:9: Error
1359 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1360 * qf_guess_filepath will return NULL.
1361 */
1362 static char_u *
1363qf_guess_filepath(filename)
1364 char_u *filename;
1365{
1366 struct dir_stack_T *ds_ptr;
1367 struct dir_stack_T *ds_tmp;
1368 char_u *fullname;
1369
1370 /* no dirs on the stack - there's nothing we can do */
1371 if (dir_stack == NULL)
1372 return NULL;
1373
1374 ds_ptr = dir_stack->next;
1375 fullname = NULL;
1376 while (ds_ptr)
1377 {
1378 vim_free(fullname);
1379 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1380
1381 /* If concat_fnames failed, just go on. The worst thing that can happen
1382 * is that we delete the entire stack.
1383 */
1384 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1385 break;
1386
1387 ds_ptr = ds_ptr->next;
1388 }
1389
1390 vim_free(fullname);
1391
1392 /* clean up all dirs we already left */
1393 while (dir_stack->next != ds_ptr)
1394 {
1395 ds_tmp = dir_stack->next;
1396 dir_stack->next = dir_stack->next->next;
1397 vim_free(ds_tmp->dirname);
1398 vim_free(ds_tmp);
1399 }
1400
1401 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1402
1403}
1404
1405/*
1406 * jump to a quickfix line
1407 * if dir == FORWARD go "errornr" valid entries forward
1408 * if dir == BACKWARD go "errornr" valid entries backward
1409 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1410 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1411 * else if "errornr" is zero, redisplay the same line
1412 * else go to entry "errornr"
1413 */
1414 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001415qf_jump(qi, dir, errornr, forceit)
1416 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 int dir;
1418 int errornr;
1419 int forceit;
1420{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001421 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001422 qfline_T *qf_ptr;
1423 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 int qf_index;
1425 int old_qf_fnum;
1426 int old_qf_index;
1427 int prev_index;
1428 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1429 char_u *err = e_no_more_items;
1430 linenr_T i;
1431 buf_T *old_curbuf;
1432 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 colnr_T screen_col;
1434 colnr_T char_col;
1435 char_u *line;
1436#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001437 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001438 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 int opened_window = FALSE;
1440 win_T *win;
1441 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001442 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001444 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 int print_message = TRUE;
1446 int len;
1447#ifdef FEAT_FOLDING
1448 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1449#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001450 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001451 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001453 if (qi == NULL)
1454 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001455
1456 if (qi->qf_curlist >= qi->qf_listcount
1457 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
1459 EMSG(_(e_quickfix));
1460 return;
1461 }
1462
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001463 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001465 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 old_qf_index = qf_index;
1467 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1468 {
1469 while (errornr--)
1470 {
1471 old_qf_ptr = qf_ptr;
1472 prev_index = qf_index;
1473 old_qf_fnum = qf_ptr->qf_fnum;
1474 do
1475 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001476 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 || qf_ptr->qf_next == NULL)
1478 {
1479 qf_ptr = old_qf_ptr;
1480 qf_index = prev_index;
1481 if (err != NULL)
1482 {
1483 EMSG(_(err));
1484 goto theend;
1485 }
1486 errornr = 0;
1487 break;
1488 }
1489 ++qf_index;
1490 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001491 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1492 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1494 err = NULL;
1495 }
1496 }
1497 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1498 {
1499 while (errornr--)
1500 {
1501 old_qf_ptr = qf_ptr;
1502 prev_index = qf_index;
1503 old_qf_fnum = qf_ptr->qf_fnum;
1504 do
1505 {
1506 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1507 {
1508 qf_ptr = old_qf_ptr;
1509 qf_index = prev_index;
1510 if (err != NULL)
1511 {
1512 EMSG(_(err));
1513 goto theend;
1514 }
1515 errornr = 0;
1516 break;
1517 }
1518 --qf_index;
1519 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001520 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1521 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1523 err = NULL;
1524 }
1525 }
1526 else if (errornr != 0) /* go to specified number */
1527 {
1528 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1529 {
1530 --qf_index;
1531 qf_ptr = qf_ptr->qf_prev;
1532 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001533 while (errornr > qf_index && qf_index <
1534 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 && qf_ptr->qf_next != NULL)
1536 {
1537 ++qf_index;
1538 qf_ptr = qf_ptr->qf_next;
1539 }
1540 }
1541
1542#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1544 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 /* No need to print the error message if it's visible in the error
1546 * window */
1547 print_message = FALSE;
1548
1549 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001550 * For ":helpgrep" find a help window or open one.
1551 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001552 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001553 {
1554 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001555
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001556 if (cmdmod.tab != 0)
1557 wp = NULL;
1558 else
1559 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1560 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1561 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001562 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1563 win_enter(wp, TRUE);
1564 else
1565 {
1566 /*
1567 * Split off help window; put it at far top if no position
1568 * specified, the current window is vertically split and narrow.
1569 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001570 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001571# ifdef FEAT_VERTSPLIT
1572 if (cmdmod.split == 0 && curwin->w_width != Columns
1573 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001574 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001575# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001576 if (qi != &ql_info)
1577 flags |= WSP_NEWLOC; /* don't copy the location list */
1578
1579 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001580 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001581 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001582
1583 if (curwin->w_height < p_hh)
1584 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001585
1586 if (qi != &ql_info) /* not a quickfix list */
1587 {
1588 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001589 curwin->w_llist = qi;
1590 qi->qf_refcount++;
1591 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001592 }
1593
1594 if (!p_im)
1595 restart_edit = 0; /* don't want insert mode in help file */
1596 }
1597
1598 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 * If currently in the quickfix window, find another window to show the
1600 * file in.
1601 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001602 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 /*
1605 * If there is no file specified, we don't know where to go.
1606 * But do advance, otherwise ":cn" gets stuck.
1607 */
1608 if (qf_ptr->qf_fnum == 0)
1609 goto theend;
1610
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001611 /* Locate a window showing a normal buffer */
1612 usable_win = 0;
1613 FOR_ALL_WINDOWS(win)
1614 if (win->w_buffer->b_p_bt[0] == NUL)
1615 {
1616 usable_win = 1;
1617 break;
1618 }
1619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001621 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001622 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001624 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001625 {
1626 tabpage_T *tp;
1627 win_T *wp;
1628
1629 FOR_ALL_TAB_WINDOWS(tp, wp)
1630 {
1631 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1632 {
1633 goto_tabpage_win(tp, wp);
1634 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001635 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001636 }
1637 }
1638 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001639win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001640
1641 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001642 * If there is only one window and it is the quickfix window, create a
1643 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001644 */
1645 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001647 ll_ref = curwin->w_llist_ref;
1648
Bram Moolenaar884ae642009-02-22 01:37:59 +00001649 flags = WSP_ABOVE;
1650 if (ll_ref != NULL)
1651 flags |= WSP_NEWLOC;
1652 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 goto failed; /* not enough room for window */
1654 opened_window = TRUE; /* close it when fail */
1655 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001656 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001657 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001658 if (ll_ref != NULL)
1659 {
1660 /* The new window should use the location list from the
1661 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001662 curwin->w_llist = ll_ref;
1663 ll_ref->qf_refcount++;
1664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 else
1667 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001668 if (curwin->w_llist_ref != NULL)
1669 {
1670 /* In a location window */
1671 ll_ref = curwin->w_llist_ref;
1672
1673 /* Find the window with the same location list */
1674 FOR_ALL_WINDOWS(win)
1675 if (win->w_llist == ll_ref)
1676 break;
1677 if (win == NULL)
1678 {
1679 /* Find the window showing the selected file */
1680 FOR_ALL_WINDOWS(win)
1681 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1682 break;
1683 if (win == NULL)
1684 {
1685 /* Find a previous usable window */
1686 win = curwin;
1687 do
1688 {
1689 if (win->w_buffer->b_p_bt[0] == NUL)
1690 break;
1691 if (win->w_prev == NULL)
1692 win = lastwin; /* wrap around the top */
1693 else
1694 win = win->w_prev; /* go to previous window */
1695 } while (win != curwin);
1696 }
1697 }
1698 win_goto(win);
1699
1700 /* If the location list for the window is not set, then set it
1701 * to the location list from the location window */
1702 if (win->w_llist == NULL)
1703 {
1704 win->w_llist = ll_ref;
1705 ll_ref->qf_refcount++;
1706 }
1707 }
1708 else
1709 {
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 /*
1712 * Try to find a window that shows the right buffer.
1713 * Default to the window just above the quickfix buffer.
1714 */
1715 win = curwin;
1716 altwin = NULL;
1717 for (;;)
1718 {
1719 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1720 break;
1721 if (win->w_prev == NULL)
1722 win = lastwin; /* wrap around the top */
1723 else
1724 win = win->w_prev; /* go to previous window */
1725
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001726 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
1728 /* Didn't find it, go to the window before the quickfix
1729 * window. */
1730 if (altwin != NULL)
1731 win = altwin;
1732 else if (curwin->w_prev != NULL)
1733 win = curwin->w_prev;
1734 else
1735 win = curwin->w_next;
1736 break;
1737 }
1738
1739 /* Remember a usable window. */
1740 if (altwin == NULL && !win->w_p_pvw
1741 && win->w_buffer->b_p_bt[0] == NUL)
1742 altwin = win;
1743 }
1744
1745 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748 }
1749#endif
1750
1751 /*
1752 * If there is a file name,
1753 * read the wanted file if needed, and check autowrite etc.
1754 */
1755 old_curbuf = curbuf;
1756 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001757
1758 if (qf_ptr->qf_fnum != 0)
1759 {
1760 if (qf_ptr->qf_type == 1)
1761 {
1762 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1763 * set b_p_ro flag). */
1764 if (!can_abandon(curbuf, forceit))
1765 {
1766 EMSG(_(e_nowrtmsg));
1767 ok = FALSE;
1768 }
1769 else
1770 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001771 ECMD_HIDE + ECMD_SET_HELP,
1772 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001773 }
1774 else
1775 ok = buflist_getfile(qf_ptr->qf_fnum,
1776 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1777 }
1778
1779 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 {
1781 /* When not switched to another buffer, still need to set pc mark */
1782 if (curbuf == old_curbuf)
1783 setpcmark();
1784
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001785 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001787 /*
1788 * Go to line with error, unless qf_lnum is 0.
1789 */
1790 i = qf_ptr->qf_lnum;
1791 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001793 if (i > curbuf->b_ml.ml_line_count)
1794 i = curbuf->b_ml.ml_line_count;
1795 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001797 if (qf_ptr->qf_col > 0)
1798 {
1799 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1800 if (qf_ptr->qf_viscol == TRUE)
1801 {
1802 /*
1803 * Check each character from the beginning of the error
1804 * line up to the error column. For each tab character
1805 * found, reduce the error column value by the length of
1806 * a tab character.
1807 */
1808 line = ml_get_curline();
1809 screen_col = 0;
1810 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1811 {
1812 if (*line == NUL)
1813 break;
1814 if (*line++ == '\t')
1815 {
1816 curwin->w_cursor.col -= 7 - (screen_col % 8);
1817 screen_col += 8 - (screen_col % 8);
1818 }
1819 else
1820 ++screen_col;
1821 }
1822 }
1823 check_cursor();
1824 }
1825 else
1826 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
1828 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001829 {
1830 pos_T save_cursor;
1831
1832 /* Move the cursor to the first line in the buffer */
1833 save_cursor = curwin->w_cursor;
1834 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001835 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1836 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001837 curwin->w_cursor = save_cursor;
1838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
1840#ifdef FEAT_FOLDING
1841 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1842 foldOpenCursor();
1843#endif
1844 if (print_message)
1845 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001846 /* Update the screen before showing the message, unless the screen
1847 * scrolled up. */
1848 if (!msg_scrolled)
1849 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001851 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1853 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1854 /* Add the message, skipping leading whitespace and newlines. */
1855 len = (int)STRLEN(IObuff);
1856 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1857
1858 /* Output the message. Overwrite to avoid scrolling when the 'O'
1859 * flag is present in 'shortmess'; But when not jumping, print the
1860 * whole message. */
1861 i = msg_scroll;
1862 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1863 msg_scroll = TRUE;
1864 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1865 msg_scroll = FALSE;
1866 msg_attr_keep(IObuff, 0, TRUE);
1867 msg_scroll = i;
1868 }
1869 }
1870 else
1871 {
1872#ifdef FEAT_WINDOWS
1873 if (opened_window)
1874 win_close(curwin, TRUE); /* Close opened window */
1875#endif
1876 if (qf_ptr->qf_fnum != 0)
1877 {
1878 /*
1879 * Couldn't open file, so put index back where it was. This could
1880 * happen if the file was readonly and we changed something.
1881 */
1882#ifdef FEAT_WINDOWS
1883failed:
1884#endif
1885 qf_ptr = old_qf_ptr;
1886 qf_index = old_qf_index;
1887 }
1888 }
1889theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001890 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1891 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892#ifdef FEAT_WINDOWS
1893 if (p_swb != old_swb && opened_window)
1894 {
1895 /* Restore old 'switchbuf' value, but not when an autocommand or
1896 * modeline has changed the value. */
1897 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001900 swb_flags = old_swb_flags;
1901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 else
1903 free_string_option(old_swb);
1904 }
1905#endif
1906}
1907
1908/*
1909 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001910 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 */
1912 void
1913qf_list(eap)
1914 exarg_T *eap;
1915{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001916 buf_T *buf;
1917 char_u *fname;
1918 qfline_T *qfp;
1919 int i;
1920 int idx1 = 1;
1921 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001922 char_u *arg = eap->arg;
1923 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001925 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001927 if (eap->cmdidx == CMD_llist)
1928 {
1929 qi = GET_LOC_LIST(curwin);
1930 if (qi == NULL)
1931 {
1932 EMSG(_(e_loclist));
1933 return;
1934 }
1935 }
1936
1937 if (qi->qf_curlist >= qi->qf_listcount
1938 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
1940 EMSG(_(e_quickfix));
1941 return;
1942 }
1943 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1944 {
1945 EMSG(_(e_trailing));
1946 return;
1947 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001948 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (idx1 < 0)
1950 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1951 if (idx2 < 0)
1952 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1953
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001954 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001956 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1957 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1960 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001961 msg_putchar('\n');
1962 if (got_int)
1963 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001964
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001965 fname = NULL;
1966 if (qfp->qf_fnum != 0
1967 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1968 {
1969 fname = buf->b_fname;
1970 if (qfp->qf_type == 1) /* :helpgrep */
1971 fname = gettail(fname);
1972 }
1973 if (fname == NULL)
1974 sprintf((char *)IObuff, "%2d", i);
1975 else
1976 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1977 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001978 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001979 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1980 if (qfp->qf_lnum == 0)
1981 IObuff[0] = NUL;
1982 else if (qfp->qf_col == 0)
1983 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1984 else
1985 sprintf((char *)IObuff, ":%ld col %d",
1986 qfp->qf_lnum, qfp->qf_col);
1987 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1988 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1989 msg_puts_attr(IObuff, hl_attr(HLF_N));
1990 if (qfp->qf_pattern != NULL)
1991 {
1992 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1993 STRCAT(IObuff, ":");
1994 msg_puts(IObuff);
1995 }
1996 msg_puts((char_u *)" ");
1997
1998 /* Remove newlines and leading whitespace from the text. For an
1999 * unrecognized line keep the indent, the compiler may mark a word
2000 * with ^^^^. */
2001 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2003 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002004 msg_prt_line(IObuff, FALSE);
2005 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002007
2008 qfp = qfp->qf_next;
2009 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 ui_breakcheck();
2011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012}
2013
2014/*
2015 * Remove newlines and leading whitespace from an error message.
2016 * Put the result in "buf[bufsize]".
2017 */
2018 static void
2019qf_fmt_text(text, buf, bufsize)
2020 char_u *text;
2021 char_u *buf;
2022 int bufsize;
2023{
2024 int i;
2025 char_u *p = text;
2026
2027 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2028 {
2029 if (*p == '\n')
2030 {
2031 buf[i] = ' ';
2032 while (*++p != NUL)
2033 if (!vim_iswhite(*p) && *p != '\n')
2034 break;
2035 }
2036 else
2037 buf[i] = *p++;
2038 }
2039 buf[i] = NUL;
2040}
2041
2042/*
2043 * ":colder [count]": Up in the quickfix stack.
2044 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002045 * ":lolder [count]": Up in the location list stack.
2046 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 */
2048 void
2049qf_age(eap)
2050 exarg_T *eap;
2051{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 int count;
2054
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002055 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2056 {
2057 qi = GET_LOC_LIST(curwin);
2058 if (qi == NULL)
2059 {
2060 EMSG(_(e_loclist));
2061 return;
2062 }
2063 }
2064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 if (eap->addr_count != 0)
2066 count = eap->line2;
2067 else
2068 count = 1;
2069 while (count--)
2070 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002073 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
2075 EMSG(_("E380: At bottom of quickfix stack"));
2076 return;
2077 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002078 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 else
2081 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002082 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 EMSG(_("E381: At top of quickfix stack"));
2085 return;
2086 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002087 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 qf_msg(qi);
2091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092}
2093
2094 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002095qf_msg(qi)
2096 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002099 qi->qf_curlist + 1, qi->qf_listcount,
2100 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002102 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#endif
2104}
2105
2106/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002107 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 */
2109 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002110qf_free(qi, idx)
2111 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 int idx;
2113{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002114 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 qfp = qi->qf_lists[idx].qf_start->qf_next;
2119 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2120 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2121 vim_free(qi->qf_lists[idx].qf_start);
2122 qi->qf_lists[idx].qf_start = qfp;
2123 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002125 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002126 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127}
2128
2129/*
2130 * qf_mark_adjust: adjust marks
2131 */
2132 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002133qf_mark_adjust(wp, line1, line2, amount, amount_after)
2134 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 linenr_T line1;
2136 linenr_T line2;
2137 long amount;
2138 long amount_after;
2139{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002140 int i;
2141 qfline_T *qfp;
2142 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002143 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002145 if (wp != NULL)
2146 {
2147 if (wp->w_llist == NULL)
2148 return;
2149 qi = wp->w_llist;
2150 }
2151
2152 for (idx = 0; idx < qi->qf_listcount; ++idx)
2153 if (qi->qf_lists[idx].qf_count)
2154 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2155 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (qfp->qf_fnum == curbuf->b_fnum)
2157 {
2158 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2159 {
2160 if (amount == MAXLNUM)
2161 qfp->qf_cleared = TRUE;
2162 else
2163 qfp->qf_lnum += amount;
2164 }
2165 else if (amount_after && qfp->qf_lnum > line2)
2166 qfp->qf_lnum += amount_after;
2167 }
2168}
2169
2170/*
2171 * Make a nice message out of the error character and the error number:
2172 * char number message
2173 * e or E 0 " error"
2174 * w or W 0 " warning"
2175 * i or I 0 " info"
2176 * 0 0 ""
2177 * other 0 " c"
2178 * e or E n " error n"
2179 * w or W n " warning n"
2180 * i or I n " info n"
2181 * 0 n " error n"
2182 * other n " c n"
2183 * 1 x "" :helpgrep
2184 */
2185 static char_u *
2186qf_types(c, nr)
2187 int c, nr;
2188{
2189 static char_u buf[20];
2190 static char_u cc[3];
2191 char_u *p;
2192
2193 if (c == 'W' || c == 'w')
2194 p = (char_u *)" warning";
2195 else if (c == 'I' || c == 'i')
2196 p = (char_u *)" info";
2197 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2198 p = (char_u *)" error";
2199 else if (c == 0 || c == 1)
2200 p = (char_u *)"";
2201 else
2202 {
2203 cc[0] = ' ';
2204 cc[1] = c;
2205 cc[2] = NUL;
2206 p = cc;
2207 }
2208
2209 if (nr <= 0)
2210 return p;
2211
2212 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2213 return buf;
2214}
2215
2216#if defined(FEAT_WINDOWS) || defined(PROTO)
2217/*
2218 * ":cwindow": open the quickfix window if we have errors to display,
2219 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220 * ":lwindow": open the location list window if we have locations to display,
2221 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 */
2223 void
2224ex_cwindow(eap)
2225 exarg_T *eap;
2226{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 win_T *win;
2229
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230 if (eap->cmdidx == CMD_lwindow)
2231 {
2232 qi = GET_LOC_LIST(curwin);
2233 if (qi == NULL)
2234 return;
2235 }
2236
2237 /* Look for an existing quickfix window. */
2238 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 /*
2241 * If a quickfix window is open but we have no errors to display,
2242 * close the window. If a quickfix window is not open, then open
2243 * it if we have errors; otherwise, leave it closed.
2244 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002245 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002246 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002247 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 if (win != NULL)
2250 ex_cclose(eap);
2251 }
2252 else if (win == NULL)
2253 ex_copen(eap);
2254}
2255
2256/*
2257 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002258 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 void
2261ex_cclose(eap)
2262 exarg_T *eap;
2263{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002264 win_T *win = NULL;
2265 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002267 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2268 {
2269 qi = GET_LOC_LIST(curwin);
2270 if (qi == NULL)
2271 return;
2272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002274 /* Find existing quickfix window and close it. */
2275 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (win != NULL)
2277 win_close(win, FALSE);
2278}
2279
2280/*
2281 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002282 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
2284 void
2285ex_copen(eap)
2286 exarg_T *eap;
2287{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002288 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002291 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002292 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002293 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002295 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2296 {
2297 qi = GET_LOC_LIST(curwin);
2298 if (qi == NULL)
2299 {
2300 EMSG(_(e_loclist));
2301 return;
2302 }
2303 }
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (eap->addr_count != 0)
2306 height = eap->line2;
2307 else
2308 height = QF_WINHEIGHT;
2309
2310#ifdef FEAT_VISUAL
2311 reset_VIsual_and_resel(); /* stop Visual mode */
2312#endif
2313#ifdef FEAT_GUI
2314 need_mouse_correct = TRUE;
2315#endif
2316
2317 /*
2318 * Find existing quickfix window, or open a new one.
2319 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002320 win = qf_find_win(qi);
2321
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002322 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 win_goto(win);
2324 else
2325 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002326 qf_buf = qf_find_buf(qi);
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 /* The current window becomes the previous window afterwards. */
2329 win = curwin;
2330
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002331 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2332 /* Create the new window at the very bottom. */
2333 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002334 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002336 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002338 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002340 /*
2341 * For the location list window, create a reference to the
2342 * location list from the window 'win'.
2343 */
2344 curwin->w_llist_ref = win->w_llist;
2345 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002348 if (oldwin != curwin)
2349 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002350 if (qf_buf != NULL)
2351 /* Use the existing quickfix buffer */
2352 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002353 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002354 else
2355 {
2356 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002357 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002358 /* switch off 'swapfile' */
2359 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2360 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002361 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002362 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002363 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002364#ifdef FEAT_DIFF
2365 curwin->w_p_diff = FALSE;
2366#endif
2367#ifdef FEAT_FOLDING
2368 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2369 OPT_LOCAL);
2370#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002373 /* Only set the height when still in the same tab page and there is no
2374 * window to the side. */
2375 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002376#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002377 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002378#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002379 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 win_setheight(height);
2381 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2382 if (win_valid(win))
2383 prevwin = win;
2384 }
2385
2386 /*
2387 * Fill the buffer with the quickfix list.
2388 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002389 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002391 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002392 qf_set_title(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002393
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002394 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 curwin->w_cursor.col = 0;
2396 check_cursor();
2397 update_topline(); /* scroll to show the line */
2398}
2399
2400/*
2401 * Return the number of the current entry (line number in the quickfix
2402 * window).
2403 */
2404 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405qf_current_entry(wp)
2406 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002408 qf_info_T *qi = &ql_info;
2409
2410 if (IS_LL_WINDOW(wp))
2411 /* In the location list window, use the referenced location list */
2412 qi = wp->w_llist_ref;
2413
2414 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415}
2416
2417/*
2418 * Update the cursor position in the quickfix window to the current error.
2419 * Return TRUE if there is a quickfix window.
2420 */
2421 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002422qf_win_pos_update(qi, old_qf_index)
2423 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 int old_qf_index; /* previous qf_index or zero */
2425{
2426 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002427 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 /*
2430 * Put the cursor on the current error in the quickfix window, so that
2431 * it's viewable.
2432 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (win != NULL
2435 && qf_index <= win->w_buffer->b_ml.ml_line_count
2436 && old_qf_index != qf_index)
2437 {
2438 win_T *old_curwin = curwin;
2439
2440 curwin = win;
2441 curbuf = win->w_buffer;
2442 if (qf_index > old_qf_index)
2443 {
2444 curwin->w_redraw_top = old_qf_index;
2445 curwin->w_redraw_bot = qf_index;
2446 }
2447 else
2448 {
2449 curwin->w_redraw_top = qf_index;
2450 curwin->w_redraw_bot = old_qf_index;
2451 }
2452 curwin->w_cursor.lnum = qf_index;
2453 curwin->w_cursor.col = 0;
2454 update_topline(); /* scroll to show the line */
2455 redraw_later(VALID);
2456 curwin->w_redr_status = TRUE; /* update ruler */
2457 curwin = old_curwin;
2458 curbuf = curwin->w_buffer;
2459 }
2460 return win != NULL;
2461}
2462
2463/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002464 * Check whether the given window is displaying the specified quickfix/location
2465 * list buffer
2466 */
2467 static int
2468is_qf_win(win, qi)
2469 win_T *win;
2470 qf_info_T *qi;
2471{
2472 /*
2473 * A window displaying the quickfix buffer will have the w_llist_ref field
2474 * set to NULL.
2475 * A window displaying a location list buffer will have the w_llist_ref
2476 * pointing to the location list.
2477 */
2478 if (bt_quickfix(win->w_buffer))
2479 if ((qi == &ql_info && win->w_llist_ref == NULL)
2480 || (qi != &ql_info && win->w_llist_ref == qi))
2481 return TRUE;
2482
2483 return FALSE;
2484}
2485
2486/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002487 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002488 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002489 */
2490 static win_T *
2491qf_find_win(qi)
2492 qf_info_T *qi;
2493{
2494 win_T *win;
2495
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002496 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002497 if (is_qf_win(win, qi))
2498 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002499
2500 return win;
2501}
2502
2503/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002504 * Find a quickfix buffer.
2505 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
2507 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002508qf_find_buf(qi)
2509 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002511 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002512 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
Bram Moolenaar9c102382006-05-03 21:26:49 +00002514 FOR_ALL_TAB_WINDOWS(tp, win)
2515 if (is_qf_win(win, qi))
2516 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002517
Bram Moolenaar9c102382006-05-03 21:26:49 +00002518 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519}
2520
2521/*
2522 * Find the quickfix buffer. If it exists, update the contents.
2523 */
2524 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002525qf_update_buffer(qi)
2526 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002529 win_T *win;
2530 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002534 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (buf != NULL)
2536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 /* set curwin/curbuf to buf and save a few things */
2538 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002540 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002542 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2543 && (win = qf_find_win(qi)) != NULL)
2544 {
2545 curwin_save = curwin;
2546 curwin = win;
2547 qf_set_title(qi);
2548 curwin = curwin_save;
2549
2550 }
2551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 /* restore curwin/curbuf and a few other things */
2553 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002555 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
2557}
2558
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002559 static void
2560qf_set_title(qi)
2561 qf_info_T *qi;
2562{
2563 set_internal_string_var((char_u *)"w:quickfix_title",
2564 qi->qf_lists[qi->qf_curlist].qf_title);
2565}
2566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567/*
2568 * Fill current buffer with quickfix errors, replacing any previous contents.
2569 * curbuf must be the quickfix buffer!
2570 */
2571 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002572qf_fill_buffer(qi)
2573 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002575 linenr_T lnum;
2576 qfline_T *qfp;
2577 buf_T *errbuf;
2578 int len;
2579 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 /* delete all existing lines */
2582 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2583 (void)ml_delete((linenr_T)1, FALSE);
2584
2585 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002586 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
2588 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002589 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2590 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
2592 if (qfp->qf_fnum != 0
2593 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2594 && errbuf->b_fname != NULL)
2595 {
2596 if (qfp->qf_type == 1) /* :helpgrep */
2597 STRCPY(IObuff, gettail(errbuf->b_fname));
2598 else
2599 STRCPY(IObuff, errbuf->b_fname);
2600 len = (int)STRLEN(IObuff);
2601 }
2602 else
2603 len = 0;
2604 IObuff[len++] = '|';
2605
2606 if (qfp->qf_lnum > 0)
2607 {
2608 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2609 len += (int)STRLEN(IObuff + len);
2610
2611 if (qfp->qf_col > 0)
2612 {
2613 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2614 len += (int)STRLEN(IObuff + len);
2615 }
2616
2617 sprintf((char *)IObuff + len, "%s",
2618 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2619 len += (int)STRLEN(IObuff + len);
2620 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002621 else if (qfp->qf_pattern != NULL)
2622 {
2623 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2624 len += (int)STRLEN(IObuff + len);
2625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 IObuff[len++] = '|';
2627 IObuff[len++] = ' ';
2628
2629 /* Remove newlines and leading whitespace from the text.
2630 * For an unrecognized line keep the indent, the compiler may
2631 * mark a word with ^^^^. */
2632 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2633 IObuff + len, IOSIZE - len);
2634
2635 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2636 == FAIL)
2637 break;
2638 qfp = qfp->qf_next;
2639 }
2640 /* Delete the empty line which is now at the end */
2641 (void)ml_delete(lnum + 1, FALSE);
2642 }
2643
2644 /* correct cursor position */
2645 check_lnums(TRUE);
2646
2647 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2648 * resembles reading a file into a buffer, it's more logical when using
2649 * autocommands. */
2650 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2651 curbuf->b_p_ma = FALSE;
2652
2653#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002654 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2656 FALSE, curbuf);
2657 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2658 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002659 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660#endif
2661
2662 /* make sure it will be redrawn */
2663 redraw_curbuf_later(NOT_VALID);
2664
2665 /* Restore KeyTyped, setting 'filetype' may reset it. */
2666 KeyTyped = old_KeyTyped;
2667}
2668
2669#endif /* FEAT_WINDOWS */
2670
2671/*
2672 * Return TRUE if "buf" is the quickfix buffer.
2673 */
2674 int
2675bt_quickfix(buf)
2676 buf_T *buf;
2677{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002678 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679}
2680
2681/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002682 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2683 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 */
2685 int
2686bt_nofile(buf)
2687 buf_T *buf;
2688{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002689 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2690 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691}
2692
2693/*
2694 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2695 */
2696 int
2697bt_dontwrite(buf)
2698 buf_T *buf;
2699{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002700 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701}
2702
2703 int
2704bt_dontwrite_msg(buf)
2705 buf_T *buf;
2706{
2707 if (bt_dontwrite(buf))
2708 {
2709 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2710 return TRUE;
2711 }
2712 return FALSE;
2713}
2714
2715/*
2716 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2717 * and 'bufhidden'.
2718 */
2719 int
2720buf_hide(buf)
2721 buf_T *buf;
2722{
2723 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2724 switch (buf->b_p_bh[0])
2725 {
2726 case 'u': /* "unload" */
2727 case 'w': /* "wipe" */
2728 case 'd': return FALSE; /* "delete" */
2729 case 'h': return TRUE; /* "hide" */
2730 }
2731 return (p_hid || cmdmod.hide);
2732}
2733
2734/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002735 * Return TRUE when using ":vimgrep" for ":grep".
2736 */
2737 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002738grep_internal(cmdidx)
2739 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002740{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002741 return ((cmdidx == CMD_grep
2742 || cmdidx == CMD_lgrep
2743 || cmdidx == CMD_grepadd
2744 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002745 && STRCMP("internal",
2746 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2747}
2748
2749/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002750 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 */
2752 void
2753ex_make(eap)
2754 exarg_T *eap;
2755{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002756 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 char_u *cmd;
2758 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002759 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002760 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002761 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002762#ifdef FEAT_AUTOCMD
2763 char_u *au_name = NULL;
2764
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002765 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2766 if (grep_internal(eap->cmdidx))
2767 {
2768 ex_vimgrep(eap);
2769 return;
2770 }
2771
Bram Moolenaar7c626922005-02-07 22:01:03 +00002772 switch (eap->cmdidx)
2773 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002774 case CMD_make: au_name = (char_u *)"make"; break;
2775 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2776 case CMD_grep: au_name = (char_u *)"grep"; break;
2777 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2778 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2779 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002780 default: break;
2781 }
2782 if (au_name != NULL)
2783 {
2784 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2785 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002786# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002787 if (did_throw || force_abort)
2788 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002789# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002790 }
2791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaara6557602006-02-04 22:43:20 +00002793 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2794 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002795 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002798 fname = get_mef_name();
2799 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002801 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
2803 /*
2804 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2805 */
2806 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2807 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002808 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 cmd = alloc(len);
2810 if (cmd == NULL)
2811 return;
2812 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2813 (char *)p_shq);
2814 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002815 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 /*
2817 * Output a newline if there's something else than the :make command that
2818 * was typed (in which case the cursor is in column 0).
2819 */
2820 if (msg_col == 0)
2821 msg_didout = FALSE;
2822 msg_start();
2823 MSG_PUTS(":!");
2824 msg_outtrans(cmd); /* show what we are doing */
2825
2826 /* let the shell know if we are redirecting output or not */
2827 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2828
2829#ifdef AMIGA
2830 out_flush();
2831 /* read window status report and redraw before message */
2832 (void)char_avail();
2833#endif
2834
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002836 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2837 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002838 && eap->cmdidx != CMD_lgrepadd),
2839 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002840 if (wp != NULL)
2841 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002842#ifdef FEAT_AUTOCMD
2843 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002844 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002845 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2846 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002847 if (qi->qf_curlist < qi->qf_listcount)
2848 res = qi->qf_lists[qi->qf_curlist].qf_count;
2849 else
2850 res = 0;
2851 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002852#endif
2853 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002854 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaar7c626922005-02-07 22:01:03 +00002856 mch_remove(fname);
2857 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 vim_free(cmd);
2859}
2860
2861/*
2862 * Return the name for the errorfile, in allocated memory.
2863 * Find a new unique name when 'makeef' contains "##".
2864 * Returns NULL for error.
2865 */
2866 static char_u *
2867get_mef_name()
2868{
2869 char_u *p;
2870 char_u *name;
2871 static int start = -1;
2872 static int off = 0;
2873#ifdef HAVE_LSTAT
2874 struct stat sb;
2875#endif
2876
2877 if (*p_mef == NUL)
2878 {
2879 name = vim_tempname('e');
2880 if (name == NULL)
2881 EMSG(_(e_notmp));
2882 return name;
2883 }
2884
2885 for (p = p_mef; *p; ++p)
2886 if (p[0] == '#' && p[1] == '#')
2887 break;
2888
2889 if (*p == NUL)
2890 return vim_strsave(p_mef);
2891
2892 /* Keep trying until the name doesn't exist yet. */
2893 for (;;)
2894 {
2895 if (start == -1)
2896 start = mch_get_pid();
2897 else
2898 off += 19;
2899
2900 name = alloc((unsigned)STRLEN(p_mef) + 30);
2901 if (name == NULL)
2902 break;
2903 STRCPY(name, p_mef);
2904 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2905 STRCAT(name, p + 2);
2906 if (mch_getperm(name) < 0
2907#ifdef HAVE_LSTAT
2908 /* Don't accept a symbolic link, its a security risk. */
2909 && mch_lstat((char *)name, &sb) < 0
2910#endif
2911 )
2912 break;
2913 vim_free(name);
2914 }
2915 return name;
2916}
2917
2918/*
2919 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 */
2922 void
2923ex_cc(eap)
2924 exarg_T *eap;
2925{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002926 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002927
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002928 if (eap->cmdidx == CMD_ll
2929 || eap->cmdidx == CMD_lrewind
2930 || eap->cmdidx == CMD_lfirst
2931 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002932 {
2933 qi = GET_LOC_LIST(curwin);
2934 if (qi == NULL)
2935 {
2936 EMSG(_(e_loclist));
2937 return;
2938 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002939 }
2940
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002941 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 eap->addr_count > 0
2943 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002944 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002946 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2947 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 ? 1
2949 : 32767,
2950 eap->forceit);
2951}
2952
2953/*
2954 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002955 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 */
2957 void
2958ex_cnext(eap)
2959 exarg_T *eap;
2960{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002961 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002962
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002963 if (eap->cmdidx == CMD_lnext
2964 || eap->cmdidx == CMD_lNext
2965 || eap->cmdidx == CMD_lprevious
2966 || eap->cmdidx == CMD_lnfile
2967 || eap->cmdidx == CMD_lNfile
2968 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002969 {
2970 qi = GET_LOC_LIST(curwin);
2971 if (qi == NULL)
2972 {
2973 EMSG(_(e_loclist));
2974 return;
2975 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002976 }
2977
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002978 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002980 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002982 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2983 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 ? BACKWARD_FILE
2985 : BACKWARD,
2986 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2987}
2988
2989/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002990 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002991 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 */
2993 void
2994ex_cfile(eap)
2995 exarg_T *eap;
2996{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002997 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002998 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01002999#ifdef FEAT_AUTOCMD
3000 char_u *au_name = NULL;
3001#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003002
3003 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003004 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003005 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003006
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003007#ifdef FEAT_AUTOCMD
3008 switch (eap->cmdidx)
3009 {
3010 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3011 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3012 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3013 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3014 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3015 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3016 default: break;
3017 }
3018 if (au_name != NULL)
3019 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3020#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003021#ifdef FEAT_BROWSE
3022 if (cmdmod.browse)
3023 {
3024 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3025 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3026 if (browse_file == NULL)
3027 return;
3028 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3029 vim_free(browse_file);
3030 }
3031 else
3032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003034 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003035
3036 /*
3037 * This function is used by the :cfile, :cgetfile and :caddfile
3038 * commands.
3039 * :cfile always creates a new quickfix list and jumps to the
3040 * first error.
3041 * :cgetfile creates a new quickfix list but doesn't jump to the
3042 * first error.
3043 * :caddfile adds to an existing quickfix list. If there is no
3044 * quickfix list then a new list is created.
3045 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003046 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003047 && eap->cmdidx != CMD_laddfile),
3048 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003049 && (eap->cmdidx == CMD_cfile
3050 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003051 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003052#ifdef FEAT_AUTOCMD
3053 if (au_name != NULL)
3054 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3055#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003056 if (wp != NULL)
3057 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003058 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003059 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003060
3061 else
3062 {
3063#ifdef FEAT_AUTOCMD
3064 if (au_name != NULL)
3065 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3066#endif
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068}
3069
3070/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003071 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003072 * ":vimgrepadd {pattern} file(s)"
3073 * ":lvimgrep {pattern} file(s)"
3074 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003075 */
3076 void
3077ex_vimgrep(eap)
3078 exarg_T *eap;
3079{
Bram Moolenaar81695252004-12-29 20:58:21 +00003080 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003081 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003082 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003083 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003084 char_u *s;
3085 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003086 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003087 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003088 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003089 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003090 buf_T *buf;
3091 int duplicate_name = FALSE;
3092 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003093 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003094 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003095 buf_T *first_match_buf = NULL;
3096 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003097 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003098#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3099 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003100#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003101 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003102 int flags = 0;
3103 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003104 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003105 char_u *dirname_start = NULL;
3106 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003107 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003108#ifdef FEAT_AUTOCMD
3109 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003110
3111 switch (eap->cmdidx)
3112 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003113 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3114 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3115 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003116 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003117 case CMD_grep: au_name = (char_u *)"grep"; break;
3118 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3119 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3120 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003121 default: break;
3122 }
3123 if (au_name != NULL)
3124 {
3125 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3126 curbuf->b_fname, TRUE, curbuf);
3127 if (did_throw || force_abort)
3128 return;
3129 }
3130#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003131
Bram Moolenaar754b5602006-02-09 23:53:20 +00003132 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003133 || eap->cmdidx == CMD_lvimgrep
3134 || eap->cmdidx == CMD_lgrepadd
3135 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003136 {
3137 qi = ll_get_or_alloc_list(curwin);
3138 if (qi == NULL)
3139 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003140 }
3141
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003142 if (eap->addr_count > 0)
3143 tomatch = eap->line2;
3144 else
3145 tomatch = MAXLNUM;
3146
Bram Moolenaar81695252004-12-29 20:58:21 +00003147 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003148 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003149 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003150 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003151 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003152 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003153 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003154 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003155 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003156 if (regmatch.regprog == NULL)
3157 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003158 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003159 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003160
3161 p = skipwhite(p);
3162 if (*p == NUL)
3163 {
3164 EMSG(_("E683: File name missing or invalid pattern"));
3165 goto theend;
3166 }
3167
Bram Moolenaar754b5602006-02-09 23:53:20 +00003168 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003169 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003170 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003171 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003172 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003173 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003174 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003175 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003176 prevp->qf_next != prevp; prevp = prevp->qf_next)
3177 ;
3178
3179 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003180 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003181 goto theend;
3182 if (fcount == 0)
3183 {
3184 EMSG(_(e_nomatch));
3185 goto theend;
3186 }
3187
Bram Moolenaard9462e32011-04-11 21:35:11 +02003188 dirname_start = alloc(MAXPATHL);
3189 dirname_now = alloc(MAXPATHL);
3190 if (dirname_start == NULL || dirname_now == NULL)
3191 goto theend;
3192
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003193 /* Remember the current directory, because a BufRead autocommand that does
3194 * ":lcd %:p:h" changes the meaning of short path names. */
3195 mch_dirname(dirname_start, MAXPATHL);
3196
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003197 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003198 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003199 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003200 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003201 if (time(NULL) > seconds)
3202 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003203 /* Display the file name every second or so, show the user we are
3204 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003205 seconds = time(NULL);
3206 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003207 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003208 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003209 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003210 else
3211 {
3212 msg_outtrans(p);
3213 vim_free(p);
3214 }
3215 msg_clr_eos();
3216 msg_didout = FALSE; /* overwrite this message */
3217 msg_nowait = TRUE; /* don't wait for this message */
3218 msg_col = 0;
3219 out_flush();
3220 }
3221
Bram Moolenaar81695252004-12-29 20:58:21 +00003222 buf = buflist_findname_exp(fnames[fi]);
3223 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3224 {
3225 /* Remember that a buffer with this name already exists. */
3226 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003227 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003228 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003229
3230#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3231 /* Don't do Filetype autocommands to avoid loading syntax and
3232 * indent scripts, a great speed improvement. */
3233 save_ei = au_event_disable(",Filetype");
3234#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003235 /* Don't use modelines here, it's useless. */
3236 save_mls = p_mls;
3237 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003238
3239 /* Load file into a buffer, so that 'fileencoding' is detected,
3240 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003241 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003242
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003243 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003244#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3245 au_event_restore(save_ei);
3246#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003247 }
3248 else
3249 /* Use existing, loaded buffer. */
3250 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003251
Bram Moolenaar81695252004-12-29 20:58:21 +00003252 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003253 {
3254 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003255 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003256 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003257 else
3258 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003259 /* Try for a match in all lines of the buffer.
3260 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003261 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003262 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3263 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003264 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003265 col = 0;
3266 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003267 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003268 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003269 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003270 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003271 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003272 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003273 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003274 ml_get_buf(buf,
3275 regmatch.startpos[0].lnum + lnum, FALSE),
3276 regmatch.startpos[0].lnum + lnum,
3277 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003278 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003279 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003280 0, /* nr */
3281 0, /* type */
3282 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003283 ) == FAIL)
3284 {
3285 got_int = TRUE;
3286 break;
3287 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003288 found_match = TRUE;
3289 if (--tomatch == 0)
3290 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003291 if ((flags & VGR_GLOBAL) == 0
3292 || regmatch.endpos[0].lnum > 0)
3293 break;
3294 col = regmatch.endpos[0].col
3295 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003296 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003297 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003298 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003299 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003300 if (got_int)
3301 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003302 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003303
3304 if (using_dummy)
3305 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003306 if (found_match && first_match_buf == NULL)
3307 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003308 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003309 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003310 /* Never keep a dummy buffer if there is another buffer
3311 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003312 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003313 buf = NULL;
3314 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003315 else if (!cmdmod.hide
3316 || buf->b_p_bh[0] == 'u' /* "unload" */
3317 || buf->b_p_bh[0] == 'w' /* "wipe" */
3318 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003319 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003320 /* When no match was found we don't need to remember the
3321 * buffer, wipe it out. If there was a match and it
3322 * wasn't the first one or we won't jump there: only
3323 * unload the buffer.
3324 * Ignore 'hidden' here, because it may lead to having too
3325 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003326 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003327 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003328 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003329 buf = NULL;
3330 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003331 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003332 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003333 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003334 buf = NULL;
3335 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003336 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003337
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003338 if (buf != NULL)
3339 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003340 /* If the buffer is still loaded we need to use the
3341 * directory we jumped to below. */
3342 if (buf == first_match_buf
3343 && target_dir == NULL
3344 && STRCMP(dirname_start, dirname_now) != 0)
3345 target_dir = vim_strsave(dirname_now);
3346
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003347 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003348 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003349 * need to be done (again). But not the window-local
3350 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003351 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003352#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003353 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3354 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003355#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003356 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003357 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003358 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003359 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003360 }
3361 }
3362
3363 FreeWild(fcount, fnames);
3364
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003365 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3366 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3367 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003368
3369#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003370 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003371#endif
3372
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003373#ifdef FEAT_AUTOCMD
3374 if (au_name != NULL)
3375 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3376 curbuf->b_fname, TRUE, curbuf);
3377#endif
3378
Bram Moolenaar86b68352004-12-27 21:59:20 +00003379 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003380 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003381 {
3382 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003383 {
3384 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003385 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003386 if (buf != curbuf)
3387 /* If we jumped to another buffer redrawing will already be
3388 * taken care of. */
3389 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003390
3391 /* Jump to the directory used after loading the buffer. */
3392 if (curbuf == first_match_buf && target_dir != NULL)
3393 {
3394 exarg_T ea;
3395
3396 ea.arg = target_dir;
3397 ea.cmdidx = CMD_lcd;
3398 ex_cd(&ea);
3399 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003400 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003401 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003402 else
3403 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003404
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003405 /* If we loaded a dummy buffer into the current window, the autocommands
3406 * may have messed up things, need to redraw and recompute folds. */
3407 if (redraw_for_dummy)
3408 {
3409#ifdef FEAT_FOLDING
3410 foldUpdateAll(curwin);
3411#else
3412 redraw_later(NOT_VALID);
3413#endif
3414 }
3415
Bram Moolenaar86b68352004-12-27 21:59:20 +00003416theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003417 vim_free(dirname_now);
3418 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003419 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003420 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003421}
3422
3423/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003424 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003425 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003426 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3427 * If "s" is not NULL terminate the pattern with a NUL.
3428 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003429 */
3430 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003431skip_vimgrep_pat(p, s, flags)
3432 char_u *p;
3433 char_u **s;
3434 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003435{
3436 int c;
3437
3438 if (vim_isIDc(*p))
3439 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003440 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003441 if (s != NULL)
3442 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003443 p = skiptowhite(p);
3444 if (s != NULL && *p != NUL)
3445 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003446 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003447 else
3448 {
3449 /* ":vimgrep /pattern/[g][j] fname" */
3450 if (s != NULL)
3451 *s = p + 1;
3452 c = *p;
3453 p = skip_regexp(p + 1, c, TRUE, NULL);
3454 if (*p != c)
3455 return NULL;
3456
3457 /* Truncate the pattern. */
3458 if (s != NULL)
3459 *p = NUL;
3460 ++p;
3461
3462 /* Find the flags */
3463 while (*p == 'g' || *p == 'j')
3464 {
3465 if (flags != NULL)
3466 {
3467 if (*p == 'g')
3468 *flags |= VGR_GLOBAL;
3469 else
3470 *flags |= VGR_NOJUMP;
3471 }
3472 ++p;
3473 }
3474 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003475 return p;
3476}
3477
3478/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003479 * Restore current working directory to "dirname_start" if they differ, taking
3480 * into account whether it is set locally or globally.
3481 */
3482 static void
3483restore_start_dir(dirname_start)
3484 char_u *dirname_start;
3485{
3486 char_u *dirname_now = alloc(MAXPATHL);
3487
3488 if (NULL != dirname_now)
3489 {
3490 mch_dirname(dirname_now, MAXPATHL);
3491 if (STRCMP(dirname_start, dirname_now) != 0)
3492 {
3493 /* If the directory has changed, change it back by building up an
3494 * appropriate ex command and executing it. */
3495 exarg_T ea;
3496
3497 ea.arg = dirname_start;
3498 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3499 ex_cd(&ea);
3500 }
3501 }
3502}
3503
3504/*
3505 * Load file "fname" into a dummy buffer and return the buffer pointer,
3506 * placing the directory resulting from the buffer load into the
3507 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3508 * prior to calling this function. Restores directory to "dirname_start" prior
3509 * to returning, if autocmds or the 'autochdir' option have changed it.
3510 *
3511 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3512 * or wipe_dummy_buffer() later!
3513 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003514 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003515 */
3516 static buf_T *
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003517load_dummy_buffer(fname, dirname_start, resulting_dir)
Bram Moolenaar81695252004-12-29 20:58:21 +00003518 char_u *fname;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003519 char_u *dirname_start; /* in: old directory */
3520 char_u *resulting_dir; /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003521{
3522 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003523 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003524 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003525 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003526
3527 /* Allocate a buffer without putting it in the buffer list. */
3528 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3529 if (newbuf == NULL)
3530 return NULL;
3531
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003532 /* Init the options. */
3533 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3534
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003535 /* need to open the memfile before putting the buffer in a window */
3536 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003537 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003538 /* set curwin/curbuf to buf and save a few things */
3539 aucmd_prepbuf(&aco, newbuf);
3540
3541 /* Need to set the filename for autocommands. */
3542 (void)setfname(curbuf, fname, NULL, FALSE);
3543
Bram Moolenaar81695252004-12-29 20:58:21 +00003544 /* Create swap file now to avoid the ATTENTION message. */
3545 check_need_swap(TRUE);
3546
3547 /* Remove the "dummy" flag, otherwise autocommands may not
3548 * work. */
3549 curbuf->b_flags &= ~BF_DUMMY;
3550
3551 if (readfile(fname, NULL,
3552 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3553 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003554 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003555 && !(curbuf->b_flags & BF_NEW))
3556 {
3557 failed = FALSE;
3558 if (curbuf != newbuf)
3559 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003560 /* Bloody autocommands changed the buffer! Can happen when
3561 * using netrw and editing a remote file. Use the current
3562 * buffer instead, delete the dummy one after restoring the
3563 * window stuff. */
3564 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003565 newbuf = curbuf;
3566 }
3567 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003568
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003569 /* restore curwin/curbuf and a few other things */
3570 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003571 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3572 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003573 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003574
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003575 /*
3576 * When autocommands/'autochdir' option changed directory: go back.
3577 * Let the caller know what the resulting dir was first, in case it is
3578 * important.
3579 */
3580 mch_dirname(resulting_dir, MAXPATHL);
3581 restore_start_dir(dirname_start);
3582
Bram Moolenaar81695252004-12-29 20:58:21 +00003583 if (!buf_valid(newbuf))
3584 return NULL;
3585 if (failed)
3586 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003587 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003588 return NULL;
3589 }
3590 return newbuf;
3591}
3592
3593/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003594 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3595 * directory to "dirname_start" prior to returning, if autocmds or the
3596 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003597 */
3598 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003599wipe_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003600 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003601 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003602{
3603 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003604 {
3605#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3606 cleanup_T cs;
3607
3608 /* Reset the error/interrupt/exception state here so that aborting()
3609 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3610 * work when got_int is set. */
3611 enter_cleanup(&cs);
3612#endif
3613
Bram Moolenaar81695252004-12-29 20:58:21 +00003614 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003615
3616#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3617 /* Restore the error/interrupt/exception state if not discarded by a
3618 * new aborting error, interrupt, or uncaught exception. */
3619 leave_cleanup(&cs);
3620#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003621 /* When autocommands/'autochdir' option changed directory: go back. */
3622 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003623 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003624}
3625
3626/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003627 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3628 * directory to "dirname_start" prior to returning, if autocmds or the
3629 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003630 */
3631 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003632unload_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003633 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003634 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003635{
3636 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003637 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003638 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003639
3640 /* When autocommands/'autochdir' option changed directory: go back. */
3641 restore_start_dir(dirname_start);
3642 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003643}
3644
Bram Moolenaar05159a02005-02-26 23:04:13 +00003645#if defined(FEAT_EVAL) || defined(PROTO)
3646/*
3647 * Add each quickfix error to list "list" as a dictionary.
3648 */
3649 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003650get_errorlist(wp, list)
3651 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003652 list_T *list;
3653{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003654 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003655 dict_T *dict;
3656 char_u buf[2];
3657 qfline_T *qfp;
3658 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003659 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003660
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003661 if (wp != NULL)
3662 {
3663 qi = GET_LOC_LIST(wp);
3664 if (qi == NULL)
3665 return FAIL;
3666 }
3667
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003668 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003669 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003670 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003671
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003672 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3673 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003674 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003675 /* Handle entries with a non-existing buffer number. */
3676 bufnum = qfp->qf_fnum;
3677 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3678 bufnum = 0;
3679
Bram Moolenaar05159a02005-02-26 23:04:13 +00003680 if ((dict = dict_alloc()) == NULL)
3681 return FAIL;
3682 if (list_append_dict(list, dict) == FAIL)
3683 return FAIL;
3684
3685 buf[0] = qfp->qf_type;
3686 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003687 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003688 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3689 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3690 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3691 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003692 || dict_add_nr_str(dict, "pattern", 0L,
3693 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3694 || dict_add_nr_str(dict, "text", 0L,
3695 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003696 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3697 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3698 return FAIL;
3699
3700 qfp = qfp->qf_next;
3701 }
3702 return OK;
3703}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003704
3705/*
3706 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003707 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003708 */
3709 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003710set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003711 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003712 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003713 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003714 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003715{
3716 listitem_T *li;
3717 dict_T *d;
3718 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003719 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003720 long lnum;
3721 int col, nr;
3722 int vcol;
3723 qfline_T *prevp = NULL;
3724 int valid, status;
3725 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003726 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003727 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003728
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003729 if (wp != NULL)
3730 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003731 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003732 if (qi == NULL)
3733 return FAIL;
3734 }
3735
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003736 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003737 /* make place for a new list */
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003738 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003739 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003740 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003741 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003742 prevp->qf_next != prevp; prevp = prevp->qf_next)
3743 ;
3744 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003745 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003746
3747 for (li = list->lv_first; li != NULL; li = li->li_next)
3748 {
3749 if (li->li_tv.v_type != VAR_DICT)
3750 continue; /* Skip non-dict items */
3751
3752 d = li->li_tv.vval.v_dict;
3753 if (d == NULL)
3754 continue;
3755
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003756 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003757 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003758 lnum = get_dict_number(d, (char_u *)"lnum");
3759 col = get_dict_number(d, (char_u *)"col");
3760 vcol = get_dict_number(d, (char_u *)"vcol");
3761 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003762 type = get_dict_string(d, (char_u *)"type", TRUE);
3763 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3764 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003765 if (text == NULL)
3766 text = vim_strsave((char_u *)"");
3767
3768 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003769 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003770 valid = FALSE;
3771
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003772 /* Mark entries with non-existing buffer number as not valid. Give the
3773 * error message only once. */
3774 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3775 {
3776 if (!did_bufnr_emsg)
3777 {
3778 did_bufnr_emsg = TRUE;
3779 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3780 }
3781 valid = FALSE;
3782 bufnum = 0;
3783 }
3784
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003785 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003786 NULL, /* dir */
3787 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003788 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003789 text,
3790 lnum,
3791 col,
3792 vcol, /* vis_col */
3793 pattern, /* search pattern */
3794 nr,
3795 type == NULL ? NUL : *type,
3796 valid);
3797
3798 vim_free(filename);
3799 vim_free(pattern);
3800 vim_free(text);
3801 vim_free(type);
3802
3803 if (status == FAIL)
3804 {
3805 retval = FAIL;
3806 break;
3807 }
3808 }
3809
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003810 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003811 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003812 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3813 else
3814 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3816 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003817
3818#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003819 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003820#endif
3821
3822 return retval;
3823}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003824#endif
3825
Bram Moolenaar81695252004-12-29 20:58:21 +00003826/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003827 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003828 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003829 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003830 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003831 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003832 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003833 */
3834 void
3835ex_cbuffer(eap)
3836 exarg_T *eap;
3837{
3838 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003839 qf_info_T *qi = &ql_info;
3840
Bram Moolenaardb552d602006-03-23 22:59:57 +00003841 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3842 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003843 {
3844 qi = ll_get_or_alloc_list(curwin);
3845 if (qi == NULL)
3846 return;
3847 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003848
3849 if (*eap->arg == NUL)
3850 buf = curbuf;
3851 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3852 buf = buflist_findnr(atoi((char *)eap->arg));
3853 if (buf == NULL)
3854 EMSG(_(e_invarg));
3855 else if (buf->b_ml.ml_mfp == NULL)
3856 EMSG(_("E681: Buffer is not loaded"));
3857 else
3858 {
3859 if (eap->addr_count == 0)
3860 {
3861 eap->line1 = 1;
3862 eap->line2 = buf->b_ml.ml_line_count;
3863 }
3864 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3865 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3866 EMSG(_(e_invrange));
3867 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003868 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003869 char_u *qf_title = *eap->cmdlinep;
3870
3871 if (buf->b_sfname)
3872 {
3873 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3874 (char *)qf_title, (char *)buf->b_sfname);
3875 qf_title = IObuff;
3876 }
3877
Bram Moolenaardb552d602006-03-23 22:59:57 +00003878 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3879 (eap->cmdidx != CMD_caddbuffer
3880 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003881 eap->line1, eap->line2,
3882 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003883 && (eap->cmdidx == CMD_cbuffer
3884 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003885 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3886 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003887 }
3888}
3889
Bram Moolenaar1e015462005-09-25 22:16:38 +00003890#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003891/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003892 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3893 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003894 */
3895 void
3896ex_cexpr(eap)
3897 exarg_T *eap;
3898{
3899 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003900 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003901
Bram Moolenaardb552d602006-03-23 22:59:57 +00003902 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3903 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003904 {
3905 qi = ll_get_or_alloc_list(curwin);
3906 if (qi == NULL)
3907 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003908 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003909
Bram Moolenaar4770d092006-01-12 23:22:24 +00003910 /* Evaluate the expression. When the result is a string or a list we can
3911 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003912 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003913 if (tv != NULL)
3914 {
3915 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3916 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3917 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003918 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3919 (eap->cmdidx != CMD_caddexpr
3920 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003921 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003922 && (eap->cmdidx == CMD_cexpr
3923 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003924 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003925 }
3926 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003927 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003928 free_tv(tv);
3929 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003930}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003931#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003932
3933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 * ":helpgrep {pattern}"
3935 */
3936 void
3937ex_helpgrep(eap)
3938 exarg_T *eap;
3939{
3940 regmatch_T regmatch;
3941 char_u *save_cpo;
3942 char_u *p;
3943 int fcount;
3944 char_u **fnames;
3945 FILE *fd;
3946 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003947 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003949#ifdef FEAT_MULTI_LANG
3950 char_u *lang;
3951#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003952 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003953 int new_qi = FALSE;
3954 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01003955#ifdef FEAT_AUTOCMD
3956 char_u *au_name = NULL;
3957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003959#ifdef FEAT_MULTI_LANG
3960 /* Check for a specified language */
3961 lang = check_help_lang(eap->arg);
3962#endif
3963
Bram Moolenaar73633f82012-01-20 13:39:07 +01003964#ifdef FEAT_AUTOCMD
3965 switch (eap->cmdidx)
3966 {
3967 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
3968 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
3969 default: break;
3970 }
3971 if (au_name != NULL)
3972 {
3973 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3974 curbuf->b_fname, TRUE, curbuf);
3975 if (did_throw || force_abort)
3976 return;
3977 }
3978#endif
3979
3980 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3981 save_cpo = p_cpo;
3982 p_cpo = empty_option;
3983
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003984 if (eap->cmdidx == CMD_lhelpgrep)
3985 {
3986 /* Find an existing help window */
3987 FOR_ALL_WINDOWS(wp)
3988 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3989 break;
3990
3991 if (wp == NULL) /* Help window not found */
3992 qi = NULL;
3993 else
3994 qi = wp->w_llist;
3995
3996 if (qi == NULL)
3997 {
3998 /* Allocate a new location list for help text matches */
3999 if ((qi = ll_new_list()) == NULL)
4000 return;
4001 new_qi = TRUE;
4002 }
4003 }
4004
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4006 regmatch.rm_ic = FALSE;
4007 if (regmatch.regprog != NULL)
4008 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004009#ifdef FEAT_MBYTE
4010 vimconv_T vc;
4011
4012 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4013 * differs. */
4014 vc.vc_type = CONV_NONE;
4015 if (!enc_utf8)
4016 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4017#endif
4018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 /* create a new quickfix list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004020 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022 /* Go through all directories in 'runtimepath' */
4023 p = p_rtp;
4024 while (*p != NUL && !got_int)
4025 {
4026 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4027
4028 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4029 add_pathsep(NameBuff);
4030 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4031 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4032 &fnames, EW_FILE|EW_SILENT) == OK
4033 && fcount > 0)
4034 {
4035 for (fi = 0; fi < fcount && !got_int; ++fi)
4036 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004037#ifdef FEAT_MULTI_LANG
4038 /* Skip files for a different language. */
4039 if (lang != NULL
4040 && STRNICMP(lang, fnames[fi]
4041 + STRLEN(fnames[fi]) - 3, 2) != 0
4042 && !(STRNICMP(lang, "en", 2) == 0
4043 && STRNICMP("txt", fnames[fi]
4044 + STRLEN(fnames[fi]) - 3, 3) == 0))
4045 continue;
4046#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004047 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (fd != NULL)
4049 {
4050 lnum = 1;
4051 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4052 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004053 char_u *line = IObuff;
4054#ifdef FEAT_MBYTE
4055 /* Convert a line if 'encoding' is not utf-8 and
4056 * the line contains a non-ASCII character. */
4057 if (vc.vc_type != CONV_NONE
4058 && has_non_ascii(IObuff)) {
4059 line = string_convert(&vc, IObuff, NULL);
4060 if (line == NULL)
4061 line = IObuff;
4062 }
4063#endif
4064
4065 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004067 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004070 while (l > 0 && line[l - 1] <= ' ')
4071 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004073 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 NULL, /* dir */
4075 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004076 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004077 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004079 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004080 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004081 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004082 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 0, /* nr */
4084 1, /* type */
4085 TRUE /* valid */
4086 ) == FAIL)
4087 {
4088 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004089#ifdef FEAT_MBYTE
4090 if (line != IObuff)
4091 vim_free(line);
4092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 break;
4094 }
4095 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004096#ifdef FEAT_MBYTE
4097 if (line != IObuff)
4098 vim_free(line);
4099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 ++lnum;
4101 line_breakcheck();
4102 }
4103 fclose(fd);
4104 }
4105 }
4106 FreeWild(fcount, fnames);
4107 }
4108 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 vim_free(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004111#ifdef FEAT_MBYTE
4112 if (vc.vc_type != CONV_NONE)
4113 convert_setup(&vc, NULL, NULL);
4114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004116 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4117 qi->qf_lists[qi->qf_curlist].qf_ptr =
4118 qi->qf_lists[qi->qf_curlist].qf_start;
4119 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004122 if (p_cpo == empty_option)
4123 p_cpo = save_cpo;
4124 else
4125 /* Darn, some plugin changed the value. */
4126 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
4128#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004129 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130#endif
4131
Bram Moolenaar73633f82012-01-20 13:39:07 +01004132#ifdef FEAT_AUTOCMD
4133 if (au_name != NULL)
4134 {
4135 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4136 curbuf->b_fname, TRUE, curbuf);
4137 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4138 /* autocommands made "qi" invalid */
4139 return;
4140 }
4141#endif
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004144 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004145 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004146 else
4147 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004148
4149 if (eap->cmdidx == CMD_lhelpgrep)
4150 {
4151 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004152 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004153 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4154 {
4155 if (new_qi)
4156 ll_free_all(&qi);
4157 }
4158 else if (curwin->w_llist == NULL)
4159 curwin->w_llist = qi;
4160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161}
4162
4163#endif /* FEAT_QUICKFIX */