blob: a8e50991129dc300de3006a60cfef54567fa5bd7 [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 */
82struct eformat
83{
84 regprog_T *prog; /* pre-formatted part of 'errorformat' */
85 struct eformat *next; /* pointer to next (NULL if last) */
86 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
87 char_u prefix; /* prefix of this format line: */
88 /* 'D' enter directory */
89 /* 'X' leave directory */
90 /* 'A' start of multi-line message */
91 /* 'E' error message */
92 /* 'W' warning message */
93 /* 'I' informational message */
94 /* 'C' continuation line */
95 /* 'Z' end of multi-line message */
96 /* 'G' general, unspecific message */
97 /* 'P' push file (partial) message */
98 /* 'Q' pop/quit file (partial) message */
99 /* 'O' overread (partial) message */
100 char_u flags; /* additional flags given in prefix */
101 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000102 /* '+' include whole line in message */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103};
104
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000105static 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));
106static void qf_new_list __ARGS((qf_info_T *qi));
107static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
108static void qf_msg __ARGS((qf_info_T *qi));
109static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110static char_u *qf_types __ARGS((int, int));
111static int qf_get_fnum __ARGS((char_u *, char_u *));
112static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
113static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
114static char_u *qf_guess_filepath __ARGS((char_u *));
115static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
116static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
117#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000118static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
119static win_T *qf_find_win __ARGS((qf_info_T *qi));
120static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
121static void qf_update_buffer __ARGS((qf_info_T *qi));
122static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
124static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000125static buf_T *load_dummy_buffer __ARGS((char_u *fname));
126static void wipe_dummy_buffer __ARGS((buf_T *buf));
127static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000128static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130/* Quickfix window check helper macro */
131#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
132/* Location list window check helper macro */
133#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
134/*
135 * Return location list for window 'wp'
136 * For location list window, return the referenced location list
137 */
138#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
139
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000141 * Read the errorfile "efile" into memory, line by line, building the error
142 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 * Return -1 for error, number of errors for success.
144 */
145 int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000146qf_init(wp, efile, errorformat, newlist)
147 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 char_u *efile;
149 char_u *errorformat;
150 int newlist; /* TRUE: start a new error list */
151{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000152 qf_info_T *qi = &ql_info;
153
Bram Moolenaar86b68352004-12-27 21:59:20 +0000154 if (efile == NULL)
155 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000156
157 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000158 {
159 qi = ll_get_or_alloc_list(wp);
160 if (qi == NULL)
161 return FAIL;
162 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163
164 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +0000165 (linenr_T)0, (linenr_T)0);
166}
167
168/*
169 * Read the errorfile "efile" into memory, line by line, building the error
170 * list.
171 * Alternative: when "efile" is null read errors from buffer "buf".
172 * Always use 'errorformat' from "buf" if there is a local value.
173 * Then lnumfirst and lnumlast specify the range of lines to use.
174 * Return -1 for error, number of errors for success.
175 */
176 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000177qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
178 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000179 char_u *efile;
180 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000181 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182 char_u *errorformat;
183 int newlist; /* TRUE: start a new error list */
184 linenr_T lnumfirst; /* first line number to use */
185 linenr_T lnumlast; /* last line number to use */
186{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 char_u *namebuf;
188 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000189 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 char_u *fmtstr = NULL;
191 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000192 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 int type = 0;
194 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000195 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 long lnum = 0L;
197 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000198 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000199 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 char_u *efmp;
201 struct eformat *fmt_first = NULL;
202 struct eformat *fmt_last = NULL;
203 struct eformat *fmt_ptr;
204 char_u *efm;
205 char_u *ptr;
206 char_u *srcptr;
207 int len;
208 int i;
209 int round;
210 int idx = 0;
211 int multiline = FALSE;
212 int multiignore = FALSE;
213 int multiscan = FALSE;
214 int retval = -1; /* default: return error flag */
215 char_u *directory = NULL;
216 char_u *currfile = NULL;
217 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000218 char_u *p_str = NULL;
219 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 struct dir_stack_T *file_stack = NULL;
221 regmatch_T regmatch;
222 static struct fmtpattern
223 {
224 char_u convchar;
225 char *pattern;
226 } fmt_pat[FMT_PATTERNS] =
227 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000228 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {'n', "\\d\\+"},
230 {'l', "\\d\\+"},
231 {'c', "\\d\\+"},
232 {'t', "."},
233 {'m', ".\\+"},
234 {'r', ".*"},
235 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000236 {'v', "\\d\\+"},
237 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 };
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 namebuf = alloc(CMDBUFFSIZE + 1);
241 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000242 pattern = alloc(CMDBUFFSIZE + 1);
243 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 goto qf_init_end;
245
Bram Moolenaar86b68352004-12-27 21:59:20 +0000246 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {
248 EMSG2(_(e_openerrf), efile);
249 goto qf_init_end;
250 }
251
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000252 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000254 qf_new_list(qi);
255 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000257 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
259 ;
260
261/*
262 * Each part of the format string is copied and modified from errorformat to
263 * regex prog. Only a few % characters are allowed.
264 */
265 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000266 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000267 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 else
269 efm = errorformat;
270 /*
271 * Get some space to modify the format string into.
272 */
273 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
274 for (round = FMT_PATTERNS; round > 0; )
275 i += (int)STRLEN(fmt_pat[--round].pattern);
276#ifdef COLON_IN_FILENAME
277 i += 12; /* "%f" can become twelve chars longer */
278#else
279 i += 2; /* "%f" can become two chars longer */
280#endif
281 if ((fmtstr = alloc(i)) == NULL)
282 goto error2;
283
284 while (efm[0])
285 {
286 /*
287 * Allocate a new eformat structure and put it at the end of the list
288 */
289 fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
290 if (fmt_ptr == NULL)
291 goto error2;
292 if (fmt_first == NULL) /* first one */
293 fmt_first = fmt_ptr;
294 else
295 fmt_last->next = fmt_ptr;
296 fmt_last = fmt_ptr;
297 fmt_ptr->prefix = NUL;
298 fmt_ptr->flags = NUL;
299 fmt_ptr->next = NULL;
300 fmt_ptr->prog = NULL;
301 for (round = FMT_PATTERNS; round > 0; )
302 fmt_ptr->addr[--round] = NUL;
303 /* round is 0 now */
304
305 /*
306 * Isolate one part in the 'errorformat' option
307 */
308 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
309 if (efm[len] == '\\' && efm[len + 1] != NUL)
310 ++len;
311
312 /*
313 * Build regexp pattern from current 'errorformat' option
314 */
315 ptr = fmtstr;
316 *ptr++ = '^';
317 for (efmp = efm; efmp < efm + len; ++efmp)
318 {
319 if (*efmp == '%')
320 {
321 ++efmp;
322 for (idx = 0; idx < FMT_PATTERNS; ++idx)
323 if (fmt_pat[idx].convchar == *efmp)
324 break;
325 if (idx < FMT_PATTERNS)
326 {
327 if (fmt_ptr->addr[idx])
328 {
329 sprintf((char *)errmsg,
330 _("E372: Too many %%%c in format string"), *efmp);
331 EMSG(errmsg);
332 goto error2;
333 }
334 if ((idx
335 && idx < 6
336 && vim_strchr((char_u *)"DXOPQ",
337 fmt_ptr->prefix) != NULL)
338 || (idx == 6
339 && vim_strchr((char_u *)"OPQ",
340 fmt_ptr->prefix) == NULL))
341 {
342 sprintf((char *)errmsg,
343 _("E373: Unexpected %%%c in format string"), *efmp);
344 EMSG(errmsg);
345 goto error2;
346 }
347 fmt_ptr->addr[idx] = (char_u)++round;
348 *ptr++ = '\\';
349 *ptr++ = '(';
350#ifdef BACKSLASH_IN_FILENAME
351 if (*efmp == 'f')
352 {
353 /* Also match "c:" in the file name, even when
354 * checking for a colon next: "%f:".
355 * "\%(\a:\)\=" */
356 STRCPY(ptr, "\\%(\\a:\\)\\=");
357 ptr += 10;
358 }
359#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000360 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000362 if (efmp[1] != '\\' && efmp[1] != '%')
363 {
364 /* A file name may contain spaces, but this isn't
365 * in "\f". For "%f:%l:%m" there may be a ":" in
366 * the file name. Use ".\{-1,}x" instead (x is
367 * the next character), the requirement that :999:
368 * follows should work. */
369 STRCPY(ptr, ".\\{-1,}");
370 ptr += 7;
371 }
372 else
373 {
374 /* File name followed by '\\' or '%': include as
375 * many file name chars as possible. */
376 STRCPY(ptr, "\\f\\+");
377 ptr += 4;
378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 }
380 else
381 {
382 srcptr = (char_u *)fmt_pat[idx].pattern;
383 while ((*ptr = *srcptr++) != NUL)
384 ++ptr;
385 }
386 *ptr++ = '\\';
387 *ptr++ = ')';
388 }
389 else if (*efmp == '*')
390 {
391 if (*++efmp == '[' || *efmp == '\\')
392 {
393 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
394 {
395 if (efmp[1] == '^')
396 *ptr++ = *++efmp;
397 if (efmp < efm + len)
398 {
399 *ptr++ = *++efmp; /* could be ']' */
400 while (efmp < efm + len
401 && (*ptr++ = *++efmp) != ']')
402 /* skip */;
403 if (efmp == efm + len)
404 {
405 EMSG(_("E374: Missing ] in format string"));
406 goto error2;
407 }
408 }
409 }
410 else if (efmp < efm + len) /* %*\D, %*\s etc. */
411 *ptr++ = *++efmp;
412 *ptr++ = '\\';
413 *ptr++ = '+';
414 }
415 else
416 {
417 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
418 sprintf((char *)errmsg,
419 _("E375: Unsupported %%%c in format string"), *efmp);
420 EMSG(errmsg);
421 goto error2;
422 }
423 }
424 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
425 *ptr++ = *efmp; /* regexp magic characters */
426 else if (*efmp == '#')
427 *ptr++ = '*';
428 else if (efmp == efm + 1) /* analyse prefix */
429 {
430 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
431 fmt_ptr->flags = *efmp++;
432 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
433 fmt_ptr->prefix = *efmp;
434 else
435 {
436 sprintf((char *)errmsg,
437 _("E376: Invalid %%%c in format string prefix"), *efmp);
438 EMSG(errmsg);
439 goto error2;
440 }
441 }
442 else
443 {
444 sprintf((char *)errmsg,
445 _("E377: Invalid %%%c in format string"), *efmp);
446 EMSG(errmsg);
447 goto error2;
448 }
449 }
450 else /* copy normal character */
451 {
452 if (*efmp == '\\' && efmp + 1 < efm + len)
453 ++efmp;
454 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
455 *ptr++ = '\\'; /* escape regexp atoms */
456 if (*efmp)
457 *ptr++ = *efmp;
458 }
459 }
460 *ptr++ = '$';
461 *ptr = NUL;
462 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
463 goto error2;
464 /*
465 * Advance to next part
466 */
467 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
468 }
469 if (fmt_first == NULL) /* nothing found */
470 {
471 EMSG(_("E378: 'errorformat' contains no pattern"));
472 goto error2;
473 }
474
475 /*
476 * got_int is reset here, because it was probably set when killing the
477 * ":make" command, but we still want to read the errorfile then.
478 */
479 got_int = FALSE;
480
481 /* Always ignore case when looking for a matching error. */
482 regmatch.rm_ic = TRUE;
483
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000484 if (tv != NULL)
485 {
486 if (tv->v_type == VAR_STRING)
487 p_str = tv->vval.v_string;
488 else if (tv->v_type == VAR_LIST)
489 p_li = tv->vval.v_list->lv_first;
490 }
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 /*
493 * Read the lines in the error file one by one.
494 * Try to recognize one of the error formats in each line.
495 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000496 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000498 /* Get the next line. */
499 if (fd == NULL)
500 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000501 if (tv != NULL)
502 {
503 int len;
504
505 if (tv->v_type == VAR_STRING)
506 {
507 /* Get the next line from the supplied string */
508 char_u *p;
509
510 if (!*p_str) /* Reached the end of the string */
511 break;
512
513 p = vim_strchr(p_str, '\n');
514 if (p)
515 len = p - p_str + 1;
516 else
517 len = STRLEN(p_str);
518
519 if (len > CMDBUFFSIZE - 2)
520 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
521 else
522 vim_strncpy(IObuff, p_str, len);
523
524 p_str += len;
525 }
526 else if (tv->v_type == VAR_LIST)
527 {
528 /* Get the next line from the supplied list */
529 while (p_li && p_li->li_tv.v_type != VAR_STRING)
530 p_li = p_li->li_next; /* Skip non-string items */
531
532 if (!p_li) /* End of the list */
533 break;
534
535 len = STRLEN(p_li->li_tv.vval.v_string);
536 if (len > CMDBUFFSIZE - 2)
537 len = CMDBUFFSIZE - 2;
538
539 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
540
541 p_li = p_li->li_next; /* next item */
542 }
543 }
544 else
545 {
546 /* Get the next line from the supplied buffer */
547 if (buflnum > lnumlast)
548 break;
549 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
550 CMDBUFFSIZE - 2);
551 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000552 }
553 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
554 break;
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
557 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
558 *efmp = NUL;
559#ifdef USE_CRNL
560 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
561 *efmp = NUL;
562#endif
563
564 /*
565 * Try to match each part of 'errorformat' until we find a complete
566 * match or no match.
567 */
568 valid = TRUE;
569restofline:
570 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
571 {
572 idx = fmt_ptr->prefix;
573 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
574 continue;
575 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (!multiscan)
578 errmsg[0] = NUL;
579 lnum = 0;
580 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000581 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 enr = -1;
583 type = 0;
584 tail = NULL;
585
586 regmatch.regprog = fmt_ptr->prog;
587 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
588 {
589 if ((idx == 'C' || idx == 'Z') && !multiline)
590 continue;
591 if (vim_strchr((char_u *)"EWI", idx) != NULL)
592 type = idx;
593 else
594 type = 0;
595 /*
596 * Extract error message data from matched line
597 */
598 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
599 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000600 int c = *regmatch.endp[i];
601
602 /* Expand ~/file and $HOME/file to full path. */
603 *regmatch.endp[i] = NUL;
604 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
605 *regmatch.endp[i] = c;
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000608 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 continue;
610 }
611 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
612 enr = (int)atol((char *)regmatch.startp[i]);
613 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
614 lnum = atol((char *)regmatch.startp[i]);
615 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
616 col = (int)atol((char *)regmatch.startp[i]);
617 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
618 type = *regmatch.startp[i];
Bram Moolenaar4770d092006-01-12 23:22:24 +0000619 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 STRCPY(errmsg, IObuff);
621 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
622 {
623 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000624 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
626 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
627 tail = regmatch.startp[i];
628 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
629 {
630 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
631 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000632 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
635 {
636 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000637 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000639 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
640 {
641 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
642 if (len > CMDBUFFSIZE - 5)
643 len = CMDBUFFSIZE - 5;
644 STRCPY(pattern, "^\\V");
645 STRNCAT(pattern, regmatch.startp[i], len);
646 pattern[len + 3] = '\\';
647 pattern[len + 4] = '$';
648 pattern[len + 5] = NUL;
649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 break;
651 }
652 }
653 multiscan = FALSE;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000654 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000656 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
658 if (idx == 'D') /* enter directory */
659 {
660 if (*namebuf == NUL)
661 {
662 EMSG(_("E379: Missing or empty directory name"));
663 goto error2;
664 }
665 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
666 goto error2;
667 }
668 else if (idx == 'X') /* leave directory */
669 directory = qf_pop_dir(&dir_stack);
670 }
671 namebuf[0] = NUL; /* no match found, remove file name */
672 lnum = 0; /* don't jump to this line */
673 valid = FALSE;
674 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000675 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 multiline = multiignore = FALSE;
677 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000678 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
681 multiline = TRUE; /* start of a multi-line message */
682 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
683 { /* continuation of multi-line msg */
684 if (qfprev == NULL)
685 goto error2;
686 if (*errmsg && !multiignore)
687 {
688 len = (int)STRLEN(qfprev->qf_text);
689 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
690 == NULL)
691 goto error2;
692 STRCPY(ptr, qfprev->qf_text);
693 vim_free(qfprev->qf_text);
694 qfprev->qf_text = ptr;
695 *(ptr += len) = '\n';
696 STRCPY(++ptr, errmsg);
697 }
698 if (qfprev->qf_nr == -1)
699 qfprev->qf_nr = enr;
700 if (vim_isprintc(type) && !qfprev->qf_type)
701 qfprev->qf_type = type; /* only printable chars allowed */
702 if (!qfprev->qf_lnum)
703 qfprev->qf_lnum = lnum;
704 if (!qfprev->qf_col)
705 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (!qfprev->qf_fnum)
708 qfprev->qf_fnum = qf_get_fnum(directory,
709 *namebuf || directory ? namebuf
710 : currfile && valid ? currfile : 0);
711 if (idx == 'Z')
712 multiline = multiignore = FALSE;
713 line_breakcheck();
714 continue;
715 }
716 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
717 {
718 /* global file names */
719 valid = FALSE;
720 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
721 {
722 if (*namebuf && idx == 'P')
723 currfile = qf_push_dir(namebuf, &file_stack);
724 else if (idx == 'Q')
725 currfile = qf_pop_dir(&file_stack);
726 *namebuf = NUL;
727 if (tail && *tail)
728 {
729 STRCPY(IObuff, skipwhite(tail));
730 multiscan = TRUE;
731 goto restofline;
732 }
733 }
734 }
735 if (fmt_ptr->flags == '-') /* generally exclude this line */
736 {
737 if (multiline)
738 multiignore = TRUE; /* also exclude continuation lines */
739 continue;
740 }
741 }
742
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000743 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000745 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000747 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 errmsg,
749 lnum,
750 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000751 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000752 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 enr,
754 type,
755 valid) == FAIL)
756 goto error2;
757 line_breakcheck();
758 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000759 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000761 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000763 /* no valid entry found */
764 qi->qf_lists[qi->qf_curlist].qf_ptr =
765 qi->qf_lists[qi->qf_curlist].qf_start;
766 qi->qf_lists[qi->qf_curlist].qf_index = 1;
767 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 }
769 else
770 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000771 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
772 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
773 qi->qf_lists[qi->qf_curlist].qf_ptr =
774 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000776 /* return number of matches */
777 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 goto qf_init_ok;
779 }
780 EMSG(_(e_readerrf));
781error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000782 qf_free(qi, qi->qf_curlist);
783 qi->qf_listcount--;
784 if (qi->qf_curlist > 0)
785 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000787 if (fd != NULL)
788 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
790 {
791 fmt_first = fmt_ptr->next;
792 vim_free(fmt_ptr->prog);
793 vim_free(fmt_ptr);
794 }
795 qf_clean_dir_stack(&dir_stack);
796 qf_clean_dir_stack(&file_stack);
797qf_init_end:
798 vim_free(namebuf);
799 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000800 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 vim_free(fmtstr);
802
803#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000804 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#endif
806
807 return retval;
808}
809
810/*
811 * Prepare for adding a new quickfix list.
812 */
813 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000814qf_new_list(qi)
815 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 int i;
818
819 /*
820 * If the current entry is not the last entry, delete entries below
821 * the current entry. This makes it possible to browse in a tree-like
822 * way with ":grep'.
823 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000824 while (qi->qf_listcount > qi->qf_curlist + 1)
825 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 /*
828 * When the stack is full, remove to oldest entry
829 * Otherwise, add a new entry.
830 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000831 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000833 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000835 qi->qf_lists[i - 1] = qi->qf_lists[i];
836 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000839 qi->qf_curlist = qi->qf_listcount++;
840 qi->qf_lists[qi->qf_curlist].qf_index = 0;
841 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000844/*
845 * Free a location list
846 */
847 static void
848ll_free_all(pqi)
849 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000850{
851 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000852 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000853
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000854 qi = *pqi;
855 if (qi == NULL)
856 return;
857 *pqi = NULL; /* Remove reference to this list */
858
859 qi->qf_refcount--;
860 if (qi->qf_refcount < 1)
861 {
862 /* No references to this location list */
863 for (i = 0; i < qi->qf_listcount; ++i)
864 qf_free(qi, i);
865 vim_free(qi);
866 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000867}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000868
869 void
870qf_free_all(wp)
871 win_T *wp;
872{
873 int i;
874 qf_info_T *qi = &ql_info;
875
876 if (wp != NULL)
877 {
878 /* location list */
879 ll_free_all(&wp->w_llist);
880 ll_free_all(&wp->w_llist_ref);
881 }
882 else
883 /* quickfix list */
884 for (i = 0; i < qi->qf_listcount; ++i)
885 qf_free(qi, i);
886}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888/*
889 * Add an entry to the end of the list of errors.
890 * Returns OK or FAIL.
891 */
892 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000893qf_add_entry(qi, prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000894 valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000895 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000896 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 char_u *dir; /* optional directory name */
898 char_u *fname; /* file name or NULL */
899 char_u *mesg; /* message */
900 long lnum; /* line number */
901 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000903 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 int nr; /* error number */
905 int type; /* type character */
906 int valid; /* valid entry */
907{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000908 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000910 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 return FAIL;
912 qfp->qf_fnum = qf_get_fnum(dir, fname);
913 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
914 {
915 vim_free(qfp);
916 return FAIL;
917 }
918 qfp->qf_lnum = lnum;
919 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000920 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000921 if (pattern == NULL || *pattern == NUL)
922 qfp->qf_pattern = NULL;
923 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
924 {
925 vim_free(qfp->qf_text);
926 vim_free(qfp);
927 return FAIL;
928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 qfp->qf_nr = nr;
930 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
931 type = 0;
932 qfp->qf_type = type;
933 qfp->qf_valid = valid;
934
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000935 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
936 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000938 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 qfp->qf_prev = qfp; /* first element points to itself */
940 }
941 else
942 {
943 qfp->qf_prev = *prevp;
944 (*prevp)->qf_next = qfp;
945 }
946 qfp->qf_next = qfp; /* last element points to itself */
947 qfp->qf_cleared = FALSE;
948 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000949 ++qi->qf_lists[qi->qf_curlist].qf_count;
950 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
951 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000953 qi->qf_lists[qi->qf_curlist].qf_index =
954 qi->qf_lists[qi->qf_curlist].qf_count;
955 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957
958 return OK;
959}
960
961/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000962 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000963 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000964 static qf_info_T *
965ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000966{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000967 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000968
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000969 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
970 if (qi != NULL)
971 {
972 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
973 qi->qf_refcount++;
974 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000975
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000976 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000977}
978
979/*
980 * Return the location list for window 'wp'.
981 * If not present, allocate a location list
982 */
983 static qf_info_T *
984ll_get_or_alloc_list(wp)
985 win_T *wp;
986{
987 if (IS_LL_WINDOW(wp))
988 /* For a location list window, use the referenced location list */
989 return wp->w_llist_ref;
990
991 /*
992 * For a non-location list window, w_llist_ref should not point to a
993 * location list.
994 */
995 ll_free_all(&wp->w_llist_ref);
996
997 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000998 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000999 return wp->w_llist;
1000}
1001
1002/*
1003 * Copy the location list from window "from" to window "to".
1004 */
1005 void
1006copy_loclist(from, to)
1007 win_T *from;
1008 win_T *to;
1009{
1010 qf_info_T *qi;
1011 int idx;
1012 int i;
1013
1014 /*
1015 * When copying from a location list window, copy the referenced
1016 * location list. For other windows, copy the location list for
1017 * that window.
1018 */
1019 if (IS_LL_WINDOW(from))
1020 qi = from->w_llist_ref;
1021 else
1022 qi = from->w_llist;
1023
1024 if (qi == NULL) /* no location list to copy */
1025 return;
1026
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001027 /* allocate a new location list */
1028 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001029 return;
1030
1031 to->w_llist->qf_listcount = qi->qf_listcount;
1032
1033 /* Copy the location lists one at a time */
1034 for (idx = 0; idx < qi->qf_listcount; idx++)
1035 {
1036 qf_list_T *from_qfl;
1037 qf_list_T *to_qfl;
1038
1039 to->w_llist->qf_curlist = idx;
1040
1041 from_qfl = &qi->qf_lists[idx];
1042 to_qfl = &to->w_llist->qf_lists[idx];
1043
1044 /* Some of the fields are populated by qf_add_entry() */
1045 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1046 to_qfl->qf_count = 0;
1047 to_qfl->qf_index = 0;
1048 to_qfl->qf_start = NULL;
1049 to_qfl->qf_ptr = NULL;
1050
1051 if (from_qfl->qf_count)
1052 {
1053 qfline_T *from_qfp;
1054 qfline_T *prevp = NULL;
1055
1056 /* copy all the location entries in this list */
1057 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1058 ++i, from_qfp = from_qfp->qf_next)
1059 {
1060 if (qf_add_entry(to->w_llist, &prevp,
1061 NULL,
1062 NULL,
1063 from_qfp->qf_text,
1064 from_qfp->qf_lnum,
1065 from_qfp->qf_col,
1066 from_qfp->qf_viscol,
1067 from_qfp->qf_pattern,
1068 from_qfp->qf_nr,
1069 0,
1070 from_qfp->qf_valid) == FAIL)
1071 {
1072 qf_free_all(to);
1073 return;
1074 }
1075 /*
1076 * qf_add_entry() will not set the qf_num field, as the
1077 * directory and file names are not supplied. So the qf_fnum
1078 * field is copied here.
1079 */
1080 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1081 prevp->qf_type = from_qfp->qf_type; /* error type */
1082 if (from_qfl->qf_ptr == from_qfp)
1083 to_qfl->qf_ptr = prevp; /* current location */
1084 }
1085 }
1086
1087 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1088
1089 /* When no valid entries are present in the list, qf_ptr points to
1090 * the first item in the list */
1091 if (to_qfl->qf_nonevalid == TRUE)
1092 to_qfl->qf_ptr = to_qfl->qf_start;
1093 }
1094
1095 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1096}
1097
1098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * get buffer number for file "dir.name"
1100 */
1101 static int
1102qf_get_fnum(directory, fname)
1103 char_u *directory;
1104 char_u *fname;
1105{
1106 if (fname == NULL || *fname == NUL) /* no file name */
1107 return 0;
1108 {
1109#ifdef RISCOS
1110 /* Name is reported as `main.c', but file is `c.main' */
1111 return ro_buflist_add(fname);
1112#else
1113 char_u *ptr;
1114 int fnum;
1115
1116# ifdef VMS
1117 vms_remove_version(fname);
1118# endif
1119# ifdef BACKSLASH_IN_FILENAME
1120 if (directory != NULL)
1121 slash_adjust(directory);
1122 slash_adjust(fname);
1123# endif
1124 if (directory != NULL && !vim_isAbsName(fname)
1125 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1126 {
1127 /*
1128 * Here we check if the file really exists.
1129 * This should normally be true, but if make works without
1130 * "leaving directory"-messages we might have missed a
1131 * directory change.
1132 */
1133 if (mch_getperm(ptr) < 0)
1134 {
1135 vim_free(ptr);
1136 directory = qf_guess_filepath(fname);
1137 if (directory)
1138 ptr = concat_fnames(directory, fname, TRUE);
1139 else
1140 ptr = vim_strsave(fname);
1141 }
1142 /* Use concatenated directory name and file name */
1143 fnum = buflist_add(ptr, 0);
1144 vim_free(ptr);
1145 return fnum;
1146 }
1147 return buflist_add(fname, 0);
1148#endif
1149 }
1150}
1151
1152/*
1153 * push dirbuf onto the directory stack and return pointer to actual dir or
1154 * NULL on error
1155 */
1156 static char_u *
1157qf_push_dir(dirbuf, stackptr)
1158 char_u *dirbuf;
1159 struct dir_stack_T **stackptr;
1160{
1161 struct dir_stack_T *ds_new;
1162 struct dir_stack_T *ds_ptr;
1163
1164 /* allocate new stack element and hook it in */
1165 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1166 if (ds_new == NULL)
1167 return NULL;
1168
1169 ds_new->next = *stackptr;
1170 *stackptr = ds_new;
1171
1172 /* store directory on the stack */
1173 if (vim_isAbsName(dirbuf)
1174 || (*stackptr)->next == NULL
1175 || (*stackptr && dir_stack != *stackptr))
1176 (*stackptr)->dirname = vim_strsave(dirbuf);
1177 else
1178 {
1179 /* Okay we don't have an absolute path.
1180 * dirbuf must be a subdir of one of the directories on the stack.
1181 * Let's search...
1182 */
1183 ds_new = (*stackptr)->next;
1184 (*stackptr)->dirname = NULL;
1185 while (ds_new)
1186 {
1187 vim_free((*stackptr)->dirname);
1188 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1189 TRUE);
1190 if (mch_isdir((*stackptr)->dirname) == TRUE)
1191 break;
1192
1193 ds_new = ds_new->next;
1194 }
1195
1196 /* clean up all dirs we already left */
1197 while ((*stackptr)->next != ds_new)
1198 {
1199 ds_ptr = (*stackptr)->next;
1200 (*stackptr)->next = (*stackptr)->next->next;
1201 vim_free(ds_ptr->dirname);
1202 vim_free(ds_ptr);
1203 }
1204
1205 /* Nothing found -> it must be on top level */
1206 if (ds_new == NULL)
1207 {
1208 vim_free((*stackptr)->dirname);
1209 (*stackptr)->dirname = vim_strsave(dirbuf);
1210 }
1211 }
1212
1213 if ((*stackptr)->dirname != NULL)
1214 return (*stackptr)->dirname;
1215 else
1216 {
1217 ds_ptr = *stackptr;
1218 *stackptr = (*stackptr)->next;
1219 vim_free(ds_ptr);
1220 return NULL;
1221 }
1222}
1223
1224
1225/*
1226 * pop dirbuf from the directory stack and return previous directory or NULL if
1227 * stack is empty
1228 */
1229 static char_u *
1230qf_pop_dir(stackptr)
1231 struct dir_stack_T **stackptr;
1232{
1233 struct dir_stack_T *ds_ptr;
1234
1235 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1236 * What to do if it isn't? */
1237
1238 /* pop top element and free it */
1239 if (*stackptr != NULL)
1240 {
1241 ds_ptr = *stackptr;
1242 *stackptr = (*stackptr)->next;
1243 vim_free(ds_ptr->dirname);
1244 vim_free(ds_ptr);
1245 }
1246
1247 /* return NEW top element as current dir or NULL if stack is empty*/
1248 return *stackptr ? (*stackptr)->dirname : NULL;
1249}
1250
1251/*
1252 * clean up directory stack
1253 */
1254 static void
1255qf_clean_dir_stack(stackptr)
1256 struct dir_stack_T **stackptr;
1257{
1258 struct dir_stack_T *ds_ptr;
1259
1260 while ((ds_ptr = *stackptr) != NULL)
1261 {
1262 *stackptr = (*stackptr)->next;
1263 vim_free(ds_ptr->dirname);
1264 vim_free(ds_ptr);
1265 }
1266}
1267
1268/*
1269 * Check in which directory of the directory stack the given file can be
1270 * found.
1271 * Returns a pointer to the directory name or NULL if not found
1272 * Cleans up intermediate directory entries.
1273 *
1274 * TODO: How to solve the following problem?
1275 * If we have the this directory tree:
1276 * ./
1277 * ./aa
1278 * ./aa/bb
1279 * ./bb
1280 * ./bb/x.c
1281 * and make says:
1282 * making all in aa
1283 * making all in bb
1284 * x.c:9: Error
1285 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1286 * qf_guess_filepath will return NULL.
1287 */
1288 static char_u *
1289qf_guess_filepath(filename)
1290 char_u *filename;
1291{
1292 struct dir_stack_T *ds_ptr;
1293 struct dir_stack_T *ds_tmp;
1294 char_u *fullname;
1295
1296 /* no dirs on the stack - there's nothing we can do */
1297 if (dir_stack == NULL)
1298 return NULL;
1299
1300 ds_ptr = dir_stack->next;
1301 fullname = NULL;
1302 while (ds_ptr)
1303 {
1304 vim_free(fullname);
1305 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1306
1307 /* If concat_fnames failed, just go on. The worst thing that can happen
1308 * is that we delete the entire stack.
1309 */
1310 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1311 break;
1312
1313 ds_ptr = ds_ptr->next;
1314 }
1315
1316 vim_free(fullname);
1317
1318 /* clean up all dirs we already left */
1319 while (dir_stack->next != ds_ptr)
1320 {
1321 ds_tmp = dir_stack->next;
1322 dir_stack->next = dir_stack->next->next;
1323 vim_free(ds_tmp->dirname);
1324 vim_free(ds_tmp);
1325 }
1326
1327 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1328
1329}
1330
1331/*
1332 * jump to a quickfix line
1333 * if dir == FORWARD go "errornr" valid entries forward
1334 * if dir == BACKWARD go "errornr" valid entries backward
1335 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1336 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1337 * else if "errornr" is zero, redisplay the same line
1338 * else go to entry "errornr"
1339 */
1340 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001341qf_jump(qi, dir, errornr, forceit)
1342 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int dir;
1344 int errornr;
1345 int forceit;
1346{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001347 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001348 qfline_T *qf_ptr;
1349 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 int qf_index;
1351 int old_qf_fnum;
1352 int old_qf_index;
1353 int prev_index;
1354 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1355 char_u *err = e_no_more_items;
1356 linenr_T i;
1357 buf_T *old_curbuf;
1358 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 colnr_T screen_col;
1360 colnr_T char_col;
1361 char_u *line;
1362#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001363 char_u *old_swb = p_swb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 int opened_window = FALSE;
1365 win_T *win;
1366 win_T *altwin;
1367#endif
1368 int print_message = TRUE;
1369 int len;
1370#ifdef FEAT_FOLDING
1371 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1372#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001374 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001376 if (qi == NULL)
1377 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001378
1379 if (qi->qf_curlist >= qi->qf_listcount
1380 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
1382 EMSG(_(e_quickfix));
1383 return;
1384 }
1385
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001386 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 old_qf_index = qf_index;
1390 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1391 {
1392 while (errornr--)
1393 {
1394 old_qf_ptr = qf_ptr;
1395 prev_index = qf_index;
1396 old_qf_fnum = qf_ptr->qf_fnum;
1397 do
1398 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001399 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 || qf_ptr->qf_next == NULL)
1401 {
1402 qf_ptr = old_qf_ptr;
1403 qf_index = prev_index;
1404 if (err != NULL)
1405 {
1406 EMSG(_(err));
1407 goto theend;
1408 }
1409 errornr = 0;
1410 break;
1411 }
1412 ++qf_index;
1413 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001414 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1415 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1417 err = NULL;
1418 }
1419 }
1420 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1421 {
1422 while (errornr--)
1423 {
1424 old_qf_ptr = qf_ptr;
1425 prev_index = qf_index;
1426 old_qf_fnum = qf_ptr->qf_fnum;
1427 do
1428 {
1429 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1430 {
1431 qf_ptr = old_qf_ptr;
1432 qf_index = prev_index;
1433 if (err != NULL)
1434 {
1435 EMSG(_(err));
1436 goto theend;
1437 }
1438 errornr = 0;
1439 break;
1440 }
1441 --qf_index;
1442 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001443 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1444 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1446 err = NULL;
1447 }
1448 }
1449 else if (errornr != 0) /* go to specified number */
1450 {
1451 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1452 {
1453 --qf_index;
1454 qf_ptr = qf_ptr->qf_prev;
1455 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001456 while (errornr > qf_index && qf_index <
1457 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 && qf_ptr->qf_next != NULL)
1459 {
1460 ++qf_index;
1461 qf_ptr = qf_ptr->qf_next;
1462 }
1463 }
1464
1465#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001466 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1467 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 /* No need to print the error message if it's visible in the error
1469 * window */
1470 print_message = FALSE;
1471
1472 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001473 * For ":helpgrep" find a help window or open one.
1474 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001475 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001476 {
1477 win_T *wp;
1478 int n;
1479
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001480 if (cmdmod.tab != 0)
1481 wp = NULL;
1482 else
1483 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1484 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1485 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001486 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1487 win_enter(wp, TRUE);
1488 else
1489 {
1490 /*
1491 * Split off help window; put it at far top if no position
1492 * specified, the current window is vertically split and narrow.
1493 */
1494 n = WSP_HELP;
1495# ifdef FEAT_VERTSPLIT
1496 if (cmdmod.split == 0 && curwin->w_width != Columns
1497 && curwin->w_width < 80)
1498 n |= WSP_TOP;
1499# endif
1500 if (win_split(0, n) == FAIL)
1501 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001502 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001503
1504 if (curwin->w_height < p_hh)
1505 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001506
1507 if (qi != &ql_info) /* not a quickfix list */
1508 {
1509 /* The new window should use the supplied location list */
1510 qf_free_all(curwin);
1511 curwin->w_llist = qi;
1512 qi->qf_refcount++;
1513 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001514 }
1515
1516 if (!p_im)
1517 restart_edit = 0; /* don't want insert mode in help file */
1518 }
1519
1520 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 * If currently in the quickfix window, find another window to show the
1522 * file in.
1523 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001524 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 /*
1527 * If there is no file specified, we don't know where to go.
1528 * But do advance, otherwise ":cn" gets stuck.
1529 */
1530 if (qf_ptr->qf_fnum == 0)
1531 goto theend;
1532
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001533 /* Locate a window showing a normal buffer */
1534 usable_win = 0;
1535 FOR_ALL_WINDOWS(win)
1536 if (win->w_buffer->b_p_bt[0] == NUL)
1537 {
1538 usable_win = 1;
1539 break;
1540 }
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 /*
1543 * If there is only one window, create a new one above the quickfix
1544 * window.
1545 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546 if (firstwin == lastwin || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548 ll_ref = curwin->w_llist_ref;
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (win_split(0, WSP_ABOVE) == FAIL)
1551 goto failed; /* not enough room for window */
1552 opened_window = TRUE; /* close it when fail */
1553 p_swb = empty_option; /* don't split again */
1554# ifdef FEAT_SCROLLBIND
1555 curwin->w_p_scb = FALSE;
1556# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001557 if (ll_ref != NULL)
1558 {
1559 /* The new window should use the location list from the
1560 * location list window */
1561 qf_free_all(curwin);
1562 curwin->w_llist = ll_ref;
1563 ll_ref->qf_refcount++;
1564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566 else
1567 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001568 if (curwin->w_llist_ref != NULL)
1569 {
1570 /* In a location window */
1571 ll_ref = curwin->w_llist_ref;
1572
1573 /* Find the window with the same location list */
1574 FOR_ALL_WINDOWS(win)
1575 if (win->w_llist == ll_ref)
1576 break;
1577 if (win == NULL)
1578 {
1579 /* Find the window showing the selected file */
1580 FOR_ALL_WINDOWS(win)
1581 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1582 break;
1583 if (win == NULL)
1584 {
1585 /* Find a previous usable window */
1586 win = curwin;
1587 do
1588 {
1589 if (win->w_buffer->b_p_bt[0] == NUL)
1590 break;
1591 if (win->w_prev == NULL)
1592 win = lastwin; /* wrap around the top */
1593 else
1594 win = win->w_prev; /* go to previous window */
1595 } while (win != curwin);
1596 }
1597 }
1598 win_goto(win);
1599
1600 /* If the location list for the window is not set, then set it
1601 * to the location list from the location window */
1602 if (win->w_llist == NULL)
1603 {
1604 win->w_llist = ll_ref;
1605 ll_ref->qf_refcount++;
1606 }
1607 }
1608 else
1609 {
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 /*
1612 * Try to find a window that shows the right buffer.
1613 * Default to the window just above the quickfix buffer.
1614 */
1615 win = curwin;
1616 altwin = NULL;
1617 for (;;)
1618 {
1619 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1620 break;
1621 if (win->w_prev == NULL)
1622 win = lastwin; /* wrap around the top */
1623 else
1624 win = win->w_prev; /* go to previous window */
1625
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001626 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628 /* Didn't find it, go to the window before the quickfix
1629 * window. */
1630 if (altwin != NULL)
1631 win = altwin;
1632 else if (curwin->w_prev != NULL)
1633 win = curwin->w_prev;
1634 else
1635 win = curwin->w_next;
1636 break;
1637 }
1638
1639 /* Remember a usable window. */
1640 if (altwin == NULL && !win->w_p_pvw
1641 && win->w_buffer->b_p_bt[0] == NUL)
1642 altwin = win;
1643 }
1644
1645 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 }
1648 }
1649#endif
1650
1651 /*
1652 * If there is a file name,
1653 * read the wanted file if needed, and check autowrite etc.
1654 */
1655 old_curbuf = curbuf;
1656 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001657
1658 if (qf_ptr->qf_fnum != 0)
1659 {
1660 if (qf_ptr->qf_type == 1)
1661 {
1662 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1663 * set b_p_ro flag). */
1664 if (!can_abandon(curbuf, forceit))
1665 {
1666 EMSG(_(e_nowrtmsg));
1667 ok = FALSE;
1668 }
1669 else
1670 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1671 ECMD_HIDE + ECMD_SET_HELP);
1672 }
1673 else
1674 ok = buflist_getfile(qf_ptr->qf_fnum,
1675 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1676 }
1677
1678 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 /* When not switched to another buffer, still need to set pc mark */
1681 if (curbuf == old_curbuf)
1682 setpcmark();
1683
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001684 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001686 /*
1687 * Go to line with error, unless qf_lnum is 0.
1688 */
1689 i = qf_ptr->qf_lnum;
1690 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001692 if (i > curbuf->b_ml.ml_line_count)
1693 i = curbuf->b_ml.ml_line_count;
1694 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001696 if (qf_ptr->qf_col > 0)
1697 {
1698 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1699 if (qf_ptr->qf_viscol == TRUE)
1700 {
1701 /*
1702 * Check each character from the beginning of the error
1703 * line up to the error column. For each tab character
1704 * found, reduce the error column value by the length of
1705 * a tab character.
1706 */
1707 line = ml_get_curline();
1708 screen_col = 0;
1709 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1710 {
1711 if (*line == NUL)
1712 break;
1713 if (*line++ == '\t')
1714 {
1715 curwin->w_cursor.col -= 7 - (screen_col % 8);
1716 screen_col += 8 - (screen_col % 8);
1717 }
1718 else
1719 ++screen_col;
1720 }
1721 }
1722 check_cursor();
1723 }
1724 else
1725 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
1727 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001728 {
1729 pos_T save_cursor;
1730
1731 /* Move the cursor to the first line in the buffer */
1732 save_cursor = curwin->w_cursor;
1733 curwin->w_cursor.lnum = 0;
1734 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1735 curwin->w_cursor = save_cursor;
1736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
1738#ifdef FEAT_FOLDING
1739 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1740 foldOpenCursor();
1741#endif
1742 if (print_message)
1743 {
1744 /* Update the screen before showing the message */
1745 update_topline_redraw();
1746 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001747 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1749 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1750 /* Add the message, skipping leading whitespace and newlines. */
1751 len = (int)STRLEN(IObuff);
1752 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1753
1754 /* Output the message. Overwrite to avoid scrolling when the 'O'
1755 * flag is present in 'shortmess'; But when not jumping, print the
1756 * whole message. */
1757 i = msg_scroll;
1758 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1759 msg_scroll = TRUE;
1760 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1761 msg_scroll = FALSE;
1762 msg_attr_keep(IObuff, 0, TRUE);
1763 msg_scroll = i;
1764 }
1765 }
1766 else
1767 {
1768#ifdef FEAT_WINDOWS
1769 if (opened_window)
1770 win_close(curwin, TRUE); /* Close opened window */
1771#endif
1772 if (qf_ptr->qf_fnum != 0)
1773 {
1774 /*
1775 * Couldn't open file, so put index back where it was. This could
1776 * happen if the file was readonly and we changed something.
1777 */
1778#ifdef FEAT_WINDOWS
1779failed:
1780#endif
1781 qf_ptr = old_qf_ptr;
1782 qf_index = old_qf_index;
1783 }
1784 }
1785theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001786 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1787 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#ifdef FEAT_WINDOWS
1789 if (p_swb != old_swb && opened_window)
1790 {
1791 /* Restore old 'switchbuf' value, but not when an autocommand or
1792 * modeline has changed the value. */
1793 if (p_swb == empty_option)
1794 p_swb = old_swb;
1795 else
1796 free_string_option(old_swb);
1797 }
1798#endif
1799}
1800
1801/*
1802 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001803 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 */
1805 void
1806qf_list(eap)
1807 exarg_T *eap;
1808{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001809 buf_T *buf;
1810 char_u *fname;
1811 qfline_T *qfp;
1812 int i;
1813 int idx1 = 1;
1814 int idx2 = -1;
1815 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001816 char_u *arg = eap->arg;
1817 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001819 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001821 if (eap->cmdidx == CMD_llist)
1822 {
1823 qi = GET_LOC_LIST(curwin);
1824 if (qi == NULL)
1825 {
1826 EMSG(_(e_loclist));
1827 return;
1828 }
1829 }
1830
1831 if (qi->qf_curlist >= qi->qf_listcount
1832 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
1834 EMSG(_(e_quickfix));
1835 return;
1836 }
1837 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1838 {
1839 EMSG(_(e_trailing));
1840 return;
1841 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001842 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (idx1 < 0)
1844 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1845 if (idx2 < 0)
1846 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1847
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001848 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001850 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1851 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1854 {
1855 if (need_return)
1856 {
1857 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001858 if (got_int)
1859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 need_return = FALSE;
1861 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001862
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001863 fname = NULL;
1864 if (qfp->qf_fnum != 0
1865 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1866 {
1867 fname = buf->b_fname;
1868 if (qfp->qf_type == 1) /* :helpgrep */
1869 fname = gettail(fname);
1870 }
1871 if (fname == NULL)
1872 sprintf((char *)IObuff, "%2d", i);
1873 else
1874 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1875 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001876 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001877 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1878 if (qfp->qf_lnum == 0)
1879 IObuff[0] = NUL;
1880 else if (qfp->qf_col == 0)
1881 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1882 else
1883 sprintf((char *)IObuff, ":%ld col %d",
1884 qfp->qf_lnum, qfp->qf_col);
1885 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1886 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1887 msg_puts_attr(IObuff, hl_attr(HLF_N));
1888 if (qfp->qf_pattern != NULL)
1889 {
1890 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1891 STRCAT(IObuff, ":");
1892 msg_puts(IObuff);
1893 }
1894 msg_puts((char_u *)" ");
1895
1896 /* Remove newlines and leading whitespace from the text. For an
1897 * unrecognized line keep the indent, the compiler may mark a word
1898 * with ^^^^. */
1899 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1901 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001902 msg_prt_line(IObuff, FALSE);
1903 out_flush(); /* show one line at a time */
1904 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001906
1907 qfp = qfp->qf_next;
1908 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 ui_breakcheck();
1910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911}
1912
1913/*
1914 * Remove newlines and leading whitespace from an error message.
1915 * Put the result in "buf[bufsize]".
1916 */
1917 static void
1918qf_fmt_text(text, buf, bufsize)
1919 char_u *text;
1920 char_u *buf;
1921 int bufsize;
1922{
1923 int i;
1924 char_u *p = text;
1925
1926 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1927 {
1928 if (*p == '\n')
1929 {
1930 buf[i] = ' ';
1931 while (*++p != NUL)
1932 if (!vim_iswhite(*p) && *p != '\n')
1933 break;
1934 }
1935 else
1936 buf[i] = *p++;
1937 }
1938 buf[i] = NUL;
1939}
1940
1941/*
1942 * ":colder [count]": Up in the quickfix stack.
1943 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001944 * ":lolder [count]": Up in the location list stack.
1945 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 */
1947 void
1948qf_age(eap)
1949 exarg_T *eap;
1950{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001951 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 int count;
1953
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001954 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
1955 {
1956 qi = GET_LOC_LIST(curwin);
1957 if (qi == NULL)
1958 {
1959 EMSG(_(e_loclist));
1960 return;
1961 }
1962 }
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (eap->addr_count != 0)
1965 count = eap->line2;
1966 else
1967 count = 1;
1968 while (count--)
1969 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001970 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001972 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 EMSG(_("E380: At bottom of quickfix stack"));
1975 return;
1976 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001977 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979 else
1980 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001981 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 EMSG(_("E381: At top of quickfix stack"));
1984 return;
1985 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001986 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
1988 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001989 qf_msg(qi);
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991}
1992
1993 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001994qf_msg(qi)
1995 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001998 qi->qf_curlist + 1, qi->qf_listcount,
1999 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002001 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002#endif
2003}
2004
2005/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002006 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 */
2008 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009qf_free(qi, idx)
2010 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 int idx;
2012{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002013 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 qfp = qi->qf_lists[idx].qf_start->qf_next;
2018 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2019 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2020 vim_free(qi->qf_lists[idx].qf_start);
2021 qi->qf_lists[idx].qf_start = qfp;
2022 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024}
2025
2026/*
2027 * qf_mark_adjust: adjust marks
2028 */
2029 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030qf_mark_adjust(wp, line1, line2, amount, amount_after)
2031 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 linenr_T line1;
2033 linenr_T line2;
2034 long amount;
2035 long amount_after;
2036{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002037 int i;
2038 qfline_T *qfp;
2039 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002040 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002042 if (wp != NULL)
2043 {
2044 if (wp->w_llist == NULL)
2045 return;
2046 qi = wp->w_llist;
2047 }
2048
2049 for (idx = 0; idx < qi->qf_listcount; ++idx)
2050 if (qi->qf_lists[idx].qf_count)
2051 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2052 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 if (qfp->qf_fnum == curbuf->b_fnum)
2054 {
2055 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2056 {
2057 if (amount == MAXLNUM)
2058 qfp->qf_cleared = TRUE;
2059 else
2060 qfp->qf_lnum += amount;
2061 }
2062 else if (amount_after && qfp->qf_lnum > line2)
2063 qfp->qf_lnum += amount_after;
2064 }
2065}
2066
2067/*
2068 * Make a nice message out of the error character and the error number:
2069 * char number message
2070 * e or E 0 " error"
2071 * w or W 0 " warning"
2072 * i or I 0 " info"
2073 * 0 0 ""
2074 * other 0 " c"
2075 * e or E n " error n"
2076 * w or W n " warning n"
2077 * i or I n " info n"
2078 * 0 n " error n"
2079 * other n " c n"
2080 * 1 x "" :helpgrep
2081 */
2082 static char_u *
2083qf_types(c, nr)
2084 int c, nr;
2085{
2086 static char_u buf[20];
2087 static char_u cc[3];
2088 char_u *p;
2089
2090 if (c == 'W' || c == 'w')
2091 p = (char_u *)" warning";
2092 else if (c == 'I' || c == 'i')
2093 p = (char_u *)" info";
2094 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2095 p = (char_u *)" error";
2096 else if (c == 0 || c == 1)
2097 p = (char_u *)"";
2098 else
2099 {
2100 cc[0] = ' ';
2101 cc[1] = c;
2102 cc[2] = NUL;
2103 p = cc;
2104 }
2105
2106 if (nr <= 0)
2107 return p;
2108
2109 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2110 return buf;
2111}
2112
2113#if defined(FEAT_WINDOWS) || defined(PROTO)
2114/*
2115 * ":cwindow": open the quickfix window if we have errors to display,
2116 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002117 * ":lwindow": open the location list window if we have locations to display,
2118 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
2120 void
2121ex_cwindow(eap)
2122 exarg_T *eap;
2123{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002124 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 win_T *win;
2126
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002127 if (eap->cmdidx == CMD_lwindow)
2128 {
2129 qi = GET_LOC_LIST(curwin);
2130 if (qi == NULL)
2131 return;
2132 }
2133
2134 /* Look for an existing quickfix window. */
2135 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 /*
2138 * If a quickfix window is open but we have no errors to display,
2139 * close the window. If a quickfix window is not open, then open
2140 * it if we have errors; otherwise, leave it closed.
2141 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002142 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
2143 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
2145 if (win != NULL)
2146 ex_cclose(eap);
2147 }
2148 else if (win == NULL)
2149 ex_copen(eap);
2150}
2151
2152/*
2153 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002154 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 */
2156/*ARGSUSED*/
2157 void
2158ex_cclose(eap)
2159 exarg_T *eap;
2160{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002161 win_T *win = NULL;
2162 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002164 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2165 {
2166 qi = GET_LOC_LIST(curwin);
2167 if (qi == NULL)
2168 return;
2169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171 /* Find existing quickfix window and close it. */
2172 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (win != NULL)
2174 win_close(win, FALSE);
2175}
2176
2177/*
2178 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002179 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 */
2181 void
2182ex_copen(eap)
2183 exarg_T *eap;
2184{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002185 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002188 tabpage_T *prevtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2191 {
2192 qi = GET_LOC_LIST(curwin);
2193 if (qi == NULL)
2194 {
2195 EMSG(_(e_loclist));
2196 return;
2197 }
2198 }
2199
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (eap->addr_count != 0)
2201 height = eap->line2;
2202 else
2203 height = QF_WINHEIGHT;
2204
2205#ifdef FEAT_VISUAL
2206 reset_VIsual_and_resel(); /* stop Visual mode */
2207#endif
2208#ifdef FEAT_GUI
2209 need_mouse_correct = TRUE;
2210#endif
2211
2212 /*
2213 * Find existing quickfix window, or open a new one.
2214 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 win = qf_find_win(qi);
2216
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002217 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 win_goto(win);
2219 else
2220 {
2221 /* The current window becomes the previous window afterwards. */
2222 win = curwin;
2223
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002224 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2225 /* Create the new window at the very bottom. */
2226 win_goto(lastwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (win_split(height, WSP_BELOW) == FAIL)
2228 return; /* not enough room for window */
2229#ifdef FEAT_SCROLLBIND
2230 curwin->w_p_scb = FALSE;
2231#endif
2232
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 /* Remove the location list for the quickfix window */
2234 qf_free_all(curwin);
2235
2236 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002238 /*
2239 * For the location list window, create a reference to the
2240 * location list from the window 'win'.
2241 */
2242 curwin->w_llist_ref = win->w_llist;
2243 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002245
2246 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2247 /* switch off 'swapfile' */
2248 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2249 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
2250 OPT_LOCAL);
2251 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2252 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002254 /* Only set the height when still in the same tab page and there is no
2255 * window to the side. */
2256 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002257#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002258 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002259#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002260 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 win_setheight(height);
2262 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2263 if (win_valid(win))
2264 prevwin = win;
2265 }
2266
2267 /*
2268 * Fill the buffer with the quickfix list.
2269 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002270 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002272 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 curwin->w_cursor.col = 0;
2274 check_cursor();
2275 update_topline(); /* scroll to show the line */
2276}
2277
2278/*
2279 * Return the number of the current entry (line number in the quickfix
2280 * window).
2281 */
2282 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002283qf_current_entry(wp)
2284 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002286 qf_info_T *qi = &ql_info;
2287
2288 if (IS_LL_WINDOW(wp))
2289 /* In the location list window, use the referenced location list */
2290 qi = wp->w_llist_ref;
2291
2292 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293}
2294
2295/*
2296 * Update the cursor position in the quickfix window to the current error.
2297 * Return TRUE if there is a quickfix window.
2298 */
2299 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002300qf_win_pos_update(qi, old_qf_index)
2301 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 int old_qf_index; /* previous qf_index or zero */
2303{
2304 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002305 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
2307 /*
2308 * Put the cursor on the current error in the quickfix window, so that
2309 * it's viewable.
2310 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002311 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (win != NULL
2313 && qf_index <= win->w_buffer->b_ml.ml_line_count
2314 && old_qf_index != qf_index)
2315 {
2316 win_T *old_curwin = curwin;
2317
2318 curwin = win;
2319 curbuf = win->w_buffer;
2320 if (qf_index > old_qf_index)
2321 {
2322 curwin->w_redraw_top = old_qf_index;
2323 curwin->w_redraw_bot = qf_index;
2324 }
2325 else
2326 {
2327 curwin->w_redraw_top = qf_index;
2328 curwin->w_redraw_bot = old_qf_index;
2329 }
2330 curwin->w_cursor.lnum = qf_index;
2331 curwin->w_cursor.col = 0;
2332 update_topline(); /* scroll to show the line */
2333 redraw_later(VALID);
2334 curwin->w_redr_status = TRUE; /* update ruler */
2335 curwin = old_curwin;
2336 curbuf = curwin->w_buffer;
2337 }
2338 return win != NULL;
2339}
2340
2341/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002342 * Find a window displaying the quickfix/location list 'qi'
2343 */
2344 static win_T *
2345qf_find_win(qi)
2346 qf_info_T *qi;
2347{
2348 win_T *win;
2349
2350 /*
2351 * When searching for the quickfix buffer, find a window
2352 * with w_llist_ref set to NULL.
2353 * When searching for the location list buffer, find a window
2354 * with w_llist_ref pointing to the supplied location list.
2355 */
2356 FOR_ALL_WINDOWS(win)
2357 if (bt_quickfix(win->w_buffer))
2358 if ((qi == &ql_info && win->w_llist_ref == NULL)
2359 || (qi != &ql_info && win->w_llist_ref == qi))
2360 break;
2361
2362 return win;
2363}
2364
2365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 * Find quickfix buffer.
2367 */
2368 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002369qf_find_buf(qi)
2370 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002372 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002374 win = qf_find_win(qi);
2375
2376 return (win != NULL) ? win->w_buffer: NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377}
2378
2379/*
2380 * Find the quickfix buffer. If it exists, update the contents.
2381 */
2382 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002383qf_update_buffer(qi)
2384 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 buf_T *buf;
2387#ifdef FEAT_AUTOCMD
2388 aco_save_T aco;
2389#else
2390 buf_T *save_curbuf;
2391#endif
2392
2393 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002394 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 if (buf != NULL)
2396 {
2397#ifdef FEAT_AUTOCMD
2398 /* set curwin/curbuf to buf and save a few things */
2399 aucmd_prepbuf(&aco, buf);
2400#else
2401 save_curbuf = curbuf;
2402 curbuf = buf;
2403#endif
2404
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407#ifdef FEAT_AUTOCMD
2408 /* restore curwin/curbuf and a few other things */
2409 aucmd_restbuf(&aco);
2410#else
2411 curbuf = save_curbuf;
2412#endif
2413
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002414 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416}
2417
2418/*
2419 * Fill current buffer with quickfix errors, replacing any previous contents.
2420 * curbuf must be the quickfix buffer!
2421 */
2422 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002423qf_fill_buffer(qi)
2424 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002426 linenr_T lnum;
2427 qfline_T *qfp;
2428 buf_T *errbuf;
2429 int len;
2430 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
2432 /* delete all existing lines */
2433 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2434 (void)ml_delete((linenr_T)1, FALSE);
2435
2436 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002437 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
2439 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002440 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2441 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
2443 if (qfp->qf_fnum != 0
2444 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2445 && errbuf->b_fname != NULL)
2446 {
2447 if (qfp->qf_type == 1) /* :helpgrep */
2448 STRCPY(IObuff, gettail(errbuf->b_fname));
2449 else
2450 STRCPY(IObuff, errbuf->b_fname);
2451 len = (int)STRLEN(IObuff);
2452 }
2453 else
2454 len = 0;
2455 IObuff[len++] = '|';
2456
2457 if (qfp->qf_lnum > 0)
2458 {
2459 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2460 len += (int)STRLEN(IObuff + len);
2461
2462 if (qfp->qf_col > 0)
2463 {
2464 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2465 len += (int)STRLEN(IObuff + len);
2466 }
2467
2468 sprintf((char *)IObuff + len, "%s",
2469 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2470 len += (int)STRLEN(IObuff + len);
2471 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002472 else if (qfp->qf_pattern != NULL)
2473 {
2474 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2475 len += (int)STRLEN(IObuff + len);
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 IObuff[len++] = '|';
2478 IObuff[len++] = ' ';
2479
2480 /* Remove newlines and leading whitespace from the text.
2481 * For an unrecognized line keep the indent, the compiler may
2482 * mark a word with ^^^^. */
2483 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2484 IObuff + len, IOSIZE - len);
2485
2486 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2487 == FAIL)
2488 break;
2489 qfp = qfp->qf_next;
2490 }
2491 /* Delete the empty line which is now at the end */
2492 (void)ml_delete(lnum + 1, FALSE);
2493 }
2494
2495 /* correct cursor position */
2496 check_lnums(TRUE);
2497
2498 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2499 * resembles reading a file into a buffer, it's more logical when using
2500 * autocommands. */
2501 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2502 curbuf->b_p_ma = FALSE;
2503
2504#ifdef FEAT_AUTOCMD
2505 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2506 FALSE, curbuf);
2507 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2508 FALSE, curbuf);
2509#endif
2510
2511 /* make sure it will be redrawn */
2512 redraw_curbuf_later(NOT_VALID);
2513
2514 /* Restore KeyTyped, setting 'filetype' may reset it. */
2515 KeyTyped = old_KeyTyped;
2516}
2517
2518#endif /* FEAT_WINDOWS */
2519
2520/*
2521 * Return TRUE if "buf" is the quickfix buffer.
2522 */
2523 int
2524bt_quickfix(buf)
2525 buf_T *buf;
2526{
2527 return (buf->b_p_bt[0] == 'q');
2528}
2529
2530/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002531 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2532 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
2534 int
2535bt_nofile(buf)
2536 buf_T *buf;
2537{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002538 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2539 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540}
2541
2542/*
2543 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2544 */
2545 int
2546bt_dontwrite(buf)
2547 buf_T *buf;
2548{
2549 return (buf->b_p_bt[0] == 'n');
2550}
2551
2552 int
2553bt_dontwrite_msg(buf)
2554 buf_T *buf;
2555{
2556 if (bt_dontwrite(buf))
2557 {
2558 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2559 return TRUE;
2560 }
2561 return FALSE;
2562}
2563
2564/*
2565 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2566 * and 'bufhidden'.
2567 */
2568 int
2569buf_hide(buf)
2570 buf_T *buf;
2571{
2572 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2573 switch (buf->b_p_bh[0])
2574 {
2575 case 'u': /* "unload" */
2576 case 'w': /* "wipe" */
2577 case 'd': return FALSE; /* "delete" */
2578 case 'h': return TRUE; /* "hide" */
2579 }
2580 return (p_hid || cmdmod.hide);
2581}
2582
2583/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002584 * Return TRUE when using ":vimgrep" for ":grep".
2585 */
2586 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002587grep_internal(cmdidx)
2588 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002589{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002590 return ((cmdidx == CMD_grep
2591 || cmdidx == CMD_lgrep
2592 || cmdidx == CMD_grepadd
2593 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002594 && STRCMP("internal",
2595 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2596}
2597
2598/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002599 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 */
2601 void
2602ex_make(eap)
2603 exarg_T *eap;
2604{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002605 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 char_u *cmd;
2607 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002608 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002609 qf_info_T *qi = &ql_info;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002610#ifdef FEAT_AUTOCMD
2611 char_u *au_name = NULL;
2612
2613 switch (eap->cmdidx)
2614 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002615 case CMD_make: au_name = (char_u *)"make"; break;
2616 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2617 case CMD_grep: au_name = (char_u *)"grep"; break;
2618 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2619 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2620 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002621 default: break;
2622 }
2623 if (au_name != NULL)
2624 {
2625 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2626 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002627# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002628 if (did_throw || force_abort)
2629 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002630# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002631 }
2632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
Bram Moolenaar86b68352004-12-27 21:59:20 +00002634 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002635 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002636 {
2637 ex_vimgrep(eap);
2638 return;
2639 }
2640
Bram Moolenaara6557602006-02-04 22:43:20 +00002641 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2642 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002643 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002646 fname = get_mef_name();
2647 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002649 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 /*
2652 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2653 */
2654 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2655 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002656 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 cmd = alloc(len);
2658 if (cmd == NULL)
2659 return;
2660 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2661 (char *)p_shq);
2662 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002663 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 /*
2665 * Output a newline if there's something else than the :make command that
2666 * was typed (in which case the cursor is in column 0).
2667 */
2668 if (msg_col == 0)
2669 msg_didout = FALSE;
2670 msg_start();
2671 MSG_PUTS(":!");
2672 msg_outtrans(cmd); /* show what we are doing */
2673
2674 /* let the shell know if we are redirecting output or not */
2675 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2676
2677#ifdef AMIGA
2678 out_flush();
2679 /* read window status report and redraw before message */
2680 (void)char_avail();
2681#endif
2682
Bram Moolenaara6557602006-02-04 22:43:20 +00002683 if (qf_init(wp, fname, (eap->cmdidx != CMD_make
2684 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2685 (eap->cmdidx != CMD_grepadd
2686 && eap->cmdidx != CMD_lgrepadd)) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 && !eap->forceit)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002688 {
2689 if (wp != NULL)
2690 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002691 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
Bram Moolenaar7c626922005-02-07 22:01:03 +00002694 mch_remove(fname);
2695 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002697
2698#ifdef FEAT_AUTOCMD
2699 if (au_name != NULL)
2700 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2701 curbuf->b_fname, TRUE, curbuf);
2702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703}
2704
2705/*
2706 * Return the name for the errorfile, in allocated memory.
2707 * Find a new unique name when 'makeef' contains "##".
2708 * Returns NULL for error.
2709 */
2710 static char_u *
2711get_mef_name()
2712{
2713 char_u *p;
2714 char_u *name;
2715 static int start = -1;
2716 static int off = 0;
2717#ifdef HAVE_LSTAT
2718 struct stat sb;
2719#endif
2720
2721 if (*p_mef == NUL)
2722 {
2723 name = vim_tempname('e');
2724 if (name == NULL)
2725 EMSG(_(e_notmp));
2726 return name;
2727 }
2728
2729 for (p = p_mef; *p; ++p)
2730 if (p[0] == '#' && p[1] == '#')
2731 break;
2732
2733 if (*p == NUL)
2734 return vim_strsave(p_mef);
2735
2736 /* Keep trying until the name doesn't exist yet. */
2737 for (;;)
2738 {
2739 if (start == -1)
2740 start = mch_get_pid();
2741 else
2742 off += 19;
2743
2744 name = alloc((unsigned)STRLEN(p_mef) + 30);
2745 if (name == NULL)
2746 break;
2747 STRCPY(name, p_mef);
2748 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2749 STRCAT(name, p + 2);
2750 if (mch_getperm(name) < 0
2751#ifdef HAVE_LSTAT
2752 /* Don't accept a symbolic link, its a security risk. */
2753 && mch_lstat((char *)name, &sb) < 0
2754#endif
2755 )
2756 break;
2757 vim_free(name);
2758 }
2759 return name;
2760}
2761
2762/*
2763 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002764 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
2766 void
2767ex_cc(eap)
2768 exarg_T *eap;
2769{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002770 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002771
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002772 if (eap->cmdidx == CMD_ll
2773 || eap->cmdidx == CMD_lrewind
2774 || eap->cmdidx == CMD_lfirst
2775 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002776 {
2777 qi = GET_LOC_LIST(curwin);
2778 if (qi == NULL)
2779 {
2780 EMSG(_(e_loclist));
2781 return;
2782 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783 }
2784
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002785 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 eap->addr_count > 0
2787 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002788 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002790 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2791 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 ? 1
2793 : 32767,
2794 eap->forceit);
2795}
2796
2797/*
2798 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002799 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 */
2801 void
2802ex_cnext(eap)
2803 exarg_T *eap;
2804{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002805 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002806
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002807 if (eap->cmdidx == CMD_lnext
2808 || eap->cmdidx == CMD_lNext
2809 || eap->cmdidx == CMD_lprevious
2810 || eap->cmdidx == CMD_lnfile
2811 || eap->cmdidx == CMD_lNfile
2812 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002813 {
2814 qi = GET_LOC_LIST(curwin);
2815 if (qi == NULL)
2816 {
2817 EMSG(_(e_loclist));
2818 return;
2819 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002820 }
2821
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002822 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002824 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002826 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2827 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 ? BACKWARD_FILE
2829 : BACKWARD,
2830 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2831}
2832
2833/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002834 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002835 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 */
2837 void
2838ex_cfile(eap)
2839 exarg_T *eap;
2840{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002841 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002842 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002843
2844 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2845 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002846 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002849 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002850
2851 /*
2852 * This function is used by the :cfile, :cgetfile and :caddfile
2853 * commands.
2854 * :cfile always creates a new quickfix list and jumps to the
2855 * first error.
2856 * :cgetfile creates a new quickfix list but doesn't jump to the
2857 * first error.
2858 * :caddfile adds to an existing quickfix list. If there is no
2859 * quickfix list then a new list is created.
2860 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002861 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2862 && eap->cmdidx != CMD_laddfile)) > 0
2863 && (eap->cmdidx == CMD_cfile
2864 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002865 {
2866 if (wp != NULL)
2867 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002868 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870}
2871
2872/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002873 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00002874 * ":vimgrepadd {pattern} file(s)"
2875 * ":lvimgrep {pattern} file(s)"
2876 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00002877 */
2878 void
2879ex_vimgrep(eap)
2880 exarg_T *eap;
2881{
Bram Moolenaar81695252004-12-29 20:58:21 +00002882 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002883 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002884 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002885 char_u *s;
2886 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002887 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00002888 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002889 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002890 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002891 buf_T *buf;
2892 int duplicate_name = FALSE;
2893 int using_dummy;
2894 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002895 buf_T *first_match_buf = NULL;
2896 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002897 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002898#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2899 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002900#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002901#ifndef FEAT_AUTOCMD
2902 buf_T *save_curbuf;
2903#else
2904 aco_save_T aco;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002905 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002906 int flags = 0;
2907 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002908 long tomatch;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002909
2910 switch (eap->cmdidx)
2911 {
2912 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002913 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002914 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002915 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002916 default: break;
2917 }
2918 if (au_name != NULL)
2919 {
2920 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2921 curbuf->b_fname, TRUE, curbuf);
2922 if (did_throw || force_abort)
2923 return;
2924 }
2925#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002926
Bram Moolenaar754b5602006-02-09 23:53:20 +00002927 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002928 || eap->cmdidx == CMD_lvimgrep
2929 || eap->cmdidx == CMD_lgrepadd
2930 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002931 {
2932 qi = ll_get_or_alloc_list(curwin);
2933 if (qi == NULL)
2934 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00002935 }
2936
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002937 if (eap->addr_count > 0)
2938 tomatch = eap->line2;
2939 else
2940 tomatch = MAXLNUM;
2941
Bram Moolenaar81695252004-12-29 20:58:21 +00002942 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002943 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002944 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002945 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002946 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00002947 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00002948 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002949 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002950 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002951 if (regmatch.regprog == NULL)
2952 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002953 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00002954 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002955
2956 p = skipwhite(p);
2957 if (*p == NUL)
2958 {
2959 EMSG(_("E683: File name missing or invalid pattern"));
2960 goto theend;
2961 }
2962
Bram Moolenaar754b5602006-02-09 23:53:20 +00002963 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00002964 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002965 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002966 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002967 qf_new_list(qi);
2968 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002969 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002970 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002971 prevp->qf_next != prevp; prevp = prevp->qf_next)
2972 ;
2973
2974 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002975 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002976 goto theend;
2977 if (fcount == 0)
2978 {
2979 EMSG(_(e_nomatch));
2980 goto theend;
2981 }
2982
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002983 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002984 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002985 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002986 if (time(NULL) > seconds)
2987 {
2988 /* Display the file name every second or so. */
2989 seconds = time(NULL);
2990 msg_start();
Bram Moolenaara5373fa2005-09-09 19:47:12 +00002991 p = msg_strtrunc(fnames[fi], TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002992 if (p == NULL)
2993 msg_outtrans(fnames[fi]);
2994 else
2995 {
2996 msg_outtrans(p);
2997 vim_free(p);
2998 }
2999 msg_clr_eos();
3000 msg_didout = FALSE; /* overwrite this message */
3001 msg_nowait = TRUE; /* don't wait for this message */
3002 msg_col = 0;
3003 out_flush();
3004 }
3005
Bram Moolenaar81695252004-12-29 20:58:21 +00003006 buf = buflist_findname_exp(fnames[fi]);
3007 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3008 {
3009 /* Remember that a buffer with this name already exists. */
3010 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003011 using_dummy = TRUE;
3012
3013#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3014 /* Don't do Filetype autocommands to avoid loading syntax and
3015 * indent scripts, a great speed improvement. */
3016 save_ei = au_event_disable(",Filetype");
3017#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003018 /* Don't use modelines here, it's useless. */
3019 save_mls = p_mls;
3020 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003021
3022 /* Load file into a buffer, so that 'fileencoding' is detected,
3023 * autocommands applied, etc. */
3024 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003025
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003026 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003027#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3028 au_event_restore(save_ei);
3029#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003030 }
3031 else
3032 /* Use existing, loaded buffer. */
3033 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003034
Bram Moolenaar81695252004-12-29 20:58:21 +00003035 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003036 {
3037 if (!got_int)
3038 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
3039 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003040 else
3041 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003042 /* Try for a match in all lines of the buffer.
3043 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003044 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003045 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3046 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003047 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003048 col = 0;
3049 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3050 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003051 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003052 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003053 NULL, /* dir */
3054 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00003055 ml_get_buf(buf,
3056 regmatch.startpos[0].lnum + lnum, FALSE),
3057 regmatch.startpos[0].lnum + lnum,
3058 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003059 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003060 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003061 0, /* nr */
3062 0, /* type */
3063 TRUE /* valid */
3064 ) == FAIL)
3065 {
3066 got_int = TRUE;
3067 break;
3068 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003069 found_match = TRUE;
3070 if (--tomatch == 0)
3071 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003072 if ((flags & VGR_GLOBAL) == 0
3073 || regmatch.endpos[0].lnum > 0)
3074 break;
3075 col = regmatch.endpos[0].col
3076 + (col == regmatch.endpos[0].col);
3077 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3078 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003079 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003080 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003081 if (got_int)
3082 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003083 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003084
3085 if (using_dummy)
3086 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003087 if (found_match && first_match_buf == NULL)
3088 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003089 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003090 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003091 /* Never keep a dummy buffer if there is another buffer
3092 * with the same name. */
3093 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003094 buf = NULL;
3095 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003096 else if (!cmdmod.hide
3097 || buf->b_p_bh[0] == 'u' /* "unload" */
3098 || buf->b_p_bh[0] == 'w' /* "wipe" */
3099 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003100 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003101 /* When no match was found we don't need to remember the
3102 * buffer, wipe it out. If there was a match and it
3103 * wasn't the first one or we won't jump there: only
3104 * unload the buffer.
3105 * Ignore 'hidden' here, because it may lead to having too
3106 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003107 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003108 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003109 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003110 buf = NULL;
3111 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003112 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003113 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003114 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003115 buf = NULL;
3116 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003117 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003118
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003119 if (buf != NULL)
3120 {
3121 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003122 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003123 * need to be done (again). But not the window-local
3124 * options! */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003125#if defined(FEAT_AUTOCMD)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003126 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003127#else
3128 save_curbuf = curbuf;
3129 curbuf = buf;
3130 curwin->w_buffer = curbuf;
3131#endif
3132#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003133 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3134 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003135#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003136 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003137#if defined(FEAT_AUTOCMD)
3138 aucmd_restbuf(&aco);
3139#else
3140 curbuf = save_curbuf;
3141 curwin->w_buffer = curbuf;
3142#endif
3143 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003144 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003145 }
3146 }
3147
3148 FreeWild(fcount, fnames);
3149
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003150 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3151 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3152 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003153
3154#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003155 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003156#endif
3157
3158 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003159 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003160 {
3161 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003162 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003163 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003164 else
3165 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003166
Bram Moolenaar7c626922005-02-07 22:01:03 +00003167#ifdef FEAT_AUTOCMD
3168 if (au_name != NULL)
3169 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3170 curbuf->b_fname, TRUE, curbuf);
3171#endif
3172
Bram Moolenaar86b68352004-12-27 21:59:20 +00003173theend:
3174 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003175}
3176
3177/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003178 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003179 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003180 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3181 * If "s" is not NULL terminate the pattern with a NUL.
3182 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003183 */
3184 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003185skip_vimgrep_pat(p, s, flags)
3186 char_u *p;
3187 char_u **s;
3188 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003189{
3190 int c;
3191
3192 if (vim_isIDc(*p))
3193 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003194 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003195 if (s != NULL)
3196 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003197 p = skiptowhite(p);
3198 if (s != NULL && *p != NUL)
3199 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003200 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003201 else
3202 {
3203 /* ":vimgrep /pattern/[g][j] fname" */
3204 if (s != NULL)
3205 *s = p + 1;
3206 c = *p;
3207 p = skip_regexp(p + 1, c, TRUE, NULL);
3208 if (*p != c)
3209 return NULL;
3210
3211 /* Truncate the pattern. */
3212 if (s != NULL)
3213 *p = NUL;
3214 ++p;
3215
3216 /* Find the flags */
3217 while (*p == 'g' || *p == 'j')
3218 {
3219 if (flags != NULL)
3220 {
3221 if (*p == 'g')
3222 *flags |= VGR_GLOBAL;
3223 else
3224 *flags |= VGR_NOJUMP;
3225 }
3226 ++p;
3227 }
3228 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003229 return p;
3230}
3231
3232/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003233 * Load file "fname" into a dummy buffer and return the buffer pointer.
3234 * Returns NULL if it fails.
3235 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3236 */
3237 static buf_T *
3238load_dummy_buffer(fname)
3239 char_u *fname;
3240{
3241 buf_T *newbuf;
3242 int failed = TRUE;
3243#ifdef FEAT_AUTOCMD
3244 aco_save_T aco;
3245#else
3246 buf_T *old_curbuf = curbuf;
3247#endif
3248
3249 /* Allocate a buffer without putting it in the buffer list. */
3250 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3251 if (newbuf == NULL)
3252 return NULL;
3253
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003254 /* Init the options. */
3255 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3256
Bram Moolenaar81695252004-12-29 20:58:21 +00003257#ifdef FEAT_AUTOCMD
3258 /* set curwin/curbuf to buf and save a few things */
3259 aucmd_prepbuf(&aco, newbuf);
3260#else
3261 curbuf = newbuf;
3262 curwin->w_buffer = newbuf;
3263#endif
3264
3265 /* Need to set the filename for autocommands. */
3266 (void)setfname(curbuf, fname, NULL, FALSE);
3267
Bram Moolenaar4770d092006-01-12 23:22:24 +00003268 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003269 {
3270 /* Create swap file now to avoid the ATTENTION message. */
3271 check_need_swap(TRUE);
3272
3273 /* Remove the "dummy" flag, otherwise autocommands may not
3274 * work. */
3275 curbuf->b_flags &= ~BF_DUMMY;
3276
3277 if (readfile(fname, NULL,
3278 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3279 NULL, READ_NEW | READ_DUMMY) == OK
3280 && !(curbuf->b_flags & BF_NEW))
3281 {
3282 failed = FALSE;
3283 if (curbuf != newbuf)
3284 {
3285 /* Bloody autocommands changed the buffer! */
3286 if (buf_valid(newbuf))
3287 wipe_buffer(newbuf, FALSE);
3288 newbuf = curbuf;
3289 }
3290 }
3291 }
3292
3293#ifdef FEAT_AUTOCMD
3294 /* restore curwin/curbuf and a few other things */
3295 aucmd_restbuf(&aco);
3296#else
3297 curbuf = old_curbuf;
3298 curwin->w_buffer = old_curbuf;
3299#endif
3300
3301 if (!buf_valid(newbuf))
3302 return NULL;
3303 if (failed)
3304 {
3305 wipe_dummy_buffer(newbuf);
3306 return NULL;
3307 }
3308 return newbuf;
3309}
3310
3311/*
3312 * Wipe out the dummy buffer that load_dummy_buffer() created.
3313 */
3314 static void
3315wipe_dummy_buffer(buf)
3316 buf_T *buf;
3317{
3318 if (curbuf != buf) /* safety check */
3319 wipe_buffer(buf, FALSE);
3320}
3321
3322/*
3323 * Unload the dummy buffer that load_dummy_buffer() created.
3324 */
3325 static void
3326unload_dummy_buffer(buf)
3327 buf_T *buf;
3328{
3329 if (curbuf != buf) /* safety check */
3330 close_buffer(NULL, buf, DOBUF_UNLOAD);
3331}
3332
Bram Moolenaar05159a02005-02-26 23:04:13 +00003333#if defined(FEAT_EVAL) || defined(PROTO)
3334/*
3335 * Add each quickfix error to list "list" as a dictionary.
3336 */
3337 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003338get_errorlist(wp, list)
3339 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003340 list_T *list;
3341{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003342 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003343 dict_T *dict;
3344 char_u buf[2];
3345 qfline_T *qfp;
3346 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003347
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003348 if (wp != NULL)
3349 {
3350 qi = GET_LOC_LIST(wp);
3351 if (qi == NULL)
3352 return FAIL;
3353 }
3354
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003355 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003356 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003357 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003358
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003359 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3360 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003361 {
3362 if ((dict = dict_alloc()) == NULL)
3363 return FAIL;
3364 if (list_append_dict(list, dict) == FAIL)
3365 return FAIL;
3366
3367 buf[0] = qfp->qf_type;
3368 buf[1] = NUL;
3369 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
3370 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3371 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3372 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3373 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003374 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003375 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
3376 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3377 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3378 return FAIL;
3379
3380 qfp = qfp->qf_next;
3381 }
3382 return OK;
3383}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003384
3385/*
3386 * Populate the quickfix list with the items supplied in the list
3387 * of dictionaries.
3388 */
3389 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003390set_errorlist(wp, list, action)
3391 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003392 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003393 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003394{
3395 listitem_T *li;
3396 dict_T *d;
3397 char_u *filename, *pattern, *text, *type;
3398 long lnum;
3399 int col, nr;
3400 int vcol;
3401 qfline_T *prevp = NULL;
3402 int valid, status;
3403 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003404 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003405
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003406 if (wp != NULL)
3407 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003408 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003409 if (qi == NULL)
3410 return FAIL;
3411 }
3412
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003413 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003414 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003415 qf_new_list(qi);
3416 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003417 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003418 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003419 prevp->qf_next != prevp; prevp = prevp->qf_next)
3420 ;
3421 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003422 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003423
3424 for (li = list->lv_first; li != NULL; li = li->li_next)
3425 {
3426 if (li->li_tv.v_type != VAR_DICT)
3427 continue; /* Skip non-dict items */
3428
3429 d = li->li_tv.vval.v_dict;
3430 if (d == NULL)
3431 continue;
3432
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003433 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003434 lnum = get_dict_number(d, (char_u *)"lnum");
3435 col = get_dict_number(d, (char_u *)"col");
3436 vcol = get_dict_number(d, (char_u *)"vcol");
3437 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003438 type = get_dict_string(d, (char_u *)"type", TRUE);
3439 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3440 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003441 if (text == NULL)
3442 text = vim_strsave((char_u *)"");
3443
3444 valid = TRUE;
3445 if (filename == NULL || (lnum == 0 && pattern == NULL))
3446 valid = FALSE;
3447
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003448 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003449 NULL, /* dir */
3450 filename,
3451 text,
3452 lnum,
3453 col,
3454 vcol, /* vis_col */
3455 pattern, /* search pattern */
3456 nr,
3457 type == NULL ? NUL : *type,
3458 valid);
3459
3460 vim_free(filename);
3461 vim_free(pattern);
3462 vim_free(text);
3463 vim_free(type);
3464
3465 if (status == FAIL)
3466 {
3467 retval = FAIL;
3468 break;
3469 }
3470 }
3471
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003472 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3473 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3474 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003475
3476#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003477 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003478#endif
3479
3480 return retval;
3481}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003482#endif
3483
Bram Moolenaar81695252004-12-29 20:58:21 +00003484/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003485 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003486 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003487 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003488 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003489 */
3490 void
3491ex_cbuffer(eap)
3492 exarg_T *eap;
3493{
3494 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003495 qf_info_T *qi = &ql_info;
3496
Bram Moolenaara6557602006-02-04 22:43:20 +00003497 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003498 {
3499 qi = ll_get_or_alloc_list(curwin);
3500 if (qi == NULL)
3501 return;
3502 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003503
3504 if (*eap->arg == NUL)
3505 buf = curbuf;
3506 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3507 buf = buflist_findnr(atoi((char *)eap->arg));
3508 if (buf == NULL)
3509 EMSG(_(e_invarg));
3510 else if (buf->b_ml.ml_mfp == NULL)
3511 EMSG(_("E681: Buffer is not loaded"));
3512 else
3513 {
3514 if (eap->addr_count == 0)
3515 {
3516 eap->line1 = 1;
3517 eap->line2 = buf->b_ml.ml_line_count;
3518 }
3519 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3520 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3521 EMSG(_(e_invrange));
3522 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003523 {
3524 int buffer_cmd = (eap->cmdidx == CMD_cbuffer
3525 || eap->cmdidx == CMD_lbuffer);
3526
3527 if (qf_init_ext(qi, NULL, buf, NULL, p_efm, buffer_cmd,
3528 eap->line1, eap->line2) > 0
3529 && buffer_cmd)
3530 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3531 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003532 }
3533}
3534
Bram Moolenaar1e015462005-09-25 22:16:38 +00003535#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003536/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003537 * ":cexpr {expr}" and ":caddexpr {expr}" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003538 * ":lexpr {expr}" and ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003539 */
3540 void
3541ex_cexpr(eap)
3542 exarg_T *eap;
3543{
3544 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003545 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003546
3547 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_laddexpr)
3548 {
3549 qi = ll_get_or_alloc_list(curwin);
3550 if (qi == NULL)
3551 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003552 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003553
Bram Moolenaar4770d092006-01-12 23:22:24 +00003554 /* Evaluate the expression. When the result is a string or a list we can
3555 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003556 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003557 if (tv != NULL)
3558 {
3559 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3560 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3561 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003562 int expr_cmd = (eap->cmdidx == CMD_cexpr
3563 || eap->cmdidx == CMD_lexpr);
3564 if (qf_init_ext(qi, NULL, NULL, tv, p_efm, expr_cmd,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003565 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003566 && expr_cmd)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003567 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003568 }
3569 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003570 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003571 free_tv(tv);
3572 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003573}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003574#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003575
3576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 * ":helpgrep {pattern}"
3578 */
3579 void
3580ex_helpgrep(eap)
3581 exarg_T *eap;
3582{
3583 regmatch_T regmatch;
3584 char_u *save_cpo;
3585 char_u *p;
3586 int fcount;
3587 char_u **fnames;
3588 FILE *fd;
3589 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003590 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003592#ifdef FEAT_MULTI_LANG
3593 char_u *lang;
3594#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003595 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003596 int new_qi = FALSE;
3597 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
3599 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3600 save_cpo = p_cpo;
3601 p_cpo = (char_u *)"";
3602
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003603#ifdef FEAT_MULTI_LANG
3604 /* Check for a specified language */
3605 lang = check_help_lang(eap->arg);
3606#endif
3607
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003608 if (eap->cmdidx == CMD_lhelpgrep)
3609 {
3610 /* Find an existing help window */
3611 FOR_ALL_WINDOWS(wp)
3612 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3613 break;
3614
3615 if (wp == NULL) /* Help window not found */
3616 qi = NULL;
3617 else
3618 qi = wp->w_llist;
3619
3620 if (qi == NULL)
3621 {
3622 /* Allocate a new location list for help text matches */
3623 if ((qi = ll_new_list()) == NULL)
3624 return;
3625 new_qi = TRUE;
3626 }
3627 }
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3630 regmatch.rm_ic = FALSE;
3631 if (regmatch.regprog != NULL)
3632 {
3633 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003634 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 /* Go through all directories in 'runtimepath' */
3637 p = p_rtp;
3638 while (*p != NUL && !got_int)
3639 {
3640 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3641
3642 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3643 add_pathsep(NameBuff);
3644 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3645 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3646 &fnames, EW_FILE|EW_SILENT) == OK
3647 && fcount > 0)
3648 {
3649 for (fi = 0; fi < fcount && !got_int; ++fi)
3650 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003651#ifdef FEAT_MULTI_LANG
3652 /* Skip files for a different language. */
3653 if (lang != NULL
3654 && STRNICMP(lang, fnames[fi]
3655 + STRLEN(fnames[fi]) - 3, 2) != 0
3656 && !(STRNICMP(lang, "en", 2) == 0
3657 && STRNICMP("txt", fnames[fi]
3658 + STRLEN(fnames[fi]) - 3, 3) == 0))
3659 continue;
3660#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003661 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (fd != NULL)
3663 {
3664 lnum = 1;
3665 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3666 {
3667 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3668 {
3669 int l = STRLEN(IObuff);
3670
3671 /* remove trailing CR, LF, spaces, etc. */
3672 while (l > 0 && IObuff[l - 1] <= ' ')
3673 IObuff[--l] = NUL;
3674
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003675 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 NULL, /* dir */
3677 fnames[fi],
3678 IObuff,
3679 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003680 (int)(regmatch.startp[0] - IObuff)
3681 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003682 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003683 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 0, /* nr */
3685 1, /* type */
3686 TRUE /* valid */
3687 ) == FAIL)
3688 {
3689 got_int = TRUE;
3690 break;
3691 }
3692 }
3693 ++lnum;
3694 line_breakcheck();
3695 }
3696 fclose(fd);
3697 }
3698 }
3699 FreeWild(fcount, fnames);
3700 }
3701 }
3702 vim_free(regmatch.regprog);
3703
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003704 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3705 qi->qf_lists[qi->qf_curlist].qf_ptr =
3706 qi->qf_lists[qi->qf_curlist].qf_start;
3707 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709
3710 p_cpo = save_cpo;
3711
3712#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003713 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714#endif
3715
3716 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003717 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003718 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003719 else
3720 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003721
3722 if (eap->cmdidx == CMD_lhelpgrep)
3723 {
3724 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003725 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003726 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3727 {
3728 if (new_qi)
3729 ll_free_all(&qi);
3730 }
3731 else if (curwin->w_llist == NULL)
3732 curwin->w_llist = qi;
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734}
3735
3736#endif /* FEAT_QUICKFIX */