blob: d2b24bb10bd0544e4e89d774253d6dcfd30a0696 [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));
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000109static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110static 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 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000503 if (tv->v_type == VAR_STRING)
504 {
505 /* Get the next line from the supplied string */
506 char_u *p;
507
508 if (!*p_str) /* Reached the end of the string */
509 break;
510
511 p = vim_strchr(p_str, '\n');
512 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000513 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000514 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000515 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000516
517 if (len > CMDBUFFSIZE - 2)
518 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
519 else
520 vim_strncpy(IObuff, p_str, len);
521
522 p_str += len;
523 }
524 else if (tv->v_type == VAR_LIST)
525 {
526 /* Get the next line from the supplied list */
527 while (p_li && p_li->li_tv.v_type != VAR_STRING)
528 p_li = p_li->li_next; /* Skip non-string items */
529
530 if (!p_li) /* End of the list */
531 break;
532
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000533 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000534 if (len > CMDBUFFSIZE - 2)
535 len = CMDBUFFSIZE - 2;
536
537 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
538
539 p_li = p_li->li_next; /* next item */
540 }
541 }
542 else
543 {
544 /* Get the next line from the supplied buffer */
545 if (buflnum > lnumlast)
546 break;
547 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
548 CMDBUFFSIZE - 2);
549 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000550 }
551 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
552 break;
553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
555 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
556 *efmp = NUL;
557#ifdef USE_CRNL
558 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
559 *efmp = NUL;
560#endif
561
Bram Moolenaar01265852006-03-20 21:50:15 +0000562 /* If there was no %> item start at the first pattern */
563 if (fmt_start == NULL)
564 fmt_ptr = fmt_first;
565 else
566 {
567 fmt_ptr = fmt_start;
568 fmt_start = NULL;
569 }
570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 /*
572 * Try to match each part of 'errorformat' until we find a complete
573 * match or no match.
574 */
575 valid = TRUE;
576restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000577 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 idx = fmt_ptr->prefix;
580 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
581 continue;
582 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000583 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (!multiscan)
585 errmsg[0] = NUL;
586 lnum = 0;
587 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000588 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 enr = -1;
590 type = 0;
591 tail = NULL;
592
593 regmatch.regprog = fmt_ptr->prog;
594 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
595 {
596 if ((idx == 'C' || idx == 'Z') && !multiline)
597 continue;
598 if (vim_strchr((char_u *)"EWI", idx) != NULL)
599 type = idx;
600 else
601 type = 0;
602 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000603 * Extract error message data from matched line.
604 * We check for an actual submatch, because "\[" and "\]" in
605 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 */
607 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
608 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000609 int c;
610
611 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
612 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000613
614 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000615 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000616 *regmatch.endp[i] = NUL;
617 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
618 *regmatch.endp[i] = c;
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000621 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 continue;
623 }
624 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000625 {
626 if (regmatch.startp[i] == NULL)
627 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000631 {
632 if (regmatch.startp[i] == NULL)
633 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000637 {
638 if (regmatch.startp[i] == NULL)
639 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000643 {
644 if (regmatch.startp[i] == NULL)
645 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000647 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000648 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 STRCPY(errmsg, IObuff);
650 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
651 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000652 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
653 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000655 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000658 {
659 if (regmatch.startp[i] == NULL)
660 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
664 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000665 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
666 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
668 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000669 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
672 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000673 if (regmatch.startp[i] == NULL)
674 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000676 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000678 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
679 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000680 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
681 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000682 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
683 if (len > CMDBUFFSIZE - 5)
684 len = CMDBUFFSIZE - 5;
685 STRCPY(pattern, "^\\V");
686 STRNCAT(pattern, regmatch.startp[i], len);
687 pattern[len + 3] = '\\';
688 pattern[len + 4] = '$';
689 pattern[len + 5] = NUL;
690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 break;
692 }
693 }
694 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000695
Bram Moolenaar4770d092006-01-12 23:22:24 +0000696 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000698 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
700 if (idx == 'D') /* enter directory */
701 {
702 if (*namebuf == NUL)
703 {
704 EMSG(_("E379: Missing or empty directory name"));
705 goto error2;
706 }
707 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
708 goto error2;
709 }
710 else if (idx == 'X') /* leave directory */
711 directory = qf_pop_dir(&dir_stack);
712 }
713 namebuf[0] = NUL; /* no match found, remove file name */
714 lnum = 0; /* don't jump to this line */
715 valid = FALSE;
716 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000717 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 multiline = multiignore = FALSE;
719 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000720 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000722 /* honor %> item */
723 if (fmt_ptr->conthere)
724 fmt_start = fmt_ptr;
725
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
727 multiline = TRUE; /* start of a multi-line message */
728 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
729 { /* continuation of multi-line msg */
730 if (qfprev == NULL)
731 goto error2;
732 if (*errmsg && !multiignore)
733 {
734 len = (int)STRLEN(qfprev->qf_text);
735 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
736 == NULL)
737 goto error2;
738 STRCPY(ptr, qfprev->qf_text);
739 vim_free(qfprev->qf_text);
740 qfprev->qf_text = ptr;
741 *(ptr += len) = '\n';
742 STRCPY(++ptr, errmsg);
743 }
744 if (qfprev->qf_nr == -1)
745 qfprev->qf_nr = enr;
746 if (vim_isprintc(type) && !qfprev->qf_type)
747 qfprev->qf_type = type; /* only printable chars allowed */
748 if (!qfprev->qf_lnum)
749 qfprev->qf_lnum = lnum;
750 if (!qfprev->qf_col)
751 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000752 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (!qfprev->qf_fnum)
754 qfprev->qf_fnum = qf_get_fnum(directory,
755 *namebuf || directory ? namebuf
756 : currfile && valid ? currfile : 0);
757 if (idx == 'Z')
758 multiline = multiignore = FALSE;
759 line_breakcheck();
760 continue;
761 }
762 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
763 {
764 /* global file names */
765 valid = FALSE;
766 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
767 {
768 if (*namebuf && idx == 'P')
769 currfile = qf_push_dir(namebuf, &file_stack);
770 else if (idx == 'Q')
771 currfile = qf_pop_dir(&file_stack);
772 *namebuf = NUL;
773 if (tail && *tail)
774 {
775 STRCPY(IObuff, skipwhite(tail));
776 multiscan = TRUE;
777 goto restofline;
778 }
779 }
780 }
781 if (fmt_ptr->flags == '-') /* generally exclude this line */
782 {
783 if (multiline)
784 multiignore = TRUE; /* also exclude continuation lines */
785 continue;
786 }
787 }
788
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000789 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000791 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000793 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000794 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 errmsg,
796 lnum,
797 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000799 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 enr,
801 type,
802 valid) == FAIL)
803 goto error2;
804 line_breakcheck();
805 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000806 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000808 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000810 /* no valid entry found */
811 qi->qf_lists[qi->qf_curlist].qf_ptr =
812 qi->qf_lists[qi->qf_curlist].qf_start;
813 qi->qf_lists[qi->qf_curlist].qf_index = 1;
814 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 else
817 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000818 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
819 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
820 qi->qf_lists[qi->qf_curlist].qf_ptr =
821 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000823 /* return number of matches */
824 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 goto qf_init_ok;
826 }
827 EMSG(_(e_readerrf));
828error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000829 qf_free(qi, qi->qf_curlist);
830 qi->qf_listcount--;
831 if (qi->qf_curlist > 0)
832 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000834 if (fd != NULL)
835 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
837 {
838 fmt_first = fmt_ptr->next;
839 vim_free(fmt_ptr->prog);
840 vim_free(fmt_ptr);
841 }
842 qf_clean_dir_stack(&dir_stack);
843 qf_clean_dir_stack(&file_stack);
844qf_init_end:
845 vim_free(namebuf);
846 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000847 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 vim_free(fmtstr);
849
850#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000851 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852#endif
853
854 return retval;
855}
856
857/*
858 * Prepare for adding a new quickfix list.
859 */
860 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000861qf_new_list(qi)
862 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
864 int i;
865
866 /*
867 * If the current entry is not the last entry, delete entries below
868 * the current entry. This makes it possible to browse in a tree-like
869 * way with ":grep'.
870 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000871 while (qi->qf_listcount > qi->qf_curlist + 1)
872 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874 /*
875 * When the stack is full, remove to oldest entry
876 * Otherwise, add a new entry.
877 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000878 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000880 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000882 qi->qf_lists[i - 1] = qi->qf_lists[i];
883 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
885 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000886 qi->qf_curlist = qi->qf_listcount++;
887 qi->qf_lists[qi->qf_curlist].qf_index = 0;
888 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889}
890
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000891/*
892 * Free a location list
893 */
894 static void
895ll_free_all(pqi)
896 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000897{
898 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000899 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000900
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000901 qi = *pqi;
902 if (qi == NULL)
903 return;
904 *pqi = NULL; /* Remove reference to this list */
905
906 qi->qf_refcount--;
907 if (qi->qf_refcount < 1)
908 {
909 /* No references to this location list */
910 for (i = 0; i < qi->qf_listcount; ++i)
911 qf_free(qi, i);
912 vim_free(qi);
913 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000914}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000915
916 void
917qf_free_all(wp)
918 win_T *wp;
919{
920 int i;
921 qf_info_T *qi = &ql_info;
922
923 if (wp != NULL)
924 {
925 /* location list */
926 ll_free_all(&wp->w_llist);
927 ll_free_all(&wp->w_llist_ref);
928 }
929 else
930 /* quickfix list */
931 for (i = 0; i < qi->qf_listcount; ++i)
932 qf_free(qi, i);
933}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000934
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935/*
936 * Add an entry to the end of the list of errors.
937 * Returns OK or FAIL.
938 */
939 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000940qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
941 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000942 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000943 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 char_u *dir; /* optional directory name */
945 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000946 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 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;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000960 if (bufnum != 0)
961 qfp->qf_fnum = bufnum;
962 else
963 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
965 {
966 vim_free(qfp);
967 return FAIL;
968 }
969 qfp->qf_lnum = lnum;
970 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000971 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000972 if (pattern == NULL || *pattern == NUL)
973 qfp->qf_pattern = NULL;
974 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
975 {
976 vim_free(qfp->qf_text);
977 vim_free(qfp);
978 return FAIL;
979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 qfp->qf_nr = nr;
981 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
982 type = 0;
983 qfp->qf_type = type;
984 qfp->qf_valid = valid;
985
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000986 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
987 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000989 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 qfp->qf_prev = qfp; /* first element points to itself */
991 }
992 else
993 {
994 qfp->qf_prev = *prevp;
995 (*prevp)->qf_next = qfp;
996 }
997 qfp->qf_next = qfp; /* last element points to itself */
998 qfp->qf_cleared = FALSE;
999 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001000 ++qi->qf_lists[qi->qf_curlist].qf_count;
1001 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1002 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001004 qi->qf_lists[qi->qf_curlist].qf_index =
1005 qi->qf_lists[qi->qf_curlist].qf_count;
1006 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008
1009 return OK;
1010}
1011
1012/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001013 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001014 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001015 static qf_info_T *
1016ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001017{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001018 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001019
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001020 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1021 if (qi != NULL)
1022 {
1023 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1024 qi->qf_refcount++;
1025 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001026
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001027 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001028}
1029
1030/*
1031 * Return the location list for window 'wp'.
1032 * If not present, allocate a location list
1033 */
1034 static qf_info_T *
1035ll_get_or_alloc_list(wp)
1036 win_T *wp;
1037{
1038 if (IS_LL_WINDOW(wp))
1039 /* For a location list window, use the referenced location list */
1040 return wp->w_llist_ref;
1041
1042 /*
1043 * For a non-location list window, w_llist_ref should not point to a
1044 * location list.
1045 */
1046 ll_free_all(&wp->w_llist_ref);
1047
1048 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001049 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001050 return wp->w_llist;
1051}
1052
1053/*
1054 * Copy the location list from window "from" to window "to".
1055 */
1056 void
1057copy_loclist(from, to)
1058 win_T *from;
1059 win_T *to;
1060{
1061 qf_info_T *qi;
1062 int idx;
1063 int i;
1064
1065 /*
1066 * When copying from a location list window, copy the referenced
1067 * location list. For other windows, copy the location list for
1068 * that window.
1069 */
1070 if (IS_LL_WINDOW(from))
1071 qi = from->w_llist_ref;
1072 else
1073 qi = from->w_llist;
1074
1075 if (qi == NULL) /* no location list to copy */
1076 return;
1077
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001078 /* allocate a new location list */
1079 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001080 return;
1081
1082 to->w_llist->qf_listcount = qi->qf_listcount;
1083
1084 /* Copy the location lists one at a time */
1085 for (idx = 0; idx < qi->qf_listcount; idx++)
1086 {
1087 qf_list_T *from_qfl;
1088 qf_list_T *to_qfl;
1089
1090 to->w_llist->qf_curlist = idx;
1091
1092 from_qfl = &qi->qf_lists[idx];
1093 to_qfl = &to->w_llist->qf_lists[idx];
1094
1095 /* Some of the fields are populated by qf_add_entry() */
1096 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1097 to_qfl->qf_count = 0;
1098 to_qfl->qf_index = 0;
1099 to_qfl->qf_start = NULL;
1100 to_qfl->qf_ptr = NULL;
1101
1102 if (from_qfl->qf_count)
1103 {
1104 qfline_T *from_qfp;
1105 qfline_T *prevp = NULL;
1106
1107 /* copy all the location entries in this list */
1108 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1109 ++i, from_qfp = from_qfp->qf_next)
1110 {
1111 if (qf_add_entry(to->w_llist, &prevp,
1112 NULL,
1113 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001114 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001115 from_qfp->qf_text,
1116 from_qfp->qf_lnum,
1117 from_qfp->qf_col,
1118 from_qfp->qf_viscol,
1119 from_qfp->qf_pattern,
1120 from_qfp->qf_nr,
1121 0,
1122 from_qfp->qf_valid) == FAIL)
1123 {
1124 qf_free_all(to);
1125 return;
1126 }
1127 /*
1128 * qf_add_entry() will not set the qf_num field, as the
1129 * directory and file names are not supplied. So the qf_fnum
1130 * field is copied here.
1131 */
1132 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1133 prevp->qf_type = from_qfp->qf_type; /* error type */
1134 if (from_qfl->qf_ptr == from_qfp)
1135 to_qfl->qf_ptr = prevp; /* current location */
1136 }
1137 }
1138
1139 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1140
1141 /* When no valid entries are present in the list, qf_ptr points to
1142 * the first item in the list */
1143 if (to_qfl->qf_nonevalid == TRUE)
1144 to_qfl->qf_ptr = to_qfl->qf_start;
1145 }
1146
1147 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1148}
1149
1150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * get buffer number for file "dir.name"
1152 */
1153 static int
1154qf_get_fnum(directory, fname)
1155 char_u *directory;
1156 char_u *fname;
1157{
1158 if (fname == NULL || *fname == NUL) /* no file name */
1159 return 0;
1160 {
1161#ifdef RISCOS
1162 /* Name is reported as `main.c', but file is `c.main' */
1163 return ro_buflist_add(fname);
1164#else
1165 char_u *ptr;
1166 int fnum;
1167
1168# ifdef VMS
1169 vms_remove_version(fname);
1170# endif
1171# ifdef BACKSLASH_IN_FILENAME
1172 if (directory != NULL)
1173 slash_adjust(directory);
1174 slash_adjust(fname);
1175# endif
1176 if (directory != NULL && !vim_isAbsName(fname)
1177 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1178 {
1179 /*
1180 * Here we check if the file really exists.
1181 * This should normally be true, but if make works without
1182 * "leaving directory"-messages we might have missed a
1183 * directory change.
1184 */
1185 if (mch_getperm(ptr) < 0)
1186 {
1187 vim_free(ptr);
1188 directory = qf_guess_filepath(fname);
1189 if (directory)
1190 ptr = concat_fnames(directory, fname, TRUE);
1191 else
1192 ptr = vim_strsave(fname);
1193 }
1194 /* Use concatenated directory name and file name */
1195 fnum = buflist_add(ptr, 0);
1196 vim_free(ptr);
1197 return fnum;
1198 }
1199 return buflist_add(fname, 0);
1200#endif
1201 }
1202}
1203
1204/*
1205 * push dirbuf onto the directory stack and return pointer to actual dir or
1206 * NULL on error
1207 */
1208 static char_u *
1209qf_push_dir(dirbuf, stackptr)
1210 char_u *dirbuf;
1211 struct dir_stack_T **stackptr;
1212{
1213 struct dir_stack_T *ds_new;
1214 struct dir_stack_T *ds_ptr;
1215
1216 /* allocate new stack element and hook it in */
1217 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1218 if (ds_new == NULL)
1219 return NULL;
1220
1221 ds_new->next = *stackptr;
1222 *stackptr = ds_new;
1223
1224 /* store directory on the stack */
1225 if (vim_isAbsName(dirbuf)
1226 || (*stackptr)->next == NULL
1227 || (*stackptr && dir_stack != *stackptr))
1228 (*stackptr)->dirname = vim_strsave(dirbuf);
1229 else
1230 {
1231 /* Okay we don't have an absolute path.
1232 * dirbuf must be a subdir of one of the directories on the stack.
1233 * Let's search...
1234 */
1235 ds_new = (*stackptr)->next;
1236 (*stackptr)->dirname = NULL;
1237 while (ds_new)
1238 {
1239 vim_free((*stackptr)->dirname);
1240 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1241 TRUE);
1242 if (mch_isdir((*stackptr)->dirname) == TRUE)
1243 break;
1244
1245 ds_new = ds_new->next;
1246 }
1247
1248 /* clean up all dirs we already left */
1249 while ((*stackptr)->next != ds_new)
1250 {
1251 ds_ptr = (*stackptr)->next;
1252 (*stackptr)->next = (*stackptr)->next->next;
1253 vim_free(ds_ptr->dirname);
1254 vim_free(ds_ptr);
1255 }
1256
1257 /* Nothing found -> it must be on top level */
1258 if (ds_new == NULL)
1259 {
1260 vim_free((*stackptr)->dirname);
1261 (*stackptr)->dirname = vim_strsave(dirbuf);
1262 }
1263 }
1264
1265 if ((*stackptr)->dirname != NULL)
1266 return (*stackptr)->dirname;
1267 else
1268 {
1269 ds_ptr = *stackptr;
1270 *stackptr = (*stackptr)->next;
1271 vim_free(ds_ptr);
1272 return NULL;
1273 }
1274}
1275
1276
1277/*
1278 * pop dirbuf from the directory stack and return previous directory or NULL if
1279 * stack is empty
1280 */
1281 static char_u *
1282qf_pop_dir(stackptr)
1283 struct dir_stack_T **stackptr;
1284{
1285 struct dir_stack_T *ds_ptr;
1286
1287 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1288 * What to do if it isn't? */
1289
1290 /* pop top element and free it */
1291 if (*stackptr != NULL)
1292 {
1293 ds_ptr = *stackptr;
1294 *stackptr = (*stackptr)->next;
1295 vim_free(ds_ptr->dirname);
1296 vim_free(ds_ptr);
1297 }
1298
1299 /* return NEW top element as current dir or NULL if stack is empty*/
1300 return *stackptr ? (*stackptr)->dirname : NULL;
1301}
1302
1303/*
1304 * clean up directory stack
1305 */
1306 static void
1307qf_clean_dir_stack(stackptr)
1308 struct dir_stack_T **stackptr;
1309{
1310 struct dir_stack_T *ds_ptr;
1311
1312 while ((ds_ptr = *stackptr) != NULL)
1313 {
1314 *stackptr = (*stackptr)->next;
1315 vim_free(ds_ptr->dirname);
1316 vim_free(ds_ptr);
1317 }
1318}
1319
1320/*
1321 * Check in which directory of the directory stack the given file can be
1322 * found.
1323 * Returns a pointer to the directory name or NULL if not found
1324 * Cleans up intermediate directory entries.
1325 *
1326 * TODO: How to solve the following problem?
1327 * If we have the this directory tree:
1328 * ./
1329 * ./aa
1330 * ./aa/bb
1331 * ./bb
1332 * ./bb/x.c
1333 * and make says:
1334 * making all in aa
1335 * making all in bb
1336 * x.c:9: Error
1337 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1338 * qf_guess_filepath will return NULL.
1339 */
1340 static char_u *
1341qf_guess_filepath(filename)
1342 char_u *filename;
1343{
1344 struct dir_stack_T *ds_ptr;
1345 struct dir_stack_T *ds_tmp;
1346 char_u *fullname;
1347
1348 /* no dirs on the stack - there's nothing we can do */
1349 if (dir_stack == NULL)
1350 return NULL;
1351
1352 ds_ptr = dir_stack->next;
1353 fullname = NULL;
1354 while (ds_ptr)
1355 {
1356 vim_free(fullname);
1357 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1358
1359 /* If concat_fnames failed, just go on. The worst thing that can happen
1360 * is that we delete the entire stack.
1361 */
1362 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1363 break;
1364
1365 ds_ptr = ds_ptr->next;
1366 }
1367
1368 vim_free(fullname);
1369
1370 /* clean up all dirs we already left */
1371 while (dir_stack->next != ds_ptr)
1372 {
1373 ds_tmp = dir_stack->next;
1374 dir_stack->next = dir_stack->next->next;
1375 vim_free(ds_tmp->dirname);
1376 vim_free(ds_tmp);
1377 }
1378
1379 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1380
1381}
1382
1383/*
1384 * jump to a quickfix line
1385 * if dir == FORWARD go "errornr" valid entries forward
1386 * if dir == BACKWARD go "errornr" valid entries backward
1387 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1388 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1389 * else if "errornr" is zero, redisplay the same line
1390 * else go to entry "errornr"
1391 */
1392 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001393qf_jump(qi, dir, errornr, forceit)
1394 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 int dir;
1396 int errornr;
1397 int forceit;
1398{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001399 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001400 qfline_T *qf_ptr;
1401 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 int qf_index;
1403 int old_qf_fnum;
1404 int old_qf_index;
1405 int prev_index;
1406 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1407 char_u *err = e_no_more_items;
1408 linenr_T i;
1409 buf_T *old_curbuf;
1410 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 colnr_T screen_col;
1412 colnr_T char_col;
1413 char_u *line;
1414#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001415 char_u *old_swb = p_swb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 int opened_window = FALSE;
1417 win_T *win;
1418 win_T *altwin;
1419#endif
1420 int print_message = TRUE;
1421 int len;
1422#ifdef FEAT_FOLDING
1423 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1424#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001425 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001428 if (qi == NULL)
1429 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001430
1431 if (qi->qf_curlist >= qi->qf_listcount
1432 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 {
1434 EMSG(_(e_quickfix));
1435 return;
1436 }
1437
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001438 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001440 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 old_qf_index = qf_index;
1442 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1443 {
1444 while (errornr--)
1445 {
1446 old_qf_ptr = qf_ptr;
1447 prev_index = qf_index;
1448 old_qf_fnum = qf_ptr->qf_fnum;
1449 do
1450 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001451 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 || qf_ptr->qf_next == NULL)
1453 {
1454 qf_ptr = old_qf_ptr;
1455 qf_index = prev_index;
1456 if (err != NULL)
1457 {
1458 EMSG(_(err));
1459 goto theend;
1460 }
1461 errornr = 0;
1462 break;
1463 }
1464 ++qf_index;
1465 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001466 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1467 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1469 err = NULL;
1470 }
1471 }
1472 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1473 {
1474 while (errornr--)
1475 {
1476 old_qf_ptr = qf_ptr;
1477 prev_index = qf_index;
1478 old_qf_fnum = qf_ptr->qf_fnum;
1479 do
1480 {
1481 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1482 {
1483 qf_ptr = old_qf_ptr;
1484 qf_index = prev_index;
1485 if (err != NULL)
1486 {
1487 EMSG(_(err));
1488 goto theend;
1489 }
1490 errornr = 0;
1491 break;
1492 }
1493 --qf_index;
1494 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001495 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1496 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1498 err = NULL;
1499 }
1500 }
1501 else if (errornr != 0) /* go to specified number */
1502 {
1503 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1504 {
1505 --qf_index;
1506 qf_ptr = qf_ptr->qf_prev;
1507 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001508 while (errornr > qf_index && qf_index <
1509 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 && qf_ptr->qf_next != NULL)
1511 {
1512 ++qf_index;
1513 qf_ptr = qf_ptr->qf_next;
1514 }
1515 }
1516
1517#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001518 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1519 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 /* No need to print the error message if it's visible in the error
1521 * window */
1522 print_message = FALSE;
1523
1524 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001525 * For ":helpgrep" find a help window or open one.
1526 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001527 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001528 {
1529 win_T *wp;
1530 int n;
1531
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001532 if (cmdmod.tab != 0)
1533 wp = NULL;
1534 else
1535 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1536 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1537 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001538 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1539 win_enter(wp, TRUE);
1540 else
1541 {
1542 /*
1543 * Split off help window; put it at far top if no position
1544 * specified, the current window is vertically split and narrow.
1545 */
1546 n = WSP_HELP;
1547# ifdef FEAT_VERTSPLIT
1548 if (cmdmod.split == 0 && curwin->w_width != Columns
1549 && curwin->w_width < 80)
1550 n |= WSP_TOP;
1551# endif
1552 if (win_split(0, n) == FAIL)
1553 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001554 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001555
1556 if (curwin->w_height < p_hh)
1557 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001558
1559 if (qi != &ql_info) /* not a quickfix list */
1560 {
1561 /* The new window should use the supplied location list */
1562 qf_free_all(curwin);
1563 curwin->w_llist = qi;
1564 qi->qf_refcount++;
1565 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001566 }
1567
1568 if (!p_im)
1569 restart_edit = 0; /* don't want insert mode in help file */
1570 }
1571
1572 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 * If currently in the quickfix window, find another window to show the
1574 * file in.
1575 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001576 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
1578 /*
1579 * If there is no file specified, we don't know where to go.
1580 * But do advance, otherwise ":cn" gets stuck.
1581 */
1582 if (qf_ptr->qf_fnum == 0)
1583 goto theend;
1584
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001585 /* Locate a window showing a normal buffer */
1586 usable_win = 0;
1587 FOR_ALL_WINDOWS(win)
1588 if (win->w_buffer->b_p_bt[0] == NUL)
1589 {
1590 usable_win = 1;
1591 break;
1592 }
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 /*
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001595 * If no usable window is found and 'switchbuf' is set to 'usetab'
1596 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001598 if (!usable_win && vim_strchr(p_swb, 'a') != NULL)
1599 {
1600 tabpage_T *tp;
1601 win_T *wp;
1602
1603 FOR_ALL_TAB_WINDOWS(tp, wp)
1604 {
1605 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1606 {
1607 goto_tabpage_win(tp, wp);
1608 usable_win = 1;
1609 break;
1610 }
1611 }
1612 }
1613
1614 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001615 * If there is only one window and it is the quickfix window, create a
1616 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001617 */
1618 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001620 ll_ref = curwin->w_llist_ref;
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (win_split(0, WSP_ABOVE) == FAIL)
1623 goto failed; /* not enough room for window */
1624 opened_window = TRUE; /* close it when fail */
1625 p_swb = empty_option; /* don't split again */
1626# ifdef FEAT_SCROLLBIND
1627 curwin->w_p_scb = FALSE;
1628# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001629 if (ll_ref != NULL)
1630 {
1631 /* The new window should use the location list from the
1632 * location list window */
1633 qf_free_all(curwin);
1634 curwin->w_llist = ll_ref;
1635 ll_ref->qf_refcount++;
1636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
1638 else
1639 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001640 if (curwin->w_llist_ref != NULL)
1641 {
1642 /* In a location window */
1643 ll_ref = curwin->w_llist_ref;
1644
1645 /* Find the window with the same location list */
1646 FOR_ALL_WINDOWS(win)
1647 if (win->w_llist == ll_ref)
1648 break;
1649 if (win == NULL)
1650 {
1651 /* Find the window showing the selected file */
1652 FOR_ALL_WINDOWS(win)
1653 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1654 break;
1655 if (win == NULL)
1656 {
1657 /* Find a previous usable window */
1658 win = curwin;
1659 do
1660 {
1661 if (win->w_buffer->b_p_bt[0] == NUL)
1662 break;
1663 if (win->w_prev == NULL)
1664 win = lastwin; /* wrap around the top */
1665 else
1666 win = win->w_prev; /* go to previous window */
1667 } while (win != curwin);
1668 }
1669 }
1670 win_goto(win);
1671
1672 /* If the location list for the window is not set, then set it
1673 * to the location list from the location window */
1674 if (win->w_llist == NULL)
1675 {
1676 win->w_llist = ll_ref;
1677 ll_ref->qf_refcount++;
1678 }
1679 }
1680 else
1681 {
1682
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 /*
1684 * Try to find a window that shows the right buffer.
1685 * Default to the window just above the quickfix buffer.
1686 */
1687 win = curwin;
1688 altwin = NULL;
1689 for (;;)
1690 {
1691 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1692 break;
1693 if (win->w_prev == NULL)
1694 win = lastwin; /* wrap around the top */
1695 else
1696 win = win->w_prev; /* go to previous window */
1697
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001698 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
1700 /* Didn't find it, go to the window before the quickfix
1701 * window. */
1702 if (altwin != NULL)
1703 win = altwin;
1704 else if (curwin->w_prev != NULL)
1705 win = curwin->w_prev;
1706 else
1707 win = curwin->w_next;
1708 break;
1709 }
1710
1711 /* Remember a usable window. */
1712 if (altwin == NULL && !win->w_p_pvw
1713 && win->w_buffer->b_p_bt[0] == NUL)
1714 altwin = win;
1715 }
1716
1717 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721#endif
1722
1723 /*
1724 * If there is a file name,
1725 * read the wanted file if needed, and check autowrite etc.
1726 */
1727 old_curbuf = curbuf;
1728 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001729
1730 if (qf_ptr->qf_fnum != 0)
1731 {
1732 if (qf_ptr->qf_type == 1)
1733 {
1734 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1735 * set b_p_ro flag). */
1736 if (!can_abandon(curbuf, forceit))
1737 {
1738 EMSG(_(e_nowrtmsg));
1739 ok = FALSE;
1740 }
1741 else
1742 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1743 ECMD_HIDE + ECMD_SET_HELP);
1744 }
1745 else
1746 ok = buflist_getfile(qf_ptr->qf_fnum,
1747 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1748 }
1749
1750 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
1752 /* When not switched to another buffer, still need to set pc mark */
1753 if (curbuf == old_curbuf)
1754 setpcmark();
1755
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001756 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001758 /*
1759 * Go to line with error, unless qf_lnum is 0.
1760 */
1761 i = qf_ptr->qf_lnum;
1762 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001764 if (i > curbuf->b_ml.ml_line_count)
1765 i = curbuf->b_ml.ml_line_count;
1766 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001768 if (qf_ptr->qf_col > 0)
1769 {
1770 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1771 if (qf_ptr->qf_viscol == TRUE)
1772 {
1773 /*
1774 * Check each character from the beginning of the error
1775 * line up to the error column. For each tab character
1776 * found, reduce the error column value by the length of
1777 * a tab character.
1778 */
1779 line = ml_get_curline();
1780 screen_col = 0;
1781 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1782 {
1783 if (*line == NUL)
1784 break;
1785 if (*line++ == '\t')
1786 {
1787 curwin->w_cursor.col -= 7 - (screen_col % 8);
1788 screen_col += 8 - (screen_col % 8);
1789 }
1790 else
1791 ++screen_col;
1792 }
1793 }
1794 check_cursor();
1795 }
1796 else
1797 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
1799 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001800 {
1801 pos_T save_cursor;
1802
1803 /* Move the cursor to the first line in the buffer */
1804 save_cursor = curwin->w_cursor;
1805 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001806 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1807 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001808 curwin->w_cursor = save_cursor;
1809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811#ifdef FEAT_FOLDING
1812 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1813 foldOpenCursor();
1814#endif
1815 if (print_message)
1816 {
1817 /* Update the screen before showing the message */
1818 update_topline_redraw();
1819 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001820 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1822 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1823 /* Add the message, skipping leading whitespace and newlines. */
1824 len = (int)STRLEN(IObuff);
1825 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1826
1827 /* Output the message. Overwrite to avoid scrolling when the 'O'
1828 * flag is present in 'shortmess'; But when not jumping, print the
1829 * whole message. */
1830 i = msg_scroll;
1831 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1832 msg_scroll = TRUE;
1833 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1834 msg_scroll = FALSE;
1835 msg_attr_keep(IObuff, 0, TRUE);
1836 msg_scroll = i;
1837 }
1838 }
1839 else
1840 {
1841#ifdef FEAT_WINDOWS
1842 if (opened_window)
1843 win_close(curwin, TRUE); /* Close opened window */
1844#endif
1845 if (qf_ptr->qf_fnum != 0)
1846 {
1847 /*
1848 * Couldn't open file, so put index back where it was. This could
1849 * happen if the file was readonly and we changed something.
1850 */
1851#ifdef FEAT_WINDOWS
1852failed:
1853#endif
1854 qf_ptr = old_qf_ptr;
1855 qf_index = old_qf_index;
1856 }
1857 }
1858theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001859 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1860 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861#ifdef FEAT_WINDOWS
1862 if (p_swb != old_swb && opened_window)
1863 {
1864 /* Restore old 'switchbuf' value, but not when an autocommand or
1865 * modeline has changed the value. */
1866 if (p_swb == empty_option)
1867 p_swb = old_swb;
1868 else
1869 free_string_option(old_swb);
1870 }
1871#endif
1872}
1873
1874/*
1875 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001876 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 */
1878 void
1879qf_list(eap)
1880 exarg_T *eap;
1881{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001882 buf_T *buf;
1883 char_u *fname;
1884 qfline_T *qfp;
1885 int i;
1886 int idx1 = 1;
1887 int idx2 = -1;
1888 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001889 char_u *arg = eap->arg;
1890 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001894 if (eap->cmdidx == CMD_llist)
1895 {
1896 qi = GET_LOC_LIST(curwin);
1897 if (qi == NULL)
1898 {
1899 EMSG(_(e_loclist));
1900 return;
1901 }
1902 }
1903
1904 if (qi->qf_curlist >= qi->qf_listcount
1905 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 EMSG(_(e_quickfix));
1908 return;
1909 }
1910 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1911 {
1912 EMSG(_(e_trailing));
1913 return;
1914 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001915 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (idx1 < 0)
1917 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1918 if (idx2 < 0)
1919 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1920
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001923 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1924 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
1926 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1927 {
1928 if (need_return)
1929 {
1930 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001931 if (got_int)
1932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 need_return = FALSE;
1934 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001935
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001936 fname = NULL;
1937 if (qfp->qf_fnum != 0
1938 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1939 {
1940 fname = buf->b_fname;
1941 if (qfp->qf_type == 1) /* :helpgrep */
1942 fname = gettail(fname);
1943 }
1944 if (fname == NULL)
1945 sprintf((char *)IObuff, "%2d", i);
1946 else
1947 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1948 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001949 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001950 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1951 if (qfp->qf_lnum == 0)
1952 IObuff[0] = NUL;
1953 else if (qfp->qf_col == 0)
1954 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1955 else
1956 sprintf((char *)IObuff, ":%ld col %d",
1957 qfp->qf_lnum, qfp->qf_col);
1958 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1959 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1960 msg_puts_attr(IObuff, hl_attr(HLF_N));
1961 if (qfp->qf_pattern != NULL)
1962 {
1963 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1964 STRCAT(IObuff, ":");
1965 msg_puts(IObuff);
1966 }
1967 msg_puts((char_u *)" ");
1968
1969 /* Remove newlines and leading whitespace from the text. For an
1970 * unrecognized line keep the indent, the compiler may mark a word
1971 * with ^^^^. */
1972 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1974 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001975 msg_prt_line(IObuff, FALSE);
1976 out_flush(); /* show one line at a time */
1977 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001979
1980 qfp = qfp->qf_next;
1981 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 ui_breakcheck();
1983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984}
1985
1986/*
1987 * Remove newlines and leading whitespace from an error message.
1988 * Put the result in "buf[bufsize]".
1989 */
1990 static void
1991qf_fmt_text(text, buf, bufsize)
1992 char_u *text;
1993 char_u *buf;
1994 int bufsize;
1995{
1996 int i;
1997 char_u *p = text;
1998
1999 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2000 {
2001 if (*p == '\n')
2002 {
2003 buf[i] = ' ';
2004 while (*++p != NUL)
2005 if (!vim_iswhite(*p) && *p != '\n')
2006 break;
2007 }
2008 else
2009 buf[i] = *p++;
2010 }
2011 buf[i] = NUL;
2012}
2013
2014/*
2015 * ":colder [count]": Up in the quickfix stack.
2016 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 * ":lolder [count]": Up in the location list stack.
2018 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 */
2020 void
2021qf_age(eap)
2022 exarg_T *eap;
2023{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002024 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 int count;
2026
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002027 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2028 {
2029 qi = GET_LOC_LIST(curwin);
2030 if (qi == NULL)
2031 {
2032 EMSG(_(e_loclist));
2033 return;
2034 }
2035 }
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (eap->addr_count != 0)
2038 count = eap->line2;
2039 else
2040 count = 1;
2041 while (count--)
2042 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002043 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002045 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
2047 EMSG(_("E380: At bottom of quickfix stack"));
2048 return;
2049 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002050 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052 else
2053 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002054 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 EMSG(_("E381: At top of quickfix stack"));
2057 return;
2058 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002059 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002062 qf_msg(qi);
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064}
2065
2066 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002067qf_msg(qi)
2068 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
2070 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071 qi->qf_curlist + 1, qi->qf_listcount,
2072 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002074 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075#endif
2076}
2077
2078/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002079 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 */
2081 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002082qf_free(qi, idx)
2083 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 int idx;
2085{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002086 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 qfp = qi->qf_lists[idx].qf_start->qf_next;
2091 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2092 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2093 vim_free(qi->qf_lists[idx].qf_start);
2094 qi->qf_lists[idx].qf_start = qfp;
2095 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097}
2098
2099/*
2100 * qf_mark_adjust: adjust marks
2101 */
2102 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002103qf_mark_adjust(wp, line1, line2, amount, amount_after)
2104 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 linenr_T line1;
2106 linenr_T line2;
2107 long amount;
2108 long amount_after;
2109{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002110 int i;
2111 qfline_T *qfp;
2112 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 if (wp != NULL)
2116 {
2117 if (wp->w_llist == NULL)
2118 return;
2119 qi = wp->w_llist;
2120 }
2121
2122 for (idx = 0; idx < qi->qf_listcount; ++idx)
2123 if (qi->qf_lists[idx].qf_count)
2124 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2125 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (qfp->qf_fnum == curbuf->b_fnum)
2127 {
2128 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2129 {
2130 if (amount == MAXLNUM)
2131 qfp->qf_cleared = TRUE;
2132 else
2133 qfp->qf_lnum += amount;
2134 }
2135 else if (amount_after && qfp->qf_lnum > line2)
2136 qfp->qf_lnum += amount_after;
2137 }
2138}
2139
2140/*
2141 * Make a nice message out of the error character and the error number:
2142 * char number message
2143 * e or E 0 " error"
2144 * w or W 0 " warning"
2145 * i or I 0 " info"
2146 * 0 0 ""
2147 * other 0 " c"
2148 * e or E n " error n"
2149 * w or W n " warning n"
2150 * i or I n " info n"
2151 * 0 n " error n"
2152 * other n " c n"
2153 * 1 x "" :helpgrep
2154 */
2155 static char_u *
2156qf_types(c, nr)
2157 int c, nr;
2158{
2159 static char_u buf[20];
2160 static char_u cc[3];
2161 char_u *p;
2162
2163 if (c == 'W' || c == 'w')
2164 p = (char_u *)" warning";
2165 else if (c == 'I' || c == 'i')
2166 p = (char_u *)" info";
2167 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2168 p = (char_u *)" error";
2169 else if (c == 0 || c == 1)
2170 p = (char_u *)"";
2171 else
2172 {
2173 cc[0] = ' ';
2174 cc[1] = c;
2175 cc[2] = NUL;
2176 p = cc;
2177 }
2178
2179 if (nr <= 0)
2180 return p;
2181
2182 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2183 return buf;
2184}
2185
2186#if defined(FEAT_WINDOWS) || defined(PROTO)
2187/*
2188 * ":cwindow": open the quickfix window if we have errors to display,
2189 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190 * ":lwindow": open the location list window if we have locations to display,
2191 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 */
2193 void
2194ex_cwindow(eap)
2195 exarg_T *eap;
2196{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002197 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 win_T *win;
2199
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002200 if (eap->cmdidx == CMD_lwindow)
2201 {
2202 qi = GET_LOC_LIST(curwin);
2203 if (qi == NULL)
2204 return;
2205 }
2206
2207 /* Look for an existing quickfix window. */
2208 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 /*
2211 * If a quickfix window is open but we have no errors to display,
2212 * close the window. If a quickfix window is not open, then open
2213 * it if we have errors; otherwise, leave it closed.
2214 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard68071d2006-05-02 22:08:30 +00002216 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 if (win != NULL)
2219 ex_cclose(eap);
2220 }
2221 else if (win == NULL)
2222 ex_copen(eap);
2223}
2224
2225/*
2226 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 */
2229/*ARGSUSED*/
2230 void
2231ex_cclose(eap)
2232 exarg_T *eap;
2233{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002234 win_T *win = NULL;
2235 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002237 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2238 {
2239 qi = GET_LOC_LIST(curwin);
2240 if (qi == NULL)
2241 return;
2242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 /* Find existing quickfix window and close it. */
2245 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (win != NULL)
2247 win_close(win, FALSE);
2248}
2249
2250/*
2251 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002252 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 */
2254 void
2255ex_copen(eap)
2256 exarg_T *eap;
2257{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002258 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002261 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002262 buf_T *qf_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002264 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2265 {
2266 qi = GET_LOC_LIST(curwin);
2267 if (qi == NULL)
2268 {
2269 EMSG(_(e_loclist));
2270 return;
2271 }
2272 }
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (eap->addr_count != 0)
2275 height = eap->line2;
2276 else
2277 height = QF_WINHEIGHT;
2278
2279#ifdef FEAT_VISUAL
2280 reset_VIsual_and_resel(); /* stop Visual mode */
2281#endif
2282#ifdef FEAT_GUI
2283 need_mouse_correct = TRUE;
2284#endif
2285
2286 /*
2287 * Find existing quickfix window, or open a new one.
2288 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002289 win = qf_find_win(qi);
2290
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002291 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 win_goto(win);
2293 else
2294 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002295 qf_buf = qf_find_buf(qi);
2296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 /* The current window becomes the previous window afterwards. */
2298 win = curwin;
2299
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002300 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2301 /* Create the new window at the very bottom. */
2302 win_goto(lastwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (win_split(height, WSP_BELOW) == FAIL)
2304 return; /* not enough room for window */
2305#ifdef FEAT_SCROLLBIND
2306 curwin->w_p_scb = FALSE;
2307#endif
2308
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002309 /* Remove the location list for the quickfix window */
2310 qf_free_all(curwin);
2311
2312 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002314 /*
2315 * For the location list window, create a reference to the
2316 * location list from the window 'win'.
2317 */
2318 curwin->w_llist_ref = win->w_llist;
2319 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002321
Bram Moolenaar9c102382006-05-03 21:26:49 +00002322 if (qf_buf != NULL)
2323 /* Use the existing quickfix buffer */
2324 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
2325 ECMD_HIDE + ECMD_OLDBUF);
2326 else
2327 {
2328 /* Create a new quickfix buffer */
2329 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2330 /* switch off 'swapfile' */
2331 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2332 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002333 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002334 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar7f514742007-06-28 19:33:43 +00002335 set_option_value((char_u *)"diff", 0L, NULL, OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002338 /* Only set the height when still in the same tab page and there is no
2339 * window to the side. */
2340 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002341#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002342 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002343#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002344 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 win_setheight(height);
2346 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2347 if (win_valid(win))
2348 prevwin = win;
2349 }
2350
2351 /*
2352 * Fill the buffer with the quickfix list.
2353 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002354 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002356 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 curwin->w_cursor.col = 0;
2358 check_cursor();
2359 update_topline(); /* scroll to show the line */
2360}
2361
2362/*
2363 * Return the number of the current entry (line number in the quickfix
2364 * window).
2365 */
2366 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367qf_current_entry(wp)
2368 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002370 qf_info_T *qi = &ql_info;
2371
2372 if (IS_LL_WINDOW(wp))
2373 /* In the location list window, use the referenced location list */
2374 qi = wp->w_llist_ref;
2375
2376 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377}
2378
2379/*
2380 * Update the cursor position in the quickfix window to the current error.
2381 * Return TRUE if there is a quickfix window.
2382 */
2383 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002384qf_win_pos_update(qi, old_qf_index)
2385 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 int old_qf_index; /* previous qf_index or zero */
2387{
2388 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002389 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 /*
2392 * Put the cursor on the current error in the quickfix window, so that
2393 * it's viewable.
2394 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002395 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (win != NULL
2397 && qf_index <= win->w_buffer->b_ml.ml_line_count
2398 && old_qf_index != qf_index)
2399 {
2400 win_T *old_curwin = curwin;
2401
2402 curwin = win;
2403 curbuf = win->w_buffer;
2404 if (qf_index > old_qf_index)
2405 {
2406 curwin->w_redraw_top = old_qf_index;
2407 curwin->w_redraw_bot = qf_index;
2408 }
2409 else
2410 {
2411 curwin->w_redraw_top = qf_index;
2412 curwin->w_redraw_bot = old_qf_index;
2413 }
2414 curwin->w_cursor.lnum = qf_index;
2415 curwin->w_cursor.col = 0;
2416 update_topline(); /* scroll to show the line */
2417 redraw_later(VALID);
2418 curwin->w_redr_status = TRUE; /* update ruler */
2419 curwin = old_curwin;
2420 curbuf = curwin->w_buffer;
2421 }
2422 return win != NULL;
2423}
2424
2425/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002426 * Check whether the given window is displaying the specified quickfix/location
2427 * list buffer
2428 */
2429 static int
2430is_qf_win(win, qi)
2431 win_T *win;
2432 qf_info_T *qi;
2433{
2434 /*
2435 * A window displaying the quickfix buffer will have the w_llist_ref field
2436 * set to NULL.
2437 * A window displaying a location list buffer will have the w_llist_ref
2438 * pointing to the location list.
2439 */
2440 if (bt_quickfix(win->w_buffer))
2441 if ((qi == &ql_info && win->w_llist_ref == NULL)
2442 || (qi != &ql_info && win->w_llist_ref == qi))
2443 return TRUE;
2444
2445 return FALSE;
2446}
2447
2448/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002449 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002450 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002451 */
2452 static win_T *
2453qf_find_win(qi)
2454 qf_info_T *qi;
2455{
2456 win_T *win;
2457
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002458 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002459 if (is_qf_win(win, qi))
2460 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002461
2462 return win;
2463}
2464
2465/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002466 * Find a quickfix buffer.
2467 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
2469 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002470qf_find_buf(qi)
2471 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002473 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002474 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaar9c102382006-05-03 21:26:49 +00002476 FOR_ALL_TAB_WINDOWS(tp, win)
2477 if (is_qf_win(win, qi))
2478 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002479
Bram Moolenaar9c102382006-05-03 21:26:49 +00002480 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481}
2482
2483/*
2484 * Find the quickfix buffer. If it exists, update the contents.
2485 */
2486 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002487qf_update_buffer(qi)
2488 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489{
2490 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002494 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (buf != NULL)
2496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 /* set curwin/curbuf to buf and save a few things */
2498 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002500 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 /* restore curwin/curbuf and a few other things */
2503 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002505 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507}
2508
2509/*
2510 * Fill current buffer with quickfix errors, replacing any previous contents.
2511 * curbuf must be the quickfix buffer!
2512 */
2513 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002514qf_fill_buffer(qi)
2515 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002517 linenr_T lnum;
2518 qfline_T *qfp;
2519 buf_T *errbuf;
2520 int len;
2521 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523 /* delete all existing lines */
2524 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2525 (void)ml_delete((linenr_T)1, FALSE);
2526
2527 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002528 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002531 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2532 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
2534 if (qfp->qf_fnum != 0
2535 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2536 && errbuf->b_fname != NULL)
2537 {
2538 if (qfp->qf_type == 1) /* :helpgrep */
2539 STRCPY(IObuff, gettail(errbuf->b_fname));
2540 else
2541 STRCPY(IObuff, errbuf->b_fname);
2542 len = (int)STRLEN(IObuff);
2543 }
2544 else
2545 len = 0;
2546 IObuff[len++] = '|';
2547
2548 if (qfp->qf_lnum > 0)
2549 {
2550 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2551 len += (int)STRLEN(IObuff + len);
2552
2553 if (qfp->qf_col > 0)
2554 {
2555 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2556 len += (int)STRLEN(IObuff + len);
2557 }
2558
2559 sprintf((char *)IObuff + len, "%s",
2560 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2561 len += (int)STRLEN(IObuff + len);
2562 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002563 else if (qfp->qf_pattern != NULL)
2564 {
2565 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2566 len += (int)STRLEN(IObuff + len);
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 IObuff[len++] = '|';
2569 IObuff[len++] = ' ';
2570
2571 /* Remove newlines and leading whitespace from the text.
2572 * For an unrecognized line keep the indent, the compiler may
2573 * mark a word with ^^^^. */
2574 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2575 IObuff + len, IOSIZE - len);
2576
2577 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2578 == FAIL)
2579 break;
2580 qfp = qfp->qf_next;
2581 }
2582 /* Delete the empty line which is now at the end */
2583 (void)ml_delete(lnum + 1, FALSE);
2584 }
2585
2586 /* correct cursor position */
2587 check_lnums(TRUE);
2588
2589 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2590 * resembles reading a file into a buffer, it's more logical when using
2591 * autocommands. */
2592 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2593 curbuf->b_p_ma = FALSE;
2594
2595#ifdef FEAT_AUTOCMD
2596 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2597 FALSE, curbuf);
2598 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2599 FALSE, curbuf);
2600#endif
2601
2602 /* make sure it will be redrawn */
2603 redraw_curbuf_later(NOT_VALID);
2604
2605 /* Restore KeyTyped, setting 'filetype' may reset it. */
2606 KeyTyped = old_KeyTyped;
2607}
2608
2609#endif /* FEAT_WINDOWS */
2610
2611/*
2612 * Return TRUE if "buf" is the quickfix buffer.
2613 */
2614 int
2615bt_quickfix(buf)
2616 buf_T *buf;
2617{
2618 return (buf->b_p_bt[0] == 'q');
2619}
2620
2621/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002622 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2623 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 */
2625 int
2626bt_nofile(buf)
2627 buf_T *buf;
2628{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002629 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2630 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631}
2632
2633/*
2634 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2635 */
2636 int
2637bt_dontwrite(buf)
2638 buf_T *buf;
2639{
2640 return (buf->b_p_bt[0] == 'n');
2641}
2642
2643 int
2644bt_dontwrite_msg(buf)
2645 buf_T *buf;
2646{
2647 if (bt_dontwrite(buf))
2648 {
2649 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2650 return TRUE;
2651 }
2652 return FALSE;
2653}
2654
2655/*
2656 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2657 * and 'bufhidden'.
2658 */
2659 int
2660buf_hide(buf)
2661 buf_T *buf;
2662{
2663 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2664 switch (buf->b_p_bh[0])
2665 {
2666 case 'u': /* "unload" */
2667 case 'w': /* "wipe" */
2668 case 'd': return FALSE; /* "delete" */
2669 case 'h': return TRUE; /* "hide" */
2670 }
2671 return (p_hid || cmdmod.hide);
2672}
2673
2674/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002675 * Return TRUE when using ":vimgrep" for ":grep".
2676 */
2677 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002678grep_internal(cmdidx)
2679 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002680{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002681 return ((cmdidx == CMD_grep
2682 || cmdidx == CMD_lgrep
2683 || cmdidx == CMD_grepadd
2684 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002685 && STRCMP("internal",
2686 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2687}
2688
2689/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002690 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
2692 void
2693ex_make(eap)
2694 exarg_T *eap;
2695{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002696 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 char_u *cmd;
2698 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002699 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002700 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002701 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002702#ifdef FEAT_AUTOCMD
2703 char_u *au_name = NULL;
2704
2705 switch (eap->cmdidx)
2706 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002707 case CMD_make: au_name = (char_u *)"make"; break;
2708 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2709 case CMD_grep: au_name = (char_u *)"grep"; break;
2710 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2711 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2712 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002713 default: break;
2714 }
2715 if (au_name != NULL)
2716 {
2717 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2718 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002719# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002720 if (did_throw || force_abort)
2721 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002722# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002723 }
2724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaar86b68352004-12-27 21:59:20 +00002726 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002727 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002728 {
2729 ex_vimgrep(eap);
2730 return;
2731 }
2732
Bram Moolenaara6557602006-02-04 22:43:20 +00002733 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2734 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002735 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002738 fname = get_mef_name();
2739 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002741 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 /*
2744 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2745 */
2746 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2747 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002748 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 cmd = alloc(len);
2750 if (cmd == NULL)
2751 return;
2752 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2753 (char *)p_shq);
2754 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002755 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 /*
2757 * Output a newline if there's something else than the :make command that
2758 * was typed (in which case the cursor is in column 0).
2759 */
2760 if (msg_col == 0)
2761 msg_didout = FALSE;
2762 msg_start();
2763 MSG_PUTS(":!");
2764 msg_outtrans(cmd); /* show what we are doing */
2765
2766 /* let the shell know if we are redirecting output or not */
2767 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2768
2769#ifdef AMIGA
2770 out_flush();
2771 /* read window status report and redraw before message */
2772 (void)char_avail();
2773#endif
2774
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002775 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002776 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2777 (eap->cmdidx != CMD_grepadd
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002778 && eap->cmdidx != CMD_lgrepadd));
2779#ifdef FEAT_AUTOCMD
2780 if (au_name != NULL)
2781 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2782 curbuf->b_fname, TRUE, curbuf);
2783#endif
2784 if (res > 0 && !eap->forceit)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002785 {
2786 if (wp != NULL)
2787 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002788 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
Bram Moolenaar7c626922005-02-07 22:01:03 +00002791 mch_remove(fname);
2792 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 vim_free(cmd);
2794}
2795
2796/*
2797 * Return the name for the errorfile, in allocated memory.
2798 * Find a new unique name when 'makeef' contains "##".
2799 * Returns NULL for error.
2800 */
2801 static char_u *
2802get_mef_name()
2803{
2804 char_u *p;
2805 char_u *name;
2806 static int start = -1;
2807 static int off = 0;
2808#ifdef HAVE_LSTAT
2809 struct stat sb;
2810#endif
2811
2812 if (*p_mef == NUL)
2813 {
2814 name = vim_tempname('e');
2815 if (name == NULL)
2816 EMSG(_(e_notmp));
2817 return name;
2818 }
2819
2820 for (p = p_mef; *p; ++p)
2821 if (p[0] == '#' && p[1] == '#')
2822 break;
2823
2824 if (*p == NUL)
2825 return vim_strsave(p_mef);
2826
2827 /* Keep trying until the name doesn't exist yet. */
2828 for (;;)
2829 {
2830 if (start == -1)
2831 start = mch_get_pid();
2832 else
2833 off += 19;
2834
2835 name = alloc((unsigned)STRLEN(p_mef) + 30);
2836 if (name == NULL)
2837 break;
2838 STRCPY(name, p_mef);
2839 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2840 STRCAT(name, p + 2);
2841 if (mch_getperm(name) < 0
2842#ifdef HAVE_LSTAT
2843 /* Don't accept a symbolic link, its a security risk. */
2844 && mch_lstat((char *)name, &sb) < 0
2845#endif
2846 )
2847 break;
2848 vim_free(name);
2849 }
2850 return name;
2851}
2852
2853/*
2854 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002855 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
2857 void
2858ex_cc(eap)
2859 exarg_T *eap;
2860{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002861 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002862
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002863 if (eap->cmdidx == CMD_ll
2864 || eap->cmdidx == CMD_lrewind
2865 || eap->cmdidx == CMD_lfirst
2866 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002867 {
2868 qi = GET_LOC_LIST(curwin);
2869 if (qi == NULL)
2870 {
2871 EMSG(_(e_loclist));
2872 return;
2873 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002874 }
2875
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002876 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 eap->addr_count > 0
2878 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002879 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002881 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2882 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 ? 1
2884 : 32767,
2885 eap->forceit);
2886}
2887
2888/*
2889 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 */
2892 void
2893ex_cnext(eap)
2894 exarg_T *eap;
2895{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002896 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002897
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002898 if (eap->cmdidx == CMD_lnext
2899 || eap->cmdidx == CMD_lNext
2900 || eap->cmdidx == CMD_lprevious
2901 || eap->cmdidx == CMD_lnfile
2902 || eap->cmdidx == CMD_lNfile
2903 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002904 {
2905 qi = GET_LOC_LIST(curwin);
2906 if (qi == NULL)
2907 {
2908 EMSG(_(e_loclist));
2909 return;
2910 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 }
2912
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002913 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002915 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002917 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2918 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 ? BACKWARD_FILE
2920 : BACKWARD,
2921 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2922}
2923
2924/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002925 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002926 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 */
2928 void
2929ex_cfile(eap)
2930 exarg_T *eap;
2931{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002932 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002933 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002934
2935 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2936 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002937 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002940 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002941
2942 /*
2943 * This function is used by the :cfile, :cgetfile and :caddfile
2944 * commands.
2945 * :cfile always creates a new quickfix list and jumps to the
2946 * first error.
2947 * :cgetfile creates a new quickfix list but doesn't jump to the
2948 * first error.
2949 * :caddfile adds to an existing quickfix list. If there is no
2950 * quickfix list then a new list is created.
2951 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002952 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2953 && eap->cmdidx != CMD_laddfile)) > 0
2954 && (eap->cmdidx == CMD_cfile
2955 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002956 {
2957 if (wp != NULL)
2958 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002959 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961}
2962
2963/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002964 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00002965 * ":vimgrepadd {pattern} file(s)"
2966 * ":lvimgrep {pattern} file(s)"
2967 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00002968 */
2969 void
2970ex_vimgrep(eap)
2971 exarg_T *eap;
2972{
Bram Moolenaar81695252004-12-29 20:58:21 +00002973 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002974 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002975 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00002976 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002977 char_u *s;
2978 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002979 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00002980 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002981 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002982 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002983 buf_T *buf;
2984 int duplicate_name = FALSE;
2985 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002986 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00002987 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002988 buf_T *first_match_buf = NULL;
2989 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002990 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002991#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2992 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002993#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002994 aco_save_T aco;
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00002995#ifdef FEAT_AUTOCMD
Bram Moolenaar7c626922005-02-07 22:01:03 +00002996 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002997 int flags = 0;
2998 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002999 long tomatch;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003000 char_u dirname_start[MAXPATHL];
3001 char_u dirname_now[MAXPATHL];
3002 char_u *target_dir = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003003
3004 switch (eap->cmdidx)
3005 {
3006 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003007 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003008 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003009 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003010 default: break;
3011 }
3012 if (au_name != NULL)
3013 {
3014 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3015 curbuf->b_fname, TRUE, curbuf);
3016 if (did_throw || force_abort)
3017 return;
3018 }
3019#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003020
Bram Moolenaar754b5602006-02-09 23:53:20 +00003021 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003022 || eap->cmdidx == CMD_lvimgrep
3023 || eap->cmdidx == CMD_lgrepadd
3024 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003025 {
3026 qi = ll_get_or_alloc_list(curwin);
3027 if (qi == NULL)
3028 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003029 }
3030
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003031 if (eap->addr_count > 0)
3032 tomatch = eap->line2;
3033 else
3034 tomatch = MAXLNUM;
3035
Bram Moolenaar81695252004-12-29 20:58:21 +00003036 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003037 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003038 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003039 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003040 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003041 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003042 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003043 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003044 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003045 if (regmatch.regprog == NULL)
3046 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003047 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003048 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003049
3050 p = skipwhite(p);
3051 if (*p == NUL)
3052 {
3053 EMSG(_("E683: File name missing or invalid pattern"));
3054 goto theend;
3055 }
3056
Bram Moolenaar754b5602006-02-09 23:53:20 +00003057 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003058 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003059 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003060 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003061 qf_new_list(qi);
3062 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003063 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003064 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003065 prevp->qf_next != prevp; prevp = prevp->qf_next)
3066 ;
3067
3068 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003069 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003070 goto theend;
3071 if (fcount == 0)
3072 {
3073 EMSG(_(e_nomatch));
3074 goto theend;
3075 }
3076
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003077 /* Remember the current directory, because a BufRead autocommand that does
3078 * ":lcd %:p:h" changes the meaning of short path names. */
3079 mch_dirname(dirname_start, MAXPATHL);
3080
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003081 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003082 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003083 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003084 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003085 if (time(NULL) > seconds)
3086 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003087 /* Display the file name every second or so, show the user we are
3088 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003089 seconds = time(NULL);
3090 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003091 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003092 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003093 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003094 else
3095 {
3096 msg_outtrans(p);
3097 vim_free(p);
3098 }
3099 msg_clr_eos();
3100 msg_didout = FALSE; /* overwrite this message */
3101 msg_nowait = TRUE; /* don't wait for this message */
3102 msg_col = 0;
3103 out_flush();
3104 }
3105
Bram Moolenaar81695252004-12-29 20:58:21 +00003106 buf = buflist_findname_exp(fnames[fi]);
3107 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3108 {
3109 /* Remember that a buffer with this name already exists. */
3110 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003111 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003112 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003113
3114#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3115 /* Don't do Filetype autocommands to avoid loading syntax and
3116 * indent scripts, a great speed improvement. */
3117 save_ei = au_event_disable(",Filetype");
3118#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003119 /* Don't use modelines here, it's useless. */
3120 save_mls = p_mls;
3121 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003122
3123 /* Load file into a buffer, so that 'fileencoding' is detected,
3124 * autocommands applied, etc. */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003125 buf = load_dummy_buffer(fname);
3126
3127 /* When autocommands changed directory: go back. We assume it was
3128 * ":lcd %:p:h". */
3129 mch_dirname(dirname_now, MAXPATHL);
3130 if (STRCMP(dirname_start, dirname_now) != 0)
3131 {
3132 exarg_T ea;
3133
3134 ea.arg = dirname_start;
3135 ea.cmdidx = CMD_lcd;
3136 ex_cd(&ea);
3137 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003138
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003139 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003140#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3141 au_event_restore(save_ei);
3142#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003143 }
3144 else
3145 /* Use existing, loaded buffer. */
3146 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003147
Bram Moolenaar81695252004-12-29 20:58:21 +00003148 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003149 {
3150 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003151 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003152 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003153 else
3154 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003155 /* Try for a match in all lines of the buffer.
3156 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003157 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003158 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3159 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003160 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003161 col = 0;
3162 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003163 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003164 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003165 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003166 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003167 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003168 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003169 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003170 ml_get_buf(buf,
3171 regmatch.startpos[0].lnum + lnum, FALSE),
3172 regmatch.startpos[0].lnum + lnum,
3173 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003174 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003175 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003176 0, /* nr */
3177 0, /* type */
3178 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003179 ) == FAIL)
3180 {
3181 got_int = TRUE;
3182 break;
3183 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003184 found_match = TRUE;
3185 if (--tomatch == 0)
3186 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003187 if ((flags & VGR_GLOBAL) == 0
3188 || regmatch.endpos[0].lnum > 0)
3189 break;
3190 col = regmatch.endpos[0].col
3191 + (col == regmatch.endpos[0].col);
3192 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3193 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003194 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003195 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003196 if (got_int)
3197 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003198 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003199
3200 if (using_dummy)
3201 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003202 if (found_match && first_match_buf == NULL)
3203 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003204 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003205 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003206 /* Never keep a dummy buffer if there is another buffer
3207 * with the same name. */
3208 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003209 buf = NULL;
3210 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003211 else if (!cmdmod.hide
3212 || buf->b_p_bh[0] == 'u' /* "unload" */
3213 || buf->b_p_bh[0] == 'w' /* "wipe" */
3214 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003215 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003216 /* When no match was found we don't need to remember the
3217 * buffer, wipe it out. If there was a match and it
3218 * wasn't the first one or we won't jump there: only
3219 * unload the buffer.
3220 * Ignore 'hidden' here, because it may lead to having too
3221 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003222 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003223 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003224 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003225 buf = NULL;
3226 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003227 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003228 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003229 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003230 buf = NULL;
3231 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003232 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003233
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003234 if (buf != NULL)
3235 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003236 /* If the buffer is still loaded we need to use the
3237 * directory we jumped to below. */
3238 if (buf == first_match_buf
3239 && target_dir == NULL
3240 && STRCMP(dirname_start, dirname_now) != 0)
3241 target_dir = vim_strsave(dirname_now);
3242
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003243 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003244 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003245 * need to be done (again). But not the window-local
3246 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003247 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003248#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003249 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3250 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003251#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003252 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003253 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003254 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003255 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003256 }
3257 }
3258
3259 FreeWild(fcount, fnames);
3260
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003261 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3262 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3263 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003264
3265#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003266 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003267#endif
3268
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003269#ifdef FEAT_AUTOCMD
3270 if (au_name != NULL)
3271 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3272 curbuf->b_fname, TRUE, curbuf);
3273#endif
3274
Bram Moolenaar86b68352004-12-27 21:59:20 +00003275 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003276 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003277 {
3278 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003279 {
3280 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003281 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003282 if (buf != curbuf)
3283 /* If we jumped to another buffer redrawing will already be
3284 * taken care of. */
3285 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003286
3287 /* Jump to the directory used after loading the buffer. */
3288 if (curbuf == first_match_buf && target_dir != NULL)
3289 {
3290 exarg_T ea;
3291
3292 ea.arg = target_dir;
3293 ea.cmdidx = CMD_lcd;
3294 ex_cd(&ea);
3295 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003296 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003297 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003298 else
3299 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003300
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003301 /* If we loaded a dummy buffer into the current window, the autocommands
3302 * may have messed up things, need to redraw and recompute folds. */
3303 if (redraw_for_dummy)
3304 {
3305#ifdef FEAT_FOLDING
3306 foldUpdateAll(curwin);
3307#else
3308 redraw_later(NOT_VALID);
3309#endif
3310 }
3311
Bram Moolenaar86b68352004-12-27 21:59:20 +00003312theend:
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003313 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003314 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003315}
3316
3317/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003318 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003319 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003320 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3321 * If "s" is not NULL terminate the pattern with a NUL.
3322 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003323 */
3324 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003325skip_vimgrep_pat(p, s, flags)
3326 char_u *p;
3327 char_u **s;
3328 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003329{
3330 int c;
3331
3332 if (vim_isIDc(*p))
3333 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003334 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003335 if (s != NULL)
3336 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003337 p = skiptowhite(p);
3338 if (s != NULL && *p != NUL)
3339 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003340 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003341 else
3342 {
3343 /* ":vimgrep /pattern/[g][j] fname" */
3344 if (s != NULL)
3345 *s = p + 1;
3346 c = *p;
3347 p = skip_regexp(p + 1, c, TRUE, NULL);
3348 if (*p != c)
3349 return NULL;
3350
3351 /* Truncate the pattern. */
3352 if (s != NULL)
3353 *p = NUL;
3354 ++p;
3355
3356 /* Find the flags */
3357 while (*p == 'g' || *p == 'j')
3358 {
3359 if (flags != NULL)
3360 {
3361 if (*p == 'g')
3362 *flags |= VGR_GLOBAL;
3363 else
3364 *flags |= VGR_NOJUMP;
3365 }
3366 ++p;
3367 }
3368 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003369 return p;
3370}
3371
3372/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003373 * Load file "fname" into a dummy buffer and return the buffer pointer.
3374 * Returns NULL if it fails.
3375 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3376 */
3377 static buf_T *
3378load_dummy_buffer(fname)
3379 char_u *fname;
3380{
3381 buf_T *newbuf;
3382 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003383 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003384
3385 /* Allocate a buffer without putting it in the buffer list. */
3386 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3387 if (newbuf == NULL)
3388 return NULL;
3389
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003390 /* Init the options. */
3391 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3392
Bram Moolenaar81695252004-12-29 20:58:21 +00003393 /* set curwin/curbuf to buf and save a few things */
3394 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00003395
3396 /* Need to set the filename for autocommands. */
3397 (void)setfname(curbuf, fname, NULL, FALSE);
3398
Bram Moolenaar4770d092006-01-12 23:22:24 +00003399 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003400 {
3401 /* Create swap file now to avoid the ATTENTION message. */
3402 check_need_swap(TRUE);
3403
3404 /* Remove the "dummy" flag, otherwise autocommands may not
3405 * work. */
3406 curbuf->b_flags &= ~BF_DUMMY;
3407
3408 if (readfile(fname, NULL,
3409 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3410 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003411 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003412 && !(curbuf->b_flags & BF_NEW))
3413 {
3414 failed = FALSE;
3415 if (curbuf != newbuf)
3416 {
3417 /* Bloody autocommands changed the buffer! */
3418 if (buf_valid(newbuf))
3419 wipe_buffer(newbuf, FALSE);
3420 newbuf = curbuf;
3421 }
3422 }
3423 }
3424
Bram Moolenaar81695252004-12-29 20:58:21 +00003425 /* restore curwin/curbuf and a few other things */
3426 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00003427
3428 if (!buf_valid(newbuf))
3429 return NULL;
3430 if (failed)
3431 {
3432 wipe_dummy_buffer(newbuf);
3433 return NULL;
3434 }
3435 return newbuf;
3436}
3437
3438/*
3439 * Wipe out the dummy buffer that load_dummy_buffer() created.
3440 */
3441 static void
3442wipe_dummy_buffer(buf)
3443 buf_T *buf;
3444{
3445 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003446 {
3447#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3448 cleanup_T cs;
3449
3450 /* Reset the error/interrupt/exception state here so that aborting()
3451 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3452 * work when got_int is set. */
3453 enter_cleanup(&cs);
3454#endif
3455
Bram Moolenaar81695252004-12-29 20:58:21 +00003456 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003457
3458#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3459 /* Restore the error/interrupt/exception state if not discarded by a
3460 * new aborting error, interrupt, or uncaught exception. */
3461 leave_cleanup(&cs);
3462#endif
3463 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003464}
3465
3466/*
3467 * Unload the dummy buffer that load_dummy_buffer() created.
3468 */
3469 static void
3470unload_dummy_buffer(buf)
3471 buf_T *buf;
3472{
3473 if (curbuf != buf) /* safety check */
3474 close_buffer(NULL, buf, DOBUF_UNLOAD);
3475}
3476
Bram Moolenaar05159a02005-02-26 23:04:13 +00003477#if defined(FEAT_EVAL) || defined(PROTO)
3478/*
3479 * Add each quickfix error to list "list" as a dictionary.
3480 */
3481 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003482get_errorlist(wp, list)
3483 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003484 list_T *list;
3485{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003486 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003487 dict_T *dict;
3488 char_u buf[2];
3489 qfline_T *qfp;
3490 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003491 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003492
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003493 if (wp != NULL)
3494 {
3495 qi = GET_LOC_LIST(wp);
3496 if (qi == NULL)
3497 return FAIL;
3498 }
3499
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003500 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003501 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003502 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003503
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003504 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3505 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003506 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003507 /* Handle entries with a non-existing buffer number. */
3508 bufnum = qfp->qf_fnum;
3509 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3510 bufnum = 0;
3511
Bram Moolenaar05159a02005-02-26 23:04:13 +00003512 if ((dict = dict_alloc()) == NULL)
3513 return FAIL;
3514 if (list_append_dict(list, dict) == FAIL)
3515 return FAIL;
3516
3517 buf[0] = qfp->qf_type;
3518 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003519 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003520 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3521 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3522 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3523 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003524 || dict_add_nr_str(dict, "pattern", 0L,
3525 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3526 || dict_add_nr_str(dict, "text", 0L,
3527 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003528 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3529 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3530 return FAIL;
3531
3532 qfp = qfp->qf_next;
3533 }
3534 return OK;
3535}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003536
3537/*
3538 * Populate the quickfix list with the items supplied in the list
3539 * of dictionaries.
3540 */
3541 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003542set_errorlist(wp, list, action)
3543 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003544 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003545 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003546{
3547 listitem_T *li;
3548 dict_T *d;
3549 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003550 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003551 long lnum;
3552 int col, nr;
3553 int vcol;
3554 qfline_T *prevp = NULL;
3555 int valid, status;
3556 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003557 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003558 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003559
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003560 if (wp != NULL)
3561 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003562 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003563 if (qi == NULL)
3564 return FAIL;
3565 }
3566
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003567 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003568 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003569 qf_new_list(qi);
3570 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003571 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003572 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003573 prevp->qf_next != prevp; prevp = prevp->qf_next)
3574 ;
3575 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003576 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003577
3578 for (li = list->lv_first; li != NULL; li = li->li_next)
3579 {
3580 if (li->li_tv.v_type != VAR_DICT)
3581 continue; /* Skip non-dict items */
3582
3583 d = li->li_tv.vval.v_dict;
3584 if (d == NULL)
3585 continue;
3586
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003587 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003588 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003589 lnum = get_dict_number(d, (char_u *)"lnum");
3590 col = get_dict_number(d, (char_u *)"col");
3591 vcol = get_dict_number(d, (char_u *)"vcol");
3592 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003593 type = get_dict_string(d, (char_u *)"type", TRUE);
3594 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3595 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003596 if (text == NULL)
3597 text = vim_strsave((char_u *)"");
3598
3599 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003600 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003601 valid = FALSE;
3602
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003603 /* Mark entries with non-existing buffer number as not valid. Give the
3604 * error message only once. */
3605 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3606 {
3607 if (!did_bufnr_emsg)
3608 {
3609 did_bufnr_emsg = TRUE;
3610 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3611 }
3612 valid = FALSE;
3613 bufnum = 0;
3614 }
3615
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003616 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003617 NULL, /* dir */
3618 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003619 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003620 text,
3621 lnum,
3622 col,
3623 vcol, /* vis_col */
3624 pattern, /* search pattern */
3625 nr,
3626 type == NULL ? NUL : *type,
3627 valid);
3628
3629 vim_free(filename);
3630 vim_free(pattern);
3631 vim_free(text);
3632 vim_free(type);
3633
3634 if (status == FAIL)
3635 {
3636 retval = FAIL;
3637 break;
3638 }
3639 }
3640
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003641 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3642 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3643 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003644
3645#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003646 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003647#endif
3648
3649 return retval;
3650}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003651#endif
3652
Bram Moolenaar81695252004-12-29 20:58:21 +00003653/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003654 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003655 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003656 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003657 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003658 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003659 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003660 */
3661 void
3662ex_cbuffer(eap)
3663 exarg_T *eap;
3664{
3665 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003666 qf_info_T *qi = &ql_info;
3667
Bram Moolenaardb552d602006-03-23 22:59:57 +00003668 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3669 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003670 {
3671 qi = ll_get_or_alloc_list(curwin);
3672 if (qi == NULL)
3673 return;
3674 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003675
3676 if (*eap->arg == NUL)
3677 buf = curbuf;
3678 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3679 buf = buflist_findnr(atoi((char *)eap->arg));
3680 if (buf == NULL)
3681 EMSG(_(e_invarg));
3682 else if (buf->b_ml.ml_mfp == NULL)
3683 EMSG(_("E681: Buffer is not loaded"));
3684 else
3685 {
3686 if (eap->addr_count == 0)
3687 {
3688 eap->line1 = 1;
3689 eap->line2 = buf->b_ml.ml_line_count;
3690 }
3691 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3692 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3693 EMSG(_(e_invrange));
3694 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003695 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003696 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3697 (eap->cmdidx != CMD_caddbuffer
3698 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar754b5602006-02-09 23:53:20 +00003699 eap->line1, eap->line2) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003700 && (eap->cmdidx == CMD_cbuffer
3701 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003702 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3703 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003704 }
3705}
3706
Bram Moolenaar1e015462005-09-25 22:16:38 +00003707#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003708/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003709 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3710 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003711 */
3712 void
3713ex_cexpr(eap)
3714 exarg_T *eap;
3715{
3716 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003717 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003718
Bram Moolenaardb552d602006-03-23 22:59:57 +00003719 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3720 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003721 {
3722 qi = ll_get_or_alloc_list(curwin);
3723 if (qi == NULL)
3724 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003725 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003726
Bram Moolenaar4770d092006-01-12 23:22:24 +00003727 /* Evaluate the expression. When the result is a string or a list we can
3728 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003729 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003730 if (tv != NULL)
3731 {
3732 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3733 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3734 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003735 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3736 (eap->cmdidx != CMD_caddexpr
3737 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar4770d092006-01-12 23:22:24 +00003738 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003739 && (eap->cmdidx == CMD_cexpr
3740 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003741 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003742 }
3743 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003744 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003745 free_tv(tv);
3746 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003747}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003748#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003749
3750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 * ":helpgrep {pattern}"
3752 */
3753 void
3754ex_helpgrep(eap)
3755 exarg_T *eap;
3756{
3757 regmatch_T regmatch;
3758 char_u *save_cpo;
3759 char_u *p;
3760 int fcount;
3761 char_u **fnames;
3762 FILE *fd;
3763 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003764 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003766#ifdef FEAT_MULTI_LANG
3767 char_u *lang;
3768#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003769 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003770 int new_qi = FALSE;
3771 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3774 save_cpo = p_cpo;
3775 p_cpo = (char_u *)"";
3776
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003777#ifdef FEAT_MULTI_LANG
3778 /* Check for a specified language */
3779 lang = check_help_lang(eap->arg);
3780#endif
3781
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003782 if (eap->cmdidx == CMD_lhelpgrep)
3783 {
3784 /* Find an existing help window */
3785 FOR_ALL_WINDOWS(wp)
3786 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3787 break;
3788
3789 if (wp == NULL) /* Help window not found */
3790 qi = NULL;
3791 else
3792 qi = wp->w_llist;
3793
3794 if (qi == NULL)
3795 {
3796 /* Allocate a new location list for help text matches */
3797 if ((qi = ll_new_list()) == NULL)
3798 return;
3799 new_qi = TRUE;
3800 }
3801 }
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3804 regmatch.rm_ic = FALSE;
3805 if (regmatch.regprog != NULL)
3806 {
3807 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003808 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 /* Go through all directories in 'runtimepath' */
3811 p = p_rtp;
3812 while (*p != NUL && !got_int)
3813 {
3814 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3815
3816 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3817 add_pathsep(NameBuff);
3818 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3819 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3820 &fnames, EW_FILE|EW_SILENT) == OK
3821 && fcount > 0)
3822 {
3823 for (fi = 0; fi < fcount && !got_int; ++fi)
3824 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003825#ifdef FEAT_MULTI_LANG
3826 /* Skip files for a different language. */
3827 if (lang != NULL
3828 && STRNICMP(lang, fnames[fi]
3829 + STRLEN(fnames[fi]) - 3, 2) != 0
3830 && !(STRNICMP(lang, "en", 2) == 0
3831 && STRNICMP("txt", fnames[fi]
3832 + STRLEN(fnames[fi]) - 3, 3) == 0))
3833 continue;
3834#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003835 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 if (fd != NULL)
3837 {
3838 lnum = 1;
3839 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3840 {
3841 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3842 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003843 int l = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845 /* remove trailing CR, LF, spaces, etc. */
3846 while (l > 0 && IObuff[l - 1] <= ' ')
3847 IObuff[--l] = NUL;
3848
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003849 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 NULL, /* dir */
3851 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003852 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 IObuff,
3854 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003855 (int)(regmatch.startp[0] - IObuff)
3856 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003857 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003858 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 0, /* nr */
3860 1, /* type */
3861 TRUE /* valid */
3862 ) == FAIL)
3863 {
3864 got_int = TRUE;
3865 break;
3866 }
3867 }
3868 ++lnum;
3869 line_breakcheck();
3870 }
3871 fclose(fd);
3872 }
3873 }
3874 FreeWild(fcount, fnames);
3875 }
3876 }
3877 vim_free(regmatch.regprog);
3878
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003879 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3880 qi->qf_lists[qi->qf_curlist].qf_ptr =
3881 qi->qf_lists[qi->qf_curlist].qf_start;
3882 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884
3885 p_cpo = save_cpo;
3886
3887#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003888 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889#endif
3890
3891 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003892 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003893 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003894 else
3895 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003896
3897 if (eap->cmdidx == CMD_lhelpgrep)
3898 {
3899 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003900 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003901 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3902 {
3903 if (new_qi)
3904 ll_free_all(&qi);
3905 }
3906 else if (curwin->w_llist == NULL)
3907 curwin->w_llist = qi;
3908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909}
3910
3911#endif /* FEAT_QUICKFIX */