blob: 26eac7be59143640a825d5a12aded0a6f5207fbf [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
55 qfline_T *qf_ptr; /* pointer to the current error */
56 int qf_count; /* number of errors (0 means no error list) */
57 int qf_index; /* current index in the error list */
58 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059 char_u *qf_title; /* title derived from the command that created
60 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
75};
76
77static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar68b76a62005-03-25 21:53:48 +000079#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * Structure used to hold the info of one part of 'errorformat'
83 */
Bram Moolenaar01265852006-03-20 21:50:15 +000084typedef struct efm_S efm_T;
85struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000088 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
90 char_u prefix; /* prefix of this format line: */
91 /* 'D' enter directory */
92 /* 'X' leave directory */
93 /* 'A' start of multi-line message */
94 /* 'E' error message */
95 /* 'W' warning message */
96 /* 'I' informational message */
97 /* 'C' continuation line */
98 /* 'Z' end of multi-line message */
99 /* 'G' general, unspecific message */
100 /* 'P' push file (partial) message */
101 /* 'Q' pop/quit file (partial) message */
102 /* 'O' overread (partial) message */
103 char_u flags; /* additional flags given in prefix */
104 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000105 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000106 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107};
108
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200109static int qf_init_ext __ARGS((qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title));
110static void qf_new_list __ARGS((qf_info_T *qi, char_u *qf_title));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000111static void ll_free_all __ARGS((qf_info_T **pqi));
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000112static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000113static qf_info_T *ll_new_list __ARGS((void));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000114static void qf_msg __ARGS((qf_info_T *qi));
115static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static char_u *qf_types __ARGS((int, int));
117static int qf_get_fnum __ARGS((char_u *, char_u *));
118static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
119static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
120static char_u *qf_guess_filepath __ARGS((char_u *));
121static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
122static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
123#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000124static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000125static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000126static win_T *qf_find_win __ARGS((qf_info_T *qi));
127static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
128static void qf_update_buffer __ARGS((qf_info_T *qi));
Bram Moolenaarc95e3262011-08-10 18:36:54 +0200129static void qf_set_title __ARGS((qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000133static buf_T *load_dummy_buffer __ARGS((char_u *fname));
134static void wipe_dummy_buffer __ARGS((buf_T *buf));
135static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000136static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000138/* Quickfix window check helper macro */
139#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
140/* Location list window check helper macro */
141#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
142/*
143 * Return location list for window 'wp'
144 * For location list window, return the referenced location list
145 */
146#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
147
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000149 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200150 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 * Return -1 for error, number of errors for success.
152 */
153 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200154qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000155 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 char_u *efile;
157 char_u *errorformat;
158 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200159 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000161 qf_info_T *qi = &ql_info;
162
Bram Moolenaar86b68352004-12-27 21:59:20 +0000163 if (efile == NULL)
164 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000165
166 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000167 {
168 qi = ll_get_or_alloc_list(wp);
169 if (qi == NULL)
170 return FAIL;
171 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172
173 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200174 (linenr_T)0, (linenr_T)0,
175 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000176}
177
178/*
179 * Read the errorfile "efile" into memory, line by line, building the error
180 * list.
181 * Alternative: when "efile" is null read errors from buffer "buf".
182 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200183 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
184 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185 * Return -1 for error, number of errors for success.
186 */
187 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200188qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
189 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000190 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000191 char_u *efile;
192 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000193 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000194 char_u *errorformat;
195 int newlist; /* TRUE: start a new error list */
196 linenr_T lnumfirst; /* first line number to use */
197 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200198 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000199{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 char_u *namebuf;
201 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000202 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 char_u *fmtstr = NULL;
204 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000205 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 int type = 0;
207 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 long lnum = 0L;
210 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000211 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000212 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000214 efm_T *fmt_first = NULL;
215 efm_T *fmt_last = NULL;
216 efm_T *fmt_ptr;
217 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 char_u *efm;
219 char_u *ptr;
220 char_u *srcptr;
221 int len;
222 int i;
223 int round;
224 int idx = 0;
225 int multiline = FALSE;
226 int multiignore = FALSE;
227 int multiscan = FALSE;
228 int retval = -1; /* default: return error flag */
229 char_u *directory = NULL;
230 char_u *currfile = NULL;
231 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000232 char_u *p_str = NULL;
233 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 struct dir_stack_T *file_stack = NULL;
235 regmatch_T regmatch;
236 static struct fmtpattern
237 {
238 char_u convchar;
239 char *pattern;
240 } fmt_pat[FMT_PATTERNS] =
241 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000242 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {'n', "\\d\\+"},
244 {'l', "\\d\\+"},
245 {'c', "\\d\\+"},
246 {'t', "."},
247 {'m', ".\\+"},
248 {'r', ".*"},
249 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000250 {'v', "\\d\\+"},
251 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 };
253
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 namebuf = alloc(CMDBUFFSIZE + 1);
255 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000256 pattern = alloc(CMDBUFFSIZE + 1);
257 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 goto qf_init_end;
259
Bram Moolenaar86b68352004-12-27 21:59:20 +0000260 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
262 EMSG2(_(e_openerrf), efile);
263 goto qf_init_end;
264 }
265
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000266 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200268 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000269 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000271 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200272 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 ;
274
275/*
276 * Each part of the format string is copied and modified from errorformat to
277 * regex prog. Only a few % characters are allowed.
278 */
279 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000280 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000281 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 else
283 efm = errorformat;
284 /*
285 * Get some space to modify the format string into.
286 */
287 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
288 for (round = FMT_PATTERNS; round > 0; )
289 i += (int)STRLEN(fmt_pat[--round].pattern);
290#ifdef COLON_IN_FILENAME
291 i += 12; /* "%f" can become twelve chars longer */
292#else
293 i += 2; /* "%f" can become two chars longer */
294#endif
295 if ((fmtstr = alloc(i)) == NULL)
296 goto error2;
297
Bram Moolenaar01265852006-03-20 21:50:15 +0000298 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 {
300 /*
301 * Allocate a new eformat structure and put it at the end of the list
302 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000303 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 if (fmt_ptr == NULL)
305 goto error2;
306 if (fmt_first == NULL) /* first one */
307 fmt_first = fmt_ptr;
308 else
309 fmt_last->next = fmt_ptr;
310 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312 /*
313 * Isolate one part in the 'errorformat' option
314 */
315 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
316 if (efm[len] == '\\' && efm[len + 1] != NUL)
317 ++len;
318
319 /*
320 * Build regexp pattern from current 'errorformat' option
321 */
322 ptr = fmtstr;
323 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000324 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 for (efmp = efm; efmp < efm + len; ++efmp)
326 {
327 if (*efmp == '%')
328 {
329 ++efmp;
330 for (idx = 0; idx < FMT_PATTERNS; ++idx)
331 if (fmt_pat[idx].convchar == *efmp)
332 break;
333 if (idx < FMT_PATTERNS)
334 {
335 if (fmt_ptr->addr[idx])
336 {
337 sprintf((char *)errmsg,
338 _("E372: Too many %%%c in format string"), *efmp);
339 EMSG(errmsg);
340 goto error2;
341 }
342 if ((idx
343 && idx < 6
344 && vim_strchr((char_u *)"DXOPQ",
345 fmt_ptr->prefix) != NULL)
346 || (idx == 6
347 && vim_strchr((char_u *)"OPQ",
348 fmt_ptr->prefix) == NULL))
349 {
350 sprintf((char *)errmsg,
351 _("E373: Unexpected %%%c in format string"), *efmp);
352 EMSG(errmsg);
353 goto error2;
354 }
355 fmt_ptr->addr[idx] = (char_u)++round;
356 *ptr++ = '\\';
357 *ptr++ = '(';
358#ifdef BACKSLASH_IN_FILENAME
359 if (*efmp == 'f')
360 {
361 /* Also match "c:" in the file name, even when
362 * checking for a colon next: "%f:".
363 * "\%(\a:\)\=" */
364 STRCPY(ptr, "\\%(\\a:\\)\\=");
365 ptr += 10;
366 }
367#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000368 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000370 if (efmp[1] != '\\' && efmp[1] != '%')
371 {
372 /* A file name may contain spaces, but this isn't
373 * in "\f". For "%f:%l:%m" there may be a ":" in
374 * the file name. Use ".\{-1,}x" instead (x is
375 * the next character), the requirement that :999:
376 * follows should work. */
377 STRCPY(ptr, ".\\{-1,}");
378 ptr += 7;
379 }
380 else
381 {
382 /* File name followed by '\\' or '%': include as
383 * many file name chars as possible. */
384 STRCPY(ptr, "\\f\\+");
385 ptr += 4;
386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 else
389 {
390 srcptr = (char_u *)fmt_pat[idx].pattern;
391 while ((*ptr = *srcptr++) != NUL)
392 ++ptr;
393 }
394 *ptr++ = '\\';
395 *ptr++ = ')';
396 }
397 else if (*efmp == '*')
398 {
399 if (*++efmp == '[' || *efmp == '\\')
400 {
401 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
402 {
403 if (efmp[1] == '^')
404 *ptr++ = *++efmp;
405 if (efmp < efm + len)
406 {
407 *ptr++ = *++efmp; /* could be ']' */
408 while (efmp < efm + len
409 && (*ptr++ = *++efmp) != ']')
410 /* skip */;
411 if (efmp == efm + len)
412 {
413 EMSG(_("E374: Missing ] in format string"));
414 goto error2;
415 }
416 }
417 }
418 else if (efmp < efm + len) /* %*\D, %*\s etc. */
419 *ptr++ = *++efmp;
420 *ptr++ = '\\';
421 *ptr++ = '+';
422 }
423 else
424 {
425 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
426 sprintf((char *)errmsg,
427 _("E375: Unsupported %%%c in format string"), *efmp);
428 EMSG(errmsg);
429 goto error2;
430 }
431 }
432 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
433 *ptr++ = *efmp; /* regexp magic characters */
434 else if (*efmp == '#')
435 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000436 else if (*efmp == '>')
437 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 else if (efmp == efm + 1) /* analyse prefix */
439 {
440 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
441 fmt_ptr->flags = *efmp++;
442 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
443 fmt_ptr->prefix = *efmp;
444 else
445 {
446 sprintf((char *)errmsg,
447 _("E376: Invalid %%%c in format string prefix"), *efmp);
448 EMSG(errmsg);
449 goto error2;
450 }
451 }
452 else
453 {
454 sprintf((char *)errmsg,
455 _("E377: Invalid %%%c in format string"), *efmp);
456 EMSG(errmsg);
457 goto error2;
458 }
459 }
460 else /* copy normal character */
461 {
462 if (*efmp == '\\' && efmp + 1 < efm + len)
463 ++efmp;
464 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
465 *ptr++ = '\\'; /* escape regexp atoms */
466 if (*efmp)
467 *ptr++ = *efmp;
468 }
469 }
470 *ptr++ = '$';
471 *ptr = NUL;
472 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
473 goto error2;
474 /*
475 * Advance to next part
476 */
477 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
478 }
479 if (fmt_first == NULL) /* nothing found */
480 {
481 EMSG(_("E378: 'errorformat' contains no pattern"));
482 goto error2;
483 }
484
485 /*
486 * got_int is reset here, because it was probably set when killing the
487 * ":make" command, but we still want to read the errorfile then.
488 */
489 got_int = FALSE;
490
491 /* Always ignore case when looking for a matching error. */
492 regmatch.rm_ic = TRUE;
493
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000494 if (tv != NULL)
495 {
496 if (tv->v_type == VAR_STRING)
497 p_str = tv->vval.v_string;
498 else if (tv->v_type == VAR_LIST)
499 p_li = tv->vval.v_list->lv_first;
500 }
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 /*
503 * Read the lines in the error file one by one.
504 * Try to recognize one of the error formats in each line.
505 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000506 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000508 /* Get the next line. */
509 if (fd == NULL)
510 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000511 if (tv != NULL)
512 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000513 if (tv->v_type == VAR_STRING)
514 {
515 /* Get the next line from the supplied string */
516 char_u *p;
517
518 if (!*p_str) /* Reached the end of the string */
519 break;
520
521 p = vim_strchr(p_str, '\n');
522 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000523 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000524 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000525 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000526
527 if (len > CMDBUFFSIZE - 2)
528 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
529 else
530 vim_strncpy(IObuff, p_str, len);
531
532 p_str += len;
533 }
534 else if (tv->v_type == VAR_LIST)
535 {
536 /* Get the next line from the supplied list */
537 while (p_li && p_li->li_tv.v_type != VAR_STRING)
538 p_li = p_li->li_next; /* Skip non-string items */
539
540 if (!p_li) /* End of the list */
541 break;
542
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000543 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000544 if (len > CMDBUFFSIZE - 2)
545 len = CMDBUFFSIZE - 2;
546
547 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
548
549 p_li = p_li->li_next; /* next item */
550 }
551 }
552 else
553 {
554 /* Get the next line from the supplied buffer */
555 if (buflnum > lnumlast)
556 break;
557 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
558 CMDBUFFSIZE - 2);
559 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000560 }
561 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
562 break;
563
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200565#ifdef FEAT_MBYTE
566 remove_bom(IObuff);
567#endif
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
570 *efmp = NUL;
571#ifdef USE_CRNL
572 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
573 *efmp = NUL;
574#endif
575
Bram Moolenaar01265852006-03-20 21:50:15 +0000576 /* If there was no %> item start at the first pattern */
577 if (fmt_start == NULL)
578 fmt_ptr = fmt_first;
579 else
580 {
581 fmt_ptr = fmt_start;
582 fmt_start = NULL;
583 }
584
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 /*
586 * Try to match each part of 'errorformat' until we find a complete
587 * match or no match.
588 */
589 valid = TRUE;
590restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000591 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {
593 idx = fmt_ptr->prefix;
594 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
595 continue;
596 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000597 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (!multiscan)
599 errmsg[0] = NUL;
600 lnum = 0;
601 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000602 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 enr = -1;
604 type = 0;
605 tail = NULL;
606
607 regmatch.regprog = fmt_ptr->prog;
608 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
609 {
610 if ((idx == 'C' || idx == 'Z') && !multiline)
611 continue;
612 if (vim_strchr((char_u *)"EWI", idx) != NULL)
613 type = idx;
614 else
615 type = 0;
616 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000617 * Extract error message data from matched line.
618 * We check for an actual submatch, because "\[" and "\]" in
619 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 */
621 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
622 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000623 int c;
624
625 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
626 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000627
628 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000629 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000630 *regmatch.endp[i] = NUL;
631 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
632 *regmatch.endp[i] = c;
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000635 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 continue;
637 }
638 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000639 {
640 if (regmatch.startp[i] == NULL)
641 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000645 {
646 if (regmatch.startp[i] == NULL)
647 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000651 {
652 if (regmatch.startp[i] == NULL)
653 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000657 {
658 if (regmatch.startp[i] == NULL)
659 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000661 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000662 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 STRCPY(errmsg, IObuff);
664 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
665 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000666 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
667 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000669 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000672 {
673 if (regmatch.startp[i] == NULL)
674 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
678 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000679 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
680 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
682 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000683 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
686 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000687 if (regmatch.startp[i] == NULL)
688 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000690 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000692 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
693 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000694 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
695 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000696 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
697 if (len > CMDBUFFSIZE - 5)
698 len = CMDBUFFSIZE - 5;
699 STRCPY(pattern, "^\\V");
700 STRNCAT(pattern, regmatch.startp[i], len);
701 pattern[len + 3] = '\\';
702 pattern[len + 4] = '$';
703 pattern[len + 5] = NUL;
704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 break;
706 }
707 }
708 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000709
Bram Moolenaar4770d092006-01-12 23:22:24 +0000710 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000712 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 if (idx == 'D') /* enter directory */
715 {
716 if (*namebuf == NUL)
717 {
718 EMSG(_("E379: Missing or empty directory name"));
719 goto error2;
720 }
721 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
722 goto error2;
723 }
724 else if (idx == 'X') /* leave directory */
725 directory = qf_pop_dir(&dir_stack);
726 }
727 namebuf[0] = NUL; /* no match found, remove file name */
728 lnum = 0; /* don't jump to this line */
729 valid = FALSE;
730 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000731 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 multiline = multiignore = FALSE;
733 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000734 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000736 /* honor %> item */
737 if (fmt_ptr->conthere)
738 fmt_start = fmt_ptr;
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
741 multiline = TRUE; /* start of a multi-line message */
742 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
743 { /* continuation of multi-line msg */
744 if (qfprev == NULL)
745 goto error2;
746 if (*errmsg && !multiignore)
747 {
748 len = (int)STRLEN(qfprev->qf_text);
749 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
750 == NULL)
751 goto error2;
752 STRCPY(ptr, qfprev->qf_text);
753 vim_free(qfprev->qf_text);
754 qfprev->qf_text = ptr;
755 *(ptr += len) = '\n';
756 STRCPY(++ptr, errmsg);
757 }
758 if (qfprev->qf_nr == -1)
759 qfprev->qf_nr = enr;
760 if (vim_isprintc(type) && !qfprev->qf_type)
761 qfprev->qf_type = type; /* only printable chars allowed */
762 if (!qfprev->qf_lnum)
763 qfprev->qf_lnum = lnum;
764 if (!qfprev->qf_col)
765 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (!qfprev->qf_fnum)
768 qfprev->qf_fnum = qf_get_fnum(directory,
769 *namebuf || directory ? namebuf
770 : currfile && valid ? currfile : 0);
771 if (idx == 'Z')
772 multiline = multiignore = FALSE;
773 line_breakcheck();
774 continue;
775 }
776 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
777 {
778 /* global file names */
779 valid = FALSE;
780 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
781 {
782 if (*namebuf && idx == 'P')
783 currfile = qf_push_dir(namebuf, &file_stack);
784 else if (idx == 'Q')
785 currfile = qf_pop_dir(&file_stack);
786 *namebuf = NUL;
787 if (tail && *tail)
788 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000789 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 multiscan = TRUE;
791 goto restofline;
792 }
793 }
794 }
795 if (fmt_ptr->flags == '-') /* generally exclude this line */
796 {
797 if (multiline)
798 multiignore = TRUE; /* also exclude continuation lines */
799 continue;
800 }
801 }
802
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000803 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000805 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000807 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000808 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 errmsg,
810 lnum,
811 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000813 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 enr,
815 type,
816 valid) == FAIL)
817 goto error2;
818 line_breakcheck();
819 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000820 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000822 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000824 /* no valid entry found */
825 qi->qf_lists[qi->qf_curlist].qf_ptr =
826 qi->qf_lists[qi->qf_curlist].qf_start;
827 qi->qf_lists[qi->qf_curlist].qf_index = 1;
828 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 }
830 else
831 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000832 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
833 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
834 qi->qf_lists[qi->qf_curlist].qf_ptr =
835 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000837 /* return number of matches */
838 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 goto qf_init_ok;
840 }
841 EMSG(_(e_readerrf));
842error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000843 qf_free(qi, qi->qf_curlist);
844 qi->qf_listcount--;
845 if (qi->qf_curlist > 0)
846 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000848 if (fd != NULL)
849 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
851 {
852 fmt_first = fmt_ptr->next;
853 vim_free(fmt_ptr->prog);
854 vim_free(fmt_ptr);
855 }
856 qf_clean_dir_stack(&dir_stack);
857 qf_clean_dir_stack(&file_stack);
858qf_init_end:
859 vim_free(namebuf);
860 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000861 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 vim_free(fmtstr);
863
864#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000865 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#endif
867
868 return retval;
869}
870
871/*
872 * Prepare for adding a new quickfix list.
873 */
874 static void
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200875qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000876 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200877 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 int i;
880
881 /*
882 * If the current entry is not the last entry, delete entries below
883 * the current entry. This makes it possible to browse in a tree-like
884 * way with ":grep'.
885 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000886 while (qi->qf_listcount > qi->qf_curlist + 1)
887 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 /*
890 * When the stack is full, remove to oldest entry
891 * Otherwise, add a new entry.
892 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000893 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000895 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000897 qi->qf_lists[i - 1] = qi->qf_lists[i];
898 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000901 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100902 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200903 if (qf_title != NULL)
904 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200905 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200906
907 qi->qf_lists[qi->qf_curlist].qf_title = p;
908 if (p != NULL)
909 sprintf((char *)p, ":%s", (char *)qf_title);
910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911}
912
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000913/*
914 * Free a location list
915 */
916 static void
917ll_free_all(pqi)
918 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000919{
920 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000921 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000922
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000923 qi = *pqi;
924 if (qi == NULL)
925 return;
926 *pqi = NULL; /* Remove reference to this list */
927
928 qi->qf_refcount--;
929 if (qi->qf_refcount < 1)
930 {
931 /* No references to this location list */
932 for (i = 0; i < qi->qf_listcount; ++i)
933 qf_free(qi, i);
934 vim_free(qi);
935 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000936}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000937
938 void
939qf_free_all(wp)
940 win_T *wp;
941{
942 int i;
943 qf_info_T *qi = &ql_info;
944
945 if (wp != NULL)
946 {
947 /* location list */
948 ll_free_all(&wp->w_llist);
949 ll_free_all(&wp->w_llist_ref);
950 }
951 else
952 /* quickfix list */
953 for (i = 0; i < qi->qf_listcount; ++i)
954 qf_free(qi, i);
955}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957/*
958 * Add an entry to the end of the list of errors.
959 * Returns OK or FAIL.
960 */
961 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000962qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
963 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000964 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000965 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 char_u *dir; /* optional directory name */
967 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000968 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 char_u *mesg; /* message */
970 long lnum; /* line number */
971 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000972 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000973 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 int nr; /* error number */
975 int type; /* type character */
976 int valid; /* valid entry */
977{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000980 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000982 if (bufnum != 0)
983 qfp->qf_fnum = bufnum;
984 else
985 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
987 {
988 vim_free(qfp);
989 return FAIL;
990 }
991 qfp->qf_lnum = lnum;
992 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000993 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 if (pattern == NULL || *pattern == NUL)
995 qfp->qf_pattern = NULL;
996 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
997 {
998 vim_free(qfp->qf_text);
999 vim_free(qfp);
1000 return FAIL;
1001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 qfp->qf_nr = nr;
1003 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1004 type = 0;
1005 qfp->qf_type = type;
1006 qfp->qf_valid = valid;
1007
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001008 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1009 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001011 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 qfp->qf_prev = qfp; /* first element points to itself */
1013 }
1014 else
1015 {
1016 qfp->qf_prev = *prevp;
1017 (*prevp)->qf_next = qfp;
1018 }
1019 qfp->qf_next = qfp; /* last element points to itself */
1020 qfp->qf_cleared = FALSE;
1021 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001022 ++qi->qf_lists[qi->qf_curlist].qf_count;
1023 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1024 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001026 qi->qf_lists[qi->qf_curlist].qf_index =
1027 qi->qf_lists[qi->qf_curlist].qf_count;
1028 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001035 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001036 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001037 static qf_info_T *
1038ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001039{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001040 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001041
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001042 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1043 if (qi != NULL)
1044 {
1045 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1046 qi->qf_refcount++;
1047 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001048
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001049 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001050}
1051
1052/*
1053 * Return the location list for window 'wp'.
1054 * If not present, allocate a location list
1055 */
1056 static qf_info_T *
1057ll_get_or_alloc_list(wp)
1058 win_T *wp;
1059{
1060 if (IS_LL_WINDOW(wp))
1061 /* For a location list window, use the referenced location list */
1062 return wp->w_llist_ref;
1063
1064 /*
1065 * For a non-location list window, w_llist_ref should not point to a
1066 * location list.
1067 */
1068 ll_free_all(&wp->w_llist_ref);
1069
1070 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001071 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001072 return wp->w_llist;
1073}
1074
1075/*
1076 * Copy the location list from window "from" to window "to".
1077 */
1078 void
1079copy_loclist(from, to)
1080 win_T *from;
1081 win_T *to;
1082{
1083 qf_info_T *qi;
1084 int idx;
1085 int i;
1086
1087 /*
1088 * When copying from a location list window, copy the referenced
1089 * location list. For other windows, copy the location list for
1090 * that window.
1091 */
1092 if (IS_LL_WINDOW(from))
1093 qi = from->w_llist_ref;
1094 else
1095 qi = from->w_llist;
1096
1097 if (qi == NULL) /* no location list to copy */
1098 return;
1099
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001100 /* allocate a new location list */
1101 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001102 return;
1103
1104 to->w_llist->qf_listcount = qi->qf_listcount;
1105
1106 /* Copy the location lists one at a time */
1107 for (idx = 0; idx < qi->qf_listcount; idx++)
1108 {
1109 qf_list_T *from_qfl;
1110 qf_list_T *to_qfl;
1111
1112 to->w_llist->qf_curlist = idx;
1113
1114 from_qfl = &qi->qf_lists[idx];
1115 to_qfl = &to->w_llist->qf_lists[idx];
1116
1117 /* Some of the fields are populated by qf_add_entry() */
1118 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1119 to_qfl->qf_count = 0;
1120 to_qfl->qf_index = 0;
1121 to_qfl->qf_start = NULL;
1122 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001123 if (from_qfl->qf_title != NULL)
1124 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1125 else
1126 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001127
1128 if (from_qfl->qf_count)
1129 {
1130 qfline_T *from_qfp;
1131 qfline_T *prevp = NULL;
1132
1133 /* copy all the location entries in this list */
1134 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1135 ++i, from_qfp = from_qfp->qf_next)
1136 {
1137 if (qf_add_entry(to->w_llist, &prevp,
1138 NULL,
1139 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001140 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001141 from_qfp->qf_text,
1142 from_qfp->qf_lnum,
1143 from_qfp->qf_col,
1144 from_qfp->qf_viscol,
1145 from_qfp->qf_pattern,
1146 from_qfp->qf_nr,
1147 0,
1148 from_qfp->qf_valid) == FAIL)
1149 {
1150 qf_free_all(to);
1151 return;
1152 }
1153 /*
1154 * qf_add_entry() will not set the qf_num field, as the
1155 * directory and file names are not supplied. So the qf_fnum
1156 * field is copied here.
1157 */
1158 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1159 prevp->qf_type = from_qfp->qf_type; /* error type */
1160 if (from_qfl->qf_ptr == from_qfp)
1161 to_qfl->qf_ptr = prevp; /* current location */
1162 }
1163 }
1164
1165 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1166
1167 /* When no valid entries are present in the list, qf_ptr points to
1168 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001169 if (to_qfl->qf_nonevalid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001170 to_qfl->qf_ptr = to_qfl->qf_start;
1171 }
1172
1173 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1174}
1175
1176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 * get buffer number for file "dir.name"
1178 */
1179 static int
1180qf_get_fnum(directory, fname)
1181 char_u *directory;
1182 char_u *fname;
1183{
1184 if (fname == NULL || *fname == NUL) /* no file name */
1185 return 0;
1186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 char_u *ptr;
1188 int fnum;
1189
Bram Moolenaare60acc12011-05-10 16:41:25 +02001190#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001192#endif
1193#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (directory != NULL)
1195 slash_adjust(directory);
1196 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (directory != NULL && !vim_isAbsName(fname)
1199 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1200 {
1201 /*
1202 * Here we check if the file really exists.
1203 * This should normally be true, but if make works without
1204 * "leaving directory"-messages we might have missed a
1205 * directory change.
1206 */
1207 if (mch_getperm(ptr) < 0)
1208 {
1209 vim_free(ptr);
1210 directory = qf_guess_filepath(fname);
1211 if (directory)
1212 ptr = concat_fnames(directory, fname, TRUE);
1213 else
1214 ptr = vim_strsave(fname);
1215 }
1216 /* Use concatenated directory name and file name */
1217 fnum = buflist_add(ptr, 0);
1218 vim_free(ptr);
1219 return fnum;
1220 }
1221 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223}
1224
1225/*
1226 * push dirbuf onto the directory stack and return pointer to actual dir or
1227 * NULL on error
1228 */
1229 static char_u *
1230qf_push_dir(dirbuf, stackptr)
1231 char_u *dirbuf;
1232 struct dir_stack_T **stackptr;
1233{
1234 struct dir_stack_T *ds_new;
1235 struct dir_stack_T *ds_ptr;
1236
1237 /* allocate new stack element and hook it in */
1238 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1239 if (ds_new == NULL)
1240 return NULL;
1241
1242 ds_new->next = *stackptr;
1243 *stackptr = ds_new;
1244
1245 /* store directory on the stack */
1246 if (vim_isAbsName(dirbuf)
1247 || (*stackptr)->next == NULL
1248 || (*stackptr && dir_stack != *stackptr))
1249 (*stackptr)->dirname = vim_strsave(dirbuf);
1250 else
1251 {
1252 /* Okay we don't have an absolute path.
1253 * dirbuf must be a subdir of one of the directories on the stack.
1254 * Let's search...
1255 */
1256 ds_new = (*stackptr)->next;
1257 (*stackptr)->dirname = NULL;
1258 while (ds_new)
1259 {
1260 vim_free((*stackptr)->dirname);
1261 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1262 TRUE);
1263 if (mch_isdir((*stackptr)->dirname) == TRUE)
1264 break;
1265
1266 ds_new = ds_new->next;
1267 }
1268
1269 /* clean up all dirs we already left */
1270 while ((*stackptr)->next != ds_new)
1271 {
1272 ds_ptr = (*stackptr)->next;
1273 (*stackptr)->next = (*stackptr)->next->next;
1274 vim_free(ds_ptr->dirname);
1275 vim_free(ds_ptr);
1276 }
1277
1278 /* Nothing found -> it must be on top level */
1279 if (ds_new == NULL)
1280 {
1281 vim_free((*stackptr)->dirname);
1282 (*stackptr)->dirname = vim_strsave(dirbuf);
1283 }
1284 }
1285
1286 if ((*stackptr)->dirname != NULL)
1287 return (*stackptr)->dirname;
1288 else
1289 {
1290 ds_ptr = *stackptr;
1291 *stackptr = (*stackptr)->next;
1292 vim_free(ds_ptr);
1293 return NULL;
1294 }
1295}
1296
1297
1298/*
1299 * pop dirbuf from the directory stack and return previous directory or NULL if
1300 * stack is empty
1301 */
1302 static char_u *
1303qf_pop_dir(stackptr)
1304 struct dir_stack_T **stackptr;
1305{
1306 struct dir_stack_T *ds_ptr;
1307
1308 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1309 * What to do if it isn't? */
1310
1311 /* pop top element and free it */
1312 if (*stackptr != NULL)
1313 {
1314 ds_ptr = *stackptr;
1315 *stackptr = (*stackptr)->next;
1316 vim_free(ds_ptr->dirname);
1317 vim_free(ds_ptr);
1318 }
1319
1320 /* return NEW top element as current dir or NULL if stack is empty*/
1321 return *stackptr ? (*stackptr)->dirname : NULL;
1322}
1323
1324/*
1325 * clean up directory stack
1326 */
1327 static void
1328qf_clean_dir_stack(stackptr)
1329 struct dir_stack_T **stackptr;
1330{
1331 struct dir_stack_T *ds_ptr;
1332
1333 while ((ds_ptr = *stackptr) != NULL)
1334 {
1335 *stackptr = (*stackptr)->next;
1336 vim_free(ds_ptr->dirname);
1337 vim_free(ds_ptr);
1338 }
1339}
1340
1341/*
1342 * Check in which directory of the directory stack the given file can be
1343 * found.
1344 * Returns a pointer to the directory name or NULL if not found
1345 * Cleans up intermediate directory entries.
1346 *
1347 * TODO: How to solve the following problem?
1348 * If we have the this directory tree:
1349 * ./
1350 * ./aa
1351 * ./aa/bb
1352 * ./bb
1353 * ./bb/x.c
1354 * and make says:
1355 * making all in aa
1356 * making all in bb
1357 * x.c:9: Error
1358 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1359 * qf_guess_filepath will return NULL.
1360 */
1361 static char_u *
1362qf_guess_filepath(filename)
1363 char_u *filename;
1364{
1365 struct dir_stack_T *ds_ptr;
1366 struct dir_stack_T *ds_tmp;
1367 char_u *fullname;
1368
1369 /* no dirs on the stack - there's nothing we can do */
1370 if (dir_stack == NULL)
1371 return NULL;
1372
1373 ds_ptr = dir_stack->next;
1374 fullname = NULL;
1375 while (ds_ptr)
1376 {
1377 vim_free(fullname);
1378 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1379
1380 /* If concat_fnames failed, just go on. The worst thing that can happen
1381 * is that we delete the entire stack.
1382 */
1383 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1384 break;
1385
1386 ds_ptr = ds_ptr->next;
1387 }
1388
1389 vim_free(fullname);
1390
1391 /* clean up all dirs we already left */
1392 while (dir_stack->next != ds_ptr)
1393 {
1394 ds_tmp = dir_stack->next;
1395 dir_stack->next = dir_stack->next->next;
1396 vim_free(ds_tmp->dirname);
1397 vim_free(ds_tmp);
1398 }
1399
1400 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1401
1402}
1403
1404/*
1405 * jump to a quickfix line
1406 * if dir == FORWARD go "errornr" valid entries forward
1407 * if dir == BACKWARD go "errornr" valid entries backward
1408 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1409 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1410 * else if "errornr" is zero, redisplay the same line
1411 * else go to entry "errornr"
1412 */
1413 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001414qf_jump(qi, dir, errornr, forceit)
1415 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 int dir;
1417 int errornr;
1418 int forceit;
1419{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001420 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001421 qfline_T *qf_ptr;
1422 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 int qf_index;
1424 int old_qf_fnum;
1425 int old_qf_index;
1426 int prev_index;
1427 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1428 char_u *err = e_no_more_items;
1429 linenr_T i;
1430 buf_T *old_curbuf;
1431 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 colnr_T screen_col;
1433 colnr_T char_col;
1434 char_u *line;
1435#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001436 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001437 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 int opened_window = FALSE;
1439 win_T *win;
1440 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001441 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001443 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 int print_message = TRUE;
1445 int len;
1446#ifdef FEAT_FOLDING
1447 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1448#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001449 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001450 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001452 if (qi == NULL)
1453 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001454
1455 if (qi->qf_curlist >= qi->qf_listcount
1456 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
1458 EMSG(_(e_quickfix));
1459 return;
1460 }
1461
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001462 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001464 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 old_qf_index = qf_index;
1466 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1467 {
1468 while (errornr--)
1469 {
1470 old_qf_ptr = qf_ptr;
1471 prev_index = qf_index;
1472 old_qf_fnum = qf_ptr->qf_fnum;
1473 do
1474 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001475 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 || qf_ptr->qf_next == NULL)
1477 {
1478 qf_ptr = old_qf_ptr;
1479 qf_index = prev_index;
1480 if (err != NULL)
1481 {
1482 EMSG(_(err));
1483 goto theend;
1484 }
1485 errornr = 0;
1486 break;
1487 }
1488 ++qf_index;
1489 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001490 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1491 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1493 err = NULL;
1494 }
1495 }
1496 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1497 {
1498 while (errornr--)
1499 {
1500 old_qf_ptr = qf_ptr;
1501 prev_index = qf_index;
1502 old_qf_fnum = qf_ptr->qf_fnum;
1503 do
1504 {
1505 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1506 {
1507 qf_ptr = old_qf_ptr;
1508 qf_index = prev_index;
1509 if (err != NULL)
1510 {
1511 EMSG(_(err));
1512 goto theend;
1513 }
1514 errornr = 0;
1515 break;
1516 }
1517 --qf_index;
1518 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001519 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1520 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1522 err = NULL;
1523 }
1524 }
1525 else if (errornr != 0) /* go to specified number */
1526 {
1527 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1528 {
1529 --qf_index;
1530 qf_ptr = qf_ptr->qf_prev;
1531 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532 while (errornr > qf_index && qf_index <
1533 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 && qf_ptr->qf_next != NULL)
1535 {
1536 ++qf_index;
1537 qf_ptr = qf_ptr->qf_next;
1538 }
1539 }
1540
1541#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001542 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1543 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 /* No need to print the error message if it's visible in the error
1545 * window */
1546 print_message = FALSE;
1547
1548 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001549 * For ":helpgrep" find a help window or open one.
1550 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001551 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001552 {
1553 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001554
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001555 if (cmdmod.tab != 0)
1556 wp = NULL;
1557 else
1558 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1559 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1560 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001561 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1562 win_enter(wp, TRUE);
1563 else
1564 {
1565 /*
1566 * Split off help window; put it at far top if no position
1567 * specified, the current window is vertically split and narrow.
1568 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001569 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001570# ifdef FEAT_VERTSPLIT
1571 if (cmdmod.split == 0 && curwin->w_width != Columns
1572 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001573 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001574# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001575 if (qi != &ql_info)
1576 flags |= WSP_NEWLOC; /* don't copy the location list */
1577
1578 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001579 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001580 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001581
1582 if (curwin->w_height < p_hh)
1583 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001584
1585 if (qi != &ql_info) /* not a quickfix list */
1586 {
1587 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001588 curwin->w_llist = qi;
1589 qi->qf_refcount++;
1590 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001591 }
1592
1593 if (!p_im)
1594 restart_edit = 0; /* don't want insert mode in help file */
1595 }
1596
1597 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 * If currently in the quickfix window, find another window to show the
1599 * file in.
1600 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001601 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 /*
1604 * If there is no file specified, we don't know where to go.
1605 * But do advance, otherwise ":cn" gets stuck.
1606 */
1607 if (qf_ptr->qf_fnum == 0)
1608 goto theend;
1609
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001610 /* Locate a window showing a normal buffer */
1611 usable_win = 0;
1612 FOR_ALL_WINDOWS(win)
1613 if (win->w_buffer->b_p_bt[0] == NUL)
1614 {
1615 usable_win = 1;
1616 break;
1617 }
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001620 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001621 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001623 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001624 {
1625 tabpage_T *tp;
1626 win_T *wp;
1627
1628 FOR_ALL_TAB_WINDOWS(tp, wp)
1629 {
1630 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1631 {
1632 goto_tabpage_win(tp, wp);
1633 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001634 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001635 }
1636 }
1637 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001638win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001639
1640 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001641 * If there is only one window and it is the quickfix window, create a
1642 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001643 */
1644 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001646 ll_ref = curwin->w_llist_ref;
1647
Bram Moolenaar884ae642009-02-22 01:37:59 +00001648 flags = WSP_ABOVE;
1649 if (ll_ref != NULL)
1650 flags |= WSP_NEWLOC;
1651 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 goto failed; /* not enough room for window */
1653 opened_window = TRUE; /* close it when fail */
1654 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001655 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001656 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001657 if (ll_ref != NULL)
1658 {
1659 /* The new window should use the location list from the
1660 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001661 curwin->w_llist = ll_ref;
1662 ll_ref->qf_refcount++;
1663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
1665 else
1666 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001667 if (curwin->w_llist_ref != NULL)
1668 {
1669 /* In a location window */
1670 ll_ref = curwin->w_llist_ref;
1671
1672 /* Find the window with the same location list */
1673 FOR_ALL_WINDOWS(win)
1674 if (win->w_llist == ll_ref)
1675 break;
1676 if (win == NULL)
1677 {
1678 /* Find the window showing the selected file */
1679 FOR_ALL_WINDOWS(win)
1680 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1681 break;
1682 if (win == NULL)
1683 {
1684 /* Find a previous usable window */
1685 win = curwin;
1686 do
1687 {
1688 if (win->w_buffer->b_p_bt[0] == NUL)
1689 break;
1690 if (win->w_prev == NULL)
1691 win = lastwin; /* wrap around the top */
1692 else
1693 win = win->w_prev; /* go to previous window */
1694 } while (win != curwin);
1695 }
1696 }
1697 win_goto(win);
1698
1699 /* If the location list for the window is not set, then set it
1700 * to the location list from the location window */
1701 if (win->w_llist == NULL)
1702 {
1703 win->w_llist = ll_ref;
1704 ll_ref->qf_refcount++;
1705 }
1706 }
1707 else
1708 {
1709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 /*
1711 * Try to find a window that shows the right buffer.
1712 * Default to the window just above the quickfix buffer.
1713 */
1714 win = curwin;
1715 altwin = NULL;
1716 for (;;)
1717 {
1718 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1719 break;
1720 if (win->w_prev == NULL)
1721 win = lastwin; /* wrap around the top */
1722 else
1723 win = win->w_prev; /* go to previous window */
1724
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001725 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
1727 /* Didn't find it, go to the window before the quickfix
1728 * window. */
1729 if (altwin != NULL)
1730 win = altwin;
1731 else if (curwin->w_prev != NULL)
1732 win = curwin->w_prev;
1733 else
1734 win = curwin->w_next;
1735 break;
1736 }
1737
1738 /* Remember a usable window. */
1739 if (altwin == NULL && !win->w_p_pvw
1740 && win->w_buffer->b_p_bt[0] == NUL)
1741 altwin = win;
1742 }
1743
1744 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
1747 }
1748#endif
1749
1750 /*
1751 * If there is a file name,
1752 * read the wanted file if needed, and check autowrite etc.
1753 */
1754 old_curbuf = curbuf;
1755 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001756
1757 if (qf_ptr->qf_fnum != 0)
1758 {
1759 if (qf_ptr->qf_type == 1)
1760 {
1761 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1762 * set b_p_ro flag). */
1763 if (!can_abandon(curbuf, forceit))
1764 {
1765 EMSG(_(e_nowrtmsg));
1766 ok = FALSE;
1767 }
1768 else
1769 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001770 ECMD_HIDE + ECMD_SET_HELP,
1771 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001772 }
1773 else
1774 ok = buflist_getfile(qf_ptr->qf_fnum,
1775 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1776 }
1777
1778 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
1780 /* When not switched to another buffer, still need to set pc mark */
1781 if (curbuf == old_curbuf)
1782 setpcmark();
1783
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001784 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001786 /*
1787 * Go to line with error, unless qf_lnum is 0.
1788 */
1789 i = qf_ptr->qf_lnum;
1790 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001792 if (i > curbuf->b_ml.ml_line_count)
1793 i = curbuf->b_ml.ml_line_count;
1794 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001796 if (qf_ptr->qf_col > 0)
1797 {
1798 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1799 if (qf_ptr->qf_viscol == TRUE)
1800 {
1801 /*
1802 * Check each character from the beginning of the error
1803 * line up to the error column. For each tab character
1804 * found, reduce the error column value by the length of
1805 * a tab character.
1806 */
1807 line = ml_get_curline();
1808 screen_col = 0;
1809 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1810 {
1811 if (*line == NUL)
1812 break;
1813 if (*line++ == '\t')
1814 {
1815 curwin->w_cursor.col -= 7 - (screen_col % 8);
1816 screen_col += 8 - (screen_col % 8);
1817 }
1818 else
1819 ++screen_col;
1820 }
1821 }
1822 check_cursor();
1823 }
1824 else
1825 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001828 {
1829 pos_T save_cursor;
1830
1831 /* Move the cursor to the first line in the buffer */
1832 save_cursor = curwin->w_cursor;
1833 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001834 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1835 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001836 curwin->w_cursor = save_cursor;
1837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839#ifdef FEAT_FOLDING
1840 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1841 foldOpenCursor();
1842#endif
1843 if (print_message)
1844 {
1845 /* Update the screen before showing the message */
1846 update_topline_redraw();
1847 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001848 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1850 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1851 /* Add the message, skipping leading whitespace and newlines. */
1852 len = (int)STRLEN(IObuff);
1853 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1854
1855 /* Output the message. Overwrite to avoid scrolling when the 'O'
1856 * flag is present in 'shortmess'; But when not jumping, print the
1857 * whole message. */
1858 i = msg_scroll;
1859 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1860 msg_scroll = TRUE;
1861 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1862 msg_scroll = FALSE;
1863 msg_attr_keep(IObuff, 0, TRUE);
1864 msg_scroll = i;
1865 }
1866 }
1867 else
1868 {
1869#ifdef FEAT_WINDOWS
1870 if (opened_window)
1871 win_close(curwin, TRUE); /* Close opened window */
1872#endif
1873 if (qf_ptr->qf_fnum != 0)
1874 {
1875 /*
1876 * Couldn't open file, so put index back where it was. This could
1877 * happen if the file was readonly and we changed something.
1878 */
1879#ifdef FEAT_WINDOWS
1880failed:
1881#endif
1882 qf_ptr = old_qf_ptr;
1883 qf_index = old_qf_index;
1884 }
1885 }
1886theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001887 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1888 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889#ifdef FEAT_WINDOWS
1890 if (p_swb != old_swb && opened_window)
1891 {
1892 /* Restore old 'switchbuf' value, but not when an autocommand or
1893 * modeline has changed the value. */
1894 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001897 swb_flags = old_swb_flags;
1898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 else
1900 free_string_option(old_swb);
1901 }
1902#endif
1903}
1904
1905/*
1906 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001907 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 */
1909 void
1910qf_list(eap)
1911 exarg_T *eap;
1912{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001913 buf_T *buf;
1914 char_u *fname;
1915 qfline_T *qfp;
1916 int i;
1917 int idx1 = 1;
1918 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001919 char_u *arg = eap->arg;
1920 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001922 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001924 if (eap->cmdidx == CMD_llist)
1925 {
1926 qi = GET_LOC_LIST(curwin);
1927 if (qi == NULL)
1928 {
1929 EMSG(_(e_loclist));
1930 return;
1931 }
1932 }
1933
1934 if (qi->qf_curlist >= qi->qf_listcount
1935 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
1937 EMSG(_(e_quickfix));
1938 return;
1939 }
1940 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1941 {
1942 EMSG(_(e_trailing));
1943 return;
1944 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001945 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (idx1 < 0)
1947 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1948 if (idx2 < 0)
1949 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1950
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001951 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001953 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1954 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1957 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001958 msg_putchar('\n');
1959 if (got_int)
1960 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001961
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001962 fname = NULL;
1963 if (qfp->qf_fnum != 0
1964 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1965 {
1966 fname = buf->b_fname;
1967 if (qfp->qf_type == 1) /* :helpgrep */
1968 fname = gettail(fname);
1969 }
1970 if (fname == NULL)
1971 sprintf((char *)IObuff, "%2d", i);
1972 else
1973 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1974 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001975 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001976 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1977 if (qfp->qf_lnum == 0)
1978 IObuff[0] = NUL;
1979 else if (qfp->qf_col == 0)
1980 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1981 else
1982 sprintf((char *)IObuff, ":%ld col %d",
1983 qfp->qf_lnum, qfp->qf_col);
1984 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1985 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1986 msg_puts_attr(IObuff, hl_attr(HLF_N));
1987 if (qfp->qf_pattern != NULL)
1988 {
1989 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1990 STRCAT(IObuff, ":");
1991 msg_puts(IObuff);
1992 }
1993 msg_puts((char_u *)" ");
1994
1995 /* Remove newlines and leading whitespace from the text. For an
1996 * unrecognized line keep the indent, the compiler may mark a word
1997 * with ^^^^. */
1998 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2000 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002001 msg_prt_line(IObuff, FALSE);
2002 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002004
2005 qfp = qfp->qf_next;
2006 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 ui_breakcheck();
2008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009}
2010
2011/*
2012 * Remove newlines and leading whitespace from an error message.
2013 * Put the result in "buf[bufsize]".
2014 */
2015 static void
2016qf_fmt_text(text, buf, bufsize)
2017 char_u *text;
2018 char_u *buf;
2019 int bufsize;
2020{
2021 int i;
2022 char_u *p = text;
2023
2024 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2025 {
2026 if (*p == '\n')
2027 {
2028 buf[i] = ' ';
2029 while (*++p != NUL)
2030 if (!vim_iswhite(*p) && *p != '\n')
2031 break;
2032 }
2033 else
2034 buf[i] = *p++;
2035 }
2036 buf[i] = NUL;
2037}
2038
2039/*
2040 * ":colder [count]": Up in the quickfix stack.
2041 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002042 * ":lolder [count]": Up in the location list stack.
2043 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 */
2045 void
2046qf_age(eap)
2047 exarg_T *eap;
2048{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002049 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 int count;
2051
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2053 {
2054 qi = GET_LOC_LIST(curwin);
2055 if (qi == NULL)
2056 {
2057 EMSG(_(e_loclist));
2058 return;
2059 }
2060 }
2061
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (eap->addr_count != 0)
2063 count = eap->line2;
2064 else
2065 count = 1;
2066 while (count--)
2067 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002068 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002070 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
2072 EMSG(_("E380: At bottom of quickfix stack"));
2073 return;
2074 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002075 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077 else
2078 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002079 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
2081 EMSG(_("E381: At top of quickfix stack"));
2082 return;
2083 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002084 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002087 qf_msg(qi);
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089}
2090
2091 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092qf_msg(qi)
2093 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
2095 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002096 qi->qf_curlist + 1, qi->qf_listcount,
2097 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002099 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100#endif
2101}
2102
2103/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002104 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 */
2106 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002107qf_free(qi, idx)
2108 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 int idx;
2110{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002111 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 qfp = qi->qf_lists[idx].qf_start->qf_next;
2116 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2117 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2118 vim_free(qi->qf_lists[idx].qf_start);
2119 qi->qf_lists[idx].qf_start = qfp;
2120 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002122 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002123 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124}
2125
2126/*
2127 * qf_mark_adjust: adjust marks
2128 */
2129 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002130qf_mark_adjust(wp, line1, line2, amount, amount_after)
2131 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 linenr_T line1;
2133 linenr_T line2;
2134 long amount;
2135 long amount_after;
2136{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002137 int i;
2138 qfline_T *qfp;
2139 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002140 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002142 if (wp != NULL)
2143 {
2144 if (wp->w_llist == NULL)
2145 return;
2146 qi = wp->w_llist;
2147 }
2148
2149 for (idx = 0; idx < qi->qf_listcount; ++idx)
2150 if (qi->qf_lists[idx].qf_count)
2151 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2152 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (qfp->qf_fnum == curbuf->b_fnum)
2154 {
2155 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2156 {
2157 if (amount == MAXLNUM)
2158 qfp->qf_cleared = TRUE;
2159 else
2160 qfp->qf_lnum += amount;
2161 }
2162 else if (amount_after && qfp->qf_lnum > line2)
2163 qfp->qf_lnum += amount_after;
2164 }
2165}
2166
2167/*
2168 * Make a nice message out of the error character and the error number:
2169 * char number message
2170 * e or E 0 " error"
2171 * w or W 0 " warning"
2172 * i or I 0 " info"
2173 * 0 0 ""
2174 * other 0 " c"
2175 * e or E n " error n"
2176 * w or W n " warning n"
2177 * i or I n " info n"
2178 * 0 n " error n"
2179 * other n " c n"
2180 * 1 x "" :helpgrep
2181 */
2182 static char_u *
2183qf_types(c, nr)
2184 int c, nr;
2185{
2186 static char_u buf[20];
2187 static char_u cc[3];
2188 char_u *p;
2189
2190 if (c == 'W' || c == 'w')
2191 p = (char_u *)" warning";
2192 else if (c == 'I' || c == 'i')
2193 p = (char_u *)" info";
2194 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2195 p = (char_u *)" error";
2196 else if (c == 0 || c == 1)
2197 p = (char_u *)"";
2198 else
2199 {
2200 cc[0] = ' ';
2201 cc[1] = c;
2202 cc[2] = NUL;
2203 p = cc;
2204 }
2205
2206 if (nr <= 0)
2207 return p;
2208
2209 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2210 return buf;
2211}
2212
2213#if defined(FEAT_WINDOWS) || defined(PROTO)
2214/*
2215 * ":cwindow": open the quickfix window if we have errors to display,
2216 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002217 * ":lwindow": open the location list window if we have locations to display,
2218 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 */
2220 void
2221ex_cwindow(eap)
2222 exarg_T *eap;
2223{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002224 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 win_T *win;
2226
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 if (eap->cmdidx == CMD_lwindow)
2228 {
2229 qi = GET_LOC_LIST(curwin);
2230 if (qi == NULL)
2231 return;
2232 }
2233
2234 /* Look for an existing quickfix window. */
2235 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 /*
2238 * If a quickfix window is open but we have no errors to display,
2239 * close the window. If a quickfix window is not open, then open
2240 * it if we have errors; otherwise, leave it closed.
2241 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002242 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002243 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002244 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 if (win != NULL)
2247 ex_cclose(eap);
2248 }
2249 else if (win == NULL)
2250 ex_copen(eap);
2251}
2252
2253/*
2254 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002255 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 void
2258ex_cclose(eap)
2259 exarg_T *eap;
2260{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002261 win_T *win = NULL;
2262 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002264 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2265 {
2266 qi = GET_LOC_LIST(curwin);
2267 if (qi == NULL)
2268 return;
2269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002271 /* Find existing quickfix window and close it. */
2272 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (win != NULL)
2274 win_close(win, FALSE);
2275}
2276
2277/*
2278 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002279 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 */
2281 void
2282ex_copen(eap)
2283 exarg_T *eap;
2284{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002285 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002288 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002289 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002290 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002292 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2293 {
2294 qi = GET_LOC_LIST(curwin);
2295 if (qi == NULL)
2296 {
2297 EMSG(_(e_loclist));
2298 return;
2299 }
2300 }
2301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (eap->addr_count != 0)
2303 height = eap->line2;
2304 else
2305 height = QF_WINHEIGHT;
2306
2307#ifdef FEAT_VISUAL
2308 reset_VIsual_and_resel(); /* stop Visual mode */
2309#endif
2310#ifdef FEAT_GUI
2311 need_mouse_correct = TRUE;
2312#endif
2313
2314 /*
2315 * Find existing quickfix window, or open a new one.
2316 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002317 win = qf_find_win(qi);
2318
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002319 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 win_goto(win);
2321 else
2322 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002323 qf_buf = qf_find_buf(qi);
2324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 /* The current window becomes the previous window afterwards. */
2326 win = curwin;
2327
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002328 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2329 /* Create the new window at the very bottom. */
2330 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002331 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002333 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 /*
2338 * For the location list window, create a reference to the
2339 * location list from the window 'win'.
2340 */
2341 curwin->w_llist_ref = win->w_llist;
2342 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002344
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002345 if (oldwin != curwin)
2346 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002347 if (qf_buf != NULL)
2348 /* Use the existing quickfix buffer */
2349 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002350 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002351 else
2352 {
2353 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002354 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002355 /* switch off 'swapfile' */
2356 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2357 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002358 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002359 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002360 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002361#ifdef FEAT_DIFF
2362 curwin->w_p_diff = FALSE;
2363#endif
2364#ifdef FEAT_FOLDING
2365 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2366 OPT_LOCAL);
2367#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002370 /* Only set the height when still in the same tab page and there is no
2371 * window to the side. */
2372 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002373#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002374 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002375#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002376 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 win_setheight(height);
2378 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2379 if (win_valid(win))
2380 prevwin = win;
2381 }
2382
2383 /*
2384 * Fill the buffer with the quickfix list.
2385 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002386 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002388 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002389 qf_set_title(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002390
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002391 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 curwin->w_cursor.col = 0;
2393 check_cursor();
2394 update_topline(); /* scroll to show the line */
2395}
2396
2397/*
2398 * Return the number of the current entry (line number in the quickfix
2399 * window).
2400 */
2401 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402qf_current_entry(wp)
2403 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405 qf_info_T *qi = &ql_info;
2406
2407 if (IS_LL_WINDOW(wp))
2408 /* In the location list window, use the referenced location list */
2409 qi = wp->w_llist_ref;
2410
2411 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412}
2413
2414/*
2415 * Update the cursor position in the quickfix window to the current error.
2416 * Return TRUE if there is a quickfix window.
2417 */
2418 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002419qf_win_pos_update(qi, old_qf_index)
2420 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 int old_qf_index; /* previous qf_index or zero */
2422{
2423 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002424 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 /*
2427 * Put the cursor on the current error in the quickfix window, so that
2428 * it's viewable.
2429 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002430 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 if (win != NULL
2432 && qf_index <= win->w_buffer->b_ml.ml_line_count
2433 && old_qf_index != qf_index)
2434 {
2435 win_T *old_curwin = curwin;
2436
2437 curwin = win;
2438 curbuf = win->w_buffer;
2439 if (qf_index > old_qf_index)
2440 {
2441 curwin->w_redraw_top = old_qf_index;
2442 curwin->w_redraw_bot = qf_index;
2443 }
2444 else
2445 {
2446 curwin->w_redraw_top = qf_index;
2447 curwin->w_redraw_bot = old_qf_index;
2448 }
2449 curwin->w_cursor.lnum = qf_index;
2450 curwin->w_cursor.col = 0;
2451 update_topline(); /* scroll to show the line */
2452 redraw_later(VALID);
2453 curwin->w_redr_status = TRUE; /* update ruler */
2454 curwin = old_curwin;
2455 curbuf = curwin->w_buffer;
2456 }
2457 return win != NULL;
2458}
2459
2460/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002461 * Check whether the given window is displaying the specified quickfix/location
2462 * list buffer
2463 */
2464 static int
2465is_qf_win(win, qi)
2466 win_T *win;
2467 qf_info_T *qi;
2468{
2469 /*
2470 * A window displaying the quickfix buffer will have the w_llist_ref field
2471 * set to NULL.
2472 * A window displaying a location list buffer will have the w_llist_ref
2473 * pointing to the location list.
2474 */
2475 if (bt_quickfix(win->w_buffer))
2476 if ((qi == &ql_info && win->w_llist_ref == NULL)
2477 || (qi != &ql_info && win->w_llist_ref == qi))
2478 return TRUE;
2479
2480 return FALSE;
2481}
2482
2483/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002484 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002485 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002486 */
2487 static win_T *
2488qf_find_win(qi)
2489 qf_info_T *qi;
2490{
2491 win_T *win;
2492
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002493 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002494 if (is_qf_win(win, qi))
2495 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002496
2497 return win;
2498}
2499
2500/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002501 * Find a quickfix buffer.
2502 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
2504 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002505qf_find_buf(qi)
2506 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002508 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002509 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
Bram Moolenaar9c102382006-05-03 21:26:49 +00002511 FOR_ALL_TAB_WINDOWS(tp, win)
2512 if (is_qf_win(win, qi))
2513 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002514
Bram Moolenaar9c102382006-05-03 21:26:49 +00002515 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516}
2517
2518/*
2519 * Find the quickfix buffer. If it exists, update the contents.
2520 */
2521 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002522qf_update_buffer(qi)
2523 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002526 win_T *win;
2527 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002531 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (buf != NULL)
2533 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 /* set curwin/curbuf to buf and save a few things */
2535 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002537 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002539 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2540 && (win = qf_find_win(qi)) != NULL)
2541 {
2542 curwin_save = curwin;
2543 curwin = win;
2544 qf_set_title(qi);
2545 curwin = curwin_save;
2546
2547 }
2548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 /* restore curwin/curbuf and a few other things */
2550 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002552 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554}
2555
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002556 static void
2557qf_set_title(qi)
2558 qf_info_T *qi;
2559{
2560 set_internal_string_var((char_u *)"w:quickfix_title",
2561 qi->qf_lists[qi->qf_curlist].qf_title);
2562}
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564/*
2565 * Fill current buffer with quickfix errors, replacing any previous contents.
2566 * curbuf must be the quickfix buffer!
2567 */
2568 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002569qf_fill_buffer(qi)
2570 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002572 linenr_T lnum;
2573 qfline_T *qfp;
2574 buf_T *errbuf;
2575 int len;
2576 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
2578 /* delete all existing lines */
2579 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2580 (void)ml_delete((linenr_T)1, FALSE);
2581
2582 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002583 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
2585 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002586 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2587 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
2589 if (qfp->qf_fnum != 0
2590 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2591 && errbuf->b_fname != NULL)
2592 {
2593 if (qfp->qf_type == 1) /* :helpgrep */
2594 STRCPY(IObuff, gettail(errbuf->b_fname));
2595 else
2596 STRCPY(IObuff, errbuf->b_fname);
2597 len = (int)STRLEN(IObuff);
2598 }
2599 else
2600 len = 0;
2601 IObuff[len++] = '|';
2602
2603 if (qfp->qf_lnum > 0)
2604 {
2605 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2606 len += (int)STRLEN(IObuff + len);
2607
2608 if (qfp->qf_col > 0)
2609 {
2610 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2611 len += (int)STRLEN(IObuff + len);
2612 }
2613
2614 sprintf((char *)IObuff + len, "%s",
2615 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2616 len += (int)STRLEN(IObuff + len);
2617 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002618 else if (qfp->qf_pattern != NULL)
2619 {
2620 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2621 len += (int)STRLEN(IObuff + len);
2622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 IObuff[len++] = '|';
2624 IObuff[len++] = ' ';
2625
2626 /* Remove newlines and leading whitespace from the text.
2627 * For an unrecognized line keep the indent, the compiler may
2628 * mark a word with ^^^^. */
2629 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2630 IObuff + len, IOSIZE - len);
2631
2632 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2633 == FAIL)
2634 break;
2635 qfp = qfp->qf_next;
2636 }
2637 /* Delete the empty line which is now at the end */
2638 (void)ml_delete(lnum + 1, FALSE);
2639 }
2640
2641 /* correct cursor position */
2642 check_lnums(TRUE);
2643
2644 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2645 * resembles reading a file into a buffer, it's more logical when using
2646 * autocommands. */
2647 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2648 curbuf->b_p_ma = FALSE;
2649
2650#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002651 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2653 FALSE, curbuf);
2654 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2655 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002656 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658
2659 /* make sure it will be redrawn */
2660 redraw_curbuf_later(NOT_VALID);
2661
2662 /* Restore KeyTyped, setting 'filetype' may reset it. */
2663 KeyTyped = old_KeyTyped;
2664}
2665
2666#endif /* FEAT_WINDOWS */
2667
2668/*
2669 * Return TRUE if "buf" is the quickfix buffer.
2670 */
2671 int
2672bt_quickfix(buf)
2673 buf_T *buf;
2674{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002675 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676}
2677
2678/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002679 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2680 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 */
2682 int
2683bt_nofile(buf)
2684 buf_T *buf;
2685{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002686 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2687 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688}
2689
2690/*
2691 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2692 */
2693 int
2694bt_dontwrite(buf)
2695 buf_T *buf;
2696{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002697 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698}
2699
2700 int
2701bt_dontwrite_msg(buf)
2702 buf_T *buf;
2703{
2704 if (bt_dontwrite(buf))
2705 {
2706 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2707 return TRUE;
2708 }
2709 return FALSE;
2710}
2711
2712/*
2713 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2714 * and 'bufhidden'.
2715 */
2716 int
2717buf_hide(buf)
2718 buf_T *buf;
2719{
2720 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2721 switch (buf->b_p_bh[0])
2722 {
2723 case 'u': /* "unload" */
2724 case 'w': /* "wipe" */
2725 case 'd': return FALSE; /* "delete" */
2726 case 'h': return TRUE; /* "hide" */
2727 }
2728 return (p_hid || cmdmod.hide);
2729}
2730
2731/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002732 * Return TRUE when using ":vimgrep" for ":grep".
2733 */
2734 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002735grep_internal(cmdidx)
2736 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002737{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002738 return ((cmdidx == CMD_grep
2739 || cmdidx == CMD_lgrep
2740 || cmdidx == CMD_grepadd
2741 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002742 && STRCMP("internal",
2743 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2744}
2745
2746/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002747 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 */
2749 void
2750ex_make(eap)
2751 exarg_T *eap;
2752{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002753 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 char_u *cmd;
2755 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002756 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002757 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002758 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002759#ifdef FEAT_AUTOCMD
2760 char_u *au_name = NULL;
2761
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002762 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2763 if (grep_internal(eap->cmdidx))
2764 {
2765 ex_vimgrep(eap);
2766 return;
2767 }
2768
Bram Moolenaar7c626922005-02-07 22:01:03 +00002769 switch (eap->cmdidx)
2770 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002771 case CMD_make: au_name = (char_u *)"make"; break;
2772 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2773 case CMD_grep: au_name = (char_u *)"grep"; break;
2774 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2775 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2776 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002777 default: break;
2778 }
2779 if (au_name != NULL)
2780 {
2781 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2782 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002783# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002784 if (did_throw || force_abort)
2785 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002786# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002787 }
2788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
Bram Moolenaara6557602006-02-04 22:43:20 +00002790 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2791 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002792 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002795 fname = get_mef_name();
2796 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002798 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 /*
2801 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2802 */
2803 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2804 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002805 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 cmd = alloc(len);
2807 if (cmd == NULL)
2808 return;
2809 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2810 (char *)p_shq);
2811 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002812 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 /*
2814 * Output a newline if there's something else than the :make command that
2815 * was typed (in which case the cursor is in column 0).
2816 */
2817 if (msg_col == 0)
2818 msg_didout = FALSE;
2819 msg_start();
2820 MSG_PUTS(":!");
2821 msg_outtrans(cmd); /* show what we are doing */
2822
2823 /* let the shell know if we are redirecting output or not */
2824 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2825
2826#ifdef AMIGA
2827 out_flush();
2828 /* read window status report and redraw before message */
2829 (void)char_avail();
2830#endif
2831
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002832 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002833 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2834 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002835 && eap->cmdidx != CMD_lgrepadd),
2836 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002837 if (wp != NULL)
2838 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002839#ifdef FEAT_AUTOCMD
2840 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002841 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002842 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2843 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002844 if (qi->qf_curlist < qi->qf_listcount)
2845 res = qi->qf_lists[qi->qf_curlist].qf_count;
2846 else
2847 res = 0;
2848 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002849#endif
2850 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002851 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
Bram Moolenaar7c626922005-02-07 22:01:03 +00002853 mch_remove(fname);
2854 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 vim_free(cmd);
2856}
2857
2858/*
2859 * Return the name for the errorfile, in allocated memory.
2860 * Find a new unique name when 'makeef' contains "##".
2861 * Returns NULL for error.
2862 */
2863 static char_u *
2864get_mef_name()
2865{
2866 char_u *p;
2867 char_u *name;
2868 static int start = -1;
2869 static int off = 0;
2870#ifdef HAVE_LSTAT
2871 struct stat sb;
2872#endif
2873
2874 if (*p_mef == NUL)
2875 {
2876 name = vim_tempname('e');
2877 if (name == NULL)
2878 EMSG(_(e_notmp));
2879 return name;
2880 }
2881
2882 for (p = p_mef; *p; ++p)
2883 if (p[0] == '#' && p[1] == '#')
2884 break;
2885
2886 if (*p == NUL)
2887 return vim_strsave(p_mef);
2888
2889 /* Keep trying until the name doesn't exist yet. */
2890 for (;;)
2891 {
2892 if (start == -1)
2893 start = mch_get_pid();
2894 else
2895 off += 19;
2896
2897 name = alloc((unsigned)STRLEN(p_mef) + 30);
2898 if (name == NULL)
2899 break;
2900 STRCPY(name, p_mef);
2901 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2902 STRCAT(name, p + 2);
2903 if (mch_getperm(name) < 0
2904#ifdef HAVE_LSTAT
2905 /* Don't accept a symbolic link, its a security risk. */
2906 && mch_lstat((char *)name, &sb) < 0
2907#endif
2908 )
2909 break;
2910 vim_free(name);
2911 }
2912 return name;
2913}
2914
2915/*
2916 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002917 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
2919 void
2920ex_cc(eap)
2921 exarg_T *eap;
2922{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002923 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002924
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002925 if (eap->cmdidx == CMD_ll
2926 || eap->cmdidx == CMD_lrewind
2927 || eap->cmdidx == CMD_lfirst
2928 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002929 {
2930 qi = GET_LOC_LIST(curwin);
2931 if (qi == NULL)
2932 {
2933 EMSG(_(e_loclist));
2934 return;
2935 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002936 }
2937
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002938 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 eap->addr_count > 0
2940 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002941 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002943 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2944 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 ? 1
2946 : 32767,
2947 eap->forceit);
2948}
2949
2950/*
2951 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002952 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 */
2954 void
2955ex_cnext(eap)
2956 exarg_T *eap;
2957{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002958 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002959
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002960 if (eap->cmdidx == CMD_lnext
2961 || eap->cmdidx == CMD_lNext
2962 || eap->cmdidx == CMD_lprevious
2963 || eap->cmdidx == CMD_lnfile
2964 || eap->cmdidx == CMD_lNfile
2965 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002966 {
2967 qi = GET_LOC_LIST(curwin);
2968 if (qi == NULL)
2969 {
2970 EMSG(_(e_loclist));
2971 return;
2972 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002973 }
2974
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002975 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002977 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2980 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 ? BACKWARD_FILE
2982 : BACKWARD,
2983 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2984}
2985
2986/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002987 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002988 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 */
2990 void
2991ex_cfile(eap)
2992 exarg_T *eap;
2993{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002994 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002995 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002996
2997 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2998 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002999 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003000
Bram Moolenaar9028b102010-07-11 16:58:51 +02003001#ifdef FEAT_BROWSE
3002 if (cmdmod.browse)
3003 {
3004 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3005 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3006 if (browse_file == NULL)
3007 return;
3008 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3009 vim_free(browse_file);
3010 }
3011 else
3012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003014 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003015
3016 /*
3017 * This function is used by the :cfile, :cgetfile and :caddfile
3018 * commands.
3019 * :cfile always creates a new quickfix list and jumps to the
3020 * first error.
3021 * :cgetfile creates a new quickfix list but doesn't jump to the
3022 * first error.
3023 * :caddfile adds to an existing quickfix list. If there is no
3024 * quickfix list then a new list is created.
3025 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003026 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003027 && eap->cmdidx != CMD_laddfile),
3028 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003029 && (eap->cmdidx == CMD_cfile
3030 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003031 {
3032 if (wp != NULL)
3033 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003034 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036}
3037
3038/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003039 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003040 * ":vimgrepadd {pattern} file(s)"
3041 * ":lvimgrep {pattern} file(s)"
3042 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003043 */
3044 void
3045ex_vimgrep(eap)
3046 exarg_T *eap;
3047{
Bram Moolenaar81695252004-12-29 20:58:21 +00003048 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003049 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003050 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003051 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003052 char_u *s;
3053 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003054 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003055 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003056 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003057 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003058 buf_T *buf;
3059 int duplicate_name = FALSE;
3060 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003061 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003062 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003063 buf_T *first_match_buf = NULL;
3064 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003065 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003066#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3067 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003068#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003069 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003070 int flags = 0;
3071 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003072 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003073 char_u *dirname_start = NULL;
3074 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003075 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003076#ifdef FEAT_AUTOCMD
3077 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003078
3079 switch (eap->cmdidx)
3080 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003081 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3082 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3083 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003084 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003085 case CMD_grep: au_name = (char_u *)"grep"; break;
3086 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3087 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3088 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003089 default: break;
3090 }
3091 if (au_name != NULL)
3092 {
3093 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3094 curbuf->b_fname, TRUE, curbuf);
3095 if (did_throw || force_abort)
3096 return;
3097 }
3098#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003099
Bram Moolenaar754b5602006-02-09 23:53:20 +00003100 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003101 || eap->cmdidx == CMD_lvimgrep
3102 || eap->cmdidx == CMD_lgrepadd
3103 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003104 {
3105 qi = ll_get_or_alloc_list(curwin);
3106 if (qi == NULL)
3107 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003108 }
3109
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003110 if (eap->addr_count > 0)
3111 tomatch = eap->line2;
3112 else
3113 tomatch = MAXLNUM;
3114
Bram Moolenaar81695252004-12-29 20:58:21 +00003115 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003116 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003117 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003118 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003119 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003120 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003121 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003122 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003123 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003124 if (regmatch.regprog == NULL)
3125 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003126 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003127 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003128
3129 p = skipwhite(p);
3130 if (*p == NUL)
3131 {
3132 EMSG(_("E683: File name missing or invalid pattern"));
3133 goto theend;
3134 }
3135
Bram Moolenaar754b5602006-02-09 23:53:20 +00003136 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003137 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003138 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003139 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003140 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003141 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003142 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003143 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003144 prevp->qf_next != prevp; prevp = prevp->qf_next)
3145 ;
3146
3147 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003148 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003149 goto theend;
3150 if (fcount == 0)
3151 {
3152 EMSG(_(e_nomatch));
3153 goto theend;
3154 }
3155
Bram Moolenaard9462e32011-04-11 21:35:11 +02003156 dirname_start = alloc(MAXPATHL);
3157 dirname_now = alloc(MAXPATHL);
3158 if (dirname_start == NULL || dirname_now == NULL)
3159 goto theend;
3160
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003161 /* Remember the current directory, because a BufRead autocommand that does
3162 * ":lcd %:p:h" changes the meaning of short path names. */
3163 mch_dirname(dirname_start, MAXPATHL);
3164
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003165 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003166 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003167 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003168 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003169 if (time(NULL) > seconds)
3170 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003171 /* Display the file name every second or so, show the user we are
3172 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003173 seconds = time(NULL);
3174 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003175 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003176 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003177 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003178 else
3179 {
3180 msg_outtrans(p);
3181 vim_free(p);
3182 }
3183 msg_clr_eos();
3184 msg_didout = FALSE; /* overwrite this message */
3185 msg_nowait = TRUE; /* don't wait for this message */
3186 msg_col = 0;
3187 out_flush();
3188 }
3189
Bram Moolenaar81695252004-12-29 20:58:21 +00003190 buf = buflist_findname_exp(fnames[fi]);
3191 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3192 {
3193 /* Remember that a buffer with this name already exists. */
3194 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003195 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003196 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003197
3198#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3199 /* Don't do Filetype autocommands to avoid loading syntax and
3200 * indent scripts, a great speed improvement. */
3201 save_ei = au_event_disable(",Filetype");
3202#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003203 /* Don't use modelines here, it's useless. */
3204 save_mls = p_mls;
3205 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003206
3207 /* Load file into a buffer, so that 'fileencoding' is detected,
3208 * autocommands applied, etc. */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003209 buf = load_dummy_buffer(fname);
3210
3211 /* When autocommands changed directory: go back. We assume it was
3212 * ":lcd %:p:h". */
3213 mch_dirname(dirname_now, MAXPATHL);
3214 if (STRCMP(dirname_start, dirname_now) != 0)
3215 {
3216 exarg_T ea;
3217
3218 ea.arg = dirname_start;
3219 ea.cmdidx = CMD_lcd;
3220 ex_cd(&ea);
3221 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003222
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003223 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003224#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3225 au_event_restore(save_ei);
3226#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003227 }
3228 else
3229 /* Use existing, loaded buffer. */
3230 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003231
Bram Moolenaar81695252004-12-29 20:58:21 +00003232 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003233 {
3234 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003235 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003236 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003237 else
3238 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003239 /* Try for a match in all lines of the buffer.
3240 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003241 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003242 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3243 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003244 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003245 col = 0;
3246 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003247 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003248 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003249 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003250 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003251 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003252 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003253 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003254 ml_get_buf(buf,
3255 regmatch.startpos[0].lnum + lnum, FALSE),
3256 regmatch.startpos[0].lnum + lnum,
3257 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003258 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003259 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003260 0, /* nr */
3261 0, /* type */
3262 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003263 ) == FAIL)
3264 {
3265 got_int = TRUE;
3266 break;
3267 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003268 found_match = TRUE;
3269 if (--tomatch == 0)
3270 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003271 if ((flags & VGR_GLOBAL) == 0
3272 || regmatch.endpos[0].lnum > 0)
3273 break;
3274 col = regmatch.endpos[0].col
3275 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003276 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003277 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003278 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003279 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003280 if (got_int)
3281 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003282 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003283
3284 if (using_dummy)
3285 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003286 if (found_match && first_match_buf == NULL)
3287 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003288 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003289 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003290 /* Never keep a dummy buffer if there is another buffer
3291 * with the same name. */
3292 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003293 buf = NULL;
3294 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003295 else if (!cmdmod.hide
3296 || buf->b_p_bh[0] == 'u' /* "unload" */
3297 || buf->b_p_bh[0] == 'w' /* "wipe" */
3298 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003299 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003300 /* When no match was found we don't need to remember the
3301 * buffer, wipe it out. If there was a match and it
3302 * wasn't the first one or we won't jump there: only
3303 * unload the buffer.
3304 * Ignore 'hidden' here, because it may lead to having too
3305 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003306 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003307 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003308 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003309 buf = NULL;
3310 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003311 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003312 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003313 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003314 buf = NULL;
3315 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003316 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003317
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003318 if (buf != NULL)
3319 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003320 /* If the buffer is still loaded we need to use the
3321 * directory we jumped to below. */
3322 if (buf == first_match_buf
3323 && target_dir == NULL
3324 && STRCMP(dirname_start, dirname_now) != 0)
3325 target_dir = vim_strsave(dirname_now);
3326
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003327 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003328 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003329 * need to be done (again). But not the window-local
3330 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003331 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003332#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003333 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3334 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003335#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003336 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003337 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003338 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003339 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003340 }
3341 }
3342
3343 FreeWild(fcount, fnames);
3344
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003345 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3346 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3347 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003348
3349#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003350 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003351#endif
3352
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003353#ifdef FEAT_AUTOCMD
3354 if (au_name != NULL)
3355 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3356 curbuf->b_fname, TRUE, curbuf);
3357#endif
3358
Bram Moolenaar86b68352004-12-27 21:59:20 +00003359 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003360 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003361 {
3362 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003363 {
3364 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003365 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003366 if (buf != curbuf)
3367 /* If we jumped to another buffer redrawing will already be
3368 * taken care of. */
3369 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003370
3371 /* Jump to the directory used after loading the buffer. */
3372 if (curbuf == first_match_buf && target_dir != NULL)
3373 {
3374 exarg_T ea;
3375
3376 ea.arg = target_dir;
3377 ea.cmdidx = CMD_lcd;
3378 ex_cd(&ea);
3379 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003380 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003381 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003382 else
3383 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003384
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003385 /* If we loaded a dummy buffer into the current window, the autocommands
3386 * may have messed up things, need to redraw and recompute folds. */
3387 if (redraw_for_dummy)
3388 {
3389#ifdef FEAT_FOLDING
3390 foldUpdateAll(curwin);
3391#else
3392 redraw_later(NOT_VALID);
3393#endif
3394 }
3395
Bram Moolenaar86b68352004-12-27 21:59:20 +00003396theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003397 vim_free(dirname_now);
3398 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003399 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003400 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003401}
3402
3403/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003404 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003405 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003406 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3407 * If "s" is not NULL terminate the pattern with a NUL.
3408 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003409 */
3410 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003411skip_vimgrep_pat(p, s, flags)
3412 char_u *p;
3413 char_u **s;
3414 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003415{
3416 int c;
3417
3418 if (vim_isIDc(*p))
3419 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003420 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003421 if (s != NULL)
3422 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003423 p = skiptowhite(p);
3424 if (s != NULL && *p != NUL)
3425 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003426 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003427 else
3428 {
3429 /* ":vimgrep /pattern/[g][j] fname" */
3430 if (s != NULL)
3431 *s = p + 1;
3432 c = *p;
3433 p = skip_regexp(p + 1, c, TRUE, NULL);
3434 if (*p != c)
3435 return NULL;
3436
3437 /* Truncate the pattern. */
3438 if (s != NULL)
3439 *p = NUL;
3440 ++p;
3441
3442 /* Find the flags */
3443 while (*p == 'g' || *p == 'j')
3444 {
3445 if (flags != NULL)
3446 {
3447 if (*p == 'g')
3448 *flags |= VGR_GLOBAL;
3449 else
3450 *flags |= VGR_NOJUMP;
3451 }
3452 ++p;
3453 }
3454 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003455 return p;
3456}
3457
3458/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003459 * Load file "fname" into a dummy buffer and return the buffer pointer.
3460 * Returns NULL if it fails.
3461 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3462 */
3463 static buf_T *
3464load_dummy_buffer(fname)
3465 char_u *fname;
3466{
3467 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003468 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003469 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003470 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003471
3472 /* Allocate a buffer without putting it in the buffer list. */
3473 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3474 if (newbuf == NULL)
3475 return NULL;
3476
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003477 /* Init the options. */
3478 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3479
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003480 /* need to open the memfile before putting the buffer in a window */
3481 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003482 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003483 /* set curwin/curbuf to buf and save a few things */
3484 aucmd_prepbuf(&aco, newbuf);
3485
3486 /* Need to set the filename for autocommands. */
3487 (void)setfname(curbuf, fname, NULL, FALSE);
3488
Bram Moolenaar81695252004-12-29 20:58:21 +00003489 /* Create swap file now to avoid the ATTENTION message. */
3490 check_need_swap(TRUE);
3491
3492 /* Remove the "dummy" flag, otherwise autocommands may not
3493 * work. */
3494 curbuf->b_flags &= ~BF_DUMMY;
3495
3496 if (readfile(fname, NULL,
3497 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3498 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003499 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003500 && !(curbuf->b_flags & BF_NEW))
3501 {
3502 failed = FALSE;
3503 if (curbuf != newbuf)
3504 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003505 /* Bloody autocommands changed the buffer! Can happen when
3506 * using netrw and editing a remote file. Use the current
3507 * buffer instead, delete the dummy one after restoring the
3508 * window stuff. */
3509 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003510 newbuf = curbuf;
3511 }
3512 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003513
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003514 /* restore curwin/curbuf and a few other things */
3515 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003516 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3517 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003518 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003519
3520 if (!buf_valid(newbuf))
3521 return NULL;
3522 if (failed)
3523 {
3524 wipe_dummy_buffer(newbuf);
3525 return NULL;
3526 }
3527 return newbuf;
3528}
3529
3530/*
3531 * Wipe out the dummy buffer that load_dummy_buffer() created.
3532 */
3533 static void
3534wipe_dummy_buffer(buf)
3535 buf_T *buf;
3536{
3537 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003538 {
3539#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3540 cleanup_T cs;
3541
3542 /* Reset the error/interrupt/exception state here so that aborting()
3543 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3544 * work when got_int is set. */
3545 enter_cleanup(&cs);
3546#endif
3547
Bram Moolenaar81695252004-12-29 20:58:21 +00003548 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003549
3550#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3551 /* Restore the error/interrupt/exception state if not discarded by a
3552 * new aborting error, interrupt, or uncaught exception. */
3553 leave_cleanup(&cs);
3554#endif
3555 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003556}
3557
3558/*
3559 * Unload the dummy buffer that load_dummy_buffer() created.
3560 */
3561 static void
3562unload_dummy_buffer(buf)
3563 buf_T *buf;
3564{
3565 if (curbuf != buf) /* safety check */
3566 close_buffer(NULL, buf, DOBUF_UNLOAD);
3567}
3568
Bram Moolenaar05159a02005-02-26 23:04:13 +00003569#if defined(FEAT_EVAL) || defined(PROTO)
3570/*
3571 * Add each quickfix error to list "list" as a dictionary.
3572 */
3573 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003574get_errorlist(wp, list)
3575 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003576 list_T *list;
3577{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003578 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003579 dict_T *dict;
3580 char_u buf[2];
3581 qfline_T *qfp;
3582 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003583 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003584
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003585 if (wp != NULL)
3586 {
3587 qi = GET_LOC_LIST(wp);
3588 if (qi == NULL)
3589 return FAIL;
3590 }
3591
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003592 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003593 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003594 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003595
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003596 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3597 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003598 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003599 /* Handle entries with a non-existing buffer number. */
3600 bufnum = qfp->qf_fnum;
3601 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3602 bufnum = 0;
3603
Bram Moolenaar05159a02005-02-26 23:04:13 +00003604 if ((dict = dict_alloc()) == NULL)
3605 return FAIL;
3606 if (list_append_dict(list, dict) == FAIL)
3607 return FAIL;
3608
3609 buf[0] = qfp->qf_type;
3610 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003611 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003612 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3613 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3614 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3615 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003616 || dict_add_nr_str(dict, "pattern", 0L,
3617 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3618 || dict_add_nr_str(dict, "text", 0L,
3619 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003620 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3621 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3622 return FAIL;
3623
3624 qfp = qfp->qf_next;
3625 }
3626 return OK;
3627}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003628
3629/*
3630 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003631 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003632 */
3633 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003634set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003635 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003636 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003637 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003638 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003639{
3640 listitem_T *li;
3641 dict_T *d;
3642 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003643 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003644 long lnum;
3645 int col, nr;
3646 int vcol;
3647 qfline_T *prevp = NULL;
3648 int valid, status;
3649 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003650 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003651 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003652
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003653 if (wp != NULL)
3654 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003655 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003656 if (qi == NULL)
3657 return FAIL;
3658 }
3659
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003660 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003661 /* make place for a new list */
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003662 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003663 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003664 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003665 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003666 prevp->qf_next != prevp; prevp = prevp->qf_next)
3667 ;
3668 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003669 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003670
3671 for (li = list->lv_first; li != NULL; li = li->li_next)
3672 {
3673 if (li->li_tv.v_type != VAR_DICT)
3674 continue; /* Skip non-dict items */
3675
3676 d = li->li_tv.vval.v_dict;
3677 if (d == NULL)
3678 continue;
3679
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003680 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003681 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003682 lnum = get_dict_number(d, (char_u *)"lnum");
3683 col = get_dict_number(d, (char_u *)"col");
3684 vcol = get_dict_number(d, (char_u *)"vcol");
3685 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003686 type = get_dict_string(d, (char_u *)"type", TRUE);
3687 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3688 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003689 if (text == NULL)
3690 text = vim_strsave((char_u *)"");
3691
3692 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003693 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003694 valid = FALSE;
3695
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003696 /* Mark entries with non-existing buffer number as not valid. Give the
3697 * error message only once. */
3698 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3699 {
3700 if (!did_bufnr_emsg)
3701 {
3702 did_bufnr_emsg = TRUE;
3703 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3704 }
3705 valid = FALSE;
3706 bufnum = 0;
3707 }
3708
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003709 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003710 NULL, /* dir */
3711 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003712 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003713 text,
3714 lnum,
3715 col,
3716 vcol, /* vis_col */
3717 pattern, /* search pattern */
3718 nr,
3719 type == NULL ? NUL : *type,
3720 valid);
3721
3722 vim_free(filename);
3723 vim_free(pattern);
3724 vim_free(text);
3725 vim_free(type);
3726
3727 if (status == FAIL)
3728 {
3729 retval = FAIL;
3730 break;
3731 }
3732 }
3733
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003734 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003735 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003736 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3737 else
3738 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003739 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3740 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003741
3742#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003743 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003744#endif
3745
3746 return retval;
3747}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003748#endif
3749
Bram Moolenaar81695252004-12-29 20:58:21 +00003750/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003751 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003752 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003753 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003754 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003755 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003756 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003757 */
3758 void
3759ex_cbuffer(eap)
3760 exarg_T *eap;
3761{
3762 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003763 qf_info_T *qi = &ql_info;
3764
Bram Moolenaardb552d602006-03-23 22:59:57 +00003765 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3766 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003767 {
3768 qi = ll_get_or_alloc_list(curwin);
3769 if (qi == NULL)
3770 return;
3771 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003772
3773 if (*eap->arg == NUL)
3774 buf = curbuf;
3775 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3776 buf = buflist_findnr(atoi((char *)eap->arg));
3777 if (buf == NULL)
3778 EMSG(_(e_invarg));
3779 else if (buf->b_ml.ml_mfp == NULL)
3780 EMSG(_("E681: Buffer is not loaded"));
3781 else
3782 {
3783 if (eap->addr_count == 0)
3784 {
3785 eap->line1 = 1;
3786 eap->line2 = buf->b_ml.ml_line_count;
3787 }
3788 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3789 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3790 EMSG(_(e_invrange));
3791 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003792 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003793 char_u *qf_title = *eap->cmdlinep;
3794
3795 if (buf->b_sfname)
3796 {
3797 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3798 (char *)qf_title, (char *)buf->b_sfname);
3799 qf_title = IObuff;
3800 }
3801
Bram Moolenaardb552d602006-03-23 22:59:57 +00003802 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3803 (eap->cmdidx != CMD_caddbuffer
3804 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003805 eap->line1, eap->line2,
3806 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003807 && (eap->cmdidx == CMD_cbuffer
3808 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003809 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3810 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003811 }
3812}
3813
Bram Moolenaar1e015462005-09-25 22:16:38 +00003814#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003815/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003816 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3817 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003818 */
3819 void
3820ex_cexpr(eap)
3821 exarg_T *eap;
3822{
3823 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003824 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003825
Bram Moolenaardb552d602006-03-23 22:59:57 +00003826 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3827 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003828 {
3829 qi = ll_get_or_alloc_list(curwin);
3830 if (qi == NULL)
3831 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003832 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003833
Bram Moolenaar4770d092006-01-12 23:22:24 +00003834 /* Evaluate the expression. When the result is a string or a list we can
3835 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003836 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003837 if (tv != NULL)
3838 {
3839 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3840 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3841 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003842 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3843 (eap->cmdidx != CMD_caddexpr
3844 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003845 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003846 && (eap->cmdidx == CMD_cexpr
3847 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003848 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003849 }
3850 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003851 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003852 free_tv(tv);
3853 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003854}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003855#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003856
3857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 * ":helpgrep {pattern}"
3859 */
3860 void
3861ex_helpgrep(eap)
3862 exarg_T *eap;
3863{
3864 regmatch_T regmatch;
3865 char_u *save_cpo;
3866 char_u *p;
3867 int fcount;
3868 char_u **fnames;
3869 FILE *fd;
3870 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003871 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003873#ifdef FEAT_MULTI_LANG
3874 char_u *lang;
3875#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003876 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003877 int new_qi = FALSE;
3878 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3881 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00003882 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003884#ifdef FEAT_MULTI_LANG
3885 /* Check for a specified language */
3886 lang = check_help_lang(eap->arg);
3887#endif
3888
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003889 if (eap->cmdidx == CMD_lhelpgrep)
3890 {
3891 /* Find an existing help window */
3892 FOR_ALL_WINDOWS(wp)
3893 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3894 break;
3895
3896 if (wp == NULL) /* Help window not found */
3897 qi = NULL;
3898 else
3899 qi = wp->w_llist;
3900
3901 if (qi == NULL)
3902 {
3903 /* Allocate a new location list for help text matches */
3904 if ((qi = ll_new_list()) == NULL)
3905 return;
3906 new_qi = TRUE;
3907 }
3908 }
3909
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3911 regmatch.rm_ic = FALSE;
3912 if (regmatch.regprog != NULL)
3913 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003914#ifdef FEAT_MBYTE
3915 vimconv_T vc;
3916
3917 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
3918 * differs. */
3919 vc.vc_type = CONV_NONE;
3920 if (!enc_utf8)
3921 convert_setup(&vc, (char_u *)"utf-8", p_enc);
3922#endif
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 /* create a new quickfix list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003925 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 /* Go through all directories in 'runtimepath' */
3928 p = p_rtp;
3929 while (*p != NUL && !got_int)
3930 {
3931 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3932
3933 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3934 add_pathsep(NameBuff);
3935 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3936 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3937 &fnames, EW_FILE|EW_SILENT) == OK
3938 && fcount > 0)
3939 {
3940 for (fi = 0; fi < fcount && !got_int; ++fi)
3941 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003942#ifdef FEAT_MULTI_LANG
3943 /* Skip files for a different language. */
3944 if (lang != NULL
3945 && STRNICMP(lang, fnames[fi]
3946 + STRLEN(fnames[fi]) - 3, 2) != 0
3947 && !(STRNICMP(lang, "en", 2) == 0
3948 && STRNICMP("txt", fnames[fi]
3949 + STRLEN(fnames[fi]) - 3, 3) == 0))
3950 continue;
3951#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003952 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (fd != NULL)
3954 {
3955 lnum = 1;
3956 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3957 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003958 char_u *line = IObuff;
3959#ifdef FEAT_MBYTE
3960 /* Convert a line if 'encoding' is not utf-8 and
3961 * the line contains a non-ASCII character. */
3962 if (vc.vc_type != CONV_NONE
3963 && has_non_ascii(IObuff)) {
3964 line = string_convert(&vc, IObuff, NULL);
3965 if (line == NULL)
3966 line = IObuff;
3967 }
3968#endif
3969
3970 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003972 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003975 while (l > 0 && line[l - 1] <= ' ')
3976 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003978 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 NULL, /* dir */
3980 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003981 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003982 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003984 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00003985 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003986 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003987 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 0, /* nr */
3989 1, /* type */
3990 TRUE /* valid */
3991 ) == FAIL)
3992 {
3993 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003994#ifdef FEAT_MBYTE
3995 if (line != IObuff)
3996 vim_free(line);
3997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 break;
3999 }
4000 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004001#ifdef FEAT_MBYTE
4002 if (line != IObuff)
4003 vim_free(line);
4004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 ++lnum;
4006 line_breakcheck();
4007 }
4008 fclose(fd);
4009 }
4010 }
4011 FreeWild(fcount, fnames);
4012 }
4013 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004014
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 vim_free(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004016#ifdef FEAT_MBYTE
4017 if (vc.vc_type != CONV_NONE)
4018 convert_setup(&vc, NULL, NULL);
4019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004021 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4022 qi->qf_lists[qi->qf_curlist].qf_ptr =
4023 qi->qf_lists[qi->qf_curlist].qf_start;
4024 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004027 if (p_cpo == empty_option)
4028 p_cpo = save_cpo;
4029 else
4030 /* Darn, some plugin changed the value. */
4031 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
4033#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004034 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035#endif
4036
4037 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004038 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004039 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004040 else
4041 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004042
4043 if (eap->cmdidx == CMD_lhelpgrep)
4044 {
4045 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004046 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004047 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4048 {
4049 if (new_qi)
4050 ll_free_all(&qi);
4051 }
4052 else if (curwin->w_llist == NULL)
4053 curwin->w_llist = qi;
4054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055}
4056
4057#endif /* FEAT_QUICKFIX */