blob: 6268ec66f3f2d51f7ef16557ff2760e7237d84bc [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
55 qfline_T *qf_ptr; /* pointer to the current error */
56 int qf_count; /* number of errors (0 means no error list) */
57 int qf_index; /* current index in the error list */
58 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000059} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061struct qf_info_S
62{
63 /*
64 * Count of references to this list. Used only for location lists.
65 * When a location list window reference this list, qf_refcount
66 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
67 * reaches 0, the list is freed.
68 */
69 int qf_refcount;
70 int qf_listcount; /* current number of lists */
71 int qf_curlist; /* current error list */
72 qf_list_T qf_lists[LISTCOUNT];
73};
74
75static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar68b76a62005-03-25 21:53:48 +000077#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79/*
80 * Structure used to hold the info of one part of 'errorformat'
81 */
Bram Moolenaar01265852006-03-20 21:50:15 +000082typedef struct efm_S efm_T;
83struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000086 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
88 char_u prefix; /* prefix of this format line: */
89 /* 'D' enter directory */
90 /* 'X' leave directory */
91 /* 'A' start of multi-line message */
92 /* 'E' error message */
93 /* 'W' warning message */
94 /* 'I' informational message */
95 /* 'C' continuation line */
96 /* 'Z' end of multi-line message */
97 /* 'G' general, unspecific message */
98 /* 'P' push file (partial) message */
99 /* 'Q' pop/quit file (partial) message */
100 /* 'O' overread (partial) message */
101 char_u flags; /* additional flags given in prefix */
102 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000103 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000104 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105};
106
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000107static int qf_init_ext __ARGS((qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast));
108static void qf_new_list __ARGS((qf_info_T *qi));
109static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
110static void qf_msg __ARGS((qf_info_T *qi));
111static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112static char_u *qf_types __ARGS((int, int));
113static int qf_get_fnum __ARGS((char_u *, char_u *));
114static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
115static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
116static char_u *qf_guess_filepath __ARGS((char_u *));
117static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
118static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
119#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000120static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000121static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000122static win_T *qf_find_win __ARGS((qf_info_T *qi));
123static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
124static void qf_update_buffer __ARGS((qf_info_T *qi));
125static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000128static buf_T *load_dummy_buffer __ARGS((char_u *fname));
129static void wipe_dummy_buffer __ARGS((buf_T *buf));
130static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000131static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000133/* Quickfix window check helper macro */
134#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
135/* Location list window check helper macro */
136#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
137/*
138 * Return location list for window 'wp'
139 * For location list window, return the referenced location list
140 */
141#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000144 * Read the errorfile "efile" into memory, line by line, building the error
145 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 * Return -1 for error, number of errors for success.
147 */
148 int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000149qf_init(wp, efile, errorformat, newlist)
150 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 char_u *efile;
152 char_u *errorformat;
153 int newlist; /* TRUE: start a new error list */
154{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000155 qf_info_T *qi = &ql_info;
156
Bram Moolenaar86b68352004-12-27 21:59:20 +0000157 if (efile == NULL)
158 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000159
160 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000161 {
162 qi = ll_get_or_alloc_list(wp);
163 if (qi == NULL)
164 return FAIL;
165 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166
167 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +0000168 (linenr_T)0, (linenr_T)0);
169}
170
171/*
172 * Read the errorfile "efile" into memory, line by line, building the error
173 * list.
174 * Alternative: when "efile" is null read errors from buffer "buf".
175 * Always use 'errorformat' from "buf" if there is a local value.
176 * Then lnumfirst and lnumlast specify the range of lines to use.
177 * Return -1 for error, number of errors for success.
178 */
179 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000180qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
181 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182 char_u *efile;
183 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000184 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185 char_u *errorformat;
186 int newlist; /* TRUE: start a new error list */
187 linenr_T lnumfirst; /* first line number to use */
188 linenr_T lnumlast; /* last line number to use */
189{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 char_u *namebuf;
191 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000192 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 char_u *fmtstr = NULL;
194 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000195 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 int type = 0;
197 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000198 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 long lnum = 0L;
200 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000201 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000202 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000204 efm_T *fmt_first = NULL;
205 efm_T *fmt_last = NULL;
206 efm_T *fmt_ptr;
207 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 char_u *efm;
209 char_u *ptr;
210 char_u *srcptr;
211 int len;
212 int i;
213 int round;
214 int idx = 0;
215 int multiline = FALSE;
216 int multiignore = FALSE;
217 int multiscan = FALSE;
218 int retval = -1; /* default: return error flag */
219 char_u *directory = NULL;
220 char_u *currfile = NULL;
221 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000222 char_u *p_str = NULL;
223 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 struct dir_stack_T *file_stack = NULL;
225 regmatch_T regmatch;
226 static struct fmtpattern
227 {
228 char_u convchar;
229 char *pattern;
230 } fmt_pat[FMT_PATTERNS] =
231 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000232 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {'n', "\\d\\+"},
234 {'l', "\\d\\+"},
235 {'c', "\\d\\+"},
236 {'t', "."},
237 {'m', ".\\+"},
238 {'r', ".*"},
239 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000240 {'v', "\\d\\+"},
241 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 };
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 namebuf = alloc(CMDBUFFSIZE + 1);
245 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000246 pattern = alloc(CMDBUFFSIZE + 1);
247 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 goto qf_init_end;
249
Bram Moolenaar86b68352004-12-27 21:59:20 +0000250 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 {
252 EMSG2(_(e_openerrf), efile);
253 goto qf_init_end;
254 }
255
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000256 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000258 qf_new_list(qi);
259 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000261 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
263 ;
264
265/*
266 * Each part of the format string is copied and modified from errorformat to
267 * regex prog. Only a few % characters are allowed.
268 */
269 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000270 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000271 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 else
273 efm = errorformat;
274 /*
275 * Get some space to modify the format string into.
276 */
277 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
278 for (round = FMT_PATTERNS; round > 0; )
279 i += (int)STRLEN(fmt_pat[--round].pattern);
280#ifdef COLON_IN_FILENAME
281 i += 12; /* "%f" can become twelve chars longer */
282#else
283 i += 2; /* "%f" can become two chars longer */
284#endif
285 if ((fmtstr = alloc(i)) == NULL)
286 goto error2;
287
Bram Moolenaar01265852006-03-20 21:50:15 +0000288 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {
290 /*
291 * Allocate a new eformat structure and put it at the end of the list
292 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000293 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 if (fmt_ptr == NULL)
295 goto error2;
296 if (fmt_first == NULL) /* first one */
297 fmt_first = fmt_ptr;
298 else
299 fmt_last->next = fmt_ptr;
300 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302 /*
303 * Isolate one part in the 'errorformat' option
304 */
305 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
306 if (efm[len] == '\\' && efm[len + 1] != NUL)
307 ++len;
308
309 /*
310 * Build regexp pattern from current 'errorformat' option
311 */
312 ptr = fmtstr;
313 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000314 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 for (efmp = efm; efmp < efm + len; ++efmp)
316 {
317 if (*efmp == '%')
318 {
319 ++efmp;
320 for (idx = 0; idx < FMT_PATTERNS; ++idx)
321 if (fmt_pat[idx].convchar == *efmp)
322 break;
323 if (idx < FMT_PATTERNS)
324 {
325 if (fmt_ptr->addr[idx])
326 {
327 sprintf((char *)errmsg,
328 _("E372: Too many %%%c in format string"), *efmp);
329 EMSG(errmsg);
330 goto error2;
331 }
332 if ((idx
333 && idx < 6
334 && vim_strchr((char_u *)"DXOPQ",
335 fmt_ptr->prefix) != NULL)
336 || (idx == 6
337 && vim_strchr((char_u *)"OPQ",
338 fmt_ptr->prefix) == NULL))
339 {
340 sprintf((char *)errmsg,
341 _("E373: Unexpected %%%c in format string"), *efmp);
342 EMSG(errmsg);
343 goto error2;
344 }
345 fmt_ptr->addr[idx] = (char_u)++round;
346 *ptr++ = '\\';
347 *ptr++ = '(';
348#ifdef BACKSLASH_IN_FILENAME
349 if (*efmp == 'f')
350 {
351 /* Also match "c:" in the file name, even when
352 * checking for a colon next: "%f:".
353 * "\%(\a:\)\=" */
354 STRCPY(ptr, "\\%(\\a:\\)\\=");
355 ptr += 10;
356 }
357#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000358 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000360 if (efmp[1] != '\\' && efmp[1] != '%')
361 {
362 /* A file name may contain spaces, but this isn't
363 * in "\f". For "%f:%l:%m" there may be a ":" in
364 * the file name. Use ".\{-1,}x" instead (x is
365 * the next character), the requirement that :999:
366 * follows should work. */
367 STRCPY(ptr, ".\\{-1,}");
368 ptr += 7;
369 }
370 else
371 {
372 /* File name followed by '\\' or '%': include as
373 * many file name chars as possible. */
374 STRCPY(ptr, "\\f\\+");
375 ptr += 4;
376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 }
378 else
379 {
380 srcptr = (char_u *)fmt_pat[idx].pattern;
381 while ((*ptr = *srcptr++) != NUL)
382 ++ptr;
383 }
384 *ptr++ = '\\';
385 *ptr++ = ')';
386 }
387 else if (*efmp == '*')
388 {
389 if (*++efmp == '[' || *efmp == '\\')
390 {
391 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
392 {
393 if (efmp[1] == '^')
394 *ptr++ = *++efmp;
395 if (efmp < efm + len)
396 {
397 *ptr++ = *++efmp; /* could be ']' */
398 while (efmp < efm + len
399 && (*ptr++ = *++efmp) != ']')
400 /* skip */;
401 if (efmp == efm + len)
402 {
403 EMSG(_("E374: Missing ] in format string"));
404 goto error2;
405 }
406 }
407 }
408 else if (efmp < efm + len) /* %*\D, %*\s etc. */
409 *ptr++ = *++efmp;
410 *ptr++ = '\\';
411 *ptr++ = '+';
412 }
413 else
414 {
415 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
416 sprintf((char *)errmsg,
417 _("E375: Unsupported %%%c in format string"), *efmp);
418 EMSG(errmsg);
419 goto error2;
420 }
421 }
422 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
423 *ptr++ = *efmp; /* regexp magic characters */
424 else if (*efmp == '#')
425 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000426 else if (*efmp == '>')
427 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 else if (efmp == efm + 1) /* analyse prefix */
429 {
430 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
431 fmt_ptr->flags = *efmp++;
432 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
433 fmt_ptr->prefix = *efmp;
434 else
435 {
436 sprintf((char *)errmsg,
437 _("E376: Invalid %%%c in format string prefix"), *efmp);
438 EMSG(errmsg);
439 goto error2;
440 }
441 }
442 else
443 {
444 sprintf((char *)errmsg,
445 _("E377: Invalid %%%c in format string"), *efmp);
446 EMSG(errmsg);
447 goto error2;
448 }
449 }
450 else /* copy normal character */
451 {
452 if (*efmp == '\\' && efmp + 1 < efm + len)
453 ++efmp;
454 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
455 *ptr++ = '\\'; /* escape regexp atoms */
456 if (*efmp)
457 *ptr++ = *efmp;
458 }
459 }
460 *ptr++ = '$';
461 *ptr = NUL;
462 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
463 goto error2;
464 /*
465 * Advance to next part
466 */
467 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
468 }
469 if (fmt_first == NULL) /* nothing found */
470 {
471 EMSG(_("E378: 'errorformat' contains no pattern"));
472 goto error2;
473 }
474
475 /*
476 * got_int is reset here, because it was probably set when killing the
477 * ":make" command, but we still want to read the errorfile then.
478 */
479 got_int = FALSE;
480
481 /* Always ignore case when looking for a matching error. */
482 regmatch.rm_ic = TRUE;
483
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000484 if (tv != NULL)
485 {
486 if (tv->v_type == VAR_STRING)
487 p_str = tv->vval.v_string;
488 else if (tv->v_type == VAR_LIST)
489 p_li = tv->vval.v_list->lv_first;
490 }
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 /*
493 * Read the lines in the error file one by one.
494 * Try to recognize one of the error formats in each line.
495 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000496 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000498 /* Get the next line. */
499 if (fd == NULL)
500 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000501 if (tv != NULL)
502 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000503 if (tv->v_type == VAR_STRING)
504 {
505 /* Get the next line from the supplied string */
506 char_u *p;
507
508 if (!*p_str) /* Reached the end of the string */
509 break;
510
511 p = vim_strchr(p_str, '\n');
512 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000513 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000514 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000515 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000516
517 if (len > CMDBUFFSIZE - 2)
518 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
519 else
520 vim_strncpy(IObuff, p_str, len);
521
522 p_str += len;
523 }
524 else if (tv->v_type == VAR_LIST)
525 {
526 /* Get the next line from the supplied list */
527 while (p_li && p_li->li_tv.v_type != VAR_STRING)
528 p_li = p_li->li_next; /* Skip non-string items */
529
530 if (!p_li) /* End of the list */
531 break;
532
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000533 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000534 if (len > CMDBUFFSIZE - 2)
535 len = CMDBUFFSIZE - 2;
536
537 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
538
539 p_li = p_li->li_next; /* next item */
540 }
541 }
542 else
543 {
544 /* Get the next line from the supplied buffer */
545 if (buflnum > lnumlast)
546 break;
547 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
548 CMDBUFFSIZE - 2);
549 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000550 }
551 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
552 break;
553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
555 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
556 *efmp = NUL;
557#ifdef USE_CRNL
558 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
559 *efmp = NUL;
560#endif
561
Bram Moolenaar01265852006-03-20 21:50:15 +0000562 /* If there was no %> item start at the first pattern */
563 if (fmt_start == NULL)
564 fmt_ptr = fmt_first;
565 else
566 {
567 fmt_ptr = fmt_start;
568 fmt_start = NULL;
569 }
570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 /*
572 * Try to match each part of 'errorformat' until we find a complete
573 * match or no match.
574 */
575 valid = TRUE;
576restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000577 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 idx = fmt_ptr->prefix;
580 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
581 continue;
582 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000583 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (!multiscan)
585 errmsg[0] = NUL;
586 lnum = 0;
587 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000588 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 enr = -1;
590 type = 0;
591 tail = NULL;
592
593 regmatch.regprog = fmt_ptr->prog;
594 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
595 {
596 if ((idx == 'C' || idx == 'Z') && !multiline)
597 continue;
598 if (vim_strchr((char_u *)"EWI", idx) != NULL)
599 type = idx;
600 else
601 type = 0;
602 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000603 * Extract error message data from matched line.
604 * We check for an actual submatch, because "\[" and "\]" in
605 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 */
607 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
608 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000609 int c;
610
611 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
612 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000613
614 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000615 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000616 *regmatch.endp[i] = NUL;
617 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
618 *regmatch.endp[i] = c;
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000621 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 continue;
623 }
624 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000625 {
626 if (regmatch.startp[i] == NULL)
627 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000631 {
632 if (regmatch.startp[i] == NULL)
633 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000637 {
638 if (regmatch.startp[i] == NULL)
639 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000643 {
644 if (regmatch.startp[i] == NULL)
645 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000647 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000648 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 STRCPY(errmsg, IObuff);
650 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
651 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000652 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
653 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000655 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000658 {
659 if (regmatch.startp[i] == NULL)
660 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
664 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000665 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
666 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
668 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000669 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
672 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000673 if (regmatch.startp[i] == NULL)
674 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000676 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000678 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
679 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000680 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
681 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000682 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
683 if (len > CMDBUFFSIZE - 5)
684 len = CMDBUFFSIZE - 5;
685 STRCPY(pattern, "^\\V");
686 STRNCAT(pattern, regmatch.startp[i], len);
687 pattern[len + 3] = '\\';
688 pattern[len + 4] = '$';
689 pattern[len + 5] = NUL;
690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 break;
692 }
693 }
694 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000695
Bram Moolenaar4770d092006-01-12 23:22:24 +0000696 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000698 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
700 if (idx == 'D') /* enter directory */
701 {
702 if (*namebuf == NUL)
703 {
704 EMSG(_("E379: Missing or empty directory name"));
705 goto error2;
706 }
707 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
708 goto error2;
709 }
710 else if (idx == 'X') /* leave directory */
711 directory = qf_pop_dir(&dir_stack);
712 }
713 namebuf[0] = NUL; /* no match found, remove file name */
714 lnum = 0; /* don't jump to this line */
715 valid = FALSE;
716 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000717 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 multiline = multiignore = FALSE;
719 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000720 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000722 /* honor %> item */
723 if (fmt_ptr->conthere)
724 fmt_start = fmt_ptr;
725
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
727 multiline = TRUE; /* start of a multi-line message */
728 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
729 { /* continuation of multi-line msg */
730 if (qfprev == NULL)
731 goto error2;
732 if (*errmsg && !multiignore)
733 {
734 len = (int)STRLEN(qfprev->qf_text);
735 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
736 == NULL)
737 goto error2;
738 STRCPY(ptr, qfprev->qf_text);
739 vim_free(qfprev->qf_text);
740 qfprev->qf_text = ptr;
741 *(ptr += len) = '\n';
742 STRCPY(++ptr, errmsg);
743 }
744 if (qfprev->qf_nr == -1)
745 qfprev->qf_nr = enr;
746 if (vim_isprintc(type) && !qfprev->qf_type)
747 qfprev->qf_type = type; /* only printable chars allowed */
748 if (!qfprev->qf_lnum)
749 qfprev->qf_lnum = lnum;
750 if (!qfprev->qf_col)
751 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000752 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (!qfprev->qf_fnum)
754 qfprev->qf_fnum = qf_get_fnum(directory,
755 *namebuf || directory ? namebuf
756 : currfile && valid ? currfile : 0);
757 if (idx == 'Z')
758 multiline = multiignore = FALSE;
759 line_breakcheck();
760 continue;
761 }
762 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
763 {
764 /* global file names */
765 valid = FALSE;
766 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
767 {
768 if (*namebuf && idx == 'P')
769 currfile = qf_push_dir(namebuf, &file_stack);
770 else if (idx == 'Q')
771 currfile = qf_pop_dir(&file_stack);
772 *namebuf = NUL;
773 if (tail && *tail)
774 {
775 STRCPY(IObuff, skipwhite(tail));
776 multiscan = TRUE;
777 goto restofline;
778 }
779 }
780 }
781 if (fmt_ptr->flags == '-') /* generally exclude this line */
782 {
783 if (multiline)
784 multiignore = TRUE; /* also exclude continuation lines */
785 continue;
786 }
787 }
788
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000789 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000791 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000793 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 errmsg,
795 lnum,
796 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000798 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 enr,
800 type,
801 valid) == FAIL)
802 goto error2;
803 line_breakcheck();
804 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000805 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000807 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000809 /* no valid entry found */
810 qi->qf_lists[qi->qf_curlist].qf_ptr =
811 qi->qf_lists[qi->qf_curlist].qf_start;
812 qi->qf_lists[qi->qf_curlist].qf_index = 1;
813 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
815 else
816 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000817 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
818 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
819 qi->qf_lists[qi->qf_curlist].qf_ptr =
820 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000822 /* return number of matches */
823 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 goto qf_init_ok;
825 }
826 EMSG(_(e_readerrf));
827error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000828 qf_free(qi, qi->qf_curlist);
829 qi->qf_listcount--;
830 if (qi->qf_curlist > 0)
831 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000833 if (fd != NULL)
834 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
836 {
837 fmt_first = fmt_ptr->next;
838 vim_free(fmt_ptr->prog);
839 vim_free(fmt_ptr);
840 }
841 qf_clean_dir_stack(&dir_stack);
842 qf_clean_dir_stack(&file_stack);
843qf_init_end:
844 vim_free(namebuf);
845 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000846 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 vim_free(fmtstr);
848
849#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000850 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851#endif
852
853 return retval;
854}
855
856/*
857 * Prepare for adding a new quickfix list.
858 */
859 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000860qf_new_list(qi)
861 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 int i;
864
865 /*
866 * If the current entry is not the last entry, delete entries below
867 * the current entry. This makes it possible to browse in a tree-like
868 * way with ":grep'.
869 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000870 while (qi->qf_listcount > qi->qf_curlist + 1)
871 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 /*
874 * When the stack is full, remove to oldest entry
875 * Otherwise, add a new entry.
876 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000877 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000879 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000881 qi->qf_lists[i - 1] = qi->qf_lists[i];
882 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000885 qi->qf_curlist = qi->qf_listcount++;
886 qi->qf_lists[qi->qf_curlist].qf_index = 0;
887 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888}
889
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000890/*
891 * Free a location list
892 */
893 static void
894ll_free_all(pqi)
895 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000896{
897 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000898 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000899
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000900 qi = *pqi;
901 if (qi == NULL)
902 return;
903 *pqi = NULL; /* Remove reference to this list */
904
905 qi->qf_refcount--;
906 if (qi->qf_refcount < 1)
907 {
908 /* No references to this location list */
909 for (i = 0; i < qi->qf_listcount; ++i)
910 qf_free(qi, i);
911 vim_free(qi);
912 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000913}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000914
915 void
916qf_free_all(wp)
917 win_T *wp;
918{
919 int i;
920 qf_info_T *qi = &ql_info;
921
922 if (wp != NULL)
923 {
924 /* location list */
925 ll_free_all(&wp->w_llist);
926 ll_free_all(&wp->w_llist_ref);
927 }
928 else
929 /* quickfix list */
930 for (i = 0; i < qi->qf_listcount; ++i)
931 qf_free(qi, i);
932}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000933
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934/*
935 * Add an entry to the end of the list of errors.
936 * Returns OK or FAIL.
937 */
938 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000939qf_add_entry(qi, prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000940 valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000941 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000942 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 char_u *dir; /* optional directory name */
944 char_u *fname; /* file name or NULL */
945 char_u *mesg; /* message */
946 long lnum; /* line number */
947 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000948 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000949 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 int nr; /* error number */
951 int type; /* type character */
952 int valid; /* valid entry */
953{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000954 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000956 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 return FAIL;
958 qfp->qf_fnum = qf_get_fnum(dir, fname);
959 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
960 {
961 vim_free(qfp);
962 return FAIL;
963 }
964 qfp->qf_lnum = lnum;
965 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000966 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000967 if (pattern == NULL || *pattern == NUL)
968 qfp->qf_pattern = NULL;
969 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
970 {
971 vim_free(qfp->qf_text);
972 vim_free(qfp);
973 return FAIL;
974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 qfp->qf_nr = nr;
976 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
977 type = 0;
978 qfp->qf_type = type;
979 qfp->qf_valid = valid;
980
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000981 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
982 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000984 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 qfp->qf_prev = qfp; /* first element points to itself */
986 }
987 else
988 {
989 qfp->qf_prev = *prevp;
990 (*prevp)->qf_next = qfp;
991 }
992 qfp->qf_next = qfp; /* last element points to itself */
993 qfp->qf_cleared = FALSE;
994 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000995 ++qi->qf_lists[qi->qf_curlist].qf_count;
996 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
997 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000999 qi->qf_lists[qi->qf_curlist].qf_index =
1000 qi->qf_lists[qi->qf_curlist].qf_count;
1001 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003
1004 return OK;
1005}
1006
1007/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001008 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001009 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001010 static qf_info_T *
1011ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001012{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001013 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001014
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001015 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1016 if (qi != NULL)
1017 {
1018 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1019 qi->qf_refcount++;
1020 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001021
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001022 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001023}
1024
1025/*
1026 * Return the location list for window 'wp'.
1027 * If not present, allocate a location list
1028 */
1029 static qf_info_T *
1030ll_get_or_alloc_list(wp)
1031 win_T *wp;
1032{
1033 if (IS_LL_WINDOW(wp))
1034 /* For a location list window, use the referenced location list */
1035 return wp->w_llist_ref;
1036
1037 /*
1038 * For a non-location list window, w_llist_ref should not point to a
1039 * location list.
1040 */
1041 ll_free_all(&wp->w_llist_ref);
1042
1043 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001044 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001045 return wp->w_llist;
1046}
1047
1048/*
1049 * Copy the location list from window "from" to window "to".
1050 */
1051 void
1052copy_loclist(from, to)
1053 win_T *from;
1054 win_T *to;
1055{
1056 qf_info_T *qi;
1057 int idx;
1058 int i;
1059
1060 /*
1061 * When copying from a location list window, copy the referenced
1062 * location list. For other windows, copy the location list for
1063 * that window.
1064 */
1065 if (IS_LL_WINDOW(from))
1066 qi = from->w_llist_ref;
1067 else
1068 qi = from->w_llist;
1069
1070 if (qi == NULL) /* no location list to copy */
1071 return;
1072
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001073 /* allocate a new location list */
1074 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001075 return;
1076
1077 to->w_llist->qf_listcount = qi->qf_listcount;
1078
1079 /* Copy the location lists one at a time */
1080 for (idx = 0; idx < qi->qf_listcount; idx++)
1081 {
1082 qf_list_T *from_qfl;
1083 qf_list_T *to_qfl;
1084
1085 to->w_llist->qf_curlist = idx;
1086
1087 from_qfl = &qi->qf_lists[idx];
1088 to_qfl = &to->w_llist->qf_lists[idx];
1089
1090 /* Some of the fields are populated by qf_add_entry() */
1091 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1092 to_qfl->qf_count = 0;
1093 to_qfl->qf_index = 0;
1094 to_qfl->qf_start = NULL;
1095 to_qfl->qf_ptr = NULL;
1096
1097 if (from_qfl->qf_count)
1098 {
1099 qfline_T *from_qfp;
1100 qfline_T *prevp = NULL;
1101
1102 /* copy all the location entries in this list */
1103 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1104 ++i, from_qfp = from_qfp->qf_next)
1105 {
1106 if (qf_add_entry(to->w_llist, &prevp,
1107 NULL,
1108 NULL,
1109 from_qfp->qf_text,
1110 from_qfp->qf_lnum,
1111 from_qfp->qf_col,
1112 from_qfp->qf_viscol,
1113 from_qfp->qf_pattern,
1114 from_qfp->qf_nr,
1115 0,
1116 from_qfp->qf_valid) == FAIL)
1117 {
1118 qf_free_all(to);
1119 return;
1120 }
1121 /*
1122 * qf_add_entry() will not set the qf_num field, as the
1123 * directory and file names are not supplied. So the qf_fnum
1124 * field is copied here.
1125 */
1126 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1127 prevp->qf_type = from_qfp->qf_type; /* error type */
1128 if (from_qfl->qf_ptr == from_qfp)
1129 to_qfl->qf_ptr = prevp; /* current location */
1130 }
1131 }
1132
1133 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1134
1135 /* When no valid entries are present in the list, qf_ptr points to
1136 * the first item in the list */
1137 if (to_qfl->qf_nonevalid == TRUE)
1138 to_qfl->qf_ptr = to_qfl->qf_start;
1139 }
1140
1141 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1142}
1143
1144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * get buffer number for file "dir.name"
1146 */
1147 static int
1148qf_get_fnum(directory, fname)
1149 char_u *directory;
1150 char_u *fname;
1151{
1152 if (fname == NULL || *fname == NUL) /* no file name */
1153 return 0;
1154 {
1155#ifdef RISCOS
1156 /* Name is reported as `main.c', but file is `c.main' */
1157 return ro_buflist_add(fname);
1158#else
1159 char_u *ptr;
1160 int fnum;
1161
1162# ifdef VMS
1163 vms_remove_version(fname);
1164# endif
1165# ifdef BACKSLASH_IN_FILENAME
1166 if (directory != NULL)
1167 slash_adjust(directory);
1168 slash_adjust(fname);
1169# endif
1170 if (directory != NULL && !vim_isAbsName(fname)
1171 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1172 {
1173 /*
1174 * Here we check if the file really exists.
1175 * This should normally be true, but if make works without
1176 * "leaving directory"-messages we might have missed a
1177 * directory change.
1178 */
1179 if (mch_getperm(ptr) < 0)
1180 {
1181 vim_free(ptr);
1182 directory = qf_guess_filepath(fname);
1183 if (directory)
1184 ptr = concat_fnames(directory, fname, TRUE);
1185 else
1186 ptr = vim_strsave(fname);
1187 }
1188 /* Use concatenated directory name and file name */
1189 fnum = buflist_add(ptr, 0);
1190 vim_free(ptr);
1191 return fnum;
1192 }
1193 return buflist_add(fname, 0);
1194#endif
1195 }
1196}
1197
1198/*
1199 * push dirbuf onto the directory stack and return pointer to actual dir or
1200 * NULL on error
1201 */
1202 static char_u *
1203qf_push_dir(dirbuf, stackptr)
1204 char_u *dirbuf;
1205 struct dir_stack_T **stackptr;
1206{
1207 struct dir_stack_T *ds_new;
1208 struct dir_stack_T *ds_ptr;
1209
1210 /* allocate new stack element and hook it in */
1211 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1212 if (ds_new == NULL)
1213 return NULL;
1214
1215 ds_new->next = *stackptr;
1216 *stackptr = ds_new;
1217
1218 /* store directory on the stack */
1219 if (vim_isAbsName(dirbuf)
1220 || (*stackptr)->next == NULL
1221 || (*stackptr && dir_stack != *stackptr))
1222 (*stackptr)->dirname = vim_strsave(dirbuf);
1223 else
1224 {
1225 /* Okay we don't have an absolute path.
1226 * dirbuf must be a subdir of one of the directories on the stack.
1227 * Let's search...
1228 */
1229 ds_new = (*stackptr)->next;
1230 (*stackptr)->dirname = NULL;
1231 while (ds_new)
1232 {
1233 vim_free((*stackptr)->dirname);
1234 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1235 TRUE);
1236 if (mch_isdir((*stackptr)->dirname) == TRUE)
1237 break;
1238
1239 ds_new = ds_new->next;
1240 }
1241
1242 /* clean up all dirs we already left */
1243 while ((*stackptr)->next != ds_new)
1244 {
1245 ds_ptr = (*stackptr)->next;
1246 (*stackptr)->next = (*stackptr)->next->next;
1247 vim_free(ds_ptr->dirname);
1248 vim_free(ds_ptr);
1249 }
1250
1251 /* Nothing found -> it must be on top level */
1252 if (ds_new == NULL)
1253 {
1254 vim_free((*stackptr)->dirname);
1255 (*stackptr)->dirname = vim_strsave(dirbuf);
1256 }
1257 }
1258
1259 if ((*stackptr)->dirname != NULL)
1260 return (*stackptr)->dirname;
1261 else
1262 {
1263 ds_ptr = *stackptr;
1264 *stackptr = (*stackptr)->next;
1265 vim_free(ds_ptr);
1266 return NULL;
1267 }
1268}
1269
1270
1271/*
1272 * pop dirbuf from the directory stack and return previous directory or NULL if
1273 * stack is empty
1274 */
1275 static char_u *
1276qf_pop_dir(stackptr)
1277 struct dir_stack_T **stackptr;
1278{
1279 struct dir_stack_T *ds_ptr;
1280
1281 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1282 * What to do if it isn't? */
1283
1284 /* pop top element and free it */
1285 if (*stackptr != NULL)
1286 {
1287 ds_ptr = *stackptr;
1288 *stackptr = (*stackptr)->next;
1289 vim_free(ds_ptr->dirname);
1290 vim_free(ds_ptr);
1291 }
1292
1293 /* return NEW top element as current dir or NULL if stack is empty*/
1294 return *stackptr ? (*stackptr)->dirname : NULL;
1295}
1296
1297/*
1298 * clean up directory stack
1299 */
1300 static void
1301qf_clean_dir_stack(stackptr)
1302 struct dir_stack_T **stackptr;
1303{
1304 struct dir_stack_T *ds_ptr;
1305
1306 while ((ds_ptr = *stackptr) != NULL)
1307 {
1308 *stackptr = (*stackptr)->next;
1309 vim_free(ds_ptr->dirname);
1310 vim_free(ds_ptr);
1311 }
1312}
1313
1314/*
1315 * Check in which directory of the directory stack the given file can be
1316 * found.
1317 * Returns a pointer to the directory name or NULL if not found
1318 * Cleans up intermediate directory entries.
1319 *
1320 * TODO: How to solve the following problem?
1321 * If we have the this directory tree:
1322 * ./
1323 * ./aa
1324 * ./aa/bb
1325 * ./bb
1326 * ./bb/x.c
1327 * and make says:
1328 * making all in aa
1329 * making all in bb
1330 * x.c:9: Error
1331 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1332 * qf_guess_filepath will return NULL.
1333 */
1334 static char_u *
1335qf_guess_filepath(filename)
1336 char_u *filename;
1337{
1338 struct dir_stack_T *ds_ptr;
1339 struct dir_stack_T *ds_tmp;
1340 char_u *fullname;
1341
1342 /* no dirs on the stack - there's nothing we can do */
1343 if (dir_stack == NULL)
1344 return NULL;
1345
1346 ds_ptr = dir_stack->next;
1347 fullname = NULL;
1348 while (ds_ptr)
1349 {
1350 vim_free(fullname);
1351 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1352
1353 /* If concat_fnames failed, just go on. The worst thing that can happen
1354 * is that we delete the entire stack.
1355 */
1356 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1357 break;
1358
1359 ds_ptr = ds_ptr->next;
1360 }
1361
1362 vim_free(fullname);
1363
1364 /* clean up all dirs we already left */
1365 while (dir_stack->next != ds_ptr)
1366 {
1367 ds_tmp = dir_stack->next;
1368 dir_stack->next = dir_stack->next->next;
1369 vim_free(ds_tmp->dirname);
1370 vim_free(ds_tmp);
1371 }
1372
1373 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1374
1375}
1376
1377/*
1378 * jump to a quickfix line
1379 * if dir == FORWARD go "errornr" valid entries forward
1380 * if dir == BACKWARD go "errornr" valid entries backward
1381 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1382 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1383 * else if "errornr" is zero, redisplay the same line
1384 * else go to entry "errornr"
1385 */
1386 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001387qf_jump(qi, dir, errornr, forceit)
1388 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 int dir;
1390 int errornr;
1391 int forceit;
1392{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001394 qfline_T *qf_ptr;
1395 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 int qf_index;
1397 int old_qf_fnum;
1398 int old_qf_index;
1399 int prev_index;
1400 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1401 char_u *err = e_no_more_items;
1402 linenr_T i;
1403 buf_T *old_curbuf;
1404 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 colnr_T screen_col;
1406 colnr_T char_col;
1407 char_u *line;
1408#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001409 char_u *old_swb = p_swb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 int opened_window = FALSE;
1411 win_T *win;
1412 win_T *altwin;
1413#endif
1414 int print_message = TRUE;
1415 int len;
1416#ifdef FEAT_FOLDING
1417 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1418#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001419 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001420 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001422 if (qi == NULL)
1423 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001424
1425 if (qi->qf_curlist >= qi->qf_listcount
1426 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
1428 EMSG(_(e_quickfix));
1429 return;
1430 }
1431
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001432 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001434 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 old_qf_index = qf_index;
1436 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1437 {
1438 while (errornr--)
1439 {
1440 old_qf_ptr = qf_ptr;
1441 prev_index = qf_index;
1442 old_qf_fnum = qf_ptr->qf_fnum;
1443 do
1444 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001445 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 || qf_ptr->qf_next == NULL)
1447 {
1448 qf_ptr = old_qf_ptr;
1449 qf_index = prev_index;
1450 if (err != NULL)
1451 {
1452 EMSG(_(err));
1453 goto theend;
1454 }
1455 errornr = 0;
1456 break;
1457 }
1458 ++qf_index;
1459 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001460 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1461 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1463 err = NULL;
1464 }
1465 }
1466 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1467 {
1468 while (errornr--)
1469 {
1470 old_qf_ptr = qf_ptr;
1471 prev_index = qf_index;
1472 old_qf_fnum = qf_ptr->qf_fnum;
1473 do
1474 {
1475 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1476 {
1477 qf_ptr = old_qf_ptr;
1478 qf_index = prev_index;
1479 if (err != NULL)
1480 {
1481 EMSG(_(err));
1482 goto theend;
1483 }
1484 errornr = 0;
1485 break;
1486 }
1487 --qf_index;
1488 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001489 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1490 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1492 err = NULL;
1493 }
1494 }
1495 else if (errornr != 0) /* go to specified number */
1496 {
1497 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1498 {
1499 --qf_index;
1500 qf_ptr = qf_ptr->qf_prev;
1501 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001502 while (errornr > qf_index && qf_index <
1503 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 && qf_ptr->qf_next != NULL)
1505 {
1506 ++qf_index;
1507 qf_ptr = qf_ptr->qf_next;
1508 }
1509 }
1510
1511#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001512 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1513 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /* No need to print the error message if it's visible in the error
1515 * window */
1516 print_message = FALSE;
1517
1518 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001519 * For ":helpgrep" find a help window or open one.
1520 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001521 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001522 {
1523 win_T *wp;
1524 int n;
1525
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001526 if (cmdmod.tab != 0)
1527 wp = NULL;
1528 else
1529 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1530 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1531 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001532 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1533 win_enter(wp, TRUE);
1534 else
1535 {
1536 /*
1537 * Split off help window; put it at far top if no position
1538 * specified, the current window is vertically split and narrow.
1539 */
1540 n = WSP_HELP;
1541# ifdef FEAT_VERTSPLIT
1542 if (cmdmod.split == 0 && curwin->w_width != Columns
1543 && curwin->w_width < 80)
1544 n |= WSP_TOP;
1545# endif
1546 if (win_split(0, n) == FAIL)
1547 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001548 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001549
1550 if (curwin->w_height < p_hh)
1551 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001552
1553 if (qi != &ql_info) /* not a quickfix list */
1554 {
1555 /* The new window should use the supplied location list */
1556 qf_free_all(curwin);
1557 curwin->w_llist = qi;
1558 qi->qf_refcount++;
1559 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001560 }
1561
1562 if (!p_im)
1563 restart_edit = 0; /* don't want insert mode in help file */
1564 }
1565
1566 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 * If currently in the quickfix window, find another window to show the
1568 * file in.
1569 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001570 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
1572 /*
1573 * If there is no file specified, we don't know where to go.
1574 * But do advance, otherwise ":cn" gets stuck.
1575 */
1576 if (qf_ptr->qf_fnum == 0)
1577 goto theend;
1578
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001579 /* Locate a window showing a normal buffer */
1580 usable_win = 0;
1581 FOR_ALL_WINDOWS(win)
1582 if (win->w_buffer->b_p_bt[0] == NUL)
1583 {
1584 usable_win = 1;
1585 break;
1586 }
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 /*
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001589 * If no usable window is found and 'switchbuf' is set to 'usetab'
1590 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001592 if (!usable_win && vim_strchr(p_swb, 'a') != NULL)
1593 {
1594 tabpage_T *tp;
1595 win_T *wp;
1596
1597 FOR_ALL_TAB_WINDOWS(tp, wp)
1598 {
1599 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1600 {
1601 goto_tabpage_win(tp, wp);
1602 usable_win = 1;
1603 break;
1604 }
1605 }
1606 }
1607
1608 /*
1609 * If there is only one window and is the quickfix window, create a new
1610 * one above the quickfix window.
1611 */
1612 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001614 ll_ref = curwin->w_llist_ref;
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (win_split(0, WSP_ABOVE) == FAIL)
1617 goto failed; /* not enough room for window */
1618 opened_window = TRUE; /* close it when fail */
1619 p_swb = empty_option; /* don't split again */
1620# ifdef FEAT_SCROLLBIND
1621 curwin->w_p_scb = FALSE;
1622# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001623 if (ll_ref != NULL)
1624 {
1625 /* The new window should use the location list from the
1626 * location list window */
1627 qf_free_all(curwin);
1628 curwin->w_llist = ll_ref;
1629 ll_ref->qf_refcount++;
1630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
1632 else
1633 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001634 if (curwin->w_llist_ref != NULL)
1635 {
1636 /* In a location window */
1637 ll_ref = curwin->w_llist_ref;
1638
1639 /* Find the window with the same location list */
1640 FOR_ALL_WINDOWS(win)
1641 if (win->w_llist == ll_ref)
1642 break;
1643 if (win == NULL)
1644 {
1645 /* Find the window showing the selected file */
1646 FOR_ALL_WINDOWS(win)
1647 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1648 break;
1649 if (win == NULL)
1650 {
1651 /* Find a previous usable window */
1652 win = curwin;
1653 do
1654 {
1655 if (win->w_buffer->b_p_bt[0] == NUL)
1656 break;
1657 if (win->w_prev == NULL)
1658 win = lastwin; /* wrap around the top */
1659 else
1660 win = win->w_prev; /* go to previous window */
1661 } while (win != curwin);
1662 }
1663 }
1664 win_goto(win);
1665
1666 /* If the location list for the window is not set, then set it
1667 * to the location list from the location window */
1668 if (win->w_llist == NULL)
1669 {
1670 win->w_llist = ll_ref;
1671 ll_ref->qf_refcount++;
1672 }
1673 }
1674 else
1675 {
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 /*
1678 * Try to find a window that shows the right buffer.
1679 * Default to the window just above the quickfix buffer.
1680 */
1681 win = curwin;
1682 altwin = NULL;
1683 for (;;)
1684 {
1685 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1686 break;
1687 if (win->w_prev == NULL)
1688 win = lastwin; /* wrap around the top */
1689 else
1690 win = win->w_prev; /* go to previous window */
1691
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001692 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 /* Didn't find it, go to the window before the quickfix
1695 * window. */
1696 if (altwin != NULL)
1697 win = altwin;
1698 else if (curwin->w_prev != NULL)
1699 win = curwin->w_prev;
1700 else
1701 win = curwin->w_next;
1702 break;
1703 }
1704
1705 /* Remember a usable window. */
1706 if (altwin == NULL && !win->w_p_pvw
1707 && win->w_buffer->b_p_bt[0] == NUL)
1708 altwin = win;
1709 }
1710
1711 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 }
1715#endif
1716
1717 /*
1718 * If there is a file name,
1719 * read the wanted file if needed, and check autowrite etc.
1720 */
1721 old_curbuf = curbuf;
1722 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001723
1724 if (qf_ptr->qf_fnum != 0)
1725 {
1726 if (qf_ptr->qf_type == 1)
1727 {
1728 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1729 * set b_p_ro flag). */
1730 if (!can_abandon(curbuf, forceit))
1731 {
1732 EMSG(_(e_nowrtmsg));
1733 ok = FALSE;
1734 }
1735 else
1736 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1737 ECMD_HIDE + ECMD_SET_HELP);
1738 }
1739 else
1740 ok = buflist_getfile(qf_ptr->qf_fnum,
1741 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1742 }
1743
1744 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 {
1746 /* When not switched to another buffer, still need to set pc mark */
1747 if (curbuf == old_curbuf)
1748 setpcmark();
1749
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001750 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001752 /*
1753 * Go to line with error, unless qf_lnum is 0.
1754 */
1755 i = qf_ptr->qf_lnum;
1756 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001758 if (i > curbuf->b_ml.ml_line_count)
1759 i = curbuf->b_ml.ml_line_count;
1760 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001762 if (qf_ptr->qf_col > 0)
1763 {
1764 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1765 if (qf_ptr->qf_viscol == TRUE)
1766 {
1767 /*
1768 * Check each character from the beginning of the error
1769 * line up to the error column. For each tab character
1770 * found, reduce the error column value by the length of
1771 * a tab character.
1772 */
1773 line = ml_get_curline();
1774 screen_col = 0;
1775 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1776 {
1777 if (*line == NUL)
1778 break;
1779 if (*line++ == '\t')
1780 {
1781 curwin->w_cursor.col -= 7 - (screen_col % 8);
1782 screen_col += 8 - (screen_col % 8);
1783 }
1784 else
1785 ++screen_col;
1786 }
1787 }
1788 check_cursor();
1789 }
1790 else
1791 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
1793 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001794 {
1795 pos_T save_cursor;
1796
1797 /* Move the cursor to the first line in the buffer */
1798 save_cursor = curwin->w_cursor;
1799 curwin->w_cursor.lnum = 0;
1800 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1801 curwin->w_cursor = save_cursor;
1802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
1804#ifdef FEAT_FOLDING
1805 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1806 foldOpenCursor();
1807#endif
1808 if (print_message)
1809 {
1810 /* Update the screen before showing the message */
1811 update_topline_redraw();
1812 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001813 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1815 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1816 /* Add the message, skipping leading whitespace and newlines. */
1817 len = (int)STRLEN(IObuff);
1818 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1819
1820 /* Output the message. Overwrite to avoid scrolling when the 'O'
1821 * flag is present in 'shortmess'; But when not jumping, print the
1822 * whole message. */
1823 i = msg_scroll;
1824 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1825 msg_scroll = TRUE;
1826 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1827 msg_scroll = FALSE;
1828 msg_attr_keep(IObuff, 0, TRUE);
1829 msg_scroll = i;
1830 }
1831 }
1832 else
1833 {
1834#ifdef FEAT_WINDOWS
1835 if (opened_window)
1836 win_close(curwin, TRUE); /* Close opened window */
1837#endif
1838 if (qf_ptr->qf_fnum != 0)
1839 {
1840 /*
1841 * Couldn't open file, so put index back where it was. This could
1842 * happen if the file was readonly and we changed something.
1843 */
1844#ifdef FEAT_WINDOWS
1845failed:
1846#endif
1847 qf_ptr = old_qf_ptr;
1848 qf_index = old_qf_index;
1849 }
1850 }
1851theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001852 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1853 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#ifdef FEAT_WINDOWS
1855 if (p_swb != old_swb && opened_window)
1856 {
1857 /* Restore old 'switchbuf' value, but not when an autocommand or
1858 * modeline has changed the value. */
1859 if (p_swb == empty_option)
1860 p_swb = old_swb;
1861 else
1862 free_string_option(old_swb);
1863 }
1864#endif
1865}
1866
1867/*
1868 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
1871 void
1872qf_list(eap)
1873 exarg_T *eap;
1874{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001875 buf_T *buf;
1876 char_u *fname;
1877 qfline_T *qfp;
1878 int i;
1879 int idx1 = 1;
1880 int idx2 = -1;
1881 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001882 char_u *arg = eap->arg;
1883 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001885 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001887 if (eap->cmdidx == CMD_llist)
1888 {
1889 qi = GET_LOC_LIST(curwin);
1890 if (qi == NULL)
1891 {
1892 EMSG(_(e_loclist));
1893 return;
1894 }
1895 }
1896
1897 if (qi->qf_curlist >= qi->qf_listcount
1898 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
1900 EMSG(_(e_quickfix));
1901 return;
1902 }
1903 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1904 {
1905 EMSG(_(e_trailing));
1906 return;
1907 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001908 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (idx1 < 0)
1910 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1911 if (idx2 < 0)
1912 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1913
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001914 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001916 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1917 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1920 {
1921 if (need_return)
1922 {
1923 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001924 if (got_int)
1925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 need_return = FALSE;
1927 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001928
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001929 fname = NULL;
1930 if (qfp->qf_fnum != 0
1931 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1932 {
1933 fname = buf->b_fname;
1934 if (qfp->qf_type == 1) /* :helpgrep */
1935 fname = gettail(fname);
1936 }
1937 if (fname == NULL)
1938 sprintf((char *)IObuff, "%2d", i);
1939 else
1940 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1941 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001943 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1944 if (qfp->qf_lnum == 0)
1945 IObuff[0] = NUL;
1946 else if (qfp->qf_col == 0)
1947 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1948 else
1949 sprintf((char *)IObuff, ":%ld col %d",
1950 qfp->qf_lnum, qfp->qf_col);
1951 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1952 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1953 msg_puts_attr(IObuff, hl_attr(HLF_N));
1954 if (qfp->qf_pattern != NULL)
1955 {
1956 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1957 STRCAT(IObuff, ":");
1958 msg_puts(IObuff);
1959 }
1960 msg_puts((char_u *)" ");
1961
1962 /* Remove newlines and leading whitespace from the text. For an
1963 * unrecognized line keep the indent, the compiler may mark a word
1964 * with ^^^^. */
1965 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1967 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001968 msg_prt_line(IObuff, FALSE);
1969 out_flush(); /* show one line at a time */
1970 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001972
1973 qfp = qfp->qf_next;
1974 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 ui_breakcheck();
1976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977}
1978
1979/*
1980 * Remove newlines and leading whitespace from an error message.
1981 * Put the result in "buf[bufsize]".
1982 */
1983 static void
1984qf_fmt_text(text, buf, bufsize)
1985 char_u *text;
1986 char_u *buf;
1987 int bufsize;
1988{
1989 int i;
1990 char_u *p = text;
1991
1992 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1993 {
1994 if (*p == '\n')
1995 {
1996 buf[i] = ' ';
1997 while (*++p != NUL)
1998 if (!vim_iswhite(*p) && *p != '\n')
1999 break;
2000 }
2001 else
2002 buf[i] = *p++;
2003 }
2004 buf[i] = NUL;
2005}
2006
2007/*
2008 * ":colder [count]": Up in the quickfix stack.
2009 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002010 * ":lolder [count]": Up in the location list stack.
2011 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 */
2013 void
2014qf_age(eap)
2015 exarg_T *eap;
2016{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 int count;
2019
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002020 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2021 {
2022 qi = GET_LOC_LIST(curwin);
2023 if (qi == NULL)
2024 {
2025 EMSG(_(e_loclist));
2026 return;
2027 }
2028 }
2029
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (eap->addr_count != 0)
2031 count = eap->line2;
2032 else
2033 count = 1;
2034 while (count--)
2035 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002036 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002038 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
2040 EMSG(_("E380: At bottom of quickfix stack"));
2041 return;
2042 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002043 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045 else
2046 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002047 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
2049 EMSG(_("E381: At top of quickfix stack"));
2050 return;
2051 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002055 qf_msg(qi);
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057}
2058
2059 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002060qf_msg(qi)
2061 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
2063 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002064 qi->qf_curlist + 1, qi->qf_listcount,
2065 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002067 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068#endif
2069}
2070
2071/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002072 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 */
2074 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002075qf_free(qi, idx)
2076 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 int idx;
2078{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002079 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002081 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002083 qfp = qi->qf_lists[idx].qf_start->qf_next;
2084 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2085 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2086 vim_free(qi->qf_lists[idx].qf_start);
2087 qi->qf_lists[idx].qf_start = qfp;
2088 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090}
2091
2092/*
2093 * qf_mark_adjust: adjust marks
2094 */
2095 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002096qf_mark_adjust(wp, line1, line2, amount, amount_after)
2097 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 linenr_T line1;
2099 linenr_T line2;
2100 long amount;
2101 long amount_after;
2102{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002103 int i;
2104 qfline_T *qfp;
2105 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002108 if (wp != NULL)
2109 {
2110 if (wp->w_llist == NULL)
2111 return;
2112 qi = wp->w_llist;
2113 }
2114
2115 for (idx = 0; idx < qi->qf_listcount; ++idx)
2116 if (qi->qf_lists[idx].qf_count)
2117 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2118 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if (qfp->qf_fnum == curbuf->b_fnum)
2120 {
2121 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2122 {
2123 if (amount == MAXLNUM)
2124 qfp->qf_cleared = TRUE;
2125 else
2126 qfp->qf_lnum += amount;
2127 }
2128 else if (amount_after && qfp->qf_lnum > line2)
2129 qfp->qf_lnum += amount_after;
2130 }
2131}
2132
2133/*
2134 * Make a nice message out of the error character and the error number:
2135 * char number message
2136 * e or E 0 " error"
2137 * w or W 0 " warning"
2138 * i or I 0 " info"
2139 * 0 0 ""
2140 * other 0 " c"
2141 * e or E n " error n"
2142 * w or W n " warning n"
2143 * i or I n " info n"
2144 * 0 n " error n"
2145 * other n " c n"
2146 * 1 x "" :helpgrep
2147 */
2148 static char_u *
2149qf_types(c, nr)
2150 int c, nr;
2151{
2152 static char_u buf[20];
2153 static char_u cc[3];
2154 char_u *p;
2155
2156 if (c == 'W' || c == 'w')
2157 p = (char_u *)" warning";
2158 else if (c == 'I' || c == 'i')
2159 p = (char_u *)" info";
2160 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2161 p = (char_u *)" error";
2162 else if (c == 0 || c == 1)
2163 p = (char_u *)"";
2164 else
2165 {
2166 cc[0] = ' ';
2167 cc[1] = c;
2168 cc[2] = NUL;
2169 p = cc;
2170 }
2171
2172 if (nr <= 0)
2173 return p;
2174
2175 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2176 return buf;
2177}
2178
2179#if defined(FEAT_WINDOWS) || defined(PROTO)
2180/*
2181 * ":cwindow": open the quickfix window if we have errors to display,
2182 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002183 * ":lwindow": open the location list window if we have locations to display,
2184 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 */
2186 void
2187ex_cwindow(eap)
2188 exarg_T *eap;
2189{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 win_T *win;
2192
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002193 if (eap->cmdidx == CMD_lwindow)
2194 {
2195 qi = GET_LOC_LIST(curwin);
2196 if (qi == NULL)
2197 return;
2198 }
2199
2200 /* Look for an existing quickfix window. */
2201 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 /*
2204 * If a quickfix window is open but we have no errors to display,
2205 * close the window. If a quickfix window is not open, then open
2206 * it if we have errors; otherwise, leave it closed.
2207 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002208 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard68071d2006-05-02 22:08:30 +00002209 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
2211 if (win != NULL)
2212 ex_cclose(eap);
2213 }
2214 else if (win == NULL)
2215 ex_copen(eap);
2216}
2217
2218/*
2219 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 */
2222/*ARGSUSED*/
2223 void
2224ex_cclose(eap)
2225 exarg_T *eap;
2226{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 win_T *win = NULL;
2228 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2231 {
2232 qi = GET_LOC_LIST(curwin);
2233 if (qi == NULL)
2234 return;
2235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002237 /* Find existing quickfix window and close it. */
2238 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (win != NULL)
2240 win_close(win, FALSE);
2241}
2242
2243/*
2244 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002245 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 */
2247 void
2248ex_copen(eap)
2249 exarg_T *eap;
2250{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002251 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002254 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002255 buf_T *qf_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002257 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2258 {
2259 qi = GET_LOC_LIST(curwin);
2260 if (qi == NULL)
2261 {
2262 EMSG(_(e_loclist));
2263 return;
2264 }
2265 }
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (eap->addr_count != 0)
2268 height = eap->line2;
2269 else
2270 height = QF_WINHEIGHT;
2271
2272#ifdef FEAT_VISUAL
2273 reset_VIsual_and_resel(); /* stop Visual mode */
2274#endif
2275#ifdef FEAT_GUI
2276 need_mouse_correct = TRUE;
2277#endif
2278
2279 /*
2280 * Find existing quickfix window, or open a new one.
2281 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002282 win = qf_find_win(qi);
2283
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002284 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 win_goto(win);
2286 else
2287 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002288 qf_buf = qf_find_buf(qi);
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 /* The current window becomes the previous window afterwards. */
2291 win = curwin;
2292
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002293 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2294 /* Create the new window at the very bottom. */
2295 win_goto(lastwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (win_split(height, WSP_BELOW) == FAIL)
2297 return; /* not enough room for window */
2298#ifdef FEAT_SCROLLBIND
2299 curwin->w_p_scb = FALSE;
2300#endif
2301
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002302 /* Remove the location list for the quickfix window */
2303 qf_free_all(curwin);
2304
2305 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307 /*
2308 * For the location list window, create a reference to the
2309 * location list from the window 'win'.
2310 */
2311 curwin->w_llist_ref = win->w_llist;
2312 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002314
Bram Moolenaar9c102382006-05-03 21:26:49 +00002315 if (qf_buf != NULL)
2316 /* Use the existing quickfix buffer */
2317 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
2318 ECMD_HIDE + ECMD_OLDBUF);
2319 else
2320 {
2321 /* Create a new quickfix buffer */
2322 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2323 /* switch off 'swapfile' */
2324 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2325 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002326 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002327 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2328 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
2329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002331 /* Only set the height when still in the same tab page and there is no
2332 * window to the side. */
2333 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002334#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002335 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002336#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 win_setheight(height);
2339 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2340 if (win_valid(win))
2341 prevwin = win;
2342 }
2343
2344 /*
2345 * Fill the buffer with the quickfix list.
2346 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002349 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 curwin->w_cursor.col = 0;
2351 check_cursor();
2352 update_topline(); /* scroll to show the line */
2353}
2354
2355/*
2356 * Return the number of the current entry (line number in the quickfix
2357 * window).
2358 */
2359 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002360qf_current_entry(wp)
2361 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002363 qf_info_T *qi = &ql_info;
2364
2365 if (IS_LL_WINDOW(wp))
2366 /* In the location list window, use the referenced location list */
2367 qi = wp->w_llist_ref;
2368
2369 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370}
2371
2372/*
2373 * Update the cursor position in the quickfix window to the current error.
2374 * Return TRUE if there is a quickfix window.
2375 */
2376 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002377qf_win_pos_update(qi, old_qf_index)
2378 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 int old_qf_index; /* previous qf_index or zero */
2380{
2381 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002382 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 /*
2385 * Put the cursor on the current error in the quickfix window, so that
2386 * it's viewable.
2387 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002388 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (win != NULL
2390 && qf_index <= win->w_buffer->b_ml.ml_line_count
2391 && old_qf_index != qf_index)
2392 {
2393 win_T *old_curwin = curwin;
2394
2395 curwin = win;
2396 curbuf = win->w_buffer;
2397 if (qf_index > old_qf_index)
2398 {
2399 curwin->w_redraw_top = old_qf_index;
2400 curwin->w_redraw_bot = qf_index;
2401 }
2402 else
2403 {
2404 curwin->w_redraw_top = qf_index;
2405 curwin->w_redraw_bot = old_qf_index;
2406 }
2407 curwin->w_cursor.lnum = qf_index;
2408 curwin->w_cursor.col = 0;
2409 update_topline(); /* scroll to show the line */
2410 redraw_later(VALID);
2411 curwin->w_redr_status = TRUE; /* update ruler */
2412 curwin = old_curwin;
2413 curbuf = curwin->w_buffer;
2414 }
2415 return win != NULL;
2416}
2417
2418/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002419 * Check whether the given window is displaying the specified quickfix/location
2420 * list buffer
2421 */
2422 static int
2423is_qf_win(win, qi)
2424 win_T *win;
2425 qf_info_T *qi;
2426{
2427 /*
2428 * A window displaying the quickfix buffer will have the w_llist_ref field
2429 * set to NULL.
2430 * A window displaying a location list buffer will have the w_llist_ref
2431 * pointing to the location list.
2432 */
2433 if (bt_quickfix(win->w_buffer))
2434 if ((qi == &ql_info && win->w_llist_ref == NULL)
2435 || (qi != &ql_info && win->w_llist_ref == qi))
2436 return TRUE;
2437
2438 return FALSE;
2439}
2440
2441/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002442 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002443 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002444 */
2445 static win_T *
2446qf_find_win(qi)
2447 qf_info_T *qi;
2448{
2449 win_T *win;
2450
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002451 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002452 if (is_qf_win(win, qi))
2453 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002454
2455 return win;
2456}
2457
2458/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002459 * Find a quickfix buffer.
2460 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
2462 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002463qf_find_buf(qi)
2464 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002466 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002467 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaar9c102382006-05-03 21:26:49 +00002469 FOR_ALL_TAB_WINDOWS(tp, win)
2470 if (is_qf_win(win, qi))
2471 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002472
Bram Moolenaar9c102382006-05-03 21:26:49 +00002473 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474}
2475
2476/*
2477 * Find the quickfix buffer. If it exists, update the contents.
2478 */
2479 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002480qf_update_buffer(qi)
2481 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002487 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (buf != NULL)
2489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 /* set curwin/curbuf to buf and save a few things */
2491 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002493 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 /* restore curwin/curbuf and a few other things */
2496 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002498 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500}
2501
2502/*
2503 * Fill current buffer with quickfix errors, replacing any previous contents.
2504 * curbuf must be the quickfix buffer!
2505 */
2506 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002507qf_fill_buffer(qi)
2508 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002510 linenr_T lnum;
2511 qfline_T *qfp;
2512 buf_T *errbuf;
2513 int len;
2514 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516 /* delete all existing lines */
2517 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2518 (void)ml_delete((linenr_T)1, FALSE);
2519
2520 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002521 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
2523 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002524 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2525 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 if (qfp->qf_fnum != 0
2528 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2529 && errbuf->b_fname != NULL)
2530 {
2531 if (qfp->qf_type == 1) /* :helpgrep */
2532 STRCPY(IObuff, gettail(errbuf->b_fname));
2533 else
2534 STRCPY(IObuff, errbuf->b_fname);
2535 len = (int)STRLEN(IObuff);
2536 }
2537 else
2538 len = 0;
2539 IObuff[len++] = '|';
2540
2541 if (qfp->qf_lnum > 0)
2542 {
2543 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2544 len += (int)STRLEN(IObuff + len);
2545
2546 if (qfp->qf_col > 0)
2547 {
2548 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2549 len += (int)STRLEN(IObuff + len);
2550 }
2551
2552 sprintf((char *)IObuff + len, "%s",
2553 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2554 len += (int)STRLEN(IObuff + len);
2555 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002556 else if (qfp->qf_pattern != NULL)
2557 {
2558 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2559 len += (int)STRLEN(IObuff + len);
2560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 IObuff[len++] = '|';
2562 IObuff[len++] = ' ';
2563
2564 /* Remove newlines and leading whitespace from the text.
2565 * For an unrecognized line keep the indent, the compiler may
2566 * mark a word with ^^^^. */
2567 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2568 IObuff + len, IOSIZE - len);
2569
2570 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2571 == FAIL)
2572 break;
2573 qfp = qfp->qf_next;
2574 }
2575 /* Delete the empty line which is now at the end */
2576 (void)ml_delete(lnum + 1, FALSE);
2577 }
2578
2579 /* correct cursor position */
2580 check_lnums(TRUE);
2581
2582 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2583 * resembles reading a file into a buffer, it's more logical when using
2584 * autocommands. */
2585 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2586 curbuf->b_p_ma = FALSE;
2587
2588#ifdef FEAT_AUTOCMD
2589 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2590 FALSE, curbuf);
2591 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2592 FALSE, curbuf);
2593#endif
2594
2595 /* make sure it will be redrawn */
2596 redraw_curbuf_later(NOT_VALID);
2597
2598 /* Restore KeyTyped, setting 'filetype' may reset it. */
2599 KeyTyped = old_KeyTyped;
2600}
2601
2602#endif /* FEAT_WINDOWS */
2603
2604/*
2605 * Return TRUE if "buf" is the quickfix buffer.
2606 */
2607 int
2608bt_quickfix(buf)
2609 buf_T *buf;
2610{
2611 return (buf->b_p_bt[0] == 'q');
2612}
2613
2614/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002615 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2616 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 */
2618 int
2619bt_nofile(buf)
2620 buf_T *buf;
2621{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002622 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2623 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624}
2625
2626/*
2627 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2628 */
2629 int
2630bt_dontwrite(buf)
2631 buf_T *buf;
2632{
2633 return (buf->b_p_bt[0] == 'n');
2634}
2635
2636 int
2637bt_dontwrite_msg(buf)
2638 buf_T *buf;
2639{
2640 if (bt_dontwrite(buf))
2641 {
2642 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2643 return TRUE;
2644 }
2645 return FALSE;
2646}
2647
2648/*
2649 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2650 * and 'bufhidden'.
2651 */
2652 int
2653buf_hide(buf)
2654 buf_T *buf;
2655{
2656 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2657 switch (buf->b_p_bh[0])
2658 {
2659 case 'u': /* "unload" */
2660 case 'w': /* "wipe" */
2661 case 'd': return FALSE; /* "delete" */
2662 case 'h': return TRUE; /* "hide" */
2663 }
2664 return (p_hid || cmdmod.hide);
2665}
2666
2667/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002668 * Return TRUE when using ":vimgrep" for ":grep".
2669 */
2670 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002671grep_internal(cmdidx)
2672 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002673{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002674 return ((cmdidx == CMD_grep
2675 || cmdidx == CMD_lgrep
2676 || cmdidx == CMD_grepadd
2677 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002678 && STRCMP("internal",
2679 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2680}
2681
2682/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002683 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 */
2685 void
2686ex_make(eap)
2687 exarg_T *eap;
2688{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002689 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 char_u *cmd;
2691 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002692 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002693 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002694 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002695#ifdef FEAT_AUTOCMD
2696 char_u *au_name = NULL;
2697
2698 switch (eap->cmdidx)
2699 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002700 case CMD_make: au_name = (char_u *)"make"; break;
2701 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2702 case CMD_grep: au_name = (char_u *)"grep"; break;
2703 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2704 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2705 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002706 default: break;
2707 }
2708 if (au_name != NULL)
2709 {
2710 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2711 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002712# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002713 if (did_throw || force_abort)
2714 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002715# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002716 }
2717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
Bram Moolenaar86b68352004-12-27 21:59:20 +00002719 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002720 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002721 {
2722 ex_vimgrep(eap);
2723 return;
2724 }
2725
Bram Moolenaara6557602006-02-04 22:43:20 +00002726 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2727 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002728 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002731 fname = get_mef_name();
2732 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002734 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /*
2737 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2738 */
2739 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2740 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002741 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 cmd = alloc(len);
2743 if (cmd == NULL)
2744 return;
2745 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2746 (char *)p_shq);
2747 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002748 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 /*
2750 * Output a newline if there's something else than the :make command that
2751 * was typed (in which case the cursor is in column 0).
2752 */
2753 if (msg_col == 0)
2754 msg_didout = FALSE;
2755 msg_start();
2756 MSG_PUTS(":!");
2757 msg_outtrans(cmd); /* show what we are doing */
2758
2759 /* let the shell know if we are redirecting output or not */
2760 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2761
2762#ifdef AMIGA
2763 out_flush();
2764 /* read window status report and redraw before message */
2765 (void)char_avail();
2766#endif
2767
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002768 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002769 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2770 (eap->cmdidx != CMD_grepadd
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002771 && eap->cmdidx != CMD_lgrepadd));
2772#ifdef FEAT_AUTOCMD
2773 if (au_name != NULL)
2774 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2775 curbuf->b_fname, TRUE, curbuf);
2776#endif
2777 if (res > 0 && !eap->forceit)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002778 {
2779 if (wp != NULL)
2780 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002781 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaar7c626922005-02-07 22:01:03 +00002784 mch_remove(fname);
2785 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 vim_free(cmd);
2787}
2788
2789/*
2790 * Return the name for the errorfile, in allocated memory.
2791 * Find a new unique name when 'makeef' contains "##".
2792 * Returns NULL for error.
2793 */
2794 static char_u *
2795get_mef_name()
2796{
2797 char_u *p;
2798 char_u *name;
2799 static int start = -1;
2800 static int off = 0;
2801#ifdef HAVE_LSTAT
2802 struct stat sb;
2803#endif
2804
2805 if (*p_mef == NUL)
2806 {
2807 name = vim_tempname('e');
2808 if (name == NULL)
2809 EMSG(_(e_notmp));
2810 return name;
2811 }
2812
2813 for (p = p_mef; *p; ++p)
2814 if (p[0] == '#' && p[1] == '#')
2815 break;
2816
2817 if (*p == NUL)
2818 return vim_strsave(p_mef);
2819
2820 /* Keep trying until the name doesn't exist yet. */
2821 for (;;)
2822 {
2823 if (start == -1)
2824 start = mch_get_pid();
2825 else
2826 off += 19;
2827
2828 name = alloc((unsigned)STRLEN(p_mef) + 30);
2829 if (name == NULL)
2830 break;
2831 STRCPY(name, p_mef);
2832 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2833 STRCAT(name, p + 2);
2834 if (mch_getperm(name) < 0
2835#ifdef HAVE_LSTAT
2836 /* Don't accept a symbolic link, its a security risk. */
2837 && mch_lstat((char *)name, &sb) < 0
2838#endif
2839 )
2840 break;
2841 vim_free(name);
2842 }
2843 return name;
2844}
2845
2846/*
2847 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002848 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
2850 void
2851ex_cc(eap)
2852 exarg_T *eap;
2853{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002854 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002855
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002856 if (eap->cmdidx == CMD_ll
2857 || eap->cmdidx == CMD_lrewind
2858 || eap->cmdidx == CMD_lfirst
2859 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002860 {
2861 qi = GET_LOC_LIST(curwin);
2862 if (qi == NULL)
2863 {
2864 EMSG(_(e_loclist));
2865 return;
2866 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002867 }
2868
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002869 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 eap->addr_count > 0
2871 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002872 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002874 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2875 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 ? 1
2877 : 32767,
2878 eap->forceit);
2879}
2880
2881/*
2882 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002883 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 */
2885 void
2886ex_cnext(eap)
2887 exarg_T *eap;
2888{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002889 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002891 if (eap->cmdidx == CMD_lnext
2892 || eap->cmdidx == CMD_lNext
2893 || eap->cmdidx == CMD_lprevious
2894 || eap->cmdidx == CMD_lnfile
2895 || eap->cmdidx == CMD_lNfile
2896 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002897 {
2898 qi = GET_LOC_LIST(curwin);
2899 if (qi == NULL)
2900 {
2901 EMSG(_(e_loclist));
2902 return;
2903 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002904 }
2905
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002906 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002908 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002910 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2911 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 ? BACKWARD_FILE
2913 : BACKWARD,
2914 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2915}
2916
2917/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002918 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
2921 void
2922ex_cfile(eap)
2923 exarg_T *eap;
2924{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002926 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002927
2928 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2929 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002930 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002933 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002934
2935 /*
2936 * This function is used by the :cfile, :cgetfile and :caddfile
2937 * commands.
2938 * :cfile always creates a new quickfix list and jumps to the
2939 * first error.
2940 * :cgetfile creates a new quickfix list but doesn't jump to the
2941 * first error.
2942 * :caddfile adds to an existing quickfix list. If there is no
2943 * quickfix list then a new list is created.
2944 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002945 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2946 && eap->cmdidx != CMD_laddfile)) > 0
2947 && (eap->cmdidx == CMD_cfile
2948 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002949 {
2950 if (wp != NULL)
2951 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002952 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954}
2955
2956/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002957 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00002958 * ":vimgrepadd {pattern} file(s)"
2959 * ":lvimgrep {pattern} file(s)"
2960 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00002961 */
2962 void
2963ex_vimgrep(eap)
2964 exarg_T *eap;
2965{
Bram Moolenaar81695252004-12-29 20:58:21 +00002966 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002967 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002968 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002969 char_u *s;
2970 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002971 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00002972 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002973 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002974 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002975 buf_T *buf;
2976 int duplicate_name = FALSE;
2977 int using_dummy;
2978 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002979 buf_T *first_match_buf = NULL;
2980 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002981 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002982#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2983 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002984#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002985 aco_save_T aco;
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00002986#ifdef FEAT_AUTOCMD
Bram Moolenaar7c626922005-02-07 22:01:03 +00002987 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002988 int flags = 0;
2989 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002990 long tomatch;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002991
2992 switch (eap->cmdidx)
2993 {
2994 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002995 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002996 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002997 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002998 default: break;
2999 }
3000 if (au_name != NULL)
3001 {
3002 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3003 curbuf->b_fname, TRUE, curbuf);
3004 if (did_throw || force_abort)
3005 return;
3006 }
3007#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003008
Bram Moolenaar754b5602006-02-09 23:53:20 +00003009 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003010 || eap->cmdidx == CMD_lvimgrep
3011 || eap->cmdidx == CMD_lgrepadd
3012 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003013 {
3014 qi = ll_get_or_alloc_list(curwin);
3015 if (qi == NULL)
3016 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003017 }
3018
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003019 if (eap->addr_count > 0)
3020 tomatch = eap->line2;
3021 else
3022 tomatch = MAXLNUM;
3023
Bram Moolenaar81695252004-12-29 20:58:21 +00003024 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003025 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003026 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003027 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003028 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003029 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003030 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003031 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003032 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003033 if (regmatch.regprog == NULL)
3034 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003035 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003036 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003037
3038 p = skipwhite(p);
3039 if (*p == NUL)
3040 {
3041 EMSG(_("E683: File name missing or invalid pattern"));
3042 goto theend;
3043 }
3044
Bram Moolenaar754b5602006-02-09 23:53:20 +00003045 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003046 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003047 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003048 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003049 qf_new_list(qi);
3050 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003051 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003052 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003053 prevp->qf_next != prevp; prevp = prevp->qf_next)
3054 ;
3055
3056 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003057 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003058 goto theend;
3059 if (fcount == 0)
3060 {
3061 EMSG(_(e_nomatch));
3062 goto theend;
3063 }
3064
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003065 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003066 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003067 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003068 if (time(NULL) > seconds)
3069 {
3070 /* Display the file name every second or so. */
3071 seconds = time(NULL);
3072 msg_start();
Bram Moolenaara5373fa2005-09-09 19:47:12 +00003073 p = msg_strtrunc(fnames[fi], TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003074 if (p == NULL)
3075 msg_outtrans(fnames[fi]);
3076 else
3077 {
3078 msg_outtrans(p);
3079 vim_free(p);
3080 }
3081 msg_clr_eos();
3082 msg_didout = FALSE; /* overwrite this message */
3083 msg_nowait = TRUE; /* don't wait for this message */
3084 msg_col = 0;
3085 out_flush();
3086 }
3087
Bram Moolenaar81695252004-12-29 20:58:21 +00003088 buf = buflist_findname_exp(fnames[fi]);
3089 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3090 {
3091 /* Remember that a buffer with this name already exists. */
3092 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003093 using_dummy = TRUE;
3094
3095#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3096 /* Don't do Filetype autocommands to avoid loading syntax and
3097 * indent scripts, a great speed improvement. */
3098 save_ei = au_event_disable(",Filetype");
3099#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003100 /* Don't use modelines here, it's useless. */
3101 save_mls = p_mls;
3102 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003103
3104 /* Load file into a buffer, so that 'fileencoding' is detected,
3105 * autocommands applied, etc. */
3106 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003107
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003108 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003109#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3110 au_event_restore(save_ei);
3111#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003112 }
3113 else
3114 /* Use existing, loaded buffer. */
3115 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003116
Bram Moolenaar81695252004-12-29 20:58:21 +00003117 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003118 {
3119 if (!got_int)
3120 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
3121 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003122 else
3123 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003124 /* Try for a match in all lines of the buffer.
3125 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003126 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003127 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3128 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003129 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003130 col = 0;
3131 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3132 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003133 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003134 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003135 NULL, /* dir */
3136 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00003137 ml_get_buf(buf,
3138 regmatch.startpos[0].lnum + lnum, FALSE),
3139 regmatch.startpos[0].lnum + lnum,
3140 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003141 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003142 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003143 0, /* nr */
3144 0, /* type */
3145 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003146 ) == FAIL)
3147 {
3148 got_int = TRUE;
3149 break;
3150 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003151 found_match = TRUE;
3152 if (--tomatch == 0)
3153 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003154 if ((flags & VGR_GLOBAL) == 0
3155 || regmatch.endpos[0].lnum > 0)
3156 break;
3157 col = regmatch.endpos[0].col
3158 + (col == regmatch.endpos[0].col);
3159 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3160 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003161 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003162 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003163 if (got_int)
3164 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003165 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003166
3167 if (using_dummy)
3168 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003169 if (found_match && first_match_buf == NULL)
3170 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003171 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003172 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003173 /* Never keep a dummy buffer if there is another buffer
3174 * with the same name. */
3175 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003176 buf = NULL;
3177 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003178 else if (!cmdmod.hide
3179 || buf->b_p_bh[0] == 'u' /* "unload" */
3180 || buf->b_p_bh[0] == 'w' /* "wipe" */
3181 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003182 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003183 /* When no match was found we don't need to remember the
3184 * buffer, wipe it out. If there was a match and it
3185 * wasn't the first one or we won't jump there: only
3186 * unload the buffer.
3187 * Ignore 'hidden' here, because it may lead to having too
3188 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003189 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003190 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003191 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003192 buf = NULL;
3193 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003194 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003195 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003196 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003197 buf = NULL;
3198 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003199 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003200
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003201 if (buf != NULL)
3202 {
3203 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003204 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003205 * need to be done (again). But not the window-local
3206 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003207 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003208#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003209 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3210 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003211#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003212 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003213 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003214 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003215 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003216 }
3217 }
3218
3219 FreeWild(fcount, fnames);
3220
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003221 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3222 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3223 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003224
3225#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003226 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003227#endif
3228
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003229#ifdef FEAT_AUTOCMD
3230 if (au_name != NULL)
3231 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3232 curbuf->b_fname, TRUE, curbuf);
3233#endif
3234
Bram Moolenaar86b68352004-12-27 21:59:20 +00003235 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003236 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003237 {
3238 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003239 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003240 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003241 else
3242 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003243
3244theend:
3245 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003246}
3247
3248/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003249 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003250 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003251 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3252 * If "s" is not NULL terminate the pattern with a NUL.
3253 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003254 */
3255 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003256skip_vimgrep_pat(p, s, flags)
3257 char_u *p;
3258 char_u **s;
3259 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003260{
3261 int c;
3262
3263 if (vim_isIDc(*p))
3264 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003265 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003266 if (s != NULL)
3267 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003268 p = skiptowhite(p);
3269 if (s != NULL && *p != NUL)
3270 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003271 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003272 else
3273 {
3274 /* ":vimgrep /pattern/[g][j] fname" */
3275 if (s != NULL)
3276 *s = p + 1;
3277 c = *p;
3278 p = skip_regexp(p + 1, c, TRUE, NULL);
3279 if (*p != c)
3280 return NULL;
3281
3282 /* Truncate the pattern. */
3283 if (s != NULL)
3284 *p = NUL;
3285 ++p;
3286
3287 /* Find the flags */
3288 while (*p == 'g' || *p == 'j')
3289 {
3290 if (flags != NULL)
3291 {
3292 if (*p == 'g')
3293 *flags |= VGR_GLOBAL;
3294 else
3295 *flags |= VGR_NOJUMP;
3296 }
3297 ++p;
3298 }
3299 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003300 return p;
3301}
3302
3303/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003304 * Load file "fname" into a dummy buffer and return the buffer pointer.
3305 * Returns NULL if it fails.
3306 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3307 */
3308 static buf_T *
3309load_dummy_buffer(fname)
3310 char_u *fname;
3311{
3312 buf_T *newbuf;
3313 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003314 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003315
3316 /* Allocate a buffer without putting it in the buffer list. */
3317 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3318 if (newbuf == NULL)
3319 return NULL;
3320
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003321 /* Init the options. */
3322 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3323
Bram Moolenaar81695252004-12-29 20:58:21 +00003324 /* set curwin/curbuf to buf and save a few things */
3325 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00003326
3327 /* Need to set the filename for autocommands. */
3328 (void)setfname(curbuf, fname, NULL, FALSE);
3329
Bram Moolenaar4770d092006-01-12 23:22:24 +00003330 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003331 {
3332 /* Create swap file now to avoid the ATTENTION message. */
3333 check_need_swap(TRUE);
3334
3335 /* Remove the "dummy" flag, otherwise autocommands may not
3336 * work. */
3337 curbuf->b_flags &= ~BF_DUMMY;
3338
3339 if (readfile(fname, NULL,
3340 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3341 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003342 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003343 && !(curbuf->b_flags & BF_NEW))
3344 {
3345 failed = FALSE;
3346 if (curbuf != newbuf)
3347 {
3348 /* Bloody autocommands changed the buffer! */
3349 if (buf_valid(newbuf))
3350 wipe_buffer(newbuf, FALSE);
3351 newbuf = curbuf;
3352 }
3353 }
3354 }
3355
Bram Moolenaar81695252004-12-29 20:58:21 +00003356 /* restore curwin/curbuf and a few other things */
3357 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00003358
3359 if (!buf_valid(newbuf))
3360 return NULL;
3361 if (failed)
3362 {
3363 wipe_dummy_buffer(newbuf);
3364 return NULL;
3365 }
3366 return newbuf;
3367}
3368
3369/*
3370 * Wipe out the dummy buffer that load_dummy_buffer() created.
3371 */
3372 static void
3373wipe_dummy_buffer(buf)
3374 buf_T *buf;
3375{
3376 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003377 {
3378#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3379 cleanup_T cs;
3380
3381 /* Reset the error/interrupt/exception state here so that aborting()
3382 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3383 * work when got_int is set. */
3384 enter_cleanup(&cs);
3385#endif
3386
Bram Moolenaar81695252004-12-29 20:58:21 +00003387 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003388
3389#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3390 /* Restore the error/interrupt/exception state if not discarded by a
3391 * new aborting error, interrupt, or uncaught exception. */
3392 leave_cleanup(&cs);
3393#endif
3394 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003395}
3396
3397/*
3398 * Unload the dummy buffer that load_dummy_buffer() created.
3399 */
3400 static void
3401unload_dummy_buffer(buf)
3402 buf_T *buf;
3403{
3404 if (curbuf != buf) /* safety check */
3405 close_buffer(NULL, buf, DOBUF_UNLOAD);
3406}
3407
Bram Moolenaar05159a02005-02-26 23:04:13 +00003408#if defined(FEAT_EVAL) || defined(PROTO)
3409/*
3410 * Add each quickfix error to list "list" as a dictionary.
3411 */
3412 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003413get_errorlist(wp, list)
3414 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003415 list_T *list;
3416{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003417 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003418 dict_T *dict;
3419 char_u buf[2];
3420 qfline_T *qfp;
3421 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003422
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003423 if (wp != NULL)
3424 {
3425 qi = GET_LOC_LIST(wp);
3426 if (qi == NULL)
3427 return FAIL;
3428 }
3429
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003430 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003431 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003432 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003433
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003434 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3435 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003436 {
3437 if ((dict = dict_alloc()) == NULL)
3438 return FAIL;
3439 if (list_append_dict(list, dict) == FAIL)
3440 return FAIL;
3441
3442 buf[0] = qfp->qf_type;
3443 buf[1] = NUL;
3444 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
3445 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3446 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3447 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3448 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003449 || dict_add_nr_str(dict, "pattern", 0L,
3450 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3451 || dict_add_nr_str(dict, "text", 0L,
3452 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003453 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3454 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3455 return FAIL;
3456
3457 qfp = qfp->qf_next;
3458 }
3459 return OK;
3460}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003461
3462/*
3463 * Populate the quickfix list with the items supplied in the list
3464 * of dictionaries.
3465 */
3466 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003467set_errorlist(wp, list, action)
3468 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003469 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003470 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003471{
3472 listitem_T *li;
3473 dict_T *d;
3474 char_u *filename, *pattern, *text, *type;
3475 long lnum;
3476 int col, nr;
3477 int vcol;
3478 qfline_T *prevp = NULL;
3479 int valid, status;
3480 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003481 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003482
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003483 if (wp != NULL)
3484 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003485 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003486 if (qi == NULL)
3487 return FAIL;
3488 }
3489
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003490 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003491 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003492 qf_new_list(qi);
3493 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003494 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003495 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003496 prevp->qf_next != prevp; prevp = prevp->qf_next)
3497 ;
3498 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003499 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003500
3501 for (li = list->lv_first; li != NULL; li = li->li_next)
3502 {
3503 if (li->li_tv.v_type != VAR_DICT)
3504 continue; /* Skip non-dict items */
3505
3506 d = li->li_tv.vval.v_dict;
3507 if (d == NULL)
3508 continue;
3509
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003510 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003511 lnum = get_dict_number(d, (char_u *)"lnum");
3512 col = get_dict_number(d, (char_u *)"col");
3513 vcol = get_dict_number(d, (char_u *)"vcol");
3514 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003515 type = get_dict_string(d, (char_u *)"type", TRUE);
3516 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3517 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003518 if (text == NULL)
3519 text = vim_strsave((char_u *)"");
3520
3521 valid = TRUE;
3522 if (filename == NULL || (lnum == 0 && pattern == NULL))
3523 valid = FALSE;
3524
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003525 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003526 NULL, /* dir */
3527 filename,
3528 text,
3529 lnum,
3530 col,
3531 vcol, /* vis_col */
3532 pattern, /* search pattern */
3533 nr,
3534 type == NULL ? NUL : *type,
3535 valid);
3536
3537 vim_free(filename);
3538 vim_free(pattern);
3539 vim_free(text);
3540 vim_free(type);
3541
3542 if (status == FAIL)
3543 {
3544 retval = FAIL;
3545 break;
3546 }
3547 }
3548
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003549 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3550 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3551 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003552
3553#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003554 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003555#endif
3556
3557 return retval;
3558}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003559#endif
3560
Bram Moolenaar81695252004-12-29 20:58:21 +00003561/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003562 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003563 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003564 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003565 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003566 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003567 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003568 */
3569 void
3570ex_cbuffer(eap)
3571 exarg_T *eap;
3572{
3573 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003574 qf_info_T *qi = &ql_info;
3575
Bram Moolenaardb552d602006-03-23 22:59:57 +00003576 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3577 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003578 {
3579 qi = ll_get_or_alloc_list(curwin);
3580 if (qi == NULL)
3581 return;
3582 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003583
3584 if (*eap->arg == NUL)
3585 buf = curbuf;
3586 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3587 buf = buflist_findnr(atoi((char *)eap->arg));
3588 if (buf == NULL)
3589 EMSG(_(e_invarg));
3590 else if (buf->b_ml.ml_mfp == NULL)
3591 EMSG(_("E681: Buffer is not loaded"));
3592 else
3593 {
3594 if (eap->addr_count == 0)
3595 {
3596 eap->line1 = 1;
3597 eap->line2 = buf->b_ml.ml_line_count;
3598 }
3599 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3600 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3601 EMSG(_(e_invrange));
3602 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003603 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003604 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3605 (eap->cmdidx != CMD_caddbuffer
3606 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar754b5602006-02-09 23:53:20 +00003607 eap->line1, eap->line2) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003608 && (eap->cmdidx == CMD_cbuffer
3609 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003610 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3611 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003612 }
3613}
3614
Bram Moolenaar1e015462005-09-25 22:16:38 +00003615#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003616/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003617 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3618 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003619 */
3620 void
3621ex_cexpr(eap)
3622 exarg_T *eap;
3623{
3624 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003625 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003626
Bram Moolenaardb552d602006-03-23 22:59:57 +00003627 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3628 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003629 {
3630 qi = ll_get_or_alloc_list(curwin);
3631 if (qi == NULL)
3632 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003633 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003634
Bram Moolenaar4770d092006-01-12 23:22:24 +00003635 /* Evaluate the expression. When the result is a string or a list we can
3636 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003637 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003638 if (tv != NULL)
3639 {
3640 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3641 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3642 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003643 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3644 (eap->cmdidx != CMD_caddexpr
3645 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar4770d092006-01-12 23:22:24 +00003646 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003647 && (eap->cmdidx == CMD_cexpr
3648 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003649 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003650 }
3651 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003652 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003653 free_tv(tv);
3654 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003655}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003656#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003657
3658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 * ":helpgrep {pattern}"
3660 */
3661 void
3662ex_helpgrep(eap)
3663 exarg_T *eap;
3664{
3665 regmatch_T regmatch;
3666 char_u *save_cpo;
3667 char_u *p;
3668 int fcount;
3669 char_u **fnames;
3670 FILE *fd;
3671 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003672 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003674#ifdef FEAT_MULTI_LANG
3675 char_u *lang;
3676#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003677 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003678 int new_qi = FALSE;
3679 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
3681 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3682 save_cpo = p_cpo;
3683 p_cpo = (char_u *)"";
3684
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003685#ifdef FEAT_MULTI_LANG
3686 /* Check for a specified language */
3687 lang = check_help_lang(eap->arg);
3688#endif
3689
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003690 if (eap->cmdidx == CMD_lhelpgrep)
3691 {
3692 /* Find an existing help window */
3693 FOR_ALL_WINDOWS(wp)
3694 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3695 break;
3696
3697 if (wp == NULL) /* Help window not found */
3698 qi = NULL;
3699 else
3700 qi = wp->w_llist;
3701
3702 if (qi == NULL)
3703 {
3704 /* Allocate a new location list for help text matches */
3705 if ((qi = ll_new_list()) == NULL)
3706 return;
3707 new_qi = TRUE;
3708 }
3709 }
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3712 regmatch.rm_ic = FALSE;
3713 if (regmatch.regprog != NULL)
3714 {
3715 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003716 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718 /* Go through all directories in 'runtimepath' */
3719 p = p_rtp;
3720 while (*p != NUL && !got_int)
3721 {
3722 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3723
3724 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3725 add_pathsep(NameBuff);
3726 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3727 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3728 &fnames, EW_FILE|EW_SILENT) == OK
3729 && fcount > 0)
3730 {
3731 for (fi = 0; fi < fcount && !got_int; ++fi)
3732 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003733#ifdef FEAT_MULTI_LANG
3734 /* Skip files for a different language. */
3735 if (lang != NULL
3736 && STRNICMP(lang, fnames[fi]
3737 + STRLEN(fnames[fi]) - 3, 2) != 0
3738 && !(STRNICMP(lang, "en", 2) == 0
3739 && STRNICMP("txt", fnames[fi]
3740 + STRLEN(fnames[fi]) - 3, 3) == 0))
3741 continue;
3742#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003743 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (fd != NULL)
3745 {
3746 lnum = 1;
3747 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3748 {
3749 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3750 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003751 int l = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
3753 /* remove trailing CR, LF, spaces, etc. */
3754 while (l > 0 && IObuff[l - 1] <= ' ')
3755 IObuff[--l] = NUL;
3756
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003757 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 NULL, /* dir */
3759 fnames[fi],
3760 IObuff,
3761 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003762 (int)(regmatch.startp[0] - IObuff)
3763 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003764 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003765 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 0, /* nr */
3767 1, /* type */
3768 TRUE /* valid */
3769 ) == FAIL)
3770 {
3771 got_int = TRUE;
3772 break;
3773 }
3774 }
3775 ++lnum;
3776 line_breakcheck();
3777 }
3778 fclose(fd);
3779 }
3780 }
3781 FreeWild(fcount, fnames);
3782 }
3783 }
3784 vim_free(regmatch.regprog);
3785
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003786 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3787 qi->qf_lists[qi->qf_curlist].qf_ptr =
3788 qi->qf_lists[qi->qf_curlist].qf_start;
3789 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791
3792 p_cpo = save_cpo;
3793
3794#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003795 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#endif
3797
3798 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003799 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003800 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003801 else
3802 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003803
3804 if (eap->cmdidx == CMD_lhelpgrep)
3805 {
3806 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003807 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003808 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3809 {
3810 if (new_qi)
3811 ll_free_all(&qi);
3812 }
3813 else if (curwin->w_llist == NULL)
3814 curwin->w_llist = qi;
3815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816}
3817
3818#endif /* FEAT_QUICKFIX */