blob: ca6cc4080172154290bacfa7d497b4c6b2a82561 [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 Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000129/* Quickfix window check helper macro */
130#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
131/* Location list window check helper macro */
132#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
133/*
134 * Return location list for window 'wp'
135 * For location list window, return the referenced location list
136 */
137#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000140 * Read the errorfile "efile" into memory, line by line, building the error
141 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 * Return -1 for error, number of errors for success.
143 */
144 int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000145qf_init(wp, efile, errorformat, newlist)
146 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 char_u *efile;
148 char_u *errorformat;
149 int newlist; /* TRUE: start a new error list */
150{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000151 qf_info_T *qi = &ql_info;
152
Bram Moolenaar86b68352004-12-27 21:59:20 +0000153 if (efile == NULL)
154 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000155
156 if (wp != NULL)
157 qi = GET_LOC_LIST(wp);
158
159 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +0000160 (linenr_T)0, (linenr_T)0);
161}
162
163/*
164 * Read the errorfile "efile" into memory, line by line, building the error
165 * list.
166 * Alternative: when "efile" is null read errors from buffer "buf".
167 * Always use 'errorformat' from "buf" if there is a local value.
168 * Then lnumfirst and lnumlast specify the range of lines to use.
169 * Return -1 for error, number of errors for success.
170 */
171 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
173 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000174 char_u *efile;
175 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000176 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000177 char_u *errorformat;
178 int newlist; /* TRUE: start a new error list */
179 linenr_T lnumfirst; /* first line number to use */
180 linenr_T lnumlast; /* last line number to use */
181{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 char_u *namebuf;
183 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000184 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 char_u *fmtstr = NULL;
186 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 int type = 0;
189 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000190 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 long lnum = 0L;
192 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000193 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000194 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 char_u *efmp;
196 struct eformat *fmt_first = NULL;
197 struct eformat *fmt_last = NULL;
198 struct eformat *fmt_ptr;
199 char_u *efm;
200 char_u *ptr;
201 char_u *srcptr;
202 int len;
203 int i;
204 int round;
205 int idx = 0;
206 int multiline = FALSE;
207 int multiignore = FALSE;
208 int multiscan = FALSE;
209 int retval = -1; /* default: return error flag */
210 char_u *directory = NULL;
211 char_u *currfile = NULL;
212 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000213 char_u *p_str = NULL;
214 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 struct dir_stack_T *file_stack = NULL;
216 regmatch_T regmatch;
217 static struct fmtpattern
218 {
219 char_u convchar;
220 char *pattern;
221 } fmt_pat[FMT_PATTERNS] =
222 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000223 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 {'n', "\\d\\+"},
225 {'l', "\\d\\+"},
226 {'c', "\\d\\+"},
227 {'t', "."},
228 {'m', ".\\+"},
229 {'r', ".*"},
230 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000231 {'v', "\\d\\+"},
232 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 };
234
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 namebuf = alloc(CMDBUFFSIZE + 1);
236 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000237 pattern = alloc(CMDBUFFSIZE + 1);
238 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 goto qf_init_end;
240
Bram Moolenaar86b68352004-12-27 21:59:20 +0000241 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {
243 EMSG2(_(e_openerrf), efile);
244 goto qf_init_end;
245 }
246
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000247 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000249 qf_new_list(qi);
250 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000252 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
254 ;
255
256/*
257 * Each part of the format string is copied and modified from errorformat to
258 * regex prog. Only a few % characters are allowed.
259 */
260 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000261 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000262 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 else
264 efm = errorformat;
265 /*
266 * Get some space to modify the format string into.
267 */
268 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
269 for (round = FMT_PATTERNS; round > 0; )
270 i += (int)STRLEN(fmt_pat[--round].pattern);
271#ifdef COLON_IN_FILENAME
272 i += 12; /* "%f" can become twelve chars longer */
273#else
274 i += 2; /* "%f" can become two chars longer */
275#endif
276 if ((fmtstr = alloc(i)) == NULL)
277 goto error2;
278
279 while (efm[0])
280 {
281 /*
282 * Allocate a new eformat structure and put it at the end of the list
283 */
284 fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
285 if (fmt_ptr == NULL)
286 goto error2;
287 if (fmt_first == NULL) /* first one */
288 fmt_first = fmt_ptr;
289 else
290 fmt_last->next = fmt_ptr;
291 fmt_last = fmt_ptr;
292 fmt_ptr->prefix = NUL;
293 fmt_ptr->flags = NUL;
294 fmt_ptr->next = NULL;
295 fmt_ptr->prog = NULL;
296 for (round = FMT_PATTERNS; round > 0; )
297 fmt_ptr->addr[--round] = NUL;
298 /* round is 0 now */
299
300 /*
301 * Isolate one part in the 'errorformat' option
302 */
303 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
304 if (efm[len] == '\\' && efm[len + 1] != NUL)
305 ++len;
306
307 /*
308 * Build regexp pattern from current 'errorformat' option
309 */
310 ptr = fmtstr;
311 *ptr++ = '^';
312 for (efmp = efm; efmp < efm + len; ++efmp)
313 {
314 if (*efmp == '%')
315 {
316 ++efmp;
317 for (idx = 0; idx < FMT_PATTERNS; ++idx)
318 if (fmt_pat[idx].convchar == *efmp)
319 break;
320 if (idx < FMT_PATTERNS)
321 {
322 if (fmt_ptr->addr[idx])
323 {
324 sprintf((char *)errmsg,
325 _("E372: Too many %%%c in format string"), *efmp);
326 EMSG(errmsg);
327 goto error2;
328 }
329 if ((idx
330 && idx < 6
331 && vim_strchr((char_u *)"DXOPQ",
332 fmt_ptr->prefix) != NULL)
333 || (idx == 6
334 && vim_strchr((char_u *)"OPQ",
335 fmt_ptr->prefix) == NULL))
336 {
337 sprintf((char *)errmsg,
338 _("E373: Unexpected %%%c in format string"), *efmp);
339 EMSG(errmsg);
340 goto error2;
341 }
342 fmt_ptr->addr[idx] = (char_u)++round;
343 *ptr++ = '\\';
344 *ptr++ = '(';
345#ifdef BACKSLASH_IN_FILENAME
346 if (*efmp == 'f')
347 {
348 /* Also match "c:" in the file name, even when
349 * checking for a colon next: "%f:".
350 * "\%(\a:\)\=" */
351 STRCPY(ptr, "\\%(\\a:\\)\\=");
352 ptr += 10;
353 }
354#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000355 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000357 if (efmp[1] != '\\' && efmp[1] != '%')
358 {
359 /* A file name may contain spaces, but this isn't
360 * in "\f". For "%f:%l:%m" there may be a ":" in
361 * the file name. Use ".\{-1,}x" instead (x is
362 * the next character), the requirement that :999:
363 * follows should work. */
364 STRCPY(ptr, ".\\{-1,}");
365 ptr += 7;
366 }
367 else
368 {
369 /* File name followed by '\\' or '%': include as
370 * many file name chars as possible. */
371 STRCPY(ptr, "\\f\\+");
372 ptr += 4;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
375 else
376 {
377 srcptr = (char_u *)fmt_pat[idx].pattern;
378 while ((*ptr = *srcptr++) != NUL)
379 ++ptr;
380 }
381 *ptr++ = '\\';
382 *ptr++ = ')';
383 }
384 else if (*efmp == '*')
385 {
386 if (*++efmp == '[' || *efmp == '\\')
387 {
388 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
389 {
390 if (efmp[1] == '^')
391 *ptr++ = *++efmp;
392 if (efmp < efm + len)
393 {
394 *ptr++ = *++efmp; /* could be ']' */
395 while (efmp < efm + len
396 && (*ptr++ = *++efmp) != ']')
397 /* skip */;
398 if (efmp == efm + len)
399 {
400 EMSG(_("E374: Missing ] in format string"));
401 goto error2;
402 }
403 }
404 }
405 else if (efmp < efm + len) /* %*\D, %*\s etc. */
406 *ptr++ = *++efmp;
407 *ptr++ = '\\';
408 *ptr++ = '+';
409 }
410 else
411 {
412 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
413 sprintf((char *)errmsg,
414 _("E375: Unsupported %%%c in format string"), *efmp);
415 EMSG(errmsg);
416 goto error2;
417 }
418 }
419 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
420 *ptr++ = *efmp; /* regexp magic characters */
421 else if (*efmp == '#')
422 *ptr++ = '*';
423 else if (efmp == efm + 1) /* analyse prefix */
424 {
425 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
426 fmt_ptr->flags = *efmp++;
427 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
428 fmt_ptr->prefix = *efmp;
429 else
430 {
431 sprintf((char *)errmsg,
432 _("E376: Invalid %%%c in format string prefix"), *efmp);
433 EMSG(errmsg);
434 goto error2;
435 }
436 }
437 else
438 {
439 sprintf((char *)errmsg,
440 _("E377: Invalid %%%c in format string"), *efmp);
441 EMSG(errmsg);
442 goto error2;
443 }
444 }
445 else /* copy normal character */
446 {
447 if (*efmp == '\\' && efmp + 1 < efm + len)
448 ++efmp;
449 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
450 *ptr++ = '\\'; /* escape regexp atoms */
451 if (*efmp)
452 *ptr++ = *efmp;
453 }
454 }
455 *ptr++ = '$';
456 *ptr = NUL;
457 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
458 goto error2;
459 /*
460 * Advance to next part
461 */
462 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
463 }
464 if (fmt_first == NULL) /* nothing found */
465 {
466 EMSG(_("E378: 'errorformat' contains no pattern"));
467 goto error2;
468 }
469
470 /*
471 * got_int is reset here, because it was probably set when killing the
472 * ":make" command, but we still want to read the errorfile then.
473 */
474 got_int = FALSE;
475
476 /* Always ignore case when looking for a matching error. */
477 regmatch.rm_ic = TRUE;
478
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000479 if (tv != NULL)
480 {
481 if (tv->v_type == VAR_STRING)
482 p_str = tv->vval.v_string;
483 else if (tv->v_type == VAR_LIST)
484 p_li = tv->vval.v_list->lv_first;
485 }
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 /*
488 * Read the lines in the error file one by one.
489 * Try to recognize one of the error formats in each line.
490 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000491 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000493 /* Get the next line. */
494 if (fd == NULL)
495 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000496 if (tv != NULL)
497 {
498 int len;
499
500 if (tv->v_type == VAR_STRING)
501 {
502 /* Get the next line from the supplied string */
503 char_u *p;
504
505 if (!*p_str) /* Reached the end of the string */
506 break;
507
508 p = vim_strchr(p_str, '\n');
509 if (p)
510 len = p - p_str + 1;
511 else
512 len = STRLEN(p_str);
513
514 if (len > CMDBUFFSIZE - 2)
515 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
516 else
517 vim_strncpy(IObuff, p_str, len);
518
519 p_str += len;
520 }
521 else if (tv->v_type == VAR_LIST)
522 {
523 /* Get the next line from the supplied list */
524 while (p_li && p_li->li_tv.v_type != VAR_STRING)
525 p_li = p_li->li_next; /* Skip non-string items */
526
527 if (!p_li) /* End of the list */
528 break;
529
530 len = STRLEN(p_li->li_tv.vval.v_string);
531 if (len > CMDBUFFSIZE - 2)
532 len = CMDBUFFSIZE - 2;
533
534 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
535
536 p_li = p_li->li_next; /* next item */
537 }
538 }
539 else
540 {
541 /* Get the next line from the supplied buffer */
542 if (buflnum > lnumlast)
543 break;
544 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
545 CMDBUFFSIZE - 2);
546 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000547 }
548 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
549 break;
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
552 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
553 *efmp = NUL;
554#ifdef USE_CRNL
555 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
556 *efmp = NUL;
557#endif
558
559 /*
560 * Try to match each part of 'errorformat' until we find a complete
561 * match or no match.
562 */
563 valid = TRUE;
564restofline:
565 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
566 {
567 idx = fmt_ptr->prefix;
568 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
569 continue;
570 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000571 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (!multiscan)
573 errmsg[0] = NUL;
574 lnum = 0;
575 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000576 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 enr = -1;
578 type = 0;
579 tail = NULL;
580
581 regmatch.regprog = fmt_ptr->prog;
582 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
583 {
584 if ((idx == 'C' || idx == 'Z') && !multiline)
585 continue;
586 if (vim_strchr((char_u *)"EWI", idx) != NULL)
587 type = idx;
588 else
589 type = 0;
590 /*
591 * Extract error message data from matched line
592 */
593 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
594 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000595 int c = *regmatch.endp[i];
596
597 /* Expand ~/file and $HOME/file to full path. */
598 *regmatch.endp[i] = NUL;
599 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
600 *regmatch.endp[i] = c;
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000603 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 continue;
605 }
606 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
607 enr = (int)atol((char *)regmatch.startp[i]);
608 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
609 lnum = atol((char *)regmatch.startp[i]);
610 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
611 col = (int)atol((char *)regmatch.startp[i]);
612 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
613 type = *regmatch.startp[i];
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 STRCPY(errmsg, IObuff);
616 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
617 {
618 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000619 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 }
621 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
622 tail = regmatch.startp[i];
623 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
624 {
625 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
626 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000627 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
630 {
631 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000632 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000634 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
635 {
636 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
637 if (len > CMDBUFFSIZE - 5)
638 len = CMDBUFFSIZE - 5;
639 STRCPY(pattern, "^\\V");
640 STRNCAT(pattern, regmatch.startp[i], len);
641 pattern[len + 3] = '\\';
642 pattern[len + 4] = '$';
643 pattern[len + 5] = NUL;
644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 break;
646 }
647 }
648 multiscan = FALSE;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000649 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000651 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653 if (idx == 'D') /* enter directory */
654 {
655 if (*namebuf == NUL)
656 {
657 EMSG(_("E379: Missing or empty directory name"));
658 goto error2;
659 }
660 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
661 goto error2;
662 }
663 else if (idx == 'X') /* leave directory */
664 directory = qf_pop_dir(&dir_stack);
665 }
666 namebuf[0] = NUL; /* no match found, remove file name */
667 lnum = 0; /* don't jump to this line */
668 valid = FALSE;
669 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000670 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 multiline = multiignore = FALSE;
672 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000673 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
675 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
676 multiline = TRUE; /* start of a multi-line message */
677 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
678 { /* continuation of multi-line msg */
679 if (qfprev == NULL)
680 goto error2;
681 if (*errmsg && !multiignore)
682 {
683 len = (int)STRLEN(qfprev->qf_text);
684 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
685 == NULL)
686 goto error2;
687 STRCPY(ptr, qfprev->qf_text);
688 vim_free(qfprev->qf_text);
689 qfprev->qf_text = ptr;
690 *(ptr += len) = '\n';
691 STRCPY(++ptr, errmsg);
692 }
693 if (qfprev->qf_nr == -1)
694 qfprev->qf_nr = enr;
695 if (vim_isprintc(type) && !qfprev->qf_type)
696 qfprev->qf_type = type; /* only printable chars allowed */
697 if (!qfprev->qf_lnum)
698 qfprev->qf_lnum = lnum;
699 if (!qfprev->qf_col)
700 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (!qfprev->qf_fnum)
703 qfprev->qf_fnum = qf_get_fnum(directory,
704 *namebuf || directory ? namebuf
705 : currfile && valid ? currfile : 0);
706 if (idx == 'Z')
707 multiline = multiignore = FALSE;
708 line_breakcheck();
709 continue;
710 }
711 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
712 {
713 /* global file names */
714 valid = FALSE;
715 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
716 {
717 if (*namebuf && idx == 'P')
718 currfile = qf_push_dir(namebuf, &file_stack);
719 else if (idx == 'Q')
720 currfile = qf_pop_dir(&file_stack);
721 *namebuf = NUL;
722 if (tail && *tail)
723 {
724 STRCPY(IObuff, skipwhite(tail));
725 multiscan = TRUE;
726 goto restofline;
727 }
728 }
729 }
730 if (fmt_ptr->flags == '-') /* generally exclude this line */
731 {
732 if (multiline)
733 multiignore = TRUE; /* also exclude continuation lines */
734 continue;
735 }
736 }
737
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000738 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000740 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000742 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 errmsg,
744 lnum,
745 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000746 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000747 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 enr,
749 type,
750 valid) == FAIL)
751 goto error2;
752 line_breakcheck();
753 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000754 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000756 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000758 /* no valid entry found */
759 qi->qf_lists[qi->qf_curlist].qf_ptr =
760 qi->qf_lists[qi->qf_curlist].qf_start;
761 qi->qf_lists[qi->qf_curlist].qf_index = 1;
762 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764 else
765 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000766 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
767 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
768 qi->qf_lists[qi->qf_curlist].qf_ptr =
769 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000771 /* return number of matches */
772 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 goto qf_init_ok;
774 }
775 EMSG(_(e_readerrf));
776error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000777 qf_free(qi, qi->qf_curlist);
778 qi->qf_listcount--;
779 if (qi->qf_curlist > 0)
780 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000782 if (fd != NULL)
783 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
785 {
786 fmt_first = fmt_ptr->next;
787 vim_free(fmt_ptr->prog);
788 vim_free(fmt_ptr);
789 }
790 qf_clean_dir_stack(&dir_stack);
791 qf_clean_dir_stack(&file_stack);
792qf_init_end:
793 vim_free(namebuf);
794 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000795 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 vim_free(fmtstr);
797
798#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000799 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#endif
801
802 return retval;
803}
804
805/*
806 * Prepare for adding a new quickfix list.
807 */
808 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000809qf_new_list(qi)
810 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 int i;
813
814 /*
815 * If the current entry is not the last entry, delete entries below
816 * the current entry. This makes it possible to browse in a tree-like
817 * way with ":grep'.
818 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000819 while (qi->qf_listcount > qi->qf_curlist + 1)
820 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
822 /*
823 * When the stack is full, remove to oldest entry
824 * Otherwise, add a new entry.
825 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000826 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000828 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000830 qi->qf_lists[i - 1] = qi->qf_lists[i];
831 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000834 qi->qf_curlist = qi->qf_listcount++;
835 qi->qf_lists[qi->qf_curlist].qf_index = 0;
836 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000839/*
840 * Free a location list
841 */
842 static void
843ll_free_all(pqi)
844 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000845{
846 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000847 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000848
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000849 qi = *pqi;
850 if (qi == NULL)
851 return;
852 *pqi = NULL; /* Remove reference to this list */
853
854 qi->qf_refcount--;
855 if (qi->qf_refcount < 1)
856 {
857 /* No references to this location list */
858 for (i = 0; i < qi->qf_listcount; ++i)
859 qf_free(qi, i);
860 vim_free(qi);
861 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000862}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000863
864 void
865qf_free_all(wp)
866 win_T *wp;
867{
868 int i;
869 qf_info_T *qi = &ql_info;
870
871 if (wp != NULL)
872 {
873 /* location list */
874 ll_free_all(&wp->w_llist);
875 ll_free_all(&wp->w_llist_ref);
876 }
877 else
878 /* quickfix list */
879 for (i = 0; i < qi->qf_listcount; ++i)
880 qf_free(qi, i);
881}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883/*
884 * Add an entry to the end of the list of errors.
885 * Returns OK or FAIL.
886 */
887 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000888qf_add_entry(qi, prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000889 valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000890 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000891 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 char_u *dir; /* optional directory name */
893 char_u *fname; /* file name or NULL */
894 char_u *mesg; /* message */
895 long lnum; /* line number */
896 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000898 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 int nr; /* error number */
900 int type; /* type character */
901 int valid; /* valid entry */
902{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000903 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000905 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 return FAIL;
907 qfp->qf_fnum = qf_get_fnum(dir, fname);
908 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
909 {
910 vim_free(qfp);
911 return FAIL;
912 }
913 qfp->qf_lnum = lnum;
914 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000916 if (pattern == NULL || *pattern == NUL)
917 qfp->qf_pattern = NULL;
918 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
919 {
920 vim_free(qfp->qf_text);
921 vim_free(qfp);
922 return FAIL;
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 qfp->qf_nr = nr;
925 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
926 type = 0;
927 qfp->qf_type = type;
928 qfp->qf_valid = valid;
929
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000930 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
931 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000933 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 qfp->qf_prev = qfp; /* first element points to itself */
935 }
936 else
937 {
938 qfp->qf_prev = *prevp;
939 (*prevp)->qf_next = qfp;
940 }
941 qfp->qf_next = qfp; /* last element points to itself */
942 qfp->qf_cleared = FALSE;
943 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000944 ++qi->qf_lists[qi->qf_curlist].qf_count;
945 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
946 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000948 qi->qf_lists[qi->qf_curlist].qf_index =
949 qi->qf_lists[qi->qf_curlist].qf_count;
950 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952
953 return OK;
954}
955
956/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000957 * Allocate a new location list for window 'wp'
958 */
959 static int
960ll_new_list(wp)
961 win_T *wp;
962{
963 if ((wp->w_llist = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T))) == NULL)
964 return FAIL;
965
966 vim_memset(wp->w_llist, 0, (size_t)(sizeof(qf_info_T)));
967 wp->w_llist->qf_refcount++;
968
969 return OK;
970}
971
972/*
973 * Return the location list for window 'wp'.
974 * If not present, allocate a location list
975 */
976 static qf_info_T *
977ll_get_or_alloc_list(wp)
978 win_T *wp;
979{
980 if (IS_LL_WINDOW(wp))
981 /* For a location list window, use the referenced location list */
982 return wp->w_llist_ref;
983
984 /*
985 * For a non-location list window, w_llist_ref should not point to a
986 * location list.
987 */
988 ll_free_all(&wp->w_llist_ref);
989
990 if (wp->w_llist == NULL)
991 if (ll_new_list(wp) == FAIL) /* new location list */
992 return NULL;
993 return wp->w_llist;
994}
995
996/*
997 * Copy the location list from window "from" to window "to".
998 */
999 void
1000copy_loclist(from, to)
1001 win_T *from;
1002 win_T *to;
1003{
1004 qf_info_T *qi;
1005 int idx;
1006 int i;
1007
1008 /*
1009 * When copying from a location list window, copy the referenced
1010 * location list. For other windows, copy the location list for
1011 * that window.
1012 */
1013 if (IS_LL_WINDOW(from))
1014 qi = from->w_llist_ref;
1015 else
1016 qi = from->w_llist;
1017
1018 if (qi == NULL) /* no location list to copy */
1019 return;
1020
1021 if (ll_new_list(to) == FAIL) /* allocate a new location list */
1022 return;
1023
1024 to->w_llist->qf_listcount = qi->qf_listcount;
1025
1026 /* Copy the location lists one at a time */
1027 for (idx = 0; idx < qi->qf_listcount; idx++)
1028 {
1029 qf_list_T *from_qfl;
1030 qf_list_T *to_qfl;
1031
1032 to->w_llist->qf_curlist = idx;
1033
1034 from_qfl = &qi->qf_lists[idx];
1035 to_qfl = &to->w_llist->qf_lists[idx];
1036
1037 /* Some of the fields are populated by qf_add_entry() */
1038 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1039 to_qfl->qf_count = 0;
1040 to_qfl->qf_index = 0;
1041 to_qfl->qf_start = NULL;
1042 to_qfl->qf_ptr = NULL;
1043
1044 if (from_qfl->qf_count)
1045 {
1046 qfline_T *from_qfp;
1047 qfline_T *prevp = NULL;
1048
1049 /* copy all the location entries in this list */
1050 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1051 ++i, from_qfp = from_qfp->qf_next)
1052 {
1053 if (qf_add_entry(to->w_llist, &prevp,
1054 NULL,
1055 NULL,
1056 from_qfp->qf_text,
1057 from_qfp->qf_lnum,
1058 from_qfp->qf_col,
1059 from_qfp->qf_viscol,
1060 from_qfp->qf_pattern,
1061 from_qfp->qf_nr,
1062 0,
1063 from_qfp->qf_valid) == FAIL)
1064 {
1065 qf_free_all(to);
1066 return;
1067 }
1068 /*
1069 * qf_add_entry() will not set the qf_num field, as the
1070 * directory and file names are not supplied. So the qf_fnum
1071 * field is copied here.
1072 */
1073 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1074 prevp->qf_type = from_qfp->qf_type; /* error type */
1075 if (from_qfl->qf_ptr == from_qfp)
1076 to_qfl->qf_ptr = prevp; /* current location */
1077 }
1078 }
1079
1080 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1081
1082 /* When no valid entries are present in the list, qf_ptr points to
1083 * the first item in the list */
1084 if (to_qfl->qf_nonevalid == TRUE)
1085 to_qfl->qf_ptr = to_qfl->qf_start;
1086 }
1087
1088 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1089}
1090
1091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 * get buffer number for file "dir.name"
1093 */
1094 static int
1095qf_get_fnum(directory, fname)
1096 char_u *directory;
1097 char_u *fname;
1098{
1099 if (fname == NULL || *fname == NUL) /* no file name */
1100 return 0;
1101 {
1102#ifdef RISCOS
1103 /* Name is reported as `main.c', but file is `c.main' */
1104 return ro_buflist_add(fname);
1105#else
1106 char_u *ptr;
1107 int fnum;
1108
1109# ifdef VMS
1110 vms_remove_version(fname);
1111# endif
1112# ifdef BACKSLASH_IN_FILENAME
1113 if (directory != NULL)
1114 slash_adjust(directory);
1115 slash_adjust(fname);
1116# endif
1117 if (directory != NULL && !vim_isAbsName(fname)
1118 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1119 {
1120 /*
1121 * Here we check if the file really exists.
1122 * This should normally be true, but if make works without
1123 * "leaving directory"-messages we might have missed a
1124 * directory change.
1125 */
1126 if (mch_getperm(ptr) < 0)
1127 {
1128 vim_free(ptr);
1129 directory = qf_guess_filepath(fname);
1130 if (directory)
1131 ptr = concat_fnames(directory, fname, TRUE);
1132 else
1133 ptr = vim_strsave(fname);
1134 }
1135 /* Use concatenated directory name and file name */
1136 fnum = buflist_add(ptr, 0);
1137 vim_free(ptr);
1138 return fnum;
1139 }
1140 return buflist_add(fname, 0);
1141#endif
1142 }
1143}
1144
1145/*
1146 * push dirbuf onto the directory stack and return pointer to actual dir or
1147 * NULL on error
1148 */
1149 static char_u *
1150qf_push_dir(dirbuf, stackptr)
1151 char_u *dirbuf;
1152 struct dir_stack_T **stackptr;
1153{
1154 struct dir_stack_T *ds_new;
1155 struct dir_stack_T *ds_ptr;
1156
1157 /* allocate new stack element and hook it in */
1158 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1159 if (ds_new == NULL)
1160 return NULL;
1161
1162 ds_new->next = *stackptr;
1163 *stackptr = ds_new;
1164
1165 /* store directory on the stack */
1166 if (vim_isAbsName(dirbuf)
1167 || (*stackptr)->next == NULL
1168 || (*stackptr && dir_stack != *stackptr))
1169 (*stackptr)->dirname = vim_strsave(dirbuf);
1170 else
1171 {
1172 /* Okay we don't have an absolute path.
1173 * dirbuf must be a subdir of one of the directories on the stack.
1174 * Let's search...
1175 */
1176 ds_new = (*stackptr)->next;
1177 (*stackptr)->dirname = NULL;
1178 while (ds_new)
1179 {
1180 vim_free((*stackptr)->dirname);
1181 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1182 TRUE);
1183 if (mch_isdir((*stackptr)->dirname) == TRUE)
1184 break;
1185
1186 ds_new = ds_new->next;
1187 }
1188
1189 /* clean up all dirs we already left */
1190 while ((*stackptr)->next != ds_new)
1191 {
1192 ds_ptr = (*stackptr)->next;
1193 (*stackptr)->next = (*stackptr)->next->next;
1194 vim_free(ds_ptr->dirname);
1195 vim_free(ds_ptr);
1196 }
1197
1198 /* Nothing found -> it must be on top level */
1199 if (ds_new == NULL)
1200 {
1201 vim_free((*stackptr)->dirname);
1202 (*stackptr)->dirname = vim_strsave(dirbuf);
1203 }
1204 }
1205
1206 if ((*stackptr)->dirname != NULL)
1207 return (*stackptr)->dirname;
1208 else
1209 {
1210 ds_ptr = *stackptr;
1211 *stackptr = (*stackptr)->next;
1212 vim_free(ds_ptr);
1213 return NULL;
1214 }
1215}
1216
1217
1218/*
1219 * pop dirbuf from the directory stack and return previous directory or NULL if
1220 * stack is empty
1221 */
1222 static char_u *
1223qf_pop_dir(stackptr)
1224 struct dir_stack_T **stackptr;
1225{
1226 struct dir_stack_T *ds_ptr;
1227
1228 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1229 * What to do if it isn't? */
1230
1231 /* pop top element and free it */
1232 if (*stackptr != NULL)
1233 {
1234 ds_ptr = *stackptr;
1235 *stackptr = (*stackptr)->next;
1236 vim_free(ds_ptr->dirname);
1237 vim_free(ds_ptr);
1238 }
1239
1240 /* return NEW top element as current dir or NULL if stack is empty*/
1241 return *stackptr ? (*stackptr)->dirname : NULL;
1242}
1243
1244/*
1245 * clean up directory stack
1246 */
1247 static void
1248qf_clean_dir_stack(stackptr)
1249 struct dir_stack_T **stackptr;
1250{
1251 struct dir_stack_T *ds_ptr;
1252
1253 while ((ds_ptr = *stackptr) != NULL)
1254 {
1255 *stackptr = (*stackptr)->next;
1256 vim_free(ds_ptr->dirname);
1257 vim_free(ds_ptr);
1258 }
1259}
1260
1261/*
1262 * Check in which directory of the directory stack the given file can be
1263 * found.
1264 * Returns a pointer to the directory name or NULL if not found
1265 * Cleans up intermediate directory entries.
1266 *
1267 * TODO: How to solve the following problem?
1268 * If we have the this directory tree:
1269 * ./
1270 * ./aa
1271 * ./aa/bb
1272 * ./bb
1273 * ./bb/x.c
1274 * and make says:
1275 * making all in aa
1276 * making all in bb
1277 * x.c:9: Error
1278 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1279 * qf_guess_filepath will return NULL.
1280 */
1281 static char_u *
1282qf_guess_filepath(filename)
1283 char_u *filename;
1284{
1285 struct dir_stack_T *ds_ptr;
1286 struct dir_stack_T *ds_tmp;
1287 char_u *fullname;
1288
1289 /* no dirs on the stack - there's nothing we can do */
1290 if (dir_stack == NULL)
1291 return NULL;
1292
1293 ds_ptr = dir_stack->next;
1294 fullname = NULL;
1295 while (ds_ptr)
1296 {
1297 vim_free(fullname);
1298 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1299
1300 /* If concat_fnames failed, just go on. The worst thing that can happen
1301 * is that we delete the entire stack.
1302 */
1303 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1304 break;
1305
1306 ds_ptr = ds_ptr->next;
1307 }
1308
1309 vim_free(fullname);
1310
1311 /* clean up all dirs we already left */
1312 while (dir_stack->next != ds_ptr)
1313 {
1314 ds_tmp = dir_stack->next;
1315 dir_stack->next = dir_stack->next->next;
1316 vim_free(ds_tmp->dirname);
1317 vim_free(ds_tmp);
1318 }
1319
1320 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1321
1322}
1323
1324/*
1325 * jump to a quickfix line
1326 * if dir == FORWARD go "errornr" valid entries forward
1327 * if dir == BACKWARD go "errornr" valid entries backward
1328 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1329 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1330 * else if "errornr" is zero, redisplay the same line
1331 * else go to entry "errornr"
1332 */
1333 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001334qf_jump(winptr, dir, errornr, forceit)
1335 win_T *winptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 int dir;
1337 int errornr;
1338 int forceit;
1339{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001340 qf_info_T *qi = &ql_info;
1341 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001342 qfline_T *qf_ptr;
1343 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 int qf_index;
1345 int old_qf_fnum;
1346 int old_qf_index;
1347 int prev_index;
1348 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1349 char_u *err = e_no_more_items;
1350 linenr_T i;
1351 buf_T *old_curbuf;
1352 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 colnr_T screen_col;
1354 colnr_T char_col;
1355 char_u *line;
1356#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001357 char_u *old_swb = p_swb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 int opened_window = FALSE;
1359 win_T *win;
1360 win_T *altwin;
1361#endif
1362 int print_message = TRUE;
1363 int len;
1364#ifdef FEAT_FOLDING
1365 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1366#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001367 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001368 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001370 if (winptr != NULL)
1371 qi = GET_LOC_LIST(winptr);
1372
1373 if (qi->qf_curlist >= qi->qf_listcount
1374 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376 EMSG(_(e_quickfix));
1377 return;
1378 }
1379
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001380 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001382 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 old_qf_index = qf_index;
1384 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1385 {
1386 while (errornr--)
1387 {
1388 old_qf_ptr = qf_ptr;
1389 prev_index = qf_index;
1390 old_qf_fnum = qf_ptr->qf_fnum;
1391 do
1392 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 || qf_ptr->qf_next == NULL)
1395 {
1396 qf_ptr = old_qf_ptr;
1397 qf_index = prev_index;
1398 if (err != NULL)
1399 {
1400 EMSG(_(err));
1401 goto theend;
1402 }
1403 errornr = 0;
1404 break;
1405 }
1406 ++qf_index;
1407 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001408 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1409 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1411 err = NULL;
1412 }
1413 }
1414 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1415 {
1416 while (errornr--)
1417 {
1418 old_qf_ptr = qf_ptr;
1419 prev_index = qf_index;
1420 old_qf_fnum = qf_ptr->qf_fnum;
1421 do
1422 {
1423 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1424 {
1425 qf_ptr = old_qf_ptr;
1426 qf_index = prev_index;
1427 if (err != NULL)
1428 {
1429 EMSG(_(err));
1430 goto theend;
1431 }
1432 errornr = 0;
1433 break;
1434 }
1435 --qf_index;
1436 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001437 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1438 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1440 err = NULL;
1441 }
1442 }
1443 else if (errornr != 0) /* go to specified number */
1444 {
1445 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1446 {
1447 --qf_index;
1448 qf_ptr = qf_ptr->qf_prev;
1449 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001450 while (errornr > qf_index && qf_index <
1451 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 && qf_ptr->qf_next != NULL)
1453 {
1454 ++qf_index;
1455 qf_ptr = qf_ptr->qf_next;
1456 }
1457 }
1458
1459#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001460 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1461 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 /* No need to print the error message if it's visible in the error
1463 * window */
1464 print_message = FALSE;
1465
1466 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001467 * For ":helpgrep" find a help window or open one.
1468 */
1469 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1470 {
1471 win_T *wp;
1472 int n;
1473
1474 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1475 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1476 break;
1477 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1478 win_enter(wp, TRUE);
1479 else
1480 {
1481 /*
1482 * Split off help window; put it at far top if no position
1483 * specified, the current window is vertically split and narrow.
1484 */
1485 n = WSP_HELP;
1486# ifdef FEAT_VERTSPLIT
1487 if (cmdmod.split == 0 && curwin->w_width != Columns
1488 && curwin->w_width < 80)
1489 n |= WSP_TOP;
1490# endif
1491 if (win_split(0, n) == FAIL)
1492 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001493 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001494
1495 if (curwin->w_height < p_hh)
1496 win_setheight((int)p_hh);
1497 }
1498
1499 if (!p_im)
1500 restart_edit = 0; /* don't want insert mode in help file */
1501 }
1502
1503 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 * If currently in the quickfix window, find another window to show the
1505 * file in.
1506 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001507 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 {
1509 /*
1510 * If there is no file specified, we don't know where to go.
1511 * But do advance, otherwise ":cn" gets stuck.
1512 */
1513 if (qf_ptr->qf_fnum == 0)
1514 goto theend;
1515
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001516 /* Locate a window showing a normal buffer */
1517 usable_win = 0;
1518 FOR_ALL_WINDOWS(win)
1519 if (win->w_buffer->b_p_bt[0] == NUL)
1520 {
1521 usable_win = 1;
1522 break;
1523 }
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 /*
1526 * If there is only one window, create a new one above the quickfix
1527 * window.
1528 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529 if (firstwin == lastwin || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531 ll_ref = curwin->w_llist_ref;
1532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (win_split(0, WSP_ABOVE) == FAIL)
1534 goto failed; /* not enough room for window */
1535 opened_window = TRUE; /* close it when fail */
1536 p_swb = empty_option; /* don't split again */
1537# ifdef FEAT_SCROLLBIND
1538 curwin->w_p_scb = FALSE;
1539# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001540 if (ll_ref != NULL)
1541 {
1542 /* The new window should use the location list from the
1543 * location list window */
1544 qf_free_all(curwin);
1545 curwin->w_llist = ll_ref;
1546 ll_ref->qf_refcount++;
1547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 }
1549 else
1550 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551 if (curwin->w_llist_ref != NULL)
1552 {
1553 /* In a location window */
1554 ll_ref = curwin->w_llist_ref;
1555
1556 /* Find the window with the same location list */
1557 FOR_ALL_WINDOWS(win)
1558 if (win->w_llist == ll_ref)
1559 break;
1560 if (win == NULL)
1561 {
1562 /* Find the window showing the selected file */
1563 FOR_ALL_WINDOWS(win)
1564 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1565 break;
1566 if (win == NULL)
1567 {
1568 /* Find a previous usable window */
1569 win = curwin;
1570 do
1571 {
1572 if (win->w_buffer->b_p_bt[0] == NUL)
1573 break;
1574 if (win->w_prev == NULL)
1575 win = lastwin; /* wrap around the top */
1576 else
1577 win = win->w_prev; /* go to previous window */
1578 } while (win != curwin);
1579 }
1580 }
1581 win_goto(win);
1582
1583 /* If the location list for the window is not set, then set it
1584 * to the location list from the location window */
1585 if (win->w_llist == NULL)
1586 {
1587 win->w_llist = ll_ref;
1588 ll_ref->qf_refcount++;
1589 }
1590 }
1591 else
1592 {
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 /*
1595 * Try to find a window that shows the right buffer.
1596 * Default to the window just above the quickfix buffer.
1597 */
1598 win = curwin;
1599 altwin = NULL;
1600 for (;;)
1601 {
1602 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1603 break;
1604 if (win->w_prev == NULL)
1605 win = lastwin; /* wrap around the top */
1606 else
1607 win = win->w_prev; /* go to previous window */
1608
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001609 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 /* Didn't find it, go to the window before the quickfix
1612 * window. */
1613 if (altwin != NULL)
1614 win = altwin;
1615 else if (curwin->w_prev != NULL)
1616 win = curwin->w_prev;
1617 else
1618 win = curwin->w_next;
1619 break;
1620 }
1621
1622 /* Remember a usable window. */
1623 if (altwin == NULL && !win->w_p_pvw
1624 && win->w_buffer->b_p_bt[0] == NUL)
1625 altwin = win;
1626 }
1627
1628 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 }
1631 }
1632#endif
1633
1634 /*
1635 * If there is a file name,
1636 * read the wanted file if needed, and check autowrite etc.
1637 */
1638 old_curbuf = curbuf;
1639 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001640
1641 if (qf_ptr->qf_fnum != 0)
1642 {
1643 if (qf_ptr->qf_type == 1)
1644 {
1645 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1646 * set b_p_ro flag). */
1647 if (!can_abandon(curbuf, forceit))
1648 {
1649 EMSG(_(e_nowrtmsg));
1650 ok = FALSE;
1651 }
1652 else
1653 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1654 ECMD_HIDE + ECMD_SET_HELP);
1655 }
1656 else
1657 ok = buflist_getfile(qf_ptr->qf_fnum,
1658 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1659 }
1660
1661 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 {
1663 /* When not switched to another buffer, still need to set pc mark */
1664 if (curbuf == old_curbuf)
1665 setpcmark();
1666
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001667 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001669 /*
1670 * Go to line with error, unless qf_lnum is 0.
1671 */
1672 i = qf_ptr->qf_lnum;
1673 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001675 if (i > curbuf->b_ml.ml_line_count)
1676 i = curbuf->b_ml.ml_line_count;
1677 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001679 if (qf_ptr->qf_col > 0)
1680 {
1681 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1682 if (qf_ptr->qf_viscol == TRUE)
1683 {
1684 /*
1685 * Check each character from the beginning of the error
1686 * line up to the error column. For each tab character
1687 * found, reduce the error column value by the length of
1688 * a tab character.
1689 */
1690 line = ml_get_curline();
1691 screen_col = 0;
1692 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1693 {
1694 if (*line == NUL)
1695 break;
1696 if (*line++ == '\t')
1697 {
1698 curwin->w_cursor.col -= 7 - (screen_col % 8);
1699 screen_col += 8 - (screen_col % 8);
1700 }
1701 else
1702 ++screen_col;
1703 }
1704 }
1705 check_cursor();
1706 }
1707 else
1708 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001711 {
1712 pos_T save_cursor;
1713
1714 /* Move the cursor to the first line in the buffer */
1715 save_cursor = curwin->w_cursor;
1716 curwin->w_cursor.lnum = 0;
1717 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1718 curwin->w_cursor = save_cursor;
1719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721#ifdef FEAT_FOLDING
1722 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1723 foldOpenCursor();
1724#endif
1725 if (print_message)
1726 {
1727 /* Update the screen before showing the message */
1728 update_topline_redraw();
1729 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001730 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1732 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1733 /* Add the message, skipping leading whitespace and newlines. */
1734 len = (int)STRLEN(IObuff);
1735 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1736
1737 /* Output the message. Overwrite to avoid scrolling when the 'O'
1738 * flag is present in 'shortmess'; But when not jumping, print the
1739 * whole message. */
1740 i = msg_scroll;
1741 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1742 msg_scroll = TRUE;
1743 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1744 msg_scroll = FALSE;
1745 msg_attr_keep(IObuff, 0, TRUE);
1746 msg_scroll = i;
1747 }
1748 }
1749 else
1750 {
1751#ifdef FEAT_WINDOWS
1752 if (opened_window)
1753 win_close(curwin, TRUE); /* Close opened window */
1754#endif
1755 if (qf_ptr->qf_fnum != 0)
1756 {
1757 /*
1758 * Couldn't open file, so put index back where it was. This could
1759 * happen if the file was readonly and we changed something.
1760 */
1761#ifdef FEAT_WINDOWS
1762failed:
1763#endif
1764 qf_ptr = old_qf_ptr;
1765 qf_index = old_qf_index;
1766 }
1767 }
1768theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001769 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1770 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#ifdef FEAT_WINDOWS
1772 if (p_swb != old_swb && opened_window)
1773 {
1774 /* Restore old 'switchbuf' value, but not when an autocommand or
1775 * modeline has changed the value. */
1776 if (p_swb == empty_option)
1777 p_swb = old_swb;
1778 else
1779 free_string_option(old_swb);
1780 }
1781#endif
1782}
1783
1784/*
1785 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001786 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 */
1788 void
1789qf_list(eap)
1790 exarg_T *eap;
1791{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001792 buf_T *buf;
1793 char_u *fname;
1794 qfline_T *qfp;
1795 int i;
1796 int idx1 = 1;
1797 int idx2 = -1;
1798 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001799 char_u *arg = eap->arg;
1800 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001802 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001804 if (eap->cmdidx == CMD_llist)
1805 {
1806 qi = GET_LOC_LIST(curwin);
1807 if (qi == NULL)
1808 {
1809 EMSG(_(e_loclist));
1810 return;
1811 }
1812 }
1813
1814 if (qi->qf_curlist >= qi->qf_listcount
1815 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
1817 EMSG(_(e_quickfix));
1818 return;
1819 }
1820 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1821 {
1822 EMSG(_(e_trailing));
1823 return;
1824 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001825 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (idx1 < 0)
1827 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1828 if (idx2 < 0)
1829 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1830
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001831 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001833 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1834 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1837 {
1838 if (need_return)
1839 {
1840 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001841 if (got_int)
1842 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 need_return = FALSE;
1844 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001845
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001846 fname = NULL;
1847 if (qfp->qf_fnum != 0
1848 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1849 {
1850 fname = buf->b_fname;
1851 if (qfp->qf_type == 1) /* :helpgrep */
1852 fname = gettail(fname);
1853 }
1854 if (fname == NULL)
1855 sprintf((char *)IObuff, "%2d", i);
1856 else
1857 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1858 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001859 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001860 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1861 if (qfp->qf_lnum == 0)
1862 IObuff[0] = NUL;
1863 else if (qfp->qf_col == 0)
1864 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1865 else
1866 sprintf((char *)IObuff, ":%ld col %d",
1867 qfp->qf_lnum, qfp->qf_col);
1868 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1869 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1870 msg_puts_attr(IObuff, hl_attr(HLF_N));
1871 if (qfp->qf_pattern != NULL)
1872 {
1873 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1874 STRCAT(IObuff, ":");
1875 msg_puts(IObuff);
1876 }
1877 msg_puts((char_u *)" ");
1878
1879 /* Remove newlines and leading whitespace from the text. For an
1880 * unrecognized line keep the indent, the compiler may mark a word
1881 * with ^^^^. */
1882 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1884 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001885 msg_prt_line(IObuff, FALSE);
1886 out_flush(); /* show one line at a time */
1887 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001889
1890 qfp = qfp->qf_next;
1891 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 ui_breakcheck();
1893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894}
1895
1896/*
1897 * Remove newlines and leading whitespace from an error message.
1898 * Put the result in "buf[bufsize]".
1899 */
1900 static void
1901qf_fmt_text(text, buf, bufsize)
1902 char_u *text;
1903 char_u *buf;
1904 int bufsize;
1905{
1906 int i;
1907 char_u *p = text;
1908
1909 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1910 {
1911 if (*p == '\n')
1912 {
1913 buf[i] = ' ';
1914 while (*++p != NUL)
1915 if (!vim_iswhite(*p) && *p != '\n')
1916 break;
1917 }
1918 else
1919 buf[i] = *p++;
1920 }
1921 buf[i] = NUL;
1922}
1923
1924/*
1925 * ":colder [count]": Up in the quickfix stack.
1926 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001927 * ":lolder [count]": Up in the location list stack.
1928 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 */
1930 void
1931qf_age(eap)
1932 exarg_T *eap;
1933{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001934 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 int count;
1936
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001937 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
1938 {
1939 qi = GET_LOC_LIST(curwin);
1940 if (qi == NULL)
1941 {
1942 EMSG(_(e_loclist));
1943 return;
1944 }
1945 }
1946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (eap->addr_count != 0)
1948 count = eap->line2;
1949 else
1950 count = 1;
1951 while (count--)
1952 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001953 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 EMSG(_("E380: At bottom of quickfix stack"));
1958 return;
1959 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001960 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962 else
1963 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001964 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 EMSG(_("E381: At top of quickfix stack"));
1967 return;
1968 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001972 qf_msg(qi);
1973
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974}
1975
1976 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001977qf_msg(qi)
1978 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
1980 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001981 qi->qf_curlist + 1, qi->qf_listcount,
1982 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001984 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985#endif
1986}
1987
1988/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00001989 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 */
1991 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001992qf_free(qi, idx)
1993 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 int idx;
1995{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001996 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001998 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002000 qfp = qi->qf_lists[idx].qf_start->qf_next;
2001 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2002 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2003 vim_free(qi->qf_lists[idx].qf_start);
2004 qi->qf_lists[idx].qf_start = qfp;
2005 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007}
2008
2009/*
2010 * qf_mark_adjust: adjust marks
2011 */
2012 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002013qf_mark_adjust(wp, line1, line2, amount, amount_after)
2014 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 linenr_T line1;
2016 linenr_T line2;
2017 long amount;
2018 long amount_after;
2019{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002020 int i;
2021 qfline_T *qfp;
2022 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002023 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025 if (wp != NULL)
2026 {
2027 if (wp->w_llist == NULL)
2028 return;
2029 qi = wp->w_llist;
2030 }
2031
2032 for (idx = 0; idx < qi->qf_listcount; ++idx)
2033 if (qi->qf_lists[idx].qf_count)
2034 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2035 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (qfp->qf_fnum == curbuf->b_fnum)
2037 {
2038 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2039 {
2040 if (amount == MAXLNUM)
2041 qfp->qf_cleared = TRUE;
2042 else
2043 qfp->qf_lnum += amount;
2044 }
2045 else if (amount_after && qfp->qf_lnum > line2)
2046 qfp->qf_lnum += amount_after;
2047 }
2048}
2049
2050/*
2051 * Make a nice message out of the error character and the error number:
2052 * char number message
2053 * e or E 0 " error"
2054 * w or W 0 " warning"
2055 * i or I 0 " info"
2056 * 0 0 ""
2057 * other 0 " c"
2058 * e or E n " error n"
2059 * w or W n " warning n"
2060 * i or I n " info n"
2061 * 0 n " error n"
2062 * other n " c n"
2063 * 1 x "" :helpgrep
2064 */
2065 static char_u *
2066qf_types(c, nr)
2067 int c, nr;
2068{
2069 static char_u buf[20];
2070 static char_u cc[3];
2071 char_u *p;
2072
2073 if (c == 'W' || c == 'w')
2074 p = (char_u *)" warning";
2075 else if (c == 'I' || c == 'i')
2076 p = (char_u *)" info";
2077 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2078 p = (char_u *)" error";
2079 else if (c == 0 || c == 1)
2080 p = (char_u *)"";
2081 else
2082 {
2083 cc[0] = ' ';
2084 cc[1] = c;
2085 cc[2] = NUL;
2086 p = cc;
2087 }
2088
2089 if (nr <= 0)
2090 return p;
2091
2092 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2093 return buf;
2094}
2095
2096#if defined(FEAT_WINDOWS) || defined(PROTO)
2097/*
2098 * ":cwindow": open the quickfix window if we have errors to display,
2099 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002100 * ":lwindow": open the location list window if we have locations to display,
2101 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 */
2103 void
2104ex_cwindow(eap)
2105 exarg_T *eap;
2106{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002107 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 win_T *win;
2109
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002110 if (eap->cmdidx == CMD_lwindow)
2111 {
2112 qi = GET_LOC_LIST(curwin);
2113 if (qi == NULL)
2114 return;
2115 }
2116
2117 /* Look for an existing quickfix window. */
2118 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120 /*
2121 * If a quickfix window is open but we have no errors to display,
2122 * close the window. If a quickfix window is not open, then open
2123 * it if we have errors; otherwise, leave it closed.
2124 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002125 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
2126 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
2128 if (win != NULL)
2129 ex_cclose(eap);
2130 }
2131 else if (win == NULL)
2132 ex_copen(eap);
2133}
2134
2135/*
2136 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002137 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 */
2139/*ARGSUSED*/
2140 void
2141ex_cclose(eap)
2142 exarg_T *eap;
2143{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002144 win_T *win = NULL;
2145 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002147 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2148 {
2149 qi = GET_LOC_LIST(curwin);
2150 if (qi == NULL)
2151 return;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002154 /* Find existing quickfix window and close it. */
2155 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (win != NULL)
2157 win_close(win, FALSE);
2158}
2159
2160/*
2161 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 */
2164 void
2165ex_copen(eap)
2166 exarg_T *eap;
2167{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002168 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 win_T *win;
2171
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002172 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2173 {
2174 qi = GET_LOC_LIST(curwin);
2175 if (qi == NULL)
2176 {
2177 EMSG(_(e_loclist));
2178 return;
2179 }
2180 }
2181
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (eap->addr_count != 0)
2183 height = eap->line2;
2184 else
2185 height = QF_WINHEIGHT;
2186
2187#ifdef FEAT_VISUAL
2188 reset_VIsual_and_resel(); /* stop Visual mode */
2189#endif
2190#ifdef FEAT_GUI
2191 need_mouse_correct = TRUE;
2192#endif
2193
2194 /*
2195 * Find existing quickfix window, or open a new one.
2196 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002197 win = qf_find_win(qi);
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (win != NULL)
2200 win_goto(win);
2201 else
2202 {
2203 /* The current window becomes the previous window afterwards. */
2204 win = curwin;
2205
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002206 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2207 /* Create the new window at the very bottom. */
2208 win_goto(lastwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (win_split(height, WSP_BELOW) == FAIL)
2210 return; /* not enough room for window */
2211#ifdef FEAT_SCROLLBIND
2212 curwin->w_p_scb = FALSE;
2213#endif
2214
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 /* Remove the location list for the quickfix window */
2216 qf_free_all(curwin);
2217
2218 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220 /*
2221 * For the location list window, create a reference to the
2222 * location list from the window 'win'.
2223 */
2224 curwin->w_llist_ref = win->w_llist;
2225 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227
2228 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2229 /* switch off 'swapfile' */
2230 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2231 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
2232 OPT_LOCAL);
2233 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2234 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002236#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 /* Only set the height when there is no window to the side. */
2238 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 win_setheight(height);
2241 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2242 if (win_valid(win))
2243 prevwin = win;
2244 }
2245
2246 /*
2247 * Fill the buffer with the quickfix list.
2248 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002249 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002251 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 curwin->w_cursor.col = 0;
2253 check_cursor();
2254 update_topline(); /* scroll to show the line */
2255}
2256
2257/*
2258 * Return the number of the current entry (line number in the quickfix
2259 * window).
2260 */
2261 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002262qf_current_entry(wp)
2263 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002265 qf_info_T *qi = &ql_info;
2266
2267 if (IS_LL_WINDOW(wp))
2268 /* In the location list window, use the referenced location list */
2269 qi = wp->w_llist_ref;
2270
2271 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272}
2273
2274/*
2275 * Update the cursor position in the quickfix window to the current error.
2276 * Return TRUE if there is a quickfix window.
2277 */
2278 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002279qf_win_pos_update(qi, old_qf_index)
2280 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int old_qf_index; /* previous qf_index or zero */
2282{
2283 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002284 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 /*
2287 * Put the cursor on the current error in the quickfix window, so that
2288 * it's viewable.
2289 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002290 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (win != NULL
2292 && qf_index <= win->w_buffer->b_ml.ml_line_count
2293 && old_qf_index != qf_index)
2294 {
2295 win_T *old_curwin = curwin;
2296
2297 curwin = win;
2298 curbuf = win->w_buffer;
2299 if (qf_index > old_qf_index)
2300 {
2301 curwin->w_redraw_top = old_qf_index;
2302 curwin->w_redraw_bot = qf_index;
2303 }
2304 else
2305 {
2306 curwin->w_redraw_top = qf_index;
2307 curwin->w_redraw_bot = old_qf_index;
2308 }
2309 curwin->w_cursor.lnum = qf_index;
2310 curwin->w_cursor.col = 0;
2311 update_topline(); /* scroll to show the line */
2312 redraw_later(VALID);
2313 curwin->w_redr_status = TRUE; /* update ruler */
2314 curwin = old_curwin;
2315 curbuf = curwin->w_buffer;
2316 }
2317 return win != NULL;
2318}
2319
2320/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002321 * Find a window displaying the quickfix/location list 'qi'
2322 */
2323 static win_T *
2324qf_find_win(qi)
2325 qf_info_T *qi;
2326{
2327 win_T *win;
2328
2329 /*
2330 * When searching for the quickfix buffer, find a window
2331 * with w_llist_ref set to NULL.
2332 * When searching for the location list buffer, find a window
2333 * with w_llist_ref pointing to the supplied location list.
2334 */
2335 FOR_ALL_WINDOWS(win)
2336 if (bt_quickfix(win->w_buffer))
2337 if ((qi == &ql_info && win->w_llist_ref == NULL)
2338 || (qi != &ql_info && win->w_llist_ref == qi))
2339 break;
2340
2341 return win;
2342}
2343
2344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 * Find quickfix buffer.
2346 */
2347 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002348qf_find_buf(qi)
2349 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002351 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002353 win = qf_find_win(qi);
2354
2355 return (win != NULL) ? win->w_buffer: NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356}
2357
2358/*
2359 * Find the quickfix buffer. If it exists, update the contents.
2360 */
2361 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002362qf_update_buffer(qi)
2363 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
2365 buf_T *buf;
2366#ifdef FEAT_AUTOCMD
2367 aco_save_T aco;
2368#else
2369 buf_T *save_curbuf;
2370#endif
2371
2372 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002373 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (buf != NULL)
2375 {
2376#ifdef FEAT_AUTOCMD
2377 /* set curwin/curbuf to buf and save a few things */
2378 aucmd_prepbuf(&aco, buf);
2379#else
2380 save_curbuf = curbuf;
2381 curbuf = buf;
2382#endif
2383
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002384 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386#ifdef FEAT_AUTOCMD
2387 /* restore curwin/curbuf and a few other things */
2388 aucmd_restbuf(&aco);
2389#else
2390 curbuf = save_curbuf;
2391#endif
2392
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002393 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395}
2396
2397/*
2398 * Fill current buffer with quickfix errors, replacing any previous contents.
2399 * curbuf must be the quickfix buffer!
2400 */
2401 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402qf_fill_buffer(qi)
2403 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002405 linenr_T lnum;
2406 qfline_T *qfp;
2407 buf_T *errbuf;
2408 int len;
2409 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 /* delete all existing lines */
2412 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2413 (void)ml_delete((linenr_T)1, FALSE);
2414
2415 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002416 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002419 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2420 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 if (qfp->qf_fnum != 0
2423 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2424 && errbuf->b_fname != NULL)
2425 {
2426 if (qfp->qf_type == 1) /* :helpgrep */
2427 STRCPY(IObuff, gettail(errbuf->b_fname));
2428 else
2429 STRCPY(IObuff, errbuf->b_fname);
2430 len = (int)STRLEN(IObuff);
2431 }
2432 else
2433 len = 0;
2434 IObuff[len++] = '|';
2435
2436 if (qfp->qf_lnum > 0)
2437 {
2438 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2439 len += (int)STRLEN(IObuff + len);
2440
2441 if (qfp->qf_col > 0)
2442 {
2443 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2444 len += (int)STRLEN(IObuff + len);
2445 }
2446
2447 sprintf((char *)IObuff + len, "%s",
2448 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2449 len += (int)STRLEN(IObuff + len);
2450 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002451 else if (qfp->qf_pattern != NULL)
2452 {
2453 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2454 len += (int)STRLEN(IObuff + len);
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 IObuff[len++] = '|';
2457 IObuff[len++] = ' ';
2458
2459 /* Remove newlines and leading whitespace from the text.
2460 * For an unrecognized line keep the indent, the compiler may
2461 * mark a word with ^^^^. */
2462 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2463 IObuff + len, IOSIZE - len);
2464
2465 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2466 == FAIL)
2467 break;
2468 qfp = qfp->qf_next;
2469 }
2470 /* Delete the empty line which is now at the end */
2471 (void)ml_delete(lnum + 1, FALSE);
2472 }
2473
2474 /* correct cursor position */
2475 check_lnums(TRUE);
2476
2477 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2478 * resembles reading a file into a buffer, it's more logical when using
2479 * autocommands. */
2480 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2481 curbuf->b_p_ma = FALSE;
2482
2483#ifdef FEAT_AUTOCMD
2484 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2485 FALSE, curbuf);
2486 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2487 FALSE, curbuf);
2488#endif
2489
2490 /* make sure it will be redrawn */
2491 redraw_curbuf_later(NOT_VALID);
2492
2493 /* Restore KeyTyped, setting 'filetype' may reset it. */
2494 KeyTyped = old_KeyTyped;
2495}
2496
2497#endif /* FEAT_WINDOWS */
2498
2499/*
2500 * Return TRUE if "buf" is the quickfix buffer.
2501 */
2502 int
2503bt_quickfix(buf)
2504 buf_T *buf;
2505{
2506 return (buf->b_p_bt[0] == 'q');
2507}
2508
2509/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002510 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2511 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 */
2513 int
2514bt_nofile(buf)
2515 buf_T *buf;
2516{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002517 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2518 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519}
2520
2521/*
2522 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2523 */
2524 int
2525bt_dontwrite(buf)
2526 buf_T *buf;
2527{
2528 return (buf->b_p_bt[0] == 'n');
2529}
2530
2531 int
2532bt_dontwrite_msg(buf)
2533 buf_T *buf;
2534{
2535 if (bt_dontwrite(buf))
2536 {
2537 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2538 return TRUE;
2539 }
2540 return FALSE;
2541}
2542
2543/*
2544 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2545 * and 'bufhidden'.
2546 */
2547 int
2548buf_hide(buf)
2549 buf_T *buf;
2550{
2551 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2552 switch (buf->b_p_bh[0])
2553 {
2554 case 'u': /* "unload" */
2555 case 'w': /* "wipe" */
2556 case 'd': return FALSE; /* "delete" */
2557 case 'h': return TRUE; /* "hide" */
2558 }
2559 return (p_hid || cmdmod.hide);
2560}
2561
2562/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002563 * Return TRUE when using ":vimgrep" for ":grep".
2564 */
2565 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002566grep_internal(cmdidx)
2567 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002568{
Bram Moolenaar81695252004-12-29 20:58:21 +00002569 return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002570 && STRCMP("internal",
2571 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2572}
2573
2574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 * Used for ":make", ":grep" and ":grepadd".
2576 */
2577 void
2578ex_make(eap)
2579 exarg_T *eap;
2580{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002581 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 char_u *cmd;
2583 unsigned len;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002584#ifdef FEAT_AUTOCMD
2585 char_u *au_name = NULL;
2586
2587 switch (eap->cmdidx)
2588 {
2589 case CMD_make: au_name = (char_u *)"make"; break;
2590 case CMD_grep: au_name = (char_u *)"grep"; break;
2591 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2592 default: break;
2593 }
2594 if (au_name != NULL)
2595 {
2596 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2597 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002598# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002599 if (did_throw || force_abort)
2600 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002601# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002602 }
2603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaar86b68352004-12-27 21:59:20 +00002605 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002606 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002607 {
2608 ex_vimgrep(eap);
2609 return;
2610 }
2611
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002613 fname = get_mef_name();
2614 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002616 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
2618 /*
2619 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2620 */
2621 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2622 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002623 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 cmd = alloc(len);
2625 if (cmd == NULL)
2626 return;
2627 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2628 (char *)p_shq);
2629 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002630 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 /*
2632 * Output a newline if there's something else than the :make command that
2633 * was typed (in which case the cursor is in column 0).
2634 */
2635 if (msg_col == 0)
2636 msg_didout = FALSE;
2637 msg_start();
2638 MSG_PUTS(":!");
2639 msg_outtrans(cmd); /* show what we are doing */
2640
2641 /* let the shell know if we are redirecting output or not */
2642 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2643
2644#ifdef AMIGA
2645 out_flush();
2646 /* read window status report and redraw before message */
2647 (void)char_avail();
2648#endif
2649
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002650 if (qf_init(NULL, fname, eap->cmdidx != CMD_make ? p_gefm : p_efm,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002651 eap->cmdidx != CMD_grepadd) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 && !eap->forceit)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002653 qf_jump(NULL, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
Bram Moolenaar7c626922005-02-07 22:01:03 +00002655 mch_remove(fname);
2656 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002658
2659#ifdef FEAT_AUTOCMD
2660 if (au_name != NULL)
2661 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2662 curbuf->b_fname, TRUE, curbuf);
2663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664}
2665
2666/*
2667 * Return the name for the errorfile, in allocated memory.
2668 * Find a new unique name when 'makeef' contains "##".
2669 * Returns NULL for error.
2670 */
2671 static char_u *
2672get_mef_name()
2673{
2674 char_u *p;
2675 char_u *name;
2676 static int start = -1;
2677 static int off = 0;
2678#ifdef HAVE_LSTAT
2679 struct stat sb;
2680#endif
2681
2682 if (*p_mef == NUL)
2683 {
2684 name = vim_tempname('e');
2685 if (name == NULL)
2686 EMSG(_(e_notmp));
2687 return name;
2688 }
2689
2690 for (p = p_mef; *p; ++p)
2691 if (p[0] == '#' && p[1] == '#')
2692 break;
2693
2694 if (*p == NUL)
2695 return vim_strsave(p_mef);
2696
2697 /* Keep trying until the name doesn't exist yet. */
2698 for (;;)
2699 {
2700 if (start == -1)
2701 start = mch_get_pid();
2702 else
2703 off += 19;
2704
2705 name = alloc((unsigned)STRLEN(p_mef) + 30);
2706 if (name == NULL)
2707 break;
2708 STRCPY(name, p_mef);
2709 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2710 STRCAT(name, p + 2);
2711 if (mch_getperm(name) < 0
2712#ifdef HAVE_LSTAT
2713 /* Don't accept a symbolic link, its a security risk. */
2714 && mch_lstat((char *)name, &sb) < 0
2715#endif
2716 )
2717 break;
2718 vim_free(name);
2719 }
2720 return name;
2721}
2722
2723/*
2724 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002725 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 */
2727 void
2728ex_cc(eap)
2729 exarg_T *eap;
2730{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002731 win_T *wp = NULL;
2732 qf_info_T *qi;
2733
2734 if (eap->cmdidx == CMD_ll || eap->cmdidx == CMD_lrewind
2735 || eap->cmdidx == CMD_lfirst || eap->cmdidx == CMD_llast)
2736 {
2737 qi = GET_LOC_LIST(curwin);
2738 if (qi == NULL)
2739 {
2740 EMSG(_(e_loclist));
2741 return;
2742 }
2743 wp = curwin;
2744 }
2745
2746 qf_jump(wp, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 eap->addr_count > 0
2748 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002749 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002751 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2752 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 ? 1
2754 : 32767,
2755 eap->forceit);
2756}
2757
2758/*
2759 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002760 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 */
2762 void
2763ex_cnext(eap)
2764 exarg_T *eap;
2765{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002766 win_T *wp = NULL;
2767 qf_info_T *qi;
2768
2769 if (eap->cmdidx == CMD_lnext || eap->cmdidx == CMD_lNext
2770 || eap->cmdidx == CMD_lprevious || eap->cmdidx == CMD_lnfile
2771 || eap->cmdidx == CMD_lNfile || eap->cmdidx == CMD_lpfile)
2772 {
2773 qi = GET_LOC_LIST(curwin);
2774 if (qi == NULL)
2775 {
2776 EMSG(_(e_loclist));
2777 return;
2778 }
2779 wp = curwin;
2780 }
2781
2782 qf_jump(wp, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002784 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2787 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 ? BACKWARD_FILE
2789 : BACKWARD,
2790 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2791}
2792
2793/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002794 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002795 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
2797 void
2798ex_cfile(eap)
2799 exarg_T *eap;
2800{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002801 win_T *wp = NULL;
2802 qf_info_T *qi;
2803
2804 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2805 || eap->cmdidx == CMD_laddfile)
2806 {
2807 qi = ll_get_or_alloc_list(curwin);
2808 if (qi == NULL)
2809 return;
2810 wp = curwin;
2811 }
2812
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if (*eap->arg != NUL)
2814 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002815
2816 /*
2817 * This function is used by the :cfile, :cgetfile and :caddfile
2818 * commands.
2819 * :cfile always creates a new quickfix list and jumps to the
2820 * first error.
2821 * :cgetfile creates a new quickfix list but doesn't jump to the
2822 * first error.
2823 * :caddfile adds to an existing quickfix list. If there is no
2824 * quickfix list then a new list is created.
2825 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002826 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2827 && eap->cmdidx != CMD_laddfile)) > 0
2828 && (eap->cmdidx == CMD_cfile
2829 || eap->cmdidx == CMD_lfile))
2830 qf_jump(wp, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831}
2832
2833/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002834 * ":vimgrep {pattern} file(s)"
2835 */
2836 void
2837ex_vimgrep(eap)
2838 exarg_T *eap;
2839{
Bram Moolenaar81695252004-12-29 20:58:21 +00002840 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002841 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002842 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002843 char_u *s;
2844 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002845 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002846 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002847 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002848 buf_T *buf;
2849 int duplicate_name = FALSE;
2850 int using_dummy;
2851 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002852 buf_T *first_match_buf = NULL;
2853 time_t seconds = 0;
2854#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2855 char_u *save_ei = NULL;
2856 aco_save_T aco;
2857#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002858#ifdef FEAT_AUTOCMD
2859 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002860 int flags = 0;
2861 colnr_T col;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002862 qf_info_T *qi = &ql_info;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002863
2864 switch (eap->cmdidx)
2865 {
2866 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
2867 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
2868 default: break;
2869 }
2870 if (au_name != NULL)
2871 {
2872 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2873 curbuf->b_fname, TRUE, curbuf);
2874 if (did_throw || force_abort)
2875 return;
2876 }
2877#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002878
Bram Moolenaar81695252004-12-29 20:58:21 +00002879 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002880 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002881 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002882 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002883 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00002884 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00002885 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002886 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002887 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002888 if (regmatch.regprog == NULL)
2889 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002890 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00002891 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002892
2893 p = skipwhite(p);
2894 if (*p == NUL)
2895 {
2896 EMSG(_("E683: File name missing or invalid pattern"));
2897 goto theend;
2898 }
2899
2900 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002901 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002902 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002903 qf_new_list(qi);
2904 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002905 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002906 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002907 prevp->qf_next != prevp; prevp = prevp->qf_next)
2908 ;
2909
2910 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002911 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002912 goto theend;
2913 if (fcount == 0)
2914 {
2915 EMSG(_(e_nomatch));
2916 goto theend;
2917 }
2918
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002919 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002920 for (fi = 0; fi < fcount && !got_int; ++fi)
2921 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002922 if (time(NULL) > seconds)
2923 {
2924 /* Display the file name every second or so. */
2925 seconds = time(NULL);
2926 msg_start();
Bram Moolenaara5373fa2005-09-09 19:47:12 +00002927 p = msg_strtrunc(fnames[fi], TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002928 if (p == NULL)
2929 msg_outtrans(fnames[fi]);
2930 else
2931 {
2932 msg_outtrans(p);
2933 vim_free(p);
2934 }
2935 msg_clr_eos();
2936 msg_didout = FALSE; /* overwrite this message */
2937 msg_nowait = TRUE; /* don't wait for this message */
2938 msg_col = 0;
2939 out_flush();
2940 }
2941
Bram Moolenaar81695252004-12-29 20:58:21 +00002942 buf = buflist_findname_exp(fnames[fi]);
2943 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2944 {
2945 /* Remember that a buffer with this name already exists. */
2946 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002947 using_dummy = TRUE;
2948
2949#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2950 /* Don't do Filetype autocommands to avoid loading syntax and
2951 * indent scripts, a great speed improvement. */
2952 save_ei = au_event_disable(",Filetype");
2953#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002954
2955 /* Load file into a buffer, so that 'fileencoding' is detected,
2956 * autocommands applied, etc. */
2957 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002958
2959#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2960 au_event_restore(save_ei);
2961#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002962 }
2963 else
2964 /* Use existing, loaded buffer. */
2965 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002966
Bram Moolenaar81695252004-12-29 20:58:21 +00002967 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002968 {
2969 if (!got_int)
2970 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2971 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002972 else
2973 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002974 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002975 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002976 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002977 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002978 /* For ":1vimgrep" look for multiple matches. */
2979 col = 0;
2980 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2981 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002982 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002983 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002984 NULL, /* dir */
2985 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00002986 ml_get_buf(buf,
2987 regmatch.startpos[0].lnum + lnum, FALSE),
2988 regmatch.startpos[0].lnum + lnum,
2989 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00002990 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002991 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002992 0, /* nr */
2993 0, /* type */
2994 TRUE /* valid */
2995 ) == FAIL)
2996 {
2997 got_int = TRUE;
2998 break;
2999 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003000 else
3001 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003002 if ((flags & VGR_GLOBAL) == 0
3003 || regmatch.endpos[0].lnum > 0)
3004 break;
3005 col = regmatch.endpos[0].col
3006 + (col == regmatch.endpos[0].col);
3007 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3008 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003009 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003010 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003011 if (got_int)
3012 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003013 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003014
3015 if (using_dummy)
3016 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003017 if (found_match && first_match_buf == NULL)
3018 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003019 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003020 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003021 /* Never keep a dummy buffer if there is another buffer
3022 * with the same name. */
3023 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003024 buf = NULL;
3025 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003026 else if (!buf_hide(buf))
3027 {
3028 /* When not hiding the buffer and no match was found we
3029 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00003030 * there was a match and it wasn't the first one or we
3031 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003032 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003033 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003034 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003035 buf = NULL;
3036 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003037 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003038 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003039 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003040 buf = NULL;
3041 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003042 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003043
3044#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3045 if (buf != NULL)
3046 {
3047 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaar748bf032005-02-02 23:04:36 +00003048 * need to be done now, in that buffer. And then the
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003049 * modelines need to be done (again). */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003050 aucmd_prepbuf(&aco, buf);
3051 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3052 buf->b_fname, TRUE, buf);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003053 do_modelines(FALSE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003054 aucmd_restbuf(&aco);
3055 }
3056#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003057 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003058 }
3059 }
3060
3061 FreeWild(fcount, fnames);
3062
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003063 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3064 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3065 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003066
3067#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003068 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003069#endif
3070
3071 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003072 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003073 {
3074 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003075 qf_jump(NULL, 0, 0, eap->forceit);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003076 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003077 else
3078 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003079
Bram Moolenaar7c626922005-02-07 22:01:03 +00003080#ifdef FEAT_AUTOCMD
3081 if (au_name != NULL)
3082 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3083 curbuf->b_fname, TRUE, curbuf);
3084#endif
3085
Bram Moolenaar86b68352004-12-27 21:59:20 +00003086theend:
3087 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003088}
3089
3090/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003091 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003092 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003093 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3094 * If "s" is not NULL terminate the pattern with a NUL.
3095 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003096 */
3097 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003098skip_vimgrep_pat(p, s, flags)
3099 char_u *p;
3100 char_u **s;
3101 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003102{
3103 int c;
3104
3105 if (vim_isIDc(*p))
3106 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003107 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003108 if (s != NULL)
3109 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003110 p = skiptowhite(p);
3111 if (s != NULL && *p != NUL)
3112 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003113 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003114 else
3115 {
3116 /* ":vimgrep /pattern/[g][j] fname" */
3117 if (s != NULL)
3118 *s = p + 1;
3119 c = *p;
3120 p = skip_regexp(p + 1, c, TRUE, NULL);
3121 if (*p != c)
3122 return NULL;
3123
3124 /* Truncate the pattern. */
3125 if (s != NULL)
3126 *p = NUL;
3127 ++p;
3128
3129 /* Find the flags */
3130 while (*p == 'g' || *p == 'j')
3131 {
3132 if (flags != NULL)
3133 {
3134 if (*p == 'g')
3135 *flags |= VGR_GLOBAL;
3136 else
3137 *flags |= VGR_NOJUMP;
3138 }
3139 ++p;
3140 }
3141 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003142 return p;
3143}
3144
3145/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003146 * Load file "fname" into a dummy buffer and return the buffer pointer.
3147 * Returns NULL if it fails.
3148 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3149 */
3150 static buf_T *
3151load_dummy_buffer(fname)
3152 char_u *fname;
3153{
3154 buf_T *newbuf;
3155 int failed = TRUE;
3156#ifdef FEAT_AUTOCMD
3157 aco_save_T aco;
3158#else
3159 buf_T *old_curbuf = curbuf;
3160#endif
3161
3162 /* Allocate a buffer without putting it in the buffer list. */
3163 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3164 if (newbuf == NULL)
3165 return NULL;
3166
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003167 /* Init the options. */
3168 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3169
Bram Moolenaar81695252004-12-29 20:58:21 +00003170#ifdef FEAT_AUTOCMD
3171 /* set curwin/curbuf to buf and save a few things */
3172 aucmd_prepbuf(&aco, newbuf);
3173#else
3174 curbuf = newbuf;
3175 curwin->w_buffer = newbuf;
3176#endif
3177
3178 /* Need to set the filename for autocommands. */
3179 (void)setfname(curbuf, fname, NULL, FALSE);
3180
Bram Moolenaar4770d092006-01-12 23:22:24 +00003181 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003182 {
3183 /* Create swap file now to avoid the ATTENTION message. */
3184 check_need_swap(TRUE);
3185
3186 /* Remove the "dummy" flag, otherwise autocommands may not
3187 * work. */
3188 curbuf->b_flags &= ~BF_DUMMY;
3189
3190 if (readfile(fname, NULL,
3191 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3192 NULL, READ_NEW | READ_DUMMY) == OK
3193 && !(curbuf->b_flags & BF_NEW))
3194 {
3195 failed = FALSE;
3196 if (curbuf != newbuf)
3197 {
3198 /* Bloody autocommands changed the buffer! */
3199 if (buf_valid(newbuf))
3200 wipe_buffer(newbuf, FALSE);
3201 newbuf = curbuf;
3202 }
3203 }
3204 }
3205
3206#ifdef FEAT_AUTOCMD
3207 /* restore curwin/curbuf and a few other things */
3208 aucmd_restbuf(&aco);
3209#else
3210 curbuf = old_curbuf;
3211 curwin->w_buffer = old_curbuf;
3212#endif
3213
3214 if (!buf_valid(newbuf))
3215 return NULL;
3216 if (failed)
3217 {
3218 wipe_dummy_buffer(newbuf);
3219 return NULL;
3220 }
3221 return newbuf;
3222}
3223
3224/*
3225 * Wipe out the dummy buffer that load_dummy_buffer() created.
3226 */
3227 static void
3228wipe_dummy_buffer(buf)
3229 buf_T *buf;
3230{
3231 if (curbuf != buf) /* safety check */
3232 wipe_buffer(buf, FALSE);
3233}
3234
3235/*
3236 * Unload the dummy buffer that load_dummy_buffer() created.
3237 */
3238 static void
3239unload_dummy_buffer(buf)
3240 buf_T *buf;
3241{
3242 if (curbuf != buf) /* safety check */
3243 close_buffer(NULL, buf, DOBUF_UNLOAD);
3244}
3245
Bram Moolenaar05159a02005-02-26 23:04:13 +00003246#if defined(FEAT_EVAL) || defined(PROTO)
3247/*
3248 * Add each quickfix error to list "list" as a dictionary.
3249 */
3250 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003251get_errorlist(wp, list)
3252 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003253 list_T *list;
3254{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003255 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003256 dict_T *dict;
3257 char_u buf[2];
3258 qfline_T *qfp;
3259 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003260
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003261 if (wp != NULL)
3262 {
3263 qi = GET_LOC_LIST(wp);
3264 if (qi == NULL)
3265 return FAIL;
3266 }
3267
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003268 if (qi->qf_curlist >= qi->qf_listcount
3269 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003270 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003271
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003272 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3273 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003274 {
3275 if ((dict = dict_alloc()) == NULL)
3276 return FAIL;
3277 if (list_append_dict(list, dict) == FAIL)
3278 return FAIL;
3279
3280 buf[0] = qfp->qf_type;
3281 buf[1] = NUL;
3282 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
3283 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3284 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3285 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3286 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003287 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003288 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
3289 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3290 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3291 return FAIL;
3292
3293 qfp = qfp->qf_next;
3294 }
3295 return OK;
3296}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003297
3298/*
3299 * Populate the quickfix list with the items supplied in the list
3300 * of dictionaries.
3301 */
3302 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003303set_errorlist(wp, list, action)
3304 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003305 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003306 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003307{
3308 listitem_T *li;
3309 dict_T *d;
3310 char_u *filename, *pattern, *text, *type;
3311 long lnum;
3312 int col, nr;
3313 int vcol;
3314 qfline_T *prevp = NULL;
3315 int valid, status;
3316 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003317 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003318
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003319 if (wp != NULL)
3320 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003321 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003322 if (qi == NULL)
3323 return FAIL;
3324 }
3325
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003326 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003327 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003328 qf_new_list(qi);
3329 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003330 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003331 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003332 prevp->qf_next != prevp; prevp = prevp->qf_next)
3333 ;
3334 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003335 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003336
3337 for (li = list->lv_first; li != NULL; li = li->li_next)
3338 {
3339 if (li->li_tv.v_type != VAR_DICT)
3340 continue; /* Skip non-dict items */
3341
3342 d = li->li_tv.vval.v_dict;
3343 if (d == NULL)
3344 continue;
3345
3346 filename = get_dict_string(d, (char_u *)"filename");
3347 lnum = get_dict_number(d, (char_u *)"lnum");
3348 col = get_dict_number(d, (char_u *)"col");
3349 vcol = get_dict_number(d, (char_u *)"vcol");
3350 nr = get_dict_number(d, (char_u *)"nr");
3351 type = get_dict_string(d, (char_u *)"type");
3352 pattern = get_dict_string(d, (char_u *)"pattern");
3353 text = get_dict_string(d, (char_u *)"text");
3354 if (text == NULL)
3355 text = vim_strsave((char_u *)"");
3356
3357 valid = TRUE;
3358 if (filename == NULL || (lnum == 0 && pattern == NULL))
3359 valid = FALSE;
3360
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003361 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003362 NULL, /* dir */
3363 filename,
3364 text,
3365 lnum,
3366 col,
3367 vcol, /* vis_col */
3368 pattern, /* search pattern */
3369 nr,
3370 type == NULL ? NUL : *type,
3371 valid);
3372
3373 vim_free(filename);
3374 vim_free(pattern);
3375 vim_free(text);
3376 vim_free(type);
3377
3378 if (status == FAIL)
3379 {
3380 retval = FAIL;
3381 break;
3382 }
3383 }
3384
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003385 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3386 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3387 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003388
3389#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003390 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003391#endif
3392
3393 return retval;
3394}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003395#endif
3396
Bram Moolenaar81695252004-12-29 20:58:21 +00003397/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003398 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003399 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003400 */
3401 void
3402ex_cbuffer(eap)
3403 exarg_T *eap;
3404{
3405 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003406 qf_info_T *qi = &ql_info;
3407
3408 if (eap->cmdidx == CMD_lbuffer)
3409 {
3410 qi = ll_get_or_alloc_list(curwin);
3411 if (qi == NULL)
3412 return;
3413 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003414
3415 if (*eap->arg == NUL)
3416 buf = curbuf;
3417 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3418 buf = buflist_findnr(atoi((char *)eap->arg));
3419 if (buf == NULL)
3420 EMSG(_(e_invarg));
3421 else if (buf->b_ml.ml_mfp == NULL)
3422 EMSG(_("E681: Buffer is not loaded"));
3423 else
3424 {
3425 if (eap->addr_count == 0)
3426 {
3427 eap->line1 = 1;
3428 eap->line2 = buf->b_ml.ml_line_count;
3429 }
3430 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3431 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3432 EMSG(_(e_invrange));
3433 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003434 qf_init_ext(qi, NULL, buf, NULL, p_efm, TRUE, eap->line1, eap->line2);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003435 }
3436}
3437
Bram Moolenaar1e015462005-09-25 22:16:38 +00003438#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003439/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003440 * ":cexpr {expr}" and ":caddexpr {expr}" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003441 * ":lexpr {expr}" and ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003442 */
3443 void
3444ex_cexpr(eap)
3445 exarg_T *eap;
3446{
3447 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003448 qf_info_T *qi = &ql_info;
3449 win_T *wp = NULL;
3450
3451 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_laddexpr)
3452 {
3453 qi = ll_get_or_alloc_list(curwin);
3454 if (qi == NULL)
3455 return;
3456 wp = curwin;
3457 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003458
Bram Moolenaar4770d092006-01-12 23:22:24 +00003459 /* Evaluate the expression. When the result is a string or a list we can
3460 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003461 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003462 if (tv != NULL)
3463 {
3464 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3465 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3466 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003467 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3468 (eap->cmdidx == CMD_cexpr
3469 || eap->cmdidx == CMD_lexpr),
Bram Moolenaar4770d092006-01-12 23:22:24 +00003470 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003471 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr))
3472 qf_jump(wp, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003473 }
3474 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003475 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003476 free_tv(tv);
3477 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003478}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003479#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003480
3481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * ":helpgrep {pattern}"
3483 */
3484 void
3485ex_helpgrep(eap)
3486 exarg_T *eap;
3487{
3488 regmatch_T regmatch;
3489 char_u *save_cpo;
3490 char_u *p;
3491 int fcount;
3492 char_u **fnames;
3493 FILE *fd;
3494 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003495 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003497#ifdef FEAT_MULTI_LANG
3498 char_u *lang;
3499#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003500 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3503 save_cpo = p_cpo;
3504 p_cpo = (char_u *)"";
3505
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003506#ifdef FEAT_MULTI_LANG
3507 /* Check for a specified language */
3508 lang = check_help_lang(eap->arg);
3509#endif
3510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3512 regmatch.rm_ic = FALSE;
3513 if (regmatch.regprog != NULL)
3514 {
3515 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003516 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
3518 /* Go through all directories in 'runtimepath' */
3519 p = p_rtp;
3520 while (*p != NUL && !got_int)
3521 {
3522 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3523
3524 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3525 add_pathsep(NameBuff);
3526 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3527 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3528 &fnames, EW_FILE|EW_SILENT) == OK
3529 && fcount > 0)
3530 {
3531 for (fi = 0; fi < fcount && !got_int; ++fi)
3532 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003533#ifdef FEAT_MULTI_LANG
3534 /* Skip files for a different language. */
3535 if (lang != NULL
3536 && STRNICMP(lang, fnames[fi]
3537 + STRLEN(fnames[fi]) - 3, 2) != 0
3538 && !(STRNICMP(lang, "en", 2) == 0
3539 && STRNICMP("txt", fnames[fi]
3540 + STRLEN(fnames[fi]) - 3, 3) == 0))
3541 continue;
3542#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003543 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (fd != NULL)
3545 {
3546 lnum = 1;
3547 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3548 {
3549 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3550 {
3551 int l = STRLEN(IObuff);
3552
3553 /* remove trailing CR, LF, spaces, etc. */
3554 while (l > 0 && IObuff[l - 1] <= ' ')
3555 IObuff[--l] = NUL;
3556
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003557 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 NULL, /* dir */
3559 fnames[fi],
3560 IObuff,
3561 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003562 (int)(regmatch.startp[0] - IObuff)
3563 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003564 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003565 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 0, /* nr */
3567 1, /* type */
3568 TRUE /* valid */
3569 ) == FAIL)
3570 {
3571 got_int = TRUE;
3572 break;
3573 }
3574 }
3575 ++lnum;
3576 line_breakcheck();
3577 }
3578 fclose(fd);
3579 }
3580 }
3581 FreeWild(fcount, fnames);
3582 }
3583 }
3584 vim_free(regmatch.regprog);
3585
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003586 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3587 qi->qf_lists[qi->qf_curlist].qf_ptr =
3588 qi->qf_lists[qi->qf_curlist].qf_start;
3589 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591
3592 p_cpo = save_cpo;
3593
3594#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003595 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596#endif
3597
3598 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003599 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3600 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003601 else
3602 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603}
3604
3605#endif /* FEAT_QUICKFIX */