blob: 8d5032221ad6e53e69664a00e669c8188dabcfd3 [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
52struct qf_list
53{
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 Moolenaar071d4272004-06-13 20:20:40 +000059} qf_lists[LISTCOUNT];
60
61static int qf_curlist = 0; /* current error list */
62static int qf_listcount = 0; /* current number of lists */
63
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * Structure used to hold the info of one part of 'errorformat'
68 */
69struct eformat
70{
71 regprog_T *prog; /* pre-formatted part of 'errorformat' */
72 struct eformat *next; /* pointer to next (NULL if last) */
73 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
74 char_u prefix; /* prefix of this format line: */
75 /* 'D' enter directory */
76 /* 'X' leave directory */
77 /* 'A' start of multi-line message */
78 /* 'E' error message */
79 /* 'W' warning message */
80 /* 'I' informational message */
81 /* 'C' continuation line */
82 /* 'Z' end of multi-line message */
83 /* 'G' general, unspecific message */
84 /* 'P' push file (partial) message */
85 /* 'Q' pop/quit file (partial) message */
86 /* 'O' overread (partial) message */
87 char_u flags; /* additional flags given in prefix */
88 /* '-' do not include this line */
89};
90
Bram Moolenaar86b68352004-12-27 21:59:20 +000091static int qf_init_ext __ARGS((char_u *efile, buf_T *buf, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast));
Bram Moolenaar071d4272004-06-13 20:20:40 +000092static void qf_new_list __ARGS((void));
Bram Moolenaar68b76a62005-03-25 21:53:48 +000093static int qf_add_entry __ARGS((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));
Bram Moolenaar071d4272004-06-13 20:20:40 +000094static void qf_msg __ARGS((void));
95static void qf_free __ARGS((int idx));
96static char_u *qf_types __ARGS((int, int));
97static int qf_get_fnum __ARGS((char_u *, char_u *));
98static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
99static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
100static char_u *qf_guess_filepath __ARGS((char_u *));
101static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
102static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
103#ifdef FEAT_WINDOWS
104static int qf_win_pos_update __ARGS((int old_qf_index));
105static buf_T *qf_find_buf __ARGS((void));
106static void qf_update_buffer __ARGS((void));
107static void qf_fill_buffer __ARGS((void));
108#endif
109static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000110static buf_T *load_dummy_buffer __ARGS((char_u *fname));
111static void wipe_dummy_buffer __ARGS((buf_T *buf));
112static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
114/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000115 * Read the errorfile "efile" into memory, line by line, building the error
116 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 * Return -1 for error, number of errors for success.
118 */
119 int
120qf_init(efile, errorformat, newlist)
121 char_u *efile;
122 char_u *errorformat;
123 int newlist; /* TRUE: start a new error list */
124{
Bram Moolenaar86b68352004-12-27 21:59:20 +0000125 if (efile == NULL)
126 return FAIL;
127 return qf_init_ext(efile, curbuf, errorformat, newlist,
128 (linenr_T)0, (linenr_T)0);
129}
130
131/*
132 * Read the errorfile "efile" into memory, line by line, building the error
133 * list.
134 * Alternative: when "efile" is null read errors from buffer "buf".
135 * Always use 'errorformat' from "buf" if there is a local value.
136 * Then lnumfirst and lnumlast specify the range of lines to use.
137 * Return -1 for error, number of errors for success.
138 */
139 static int
140qf_init_ext(efile, buf, errorformat, newlist, lnumfirst, lnumlast)
141 char_u *efile;
142 buf_T *buf;
143 char_u *errorformat;
144 int newlist; /* TRUE: start a new error list */
145 linenr_T lnumfirst; /* first line number to use */
146 linenr_T lnumlast; /* last line number to use */
147{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 char_u *namebuf;
149 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000150 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 char_u *fmtstr = NULL;
152 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000153 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 int type = 0;
155 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000156 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 long lnum = 0L;
158 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000159 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000160 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 char_u *efmp;
162 struct eformat *fmt_first = NULL;
163 struct eformat *fmt_last = NULL;
164 struct eformat *fmt_ptr;
165 char_u *efm;
166 char_u *ptr;
167 char_u *srcptr;
168 int len;
169 int i;
170 int round;
171 int idx = 0;
172 int multiline = FALSE;
173 int multiignore = FALSE;
174 int multiscan = FALSE;
175 int retval = -1; /* default: return error flag */
176 char_u *directory = NULL;
177 char_u *currfile = NULL;
178 char_u *tail = NULL;
179 struct dir_stack_T *file_stack = NULL;
180 regmatch_T regmatch;
181 static struct fmtpattern
182 {
183 char_u convchar;
184 char *pattern;
185 } fmt_pat[FMT_PATTERNS] =
186 {
187 {'f', "\\f\\+"},
188 {'n', "\\d\\+"},
189 {'l', "\\d\\+"},
190 {'c', "\\d\\+"},
191 {'t', "."},
192 {'m', ".\\+"},
193 {'r', ".*"},
194 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000195 {'v', "\\d\\+"},
196 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 };
198
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 namebuf = alloc(CMDBUFFSIZE + 1);
200 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000201 pattern = alloc(CMDBUFFSIZE + 1);
202 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 goto qf_init_end;
204
Bram Moolenaar86b68352004-12-27 21:59:20 +0000205 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {
207 EMSG2(_(e_openerrf), efile);
208 goto qf_init_end;
209 }
210
211 if (newlist || qf_curlist == qf_listcount)
212 /* make place for a new list */
213 qf_new_list();
214 else if (qf_lists[qf_curlist].qf_count > 0)
215 /* Adding to existing list, find last entry. */
216 for (qfprev = qf_lists[qf_curlist].qf_start;
217 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
218 ;
219
220/*
221 * Each part of the format string is copied and modified from errorformat to
222 * regex prog. Only a few % characters are allowed.
223 */
224 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000225 if (errorformat == p_efm && *buf->b_p_efm != NUL)
226 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 else
228 efm = errorformat;
229 /*
230 * Get some space to modify the format string into.
231 */
232 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
233 for (round = FMT_PATTERNS; round > 0; )
234 i += (int)STRLEN(fmt_pat[--round].pattern);
235#ifdef COLON_IN_FILENAME
236 i += 12; /* "%f" can become twelve chars longer */
237#else
238 i += 2; /* "%f" can become two chars longer */
239#endif
240 if ((fmtstr = alloc(i)) == NULL)
241 goto error2;
242
243 while (efm[0])
244 {
245 /*
246 * Allocate a new eformat structure and put it at the end of the list
247 */
248 fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
249 if (fmt_ptr == NULL)
250 goto error2;
251 if (fmt_first == NULL) /* first one */
252 fmt_first = fmt_ptr;
253 else
254 fmt_last->next = fmt_ptr;
255 fmt_last = fmt_ptr;
256 fmt_ptr->prefix = NUL;
257 fmt_ptr->flags = NUL;
258 fmt_ptr->next = NULL;
259 fmt_ptr->prog = NULL;
260 for (round = FMT_PATTERNS; round > 0; )
261 fmt_ptr->addr[--round] = NUL;
262 /* round is 0 now */
263
264 /*
265 * Isolate one part in the 'errorformat' option
266 */
267 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
268 if (efm[len] == '\\' && efm[len + 1] != NUL)
269 ++len;
270
271 /*
272 * Build regexp pattern from current 'errorformat' option
273 */
274 ptr = fmtstr;
275 *ptr++ = '^';
276 for (efmp = efm; efmp < efm + len; ++efmp)
277 {
278 if (*efmp == '%')
279 {
280 ++efmp;
281 for (idx = 0; idx < FMT_PATTERNS; ++idx)
282 if (fmt_pat[idx].convchar == *efmp)
283 break;
284 if (idx < FMT_PATTERNS)
285 {
286 if (fmt_ptr->addr[idx])
287 {
288 sprintf((char *)errmsg,
289 _("E372: Too many %%%c in format string"), *efmp);
290 EMSG(errmsg);
291 goto error2;
292 }
293 if ((idx
294 && idx < 6
295 && vim_strchr((char_u *)"DXOPQ",
296 fmt_ptr->prefix) != NULL)
297 || (idx == 6
298 && vim_strchr((char_u *)"OPQ",
299 fmt_ptr->prefix) == NULL))
300 {
301 sprintf((char *)errmsg,
302 _("E373: Unexpected %%%c in format string"), *efmp);
303 EMSG(errmsg);
304 goto error2;
305 }
306 fmt_ptr->addr[idx] = (char_u)++round;
307 *ptr++ = '\\';
308 *ptr++ = '(';
309#ifdef BACKSLASH_IN_FILENAME
310 if (*efmp == 'f')
311 {
312 /* Also match "c:" in the file name, even when
313 * checking for a colon next: "%f:".
314 * "\%(\a:\)\=" */
315 STRCPY(ptr, "\\%(\\a:\\)\\=");
316 ptr += 10;
317 }
318#endif
319 if (*efmp == 'f' && efmp[1] != NUL
320 && efmp[1] != '\\' && efmp[1] != '%')
321 {
322 /* A file name may contain spaces, but this isn't in
323 * "\f". use "[^x]\+" instead (x is next character) */
324 *ptr++ = '[';
325 *ptr++ = '^';
326 *ptr++ = efmp[1];
327 *ptr++ = ']';
328 *ptr++ = '\\';
329 *ptr++ = '+';
330 }
331 else
332 {
333 srcptr = (char_u *)fmt_pat[idx].pattern;
334 while ((*ptr = *srcptr++) != NUL)
335 ++ptr;
336 }
337 *ptr++ = '\\';
338 *ptr++ = ')';
339 }
340 else if (*efmp == '*')
341 {
342 if (*++efmp == '[' || *efmp == '\\')
343 {
344 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
345 {
346 if (efmp[1] == '^')
347 *ptr++ = *++efmp;
348 if (efmp < efm + len)
349 {
350 *ptr++ = *++efmp; /* could be ']' */
351 while (efmp < efm + len
352 && (*ptr++ = *++efmp) != ']')
353 /* skip */;
354 if (efmp == efm + len)
355 {
356 EMSG(_("E374: Missing ] in format string"));
357 goto error2;
358 }
359 }
360 }
361 else if (efmp < efm + len) /* %*\D, %*\s etc. */
362 *ptr++ = *++efmp;
363 *ptr++ = '\\';
364 *ptr++ = '+';
365 }
366 else
367 {
368 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
369 sprintf((char *)errmsg,
370 _("E375: Unsupported %%%c in format string"), *efmp);
371 EMSG(errmsg);
372 goto error2;
373 }
374 }
375 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
376 *ptr++ = *efmp; /* regexp magic characters */
377 else if (*efmp == '#')
378 *ptr++ = '*';
379 else if (efmp == efm + 1) /* analyse prefix */
380 {
381 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
382 fmt_ptr->flags = *efmp++;
383 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
384 fmt_ptr->prefix = *efmp;
385 else
386 {
387 sprintf((char *)errmsg,
388 _("E376: Invalid %%%c in format string prefix"), *efmp);
389 EMSG(errmsg);
390 goto error2;
391 }
392 }
393 else
394 {
395 sprintf((char *)errmsg,
396 _("E377: Invalid %%%c in format string"), *efmp);
397 EMSG(errmsg);
398 goto error2;
399 }
400 }
401 else /* copy normal character */
402 {
403 if (*efmp == '\\' && efmp + 1 < efm + len)
404 ++efmp;
405 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
406 *ptr++ = '\\'; /* escape regexp atoms */
407 if (*efmp)
408 *ptr++ = *efmp;
409 }
410 }
411 *ptr++ = '$';
412 *ptr = NUL;
413 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
414 goto error2;
415 /*
416 * Advance to next part
417 */
418 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
419 }
420 if (fmt_first == NULL) /* nothing found */
421 {
422 EMSG(_("E378: 'errorformat' contains no pattern"));
423 goto error2;
424 }
425
426 /*
427 * got_int is reset here, because it was probably set when killing the
428 * ":make" command, but we still want to read the errorfile then.
429 */
430 got_int = FALSE;
431
432 /* Always ignore case when looking for a matching error. */
433 regmatch.rm_ic = TRUE;
434
435 /*
436 * Read the lines in the error file one by one.
437 * Try to recognize one of the error formats in each line.
438 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000439 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000441 /* Get the next line. */
442 if (fd == NULL)
443 {
444 if (buflnum > lnumlast)
445 break;
446 STRNCPY(IObuff, ml_get_buf(buf, buflnum++, FALSE), CMDBUFFSIZE - 2);
447 }
448 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
449 break;
450
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
452 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
453 *efmp = NUL;
454#ifdef USE_CRNL
455 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
456 *efmp = NUL;
457#endif
458
459 /*
460 * Try to match each part of 'errorformat' until we find a complete
461 * match or no match.
462 */
463 valid = TRUE;
464restofline:
465 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
466 {
467 idx = fmt_ptr->prefix;
468 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
469 continue;
470 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000471 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 if (!multiscan)
473 errmsg[0] = NUL;
474 lnum = 0;
475 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000476 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 enr = -1;
478 type = 0;
479 tail = NULL;
480
481 regmatch.regprog = fmt_ptr->prog;
482 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
483 {
484 if ((idx == 'C' || idx == 'Z') && !multiline)
485 continue;
486 if (vim_strchr((char_u *)"EWI", idx) != NULL)
487 type = idx;
488 else
489 type = 0;
490 /*
491 * Extract error message data from matched line
492 */
493 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
494 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000495 int c = *regmatch.endp[i];
496
497 /* Expand ~/file and $HOME/file to full path. */
498 *regmatch.endp[i] = NUL;
499 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
500 *regmatch.endp[i] = c;
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000503 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 continue;
505 }
506 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
507 enr = (int)atol((char *)regmatch.startp[i]);
508 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
509 lnum = atol((char *)regmatch.startp[i]);
510 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
511 col = (int)atol((char *)regmatch.startp[i]);
512 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
513 type = *regmatch.startp[i];
514 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
515 STRCPY(errmsg, IObuff);
516 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
517 {
518 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
519 STRNCPY(errmsg, regmatch.startp[i], len);
520 errmsg[len] = NUL;
521 }
522 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
523 tail = regmatch.startp[i];
524 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
525 {
526 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
527 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000528 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
530 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
531 {
532 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000533 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000535 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
536 {
537 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
538 if (len > CMDBUFFSIZE - 5)
539 len = CMDBUFFSIZE - 5;
540 STRCPY(pattern, "^\\V");
541 STRNCAT(pattern, regmatch.startp[i], len);
542 pattern[len + 3] = '\\';
543 pattern[len + 4] = '$';
544 pattern[len + 5] = NUL;
545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 break;
547 }
548 }
549 multiscan = FALSE;
550 if (!fmt_ptr || idx == 'D' || idx == 'X')
551 {
552 if (fmt_ptr)
553 {
554 if (idx == 'D') /* enter directory */
555 {
556 if (*namebuf == NUL)
557 {
558 EMSG(_("E379: Missing or empty directory name"));
559 goto error2;
560 }
561 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
562 goto error2;
563 }
564 else if (idx == 'X') /* leave directory */
565 directory = qf_pop_dir(&dir_stack);
566 }
567 namebuf[0] = NUL; /* no match found, remove file name */
568 lnum = 0; /* don't jump to this line */
569 valid = FALSE;
570 STRCPY(errmsg, IObuff); /* copy whole line to error message */
571 if (!fmt_ptr)
572 multiline = multiignore = FALSE;
573 }
574 else if (fmt_ptr)
575 {
576 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
577 multiline = TRUE; /* start of a multi-line message */
578 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
579 { /* continuation of multi-line msg */
580 if (qfprev == NULL)
581 goto error2;
582 if (*errmsg && !multiignore)
583 {
584 len = (int)STRLEN(qfprev->qf_text);
585 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
586 == NULL)
587 goto error2;
588 STRCPY(ptr, qfprev->qf_text);
589 vim_free(qfprev->qf_text);
590 qfprev->qf_text = ptr;
591 *(ptr += len) = '\n';
592 STRCPY(++ptr, errmsg);
593 }
594 if (qfprev->qf_nr == -1)
595 qfprev->qf_nr = enr;
596 if (vim_isprintc(type) && !qfprev->qf_type)
597 qfprev->qf_type = type; /* only printable chars allowed */
598 if (!qfprev->qf_lnum)
599 qfprev->qf_lnum = lnum;
600 if (!qfprev->qf_col)
601 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000602 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 if (!qfprev->qf_fnum)
604 qfprev->qf_fnum = qf_get_fnum(directory,
605 *namebuf || directory ? namebuf
606 : currfile && valid ? currfile : 0);
607 if (idx == 'Z')
608 multiline = multiignore = FALSE;
609 line_breakcheck();
610 continue;
611 }
612 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
613 {
614 /* global file names */
615 valid = FALSE;
616 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
617 {
618 if (*namebuf && idx == 'P')
619 currfile = qf_push_dir(namebuf, &file_stack);
620 else if (idx == 'Q')
621 currfile = qf_pop_dir(&file_stack);
622 *namebuf = NUL;
623 if (tail && *tail)
624 {
625 STRCPY(IObuff, skipwhite(tail));
626 multiscan = TRUE;
627 goto restofline;
628 }
629 }
630 }
631 if (fmt_ptr->flags == '-') /* generally exclude this line */
632 {
633 if (multiline)
634 multiignore = TRUE; /* also exclude continuation lines */
635 continue;
636 }
637 }
638
639 if (qf_add_entry(&qfprev,
640 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000641 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000643 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 errmsg,
645 lnum,
646 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000647 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000648 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 enr,
650 type,
651 valid) == FAIL)
652 goto error2;
653 line_breakcheck();
654 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000655 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {
657 if (qf_lists[qf_curlist].qf_index == 0) /* no valid entry found */
658 {
659 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
660 qf_lists[qf_curlist].qf_index = 1;
661 qf_lists[qf_curlist].qf_nonevalid = TRUE;
662 }
663 else
664 {
665 qf_lists[qf_curlist].qf_nonevalid = FALSE;
666 if (qf_lists[qf_curlist].qf_ptr == NULL)
667 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
668 }
669 retval = qf_lists[qf_curlist].qf_count; /* return number of matches */
670 goto qf_init_ok;
671 }
672 EMSG(_(e_readerrf));
673error2:
674 qf_free(qf_curlist);
675 qf_listcount--;
676 if (qf_curlist > 0)
677 --qf_curlist;
678qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000679 if (fd != NULL)
680 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
682 {
683 fmt_first = fmt_ptr->next;
684 vim_free(fmt_ptr->prog);
685 vim_free(fmt_ptr);
686 }
687 qf_clean_dir_stack(&dir_stack);
688 qf_clean_dir_stack(&file_stack);
689qf_init_end:
690 vim_free(namebuf);
691 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000692 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 vim_free(fmtstr);
694
695#ifdef FEAT_WINDOWS
696 qf_update_buffer();
697#endif
698
699 return retval;
700}
701
702/*
703 * Prepare for adding a new quickfix list.
704 */
705 static void
706qf_new_list()
707{
708 int i;
709
710 /*
711 * If the current entry is not the last entry, delete entries below
712 * the current entry. This makes it possible to browse in a tree-like
713 * way with ":grep'.
714 */
715 while (qf_listcount > qf_curlist + 1)
716 qf_free(--qf_listcount);
717
718 /*
719 * When the stack is full, remove to oldest entry
720 * Otherwise, add a new entry.
721 */
722 if (qf_listcount == LISTCOUNT)
723 {
724 qf_free(0);
725 for (i = 1; i < LISTCOUNT; ++i)
726 qf_lists[i - 1] = qf_lists[i];
727 qf_curlist = LISTCOUNT - 1;
728 }
729 else
730 qf_curlist = qf_listcount++;
731 qf_lists[qf_curlist].qf_index = 0;
732 qf_lists[qf_curlist].qf_count = 0;
733}
734
735/*
736 * Add an entry to the end of the list of errors.
737 * Returns OK or FAIL.
738 */
739 static int
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000740qf_add_entry(prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
741 valid)
742 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 char_u *dir; /* optional directory name */
744 char_u *fname; /* file name or NULL */
745 char_u *mesg; /* message */
746 long lnum; /* line number */
747 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000748 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000749 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 int nr; /* error number */
751 int type; /* type character */
752 int valid; /* valid entry */
753{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000754 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000756 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 return FAIL;
758 qfp->qf_fnum = qf_get_fnum(dir, fname);
759 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
760 {
761 vim_free(qfp);
762 return FAIL;
763 }
764 qfp->qf_lnum = lnum;
765 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000767 if (pattern == NULL || *pattern == NUL)
768 qfp->qf_pattern = NULL;
769 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
770 {
771 vim_free(qfp->qf_text);
772 vim_free(qfp);
773 return FAIL;
774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 qfp->qf_nr = nr;
776 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
777 type = 0;
778 qfp->qf_type = type;
779 qfp->qf_valid = valid;
780
781 if (qf_lists[qf_curlist].qf_count == 0) /* first element in the list */
782 {
783 qf_lists[qf_curlist].qf_start = qfp;
784 qfp->qf_prev = qfp; /* first element points to itself */
785 }
786 else
787 {
788 qfp->qf_prev = *prevp;
789 (*prevp)->qf_next = qfp;
790 }
791 qfp->qf_next = qfp; /* last element points to itself */
792 qfp->qf_cleared = FALSE;
793 *prevp = qfp;
794 ++qf_lists[qf_curlist].qf_count;
795 if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid)
796 /* first valid entry */
797 {
798 qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count;
799 qf_lists[qf_curlist].qf_ptr = qfp;
800 }
801
802 return OK;
803}
804
805/*
806 * get buffer number for file "dir.name"
807 */
808 static int
809qf_get_fnum(directory, fname)
810 char_u *directory;
811 char_u *fname;
812{
813 if (fname == NULL || *fname == NUL) /* no file name */
814 return 0;
815 {
816#ifdef RISCOS
817 /* Name is reported as `main.c', but file is `c.main' */
818 return ro_buflist_add(fname);
819#else
820 char_u *ptr;
821 int fnum;
822
823# ifdef VMS
824 vms_remove_version(fname);
825# endif
826# ifdef BACKSLASH_IN_FILENAME
827 if (directory != NULL)
828 slash_adjust(directory);
829 slash_adjust(fname);
830# endif
831 if (directory != NULL && !vim_isAbsName(fname)
832 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
833 {
834 /*
835 * Here we check if the file really exists.
836 * This should normally be true, but if make works without
837 * "leaving directory"-messages we might have missed a
838 * directory change.
839 */
840 if (mch_getperm(ptr) < 0)
841 {
842 vim_free(ptr);
843 directory = qf_guess_filepath(fname);
844 if (directory)
845 ptr = concat_fnames(directory, fname, TRUE);
846 else
847 ptr = vim_strsave(fname);
848 }
849 /* Use concatenated directory name and file name */
850 fnum = buflist_add(ptr, 0);
851 vim_free(ptr);
852 return fnum;
853 }
854 return buflist_add(fname, 0);
855#endif
856 }
857}
858
859/*
860 * push dirbuf onto the directory stack and return pointer to actual dir or
861 * NULL on error
862 */
863 static char_u *
864qf_push_dir(dirbuf, stackptr)
865 char_u *dirbuf;
866 struct dir_stack_T **stackptr;
867{
868 struct dir_stack_T *ds_new;
869 struct dir_stack_T *ds_ptr;
870
871 /* allocate new stack element and hook it in */
872 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
873 if (ds_new == NULL)
874 return NULL;
875
876 ds_new->next = *stackptr;
877 *stackptr = ds_new;
878
879 /* store directory on the stack */
880 if (vim_isAbsName(dirbuf)
881 || (*stackptr)->next == NULL
882 || (*stackptr && dir_stack != *stackptr))
883 (*stackptr)->dirname = vim_strsave(dirbuf);
884 else
885 {
886 /* Okay we don't have an absolute path.
887 * dirbuf must be a subdir of one of the directories on the stack.
888 * Let's search...
889 */
890 ds_new = (*stackptr)->next;
891 (*stackptr)->dirname = NULL;
892 while (ds_new)
893 {
894 vim_free((*stackptr)->dirname);
895 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
896 TRUE);
897 if (mch_isdir((*stackptr)->dirname) == TRUE)
898 break;
899
900 ds_new = ds_new->next;
901 }
902
903 /* clean up all dirs we already left */
904 while ((*stackptr)->next != ds_new)
905 {
906 ds_ptr = (*stackptr)->next;
907 (*stackptr)->next = (*stackptr)->next->next;
908 vim_free(ds_ptr->dirname);
909 vim_free(ds_ptr);
910 }
911
912 /* Nothing found -> it must be on top level */
913 if (ds_new == NULL)
914 {
915 vim_free((*stackptr)->dirname);
916 (*stackptr)->dirname = vim_strsave(dirbuf);
917 }
918 }
919
920 if ((*stackptr)->dirname != NULL)
921 return (*stackptr)->dirname;
922 else
923 {
924 ds_ptr = *stackptr;
925 *stackptr = (*stackptr)->next;
926 vim_free(ds_ptr);
927 return NULL;
928 }
929}
930
931
932/*
933 * pop dirbuf from the directory stack and return previous directory or NULL if
934 * stack is empty
935 */
936 static char_u *
937qf_pop_dir(stackptr)
938 struct dir_stack_T **stackptr;
939{
940 struct dir_stack_T *ds_ptr;
941
942 /* TODO: Should we check if dirbuf is the directory on top of the stack?
943 * What to do if it isn't? */
944
945 /* pop top element and free it */
946 if (*stackptr != NULL)
947 {
948 ds_ptr = *stackptr;
949 *stackptr = (*stackptr)->next;
950 vim_free(ds_ptr->dirname);
951 vim_free(ds_ptr);
952 }
953
954 /* return NEW top element as current dir or NULL if stack is empty*/
955 return *stackptr ? (*stackptr)->dirname : NULL;
956}
957
958/*
959 * clean up directory stack
960 */
961 static void
962qf_clean_dir_stack(stackptr)
963 struct dir_stack_T **stackptr;
964{
965 struct dir_stack_T *ds_ptr;
966
967 while ((ds_ptr = *stackptr) != NULL)
968 {
969 *stackptr = (*stackptr)->next;
970 vim_free(ds_ptr->dirname);
971 vim_free(ds_ptr);
972 }
973}
974
975/*
976 * Check in which directory of the directory stack the given file can be
977 * found.
978 * Returns a pointer to the directory name or NULL if not found
979 * Cleans up intermediate directory entries.
980 *
981 * TODO: How to solve the following problem?
982 * If we have the this directory tree:
983 * ./
984 * ./aa
985 * ./aa/bb
986 * ./bb
987 * ./bb/x.c
988 * and make says:
989 * making all in aa
990 * making all in bb
991 * x.c:9: Error
992 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
993 * qf_guess_filepath will return NULL.
994 */
995 static char_u *
996qf_guess_filepath(filename)
997 char_u *filename;
998{
999 struct dir_stack_T *ds_ptr;
1000 struct dir_stack_T *ds_tmp;
1001 char_u *fullname;
1002
1003 /* no dirs on the stack - there's nothing we can do */
1004 if (dir_stack == NULL)
1005 return NULL;
1006
1007 ds_ptr = dir_stack->next;
1008 fullname = NULL;
1009 while (ds_ptr)
1010 {
1011 vim_free(fullname);
1012 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1013
1014 /* If concat_fnames failed, just go on. The worst thing that can happen
1015 * is that we delete the entire stack.
1016 */
1017 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1018 break;
1019
1020 ds_ptr = ds_ptr->next;
1021 }
1022
1023 vim_free(fullname);
1024
1025 /* clean up all dirs we already left */
1026 while (dir_stack->next != ds_ptr)
1027 {
1028 ds_tmp = dir_stack->next;
1029 dir_stack->next = dir_stack->next->next;
1030 vim_free(ds_tmp->dirname);
1031 vim_free(ds_tmp);
1032 }
1033
1034 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1035
1036}
1037
1038/*
1039 * jump to a quickfix line
1040 * if dir == FORWARD go "errornr" valid entries forward
1041 * if dir == BACKWARD go "errornr" valid entries backward
1042 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1043 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1044 * else if "errornr" is zero, redisplay the same line
1045 * else go to entry "errornr"
1046 */
1047 void
1048qf_jump(dir, errornr, forceit)
1049 int dir;
1050 int errornr;
1051 int forceit;
1052{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001053 qfline_T *qf_ptr;
1054 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 int qf_index;
1056 int old_qf_fnum;
1057 int old_qf_index;
1058 int prev_index;
1059 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1060 char_u *err = e_no_more_items;
1061 linenr_T i;
1062 buf_T *old_curbuf;
1063 linenr_T old_lnum;
1064 char_u *old_swb = p_swb;
1065 colnr_T screen_col;
1066 colnr_T char_col;
1067 char_u *line;
1068#ifdef FEAT_WINDOWS
1069 int opened_window = FALSE;
1070 win_T *win;
1071 win_T *altwin;
1072#endif
1073 int print_message = TRUE;
1074 int len;
1075#ifdef FEAT_FOLDING
1076 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1077#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001078 int ok = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1081 {
1082 EMSG(_(e_quickfix));
1083 return;
1084 }
1085
1086 qf_ptr = qf_lists[qf_curlist].qf_ptr;
1087 old_qf_ptr = qf_ptr;
1088 qf_index = qf_lists[qf_curlist].qf_index;
1089 old_qf_index = qf_index;
1090 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1091 {
1092 while (errornr--)
1093 {
1094 old_qf_ptr = qf_ptr;
1095 prev_index = qf_index;
1096 old_qf_fnum = qf_ptr->qf_fnum;
1097 do
1098 {
1099 if (qf_index == qf_lists[qf_curlist].qf_count
1100 || qf_ptr->qf_next == NULL)
1101 {
1102 qf_ptr = old_qf_ptr;
1103 qf_index = prev_index;
1104 if (err != NULL)
1105 {
1106 EMSG(_(err));
1107 goto theend;
1108 }
1109 errornr = 0;
1110 break;
1111 }
1112 ++qf_index;
1113 qf_ptr = qf_ptr->qf_next;
1114 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1115 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1116 err = NULL;
1117 }
1118 }
1119 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1120 {
1121 while (errornr--)
1122 {
1123 old_qf_ptr = qf_ptr;
1124 prev_index = qf_index;
1125 old_qf_fnum = qf_ptr->qf_fnum;
1126 do
1127 {
1128 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1129 {
1130 qf_ptr = old_qf_ptr;
1131 qf_index = prev_index;
1132 if (err != NULL)
1133 {
1134 EMSG(_(err));
1135 goto theend;
1136 }
1137 errornr = 0;
1138 break;
1139 }
1140 --qf_index;
1141 qf_ptr = qf_ptr->qf_prev;
1142 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1143 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1144 err = NULL;
1145 }
1146 }
1147 else if (errornr != 0) /* go to specified number */
1148 {
1149 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1150 {
1151 --qf_index;
1152 qf_ptr = qf_ptr->qf_prev;
1153 }
1154 while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count
1155 && qf_ptr->qf_next != NULL)
1156 {
1157 ++qf_index;
1158 qf_ptr = qf_ptr->qf_next;
1159 }
1160 }
1161
1162#ifdef FEAT_WINDOWS
1163 qf_lists[qf_curlist].qf_index = qf_index;
1164 if (qf_win_pos_update(old_qf_index))
1165 /* No need to print the error message if it's visible in the error
1166 * window */
1167 print_message = FALSE;
1168
1169 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001170 * For ":helpgrep" find a help window or open one.
1171 */
1172 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1173 {
1174 win_T *wp;
1175 int n;
1176
1177 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1178 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1179 break;
1180 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1181 win_enter(wp, TRUE);
1182 else
1183 {
1184 /*
1185 * Split off help window; put it at far top if no position
1186 * specified, the current window is vertically split and narrow.
1187 */
1188 n = WSP_HELP;
1189# ifdef FEAT_VERTSPLIT
1190 if (cmdmod.split == 0 && curwin->w_width != Columns
1191 && curwin->w_width < 80)
1192 n |= WSP_TOP;
1193# endif
1194 if (win_split(0, n) == FAIL)
1195 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001196 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001197
1198 if (curwin->w_height < p_hh)
1199 win_setheight((int)p_hh);
1200 }
1201
1202 if (!p_im)
1203 restart_edit = 0; /* don't want insert mode in help file */
1204 }
1205
1206 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 * If currently in the quickfix window, find another window to show the
1208 * file in.
1209 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001210 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
1212 /*
1213 * If there is no file specified, we don't know where to go.
1214 * But do advance, otherwise ":cn" gets stuck.
1215 */
1216 if (qf_ptr->qf_fnum == 0)
1217 goto theend;
1218
1219 /*
1220 * If there is only one window, create a new one above the quickfix
1221 * window.
1222 */
1223 if (firstwin == lastwin)
1224 {
1225 if (win_split(0, WSP_ABOVE) == FAIL)
1226 goto failed; /* not enough room for window */
1227 opened_window = TRUE; /* close it when fail */
1228 p_swb = empty_option; /* don't split again */
1229# ifdef FEAT_SCROLLBIND
1230 curwin->w_p_scb = FALSE;
1231# endif
1232 }
1233 else
1234 {
1235 /*
1236 * Try to find a window that shows the right buffer.
1237 * Default to the window just above the quickfix buffer.
1238 */
1239 win = curwin;
1240 altwin = NULL;
1241 for (;;)
1242 {
1243 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1244 break;
1245 if (win->w_prev == NULL)
1246 win = lastwin; /* wrap around the top */
1247 else
1248 win = win->w_prev; /* go to previous window */
1249
1250 if (bt_quickfix(win->w_buffer))
1251 {
1252 /* Didn't find it, go to the window before the quickfix
1253 * window. */
1254 if (altwin != NULL)
1255 win = altwin;
1256 else if (curwin->w_prev != NULL)
1257 win = curwin->w_prev;
1258 else
1259 win = curwin->w_next;
1260 break;
1261 }
1262
1263 /* Remember a usable window. */
1264 if (altwin == NULL && !win->w_p_pvw
1265 && win->w_buffer->b_p_bt[0] == NUL)
1266 altwin = win;
1267 }
1268
1269 win_goto(win);
1270 }
1271 }
1272#endif
1273
1274 /*
1275 * If there is a file name,
1276 * read the wanted file if needed, and check autowrite etc.
1277 */
1278 old_curbuf = curbuf;
1279 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001280
1281 if (qf_ptr->qf_fnum != 0)
1282 {
1283 if (qf_ptr->qf_type == 1)
1284 {
1285 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1286 * set b_p_ro flag). */
1287 if (!can_abandon(curbuf, forceit))
1288 {
1289 EMSG(_(e_nowrtmsg));
1290 ok = FALSE;
1291 }
1292 else
1293 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1294 ECMD_HIDE + ECMD_SET_HELP);
1295 }
1296 else
1297 ok = buflist_getfile(qf_ptr->qf_fnum,
1298 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1299 }
1300
1301 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
1303 /* When not switched to another buffer, still need to set pc mark */
1304 if (curbuf == old_curbuf)
1305 setpcmark();
1306
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001307 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001309 /*
1310 * Go to line with error, unless qf_lnum is 0.
1311 */
1312 i = qf_ptr->qf_lnum;
1313 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001315 if (i > curbuf->b_ml.ml_line_count)
1316 i = curbuf->b_ml.ml_line_count;
1317 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001319 if (qf_ptr->qf_col > 0)
1320 {
1321 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1322 if (qf_ptr->qf_viscol == TRUE)
1323 {
1324 /*
1325 * Check each character from the beginning of the error
1326 * line up to the error column. For each tab character
1327 * found, reduce the error column value by the length of
1328 * a tab character.
1329 */
1330 line = ml_get_curline();
1331 screen_col = 0;
1332 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1333 {
1334 if (*line == NUL)
1335 break;
1336 if (*line++ == '\t')
1337 {
1338 curwin->w_cursor.col -= 7 - (screen_col % 8);
1339 screen_col += 8 - (screen_col % 8);
1340 }
1341 else
1342 ++screen_col;
1343 }
1344 }
1345 check_cursor();
1346 }
1347 else
1348 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001351 {
1352 pos_T save_cursor;
1353
1354 /* Move the cursor to the first line in the buffer */
1355 save_cursor = curwin->w_cursor;
1356 curwin->w_cursor.lnum = 0;
1357 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1358 curwin->w_cursor = save_cursor;
1359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361#ifdef FEAT_FOLDING
1362 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1363 foldOpenCursor();
1364#endif
1365 if (print_message)
1366 {
1367 /* Update the screen before showing the message */
1368 update_topline_redraw();
1369 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1370 qf_lists[qf_curlist].qf_count,
1371 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1372 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1373 /* Add the message, skipping leading whitespace and newlines. */
1374 len = (int)STRLEN(IObuff);
1375 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1376
1377 /* Output the message. Overwrite to avoid scrolling when the 'O'
1378 * flag is present in 'shortmess'; But when not jumping, print the
1379 * whole message. */
1380 i = msg_scroll;
1381 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1382 msg_scroll = TRUE;
1383 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1384 msg_scroll = FALSE;
1385 msg_attr_keep(IObuff, 0, TRUE);
1386 msg_scroll = i;
1387 }
1388 }
1389 else
1390 {
1391#ifdef FEAT_WINDOWS
1392 if (opened_window)
1393 win_close(curwin, TRUE); /* Close opened window */
1394#endif
1395 if (qf_ptr->qf_fnum != 0)
1396 {
1397 /*
1398 * Couldn't open file, so put index back where it was. This could
1399 * happen if the file was readonly and we changed something.
1400 */
1401#ifdef FEAT_WINDOWS
1402failed:
1403#endif
1404 qf_ptr = old_qf_ptr;
1405 qf_index = old_qf_index;
1406 }
1407 }
1408theend:
1409 qf_lists[qf_curlist].qf_ptr = qf_ptr;
1410 qf_lists[qf_curlist].qf_index = qf_index;
1411#ifdef FEAT_WINDOWS
1412 if (p_swb != old_swb && opened_window)
1413 {
1414 /* Restore old 'switchbuf' value, but not when an autocommand or
1415 * modeline has changed the value. */
1416 if (p_swb == empty_option)
1417 p_swb = old_swb;
1418 else
1419 free_string_option(old_swb);
1420 }
1421#endif
1422}
1423
1424/*
1425 * ":clist": list all errors
1426 */
1427 void
1428qf_list(eap)
1429 exarg_T *eap;
1430{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001431 buf_T *buf;
1432 char_u *fname;
1433 qfline_T *qfp;
1434 int i;
1435 int idx1 = 1;
1436 int idx2 = -1;
1437 int need_return = TRUE;
1438 int last_printed = 1;
1439 char_u *arg = eap->arg;
1440 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 recognised errors */
1442
1443 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1444 {
1445 EMSG(_(e_quickfix));
1446 return;
1447 }
1448 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1449 {
1450 EMSG(_(e_trailing));
1451 return;
1452 }
1453 i = qf_lists[qf_curlist].qf_count;
1454 if (idx1 < 0)
1455 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1456 if (idx2 < 0)
1457 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1458
1459 more_back_used = TRUE;
1460 if (qf_lists[qf_curlist].qf_nonevalid)
1461 all = TRUE;
1462 qfp = qf_lists[qf_curlist].qf_start;
1463 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
1464 {
1465 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1466 {
1467 if (need_return)
1468 {
1469 msg_putchar('\n');
1470 need_return = FALSE;
1471 }
1472 if (more_back == 0)
1473 {
1474 fname = NULL;
1475 if (qfp->qf_fnum != 0
1476 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1477 {
1478 fname = buf->b_fname;
1479 if (qfp->qf_type == 1) /* :helpgrep */
1480 fname = gettail(fname);
1481 }
1482 if (fname == NULL)
1483 sprintf((char *)IObuff, "%2d", i);
1484 else
Bram Moolenaar051b7822005-05-19 21:00:46 +00001485 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1486 i, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1488 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1489 if (qfp->qf_lnum == 0)
1490 IObuff[0] = NUL;
1491 else if (qfp->qf_col == 0)
1492 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1493 else
1494 sprintf((char *)IObuff, ":%ld col %d",
1495 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001496 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1498 msg_puts_attr(IObuff, hl_attr(HLF_N));
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001499 if (qfp->qf_pattern != NULL)
1500 {
1501 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1502 STRCAT(IObuff, ":");
1503 msg_puts(IObuff);
1504 }
1505 msg_puts((char_u *)" ");
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 /* Remove newlines and leading whitespace from the text.
1508 * For an unrecognized line keep the indent, the compiler may
1509 * mark a word with ^^^^. */
1510 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1511 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1512 IObuff, IOSIZE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001513 msg_prt_line(IObuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 out_flush(); /* show one line at a time */
1515 need_return = TRUE;
1516 last_printed = i;
1517 }
1518 }
1519 if (more_back)
1520 {
1521 /* scrolling backwards from the more-prompt */
1522 /* TODO: compute the number of items from the screen lines */
1523 more_back = more_back * 2 - 1;
1524 while (i > last_printed - more_back && i > idx1)
1525 {
1526 do
1527 {
1528 qfp = qfp->qf_prev;
1529 --i;
1530 }
1531 while (i > idx1 && !qfp->qf_valid && !all);
1532 }
1533 more_back = 0;
1534 }
1535 else
1536 {
1537 qfp = qfp->qf_next;
1538 ++i;
1539 }
1540 ui_breakcheck();
1541 }
1542 more_back_used = FALSE;
1543}
1544
1545/*
1546 * Remove newlines and leading whitespace from an error message.
1547 * Put the result in "buf[bufsize]".
1548 */
1549 static void
1550qf_fmt_text(text, buf, bufsize)
1551 char_u *text;
1552 char_u *buf;
1553 int bufsize;
1554{
1555 int i;
1556 char_u *p = text;
1557
1558 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1559 {
1560 if (*p == '\n')
1561 {
1562 buf[i] = ' ';
1563 while (*++p != NUL)
1564 if (!vim_iswhite(*p) && *p != '\n')
1565 break;
1566 }
1567 else
1568 buf[i] = *p++;
1569 }
1570 buf[i] = NUL;
1571}
1572
1573/*
1574 * ":colder [count]": Up in the quickfix stack.
1575 * ":cnewer [count]": Down in the quickfix stack.
1576 */
1577 void
1578qf_age(eap)
1579 exarg_T *eap;
1580{
1581 int count;
1582
1583 if (eap->addr_count != 0)
1584 count = eap->line2;
1585 else
1586 count = 1;
1587 while (count--)
1588 {
1589 if (eap->cmdidx == CMD_colder)
1590 {
1591 if (qf_curlist == 0)
1592 {
1593 EMSG(_("E380: At bottom of quickfix stack"));
1594 return;
1595 }
1596 --qf_curlist;
1597 }
1598 else
1599 {
1600 if (qf_curlist >= qf_listcount - 1)
1601 {
1602 EMSG(_("E381: At top of quickfix stack"));
1603 return;
1604 }
1605 ++qf_curlist;
1606 }
1607 }
1608 qf_msg();
1609}
1610
1611 static void
1612qf_msg()
1613{
1614 smsg((char_u *)_("error list %d of %d; %d errors"),
1615 qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1616#ifdef FEAT_WINDOWS
1617 qf_update_buffer();
1618#endif
1619}
1620
1621/*
1622 * free the error list
1623 */
1624 static void
1625qf_free(idx)
1626 int idx;
1627{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001628 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 while (qf_lists[idx].qf_count)
1631 {
1632 qfp = qf_lists[idx].qf_start->qf_next;
1633 vim_free(qf_lists[idx].qf_start->qf_text);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001634 vim_free(qf_lists[idx].qf_start->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 vim_free(qf_lists[idx].qf_start);
1636 qf_lists[idx].qf_start = qfp;
1637 --qf_lists[idx].qf_count;
1638 }
1639}
1640
1641/*
1642 * qf_mark_adjust: adjust marks
1643 */
1644 void
1645qf_mark_adjust(line1, line2, amount, amount_after)
1646 linenr_T line1;
1647 linenr_T line2;
1648 long amount;
1649 long amount_after;
1650{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001651 int i;
1652 qfline_T *qfp;
1653 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
1655 for (idx = 0; idx < qf_listcount; ++idx)
1656 if (qf_lists[idx].qf_count)
1657 for (i = 0, qfp = qf_lists[idx].qf_start;
1658 i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1659 if (qfp->qf_fnum == curbuf->b_fnum)
1660 {
1661 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1662 {
1663 if (amount == MAXLNUM)
1664 qfp->qf_cleared = TRUE;
1665 else
1666 qfp->qf_lnum += amount;
1667 }
1668 else if (amount_after && qfp->qf_lnum > line2)
1669 qfp->qf_lnum += amount_after;
1670 }
1671}
1672
1673/*
1674 * Make a nice message out of the error character and the error number:
1675 * char number message
1676 * e or E 0 " error"
1677 * w or W 0 " warning"
1678 * i or I 0 " info"
1679 * 0 0 ""
1680 * other 0 " c"
1681 * e or E n " error n"
1682 * w or W n " warning n"
1683 * i or I n " info n"
1684 * 0 n " error n"
1685 * other n " c n"
1686 * 1 x "" :helpgrep
1687 */
1688 static char_u *
1689qf_types(c, nr)
1690 int c, nr;
1691{
1692 static char_u buf[20];
1693 static char_u cc[3];
1694 char_u *p;
1695
1696 if (c == 'W' || c == 'w')
1697 p = (char_u *)" warning";
1698 else if (c == 'I' || c == 'i')
1699 p = (char_u *)" info";
1700 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1701 p = (char_u *)" error";
1702 else if (c == 0 || c == 1)
1703 p = (char_u *)"";
1704 else
1705 {
1706 cc[0] = ' ';
1707 cc[1] = c;
1708 cc[2] = NUL;
1709 p = cc;
1710 }
1711
1712 if (nr <= 0)
1713 return p;
1714
1715 sprintf((char *)buf, "%s %3d", (char *)p, nr);
1716 return buf;
1717}
1718
1719#if defined(FEAT_WINDOWS) || defined(PROTO)
1720/*
1721 * ":cwindow": open the quickfix window if we have errors to display,
1722 * close it if not.
1723 */
1724 void
1725ex_cwindow(eap)
1726 exarg_T *eap;
1727{
1728 win_T *win;
1729
1730 /*
1731 * Look for an existing quickfix window.
1732 */
1733 for (win = firstwin; win != NULL; win = win->w_next)
1734 if (bt_quickfix(win->w_buffer))
1735 break;
1736
1737 /*
1738 * If a quickfix window is open but we have no errors to display,
1739 * close the window. If a quickfix window is not open, then open
1740 * it if we have errors; otherwise, leave it closed.
1741 */
1742 if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1743 {
1744 if (win != NULL)
1745 ex_cclose(eap);
1746 }
1747 else if (win == NULL)
1748 ex_copen(eap);
1749}
1750
1751/*
1752 * ":cclose": close the window showing the list of errors.
1753 */
1754/*ARGSUSED*/
1755 void
1756ex_cclose(eap)
1757 exarg_T *eap;
1758{
1759 win_T *win;
1760
1761 /*
1762 * Find existing quickfix window and close it.
1763 */
1764 for (win = firstwin; win != NULL; win = win->w_next)
1765 if (bt_quickfix(win->w_buffer))
1766 break;
1767
1768 if (win != NULL)
1769 win_close(win, FALSE);
1770}
1771
1772/*
1773 * ":copen": open a window that shows the list of errors.
1774 */
1775 void
1776ex_copen(eap)
1777 exarg_T *eap;
1778{
1779 int height;
1780 buf_T *buf;
1781 win_T *win;
1782
1783 if (eap->addr_count != 0)
1784 height = eap->line2;
1785 else
1786 height = QF_WINHEIGHT;
1787
1788#ifdef FEAT_VISUAL
1789 reset_VIsual_and_resel(); /* stop Visual mode */
1790#endif
1791#ifdef FEAT_GUI
1792 need_mouse_correct = TRUE;
1793#endif
1794
1795 /*
1796 * Find existing quickfix window, or open a new one.
1797 */
1798 for (win = firstwin; win != NULL; win = win->w_next)
1799 if (bt_quickfix(win->w_buffer))
1800 break;
1801 if (win != NULL)
1802 win_goto(win);
1803 else
1804 {
1805 /* The current window becomes the previous window afterwards. */
1806 win = curwin;
1807
1808 /* Create the new window at the very bottom. */
1809 win_goto(lastwin);
1810 if (win_split(height, WSP_BELOW) == FAIL)
1811 return; /* not enough room for window */
1812#ifdef FEAT_SCROLLBIND
1813 curwin->w_p_scb = FALSE;
1814#endif
1815
1816 /*
1817 * Find existing quickfix buffer, or create a new one.
1818 */
1819 buf = qf_find_buf();
1820 if (buf == NULL)
1821 {
1822 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1823 /* switch off 'swapfile' */
1824 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1825 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1826 OPT_LOCAL);
1827 set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1828 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1829 }
1830 else if (buf != curbuf)
1831 set_curbuf(buf, DOBUF_GOTO);
1832
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001833#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 /* Only set the height when there is no window to the side. */
1835 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 win_setheight(height);
1838 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
1839 if (win_valid(win))
1840 prevwin = win;
1841 }
1842
1843 /*
1844 * Fill the buffer with the quickfix list.
1845 */
1846 qf_fill_buffer();
1847
1848 curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1849 curwin->w_cursor.col = 0;
1850 check_cursor();
1851 update_topline(); /* scroll to show the line */
1852}
1853
1854/*
1855 * Return the number of the current entry (line number in the quickfix
1856 * window).
1857 */
1858 linenr_T
1859qf_current_entry()
1860{
1861 return qf_lists[qf_curlist].qf_index;
1862}
1863
1864/*
1865 * Update the cursor position in the quickfix window to the current error.
1866 * Return TRUE if there is a quickfix window.
1867 */
1868 static int
1869qf_win_pos_update(old_qf_index)
1870 int old_qf_index; /* previous qf_index or zero */
1871{
1872 win_T *win;
1873 int qf_index = qf_lists[qf_curlist].qf_index;
1874
1875 /*
1876 * Put the cursor on the current error in the quickfix window, so that
1877 * it's viewable.
1878 */
1879 for (win = firstwin; win != NULL; win = win->w_next)
1880 if (bt_quickfix(win->w_buffer))
1881 break;
1882 if (win != NULL
1883 && qf_index <= win->w_buffer->b_ml.ml_line_count
1884 && old_qf_index != qf_index)
1885 {
1886 win_T *old_curwin = curwin;
1887
1888 curwin = win;
1889 curbuf = win->w_buffer;
1890 if (qf_index > old_qf_index)
1891 {
1892 curwin->w_redraw_top = old_qf_index;
1893 curwin->w_redraw_bot = qf_index;
1894 }
1895 else
1896 {
1897 curwin->w_redraw_top = qf_index;
1898 curwin->w_redraw_bot = old_qf_index;
1899 }
1900 curwin->w_cursor.lnum = qf_index;
1901 curwin->w_cursor.col = 0;
1902 update_topline(); /* scroll to show the line */
1903 redraw_later(VALID);
1904 curwin->w_redr_status = TRUE; /* update ruler */
1905 curwin = old_curwin;
1906 curbuf = curwin->w_buffer;
1907 }
1908 return win != NULL;
1909}
1910
1911/*
1912 * Find quickfix buffer.
1913 */
1914 static buf_T *
1915qf_find_buf()
1916{
1917 buf_T *buf;
1918
1919 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1920 if (bt_quickfix(buf))
1921 break;
1922 return buf;
1923}
1924
1925/*
1926 * Find the quickfix buffer. If it exists, update the contents.
1927 */
1928 static void
1929qf_update_buffer()
1930{
1931 buf_T *buf;
1932#ifdef FEAT_AUTOCMD
1933 aco_save_T aco;
1934#else
1935 buf_T *save_curbuf;
1936#endif
1937
1938 /* Check if a buffer for the quickfix list exists. Update it. */
1939 buf = qf_find_buf();
1940 if (buf != NULL)
1941 {
1942#ifdef FEAT_AUTOCMD
1943 /* set curwin/curbuf to buf and save a few things */
1944 aucmd_prepbuf(&aco, buf);
1945#else
1946 save_curbuf = curbuf;
1947 curbuf = buf;
1948#endif
1949
1950 qf_fill_buffer();
1951
1952#ifdef FEAT_AUTOCMD
1953 /* restore curwin/curbuf and a few other things */
1954 aucmd_restbuf(&aco);
1955#else
1956 curbuf = save_curbuf;
1957#endif
1958
1959 (void)qf_win_pos_update(0);
1960 }
1961}
1962
1963/*
1964 * Fill current buffer with quickfix errors, replacing any previous contents.
1965 * curbuf must be the quickfix buffer!
1966 */
1967 static void
1968qf_fill_buffer()
1969{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001970 linenr_T lnum;
1971 qfline_T *qfp;
1972 buf_T *errbuf;
1973 int len;
1974 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 /* delete all existing lines */
1977 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
1978 (void)ml_delete((linenr_T)1, FALSE);
1979
1980 /* Check if there is anything to display */
1981 if (qf_curlist < qf_listcount)
1982 {
1983 /* Add one line for each error */
1984 qfp = qf_lists[qf_curlist].qf_start;
1985 for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
1986 {
1987 if (qfp->qf_fnum != 0
1988 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
1989 && errbuf->b_fname != NULL)
1990 {
1991 if (qfp->qf_type == 1) /* :helpgrep */
1992 STRCPY(IObuff, gettail(errbuf->b_fname));
1993 else
1994 STRCPY(IObuff, errbuf->b_fname);
1995 len = (int)STRLEN(IObuff);
1996 }
1997 else
1998 len = 0;
1999 IObuff[len++] = '|';
2000
2001 if (qfp->qf_lnum > 0)
2002 {
2003 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2004 len += (int)STRLEN(IObuff + len);
2005
2006 if (qfp->qf_col > 0)
2007 {
2008 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2009 len += (int)STRLEN(IObuff + len);
2010 }
2011
2012 sprintf((char *)IObuff + len, "%s",
2013 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2014 len += (int)STRLEN(IObuff + len);
2015 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002016 else if (qfp->qf_pattern != NULL)
2017 {
2018 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2019 len += (int)STRLEN(IObuff + len);
2020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 IObuff[len++] = '|';
2022 IObuff[len++] = ' ';
2023
2024 /* Remove newlines and leading whitespace from the text.
2025 * For an unrecognized line keep the indent, the compiler may
2026 * mark a word with ^^^^. */
2027 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2028 IObuff + len, IOSIZE - len);
2029
2030 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2031 == FAIL)
2032 break;
2033 qfp = qfp->qf_next;
2034 }
2035 /* Delete the empty line which is now at the end */
2036 (void)ml_delete(lnum + 1, FALSE);
2037 }
2038
2039 /* correct cursor position */
2040 check_lnums(TRUE);
2041
2042 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2043 * resembles reading a file into a buffer, it's more logical when using
2044 * autocommands. */
2045 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2046 curbuf->b_p_ma = FALSE;
2047
2048#ifdef FEAT_AUTOCMD
2049 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2050 FALSE, curbuf);
2051 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2052 FALSE, curbuf);
2053#endif
2054
2055 /* make sure it will be redrawn */
2056 redraw_curbuf_later(NOT_VALID);
2057
2058 /* Restore KeyTyped, setting 'filetype' may reset it. */
2059 KeyTyped = old_KeyTyped;
2060}
2061
2062#endif /* FEAT_WINDOWS */
2063
2064/*
2065 * Return TRUE if "buf" is the quickfix buffer.
2066 */
2067 int
2068bt_quickfix(buf)
2069 buf_T *buf;
2070{
2071 return (buf->b_p_bt[0] == 'q');
2072}
2073
2074/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002075 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2076 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 */
2078 int
2079bt_nofile(buf)
2080 buf_T *buf;
2081{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002082 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2083 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084}
2085
2086/*
2087 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2088 */
2089 int
2090bt_dontwrite(buf)
2091 buf_T *buf;
2092{
2093 return (buf->b_p_bt[0] == 'n');
2094}
2095
2096 int
2097bt_dontwrite_msg(buf)
2098 buf_T *buf;
2099{
2100 if (bt_dontwrite(buf))
2101 {
2102 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2103 return TRUE;
2104 }
2105 return FALSE;
2106}
2107
2108/*
2109 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2110 * and 'bufhidden'.
2111 */
2112 int
2113buf_hide(buf)
2114 buf_T *buf;
2115{
2116 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2117 switch (buf->b_p_bh[0])
2118 {
2119 case 'u': /* "unload" */
2120 case 'w': /* "wipe" */
2121 case 'd': return FALSE; /* "delete" */
2122 case 'h': return TRUE; /* "hide" */
2123 }
2124 return (p_hid || cmdmod.hide);
2125}
2126
2127/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002128 * Return TRUE when using ":vimgrep" for ":grep".
2129 */
2130 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002131grep_internal(cmdidx)
2132 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002133{
Bram Moolenaar81695252004-12-29 20:58:21 +00002134 return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002135 && STRCMP("internal",
2136 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2137}
2138
2139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 * Used for ":make", ":grep" and ":grepadd".
2141 */
2142 void
2143ex_make(eap)
2144 exarg_T *eap;
2145{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002146 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 char_u *cmd;
2148 unsigned len;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002149#ifdef FEAT_AUTOCMD
2150 char_u *au_name = NULL;
2151
2152 switch (eap->cmdidx)
2153 {
2154 case CMD_make: au_name = (char_u *)"make"; break;
2155 case CMD_grep: au_name = (char_u *)"grep"; break;
2156 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2157 default: break;
2158 }
2159 if (au_name != NULL)
2160 {
2161 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2162 curbuf->b_fname, TRUE, curbuf);
2163 if (did_throw || force_abort)
2164 return;
2165 }
2166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaar86b68352004-12-27 21:59:20 +00002168 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002169 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002170 {
2171 ex_vimgrep(eap);
2172 return;
2173 }
2174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002176 fname = get_mef_name();
2177 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002179 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
2181 /*
2182 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2183 */
2184 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2185 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002186 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 cmd = alloc(len);
2188 if (cmd == NULL)
2189 return;
2190 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2191 (char *)p_shq);
2192 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002193 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 /*
2195 * Output a newline if there's something else than the :make command that
2196 * was typed (in which case the cursor is in column 0).
2197 */
2198 if (msg_col == 0)
2199 msg_didout = FALSE;
2200 msg_start();
2201 MSG_PUTS(":!");
2202 msg_outtrans(cmd); /* show what we are doing */
2203
2204 /* let the shell know if we are redirecting output or not */
2205 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2206
2207#ifdef AMIGA
2208 out_flush();
2209 /* read window status report and redraw before message */
2210 (void)char_avail();
2211#endif
2212
Bram Moolenaar7c626922005-02-07 22:01:03 +00002213 if (qf_init(fname, eap->cmdidx != CMD_make ? p_gefm : p_efm,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002214 eap->cmdidx != CMD_grepadd) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 && !eap->forceit)
2216 qf_jump(0, 0, FALSE); /* display first error */
2217
Bram Moolenaar7c626922005-02-07 22:01:03 +00002218 mch_remove(fname);
2219 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002221
2222#ifdef FEAT_AUTOCMD
2223 if (au_name != NULL)
2224 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2225 curbuf->b_fname, TRUE, curbuf);
2226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227}
2228
2229/*
2230 * Return the name for the errorfile, in allocated memory.
2231 * Find a new unique name when 'makeef' contains "##".
2232 * Returns NULL for error.
2233 */
2234 static char_u *
2235get_mef_name()
2236{
2237 char_u *p;
2238 char_u *name;
2239 static int start = -1;
2240 static int off = 0;
2241#ifdef HAVE_LSTAT
2242 struct stat sb;
2243#endif
2244
2245 if (*p_mef == NUL)
2246 {
2247 name = vim_tempname('e');
2248 if (name == NULL)
2249 EMSG(_(e_notmp));
2250 return name;
2251 }
2252
2253 for (p = p_mef; *p; ++p)
2254 if (p[0] == '#' && p[1] == '#')
2255 break;
2256
2257 if (*p == NUL)
2258 return vim_strsave(p_mef);
2259
2260 /* Keep trying until the name doesn't exist yet. */
2261 for (;;)
2262 {
2263 if (start == -1)
2264 start = mch_get_pid();
2265 else
2266 off += 19;
2267
2268 name = alloc((unsigned)STRLEN(p_mef) + 30);
2269 if (name == NULL)
2270 break;
2271 STRCPY(name, p_mef);
2272 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2273 STRCAT(name, p + 2);
2274 if (mch_getperm(name) < 0
2275#ifdef HAVE_LSTAT
2276 /* Don't accept a symbolic link, its a security risk. */
2277 && mch_lstat((char *)name, &sb) < 0
2278#endif
2279 )
2280 break;
2281 vim_free(name);
2282 }
2283 return name;
2284}
2285
2286/*
2287 * ":cc", ":crewind", ":cfirst" and ":clast".
2288 */
2289 void
2290ex_cc(eap)
2291 exarg_T *eap;
2292{
2293 qf_jump(0,
2294 eap->addr_count > 0
2295 ? (int)eap->line2
2296 : eap->cmdidx == CMD_cc
2297 ? 0
2298 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2299 ? 1
2300 : 32767,
2301 eap->forceit);
2302}
2303
2304/*
2305 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2306 */
2307 void
2308ex_cnext(eap)
2309 exarg_T *eap;
2310{
2311 qf_jump(eap->cmdidx == CMD_cnext
2312 ? FORWARD
2313 : eap->cmdidx == CMD_cnfile
2314 ? FORWARD_FILE
2315 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2316 ? BACKWARD_FILE
2317 : BACKWARD,
2318 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2319}
2320
2321/*
2322 * ":cfile" command.
2323 */
2324 void
2325ex_cfile(eap)
2326 exarg_T *eap;
2327{
2328 if (*eap->arg != NUL)
2329 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
2330 if (qf_init(p_ef, p_efm, TRUE) > 0 && eap->cmdidx == CMD_cfile)
2331 qf_jump(0, 0, eap->forceit); /* display first error */
2332}
2333
2334/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002335 * ":vimgrep {pattern} file(s)"
2336 */
2337 void
2338ex_vimgrep(eap)
2339 exarg_T *eap;
2340{
Bram Moolenaar81695252004-12-29 20:58:21 +00002341 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002342 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002343 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002344 char_u *s;
2345 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002346 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002347 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002348 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002349 buf_T *buf;
2350 int duplicate_name = FALSE;
2351 int using_dummy;
2352 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002353 buf_T *first_match_buf = NULL;
2354 time_t seconds = 0;
2355#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2356 char_u *save_ei = NULL;
2357 aco_save_T aco;
2358#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002359#ifdef FEAT_AUTOCMD
2360 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002361 int flags = 0;
2362 colnr_T col;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002363
2364 switch (eap->cmdidx)
2365 {
2366 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
2367 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
2368 default: break;
2369 }
2370 if (au_name != NULL)
2371 {
2372 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2373 curbuf->b_fname, TRUE, curbuf);
2374 if (did_throw || force_abort)
2375 return;
2376 }
2377#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002378
Bram Moolenaar81695252004-12-29 20:58:21 +00002379 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002380 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002381 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002382 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002383 {
Bram Moolenaar748bf032005-02-02 23:04:36 +00002384 EMSG(_("E682: Invalid search pattern or delimiter"));
2385 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002386 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002387 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002388 if (regmatch.regprog == NULL)
2389 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002390 regmatch.rmm_ic = p_ic;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002391
2392 p = skipwhite(p);
2393 if (*p == NUL)
2394 {
2395 EMSG(_("E683: File name missing or invalid pattern"));
2396 goto theend;
2397 }
2398
2399 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
2400 || qf_curlist == qf_listcount)
2401 /* make place for a new list */
2402 qf_new_list();
2403 else if (qf_lists[qf_curlist].qf_count > 0)
2404 /* Adding to existing list, find last entry. */
2405 for (prevp = qf_lists[qf_curlist].qf_start;
2406 prevp->qf_next != prevp; prevp = prevp->qf_next)
2407 ;
2408
2409 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002410 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002411 goto theend;
2412 if (fcount == 0)
2413 {
2414 EMSG(_(e_nomatch));
2415 goto theend;
2416 }
2417
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002418 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002419 for (fi = 0; fi < fcount && !got_int; ++fi)
2420 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002421 if (time(NULL) > seconds)
2422 {
2423 /* Display the file name every second or so. */
2424 seconds = time(NULL);
2425 msg_start();
2426 p = msg_strtrunc(fnames[fi]);
2427 if (p == NULL)
2428 msg_outtrans(fnames[fi]);
2429 else
2430 {
2431 msg_outtrans(p);
2432 vim_free(p);
2433 }
2434 msg_clr_eos();
2435 msg_didout = FALSE; /* overwrite this message */
2436 msg_nowait = TRUE; /* don't wait for this message */
2437 msg_col = 0;
2438 out_flush();
2439 }
2440
Bram Moolenaar81695252004-12-29 20:58:21 +00002441 buf = buflist_findname_exp(fnames[fi]);
2442 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2443 {
2444 /* Remember that a buffer with this name already exists. */
2445 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002446 using_dummy = TRUE;
2447
2448#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2449 /* Don't do Filetype autocommands to avoid loading syntax and
2450 * indent scripts, a great speed improvement. */
2451 save_ei = au_event_disable(",Filetype");
2452#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002453
2454 /* Load file into a buffer, so that 'fileencoding' is detected,
2455 * autocommands applied, etc. */
2456 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002457
2458#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2459 au_event_restore(save_ei);
2460#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002461 }
2462 else
2463 /* Use existing, loaded buffer. */
2464 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002465
Bram Moolenaar81695252004-12-29 20:58:21 +00002466 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002467 {
2468 if (!got_int)
2469 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2470 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002471 else
2472 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002473 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002474 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002475 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002476 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002477 /* For ":1vimgrep" look for multiple matches. */
2478 col = 0;
2479 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2480 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002481 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00002482 if (qf_add_entry(&prevp,
2483 NULL, /* dir */
2484 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00002485 ml_get_buf(buf,
2486 regmatch.startpos[0].lnum + lnum, FALSE),
2487 regmatch.startpos[0].lnum + lnum,
2488 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00002489 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002490 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002491 0, /* nr */
2492 0, /* type */
2493 TRUE /* valid */
2494 ) == FAIL)
2495 {
2496 got_int = TRUE;
2497 break;
2498 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002499 else
2500 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002501 if ((flags & VGR_GLOBAL) == 0
2502 || regmatch.endpos[0].lnum > 0)
2503 break;
2504 col = regmatch.endpos[0].col
2505 + (col == regmatch.endpos[0].col);
2506 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
2507 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002508 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002509 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00002510 if (got_int)
2511 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002512 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002513
2514 if (using_dummy)
2515 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002516 if (found_match && first_match_buf == NULL)
2517 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00002518 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002519 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002520 /* Never keep a dummy buffer if there is another buffer
2521 * with the same name. */
2522 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002523 buf = NULL;
2524 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002525 else if (!buf_hide(buf))
2526 {
2527 /* When not hiding the buffer and no match was found we
2528 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00002529 * there was a match and it wasn't the first one or we
2530 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002531 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002532 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002533 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002534 buf = NULL;
2535 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002536 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002537 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002538 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002539 buf = NULL;
2540 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002541 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002542
2543#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2544 if (buf != NULL)
2545 {
2546 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaar748bf032005-02-02 23:04:36 +00002547 * need to be done now, in that buffer. And then the
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002548 * modelines need to be done (again). */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002549 aucmd_prepbuf(&aco, buf);
2550 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
2551 buf->b_fname, TRUE, buf);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002552 do_modelines(FALSE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002553 aucmd_restbuf(&aco);
2554 }
2555#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002556 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002557 }
2558 }
2559
2560 FreeWild(fcount, fnames);
2561
2562 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2563 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2564 qf_lists[qf_curlist].qf_index = 1;
2565
2566#ifdef FEAT_WINDOWS
2567 qf_update_buffer();
2568#endif
2569
2570 /* Jump to first match. */
2571 if (qf_lists[qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002572 {
2573 if ((flags & VGR_NOJUMP) == 0)
2574 qf_jump(0, 0, eap->forceit);
2575 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002576 else
2577 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002578
Bram Moolenaar7c626922005-02-07 22:01:03 +00002579#ifdef FEAT_AUTOCMD
2580 if (au_name != NULL)
2581 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2582 curbuf->b_fname, TRUE, curbuf);
2583#endif
2584
Bram Moolenaar86b68352004-12-27 21:59:20 +00002585theend:
2586 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002587}
2588
2589/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00002590 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00002591 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00002592 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
2593 * If "s" is not NULL terminate the pattern with a NUL.
2594 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00002595 */
2596 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00002597skip_vimgrep_pat(p, s, flags)
2598 char_u *p;
2599 char_u **s;
2600 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002601{
2602 int c;
2603
2604 if (vim_isIDc(*p))
2605 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002606 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00002607 if (s != NULL)
2608 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002609 p = skiptowhite(p);
2610 if (s != NULL && *p != NUL)
2611 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002612 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002613 else
2614 {
2615 /* ":vimgrep /pattern/[g][j] fname" */
2616 if (s != NULL)
2617 *s = p + 1;
2618 c = *p;
2619 p = skip_regexp(p + 1, c, TRUE, NULL);
2620 if (*p != c)
2621 return NULL;
2622
2623 /* Truncate the pattern. */
2624 if (s != NULL)
2625 *p = NUL;
2626 ++p;
2627
2628 /* Find the flags */
2629 while (*p == 'g' || *p == 'j')
2630 {
2631 if (flags != NULL)
2632 {
2633 if (*p == 'g')
2634 *flags |= VGR_GLOBAL;
2635 else
2636 *flags |= VGR_NOJUMP;
2637 }
2638 ++p;
2639 }
2640 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00002641 return p;
2642}
2643
2644/*
Bram Moolenaar81695252004-12-29 20:58:21 +00002645 * Load file "fname" into a dummy buffer and return the buffer pointer.
2646 * Returns NULL if it fails.
2647 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
2648 */
2649 static buf_T *
2650load_dummy_buffer(fname)
2651 char_u *fname;
2652{
2653 buf_T *newbuf;
2654 int failed = TRUE;
2655#ifdef FEAT_AUTOCMD
2656 aco_save_T aco;
2657#else
2658 buf_T *old_curbuf = curbuf;
2659#endif
2660
2661 /* Allocate a buffer without putting it in the buffer list. */
2662 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
2663 if (newbuf == NULL)
2664 return NULL;
2665
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002666 /* Init the options. */
2667 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
2668
Bram Moolenaar81695252004-12-29 20:58:21 +00002669#ifdef FEAT_AUTOCMD
2670 /* set curwin/curbuf to buf and save a few things */
2671 aucmd_prepbuf(&aco, newbuf);
2672#else
2673 curbuf = newbuf;
2674 curwin->w_buffer = newbuf;
2675#endif
2676
2677 /* Need to set the filename for autocommands. */
2678 (void)setfname(curbuf, fname, NULL, FALSE);
2679
2680 if (ml_open() == OK)
2681 {
2682 /* Create swap file now to avoid the ATTENTION message. */
2683 check_need_swap(TRUE);
2684
2685 /* Remove the "dummy" flag, otherwise autocommands may not
2686 * work. */
2687 curbuf->b_flags &= ~BF_DUMMY;
2688
2689 if (readfile(fname, NULL,
2690 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
2691 NULL, READ_NEW | READ_DUMMY) == OK
2692 && !(curbuf->b_flags & BF_NEW))
2693 {
2694 failed = FALSE;
2695 if (curbuf != newbuf)
2696 {
2697 /* Bloody autocommands changed the buffer! */
2698 if (buf_valid(newbuf))
2699 wipe_buffer(newbuf, FALSE);
2700 newbuf = curbuf;
2701 }
2702 }
2703 }
2704
2705#ifdef FEAT_AUTOCMD
2706 /* restore curwin/curbuf and a few other things */
2707 aucmd_restbuf(&aco);
2708#else
2709 curbuf = old_curbuf;
2710 curwin->w_buffer = old_curbuf;
2711#endif
2712
2713 if (!buf_valid(newbuf))
2714 return NULL;
2715 if (failed)
2716 {
2717 wipe_dummy_buffer(newbuf);
2718 return NULL;
2719 }
2720 return newbuf;
2721}
2722
2723/*
2724 * Wipe out the dummy buffer that load_dummy_buffer() created.
2725 */
2726 static void
2727wipe_dummy_buffer(buf)
2728 buf_T *buf;
2729{
2730 if (curbuf != buf) /* safety check */
2731 wipe_buffer(buf, FALSE);
2732}
2733
2734/*
2735 * Unload the dummy buffer that load_dummy_buffer() created.
2736 */
2737 static void
2738unload_dummy_buffer(buf)
2739 buf_T *buf;
2740{
2741 if (curbuf != buf) /* safety check */
2742 close_buffer(NULL, buf, DOBUF_UNLOAD);
2743}
2744
Bram Moolenaar05159a02005-02-26 23:04:13 +00002745#if defined(FEAT_EVAL) || defined(PROTO)
2746/*
2747 * Add each quickfix error to list "list" as a dictionary.
2748 */
2749 int
2750get_errorlist(list)
2751 list_T *list;
2752{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002753 dict_T *dict;
2754 char_u buf[2];
2755 qfline_T *qfp;
2756 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002757
2758 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
2759 {
2760 EMSG(_(e_quickfix));
2761 return FAIL;
2762 }
2763
2764 qfp = qf_lists[qf_curlist].qf_start;
2765 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; ++i)
2766 {
2767 if ((dict = dict_alloc()) == NULL)
2768 return FAIL;
2769 if (list_append_dict(list, dict) == FAIL)
2770 return FAIL;
2771
2772 buf[0] = qfp->qf_type;
2773 buf[1] = NUL;
2774 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
2775 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
2776 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
2777 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
2778 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002779 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002780 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
2781 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
2782 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
2783 return FAIL;
2784
2785 qfp = qfp->qf_next;
2786 }
2787 return OK;
2788}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002789
2790/*
2791 * Populate the quickfix list with the items supplied in the list
2792 * of dictionaries.
2793 */
2794 int
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002795set_errorlist(list, action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002796 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002797 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002798{
2799 listitem_T *li;
2800 dict_T *d;
2801 char_u *filename, *pattern, *text, *type;
2802 long lnum;
2803 int col, nr;
2804 int vcol;
2805 qfline_T *prevp = NULL;
2806 int valid, status;
2807 int retval = OK;
2808
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002809 if (action == ' ' || qf_curlist == qf_listcount)
2810 /* make place for a new list */
2811 qf_new_list();
2812 else if (action == 'a' && qf_lists[qf_curlist].qf_count > 0)
2813 /* Adding to existing list, find last entry. */
2814 for (prevp = qf_lists[qf_curlist].qf_start;
2815 prevp->qf_next != prevp; prevp = prevp->qf_next)
2816 ;
2817 else if (action == 'r')
2818 qf_free(qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002819
2820 for (li = list->lv_first; li != NULL; li = li->li_next)
2821 {
2822 if (li->li_tv.v_type != VAR_DICT)
2823 continue; /* Skip non-dict items */
2824
2825 d = li->li_tv.vval.v_dict;
2826 if (d == NULL)
2827 continue;
2828
2829 filename = get_dict_string(d, (char_u *)"filename");
2830 lnum = get_dict_number(d, (char_u *)"lnum");
2831 col = get_dict_number(d, (char_u *)"col");
2832 vcol = get_dict_number(d, (char_u *)"vcol");
2833 nr = get_dict_number(d, (char_u *)"nr");
2834 type = get_dict_string(d, (char_u *)"type");
2835 pattern = get_dict_string(d, (char_u *)"pattern");
2836 text = get_dict_string(d, (char_u *)"text");
2837 if (text == NULL)
2838 text = vim_strsave((char_u *)"");
2839
2840 valid = TRUE;
2841 if (filename == NULL || (lnum == 0 && pattern == NULL))
2842 valid = FALSE;
2843
2844 status = qf_add_entry(&prevp,
2845 NULL, /* dir */
2846 filename,
2847 text,
2848 lnum,
2849 col,
2850 vcol, /* vis_col */
2851 pattern, /* search pattern */
2852 nr,
2853 type == NULL ? NUL : *type,
2854 valid);
2855
2856 vim_free(filename);
2857 vim_free(pattern);
2858 vim_free(text);
2859 vim_free(type);
2860
2861 if (status == FAIL)
2862 {
2863 retval = FAIL;
2864 break;
2865 }
2866 }
2867
2868 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2869 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2870 qf_lists[qf_curlist].qf_index = 1;
2871
2872#ifdef FEAT_WINDOWS
2873 qf_update_buffer();
2874#endif
2875
2876 return retval;
2877}
Bram Moolenaar05159a02005-02-26 23:04:13 +00002878#endif
2879
Bram Moolenaar81695252004-12-29 20:58:21 +00002880/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002881 * ":[range]cbuffer [bufnr]" command.
2882 */
2883 void
2884ex_cbuffer(eap)
2885 exarg_T *eap;
2886{
2887 buf_T *buf = NULL;
2888
2889 if (*eap->arg == NUL)
2890 buf = curbuf;
2891 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
2892 buf = buflist_findnr(atoi((char *)eap->arg));
2893 if (buf == NULL)
2894 EMSG(_(e_invarg));
2895 else if (buf->b_ml.ml_mfp == NULL)
2896 EMSG(_("E681: Buffer is not loaded"));
2897 else
2898 {
2899 if (eap->addr_count == 0)
2900 {
2901 eap->line1 = 1;
2902 eap->line2 = buf->b_ml.ml_line_count;
2903 }
2904 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
2905 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
2906 EMSG(_(e_invrange));
2907 else
2908 qf_init_ext(NULL, buf, p_efm, TRUE, eap->line1, eap->line2);
2909 }
2910}
2911
2912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 * ":helpgrep {pattern}"
2914 */
2915 void
2916ex_helpgrep(eap)
2917 exarg_T *eap;
2918{
2919 regmatch_T regmatch;
2920 char_u *save_cpo;
2921 char_u *p;
2922 int fcount;
2923 char_u **fnames;
2924 FILE *fd;
2925 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002926 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002928#ifdef FEAT_MULTI_LANG
2929 char_u *lang;
2930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2933 save_cpo = p_cpo;
2934 p_cpo = (char_u *)"";
2935
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002936#ifdef FEAT_MULTI_LANG
2937 /* Check for a specified language */
2938 lang = check_help_lang(eap->arg);
2939#endif
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
2942 regmatch.rm_ic = FALSE;
2943 if (regmatch.regprog != NULL)
2944 {
2945 /* create a new quickfix list */
2946 qf_new_list();
2947
2948 /* Go through all directories in 'runtimepath' */
2949 p = p_rtp;
2950 while (*p != NUL && !got_int)
2951 {
2952 copy_option_part(&p, NameBuff, MAXPATHL, ",");
2953
2954 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
2955 add_pathsep(NameBuff);
2956 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
2957 if (gen_expand_wildcards(1, &NameBuff, &fcount,
2958 &fnames, EW_FILE|EW_SILENT) == OK
2959 && fcount > 0)
2960 {
2961 for (fi = 0; fi < fcount && !got_int; ++fi)
2962 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002963#ifdef FEAT_MULTI_LANG
2964 /* Skip files for a different language. */
2965 if (lang != NULL
2966 && STRNICMP(lang, fnames[fi]
2967 + STRLEN(fnames[fi]) - 3, 2) != 0
2968 && !(STRNICMP(lang, "en", 2) == 0
2969 && STRNICMP("txt", fnames[fi]
2970 + STRLEN(fnames[fi]) - 3, 3) == 0))
2971 continue;
2972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 fd = fopen((char *)fnames[fi], "r");
2974 if (fd != NULL)
2975 {
2976 lnum = 1;
2977 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
2978 {
2979 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
2980 {
2981 int l = STRLEN(IObuff);
2982
2983 /* remove trailing CR, LF, spaces, etc. */
2984 while (l > 0 && IObuff[l - 1] <= ' ')
2985 IObuff[--l] = NUL;
2986
2987 if (qf_add_entry(&prevp,
2988 NULL, /* dir */
2989 fnames[fi],
2990 IObuff,
2991 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00002992 (int)(regmatch.startp[0] - IObuff)
2993 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00002994 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002995 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 0, /* nr */
2997 1, /* type */
2998 TRUE /* valid */
2999 ) == FAIL)
3000 {
3001 got_int = TRUE;
3002 break;
3003 }
3004 }
3005 ++lnum;
3006 line_breakcheck();
3007 }
3008 fclose(fd);
3009 }
3010 }
3011 FreeWild(fcount, fnames);
3012 }
3013 }
3014 vim_free(regmatch.regprog);
3015
3016 qf_lists[qf_curlist].qf_nonevalid = FALSE;
3017 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
3018 qf_lists[qf_curlist].qf_index = 1;
3019 }
3020
3021 p_cpo = save_cpo;
3022
3023#ifdef FEAT_WINDOWS
3024 qf_update_buffer();
3025#endif
3026
3027 /* Jump to first match. */
3028 if (qf_lists[qf_curlist].qf_count > 0)
3029 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003030 else
3031 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032}
3033
3034#endif /* FEAT_QUICKFIX */