blob: 6e3fa6afe8df6cba7b1a79574a5cb1713aca91c9 [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 Moolenaard12f5c12006-01-25 22:10:52 +000059} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061struct qf_info_S
62{
63 /*
64 * Count of references to this list. Used only for location lists.
65 * When a location list window reference this list, qf_refcount
66 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
67 * reaches 0, the list is freed.
68 */
69 int qf_refcount;
70 int qf_listcount; /* current number of lists */
71 int qf_curlist; /* current error list */
72 qf_list_T qf_lists[LISTCOUNT];
73};
74
75static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar68b76a62005-03-25 21:53:48 +000077#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79/*
80 * Structure used to hold the info of one part of 'errorformat'
81 */
Bram Moolenaar01265852006-03-20 21:50:15 +000082typedef struct efm_S efm_T;
83struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000086 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
88 char_u prefix; /* prefix of this format line: */
89 /* 'D' enter directory */
90 /* 'X' leave directory */
91 /* 'A' start of multi-line message */
92 /* 'E' error message */
93 /* 'W' warning message */
94 /* 'I' informational message */
95 /* 'C' continuation line */
96 /* 'Z' end of multi-line message */
97 /* 'G' general, unspecific message */
98 /* 'P' push file (partial) message */
99 /* 'Q' pop/quit file (partial) message */
100 /* 'O' overread (partial) message */
101 char_u flags; /* additional flags given in prefix */
102 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000103 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000104 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105};
106
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000107static 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));
108static void qf_new_list __ARGS((qf_info_T *qi));
109static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
110static void qf_msg __ARGS((qf_info_T *qi));
111static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112static char_u *qf_types __ARGS((int, int));
113static int qf_get_fnum __ARGS((char_u *, char_u *));
114static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
115static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
116static char_u *qf_guess_filepath __ARGS((char_u *));
117static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
118static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
119#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000120static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000121static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000122static win_T *qf_find_win __ARGS((qf_info_T *qi));
123static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
124static void qf_update_buffer __ARGS((qf_info_T *qi));
125static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000128static buf_T *load_dummy_buffer __ARGS((char_u *fname));
129static void wipe_dummy_buffer __ARGS((buf_T *buf));
130static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000131static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000133/* Quickfix window check helper macro */
134#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
135/* Location list window check helper macro */
136#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
137/*
138 * Return location list for window 'wp'
139 * For location list window, return the referenced location list
140 */
141#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000144 * Read the errorfile "efile" into memory, line by line, building the error
145 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 * Return -1 for error, number of errors for success.
147 */
148 int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000149qf_init(wp, efile, errorformat, newlist)
150 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 char_u *efile;
152 char_u *errorformat;
153 int newlist; /* TRUE: start a new error list */
154{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000155 qf_info_T *qi = &ql_info;
156
Bram Moolenaar86b68352004-12-27 21:59:20 +0000157 if (efile == NULL)
158 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000159
160 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000161 {
162 qi = ll_get_or_alloc_list(wp);
163 if (qi == NULL)
164 return FAIL;
165 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166
167 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +0000168 (linenr_T)0, (linenr_T)0);
169}
170
171/*
172 * Read the errorfile "efile" into memory, line by line, building the error
173 * list.
174 * Alternative: when "efile" is null read errors from buffer "buf".
175 * Always use 'errorformat' from "buf" if there is a local value.
176 * Then lnumfirst and lnumlast specify the range of lines to use.
177 * Return -1 for error, number of errors for success.
178 */
179 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000180qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
181 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182 char_u *efile;
183 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000184 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185 char_u *errorformat;
186 int newlist; /* TRUE: start a new error list */
187 linenr_T lnumfirst; /* first line number to use */
188 linenr_T lnumlast; /* last line number to use */
189{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 char_u *namebuf;
191 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000192 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 char_u *fmtstr = NULL;
194 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000195 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 int type = 0;
197 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000198 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 long lnum = 0L;
200 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000201 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000202 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000204 efm_T *fmt_first = NULL;
205 efm_T *fmt_last = NULL;
206 efm_T *fmt_ptr;
207 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 char_u *efm;
209 char_u *ptr;
210 char_u *srcptr;
211 int len;
212 int i;
213 int round;
214 int idx = 0;
215 int multiline = FALSE;
216 int multiignore = FALSE;
217 int multiscan = FALSE;
218 int retval = -1; /* default: return error flag */
219 char_u *directory = NULL;
220 char_u *currfile = NULL;
221 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000222 char_u *p_str = NULL;
223 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 struct dir_stack_T *file_stack = NULL;
225 regmatch_T regmatch;
226 static struct fmtpattern
227 {
228 char_u convchar;
229 char *pattern;
230 } fmt_pat[FMT_PATTERNS] =
231 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000232 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {'n', "\\d\\+"},
234 {'l', "\\d\\+"},
235 {'c', "\\d\\+"},
236 {'t', "."},
237 {'m', ".\\+"},
238 {'r', ".*"},
239 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000240 {'v', "\\d\\+"},
241 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 };
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 namebuf = alloc(CMDBUFFSIZE + 1);
245 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000246 pattern = alloc(CMDBUFFSIZE + 1);
247 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 goto qf_init_end;
249
Bram Moolenaar86b68352004-12-27 21:59:20 +0000250 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 {
252 EMSG2(_(e_openerrf), efile);
253 goto qf_init_end;
254 }
255
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000256 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000258 qf_new_list(qi);
259 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000261 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
263 ;
264
265/*
266 * Each part of the format string is copied and modified from errorformat to
267 * regex prog. Only a few % characters are allowed.
268 */
269 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000270 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000271 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 else
273 efm = errorformat;
274 /*
275 * Get some space to modify the format string into.
276 */
277 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
278 for (round = FMT_PATTERNS; round > 0; )
279 i += (int)STRLEN(fmt_pat[--round].pattern);
280#ifdef COLON_IN_FILENAME
281 i += 12; /* "%f" can become twelve chars longer */
282#else
283 i += 2; /* "%f" can become two chars longer */
284#endif
285 if ((fmtstr = alloc(i)) == NULL)
286 goto error2;
287
Bram Moolenaar01265852006-03-20 21:50:15 +0000288 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {
290 /*
291 * Allocate a new eformat structure and put it at the end of the list
292 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000293 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 if (fmt_ptr == NULL)
295 goto error2;
296 if (fmt_first == NULL) /* first one */
297 fmt_first = fmt_ptr;
298 else
299 fmt_last->next = fmt_ptr;
300 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302 /*
303 * Isolate one part in the 'errorformat' option
304 */
305 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
306 if (efm[len] == '\\' && efm[len + 1] != NUL)
307 ++len;
308
309 /*
310 * Build regexp pattern from current 'errorformat' option
311 */
312 ptr = fmtstr;
313 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000314 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 for (efmp = efm; efmp < efm + len; ++efmp)
316 {
317 if (*efmp == '%')
318 {
319 ++efmp;
320 for (idx = 0; idx < FMT_PATTERNS; ++idx)
321 if (fmt_pat[idx].convchar == *efmp)
322 break;
323 if (idx < FMT_PATTERNS)
324 {
325 if (fmt_ptr->addr[idx])
326 {
327 sprintf((char *)errmsg,
328 _("E372: Too many %%%c in format string"), *efmp);
329 EMSG(errmsg);
330 goto error2;
331 }
332 if ((idx
333 && idx < 6
334 && vim_strchr((char_u *)"DXOPQ",
335 fmt_ptr->prefix) != NULL)
336 || (idx == 6
337 && vim_strchr((char_u *)"OPQ",
338 fmt_ptr->prefix) == NULL))
339 {
340 sprintf((char *)errmsg,
341 _("E373: Unexpected %%%c in format string"), *efmp);
342 EMSG(errmsg);
343 goto error2;
344 }
345 fmt_ptr->addr[idx] = (char_u)++round;
346 *ptr++ = '\\';
347 *ptr++ = '(';
348#ifdef BACKSLASH_IN_FILENAME
349 if (*efmp == 'f')
350 {
351 /* Also match "c:" in the file name, even when
352 * checking for a colon next: "%f:".
353 * "\%(\a:\)\=" */
354 STRCPY(ptr, "\\%(\\a:\\)\\=");
355 ptr += 10;
356 }
357#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000358 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000360 if (efmp[1] != '\\' && efmp[1] != '%')
361 {
362 /* A file name may contain spaces, but this isn't
363 * in "\f". For "%f:%l:%m" there may be a ":" in
364 * the file name. Use ".\{-1,}x" instead (x is
365 * the next character), the requirement that :999:
366 * follows should work. */
367 STRCPY(ptr, ".\\{-1,}");
368 ptr += 7;
369 }
370 else
371 {
372 /* File name followed by '\\' or '%': include as
373 * many file name chars as possible. */
374 STRCPY(ptr, "\\f\\+");
375 ptr += 4;
376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 }
378 else
379 {
380 srcptr = (char_u *)fmt_pat[idx].pattern;
381 while ((*ptr = *srcptr++) != NUL)
382 ++ptr;
383 }
384 *ptr++ = '\\';
385 *ptr++ = ')';
386 }
387 else if (*efmp == '*')
388 {
389 if (*++efmp == '[' || *efmp == '\\')
390 {
391 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
392 {
393 if (efmp[1] == '^')
394 *ptr++ = *++efmp;
395 if (efmp < efm + len)
396 {
397 *ptr++ = *++efmp; /* could be ']' */
398 while (efmp < efm + len
399 && (*ptr++ = *++efmp) != ']')
400 /* skip */;
401 if (efmp == efm + len)
402 {
403 EMSG(_("E374: Missing ] in format string"));
404 goto error2;
405 }
406 }
407 }
408 else if (efmp < efm + len) /* %*\D, %*\s etc. */
409 *ptr++ = *++efmp;
410 *ptr++ = '\\';
411 *ptr++ = '+';
412 }
413 else
414 {
415 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
416 sprintf((char *)errmsg,
417 _("E375: Unsupported %%%c in format string"), *efmp);
418 EMSG(errmsg);
419 goto error2;
420 }
421 }
422 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
423 *ptr++ = *efmp; /* regexp magic characters */
424 else if (*efmp == '#')
425 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000426 else if (*efmp == '>')
427 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 else if (efmp == efm + 1) /* analyse prefix */
429 {
430 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
431 fmt_ptr->flags = *efmp++;
432 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
433 fmt_ptr->prefix = *efmp;
434 else
435 {
436 sprintf((char *)errmsg,
437 _("E376: Invalid %%%c in format string prefix"), *efmp);
438 EMSG(errmsg);
439 goto error2;
440 }
441 }
442 else
443 {
444 sprintf((char *)errmsg,
445 _("E377: Invalid %%%c in format string"), *efmp);
446 EMSG(errmsg);
447 goto error2;
448 }
449 }
450 else /* copy normal character */
451 {
452 if (*efmp == '\\' && efmp + 1 < efm + len)
453 ++efmp;
454 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
455 *ptr++ = '\\'; /* escape regexp atoms */
456 if (*efmp)
457 *ptr++ = *efmp;
458 }
459 }
460 *ptr++ = '$';
461 *ptr = NUL;
462 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
463 goto error2;
464 /*
465 * Advance to next part
466 */
467 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
468 }
469 if (fmt_first == NULL) /* nothing found */
470 {
471 EMSG(_("E378: 'errorformat' contains no pattern"));
472 goto error2;
473 }
474
475 /*
476 * got_int is reset here, because it was probably set when killing the
477 * ":make" command, but we still want to read the errorfile then.
478 */
479 got_int = FALSE;
480
481 /* Always ignore case when looking for a matching error. */
482 regmatch.rm_ic = TRUE;
483
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000484 if (tv != NULL)
485 {
486 if (tv->v_type == VAR_STRING)
487 p_str = tv->vval.v_string;
488 else if (tv->v_type == VAR_LIST)
489 p_li = tv->vval.v_list->lv_first;
490 }
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 /*
493 * Read the lines in the error file one by one.
494 * Try to recognize one of the error formats in each line.
495 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000496 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000498 /* Get the next line. */
499 if (fd == NULL)
500 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000501 if (tv != NULL)
502 {
503 int len;
504
505 if (tv->v_type == VAR_STRING)
506 {
507 /* Get the next line from the supplied string */
508 char_u *p;
509
510 if (!*p_str) /* Reached the end of the string */
511 break;
512
513 p = vim_strchr(p_str, '\n');
514 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000515 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000516 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000517 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000518
519 if (len > CMDBUFFSIZE - 2)
520 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
521 else
522 vim_strncpy(IObuff, p_str, len);
523
524 p_str += len;
525 }
526 else if (tv->v_type == VAR_LIST)
527 {
528 /* Get the next line from the supplied list */
529 while (p_li && p_li->li_tv.v_type != VAR_STRING)
530 p_li = p_li->li_next; /* Skip non-string items */
531
532 if (!p_li) /* End of the list */
533 break;
534
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000535 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000536 if (len > CMDBUFFSIZE - 2)
537 len = CMDBUFFSIZE - 2;
538
539 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
540
541 p_li = p_li->li_next; /* next item */
542 }
543 }
544 else
545 {
546 /* Get the next line from the supplied buffer */
547 if (buflnum > lnumlast)
548 break;
549 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
550 CMDBUFFSIZE - 2);
551 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000552 }
553 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
554 break;
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
557 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
558 *efmp = NUL;
559#ifdef USE_CRNL
560 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
561 *efmp = NUL;
562#endif
563
Bram Moolenaar01265852006-03-20 21:50:15 +0000564 /* If there was no %> item start at the first pattern */
565 if (fmt_start == NULL)
566 fmt_ptr = fmt_first;
567 else
568 {
569 fmt_ptr = fmt_start;
570 fmt_start = NULL;
571 }
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 /*
574 * Try to match each part of 'errorformat' until we find a complete
575 * match or no match.
576 */
577 valid = TRUE;
578restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000579 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 idx = fmt_ptr->prefix;
582 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
583 continue;
584 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000585 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (!multiscan)
587 errmsg[0] = NUL;
588 lnum = 0;
589 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000590 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 enr = -1;
592 type = 0;
593 tail = NULL;
594
595 regmatch.regprog = fmt_ptr->prog;
596 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
597 {
598 if ((idx == 'C' || idx == 'Z') && !multiline)
599 continue;
600 if (vim_strchr((char_u *)"EWI", idx) != NULL)
601 type = idx;
602 else
603 type = 0;
604 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000605 * Extract error message data from matched line.
606 * We check for an actual submatch, because "\[" and "\]" in
607 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 */
609 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
610 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000611 int c;
612
613 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
614 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000615
616 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000617 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000618 *regmatch.endp[i] = NUL;
619 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
620 *regmatch.endp[i] = c;
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000623 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 continue;
625 }
626 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000627 {
628 if (regmatch.startp[i] == NULL)
629 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000633 {
634 if (regmatch.startp[i] == NULL)
635 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000639 {
640 if (regmatch.startp[i] == NULL)
641 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 col = (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[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000645 {
646 if (regmatch.startp[i] == NULL)
647 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000649 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000650 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 STRCPY(errmsg, IObuff);
652 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
653 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000654 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
655 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000657 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000660 {
661 if (regmatch.startp[i] == NULL)
662 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
666 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000667 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
668 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
670 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000671 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
674 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000675 if (regmatch.startp[i] == NULL)
676 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000678 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000680 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
681 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000682 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
683 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000684 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
685 if (len > CMDBUFFSIZE - 5)
686 len = CMDBUFFSIZE - 5;
687 STRCPY(pattern, "^\\V");
688 STRNCAT(pattern, regmatch.startp[i], len);
689 pattern[len + 3] = '\\';
690 pattern[len + 4] = '$';
691 pattern[len + 5] = NUL;
692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 break;
694 }
695 }
696 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000697
Bram Moolenaar4770d092006-01-12 23:22:24 +0000698 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000700 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {
702 if (idx == 'D') /* enter directory */
703 {
704 if (*namebuf == NUL)
705 {
706 EMSG(_("E379: Missing or empty directory name"));
707 goto error2;
708 }
709 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
710 goto error2;
711 }
712 else if (idx == 'X') /* leave directory */
713 directory = qf_pop_dir(&dir_stack);
714 }
715 namebuf[0] = NUL; /* no match found, remove file name */
716 lnum = 0; /* don't jump to this line */
717 valid = FALSE;
718 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000719 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 multiline = multiignore = FALSE;
721 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000722 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000724 /* honor %> item */
725 if (fmt_ptr->conthere)
726 fmt_start = fmt_ptr;
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
729 multiline = TRUE; /* start of a multi-line message */
730 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
731 { /* continuation of multi-line msg */
732 if (qfprev == NULL)
733 goto error2;
734 if (*errmsg && !multiignore)
735 {
736 len = (int)STRLEN(qfprev->qf_text);
737 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
738 == NULL)
739 goto error2;
740 STRCPY(ptr, qfprev->qf_text);
741 vim_free(qfprev->qf_text);
742 qfprev->qf_text = ptr;
743 *(ptr += len) = '\n';
744 STRCPY(++ptr, errmsg);
745 }
746 if (qfprev->qf_nr == -1)
747 qfprev->qf_nr = enr;
748 if (vim_isprintc(type) && !qfprev->qf_type)
749 qfprev->qf_type = type; /* only printable chars allowed */
750 if (!qfprev->qf_lnum)
751 qfprev->qf_lnum = lnum;
752 if (!qfprev->qf_col)
753 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000754 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (!qfprev->qf_fnum)
756 qfprev->qf_fnum = qf_get_fnum(directory,
757 *namebuf || directory ? namebuf
758 : currfile && valid ? currfile : 0);
759 if (idx == 'Z')
760 multiline = multiignore = FALSE;
761 line_breakcheck();
762 continue;
763 }
764 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
765 {
766 /* global file names */
767 valid = FALSE;
768 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
769 {
770 if (*namebuf && idx == 'P')
771 currfile = qf_push_dir(namebuf, &file_stack);
772 else if (idx == 'Q')
773 currfile = qf_pop_dir(&file_stack);
774 *namebuf = NUL;
775 if (tail && *tail)
776 {
777 STRCPY(IObuff, skipwhite(tail));
778 multiscan = TRUE;
779 goto restofline;
780 }
781 }
782 }
783 if (fmt_ptr->flags == '-') /* generally exclude this line */
784 {
785 if (multiline)
786 multiignore = TRUE; /* also exclude continuation lines */
787 continue;
788 }
789 }
790
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000791 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000793 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000795 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 errmsg,
797 lnum,
798 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000800 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 enr,
802 type,
803 valid) == FAIL)
804 goto error2;
805 line_breakcheck();
806 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000807 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000809 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000811 /* no valid entry found */
812 qi->qf_lists[qi->qf_curlist].qf_ptr =
813 qi->qf_lists[qi->qf_curlist].qf_start;
814 qi->qf_lists[qi->qf_curlist].qf_index = 1;
815 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817 else
818 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000819 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
820 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
821 qi->qf_lists[qi->qf_curlist].qf_ptr =
822 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000824 /* return number of matches */
825 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 goto qf_init_ok;
827 }
828 EMSG(_(e_readerrf));
829error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000830 qf_free(qi, qi->qf_curlist);
831 qi->qf_listcount--;
832 if (qi->qf_curlist > 0)
833 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000835 if (fd != NULL)
836 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
838 {
839 fmt_first = fmt_ptr->next;
840 vim_free(fmt_ptr->prog);
841 vim_free(fmt_ptr);
842 }
843 qf_clean_dir_stack(&dir_stack);
844 qf_clean_dir_stack(&file_stack);
845qf_init_end:
846 vim_free(namebuf);
847 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000848 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 vim_free(fmtstr);
850
851#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000852 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853#endif
854
855 return retval;
856}
857
858/*
859 * Prepare for adding a new quickfix list.
860 */
861 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000862qf_new_list(qi)
863 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 int i;
866
867 /*
868 * If the current entry is not the last entry, delete entries below
869 * the current entry. This makes it possible to browse in a tree-like
870 * way with ":grep'.
871 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000872 while (qi->qf_listcount > qi->qf_curlist + 1)
873 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875 /*
876 * When the stack is full, remove to oldest entry
877 * Otherwise, add a new entry.
878 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000879 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000881 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000883 qi->qf_lists[i - 1] = qi->qf_lists[i];
884 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000887 qi->qf_curlist = qi->qf_listcount++;
888 qi->qf_lists[qi->qf_curlist].qf_index = 0;
889 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890}
891
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000892/*
893 * Free a location list
894 */
895 static void
896ll_free_all(pqi)
897 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000898{
899 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000900 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000901
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000902 qi = *pqi;
903 if (qi == NULL)
904 return;
905 *pqi = NULL; /* Remove reference to this list */
906
907 qi->qf_refcount--;
908 if (qi->qf_refcount < 1)
909 {
910 /* No references to this location list */
911 for (i = 0; i < qi->qf_listcount; ++i)
912 qf_free(qi, i);
913 vim_free(qi);
914 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000915}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000916
917 void
918qf_free_all(wp)
919 win_T *wp;
920{
921 int i;
922 qf_info_T *qi = &ql_info;
923
924 if (wp != NULL)
925 {
926 /* location list */
927 ll_free_all(&wp->w_llist);
928 ll_free_all(&wp->w_llist_ref);
929 }
930 else
931 /* quickfix list */
932 for (i = 0; i < qi->qf_listcount; ++i)
933 qf_free(qi, i);
934}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000935
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936/*
937 * Add an entry to the end of the list of errors.
938 * Returns OK or FAIL.
939 */
940 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000941qf_add_entry(qi, prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000942 valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000943 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000944 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 char_u *dir; /* optional directory name */
946 char_u *fname; /* file name or NULL */
947 char_u *mesg; /* message */
948 long lnum; /* line number */
949 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000950 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000951 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 int nr; /* error number */
953 int type; /* type character */
954 int valid; /* valid entry */
955{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000956 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000958 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 return FAIL;
960 qfp->qf_fnum = qf_get_fnum(dir, fname);
961 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
962 {
963 vim_free(qfp);
964 return FAIL;
965 }
966 qfp->qf_lnum = lnum;
967 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000968 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000969 if (pattern == NULL || *pattern == NUL)
970 qfp->qf_pattern = NULL;
971 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
972 {
973 vim_free(qfp->qf_text);
974 vim_free(qfp);
975 return FAIL;
976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 qfp->qf_nr = nr;
978 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
979 type = 0;
980 qfp->qf_type = type;
981 qfp->qf_valid = valid;
982
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000983 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
984 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000986 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 qfp->qf_prev = qfp; /* first element points to itself */
988 }
989 else
990 {
991 qfp->qf_prev = *prevp;
992 (*prevp)->qf_next = qfp;
993 }
994 qfp->qf_next = qfp; /* last element points to itself */
995 qfp->qf_cleared = FALSE;
996 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000997 ++qi->qf_lists[qi->qf_curlist].qf_count;
998 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
999 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001001 qi->qf_lists[qi->qf_curlist].qf_index =
1002 qi->qf_lists[qi->qf_curlist].qf_count;
1003 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 }
1005
1006 return OK;
1007}
1008
1009/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001010 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001011 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001012 static qf_info_T *
1013ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001014{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001015 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001016
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001017 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1018 if (qi != NULL)
1019 {
1020 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1021 qi->qf_refcount++;
1022 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001023
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001024 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001025}
1026
1027/*
1028 * Return the location list for window 'wp'.
1029 * If not present, allocate a location list
1030 */
1031 static qf_info_T *
1032ll_get_or_alloc_list(wp)
1033 win_T *wp;
1034{
1035 if (IS_LL_WINDOW(wp))
1036 /* For a location list window, use the referenced location list */
1037 return wp->w_llist_ref;
1038
1039 /*
1040 * For a non-location list window, w_llist_ref should not point to a
1041 * location list.
1042 */
1043 ll_free_all(&wp->w_llist_ref);
1044
1045 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001046 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001047 return wp->w_llist;
1048}
1049
1050/*
1051 * Copy the location list from window "from" to window "to".
1052 */
1053 void
1054copy_loclist(from, to)
1055 win_T *from;
1056 win_T *to;
1057{
1058 qf_info_T *qi;
1059 int idx;
1060 int i;
1061
1062 /*
1063 * When copying from a location list window, copy the referenced
1064 * location list. For other windows, copy the location list for
1065 * that window.
1066 */
1067 if (IS_LL_WINDOW(from))
1068 qi = from->w_llist_ref;
1069 else
1070 qi = from->w_llist;
1071
1072 if (qi == NULL) /* no location list to copy */
1073 return;
1074
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001075 /* allocate a new location list */
1076 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001077 return;
1078
1079 to->w_llist->qf_listcount = qi->qf_listcount;
1080
1081 /* Copy the location lists one at a time */
1082 for (idx = 0; idx < qi->qf_listcount; idx++)
1083 {
1084 qf_list_T *from_qfl;
1085 qf_list_T *to_qfl;
1086
1087 to->w_llist->qf_curlist = idx;
1088
1089 from_qfl = &qi->qf_lists[idx];
1090 to_qfl = &to->w_llist->qf_lists[idx];
1091
1092 /* Some of the fields are populated by qf_add_entry() */
1093 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1094 to_qfl->qf_count = 0;
1095 to_qfl->qf_index = 0;
1096 to_qfl->qf_start = NULL;
1097 to_qfl->qf_ptr = NULL;
1098
1099 if (from_qfl->qf_count)
1100 {
1101 qfline_T *from_qfp;
1102 qfline_T *prevp = NULL;
1103
1104 /* copy all the location entries in this list */
1105 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1106 ++i, from_qfp = from_qfp->qf_next)
1107 {
1108 if (qf_add_entry(to->w_llist, &prevp,
1109 NULL,
1110 NULL,
1111 from_qfp->qf_text,
1112 from_qfp->qf_lnum,
1113 from_qfp->qf_col,
1114 from_qfp->qf_viscol,
1115 from_qfp->qf_pattern,
1116 from_qfp->qf_nr,
1117 0,
1118 from_qfp->qf_valid) == FAIL)
1119 {
1120 qf_free_all(to);
1121 return;
1122 }
1123 /*
1124 * qf_add_entry() will not set the qf_num field, as the
1125 * directory and file names are not supplied. So the qf_fnum
1126 * field is copied here.
1127 */
1128 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1129 prevp->qf_type = from_qfp->qf_type; /* error type */
1130 if (from_qfl->qf_ptr == from_qfp)
1131 to_qfl->qf_ptr = prevp; /* current location */
1132 }
1133 }
1134
1135 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1136
1137 /* When no valid entries are present in the list, qf_ptr points to
1138 * the first item in the list */
1139 if (to_qfl->qf_nonevalid == TRUE)
1140 to_qfl->qf_ptr = to_qfl->qf_start;
1141 }
1142
1143 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1144}
1145
1146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 * get buffer number for file "dir.name"
1148 */
1149 static int
1150qf_get_fnum(directory, fname)
1151 char_u *directory;
1152 char_u *fname;
1153{
1154 if (fname == NULL || *fname == NUL) /* no file name */
1155 return 0;
1156 {
1157#ifdef RISCOS
1158 /* Name is reported as `main.c', but file is `c.main' */
1159 return ro_buflist_add(fname);
1160#else
1161 char_u *ptr;
1162 int fnum;
1163
1164# ifdef VMS
1165 vms_remove_version(fname);
1166# endif
1167# ifdef BACKSLASH_IN_FILENAME
1168 if (directory != NULL)
1169 slash_adjust(directory);
1170 slash_adjust(fname);
1171# endif
1172 if (directory != NULL && !vim_isAbsName(fname)
1173 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1174 {
1175 /*
1176 * Here we check if the file really exists.
1177 * This should normally be true, but if make works without
1178 * "leaving directory"-messages we might have missed a
1179 * directory change.
1180 */
1181 if (mch_getperm(ptr) < 0)
1182 {
1183 vim_free(ptr);
1184 directory = qf_guess_filepath(fname);
1185 if (directory)
1186 ptr = concat_fnames(directory, fname, TRUE);
1187 else
1188 ptr = vim_strsave(fname);
1189 }
1190 /* Use concatenated directory name and file name */
1191 fnum = buflist_add(ptr, 0);
1192 vim_free(ptr);
1193 return fnum;
1194 }
1195 return buflist_add(fname, 0);
1196#endif
1197 }
1198}
1199
1200/*
1201 * push dirbuf onto the directory stack and return pointer to actual dir or
1202 * NULL on error
1203 */
1204 static char_u *
1205qf_push_dir(dirbuf, stackptr)
1206 char_u *dirbuf;
1207 struct dir_stack_T **stackptr;
1208{
1209 struct dir_stack_T *ds_new;
1210 struct dir_stack_T *ds_ptr;
1211
1212 /* allocate new stack element and hook it in */
1213 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1214 if (ds_new == NULL)
1215 return NULL;
1216
1217 ds_new->next = *stackptr;
1218 *stackptr = ds_new;
1219
1220 /* store directory on the stack */
1221 if (vim_isAbsName(dirbuf)
1222 || (*stackptr)->next == NULL
1223 || (*stackptr && dir_stack != *stackptr))
1224 (*stackptr)->dirname = vim_strsave(dirbuf);
1225 else
1226 {
1227 /* Okay we don't have an absolute path.
1228 * dirbuf must be a subdir of one of the directories on the stack.
1229 * Let's search...
1230 */
1231 ds_new = (*stackptr)->next;
1232 (*stackptr)->dirname = NULL;
1233 while (ds_new)
1234 {
1235 vim_free((*stackptr)->dirname);
1236 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1237 TRUE);
1238 if (mch_isdir((*stackptr)->dirname) == TRUE)
1239 break;
1240
1241 ds_new = ds_new->next;
1242 }
1243
1244 /* clean up all dirs we already left */
1245 while ((*stackptr)->next != ds_new)
1246 {
1247 ds_ptr = (*stackptr)->next;
1248 (*stackptr)->next = (*stackptr)->next->next;
1249 vim_free(ds_ptr->dirname);
1250 vim_free(ds_ptr);
1251 }
1252
1253 /* Nothing found -> it must be on top level */
1254 if (ds_new == NULL)
1255 {
1256 vim_free((*stackptr)->dirname);
1257 (*stackptr)->dirname = vim_strsave(dirbuf);
1258 }
1259 }
1260
1261 if ((*stackptr)->dirname != NULL)
1262 return (*stackptr)->dirname;
1263 else
1264 {
1265 ds_ptr = *stackptr;
1266 *stackptr = (*stackptr)->next;
1267 vim_free(ds_ptr);
1268 return NULL;
1269 }
1270}
1271
1272
1273/*
1274 * pop dirbuf from the directory stack and return previous directory or NULL if
1275 * stack is empty
1276 */
1277 static char_u *
1278qf_pop_dir(stackptr)
1279 struct dir_stack_T **stackptr;
1280{
1281 struct dir_stack_T *ds_ptr;
1282
1283 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1284 * What to do if it isn't? */
1285
1286 /* pop top element and free it */
1287 if (*stackptr != NULL)
1288 {
1289 ds_ptr = *stackptr;
1290 *stackptr = (*stackptr)->next;
1291 vim_free(ds_ptr->dirname);
1292 vim_free(ds_ptr);
1293 }
1294
1295 /* return NEW top element as current dir or NULL if stack is empty*/
1296 return *stackptr ? (*stackptr)->dirname : NULL;
1297}
1298
1299/*
1300 * clean up directory stack
1301 */
1302 static void
1303qf_clean_dir_stack(stackptr)
1304 struct dir_stack_T **stackptr;
1305{
1306 struct dir_stack_T *ds_ptr;
1307
1308 while ((ds_ptr = *stackptr) != NULL)
1309 {
1310 *stackptr = (*stackptr)->next;
1311 vim_free(ds_ptr->dirname);
1312 vim_free(ds_ptr);
1313 }
1314}
1315
1316/*
1317 * Check in which directory of the directory stack the given file can be
1318 * found.
1319 * Returns a pointer to the directory name or NULL if not found
1320 * Cleans up intermediate directory entries.
1321 *
1322 * TODO: How to solve the following problem?
1323 * If we have the this directory tree:
1324 * ./
1325 * ./aa
1326 * ./aa/bb
1327 * ./bb
1328 * ./bb/x.c
1329 * and make says:
1330 * making all in aa
1331 * making all in bb
1332 * x.c:9: Error
1333 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1334 * qf_guess_filepath will return NULL.
1335 */
1336 static char_u *
1337qf_guess_filepath(filename)
1338 char_u *filename;
1339{
1340 struct dir_stack_T *ds_ptr;
1341 struct dir_stack_T *ds_tmp;
1342 char_u *fullname;
1343
1344 /* no dirs on the stack - there's nothing we can do */
1345 if (dir_stack == NULL)
1346 return NULL;
1347
1348 ds_ptr = dir_stack->next;
1349 fullname = NULL;
1350 while (ds_ptr)
1351 {
1352 vim_free(fullname);
1353 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1354
1355 /* If concat_fnames failed, just go on. The worst thing that can happen
1356 * is that we delete the entire stack.
1357 */
1358 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1359 break;
1360
1361 ds_ptr = ds_ptr->next;
1362 }
1363
1364 vim_free(fullname);
1365
1366 /* clean up all dirs we already left */
1367 while (dir_stack->next != ds_ptr)
1368 {
1369 ds_tmp = dir_stack->next;
1370 dir_stack->next = dir_stack->next->next;
1371 vim_free(ds_tmp->dirname);
1372 vim_free(ds_tmp);
1373 }
1374
1375 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1376
1377}
1378
1379/*
1380 * jump to a quickfix line
1381 * if dir == FORWARD go "errornr" valid entries forward
1382 * if dir == BACKWARD go "errornr" valid entries backward
1383 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1384 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1385 * else if "errornr" is zero, redisplay the same line
1386 * else go to entry "errornr"
1387 */
1388 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001389qf_jump(qi, dir, errornr, forceit)
1390 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 int dir;
1392 int errornr;
1393 int forceit;
1394{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001395 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001396 qfline_T *qf_ptr;
1397 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 int qf_index;
1399 int old_qf_fnum;
1400 int old_qf_index;
1401 int prev_index;
1402 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1403 char_u *err = e_no_more_items;
1404 linenr_T i;
1405 buf_T *old_curbuf;
1406 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 colnr_T screen_col;
1408 colnr_T char_col;
1409 char_u *line;
1410#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001411 char_u *old_swb = p_swb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 int opened_window = FALSE;
1413 win_T *win;
1414 win_T *altwin;
1415#endif
1416 int print_message = TRUE;
1417 int len;
1418#ifdef FEAT_FOLDING
1419 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1420#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001421 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001422 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001424 if (qi == NULL)
1425 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426
1427 if (qi->qf_curlist >= qi->qf_listcount
1428 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
1430 EMSG(_(e_quickfix));
1431 return;
1432 }
1433
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001434 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001436 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 old_qf_index = qf_index;
1438 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1439 {
1440 while (errornr--)
1441 {
1442 old_qf_ptr = qf_ptr;
1443 prev_index = qf_index;
1444 old_qf_fnum = qf_ptr->qf_fnum;
1445 do
1446 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001447 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 || qf_ptr->qf_next == NULL)
1449 {
1450 qf_ptr = old_qf_ptr;
1451 qf_index = prev_index;
1452 if (err != NULL)
1453 {
1454 EMSG(_(err));
1455 goto theend;
1456 }
1457 errornr = 0;
1458 break;
1459 }
1460 ++qf_index;
1461 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001462 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1463 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1465 err = NULL;
1466 }
1467 }
1468 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1469 {
1470 while (errornr--)
1471 {
1472 old_qf_ptr = qf_ptr;
1473 prev_index = qf_index;
1474 old_qf_fnum = qf_ptr->qf_fnum;
1475 do
1476 {
1477 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1478 {
1479 qf_ptr = old_qf_ptr;
1480 qf_index = prev_index;
1481 if (err != NULL)
1482 {
1483 EMSG(_(err));
1484 goto theend;
1485 }
1486 errornr = 0;
1487 break;
1488 }
1489 --qf_index;
1490 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001491 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1492 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1494 err = NULL;
1495 }
1496 }
1497 else if (errornr != 0) /* go to specified number */
1498 {
1499 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1500 {
1501 --qf_index;
1502 qf_ptr = qf_ptr->qf_prev;
1503 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001504 while (errornr > qf_index && qf_index <
1505 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 && qf_ptr->qf_next != NULL)
1507 {
1508 ++qf_index;
1509 qf_ptr = qf_ptr->qf_next;
1510 }
1511 }
1512
1513#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001514 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1515 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 /* No need to print the error message if it's visible in the error
1517 * window */
1518 print_message = FALSE;
1519
1520 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001521 * For ":helpgrep" find a help window or open one.
1522 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001523 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001524 {
1525 win_T *wp;
1526 int n;
1527
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001528 if (cmdmod.tab != 0)
1529 wp = NULL;
1530 else
1531 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1532 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1533 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001534 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1535 win_enter(wp, TRUE);
1536 else
1537 {
1538 /*
1539 * Split off help window; put it at far top if no position
1540 * specified, the current window is vertically split and narrow.
1541 */
1542 n = WSP_HELP;
1543# ifdef FEAT_VERTSPLIT
1544 if (cmdmod.split == 0 && curwin->w_width != Columns
1545 && curwin->w_width < 80)
1546 n |= WSP_TOP;
1547# endif
1548 if (win_split(0, n) == FAIL)
1549 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001550 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001551
1552 if (curwin->w_height < p_hh)
1553 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001554
1555 if (qi != &ql_info) /* not a quickfix list */
1556 {
1557 /* The new window should use the supplied location list */
1558 qf_free_all(curwin);
1559 curwin->w_llist = qi;
1560 qi->qf_refcount++;
1561 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001562 }
1563
1564 if (!p_im)
1565 restart_edit = 0; /* don't want insert mode in help file */
1566 }
1567
1568 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 * If currently in the quickfix window, find another window to show the
1570 * file in.
1571 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001572 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 /*
1575 * If there is no file specified, we don't know where to go.
1576 * But do advance, otherwise ":cn" gets stuck.
1577 */
1578 if (qf_ptr->qf_fnum == 0)
1579 goto theend;
1580
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001581 /* Locate a window showing a normal buffer */
1582 usable_win = 0;
1583 FOR_ALL_WINDOWS(win)
1584 if (win->w_buffer->b_p_bt[0] == NUL)
1585 {
1586 usable_win = 1;
1587 break;
1588 }
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 /*
1591 * If there is only one window, create a new one above the quickfix
1592 * window.
1593 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001594 if (firstwin == lastwin || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001596 ll_ref = curwin->w_llist_ref;
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (win_split(0, WSP_ABOVE) == FAIL)
1599 goto failed; /* not enough room for window */
1600 opened_window = TRUE; /* close it when fail */
1601 p_swb = empty_option; /* don't split again */
1602# ifdef FEAT_SCROLLBIND
1603 curwin->w_p_scb = FALSE;
1604# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001605 if (ll_ref != NULL)
1606 {
1607 /* The new window should use the location list from the
1608 * location list window */
1609 qf_free_all(curwin);
1610 curwin->w_llist = ll_ref;
1611 ll_ref->qf_refcount++;
1612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001616 if (curwin->w_llist_ref != NULL)
1617 {
1618 /* In a location window */
1619 ll_ref = curwin->w_llist_ref;
1620
1621 /* Find the window with the same location list */
1622 FOR_ALL_WINDOWS(win)
1623 if (win->w_llist == ll_ref)
1624 break;
1625 if (win == NULL)
1626 {
1627 /* Find the window showing the selected file */
1628 FOR_ALL_WINDOWS(win)
1629 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1630 break;
1631 if (win == NULL)
1632 {
1633 /* Find a previous usable window */
1634 win = curwin;
1635 do
1636 {
1637 if (win->w_buffer->b_p_bt[0] == NUL)
1638 break;
1639 if (win->w_prev == NULL)
1640 win = lastwin; /* wrap around the top */
1641 else
1642 win = win->w_prev; /* go to previous window */
1643 } while (win != curwin);
1644 }
1645 }
1646 win_goto(win);
1647
1648 /* If the location list for the window is not set, then set it
1649 * to the location list from the location window */
1650 if (win->w_llist == NULL)
1651 {
1652 win->w_llist = ll_ref;
1653 ll_ref->qf_refcount++;
1654 }
1655 }
1656 else
1657 {
1658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 /*
1660 * Try to find a window that shows the right buffer.
1661 * Default to the window just above the quickfix buffer.
1662 */
1663 win = curwin;
1664 altwin = NULL;
1665 for (;;)
1666 {
1667 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1668 break;
1669 if (win->w_prev == NULL)
1670 win = lastwin; /* wrap around the top */
1671 else
1672 win = win->w_prev; /* go to previous window */
1673
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001674 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 {
1676 /* Didn't find it, go to the window before the quickfix
1677 * window. */
1678 if (altwin != NULL)
1679 win = altwin;
1680 else if (curwin->w_prev != NULL)
1681 win = curwin->w_prev;
1682 else
1683 win = curwin->w_next;
1684 break;
1685 }
1686
1687 /* Remember a usable window. */
1688 if (altwin == NULL && !win->w_p_pvw
1689 && win->w_buffer->b_p_bt[0] == NUL)
1690 altwin = win;
1691 }
1692
1693 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
1696 }
1697#endif
1698
1699 /*
1700 * If there is a file name,
1701 * read the wanted file if needed, and check autowrite etc.
1702 */
1703 old_curbuf = curbuf;
1704 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001705
1706 if (qf_ptr->qf_fnum != 0)
1707 {
1708 if (qf_ptr->qf_type == 1)
1709 {
1710 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1711 * set b_p_ro flag). */
1712 if (!can_abandon(curbuf, forceit))
1713 {
1714 EMSG(_(e_nowrtmsg));
1715 ok = FALSE;
1716 }
1717 else
1718 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1719 ECMD_HIDE + ECMD_SET_HELP);
1720 }
1721 else
1722 ok = buflist_getfile(qf_ptr->qf_fnum,
1723 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1724 }
1725
1726 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
1728 /* When not switched to another buffer, still need to set pc mark */
1729 if (curbuf == old_curbuf)
1730 setpcmark();
1731
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001732 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001734 /*
1735 * Go to line with error, unless qf_lnum is 0.
1736 */
1737 i = qf_ptr->qf_lnum;
1738 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001740 if (i > curbuf->b_ml.ml_line_count)
1741 i = curbuf->b_ml.ml_line_count;
1742 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001744 if (qf_ptr->qf_col > 0)
1745 {
1746 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1747 if (qf_ptr->qf_viscol == TRUE)
1748 {
1749 /*
1750 * Check each character from the beginning of the error
1751 * line up to the error column. For each tab character
1752 * found, reduce the error column value by the length of
1753 * a tab character.
1754 */
1755 line = ml_get_curline();
1756 screen_col = 0;
1757 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1758 {
1759 if (*line == NUL)
1760 break;
1761 if (*line++ == '\t')
1762 {
1763 curwin->w_cursor.col -= 7 - (screen_col % 8);
1764 screen_col += 8 - (screen_col % 8);
1765 }
1766 else
1767 ++screen_col;
1768 }
1769 }
1770 check_cursor();
1771 }
1772 else
1773 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001776 {
1777 pos_T save_cursor;
1778
1779 /* Move the cursor to the first line in the buffer */
1780 save_cursor = curwin->w_cursor;
1781 curwin->w_cursor.lnum = 0;
1782 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1783 curwin->w_cursor = save_cursor;
1784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
1786#ifdef FEAT_FOLDING
1787 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1788 foldOpenCursor();
1789#endif
1790 if (print_message)
1791 {
1792 /* Update the screen before showing the message */
1793 update_topline_redraw();
1794 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001795 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1797 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1798 /* Add the message, skipping leading whitespace and newlines. */
1799 len = (int)STRLEN(IObuff);
1800 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1801
1802 /* Output the message. Overwrite to avoid scrolling when the 'O'
1803 * flag is present in 'shortmess'; But when not jumping, print the
1804 * whole message. */
1805 i = msg_scroll;
1806 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1807 msg_scroll = TRUE;
1808 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1809 msg_scroll = FALSE;
1810 msg_attr_keep(IObuff, 0, TRUE);
1811 msg_scroll = i;
1812 }
1813 }
1814 else
1815 {
1816#ifdef FEAT_WINDOWS
1817 if (opened_window)
1818 win_close(curwin, TRUE); /* Close opened window */
1819#endif
1820 if (qf_ptr->qf_fnum != 0)
1821 {
1822 /*
1823 * Couldn't open file, so put index back where it was. This could
1824 * happen if the file was readonly and we changed something.
1825 */
1826#ifdef FEAT_WINDOWS
1827failed:
1828#endif
1829 qf_ptr = old_qf_ptr;
1830 qf_index = old_qf_index;
1831 }
1832 }
1833theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001834 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1835 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836#ifdef FEAT_WINDOWS
1837 if (p_swb != old_swb && opened_window)
1838 {
1839 /* Restore old 'switchbuf' value, but not when an autocommand or
1840 * modeline has changed the value. */
1841 if (p_swb == empty_option)
1842 p_swb = old_swb;
1843 else
1844 free_string_option(old_swb);
1845 }
1846#endif
1847}
1848
1849/*
1850 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001851 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 */
1853 void
1854qf_list(eap)
1855 exarg_T *eap;
1856{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001857 buf_T *buf;
1858 char_u *fname;
1859 qfline_T *qfp;
1860 int i;
1861 int idx1 = 1;
1862 int idx2 = -1;
1863 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001864 char_u *arg = eap->arg;
1865 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001867 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869 if (eap->cmdidx == CMD_llist)
1870 {
1871 qi = GET_LOC_LIST(curwin);
1872 if (qi == NULL)
1873 {
1874 EMSG(_(e_loclist));
1875 return;
1876 }
1877 }
1878
1879 if (qi->qf_curlist >= qi->qf_listcount
1880 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
1882 EMSG(_(e_quickfix));
1883 return;
1884 }
1885 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1886 {
1887 EMSG(_(e_trailing));
1888 return;
1889 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001890 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (idx1 < 0)
1892 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1893 if (idx2 < 0)
1894 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1895
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001896 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1899 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1902 {
1903 if (need_return)
1904 {
1905 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001906 if (got_int)
1907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 need_return = FALSE;
1909 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001910
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001911 fname = NULL;
1912 if (qfp->qf_fnum != 0
1913 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1914 {
1915 fname = buf->b_fname;
1916 if (qfp->qf_type == 1) /* :helpgrep */
1917 fname = gettail(fname);
1918 }
1919 if (fname == NULL)
1920 sprintf((char *)IObuff, "%2d", i);
1921 else
1922 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1923 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001924 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001925 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1926 if (qfp->qf_lnum == 0)
1927 IObuff[0] = NUL;
1928 else if (qfp->qf_col == 0)
1929 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1930 else
1931 sprintf((char *)IObuff, ":%ld col %d",
1932 qfp->qf_lnum, qfp->qf_col);
1933 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1934 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1935 msg_puts_attr(IObuff, hl_attr(HLF_N));
1936 if (qfp->qf_pattern != NULL)
1937 {
1938 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1939 STRCAT(IObuff, ":");
1940 msg_puts(IObuff);
1941 }
1942 msg_puts((char_u *)" ");
1943
1944 /* Remove newlines and leading whitespace from the text. For an
1945 * unrecognized line keep the indent, the compiler may mark a word
1946 * with ^^^^. */
1947 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1949 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001950 msg_prt_line(IObuff, FALSE);
1951 out_flush(); /* show one line at a time */
1952 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001954
1955 qfp = qfp->qf_next;
1956 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 ui_breakcheck();
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959}
1960
1961/*
1962 * Remove newlines and leading whitespace from an error message.
1963 * Put the result in "buf[bufsize]".
1964 */
1965 static void
1966qf_fmt_text(text, buf, bufsize)
1967 char_u *text;
1968 char_u *buf;
1969 int bufsize;
1970{
1971 int i;
1972 char_u *p = text;
1973
1974 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1975 {
1976 if (*p == '\n')
1977 {
1978 buf[i] = ' ';
1979 while (*++p != NUL)
1980 if (!vim_iswhite(*p) && *p != '\n')
1981 break;
1982 }
1983 else
1984 buf[i] = *p++;
1985 }
1986 buf[i] = NUL;
1987}
1988
1989/*
1990 * ":colder [count]": Up in the quickfix stack.
1991 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001992 * ":lolder [count]": Up in the location list stack.
1993 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 */
1995 void
1996qf_age(eap)
1997 exarg_T *eap;
1998{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001999 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 int count;
2001
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002002 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2003 {
2004 qi = GET_LOC_LIST(curwin);
2005 if (qi == NULL)
2006 {
2007 EMSG(_(e_loclist));
2008 return;
2009 }
2010 }
2011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (eap->addr_count != 0)
2013 count = eap->line2;
2014 else
2015 count = 1;
2016 while (count--)
2017 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002018 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002020 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 EMSG(_("E380: At bottom of quickfix stack"));
2023 return;
2024 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027 else
2028 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002029 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
2031 EMSG(_("E381: At top of quickfix stack"));
2032 return;
2033 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002034 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002037 qf_msg(qi);
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039}
2040
2041 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002042qf_msg(qi)
2043 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002046 qi->qf_curlist + 1, qi->qf_listcount,
2047 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002049 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050#endif
2051}
2052
2053/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002054 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 */
2056 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002057qf_free(qi, idx)
2058 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 int idx;
2060{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002061 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002063 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002065 qfp = qi->qf_lists[idx].qf_start->qf_next;
2066 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2067 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2068 vim_free(qi->qf_lists[idx].qf_start);
2069 qi->qf_lists[idx].qf_start = qfp;
2070 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072}
2073
2074/*
2075 * qf_mark_adjust: adjust marks
2076 */
2077 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002078qf_mark_adjust(wp, line1, line2, amount, amount_after)
2079 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 linenr_T line1;
2081 linenr_T line2;
2082 long amount;
2083 long amount_after;
2084{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002085 int i;
2086 qfline_T *qfp;
2087 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 if (wp != NULL)
2091 {
2092 if (wp->w_llist == NULL)
2093 return;
2094 qi = wp->w_llist;
2095 }
2096
2097 for (idx = 0; idx < qi->qf_listcount; ++idx)
2098 if (qi->qf_lists[idx].qf_count)
2099 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2100 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (qfp->qf_fnum == curbuf->b_fnum)
2102 {
2103 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2104 {
2105 if (amount == MAXLNUM)
2106 qfp->qf_cleared = TRUE;
2107 else
2108 qfp->qf_lnum += amount;
2109 }
2110 else if (amount_after && qfp->qf_lnum > line2)
2111 qfp->qf_lnum += amount_after;
2112 }
2113}
2114
2115/*
2116 * Make a nice message out of the error character and the error number:
2117 * char number message
2118 * e or E 0 " error"
2119 * w or W 0 " warning"
2120 * i or I 0 " info"
2121 * 0 0 ""
2122 * other 0 " c"
2123 * e or E n " error n"
2124 * w or W n " warning n"
2125 * i or I n " info n"
2126 * 0 n " error n"
2127 * other n " c n"
2128 * 1 x "" :helpgrep
2129 */
2130 static char_u *
2131qf_types(c, nr)
2132 int c, nr;
2133{
2134 static char_u buf[20];
2135 static char_u cc[3];
2136 char_u *p;
2137
2138 if (c == 'W' || c == 'w')
2139 p = (char_u *)" warning";
2140 else if (c == 'I' || c == 'i')
2141 p = (char_u *)" info";
2142 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2143 p = (char_u *)" error";
2144 else if (c == 0 || c == 1)
2145 p = (char_u *)"";
2146 else
2147 {
2148 cc[0] = ' ';
2149 cc[1] = c;
2150 cc[2] = NUL;
2151 p = cc;
2152 }
2153
2154 if (nr <= 0)
2155 return p;
2156
2157 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2158 return buf;
2159}
2160
2161#if defined(FEAT_WINDOWS) || defined(PROTO)
2162/*
2163 * ":cwindow": open the quickfix window if we have errors to display,
2164 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002165 * ":lwindow": open the location list window if we have locations to display,
2166 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
2168 void
2169ex_cwindow(eap)
2170 exarg_T *eap;
2171{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002172 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 win_T *win;
2174
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002175 if (eap->cmdidx == CMD_lwindow)
2176 {
2177 qi = GET_LOC_LIST(curwin);
2178 if (qi == NULL)
2179 return;
2180 }
2181
2182 /* Look for an existing quickfix window. */
2183 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185 /*
2186 * If a quickfix window is open but we have no errors to display,
2187 * close the window. If a quickfix window is not open, then open
2188 * it if we have errors; otherwise, leave it closed.
2189 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard68071d2006-05-02 22:08:30 +00002191 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 if (win != NULL)
2194 ex_cclose(eap);
2195 }
2196 else if (win == NULL)
2197 ex_copen(eap);
2198}
2199
2200/*
2201 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002202 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 */
2204/*ARGSUSED*/
2205 void
2206ex_cclose(eap)
2207 exarg_T *eap;
2208{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002209 win_T *win = NULL;
2210 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002212 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2213 {
2214 qi = GET_LOC_LIST(curwin);
2215 if (qi == NULL)
2216 return;
2217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002219 /* Find existing quickfix window and close it. */
2220 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (win != NULL)
2222 win_close(win, FALSE);
2223}
2224
2225/*
2226 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 */
2229 void
2230ex_copen(eap)
2231 exarg_T *eap;
2232{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002236 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002237 buf_T *qf_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002239 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2240 {
2241 qi = GET_LOC_LIST(curwin);
2242 if (qi == NULL)
2243 {
2244 EMSG(_(e_loclist));
2245 return;
2246 }
2247 }
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (eap->addr_count != 0)
2250 height = eap->line2;
2251 else
2252 height = QF_WINHEIGHT;
2253
2254#ifdef FEAT_VISUAL
2255 reset_VIsual_and_resel(); /* stop Visual mode */
2256#endif
2257#ifdef FEAT_GUI
2258 need_mouse_correct = TRUE;
2259#endif
2260
2261 /*
2262 * Find existing quickfix window, or open a new one.
2263 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002264 win = qf_find_win(qi);
2265
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002266 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 win_goto(win);
2268 else
2269 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002270 qf_buf = qf_find_buf(qi);
2271
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 /* The current window becomes the previous window afterwards. */
2273 win = curwin;
2274
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002275 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2276 /* Create the new window at the very bottom. */
2277 win_goto(lastwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (win_split(height, WSP_BELOW) == FAIL)
2279 return; /* not enough room for window */
2280#ifdef FEAT_SCROLLBIND
2281 curwin->w_p_scb = FALSE;
2282#endif
2283
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002284 /* Remove the location list for the quickfix window */
2285 qf_free_all(curwin);
2286
2287 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002289 /*
2290 * For the location list window, create a reference to the
2291 * location list from the window 'win'.
2292 */
2293 curwin->w_llist_ref = win->w_llist;
2294 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002296
Bram Moolenaar9c102382006-05-03 21:26:49 +00002297 if (qf_buf != NULL)
2298 /* Use the existing quickfix buffer */
2299 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
2300 ECMD_HIDE + ECMD_OLDBUF);
2301 else
2302 {
2303 /* Create a new quickfix buffer */
2304 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2305 /* switch off 'swapfile' */
2306 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2307 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002308 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002309 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2310 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
2311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002313 /* Only set the height when still in the same tab page and there is no
2314 * window to the side. */
2315 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002316#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002317 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002318#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002319 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 win_setheight(height);
2321 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2322 if (win_valid(win))
2323 prevwin = win;
2324 }
2325
2326 /*
2327 * Fill the buffer with the quickfix list.
2328 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002331 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 curwin->w_cursor.col = 0;
2333 check_cursor();
2334 update_topline(); /* scroll to show the line */
2335}
2336
2337/*
2338 * Return the number of the current entry (line number in the quickfix
2339 * window).
2340 */
2341 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002342qf_current_entry(wp)
2343 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002345 qf_info_T *qi = &ql_info;
2346
2347 if (IS_LL_WINDOW(wp))
2348 /* In the location list window, use the referenced location list */
2349 qi = wp->w_llist_ref;
2350
2351 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352}
2353
2354/*
2355 * Update the cursor position in the quickfix window to the current error.
2356 * Return TRUE if there is a quickfix window.
2357 */
2358 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002359qf_win_pos_update(qi, old_qf_index)
2360 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 int old_qf_index; /* previous qf_index or zero */
2362{
2363 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002364 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 /*
2367 * Put the cursor on the current error in the quickfix window, so that
2368 * it's viewable.
2369 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002370 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (win != NULL
2372 && qf_index <= win->w_buffer->b_ml.ml_line_count
2373 && old_qf_index != qf_index)
2374 {
2375 win_T *old_curwin = curwin;
2376
2377 curwin = win;
2378 curbuf = win->w_buffer;
2379 if (qf_index > old_qf_index)
2380 {
2381 curwin->w_redraw_top = old_qf_index;
2382 curwin->w_redraw_bot = qf_index;
2383 }
2384 else
2385 {
2386 curwin->w_redraw_top = qf_index;
2387 curwin->w_redraw_bot = old_qf_index;
2388 }
2389 curwin->w_cursor.lnum = qf_index;
2390 curwin->w_cursor.col = 0;
2391 update_topline(); /* scroll to show the line */
2392 redraw_later(VALID);
2393 curwin->w_redr_status = TRUE; /* update ruler */
2394 curwin = old_curwin;
2395 curbuf = curwin->w_buffer;
2396 }
2397 return win != NULL;
2398}
2399
2400/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002401 * Check whether the given window is displaying the specified quickfix/location
2402 * list buffer
2403 */
2404 static int
2405is_qf_win(win, qi)
2406 win_T *win;
2407 qf_info_T *qi;
2408{
2409 /*
2410 * A window displaying the quickfix buffer will have the w_llist_ref field
2411 * set to NULL.
2412 * A window displaying a location list buffer will have the w_llist_ref
2413 * pointing to the location list.
2414 */
2415 if (bt_quickfix(win->w_buffer))
2416 if ((qi == &ql_info && win->w_llist_ref == NULL)
2417 || (qi != &ql_info && win->w_llist_ref == qi))
2418 return TRUE;
2419
2420 return FALSE;
2421}
2422
2423/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002424 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002425 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426 */
2427 static win_T *
2428qf_find_win(qi)
2429 qf_info_T *qi;
2430{
2431 win_T *win;
2432
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002434 if (is_qf_win(win, qi))
2435 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002436
2437 return win;
2438}
2439
2440/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002441 * Find a quickfix buffer.
2442 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 */
2444 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002445qf_find_buf(qi)
2446 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002448 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002449 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar9c102382006-05-03 21:26:49 +00002451 FOR_ALL_TAB_WINDOWS(tp, win)
2452 if (is_qf_win(win, qi))
2453 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002454
Bram Moolenaar9c102382006-05-03 21:26:49 +00002455 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456}
2457
2458/*
2459 * Find the quickfix buffer. If it exists, update the contents.
2460 */
2461 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002462qf_update_buffer(qi)
2463 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002469 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 if (buf != NULL)
2471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 /* set curwin/curbuf to buf and save a few things */
2473 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002475 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 /* restore curwin/curbuf and a few other things */
2478 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002480 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482}
2483
2484/*
2485 * Fill current buffer with quickfix errors, replacing any previous contents.
2486 * curbuf must be the quickfix buffer!
2487 */
2488 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002489qf_fill_buffer(qi)
2490 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002492 linenr_T lnum;
2493 qfline_T *qfp;
2494 buf_T *errbuf;
2495 int len;
2496 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 /* delete all existing lines */
2499 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2500 (void)ml_delete((linenr_T)1, FALSE);
2501
2502 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002503 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002506 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2507 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 if (qfp->qf_fnum != 0
2510 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2511 && errbuf->b_fname != NULL)
2512 {
2513 if (qfp->qf_type == 1) /* :helpgrep */
2514 STRCPY(IObuff, gettail(errbuf->b_fname));
2515 else
2516 STRCPY(IObuff, errbuf->b_fname);
2517 len = (int)STRLEN(IObuff);
2518 }
2519 else
2520 len = 0;
2521 IObuff[len++] = '|';
2522
2523 if (qfp->qf_lnum > 0)
2524 {
2525 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2526 len += (int)STRLEN(IObuff + len);
2527
2528 if (qfp->qf_col > 0)
2529 {
2530 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2531 len += (int)STRLEN(IObuff + len);
2532 }
2533
2534 sprintf((char *)IObuff + len, "%s",
2535 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2536 len += (int)STRLEN(IObuff + len);
2537 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002538 else if (qfp->qf_pattern != NULL)
2539 {
2540 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2541 len += (int)STRLEN(IObuff + len);
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 IObuff[len++] = '|';
2544 IObuff[len++] = ' ';
2545
2546 /* Remove newlines and leading whitespace from the text.
2547 * For an unrecognized line keep the indent, the compiler may
2548 * mark a word with ^^^^. */
2549 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2550 IObuff + len, IOSIZE - len);
2551
2552 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2553 == FAIL)
2554 break;
2555 qfp = qfp->qf_next;
2556 }
2557 /* Delete the empty line which is now at the end */
2558 (void)ml_delete(lnum + 1, FALSE);
2559 }
2560
2561 /* correct cursor position */
2562 check_lnums(TRUE);
2563
2564 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2565 * resembles reading a file into a buffer, it's more logical when using
2566 * autocommands. */
2567 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2568 curbuf->b_p_ma = FALSE;
2569
2570#ifdef FEAT_AUTOCMD
2571 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2572 FALSE, curbuf);
2573 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2574 FALSE, curbuf);
2575#endif
2576
2577 /* make sure it will be redrawn */
2578 redraw_curbuf_later(NOT_VALID);
2579
2580 /* Restore KeyTyped, setting 'filetype' may reset it. */
2581 KeyTyped = old_KeyTyped;
2582}
2583
2584#endif /* FEAT_WINDOWS */
2585
2586/*
2587 * Return TRUE if "buf" is the quickfix buffer.
2588 */
2589 int
2590bt_quickfix(buf)
2591 buf_T *buf;
2592{
2593 return (buf->b_p_bt[0] == 'q');
2594}
2595
2596/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002597 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2598 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 */
2600 int
2601bt_nofile(buf)
2602 buf_T *buf;
2603{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002604 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2605 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606}
2607
2608/*
2609 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2610 */
2611 int
2612bt_dontwrite(buf)
2613 buf_T *buf;
2614{
2615 return (buf->b_p_bt[0] == 'n');
2616}
2617
2618 int
2619bt_dontwrite_msg(buf)
2620 buf_T *buf;
2621{
2622 if (bt_dontwrite(buf))
2623 {
2624 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2625 return TRUE;
2626 }
2627 return FALSE;
2628}
2629
2630/*
2631 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2632 * and 'bufhidden'.
2633 */
2634 int
2635buf_hide(buf)
2636 buf_T *buf;
2637{
2638 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2639 switch (buf->b_p_bh[0])
2640 {
2641 case 'u': /* "unload" */
2642 case 'w': /* "wipe" */
2643 case 'd': return FALSE; /* "delete" */
2644 case 'h': return TRUE; /* "hide" */
2645 }
2646 return (p_hid || cmdmod.hide);
2647}
2648
2649/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002650 * Return TRUE when using ":vimgrep" for ":grep".
2651 */
2652 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002653grep_internal(cmdidx)
2654 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002655{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002656 return ((cmdidx == CMD_grep
2657 || cmdidx == CMD_lgrep
2658 || cmdidx == CMD_grepadd
2659 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002660 && STRCMP("internal",
2661 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2662}
2663
2664/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002665 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 */
2667 void
2668ex_make(eap)
2669 exarg_T *eap;
2670{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002671 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 char_u *cmd;
2673 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002674 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002675 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002676 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002677#ifdef FEAT_AUTOCMD
2678 char_u *au_name = NULL;
2679
2680 switch (eap->cmdidx)
2681 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002682 case CMD_make: au_name = (char_u *)"make"; break;
2683 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2684 case CMD_grep: au_name = (char_u *)"grep"; break;
2685 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2686 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2687 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002688 default: break;
2689 }
2690 if (au_name != NULL)
2691 {
2692 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2693 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002694# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002695 if (did_throw || force_abort)
2696 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002697# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002698 }
2699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaar86b68352004-12-27 21:59:20 +00002701 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002702 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002703 {
2704 ex_vimgrep(eap);
2705 return;
2706 }
2707
Bram Moolenaara6557602006-02-04 22:43:20 +00002708 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2709 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002710 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002713 fname = get_mef_name();
2714 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002716 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 /*
2719 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2720 */
2721 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2722 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002723 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 cmd = alloc(len);
2725 if (cmd == NULL)
2726 return;
2727 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2728 (char *)p_shq);
2729 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002730 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 /*
2732 * Output a newline if there's something else than the :make command that
2733 * was typed (in which case the cursor is in column 0).
2734 */
2735 if (msg_col == 0)
2736 msg_didout = FALSE;
2737 msg_start();
2738 MSG_PUTS(":!");
2739 msg_outtrans(cmd); /* show what we are doing */
2740
2741 /* let the shell know if we are redirecting output or not */
2742 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2743
2744#ifdef AMIGA
2745 out_flush();
2746 /* read window status report and redraw before message */
2747 (void)char_avail();
2748#endif
2749
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002750 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002751 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2752 (eap->cmdidx != CMD_grepadd
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002753 && eap->cmdidx != CMD_lgrepadd));
2754#ifdef FEAT_AUTOCMD
2755 if (au_name != NULL)
2756 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2757 curbuf->b_fname, TRUE, curbuf);
2758#endif
2759 if (res > 0 && !eap->forceit)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002760 {
2761 if (wp != NULL)
2762 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002763 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar7c626922005-02-07 22:01:03 +00002766 mch_remove(fname);
2767 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 vim_free(cmd);
2769}
2770
2771/*
2772 * Return the name for the errorfile, in allocated memory.
2773 * Find a new unique name when 'makeef' contains "##".
2774 * Returns NULL for error.
2775 */
2776 static char_u *
2777get_mef_name()
2778{
2779 char_u *p;
2780 char_u *name;
2781 static int start = -1;
2782 static int off = 0;
2783#ifdef HAVE_LSTAT
2784 struct stat sb;
2785#endif
2786
2787 if (*p_mef == NUL)
2788 {
2789 name = vim_tempname('e');
2790 if (name == NULL)
2791 EMSG(_(e_notmp));
2792 return name;
2793 }
2794
2795 for (p = p_mef; *p; ++p)
2796 if (p[0] == '#' && p[1] == '#')
2797 break;
2798
2799 if (*p == NUL)
2800 return vim_strsave(p_mef);
2801
2802 /* Keep trying until the name doesn't exist yet. */
2803 for (;;)
2804 {
2805 if (start == -1)
2806 start = mch_get_pid();
2807 else
2808 off += 19;
2809
2810 name = alloc((unsigned)STRLEN(p_mef) + 30);
2811 if (name == NULL)
2812 break;
2813 STRCPY(name, p_mef);
2814 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2815 STRCAT(name, p + 2);
2816 if (mch_getperm(name) < 0
2817#ifdef HAVE_LSTAT
2818 /* Don't accept a symbolic link, its a security risk. */
2819 && mch_lstat((char *)name, &sb) < 0
2820#endif
2821 )
2822 break;
2823 vim_free(name);
2824 }
2825 return name;
2826}
2827
2828/*
2829 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002830 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 */
2832 void
2833ex_cc(eap)
2834 exarg_T *eap;
2835{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002836 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002837
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002838 if (eap->cmdidx == CMD_ll
2839 || eap->cmdidx == CMD_lrewind
2840 || eap->cmdidx == CMD_lfirst
2841 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002842 {
2843 qi = GET_LOC_LIST(curwin);
2844 if (qi == NULL)
2845 {
2846 EMSG(_(e_loclist));
2847 return;
2848 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002849 }
2850
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002851 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 eap->addr_count > 0
2853 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002854 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002856 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2857 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 ? 1
2859 : 32767,
2860 eap->forceit);
2861}
2862
2863/*
2864 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002865 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 */
2867 void
2868ex_cnext(eap)
2869 exarg_T *eap;
2870{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002871 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002872
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002873 if (eap->cmdidx == CMD_lnext
2874 || eap->cmdidx == CMD_lNext
2875 || eap->cmdidx == CMD_lprevious
2876 || eap->cmdidx == CMD_lnfile
2877 || eap->cmdidx == CMD_lNfile
2878 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002879 {
2880 qi = GET_LOC_LIST(curwin);
2881 if (qi == NULL)
2882 {
2883 EMSG(_(e_loclist));
2884 return;
2885 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002886 }
2887
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002888 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002892 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2893 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 ? BACKWARD_FILE
2895 : BACKWARD,
2896 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2897}
2898
2899/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002901 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 */
2903 void
2904ex_cfile(eap)
2905 exarg_T *eap;
2906{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002907 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002908 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002909
2910 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2911 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002912 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002915 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002916
2917 /*
2918 * This function is used by the :cfile, :cgetfile and :caddfile
2919 * commands.
2920 * :cfile always creates a new quickfix list and jumps to the
2921 * first error.
2922 * :cgetfile creates a new quickfix list but doesn't jump to the
2923 * first error.
2924 * :caddfile adds to an existing quickfix list. If there is no
2925 * quickfix list then a new list is created.
2926 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002927 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2928 && eap->cmdidx != CMD_laddfile)) > 0
2929 && (eap->cmdidx == CMD_cfile
2930 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002931 {
2932 if (wp != NULL)
2933 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002934 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936}
2937
2938/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002939 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00002940 * ":vimgrepadd {pattern} file(s)"
2941 * ":lvimgrep {pattern} file(s)"
2942 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00002943 */
2944 void
2945ex_vimgrep(eap)
2946 exarg_T *eap;
2947{
Bram Moolenaar81695252004-12-29 20:58:21 +00002948 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002949 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002950 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002951 char_u *s;
2952 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002953 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00002954 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002955 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002956 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002957 buf_T *buf;
2958 int duplicate_name = FALSE;
2959 int using_dummy;
2960 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002961 buf_T *first_match_buf = NULL;
2962 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002963 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002964#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2965 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002966#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002967 aco_save_T aco;
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00002968#ifdef FEAT_AUTOCMD
Bram Moolenaar7c626922005-02-07 22:01:03 +00002969 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002970 int flags = 0;
2971 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002972 long tomatch;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002973
2974 switch (eap->cmdidx)
2975 {
2976 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002977 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002978 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002979 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002980 default: break;
2981 }
2982 if (au_name != NULL)
2983 {
2984 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2985 curbuf->b_fname, TRUE, curbuf);
2986 if (did_throw || force_abort)
2987 return;
2988 }
2989#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002990
Bram Moolenaar754b5602006-02-09 23:53:20 +00002991 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002992 || eap->cmdidx == CMD_lvimgrep
2993 || eap->cmdidx == CMD_lgrepadd
2994 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002995 {
2996 qi = ll_get_or_alloc_list(curwin);
2997 if (qi == NULL)
2998 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00002999 }
3000
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003001 if (eap->addr_count > 0)
3002 tomatch = eap->line2;
3003 else
3004 tomatch = MAXLNUM;
3005
Bram Moolenaar81695252004-12-29 20:58:21 +00003006 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003007 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003008 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003009 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003010 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003011 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003012 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003013 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003014 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003015 if (regmatch.regprog == NULL)
3016 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003017 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003018 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003019
3020 p = skipwhite(p);
3021 if (*p == NUL)
3022 {
3023 EMSG(_("E683: File name missing or invalid pattern"));
3024 goto theend;
3025 }
3026
Bram Moolenaar754b5602006-02-09 23:53:20 +00003027 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003028 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003029 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003030 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003031 qf_new_list(qi);
3032 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003033 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003034 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003035 prevp->qf_next != prevp; prevp = prevp->qf_next)
3036 ;
3037
3038 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003039 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003040 goto theend;
3041 if (fcount == 0)
3042 {
3043 EMSG(_(e_nomatch));
3044 goto theend;
3045 }
3046
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003047 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003048 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003049 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003050 if (time(NULL) > seconds)
3051 {
3052 /* Display the file name every second or so. */
3053 seconds = time(NULL);
3054 msg_start();
Bram Moolenaara5373fa2005-09-09 19:47:12 +00003055 p = msg_strtrunc(fnames[fi], TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003056 if (p == NULL)
3057 msg_outtrans(fnames[fi]);
3058 else
3059 {
3060 msg_outtrans(p);
3061 vim_free(p);
3062 }
3063 msg_clr_eos();
3064 msg_didout = FALSE; /* overwrite this message */
3065 msg_nowait = TRUE; /* don't wait for this message */
3066 msg_col = 0;
3067 out_flush();
3068 }
3069
Bram Moolenaar81695252004-12-29 20:58:21 +00003070 buf = buflist_findname_exp(fnames[fi]);
3071 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3072 {
3073 /* Remember that a buffer with this name already exists. */
3074 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003075 using_dummy = TRUE;
3076
3077#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3078 /* Don't do Filetype autocommands to avoid loading syntax and
3079 * indent scripts, a great speed improvement. */
3080 save_ei = au_event_disable(",Filetype");
3081#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003082 /* Don't use modelines here, it's useless. */
3083 save_mls = p_mls;
3084 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003085
3086 /* Load file into a buffer, so that 'fileencoding' is detected,
3087 * autocommands applied, etc. */
3088 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003089
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003090 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003091#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3092 au_event_restore(save_ei);
3093#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003094 }
3095 else
3096 /* Use existing, loaded buffer. */
3097 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003098
Bram Moolenaar81695252004-12-29 20:58:21 +00003099 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003100 {
3101 if (!got_int)
3102 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
3103 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003104 else
3105 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003106 /* Try for a match in all lines of the buffer.
3107 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003108 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003109 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3110 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003111 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003112 col = 0;
3113 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3114 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003115 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003117 NULL, /* dir */
3118 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00003119 ml_get_buf(buf,
3120 regmatch.startpos[0].lnum + lnum, FALSE),
3121 regmatch.startpos[0].lnum + lnum,
3122 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003123 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003124 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003125 0, /* nr */
3126 0, /* type */
3127 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003128 ) == FAIL)
3129 {
3130 got_int = TRUE;
3131 break;
3132 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003133 found_match = TRUE;
3134 if (--tomatch == 0)
3135 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003136 if ((flags & VGR_GLOBAL) == 0
3137 || regmatch.endpos[0].lnum > 0)
3138 break;
3139 col = regmatch.endpos[0].col
3140 + (col == regmatch.endpos[0].col);
3141 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3142 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003143 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003144 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003145 if (got_int)
3146 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003147 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003148
3149 if (using_dummy)
3150 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003151 if (found_match && first_match_buf == NULL)
3152 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003153 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003154 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003155 /* Never keep a dummy buffer if there is another buffer
3156 * with the same name. */
3157 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003158 buf = NULL;
3159 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003160 else if (!cmdmod.hide
3161 || buf->b_p_bh[0] == 'u' /* "unload" */
3162 || buf->b_p_bh[0] == 'w' /* "wipe" */
3163 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003164 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003165 /* When no match was found we don't need to remember the
3166 * buffer, wipe it out. If there was a match and it
3167 * wasn't the first one or we won't jump there: only
3168 * unload the buffer.
3169 * Ignore 'hidden' here, because it may lead to having too
3170 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003171 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003172 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003173 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003174 buf = NULL;
3175 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003176 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003177 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003178 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003179 buf = NULL;
3180 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003181 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003182
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003183 if (buf != NULL)
3184 {
3185 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003186 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003187 * need to be done (again). But not the window-local
3188 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003189 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003190#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003191 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3192 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003193#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003194 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003195 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003196 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003197 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003198 }
3199 }
3200
3201 FreeWild(fcount, fnames);
3202
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003203 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3204 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3205 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003206
3207#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003208 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003209#endif
3210
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003211#ifdef FEAT_AUTOCMD
3212 if (au_name != NULL)
3213 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3214 curbuf->b_fname, TRUE, curbuf);
3215#endif
3216
Bram Moolenaar86b68352004-12-27 21:59:20 +00003217 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003218 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003219 {
3220 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003221 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003222 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003223 else
3224 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003225
3226theend:
3227 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003228}
3229
3230/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003231 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003232 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003233 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3234 * If "s" is not NULL terminate the pattern with a NUL.
3235 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003236 */
3237 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003238skip_vimgrep_pat(p, s, flags)
3239 char_u *p;
3240 char_u **s;
3241 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003242{
3243 int c;
3244
3245 if (vim_isIDc(*p))
3246 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003247 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003248 if (s != NULL)
3249 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003250 p = skiptowhite(p);
3251 if (s != NULL && *p != NUL)
3252 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003253 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003254 else
3255 {
3256 /* ":vimgrep /pattern/[g][j] fname" */
3257 if (s != NULL)
3258 *s = p + 1;
3259 c = *p;
3260 p = skip_regexp(p + 1, c, TRUE, NULL);
3261 if (*p != c)
3262 return NULL;
3263
3264 /* Truncate the pattern. */
3265 if (s != NULL)
3266 *p = NUL;
3267 ++p;
3268
3269 /* Find the flags */
3270 while (*p == 'g' || *p == 'j')
3271 {
3272 if (flags != NULL)
3273 {
3274 if (*p == 'g')
3275 *flags |= VGR_GLOBAL;
3276 else
3277 *flags |= VGR_NOJUMP;
3278 }
3279 ++p;
3280 }
3281 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003282 return p;
3283}
3284
3285/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003286 * Load file "fname" into a dummy buffer and return the buffer pointer.
3287 * Returns NULL if it fails.
3288 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3289 */
3290 static buf_T *
3291load_dummy_buffer(fname)
3292 char_u *fname;
3293{
3294 buf_T *newbuf;
3295 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003296 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003297
3298 /* Allocate a buffer without putting it in the buffer list. */
3299 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3300 if (newbuf == NULL)
3301 return NULL;
3302
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003303 /* Init the options. */
3304 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3305
Bram Moolenaar81695252004-12-29 20:58:21 +00003306 /* set curwin/curbuf to buf and save a few things */
3307 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00003308
3309 /* Need to set the filename for autocommands. */
3310 (void)setfname(curbuf, fname, NULL, FALSE);
3311
Bram Moolenaar4770d092006-01-12 23:22:24 +00003312 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003313 {
3314 /* Create swap file now to avoid the ATTENTION message. */
3315 check_need_swap(TRUE);
3316
3317 /* Remove the "dummy" flag, otherwise autocommands may not
3318 * work. */
3319 curbuf->b_flags &= ~BF_DUMMY;
3320
3321 if (readfile(fname, NULL,
3322 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3323 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003324 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003325 && !(curbuf->b_flags & BF_NEW))
3326 {
3327 failed = FALSE;
3328 if (curbuf != newbuf)
3329 {
3330 /* Bloody autocommands changed the buffer! */
3331 if (buf_valid(newbuf))
3332 wipe_buffer(newbuf, FALSE);
3333 newbuf = curbuf;
3334 }
3335 }
3336 }
3337
Bram Moolenaar81695252004-12-29 20:58:21 +00003338 /* restore curwin/curbuf and a few other things */
3339 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00003340
3341 if (!buf_valid(newbuf))
3342 return NULL;
3343 if (failed)
3344 {
3345 wipe_dummy_buffer(newbuf);
3346 return NULL;
3347 }
3348 return newbuf;
3349}
3350
3351/*
3352 * Wipe out the dummy buffer that load_dummy_buffer() created.
3353 */
3354 static void
3355wipe_dummy_buffer(buf)
3356 buf_T *buf;
3357{
3358 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003359 {
3360#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3361 cleanup_T cs;
3362
3363 /* Reset the error/interrupt/exception state here so that aborting()
3364 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3365 * work when got_int is set. */
3366 enter_cleanup(&cs);
3367#endif
3368
Bram Moolenaar81695252004-12-29 20:58:21 +00003369 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003370
3371#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3372 /* Restore the error/interrupt/exception state if not discarded by a
3373 * new aborting error, interrupt, or uncaught exception. */
3374 leave_cleanup(&cs);
3375#endif
3376 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003377}
3378
3379/*
3380 * Unload the dummy buffer that load_dummy_buffer() created.
3381 */
3382 static void
3383unload_dummy_buffer(buf)
3384 buf_T *buf;
3385{
3386 if (curbuf != buf) /* safety check */
3387 close_buffer(NULL, buf, DOBUF_UNLOAD);
3388}
3389
Bram Moolenaar05159a02005-02-26 23:04:13 +00003390#if defined(FEAT_EVAL) || defined(PROTO)
3391/*
3392 * Add each quickfix error to list "list" as a dictionary.
3393 */
3394 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003395get_errorlist(wp, list)
3396 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003397 list_T *list;
3398{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003399 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003400 dict_T *dict;
3401 char_u buf[2];
3402 qfline_T *qfp;
3403 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003404
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003405 if (wp != NULL)
3406 {
3407 qi = GET_LOC_LIST(wp);
3408 if (qi == NULL)
3409 return FAIL;
3410 }
3411
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003412 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003413 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003414 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003415
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003416 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3417 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003418 {
3419 if ((dict = dict_alloc()) == NULL)
3420 return FAIL;
3421 if (list_append_dict(list, dict) == FAIL)
3422 return FAIL;
3423
3424 buf[0] = qfp->qf_type;
3425 buf[1] = NUL;
3426 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
3427 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3428 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3429 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3430 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003431 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003432 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
3433 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3434 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3435 return FAIL;
3436
3437 qfp = qfp->qf_next;
3438 }
3439 return OK;
3440}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003441
3442/*
3443 * Populate the quickfix list with the items supplied in the list
3444 * of dictionaries.
3445 */
3446 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003447set_errorlist(wp, list, action)
3448 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003449 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003450 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003451{
3452 listitem_T *li;
3453 dict_T *d;
3454 char_u *filename, *pattern, *text, *type;
3455 long lnum;
3456 int col, nr;
3457 int vcol;
3458 qfline_T *prevp = NULL;
3459 int valid, status;
3460 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003461 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003462
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003463 if (wp != NULL)
3464 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003465 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003466 if (qi == NULL)
3467 return FAIL;
3468 }
3469
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003470 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003471 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003472 qf_new_list(qi);
3473 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003474 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003475 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003476 prevp->qf_next != prevp; prevp = prevp->qf_next)
3477 ;
3478 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003480
3481 for (li = list->lv_first; li != NULL; li = li->li_next)
3482 {
3483 if (li->li_tv.v_type != VAR_DICT)
3484 continue; /* Skip non-dict items */
3485
3486 d = li->li_tv.vval.v_dict;
3487 if (d == NULL)
3488 continue;
3489
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003490 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003491 lnum = get_dict_number(d, (char_u *)"lnum");
3492 col = get_dict_number(d, (char_u *)"col");
3493 vcol = get_dict_number(d, (char_u *)"vcol");
3494 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003495 type = get_dict_string(d, (char_u *)"type", TRUE);
3496 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3497 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003498 if (text == NULL)
3499 text = vim_strsave((char_u *)"");
3500
3501 valid = TRUE;
3502 if (filename == NULL || (lnum == 0 && pattern == NULL))
3503 valid = FALSE;
3504
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003505 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003506 NULL, /* dir */
3507 filename,
3508 text,
3509 lnum,
3510 col,
3511 vcol, /* vis_col */
3512 pattern, /* search pattern */
3513 nr,
3514 type == NULL ? NUL : *type,
3515 valid);
3516
3517 vim_free(filename);
3518 vim_free(pattern);
3519 vim_free(text);
3520 vim_free(type);
3521
3522 if (status == FAIL)
3523 {
3524 retval = FAIL;
3525 break;
3526 }
3527 }
3528
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003529 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3530 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3531 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003532
3533#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003534 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003535#endif
3536
3537 return retval;
3538}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003539#endif
3540
Bram Moolenaar81695252004-12-29 20:58:21 +00003541/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003542 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003543 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003544 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003545 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003546 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003547 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003548 */
3549 void
3550ex_cbuffer(eap)
3551 exarg_T *eap;
3552{
3553 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003554 qf_info_T *qi = &ql_info;
3555
Bram Moolenaardb552d602006-03-23 22:59:57 +00003556 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3557 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003558 {
3559 qi = ll_get_or_alloc_list(curwin);
3560 if (qi == NULL)
3561 return;
3562 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003563
3564 if (*eap->arg == NUL)
3565 buf = curbuf;
3566 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3567 buf = buflist_findnr(atoi((char *)eap->arg));
3568 if (buf == NULL)
3569 EMSG(_(e_invarg));
3570 else if (buf->b_ml.ml_mfp == NULL)
3571 EMSG(_("E681: Buffer is not loaded"));
3572 else
3573 {
3574 if (eap->addr_count == 0)
3575 {
3576 eap->line1 = 1;
3577 eap->line2 = buf->b_ml.ml_line_count;
3578 }
3579 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3580 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3581 EMSG(_(e_invrange));
3582 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003583 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003584 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3585 (eap->cmdidx != CMD_caddbuffer
3586 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar754b5602006-02-09 23:53:20 +00003587 eap->line1, eap->line2) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003588 && (eap->cmdidx == CMD_cbuffer
3589 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003590 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3591 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003592 }
3593}
3594
Bram Moolenaar1e015462005-09-25 22:16:38 +00003595#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003596/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003597 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3598 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003599 */
3600 void
3601ex_cexpr(eap)
3602 exarg_T *eap;
3603{
3604 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003605 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003606
Bram Moolenaardb552d602006-03-23 22:59:57 +00003607 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3608 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003609 {
3610 qi = ll_get_or_alloc_list(curwin);
3611 if (qi == NULL)
3612 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003613 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003614
Bram Moolenaar4770d092006-01-12 23:22:24 +00003615 /* Evaluate the expression. When the result is a string or a list we can
3616 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003617 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003618 if (tv != NULL)
3619 {
3620 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3621 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3622 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003623 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3624 (eap->cmdidx != CMD_caddexpr
3625 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar4770d092006-01-12 23:22:24 +00003626 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003627 && (eap->cmdidx == CMD_cexpr
3628 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003629 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003630 }
3631 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003632 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003633 free_tv(tv);
3634 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003635}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003636#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003637
3638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 * ":helpgrep {pattern}"
3640 */
3641 void
3642ex_helpgrep(eap)
3643 exarg_T *eap;
3644{
3645 regmatch_T regmatch;
3646 char_u *save_cpo;
3647 char_u *p;
3648 int fcount;
3649 char_u **fnames;
3650 FILE *fd;
3651 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003652 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003654#ifdef FEAT_MULTI_LANG
3655 char_u *lang;
3656#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003657 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003658 int new_qi = FALSE;
3659 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3662 save_cpo = p_cpo;
3663 p_cpo = (char_u *)"";
3664
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003665#ifdef FEAT_MULTI_LANG
3666 /* Check for a specified language */
3667 lang = check_help_lang(eap->arg);
3668#endif
3669
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003670 if (eap->cmdidx == CMD_lhelpgrep)
3671 {
3672 /* Find an existing help window */
3673 FOR_ALL_WINDOWS(wp)
3674 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3675 break;
3676
3677 if (wp == NULL) /* Help window not found */
3678 qi = NULL;
3679 else
3680 qi = wp->w_llist;
3681
3682 if (qi == NULL)
3683 {
3684 /* Allocate a new location list for help text matches */
3685 if ((qi = ll_new_list()) == NULL)
3686 return;
3687 new_qi = TRUE;
3688 }
3689 }
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3692 regmatch.rm_ic = FALSE;
3693 if (regmatch.regprog != NULL)
3694 {
3695 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003696 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698 /* Go through all directories in 'runtimepath' */
3699 p = p_rtp;
3700 while (*p != NUL && !got_int)
3701 {
3702 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3703
3704 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3705 add_pathsep(NameBuff);
3706 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3707 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3708 &fnames, EW_FILE|EW_SILENT) == OK
3709 && fcount > 0)
3710 {
3711 for (fi = 0; fi < fcount && !got_int; ++fi)
3712 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003713#ifdef FEAT_MULTI_LANG
3714 /* Skip files for a different language. */
3715 if (lang != NULL
3716 && STRNICMP(lang, fnames[fi]
3717 + STRLEN(fnames[fi]) - 3, 2) != 0
3718 && !(STRNICMP(lang, "en", 2) == 0
3719 && STRNICMP("txt", fnames[fi]
3720 + STRLEN(fnames[fi]) - 3, 3) == 0))
3721 continue;
3722#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003723 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (fd != NULL)
3725 {
3726 lnum = 1;
3727 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3728 {
3729 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3730 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003731 int l = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 /* remove trailing CR, LF, spaces, etc. */
3734 while (l > 0 && IObuff[l - 1] <= ' ')
3735 IObuff[--l] = NUL;
3736
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003737 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 NULL, /* dir */
3739 fnames[fi],
3740 IObuff,
3741 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003742 (int)(regmatch.startp[0] - IObuff)
3743 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003744 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003745 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 0, /* nr */
3747 1, /* type */
3748 TRUE /* valid */
3749 ) == FAIL)
3750 {
3751 got_int = TRUE;
3752 break;
3753 }
3754 }
3755 ++lnum;
3756 line_breakcheck();
3757 }
3758 fclose(fd);
3759 }
3760 }
3761 FreeWild(fcount, fnames);
3762 }
3763 }
3764 vim_free(regmatch.regprog);
3765
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003766 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3767 qi->qf_lists[qi->qf_curlist].qf_ptr =
3768 qi->qf_lists[qi->qf_curlist].qf_start;
3769 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771
3772 p_cpo = save_cpo;
3773
3774#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003775 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776#endif
3777
3778 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003779 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003780 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003781 else
3782 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003783
3784 if (eap->cmdidx == CMD_lhelpgrep)
3785 {
3786 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003787 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003788 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3789 {
3790 if (new_qi)
3791 ll_free_all(&qi);
3792 }
3793 else if (curwin->w_llist == NULL)
3794 curwin->w_llist = qi;
3795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796}
3797
3798#endif /* FEAT_QUICKFIX */