blob: 8c3dbbff65a14955c588f08751287ccabf7f9a6f [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;
2466#ifdef FEAT_AUTOCMD
2467 aco_save_T aco;
2468#else
2469 buf_T *save_curbuf;
2470#endif
2471
2472 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002473 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (buf != NULL)
2475 {
2476#ifdef FEAT_AUTOCMD
2477 /* set curwin/curbuf to buf and save a few things */
2478 aucmd_prepbuf(&aco, buf);
2479#else
2480 save_curbuf = curbuf;
2481 curbuf = buf;
2482#endif
2483
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002484 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486#ifdef FEAT_AUTOCMD
2487 /* restore curwin/curbuf and a few other things */
2488 aucmd_restbuf(&aco);
2489#else
2490 curbuf = save_curbuf;
2491#endif
2492
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002493 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495}
2496
2497/*
2498 * Fill current buffer with quickfix errors, replacing any previous contents.
2499 * curbuf must be the quickfix buffer!
2500 */
2501 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002502qf_fill_buffer(qi)
2503 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002505 linenr_T lnum;
2506 qfline_T *qfp;
2507 buf_T *errbuf;
2508 int len;
2509 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511 /* delete all existing lines */
2512 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2513 (void)ml_delete((linenr_T)1, FALSE);
2514
2515 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002516 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002519 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2520 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 if (qfp->qf_fnum != 0
2523 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2524 && errbuf->b_fname != NULL)
2525 {
2526 if (qfp->qf_type == 1) /* :helpgrep */
2527 STRCPY(IObuff, gettail(errbuf->b_fname));
2528 else
2529 STRCPY(IObuff, errbuf->b_fname);
2530 len = (int)STRLEN(IObuff);
2531 }
2532 else
2533 len = 0;
2534 IObuff[len++] = '|';
2535
2536 if (qfp->qf_lnum > 0)
2537 {
2538 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2539 len += (int)STRLEN(IObuff + len);
2540
2541 if (qfp->qf_col > 0)
2542 {
2543 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2544 len += (int)STRLEN(IObuff + len);
2545 }
2546
2547 sprintf((char *)IObuff + len, "%s",
2548 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2549 len += (int)STRLEN(IObuff + len);
2550 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002551 else if (qfp->qf_pattern != NULL)
2552 {
2553 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2554 len += (int)STRLEN(IObuff + len);
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 IObuff[len++] = '|';
2557 IObuff[len++] = ' ';
2558
2559 /* Remove newlines and leading whitespace from the text.
2560 * For an unrecognized line keep the indent, the compiler may
2561 * mark a word with ^^^^. */
2562 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2563 IObuff + len, IOSIZE - len);
2564
2565 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2566 == FAIL)
2567 break;
2568 qfp = qfp->qf_next;
2569 }
2570 /* Delete the empty line which is now at the end */
2571 (void)ml_delete(lnum + 1, FALSE);
2572 }
2573
2574 /* correct cursor position */
2575 check_lnums(TRUE);
2576
2577 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2578 * resembles reading a file into a buffer, it's more logical when using
2579 * autocommands. */
2580 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2581 curbuf->b_p_ma = FALSE;
2582
2583#ifdef FEAT_AUTOCMD
2584 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2585 FALSE, curbuf);
2586 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2587 FALSE, curbuf);
2588#endif
2589
2590 /* make sure it will be redrawn */
2591 redraw_curbuf_later(NOT_VALID);
2592
2593 /* Restore KeyTyped, setting 'filetype' may reset it. */
2594 KeyTyped = old_KeyTyped;
2595}
2596
2597#endif /* FEAT_WINDOWS */
2598
2599/*
2600 * Return TRUE if "buf" is the quickfix buffer.
2601 */
2602 int
2603bt_quickfix(buf)
2604 buf_T *buf;
2605{
2606 return (buf->b_p_bt[0] == 'q');
2607}
2608
2609/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002610 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2611 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
2613 int
2614bt_nofile(buf)
2615 buf_T *buf;
2616{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002617 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2618 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619}
2620
2621/*
2622 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2623 */
2624 int
2625bt_dontwrite(buf)
2626 buf_T *buf;
2627{
2628 return (buf->b_p_bt[0] == 'n');
2629}
2630
2631 int
2632bt_dontwrite_msg(buf)
2633 buf_T *buf;
2634{
2635 if (bt_dontwrite(buf))
2636 {
2637 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2638 return TRUE;
2639 }
2640 return FALSE;
2641}
2642
2643/*
2644 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2645 * and 'bufhidden'.
2646 */
2647 int
2648buf_hide(buf)
2649 buf_T *buf;
2650{
2651 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2652 switch (buf->b_p_bh[0])
2653 {
2654 case 'u': /* "unload" */
2655 case 'w': /* "wipe" */
2656 case 'd': return FALSE; /* "delete" */
2657 case 'h': return TRUE; /* "hide" */
2658 }
2659 return (p_hid || cmdmod.hide);
2660}
2661
2662/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002663 * Return TRUE when using ":vimgrep" for ":grep".
2664 */
2665 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002666grep_internal(cmdidx)
2667 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002668{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002669 return ((cmdidx == CMD_grep
2670 || cmdidx == CMD_lgrep
2671 || cmdidx == CMD_grepadd
2672 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002673 && STRCMP("internal",
2674 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2675}
2676
2677/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002678 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 */
2680 void
2681ex_make(eap)
2682 exarg_T *eap;
2683{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002684 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 char_u *cmd;
2686 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002687 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002688 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002689 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002690#ifdef FEAT_AUTOCMD
2691 char_u *au_name = NULL;
2692
2693 switch (eap->cmdidx)
2694 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002695 case CMD_make: au_name = (char_u *)"make"; break;
2696 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2697 case CMD_grep: au_name = (char_u *)"grep"; break;
2698 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2699 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2700 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002701 default: break;
2702 }
2703 if (au_name != NULL)
2704 {
2705 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2706 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002707# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002708 if (did_throw || force_abort)
2709 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002710# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002711 }
2712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar86b68352004-12-27 21:59:20 +00002714 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002715 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002716 {
2717 ex_vimgrep(eap);
2718 return;
2719 }
2720
Bram Moolenaara6557602006-02-04 22:43:20 +00002721 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2722 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002723 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002726 fname = get_mef_name();
2727 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002729 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 /*
2732 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2733 */
2734 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2735 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002736 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 cmd = alloc(len);
2738 if (cmd == NULL)
2739 return;
2740 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2741 (char *)p_shq);
2742 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002743 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 /*
2745 * Output a newline if there's something else than the :make command that
2746 * was typed (in which case the cursor is in column 0).
2747 */
2748 if (msg_col == 0)
2749 msg_didout = FALSE;
2750 msg_start();
2751 MSG_PUTS(":!");
2752 msg_outtrans(cmd); /* show what we are doing */
2753
2754 /* let the shell know if we are redirecting output or not */
2755 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2756
2757#ifdef AMIGA
2758 out_flush();
2759 /* read window status report and redraw before message */
2760 (void)char_avail();
2761#endif
2762
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002763 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002764 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2765 (eap->cmdidx != CMD_grepadd
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002766 && eap->cmdidx != CMD_lgrepadd));
2767#ifdef FEAT_AUTOCMD
2768 if (au_name != NULL)
2769 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2770 curbuf->b_fname, TRUE, curbuf);
2771#endif
2772 if (res > 0 && !eap->forceit)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002773 {
2774 if (wp != NULL)
2775 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002776 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
Bram Moolenaar7c626922005-02-07 22:01:03 +00002779 mch_remove(fname);
2780 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 vim_free(cmd);
2782}
2783
2784/*
2785 * Return the name for the errorfile, in allocated memory.
2786 * Find a new unique name when 'makeef' contains "##".
2787 * Returns NULL for error.
2788 */
2789 static char_u *
2790get_mef_name()
2791{
2792 char_u *p;
2793 char_u *name;
2794 static int start = -1;
2795 static int off = 0;
2796#ifdef HAVE_LSTAT
2797 struct stat sb;
2798#endif
2799
2800 if (*p_mef == NUL)
2801 {
2802 name = vim_tempname('e');
2803 if (name == NULL)
2804 EMSG(_(e_notmp));
2805 return name;
2806 }
2807
2808 for (p = p_mef; *p; ++p)
2809 if (p[0] == '#' && p[1] == '#')
2810 break;
2811
2812 if (*p == NUL)
2813 return vim_strsave(p_mef);
2814
2815 /* Keep trying until the name doesn't exist yet. */
2816 for (;;)
2817 {
2818 if (start == -1)
2819 start = mch_get_pid();
2820 else
2821 off += 19;
2822
2823 name = alloc((unsigned)STRLEN(p_mef) + 30);
2824 if (name == NULL)
2825 break;
2826 STRCPY(name, p_mef);
2827 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2828 STRCAT(name, p + 2);
2829 if (mch_getperm(name) < 0
2830#ifdef HAVE_LSTAT
2831 /* Don't accept a symbolic link, its a security risk. */
2832 && mch_lstat((char *)name, &sb) < 0
2833#endif
2834 )
2835 break;
2836 vim_free(name);
2837 }
2838 return name;
2839}
2840
2841/*
2842 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002843 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 */
2845 void
2846ex_cc(eap)
2847 exarg_T *eap;
2848{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002849 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002850
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002851 if (eap->cmdidx == CMD_ll
2852 || eap->cmdidx == CMD_lrewind
2853 || eap->cmdidx == CMD_lfirst
2854 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002855 {
2856 qi = GET_LOC_LIST(curwin);
2857 if (qi == NULL)
2858 {
2859 EMSG(_(e_loclist));
2860 return;
2861 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002862 }
2863
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002864 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 eap->addr_count > 0
2866 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002867 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002869 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2870 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 ? 1
2872 : 32767,
2873 eap->forceit);
2874}
2875
2876/*
2877 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002878 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 */
2880 void
2881ex_cnext(eap)
2882 exarg_T *eap;
2883{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002884 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002885
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002886 if (eap->cmdidx == CMD_lnext
2887 || eap->cmdidx == CMD_lNext
2888 || eap->cmdidx == CMD_lprevious
2889 || eap->cmdidx == CMD_lnfile
2890 || eap->cmdidx == CMD_lNfile
2891 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002892 {
2893 qi = GET_LOC_LIST(curwin);
2894 if (qi == NULL)
2895 {
2896 EMSG(_(e_loclist));
2897 return;
2898 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002899 }
2900
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002901 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002903 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002905 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2906 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 ? BACKWARD_FILE
2908 : BACKWARD,
2909 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2910}
2911
2912/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002914 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 */
2916 void
2917ex_cfile(eap)
2918 exarg_T *eap;
2919{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002921 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002922
2923 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2924 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002928 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002929
2930 /*
2931 * This function is used by the :cfile, :cgetfile and :caddfile
2932 * commands.
2933 * :cfile always creates a new quickfix list and jumps to the
2934 * first error.
2935 * :cgetfile creates a new quickfix list but doesn't jump to the
2936 * first error.
2937 * :caddfile adds to an existing quickfix list. If there is no
2938 * quickfix list then a new list is created.
2939 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002940 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2941 && eap->cmdidx != CMD_laddfile)) > 0
2942 && (eap->cmdidx == CMD_cfile
2943 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002944 {
2945 if (wp != NULL)
2946 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002947 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949}
2950
2951/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002952 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00002953 * ":vimgrepadd {pattern} file(s)"
2954 * ":lvimgrep {pattern} file(s)"
2955 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00002956 */
2957 void
2958ex_vimgrep(eap)
2959 exarg_T *eap;
2960{
Bram Moolenaar81695252004-12-29 20:58:21 +00002961 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002962 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002963 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002964 char_u *s;
2965 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002966 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00002967 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002968 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002969 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002970 buf_T *buf;
2971 int duplicate_name = FALSE;
2972 int using_dummy;
2973 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002974 buf_T *first_match_buf = NULL;
2975 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002976 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002977#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2978 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002979#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002980#ifndef FEAT_AUTOCMD
2981 buf_T *save_curbuf;
2982#else
2983 aco_save_T aco;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002984 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002985 int flags = 0;
2986 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002987 long tomatch;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002988
2989 switch (eap->cmdidx)
2990 {
2991 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002992 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002993 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002994 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002995 default: break;
2996 }
2997 if (au_name != NULL)
2998 {
2999 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3000 curbuf->b_fname, TRUE, curbuf);
3001 if (did_throw || force_abort)
3002 return;
3003 }
3004#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003005
Bram Moolenaar754b5602006-02-09 23:53:20 +00003006 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003007 || eap->cmdidx == CMD_lvimgrep
3008 || eap->cmdidx == CMD_lgrepadd
3009 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003010 {
3011 qi = ll_get_or_alloc_list(curwin);
3012 if (qi == NULL)
3013 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003014 }
3015
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003016 if (eap->addr_count > 0)
3017 tomatch = eap->line2;
3018 else
3019 tomatch = MAXLNUM;
3020
Bram Moolenaar81695252004-12-29 20:58:21 +00003021 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003022 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003023 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003024 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003025 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003026 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003027 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003028 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003029 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003030 if (regmatch.regprog == NULL)
3031 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003032 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003033 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003034
3035 p = skipwhite(p);
3036 if (*p == NUL)
3037 {
3038 EMSG(_("E683: File name missing or invalid pattern"));
3039 goto theend;
3040 }
3041
Bram Moolenaar754b5602006-02-09 23:53:20 +00003042 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003043 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003044 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003045 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003046 qf_new_list(qi);
3047 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003048 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003049 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003050 prevp->qf_next != prevp; prevp = prevp->qf_next)
3051 ;
3052
3053 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003054 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003055 goto theend;
3056 if (fcount == 0)
3057 {
3058 EMSG(_(e_nomatch));
3059 goto theend;
3060 }
3061
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003062 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003063 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003064 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003065 if (time(NULL) > seconds)
3066 {
3067 /* Display the file name every second or so. */
3068 seconds = time(NULL);
3069 msg_start();
Bram Moolenaara5373fa2005-09-09 19:47:12 +00003070 p = msg_strtrunc(fnames[fi], TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003071 if (p == NULL)
3072 msg_outtrans(fnames[fi]);
3073 else
3074 {
3075 msg_outtrans(p);
3076 vim_free(p);
3077 }
3078 msg_clr_eos();
3079 msg_didout = FALSE; /* overwrite this message */
3080 msg_nowait = TRUE; /* don't wait for this message */
3081 msg_col = 0;
3082 out_flush();
3083 }
3084
Bram Moolenaar81695252004-12-29 20:58:21 +00003085 buf = buflist_findname_exp(fnames[fi]);
3086 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3087 {
3088 /* Remember that a buffer with this name already exists. */
3089 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003090 using_dummy = TRUE;
3091
3092#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3093 /* Don't do Filetype autocommands to avoid loading syntax and
3094 * indent scripts, a great speed improvement. */
3095 save_ei = au_event_disable(",Filetype");
3096#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003097 /* Don't use modelines here, it's useless. */
3098 save_mls = p_mls;
3099 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003100
3101 /* Load file into a buffer, so that 'fileencoding' is detected,
3102 * autocommands applied, etc. */
3103 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003104
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003105 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003106#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3107 au_event_restore(save_ei);
3108#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003109 }
3110 else
3111 /* Use existing, loaded buffer. */
3112 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003113
Bram Moolenaar81695252004-12-29 20:58:21 +00003114 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003115 {
3116 if (!got_int)
3117 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
3118 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003119 else
3120 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003121 /* Try for a match in all lines of the buffer.
3122 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003123 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003124 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3125 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003126 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003127 col = 0;
3128 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3129 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003130 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003131 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003132 NULL, /* dir */
3133 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00003134 ml_get_buf(buf,
3135 regmatch.startpos[0].lnum + lnum, FALSE),
3136 regmatch.startpos[0].lnum + lnum,
3137 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003138 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003139 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003140 0, /* nr */
3141 0, /* type */
3142 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003143 ) == FAIL)
3144 {
3145 got_int = TRUE;
3146 break;
3147 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003148 found_match = TRUE;
3149 if (--tomatch == 0)
3150 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003151 if ((flags & VGR_GLOBAL) == 0
3152 || regmatch.endpos[0].lnum > 0)
3153 break;
3154 col = regmatch.endpos[0].col
3155 + (col == regmatch.endpos[0].col);
3156 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3157 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003158 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003159 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003160 if (got_int)
3161 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003162 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003163
3164 if (using_dummy)
3165 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003166 if (found_match && first_match_buf == NULL)
3167 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003168 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003169 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003170 /* Never keep a dummy buffer if there is another buffer
3171 * with the same name. */
3172 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003173 buf = NULL;
3174 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003175 else if (!cmdmod.hide
3176 || buf->b_p_bh[0] == 'u' /* "unload" */
3177 || buf->b_p_bh[0] == 'w' /* "wipe" */
3178 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003179 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003180 /* When no match was found we don't need to remember the
3181 * buffer, wipe it out. If there was a match and it
3182 * wasn't the first one or we won't jump there: only
3183 * unload the buffer.
3184 * Ignore 'hidden' here, because it may lead to having too
3185 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003186 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003187 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003188 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003189 buf = NULL;
3190 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003191 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003192 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003193 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003194 buf = NULL;
3195 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003196 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003197
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003198 if (buf != NULL)
3199 {
3200 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003201 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003202 * need to be done (again). But not the window-local
3203 * options! */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003204#if defined(FEAT_AUTOCMD)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003205 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003206#else
3207 save_curbuf = curbuf;
3208 curbuf = buf;
3209 curwin->w_buffer = curbuf;
3210#endif
3211#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003212 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3213 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003214#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003215 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003216#if defined(FEAT_AUTOCMD)
3217 aucmd_restbuf(&aco);
3218#else
3219 curbuf = save_curbuf;
3220 curwin->w_buffer = curbuf;
3221#endif
3222 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003223 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003224 }
3225 }
3226
3227 FreeWild(fcount, fnames);
3228
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003229 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3230 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3231 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003232
3233#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003234 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003235#endif
3236
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003237#ifdef FEAT_AUTOCMD
3238 if (au_name != NULL)
3239 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3240 curbuf->b_fname, TRUE, curbuf);
3241#endif
3242
Bram Moolenaar86b68352004-12-27 21:59:20 +00003243 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003244 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003245 {
3246 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003247 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003248 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003249 else
3250 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003251
3252theend:
3253 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003254}
3255
3256/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003257 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003258 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003259 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3260 * If "s" is not NULL terminate the pattern with a NUL.
3261 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003262 */
3263 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003264skip_vimgrep_pat(p, s, flags)
3265 char_u *p;
3266 char_u **s;
3267 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003268{
3269 int c;
3270
3271 if (vim_isIDc(*p))
3272 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003273 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003274 if (s != NULL)
3275 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003276 p = skiptowhite(p);
3277 if (s != NULL && *p != NUL)
3278 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003279 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003280 else
3281 {
3282 /* ":vimgrep /pattern/[g][j] fname" */
3283 if (s != NULL)
3284 *s = p + 1;
3285 c = *p;
3286 p = skip_regexp(p + 1, c, TRUE, NULL);
3287 if (*p != c)
3288 return NULL;
3289
3290 /* Truncate the pattern. */
3291 if (s != NULL)
3292 *p = NUL;
3293 ++p;
3294
3295 /* Find the flags */
3296 while (*p == 'g' || *p == 'j')
3297 {
3298 if (flags != NULL)
3299 {
3300 if (*p == 'g')
3301 *flags |= VGR_GLOBAL;
3302 else
3303 *flags |= VGR_NOJUMP;
3304 }
3305 ++p;
3306 }
3307 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003308 return p;
3309}
3310
3311/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003312 * Load file "fname" into a dummy buffer and return the buffer pointer.
3313 * Returns NULL if it fails.
3314 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3315 */
3316 static buf_T *
3317load_dummy_buffer(fname)
3318 char_u *fname;
3319{
3320 buf_T *newbuf;
3321 int failed = TRUE;
3322#ifdef FEAT_AUTOCMD
3323 aco_save_T aco;
3324#else
3325 buf_T *old_curbuf = curbuf;
3326#endif
3327
3328 /* Allocate a buffer without putting it in the buffer list. */
3329 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3330 if (newbuf == NULL)
3331 return NULL;
3332
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003333 /* Init the options. */
3334 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3335
Bram Moolenaar81695252004-12-29 20:58:21 +00003336#ifdef FEAT_AUTOCMD
3337 /* set curwin/curbuf to buf and save a few things */
3338 aucmd_prepbuf(&aco, newbuf);
3339#else
3340 curbuf = newbuf;
3341 curwin->w_buffer = newbuf;
3342#endif
3343
3344 /* Need to set the filename for autocommands. */
3345 (void)setfname(curbuf, fname, NULL, FALSE);
3346
Bram Moolenaar4770d092006-01-12 23:22:24 +00003347 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003348 {
3349 /* Create swap file now to avoid the ATTENTION message. */
3350 check_need_swap(TRUE);
3351
3352 /* Remove the "dummy" flag, otherwise autocommands may not
3353 * work. */
3354 curbuf->b_flags &= ~BF_DUMMY;
3355
3356 if (readfile(fname, NULL,
3357 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3358 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003359 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003360 && !(curbuf->b_flags & BF_NEW))
3361 {
3362 failed = FALSE;
3363 if (curbuf != newbuf)
3364 {
3365 /* Bloody autocommands changed the buffer! */
3366 if (buf_valid(newbuf))
3367 wipe_buffer(newbuf, FALSE);
3368 newbuf = curbuf;
3369 }
3370 }
3371 }
3372
3373#ifdef FEAT_AUTOCMD
3374 /* restore curwin/curbuf and a few other things */
3375 aucmd_restbuf(&aco);
3376#else
3377 curbuf = old_curbuf;
3378 curwin->w_buffer = old_curbuf;
3379#endif
3380
3381 if (!buf_valid(newbuf))
3382 return NULL;
3383 if (failed)
3384 {
3385 wipe_dummy_buffer(newbuf);
3386 return NULL;
3387 }
3388 return newbuf;
3389}
3390
3391/*
3392 * Wipe out the dummy buffer that load_dummy_buffer() created.
3393 */
3394 static void
3395wipe_dummy_buffer(buf)
3396 buf_T *buf;
3397{
3398 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003399 {
3400#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3401 cleanup_T cs;
3402
3403 /* Reset the error/interrupt/exception state here so that aborting()
3404 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3405 * work when got_int is set. */
3406 enter_cleanup(&cs);
3407#endif
3408
Bram Moolenaar81695252004-12-29 20:58:21 +00003409 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003410
3411#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3412 /* Restore the error/interrupt/exception state if not discarded by a
3413 * new aborting error, interrupt, or uncaught exception. */
3414 leave_cleanup(&cs);
3415#endif
3416 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003417}
3418
3419/*
3420 * Unload the dummy buffer that load_dummy_buffer() created.
3421 */
3422 static void
3423unload_dummy_buffer(buf)
3424 buf_T *buf;
3425{
3426 if (curbuf != buf) /* safety check */
3427 close_buffer(NULL, buf, DOBUF_UNLOAD);
3428}
3429
Bram Moolenaar05159a02005-02-26 23:04:13 +00003430#if defined(FEAT_EVAL) || defined(PROTO)
3431/*
3432 * Add each quickfix error to list "list" as a dictionary.
3433 */
3434 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003435get_errorlist(wp, list)
3436 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003437 list_T *list;
3438{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003439 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003440 dict_T *dict;
3441 char_u buf[2];
3442 qfline_T *qfp;
3443 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003444
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003445 if (wp != NULL)
3446 {
3447 qi = GET_LOC_LIST(wp);
3448 if (qi == NULL)
3449 return FAIL;
3450 }
3451
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003452 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003453 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003454 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003455
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003456 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3457 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003458 {
3459 if ((dict = dict_alloc()) == NULL)
3460 return FAIL;
3461 if (list_append_dict(list, dict) == FAIL)
3462 return FAIL;
3463
3464 buf[0] = qfp->qf_type;
3465 buf[1] = NUL;
3466 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
3467 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3468 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3469 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3470 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003471 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003472 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
3473 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3474 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3475 return FAIL;
3476
3477 qfp = qfp->qf_next;
3478 }
3479 return OK;
3480}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003481
3482/*
3483 * Populate the quickfix list with the items supplied in the list
3484 * of dictionaries.
3485 */
3486 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003487set_errorlist(wp, list, action)
3488 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003489 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003490 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003491{
3492 listitem_T *li;
3493 dict_T *d;
3494 char_u *filename, *pattern, *text, *type;
3495 long lnum;
3496 int col, nr;
3497 int vcol;
3498 qfline_T *prevp = NULL;
3499 int valid, status;
3500 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003501 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003502
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003503 if (wp != NULL)
3504 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003505 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003506 if (qi == NULL)
3507 return FAIL;
3508 }
3509
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003510 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003511 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003512 qf_new_list(qi);
3513 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003514 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003515 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003516 prevp->qf_next != prevp; prevp = prevp->qf_next)
3517 ;
3518 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003519 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003520
3521 for (li = list->lv_first; li != NULL; li = li->li_next)
3522 {
3523 if (li->li_tv.v_type != VAR_DICT)
3524 continue; /* Skip non-dict items */
3525
3526 d = li->li_tv.vval.v_dict;
3527 if (d == NULL)
3528 continue;
3529
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003530 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003531 lnum = get_dict_number(d, (char_u *)"lnum");
3532 col = get_dict_number(d, (char_u *)"col");
3533 vcol = get_dict_number(d, (char_u *)"vcol");
3534 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003535 type = get_dict_string(d, (char_u *)"type", TRUE);
3536 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3537 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003538 if (text == NULL)
3539 text = vim_strsave((char_u *)"");
3540
3541 valid = TRUE;
3542 if (filename == NULL || (lnum == 0 && pattern == NULL))
3543 valid = FALSE;
3544
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003545 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003546 NULL, /* dir */
3547 filename,
3548 text,
3549 lnum,
3550 col,
3551 vcol, /* vis_col */
3552 pattern, /* search pattern */
3553 nr,
3554 type == NULL ? NUL : *type,
3555 valid);
3556
3557 vim_free(filename);
3558 vim_free(pattern);
3559 vim_free(text);
3560 vim_free(type);
3561
3562 if (status == FAIL)
3563 {
3564 retval = FAIL;
3565 break;
3566 }
3567 }
3568
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003569 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3570 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3571 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003572
3573#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003574 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003575#endif
3576
3577 return retval;
3578}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003579#endif
3580
Bram Moolenaar81695252004-12-29 20:58:21 +00003581/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003582 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003583 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003584 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003585 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003586 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003587 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003588 */
3589 void
3590ex_cbuffer(eap)
3591 exarg_T *eap;
3592{
3593 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003594 qf_info_T *qi = &ql_info;
3595
Bram Moolenaardb552d602006-03-23 22:59:57 +00003596 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3597 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003598 {
3599 qi = ll_get_or_alloc_list(curwin);
3600 if (qi == NULL)
3601 return;
3602 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003603
3604 if (*eap->arg == NUL)
3605 buf = curbuf;
3606 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3607 buf = buflist_findnr(atoi((char *)eap->arg));
3608 if (buf == NULL)
3609 EMSG(_(e_invarg));
3610 else if (buf->b_ml.ml_mfp == NULL)
3611 EMSG(_("E681: Buffer is not loaded"));
3612 else
3613 {
3614 if (eap->addr_count == 0)
3615 {
3616 eap->line1 = 1;
3617 eap->line2 = buf->b_ml.ml_line_count;
3618 }
3619 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3620 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3621 EMSG(_(e_invrange));
3622 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003623 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003624 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3625 (eap->cmdidx != CMD_caddbuffer
3626 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar754b5602006-02-09 23:53:20 +00003627 eap->line1, eap->line2) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003628 && (eap->cmdidx == CMD_cbuffer
3629 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003630 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3631 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003632 }
3633}
3634
Bram Moolenaar1e015462005-09-25 22:16:38 +00003635#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003636/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003637 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3638 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003639 */
3640 void
3641ex_cexpr(eap)
3642 exarg_T *eap;
3643{
3644 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003645 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003646
Bram Moolenaardb552d602006-03-23 22:59:57 +00003647 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3648 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003649 {
3650 qi = ll_get_or_alloc_list(curwin);
3651 if (qi == NULL)
3652 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003653 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003654
Bram Moolenaar4770d092006-01-12 23:22:24 +00003655 /* Evaluate the expression. When the result is a string or a list we can
3656 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003657 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003658 if (tv != NULL)
3659 {
3660 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3661 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3662 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003663 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3664 (eap->cmdidx != CMD_caddexpr
3665 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar4770d092006-01-12 23:22:24 +00003666 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003667 && (eap->cmdidx == CMD_cexpr
3668 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003669 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003670 }
3671 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003672 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003673 free_tv(tv);
3674 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003675}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003676#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003677
3678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 * ":helpgrep {pattern}"
3680 */
3681 void
3682ex_helpgrep(eap)
3683 exarg_T *eap;
3684{
3685 regmatch_T regmatch;
3686 char_u *save_cpo;
3687 char_u *p;
3688 int fcount;
3689 char_u **fnames;
3690 FILE *fd;
3691 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003692 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003694#ifdef FEAT_MULTI_LANG
3695 char_u *lang;
3696#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003697 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003698 int new_qi = FALSE;
3699 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3702 save_cpo = p_cpo;
3703 p_cpo = (char_u *)"";
3704
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003705#ifdef FEAT_MULTI_LANG
3706 /* Check for a specified language */
3707 lang = check_help_lang(eap->arg);
3708#endif
3709
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003710 if (eap->cmdidx == CMD_lhelpgrep)
3711 {
3712 /* Find an existing help window */
3713 FOR_ALL_WINDOWS(wp)
3714 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3715 break;
3716
3717 if (wp == NULL) /* Help window not found */
3718 qi = NULL;
3719 else
3720 qi = wp->w_llist;
3721
3722 if (qi == NULL)
3723 {
3724 /* Allocate a new location list for help text matches */
3725 if ((qi = ll_new_list()) == NULL)
3726 return;
3727 new_qi = TRUE;
3728 }
3729 }
3730
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3732 regmatch.rm_ic = FALSE;
3733 if (regmatch.regprog != NULL)
3734 {
3735 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003736 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 /* Go through all directories in 'runtimepath' */
3739 p = p_rtp;
3740 while (*p != NUL && !got_int)
3741 {
3742 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3743
3744 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3745 add_pathsep(NameBuff);
3746 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3747 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3748 &fnames, EW_FILE|EW_SILENT) == OK
3749 && fcount > 0)
3750 {
3751 for (fi = 0; fi < fcount && !got_int; ++fi)
3752 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003753#ifdef FEAT_MULTI_LANG
3754 /* Skip files for a different language. */
3755 if (lang != NULL
3756 && STRNICMP(lang, fnames[fi]
3757 + STRLEN(fnames[fi]) - 3, 2) != 0
3758 && !(STRNICMP(lang, "en", 2) == 0
3759 && STRNICMP("txt", fnames[fi]
3760 + STRLEN(fnames[fi]) - 3, 3) == 0))
3761 continue;
3762#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003763 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (fd != NULL)
3765 {
3766 lnum = 1;
3767 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3768 {
3769 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3770 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003771 int l = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /* remove trailing CR, LF, spaces, etc. */
3774 while (l > 0 && IObuff[l - 1] <= ' ')
3775 IObuff[--l] = NUL;
3776
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003777 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 NULL, /* dir */
3779 fnames[fi],
3780 IObuff,
3781 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003782 (int)(regmatch.startp[0] - IObuff)
3783 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003784 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003785 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 0, /* nr */
3787 1, /* type */
3788 TRUE /* valid */
3789 ) == FAIL)
3790 {
3791 got_int = TRUE;
3792 break;
3793 }
3794 }
3795 ++lnum;
3796 line_breakcheck();
3797 }
3798 fclose(fd);
3799 }
3800 }
3801 FreeWild(fcount, fnames);
3802 }
3803 }
3804 vim_free(regmatch.regprog);
3805
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003806 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3807 qi->qf_lists[qi->qf_curlist].qf_ptr =
3808 qi->qf_lists[qi->qf_curlist].qf_start;
3809 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811
3812 p_cpo = save_cpo;
3813
3814#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816#endif
3817
3818 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003819 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003820 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003821 else
3822 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003823
3824 if (eap->cmdidx == CMD_lhelpgrep)
3825 {
3826 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003827 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003828 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3829 {
3830 if (new_qi)
3831 ll_free_all(&qi);
3832 }
3833 else if (curwin->w_llist == NULL)
3834 curwin->w_llist = qi;
3835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836}
3837
3838#endif /* FEAT_QUICKFIX */