blob: e022c787a59625e48585770c9fc8c9327e4edaeb [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 {
495 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
496 STRNCPY(namebuf, regmatch.startp[i], len);
497 namebuf[len] = NUL;
498 if (vim_strchr((char_u *)"OPQ", idx) != NULL
499 && mch_getperm(namebuf) == -1)
500 continue;
501 }
502 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
503 enr = (int)atol((char *)regmatch.startp[i]);
504 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
505 lnum = atol((char *)regmatch.startp[i]);
506 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
507 col = (int)atol((char *)regmatch.startp[i]);
508 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
509 type = *regmatch.startp[i];
510 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
511 STRCPY(errmsg, IObuff);
512 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
513 {
514 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
515 STRNCPY(errmsg, regmatch.startp[i], len);
516 errmsg[len] = NUL;
517 }
518 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
519 tail = regmatch.startp[i];
520 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
521 {
522 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
523 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000524 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
526 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
527 {
528 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000529 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000531 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
532 {
533 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
534 if (len > CMDBUFFSIZE - 5)
535 len = CMDBUFFSIZE - 5;
536 STRCPY(pattern, "^\\V");
537 STRNCAT(pattern, regmatch.startp[i], len);
538 pattern[len + 3] = '\\';
539 pattern[len + 4] = '$';
540 pattern[len + 5] = NUL;
541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 break;
543 }
544 }
545 multiscan = FALSE;
546 if (!fmt_ptr || idx == 'D' || idx == 'X')
547 {
548 if (fmt_ptr)
549 {
550 if (idx == 'D') /* enter directory */
551 {
552 if (*namebuf == NUL)
553 {
554 EMSG(_("E379: Missing or empty directory name"));
555 goto error2;
556 }
557 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
558 goto error2;
559 }
560 else if (idx == 'X') /* leave directory */
561 directory = qf_pop_dir(&dir_stack);
562 }
563 namebuf[0] = NUL; /* no match found, remove file name */
564 lnum = 0; /* don't jump to this line */
565 valid = FALSE;
566 STRCPY(errmsg, IObuff); /* copy whole line to error message */
567 if (!fmt_ptr)
568 multiline = multiignore = FALSE;
569 }
570 else if (fmt_ptr)
571 {
572 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
573 multiline = TRUE; /* start of a multi-line message */
574 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
575 { /* continuation of multi-line msg */
576 if (qfprev == NULL)
577 goto error2;
578 if (*errmsg && !multiignore)
579 {
580 len = (int)STRLEN(qfprev->qf_text);
581 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
582 == NULL)
583 goto error2;
584 STRCPY(ptr, qfprev->qf_text);
585 vim_free(qfprev->qf_text);
586 qfprev->qf_text = ptr;
587 *(ptr += len) = '\n';
588 STRCPY(++ptr, errmsg);
589 }
590 if (qfprev->qf_nr == -1)
591 qfprev->qf_nr = enr;
592 if (vim_isprintc(type) && !qfprev->qf_type)
593 qfprev->qf_type = type; /* only printable chars allowed */
594 if (!qfprev->qf_lnum)
595 qfprev->qf_lnum = lnum;
596 if (!qfprev->qf_col)
597 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000598 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!qfprev->qf_fnum)
600 qfprev->qf_fnum = qf_get_fnum(directory,
601 *namebuf || directory ? namebuf
602 : currfile && valid ? currfile : 0);
603 if (idx == 'Z')
604 multiline = multiignore = FALSE;
605 line_breakcheck();
606 continue;
607 }
608 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
609 {
610 /* global file names */
611 valid = FALSE;
612 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
613 {
614 if (*namebuf && idx == 'P')
615 currfile = qf_push_dir(namebuf, &file_stack);
616 else if (idx == 'Q')
617 currfile = qf_pop_dir(&file_stack);
618 *namebuf = NUL;
619 if (tail && *tail)
620 {
621 STRCPY(IObuff, skipwhite(tail));
622 multiscan = TRUE;
623 goto restofline;
624 }
625 }
626 }
627 if (fmt_ptr->flags == '-') /* generally exclude this line */
628 {
629 if (multiline)
630 multiignore = TRUE; /* also exclude continuation lines */
631 continue;
632 }
633 }
634
635 if (qf_add_entry(&qfprev,
636 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000637 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000639 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 errmsg,
641 lnum,
642 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000643 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000644 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 enr,
646 type,
647 valid) == FAIL)
648 goto error2;
649 line_breakcheck();
650 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000651 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653 if (qf_lists[qf_curlist].qf_index == 0) /* no valid entry found */
654 {
655 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
656 qf_lists[qf_curlist].qf_index = 1;
657 qf_lists[qf_curlist].qf_nonevalid = TRUE;
658 }
659 else
660 {
661 qf_lists[qf_curlist].qf_nonevalid = FALSE;
662 if (qf_lists[qf_curlist].qf_ptr == NULL)
663 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
664 }
665 retval = qf_lists[qf_curlist].qf_count; /* return number of matches */
666 goto qf_init_ok;
667 }
668 EMSG(_(e_readerrf));
669error2:
670 qf_free(qf_curlist);
671 qf_listcount--;
672 if (qf_curlist > 0)
673 --qf_curlist;
674qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000675 if (fd != NULL)
676 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
678 {
679 fmt_first = fmt_ptr->next;
680 vim_free(fmt_ptr->prog);
681 vim_free(fmt_ptr);
682 }
683 qf_clean_dir_stack(&dir_stack);
684 qf_clean_dir_stack(&file_stack);
685qf_init_end:
686 vim_free(namebuf);
687 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000688 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 vim_free(fmtstr);
690
691#ifdef FEAT_WINDOWS
692 qf_update_buffer();
693#endif
694
695 return retval;
696}
697
698/*
699 * Prepare for adding a new quickfix list.
700 */
701 static void
702qf_new_list()
703{
704 int i;
705
706 /*
707 * If the current entry is not the last entry, delete entries below
708 * the current entry. This makes it possible to browse in a tree-like
709 * way with ":grep'.
710 */
711 while (qf_listcount > qf_curlist + 1)
712 qf_free(--qf_listcount);
713
714 /*
715 * When the stack is full, remove to oldest entry
716 * Otherwise, add a new entry.
717 */
718 if (qf_listcount == LISTCOUNT)
719 {
720 qf_free(0);
721 for (i = 1; i < LISTCOUNT; ++i)
722 qf_lists[i - 1] = qf_lists[i];
723 qf_curlist = LISTCOUNT - 1;
724 }
725 else
726 qf_curlist = qf_listcount++;
727 qf_lists[qf_curlist].qf_index = 0;
728 qf_lists[qf_curlist].qf_count = 0;
729}
730
731/*
732 * Add an entry to the end of the list of errors.
733 * Returns OK or FAIL.
734 */
735 static int
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000736qf_add_entry(prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
737 valid)
738 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 char_u *dir; /* optional directory name */
740 char_u *fname; /* file name or NULL */
741 char_u *mesg; /* message */
742 long lnum; /* line number */
743 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000744 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000745 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 int nr; /* error number */
747 int type; /* type character */
748 int valid; /* valid entry */
749{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000750 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000752 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 return FAIL;
754 qfp->qf_fnum = qf_get_fnum(dir, fname);
755 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
756 {
757 vim_free(qfp);
758 return FAIL;
759 }
760 qfp->qf_lnum = lnum;
761 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000762 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000763 if (pattern == NULL || *pattern == NUL)
764 qfp->qf_pattern = NULL;
765 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
766 {
767 vim_free(qfp->qf_text);
768 vim_free(qfp);
769 return FAIL;
770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 qfp->qf_nr = nr;
772 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
773 type = 0;
774 qfp->qf_type = type;
775 qfp->qf_valid = valid;
776
777 if (qf_lists[qf_curlist].qf_count == 0) /* first element in the list */
778 {
779 qf_lists[qf_curlist].qf_start = qfp;
780 qfp->qf_prev = qfp; /* first element points to itself */
781 }
782 else
783 {
784 qfp->qf_prev = *prevp;
785 (*prevp)->qf_next = qfp;
786 }
787 qfp->qf_next = qfp; /* last element points to itself */
788 qfp->qf_cleared = FALSE;
789 *prevp = qfp;
790 ++qf_lists[qf_curlist].qf_count;
791 if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid)
792 /* first valid entry */
793 {
794 qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count;
795 qf_lists[qf_curlist].qf_ptr = qfp;
796 }
797
798 return OK;
799}
800
801/*
802 * get buffer number for file "dir.name"
803 */
804 static int
805qf_get_fnum(directory, fname)
806 char_u *directory;
807 char_u *fname;
808{
809 if (fname == NULL || *fname == NUL) /* no file name */
810 return 0;
811 {
812#ifdef RISCOS
813 /* Name is reported as `main.c', but file is `c.main' */
814 return ro_buflist_add(fname);
815#else
816 char_u *ptr;
817 int fnum;
818
819# ifdef VMS
820 vms_remove_version(fname);
821# endif
822# ifdef BACKSLASH_IN_FILENAME
823 if (directory != NULL)
824 slash_adjust(directory);
825 slash_adjust(fname);
826# endif
827 if (directory != NULL && !vim_isAbsName(fname)
828 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
829 {
830 /*
831 * Here we check if the file really exists.
832 * This should normally be true, but if make works without
833 * "leaving directory"-messages we might have missed a
834 * directory change.
835 */
836 if (mch_getperm(ptr) < 0)
837 {
838 vim_free(ptr);
839 directory = qf_guess_filepath(fname);
840 if (directory)
841 ptr = concat_fnames(directory, fname, TRUE);
842 else
843 ptr = vim_strsave(fname);
844 }
845 /* Use concatenated directory name and file name */
846 fnum = buflist_add(ptr, 0);
847 vim_free(ptr);
848 return fnum;
849 }
850 return buflist_add(fname, 0);
851#endif
852 }
853}
854
855/*
856 * push dirbuf onto the directory stack and return pointer to actual dir or
857 * NULL on error
858 */
859 static char_u *
860qf_push_dir(dirbuf, stackptr)
861 char_u *dirbuf;
862 struct dir_stack_T **stackptr;
863{
864 struct dir_stack_T *ds_new;
865 struct dir_stack_T *ds_ptr;
866
867 /* allocate new stack element and hook it in */
868 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
869 if (ds_new == NULL)
870 return NULL;
871
872 ds_new->next = *stackptr;
873 *stackptr = ds_new;
874
875 /* store directory on the stack */
876 if (vim_isAbsName(dirbuf)
877 || (*stackptr)->next == NULL
878 || (*stackptr && dir_stack != *stackptr))
879 (*stackptr)->dirname = vim_strsave(dirbuf);
880 else
881 {
882 /* Okay we don't have an absolute path.
883 * dirbuf must be a subdir of one of the directories on the stack.
884 * Let's search...
885 */
886 ds_new = (*stackptr)->next;
887 (*stackptr)->dirname = NULL;
888 while (ds_new)
889 {
890 vim_free((*stackptr)->dirname);
891 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
892 TRUE);
893 if (mch_isdir((*stackptr)->dirname) == TRUE)
894 break;
895
896 ds_new = ds_new->next;
897 }
898
899 /* clean up all dirs we already left */
900 while ((*stackptr)->next != ds_new)
901 {
902 ds_ptr = (*stackptr)->next;
903 (*stackptr)->next = (*stackptr)->next->next;
904 vim_free(ds_ptr->dirname);
905 vim_free(ds_ptr);
906 }
907
908 /* Nothing found -> it must be on top level */
909 if (ds_new == NULL)
910 {
911 vim_free((*stackptr)->dirname);
912 (*stackptr)->dirname = vim_strsave(dirbuf);
913 }
914 }
915
916 if ((*stackptr)->dirname != NULL)
917 return (*stackptr)->dirname;
918 else
919 {
920 ds_ptr = *stackptr;
921 *stackptr = (*stackptr)->next;
922 vim_free(ds_ptr);
923 return NULL;
924 }
925}
926
927
928/*
929 * pop dirbuf from the directory stack and return previous directory or NULL if
930 * stack is empty
931 */
932 static char_u *
933qf_pop_dir(stackptr)
934 struct dir_stack_T **stackptr;
935{
936 struct dir_stack_T *ds_ptr;
937
938 /* TODO: Should we check if dirbuf is the directory on top of the stack?
939 * What to do if it isn't? */
940
941 /* pop top element and free it */
942 if (*stackptr != NULL)
943 {
944 ds_ptr = *stackptr;
945 *stackptr = (*stackptr)->next;
946 vim_free(ds_ptr->dirname);
947 vim_free(ds_ptr);
948 }
949
950 /* return NEW top element as current dir or NULL if stack is empty*/
951 return *stackptr ? (*stackptr)->dirname : NULL;
952}
953
954/*
955 * clean up directory stack
956 */
957 static void
958qf_clean_dir_stack(stackptr)
959 struct dir_stack_T **stackptr;
960{
961 struct dir_stack_T *ds_ptr;
962
963 while ((ds_ptr = *stackptr) != NULL)
964 {
965 *stackptr = (*stackptr)->next;
966 vim_free(ds_ptr->dirname);
967 vim_free(ds_ptr);
968 }
969}
970
971/*
972 * Check in which directory of the directory stack the given file can be
973 * found.
974 * Returns a pointer to the directory name or NULL if not found
975 * Cleans up intermediate directory entries.
976 *
977 * TODO: How to solve the following problem?
978 * If we have the this directory tree:
979 * ./
980 * ./aa
981 * ./aa/bb
982 * ./bb
983 * ./bb/x.c
984 * and make says:
985 * making all in aa
986 * making all in bb
987 * x.c:9: Error
988 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
989 * qf_guess_filepath will return NULL.
990 */
991 static char_u *
992qf_guess_filepath(filename)
993 char_u *filename;
994{
995 struct dir_stack_T *ds_ptr;
996 struct dir_stack_T *ds_tmp;
997 char_u *fullname;
998
999 /* no dirs on the stack - there's nothing we can do */
1000 if (dir_stack == NULL)
1001 return NULL;
1002
1003 ds_ptr = dir_stack->next;
1004 fullname = NULL;
1005 while (ds_ptr)
1006 {
1007 vim_free(fullname);
1008 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1009
1010 /* If concat_fnames failed, just go on. The worst thing that can happen
1011 * is that we delete the entire stack.
1012 */
1013 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1014 break;
1015
1016 ds_ptr = ds_ptr->next;
1017 }
1018
1019 vim_free(fullname);
1020
1021 /* clean up all dirs we already left */
1022 while (dir_stack->next != ds_ptr)
1023 {
1024 ds_tmp = dir_stack->next;
1025 dir_stack->next = dir_stack->next->next;
1026 vim_free(ds_tmp->dirname);
1027 vim_free(ds_tmp);
1028 }
1029
1030 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1031
1032}
1033
1034/*
1035 * jump to a quickfix line
1036 * if dir == FORWARD go "errornr" valid entries forward
1037 * if dir == BACKWARD go "errornr" valid entries backward
1038 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1039 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1040 * else if "errornr" is zero, redisplay the same line
1041 * else go to entry "errornr"
1042 */
1043 void
1044qf_jump(dir, errornr, forceit)
1045 int dir;
1046 int errornr;
1047 int forceit;
1048{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001049 qfline_T *qf_ptr;
1050 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 int qf_index;
1052 int old_qf_fnum;
1053 int old_qf_index;
1054 int prev_index;
1055 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1056 char_u *err = e_no_more_items;
1057 linenr_T i;
1058 buf_T *old_curbuf;
1059 linenr_T old_lnum;
1060 char_u *old_swb = p_swb;
1061 colnr_T screen_col;
1062 colnr_T char_col;
1063 char_u *line;
1064#ifdef FEAT_WINDOWS
1065 int opened_window = FALSE;
1066 win_T *win;
1067 win_T *altwin;
1068#endif
1069 int print_message = TRUE;
1070 int len;
1071#ifdef FEAT_FOLDING
1072 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1073#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001074 int ok = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1077 {
1078 EMSG(_(e_quickfix));
1079 return;
1080 }
1081
1082 qf_ptr = qf_lists[qf_curlist].qf_ptr;
1083 old_qf_ptr = qf_ptr;
1084 qf_index = qf_lists[qf_curlist].qf_index;
1085 old_qf_index = qf_index;
1086 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1087 {
1088 while (errornr--)
1089 {
1090 old_qf_ptr = qf_ptr;
1091 prev_index = qf_index;
1092 old_qf_fnum = qf_ptr->qf_fnum;
1093 do
1094 {
1095 if (qf_index == qf_lists[qf_curlist].qf_count
1096 || qf_ptr->qf_next == NULL)
1097 {
1098 qf_ptr = old_qf_ptr;
1099 qf_index = prev_index;
1100 if (err != NULL)
1101 {
1102 EMSG(_(err));
1103 goto theend;
1104 }
1105 errornr = 0;
1106 break;
1107 }
1108 ++qf_index;
1109 qf_ptr = qf_ptr->qf_next;
1110 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1111 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1112 err = NULL;
1113 }
1114 }
1115 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1116 {
1117 while (errornr--)
1118 {
1119 old_qf_ptr = qf_ptr;
1120 prev_index = qf_index;
1121 old_qf_fnum = qf_ptr->qf_fnum;
1122 do
1123 {
1124 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1125 {
1126 qf_ptr = old_qf_ptr;
1127 qf_index = prev_index;
1128 if (err != NULL)
1129 {
1130 EMSG(_(err));
1131 goto theend;
1132 }
1133 errornr = 0;
1134 break;
1135 }
1136 --qf_index;
1137 qf_ptr = qf_ptr->qf_prev;
1138 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1139 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1140 err = NULL;
1141 }
1142 }
1143 else if (errornr != 0) /* go to specified number */
1144 {
1145 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1146 {
1147 --qf_index;
1148 qf_ptr = qf_ptr->qf_prev;
1149 }
1150 while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count
1151 && qf_ptr->qf_next != NULL)
1152 {
1153 ++qf_index;
1154 qf_ptr = qf_ptr->qf_next;
1155 }
1156 }
1157
1158#ifdef FEAT_WINDOWS
1159 qf_lists[qf_curlist].qf_index = qf_index;
1160 if (qf_win_pos_update(old_qf_index))
1161 /* No need to print the error message if it's visible in the error
1162 * window */
1163 print_message = FALSE;
1164
1165 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001166 * For ":helpgrep" find a help window or open one.
1167 */
1168 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1169 {
1170 win_T *wp;
1171 int n;
1172
1173 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1174 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1175 break;
1176 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1177 win_enter(wp, TRUE);
1178 else
1179 {
1180 /*
1181 * Split off help window; put it at far top if no position
1182 * specified, the current window is vertically split and narrow.
1183 */
1184 n = WSP_HELP;
1185# ifdef FEAT_VERTSPLIT
1186 if (cmdmod.split == 0 && curwin->w_width != Columns
1187 && curwin->w_width < 80)
1188 n |= WSP_TOP;
1189# endif
1190 if (win_split(0, n) == FAIL)
1191 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001192 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001193
1194 if (curwin->w_height < p_hh)
1195 win_setheight((int)p_hh);
1196 }
1197
1198 if (!p_im)
1199 restart_edit = 0; /* don't want insert mode in help file */
1200 }
1201
1202 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 * If currently in the quickfix window, find another window to show the
1204 * file in.
1205 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001206 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
1208 /*
1209 * If there is no file specified, we don't know where to go.
1210 * But do advance, otherwise ":cn" gets stuck.
1211 */
1212 if (qf_ptr->qf_fnum == 0)
1213 goto theend;
1214
1215 /*
1216 * If there is only one window, create a new one above the quickfix
1217 * window.
1218 */
1219 if (firstwin == lastwin)
1220 {
1221 if (win_split(0, WSP_ABOVE) == FAIL)
1222 goto failed; /* not enough room for window */
1223 opened_window = TRUE; /* close it when fail */
1224 p_swb = empty_option; /* don't split again */
1225# ifdef FEAT_SCROLLBIND
1226 curwin->w_p_scb = FALSE;
1227# endif
1228 }
1229 else
1230 {
1231 /*
1232 * Try to find a window that shows the right buffer.
1233 * Default to the window just above the quickfix buffer.
1234 */
1235 win = curwin;
1236 altwin = NULL;
1237 for (;;)
1238 {
1239 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1240 break;
1241 if (win->w_prev == NULL)
1242 win = lastwin; /* wrap around the top */
1243 else
1244 win = win->w_prev; /* go to previous window */
1245
1246 if (bt_quickfix(win->w_buffer))
1247 {
1248 /* Didn't find it, go to the window before the quickfix
1249 * window. */
1250 if (altwin != NULL)
1251 win = altwin;
1252 else if (curwin->w_prev != NULL)
1253 win = curwin->w_prev;
1254 else
1255 win = curwin->w_next;
1256 break;
1257 }
1258
1259 /* Remember a usable window. */
1260 if (altwin == NULL && !win->w_p_pvw
1261 && win->w_buffer->b_p_bt[0] == NUL)
1262 altwin = win;
1263 }
1264
1265 win_goto(win);
1266 }
1267 }
1268#endif
1269
1270 /*
1271 * If there is a file name,
1272 * read the wanted file if needed, and check autowrite etc.
1273 */
1274 old_curbuf = curbuf;
1275 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001276
1277 if (qf_ptr->qf_fnum != 0)
1278 {
1279 if (qf_ptr->qf_type == 1)
1280 {
1281 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1282 * set b_p_ro flag). */
1283 if (!can_abandon(curbuf, forceit))
1284 {
1285 EMSG(_(e_nowrtmsg));
1286 ok = FALSE;
1287 }
1288 else
1289 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1290 ECMD_HIDE + ECMD_SET_HELP);
1291 }
1292 else
1293 ok = buflist_getfile(qf_ptr->qf_fnum,
1294 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1295 }
1296
1297 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
1299 /* When not switched to another buffer, still need to set pc mark */
1300 if (curbuf == old_curbuf)
1301 setpcmark();
1302
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001303 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001305 /*
1306 * Go to line with error, unless qf_lnum is 0.
1307 */
1308 i = qf_ptr->qf_lnum;
1309 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001311 if (i > curbuf->b_ml.ml_line_count)
1312 i = curbuf->b_ml.ml_line_count;
1313 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001315 if (qf_ptr->qf_col > 0)
1316 {
1317 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1318 if (qf_ptr->qf_viscol == TRUE)
1319 {
1320 /*
1321 * Check each character from the beginning of the error
1322 * line up to the error column. For each tab character
1323 * found, reduce the error column value by the length of
1324 * a tab character.
1325 */
1326 line = ml_get_curline();
1327 screen_col = 0;
1328 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1329 {
1330 if (*line == NUL)
1331 break;
1332 if (*line++ == '\t')
1333 {
1334 curwin->w_cursor.col -= 7 - (screen_col % 8);
1335 screen_col += 8 - (screen_col % 8);
1336 }
1337 else
1338 ++screen_col;
1339 }
1340 }
1341 check_cursor();
1342 }
1343 else
1344 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001347 {
1348 pos_T save_cursor;
1349
1350 /* Move the cursor to the first line in the buffer */
1351 save_cursor = curwin->w_cursor;
1352 curwin->w_cursor.lnum = 0;
1353 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1354 curwin->w_cursor = save_cursor;
1355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
1357#ifdef FEAT_FOLDING
1358 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1359 foldOpenCursor();
1360#endif
1361 if (print_message)
1362 {
1363 /* Update the screen before showing the message */
1364 update_topline_redraw();
1365 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1366 qf_lists[qf_curlist].qf_count,
1367 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1368 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1369 /* Add the message, skipping leading whitespace and newlines. */
1370 len = (int)STRLEN(IObuff);
1371 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1372
1373 /* Output the message. Overwrite to avoid scrolling when the 'O'
1374 * flag is present in 'shortmess'; But when not jumping, print the
1375 * whole message. */
1376 i = msg_scroll;
1377 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1378 msg_scroll = TRUE;
1379 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1380 msg_scroll = FALSE;
1381 msg_attr_keep(IObuff, 0, TRUE);
1382 msg_scroll = i;
1383 }
1384 }
1385 else
1386 {
1387#ifdef FEAT_WINDOWS
1388 if (opened_window)
1389 win_close(curwin, TRUE); /* Close opened window */
1390#endif
1391 if (qf_ptr->qf_fnum != 0)
1392 {
1393 /*
1394 * Couldn't open file, so put index back where it was. This could
1395 * happen if the file was readonly and we changed something.
1396 */
1397#ifdef FEAT_WINDOWS
1398failed:
1399#endif
1400 qf_ptr = old_qf_ptr;
1401 qf_index = old_qf_index;
1402 }
1403 }
1404theend:
1405 qf_lists[qf_curlist].qf_ptr = qf_ptr;
1406 qf_lists[qf_curlist].qf_index = qf_index;
1407#ifdef FEAT_WINDOWS
1408 if (p_swb != old_swb && opened_window)
1409 {
1410 /* Restore old 'switchbuf' value, but not when an autocommand or
1411 * modeline has changed the value. */
1412 if (p_swb == empty_option)
1413 p_swb = old_swb;
1414 else
1415 free_string_option(old_swb);
1416 }
1417#endif
1418}
1419
1420/*
1421 * ":clist": list all errors
1422 */
1423 void
1424qf_list(eap)
1425 exarg_T *eap;
1426{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001427 buf_T *buf;
1428 char_u *fname;
1429 qfline_T *qfp;
1430 int i;
1431 int idx1 = 1;
1432 int idx2 = -1;
1433 int need_return = TRUE;
1434 int last_printed = 1;
1435 char_u *arg = eap->arg;
1436 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 recognised errors */
1438
1439 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1440 {
1441 EMSG(_(e_quickfix));
1442 return;
1443 }
1444 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1445 {
1446 EMSG(_(e_trailing));
1447 return;
1448 }
1449 i = qf_lists[qf_curlist].qf_count;
1450 if (idx1 < 0)
1451 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1452 if (idx2 < 0)
1453 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1454
1455 more_back_used = TRUE;
1456 if (qf_lists[qf_curlist].qf_nonevalid)
1457 all = TRUE;
1458 qfp = qf_lists[qf_curlist].qf_start;
1459 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
1460 {
1461 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1462 {
1463 if (need_return)
1464 {
1465 msg_putchar('\n');
1466 need_return = FALSE;
1467 }
1468 if (more_back == 0)
1469 {
1470 fname = NULL;
1471 if (qfp->qf_fnum != 0
1472 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1473 {
1474 fname = buf->b_fname;
1475 if (qfp->qf_type == 1) /* :helpgrep */
1476 fname = gettail(fname);
1477 }
1478 if (fname == NULL)
1479 sprintf((char *)IObuff, "%2d", i);
1480 else
1481 sprintf((char *)IObuff, "%2d %s", i, (char *)fname);
1482 msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1483 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1484 if (qfp->qf_lnum == 0)
1485 IObuff[0] = NUL;
1486 else if (qfp->qf_col == 0)
1487 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1488 else
1489 sprintf((char *)IObuff, ":%ld col %d",
1490 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001491 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1493 msg_puts_attr(IObuff, hl_attr(HLF_N));
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001494 if (qfp->qf_pattern != NULL)
1495 {
1496 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1497 STRCAT(IObuff, ":");
1498 msg_puts(IObuff);
1499 }
1500 msg_puts((char_u *)" ");
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 /* Remove newlines and leading whitespace from the text.
1503 * For an unrecognized line keep the indent, the compiler may
1504 * mark a word with ^^^^. */
1505 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1506 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1507 IObuff, IOSIZE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001508 msg_prt_line(IObuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 out_flush(); /* show one line at a time */
1510 need_return = TRUE;
1511 last_printed = i;
1512 }
1513 }
1514 if (more_back)
1515 {
1516 /* scrolling backwards from the more-prompt */
1517 /* TODO: compute the number of items from the screen lines */
1518 more_back = more_back * 2 - 1;
1519 while (i > last_printed - more_back && i > idx1)
1520 {
1521 do
1522 {
1523 qfp = qfp->qf_prev;
1524 --i;
1525 }
1526 while (i > idx1 && !qfp->qf_valid && !all);
1527 }
1528 more_back = 0;
1529 }
1530 else
1531 {
1532 qfp = qfp->qf_next;
1533 ++i;
1534 }
1535 ui_breakcheck();
1536 }
1537 more_back_used = FALSE;
1538}
1539
1540/*
1541 * Remove newlines and leading whitespace from an error message.
1542 * Put the result in "buf[bufsize]".
1543 */
1544 static void
1545qf_fmt_text(text, buf, bufsize)
1546 char_u *text;
1547 char_u *buf;
1548 int bufsize;
1549{
1550 int i;
1551 char_u *p = text;
1552
1553 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1554 {
1555 if (*p == '\n')
1556 {
1557 buf[i] = ' ';
1558 while (*++p != NUL)
1559 if (!vim_iswhite(*p) && *p != '\n')
1560 break;
1561 }
1562 else
1563 buf[i] = *p++;
1564 }
1565 buf[i] = NUL;
1566}
1567
1568/*
1569 * ":colder [count]": Up in the quickfix stack.
1570 * ":cnewer [count]": Down in the quickfix stack.
1571 */
1572 void
1573qf_age(eap)
1574 exarg_T *eap;
1575{
1576 int count;
1577
1578 if (eap->addr_count != 0)
1579 count = eap->line2;
1580 else
1581 count = 1;
1582 while (count--)
1583 {
1584 if (eap->cmdidx == CMD_colder)
1585 {
1586 if (qf_curlist == 0)
1587 {
1588 EMSG(_("E380: At bottom of quickfix stack"));
1589 return;
1590 }
1591 --qf_curlist;
1592 }
1593 else
1594 {
1595 if (qf_curlist >= qf_listcount - 1)
1596 {
1597 EMSG(_("E381: At top of quickfix stack"));
1598 return;
1599 }
1600 ++qf_curlist;
1601 }
1602 }
1603 qf_msg();
1604}
1605
1606 static void
1607qf_msg()
1608{
1609 smsg((char_u *)_("error list %d of %d; %d errors"),
1610 qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1611#ifdef FEAT_WINDOWS
1612 qf_update_buffer();
1613#endif
1614}
1615
1616/*
1617 * free the error list
1618 */
1619 static void
1620qf_free(idx)
1621 int idx;
1622{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001623 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
1625 while (qf_lists[idx].qf_count)
1626 {
1627 qfp = qf_lists[idx].qf_start->qf_next;
1628 vim_free(qf_lists[idx].qf_start->qf_text);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001629 vim_free(qf_lists[idx].qf_start->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 vim_free(qf_lists[idx].qf_start);
1631 qf_lists[idx].qf_start = qfp;
1632 --qf_lists[idx].qf_count;
1633 }
1634}
1635
1636/*
1637 * qf_mark_adjust: adjust marks
1638 */
1639 void
1640qf_mark_adjust(line1, line2, amount, amount_after)
1641 linenr_T line1;
1642 linenr_T line2;
1643 long amount;
1644 long amount_after;
1645{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001646 int i;
1647 qfline_T *qfp;
1648 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650 for (idx = 0; idx < qf_listcount; ++idx)
1651 if (qf_lists[idx].qf_count)
1652 for (i = 0, qfp = qf_lists[idx].qf_start;
1653 i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1654 if (qfp->qf_fnum == curbuf->b_fnum)
1655 {
1656 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1657 {
1658 if (amount == MAXLNUM)
1659 qfp->qf_cleared = TRUE;
1660 else
1661 qfp->qf_lnum += amount;
1662 }
1663 else if (amount_after && qfp->qf_lnum > line2)
1664 qfp->qf_lnum += amount_after;
1665 }
1666}
1667
1668/*
1669 * Make a nice message out of the error character and the error number:
1670 * char number message
1671 * e or E 0 " error"
1672 * w or W 0 " warning"
1673 * i or I 0 " info"
1674 * 0 0 ""
1675 * other 0 " c"
1676 * e or E n " error n"
1677 * w or W n " warning n"
1678 * i or I n " info n"
1679 * 0 n " error n"
1680 * other n " c n"
1681 * 1 x "" :helpgrep
1682 */
1683 static char_u *
1684qf_types(c, nr)
1685 int c, nr;
1686{
1687 static char_u buf[20];
1688 static char_u cc[3];
1689 char_u *p;
1690
1691 if (c == 'W' || c == 'w')
1692 p = (char_u *)" warning";
1693 else if (c == 'I' || c == 'i')
1694 p = (char_u *)" info";
1695 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1696 p = (char_u *)" error";
1697 else if (c == 0 || c == 1)
1698 p = (char_u *)"";
1699 else
1700 {
1701 cc[0] = ' ';
1702 cc[1] = c;
1703 cc[2] = NUL;
1704 p = cc;
1705 }
1706
1707 if (nr <= 0)
1708 return p;
1709
1710 sprintf((char *)buf, "%s %3d", (char *)p, nr);
1711 return buf;
1712}
1713
1714#if defined(FEAT_WINDOWS) || defined(PROTO)
1715/*
1716 * ":cwindow": open the quickfix window if we have errors to display,
1717 * close it if not.
1718 */
1719 void
1720ex_cwindow(eap)
1721 exarg_T *eap;
1722{
1723 win_T *win;
1724
1725 /*
1726 * Look for an existing quickfix window.
1727 */
1728 for (win = firstwin; win != NULL; win = win->w_next)
1729 if (bt_quickfix(win->w_buffer))
1730 break;
1731
1732 /*
1733 * If a quickfix window is open but we have no errors to display,
1734 * close the window. If a quickfix window is not open, then open
1735 * it if we have errors; otherwise, leave it closed.
1736 */
1737 if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1738 {
1739 if (win != NULL)
1740 ex_cclose(eap);
1741 }
1742 else if (win == NULL)
1743 ex_copen(eap);
1744}
1745
1746/*
1747 * ":cclose": close the window showing the list of errors.
1748 */
1749/*ARGSUSED*/
1750 void
1751ex_cclose(eap)
1752 exarg_T *eap;
1753{
1754 win_T *win;
1755
1756 /*
1757 * Find existing quickfix window and close it.
1758 */
1759 for (win = firstwin; win != NULL; win = win->w_next)
1760 if (bt_quickfix(win->w_buffer))
1761 break;
1762
1763 if (win != NULL)
1764 win_close(win, FALSE);
1765}
1766
1767/*
1768 * ":copen": open a window that shows the list of errors.
1769 */
1770 void
1771ex_copen(eap)
1772 exarg_T *eap;
1773{
1774 int height;
1775 buf_T *buf;
1776 win_T *win;
1777
1778 if (eap->addr_count != 0)
1779 height = eap->line2;
1780 else
1781 height = QF_WINHEIGHT;
1782
1783#ifdef FEAT_VISUAL
1784 reset_VIsual_and_resel(); /* stop Visual mode */
1785#endif
1786#ifdef FEAT_GUI
1787 need_mouse_correct = TRUE;
1788#endif
1789
1790 /*
1791 * Find existing quickfix window, or open a new one.
1792 */
1793 for (win = firstwin; win != NULL; win = win->w_next)
1794 if (bt_quickfix(win->w_buffer))
1795 break;
1796 if (win != NULL)
1797 win_goto(win);
1798 else
1799 {
1800 /* The current window becomes the previous window afterwards. */
1801 win = curwin;
1802
1803 /* Create the new window at the very bottom. */
1804 win_goto(lastwin);
1805 if (win_split(height, WSP_BELOW) == FAIL)
1806 return; /* not enough room for window */
1807#ifdef FEAT_SCROLLBIND
1808 curwin->w_p_scb = FALSE;
1809#endif
1810
1811 /*
1812 * Find existing quickfix buffer, or create a new one.
1813 */
1814 buf = qf_find_buf();
1815 if (buf == NULL)
1816 {
1817 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1818 /* switch off 'swapfile' */
1819 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1820 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1821 OPT_LOCAL);
1822 set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1823 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1824 }
1825 else if (buf != curbuf)
1826 set_curbuf(buf, DOBUF_GOTO);
1827
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001828#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 /* Only set the height when there is no window to the side. */
1830 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 win_setheight(height);
1833 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
1834 if (win_valid(win))
1835 prevwin = win;
1836 }
1837
1838 /*
1839 * Fill the buffer with the quickfix list.
1840 */
1841 qf_fill_buffer();
1842
1843 curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1844 curwin->w_cursor.col = 0;
1845 check_cursor();
1846 update_topline(); /* scroll to show the line */
1847}
1848
1849/*
1850 * Return the number of the current entry (line number in the quickfix
1851 * window).
1852 */
1853 linenr_T
1854qf_current_entry()
1855{
1856 return qf_lists[qf_curlist].qf_index;
1857}
1858
1859/*
1860 * Update the cursor position in the quickfix window to the current error.
1861 * Return TRUE if there is a quickfix window.
1862 */
1863 static int
1864qf_win_pos_update(old_qf_index)
1865 int old_qf_index; /* previous qf_index or zero */
1866{
1867 win_T *win;
1868 int qf_index = qf_lists[qf_curlist].qf_index;
1869
1870 /*
1871 * Put the cursor on the current error in the quickfix window, so that
1872 * it's viewable.
1873 */
1874 for (win = firstwin; win != NULL; win = win->w_next)
1875 if (bt_quickfix(win->w_buffer))
1876 break;
1877 if (win != NULL
1878 && qf_index <= win->w_buffer->b_ml.ml_line_count
1879 && old_qf_index != qf_index)
1880 {
1881 win_T *old_curwin = curwin;
1882
1883 curwin = win;
1884 curbuf = win->w_buffer;
1885 if (qf_index > old_qf_index)
1886 {
1887 curwin->w_redraw_top = old_qf_index;
1888 curwin->w_redraw_bot = qf_index;
1889 }
1890 else
1891 {
1892 curwin->w_redraw_top = qf_index;
1893 curwin->w_redraw_bot = old_qf_index;
1894 }
1895 curwin->w_cursor.lnum = qf_index;
1896 curwin->w_cursor.col = 0;
1897 update_topline(); /* scroll to show the line */
1898 redraw_later(VALID);
1899 curwin->w_redr_status = TRUE; /* update ruler */
1900 curwin = old_curwin;
1901 curbuf = curwin->w_buffer;
1902 }
1903 return win != NULL;
1904}
1905
1906/*
1907 * Find quickfix buffer.
1908 */
1909 static buf_T *
1910qf_find_buf()
1911{
1912 buf_T *buf;
1913
1914 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1915 if (bt_quickfix(buf))
1916 break;
1917 return buf;
1918}
1919
1920/*
1921 * Find the quickfix buffer. If it exists, update the contents.
1922 */
1923 static void
1924qf_update_buffer()
1925{
1926 buf_T *buf;
1927#ifdef FEAT_AUTOCMD
1928 aco_save_T aco;
1929#else
1930 buf_T *save_curbuf;
1931#endif
1932
1933 /* Check if a buffer for the quickfix list exists. Update it. */
1934 buf = qf_find_buf();
1935 if (buf != NULL)
1936 {
1937#ifdef FEAT_AUTOCMD
1938 /* set curwin/curbuf to buf and save a few things */
1939 aucmd_prepbuf(&aco, buf);
1940#else
1941 save_curbuf = curbuf;
1942 curbuf = buf;
1943#endif
1944
1945 qf_fill_buffer();
1946
1947#ifdef FEAT_AUTOCMD
1948 /* restore curwin/curbuf and a few other things */
1949 aucmd_restbuf(&aco);
1950#else
1951 curbuf = save_curbuf;
1952#endif
1953
1954 (void)qf_win_pos_update(0);
1955 }
1956}
1957
1958/*
1959 * Fill current buffer with quickfix errors, replacing any previous contents.
1960 * curbuf must be the quickfix buffer!
1961 */
1962 static void
1963qf_fill_buffer()
1964{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001965 linenr_T lnum;
1966 qfline_T *qfp;
1967 buf_T *errbuf;
1968 int len;
1969 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
1971 /* delete all existing lines */
1972 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
1973 (void)ml_delete((linenr_T)1, FALSE);
1974
1975 /* Check if there is anything to display */
1976 if (qf_curlist < qf_listcount)
1977 {
1978 /* Add one line for each error */
1979 qfp = qf_lists[qf_curlist].qf_start;
1980 for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
1981 {
1982 if (qfp->qf_fnum != 0
1983 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
1984 && errbuf->b_fname != NULL)
1985 {
1986 if (qfp->qf_type == 1) /* :helpgrep */
1987 STRCPY(IObuff, gettail(errbuf->b_fname));
1988 else
1989 STRCPY(IObuff, errbuf->b_fname);
1990 len = (int)STRLEN(IObuff);
1991 }
1992 else
1993 len = 0;
1994 IObuff[len++] = '|';
1995
1996 if (qfp->qf_lnum > 0)
1997 {
1998 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
1999 len += (int)STRLEN(IObuff + len);
2000
2001 if (qfp->qf_col > 0)
2002 {
2003 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2004 len += (int)STRLEN(IObuff + len);
2005 }
2006
2007 sprintf((char *)IObuff + len, "%s",
2008 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2009 len += (int)STRLEN(IObuff + len);
2010 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002011 else if (qfp->qf_pattern != NULL)
2012 {
2013 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2014 len += (int)STRLEN(IObuff + len);
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 IObuff[len++] = '|';
2017 IObuff[len++] = ' ';
2018
2019 /* Remove newlines and leading whitespace from the text.
2020 * For an unrecognized line keep the indent, the compiler may
2021 * mark a word with ^^^^. */
2022 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2023 IObuff + len, IOSIZE - len);
2024
2025 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2026 == FAIL)
2027 break;
2028 qfp = qfp->qf_next;
2029 }
2030 /* Delete the empty line which is now at the end */
2031 (void)ml_delete(lnum + 1, FALSE);
2032 }
2033
2034 /* correct cursor position */
2035 check_lnums(TRUE);
2036
2037 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2038 * resembles reading a file into a buffer, it's more logical when using
2039 * autocommands. */
2040 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2041 curbuf->b_p_ma = FALSE;
2042
2043#ifdef FEAT_AUTOCMD
2044 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2045 FALSE, curbuf);
2046 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2047 FALSE, curbuf);
2048#endif
2049
2050 /* make sure it will be redrawn */
2051 redraw_curbuf_later(NOT_VALID);
2052
2053 /* Restore KeyTyped, setting 'filetype' may reset it. */
2054 KeyTyped = old_KeyTyped;
2055}
2056
2057#endif /* FEAT_WINDOWS */
2058
2059/*
2060 * Return TRUE if "buf" is the quickfix buffer.
2061 */
2062 int
2063bt_quickfix(buf)
2064 buf_T *buf;
2065{
2066 return (buf->b_p_bt[0] == 'q');
2067}
2068
2069/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002070 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2071 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 */
2073 int
2074bt_nofile(buf)
2075 buf_T *buf;
2076{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002077 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2078 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079}
2080
2081/*
2082 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2083 */
2084 int
2085bt_dontwrite(buf)
2086 buf_T *buf;
2087{
2088 return (buf->b_p_bt[0] == 'n');
2089}
2090
2091 int
2092bt_dontwrite_msg(buf)
2093 buf_T *buf;
2094{
2095 if (bt_dontwrite(buf))
2096 {
2097 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2098 return TRUE;
2099 }
2100 return FALSE;
2101}
2102
2103/*
2104 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2105 * and 'bufhidden'.
2106 */
2107 int
2108buf_hide(buf)
2109 buf_T *buf;
2110{
2111 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2112 switch (buf->b_p_bh[0])
2113 {
2114 case 'u': /* "unload" */
2115 case 'w': /* "wipe" */
2116 case 'd': return FALSE; /* "delete" */
2117 case 'h': return TRUE; /* "hide" */
2118 }
2119 return (p_hid || cmdmod.hide);
2120}
2121
2122/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002123 * Return TRUE when using ":vimgrep" for ":grep".
2124 */
2125 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002126grep_internal(cmdidx)
2127 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002128{
Bram Moolenaar81695252004-12-29 20:58:21 +00002129 return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002130 && STRCMP("internal",
2131 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2132}
2133
2134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 * Used for ":make", ":grep" and ":grepadd".
2136 */
2137 void
2138ex_make(eap)
2139 exarg_T *eap;
2140{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002141 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 char_u *cmd;
2143 unsigned len;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002144#ifdef FEAT_AUTOCMD
2145 char_u *au_name = NULL;
2146
2147 switch (eap->cmdidx)
2148 {
2149 case CMD_make: au_name = (char_u *)"make"; break;
2150 case CMD_grep: au_name = (char_u *)"grep"; break;
2151 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2152 default: break;
2153 }
2154 if (au_name != NULL)
2155 {
2156 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2157 curbuf->b_fname, TRUE, curbuf);
2158 if (did_throw || force_abort)
2159 return;
2160 }
2161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
Bram Moolenaar86b68352004-12-27 21:59:20 +00002163 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002164 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002165 {
2166 ex_vimgrep(eap);
2167 return;
2168 }
2169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002171 fname = get_mef_name();
2172 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002174 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176 /*
2177 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2178 */
2179 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2180 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002181 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 cmd = alloc(len);
2183 if (cmd == NULL)
2184 return;
2185 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2186 (char *)p_shq);
2187 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002188 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 /*
2190 * Output a newline if there's something else than the :make command that
2191 * was typed (in which case the cursor is in column 0).
2192 */
2193 if (msg_col == 0)
2194 msg_didout = FALSE;
2195 msg_start();
2196 MSG_PUTS(":!");
2197 msg_outtrans(cmd); /* show what we are doing */
2198
2199 /* let the shell know if we are redirecting output or not */
2200 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2201
2202#ifdef AMIGA
2203 out_flush();
2204 /* read window status report and redraw before message */
2205 (void)char_avail();
2206#endif
2207
Bram Moolenaar7c626922005-02-07 22:01:03 +00002208 if (qf_init(fname, eap->cmdidx != CMD_make ? p_gefm : p_efm,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002209 eap->cmdidx != CMD_grepadd) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 && !eap->forceit)
2211 qf_jump(0, 0, FALSE); /* display first error */
2212
Bram Moolenaar7c626922005-02-07 22:01:03 +00002213 mch_remove(fname);
2214 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002216
2217#ifdef FEAT_AUTOCMD
2218 if (au_name != NULL)
2219 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2220 curbuf->b_fname, TRUE, curbuf);
2221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222}
2223
2224/*
2225 * Return the name for the errorfile, in allocated memory.
2226 * Find a new unique name when 'makeef' contains "##".
2227 * Returns NULL for error.
2228 */
2229 static char_u *
2230get_mef_name()
2231{
2232 char_u *p;
2233 char_u *name;
2234 static int start = -1;
2235 static int off = 0;
2236#ifdef HAVE_LSTAT
2237 struct stat sb;
2238#endif
2239
2240 if (*p_mef == NUL)
2241 {
2242 name = vim_tempname('e');
2243 if (name == NULL)
2244 EMSG(_(e_notmp));
2245 return name;
2246 }
2247
2248 for (p = p_mef; *p; ++p)
2249 if (p[0] == '#' && p[1] == '#')
2250 break;
2251
2252 if (*p == NUL)
2253 return vim_strsave(p_mef);
2254
2255 /* Keep trying until the name doesn't exist yet. */
2256 for (;;)
2257 {
2258 if (start == -1)
2259 start = mch_get_pid();
2260 else
2261 off += 19;
2262
2263 name = alloc((unsigned)STRLEN(p_mef) + 30);
2264 if (name == NULL)
2265 break;
2266 STRCPY(name, p_mef);
2267 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2268 STRCAT(name, p + 2);
2269 if (mch_getperm(name) < 0
2270#ifdef HAVE_LSTAT
2271 /* Don't accept a symbolic link, its a security risk. */
2272 && mch_lstat((char *)name, &sb) < 0
2273#endif
2274 )
2275 break;
2276 vim_free(name);
2277 }
2278 return name;
2279}
2280
2281/*
2282 * ":cc", ":crewind", ":cfirst" and ":clast".
2283 */
2284 void
2285ex_cc(eap)
2286 exarg_T *eap;
2287{
2288 qf_jump(0,
2289 eap->addr_count > 0
2290 ? (int)eap->line2
2291 : eap->cmdidx == CMD_cc
2292 ? 0
2293 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2294 ? 1
2295 : 32767,
2296 eap->forceit);
2297}
2298
2299/*
2300 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2301 */
2302 void
2303ex_cnext(eap)
2304 exarg_T *eap;
2305{
2306 qf_jump(eap->cmdidx == CMD_cnext
2307 ? FORWARD
2308 : eap->cmdidx == CMD_cnfile
2309 ? FORWARD_FILE
2310 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2311 ? BACKWARD_FILE
2312 : BACKWARD,
2313 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2314}
2315
2316/*
2317 * ":cfile" command.
2318 */
2319 void
2320ex_cfile(eap)
2321 exarg_T *eap;
2322{
2323 if (*eap->arg != NUL)
2324 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
2325 if (qf_init(p_ef, p_efm, TRUE) > 0 && eap->cmdidx == CMD_cfile)
2326 qf_jump(0, 0, eap->forceit); /* display first error */
2327}
2328
2329/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002330 * ":vimgrep {pattern} file(s)"
2331 */
2332 void
2333ex_vimgrep(eap)
2334 exarg_T *eap;
2335{
Bram Moolenaar81695252004-12-29 20:58:21 +00002336 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002337 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002338 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002339 char_u *s;
2340 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002341 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002342 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002343 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002344 buf_T *buf;
2345 int duplicate_name = FALSE;
2346 int using_dummy;
2347 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002348 buf_T *first_match_buf = NULL;
2349 time_t seconds = 0;
2350#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2351 char_u *save_ei = NULL;
2352 aco_save_T aco;
2353#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002354#ifdef FEAT_AUTOCMD
2355 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002356 int flags = 0;
2357 colnr_T col;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002358
2359 switch (eap->cmdidx)
2360 {
2361 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
2362 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
2363 default: break;
2364 }
2365 if (au_name != NULL)
2366 {
2367 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2368 curbuf->b_fname, TRUE, curbuf);
2369 if (did_throw || force_abort)
2370 return;
2371 }
2372#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002373
Bram Moolenaar81695252004-12-29 20:58:21 +00002374 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002375 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002376 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002377 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002378 {
Bram Moolenaar748bf032005-02-02 23:04:36 +00002379 EMSG(_("E682: Invalid search pattern or delimiter"));
2380 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002381 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002382 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002383 if (regmatch.regprog == NULL)
2384 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002385 regmatch.rmm_ic = p_ic;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002386
2387 p = skipwhite(p);
2388 if (*p == NUL)
2389 {
2390 EMSG(_("E683: File name missing or invalid pattern"));
2391 goto theend;
2392 }
2393
2394 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
2395 || qf_curlist == qf_listcount)
2396 /* make place for a new list */
2397 qf_new_list();
2398 else if (qf_lists[qf_curlist].qf_count > 0)
2399 /* Adding to existing list, find last entry. */
2400 for (prevp = qf_lists[qf_curlist].qf_start;
2401 prevp->qf_next != prevp; prevp = prevp->qf_next)
2402 ;
2403
2404 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002405 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002406 goto theend;
2407 if (fcount == 0)
2408 {
2409 EMSG(_(e_nomatch));
2410 goto theend;
2411 }
2412
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002413 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002414 for (fi = 0; fi < fcount && !got_int; ++fi)
2415 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002416 if (time(NULL) > seconds)
2417 {
2418 /* Display the file name every second or so. */
2419 seconds = time(NULL);
2420 msg_start();
2421 p = msg_strtrunc(fnames[fi]);
2422 if (p == NULL)
2423 msg_outtrans(fnames[fi]);
2424 else
2425 {
2426 msg_outtrans(p);
2427 vim_free(p);
2428 }
2429 msg_clr_eos();
2430 msg_didout = FALSE; /* overwrite this message */
2431 msg_nowait = TRUE; /* don't wait for this message */
2432 msg_col = 0;
2433 out_flush();
2434 }
2435
Bram Moolenaar81695252004-12-29 20:58:21 +00002436 buf = buflist_findname_exp(fnames[fi]);
2437 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2438 {
2439 /* Remember that a buffer with this name already exists. */
2440 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002441 using_dummy = TRUE;
2442
2443#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2444 /* Don't do Filetype autocommands to avoid loading syntax and
2445 * indent scripts, a great speed improvement. */
2446 save_ei = au_event_disable(",Filetype");
2447#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002448
2449 /* Load file into a buffer, so that 'fileencoding' is detected,
2450 * autocommands applied, etc. */
2451 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002452
2453#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2454 au_event_restore(save_ei);
2455#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002456 }
2457 else
2458 /* Use existing, loaded buffer. */
2459 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002460
Bram Moolenaar81695252004-12-29 20:58:21 +00002461 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002462 {
2463 if (!got_int)
2464 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2465 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002466 else
2467 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002468 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002469 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002470 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002471 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002472 /* For ":1vimgrep" look for multiple matches. */
2473 col = 0;
2474 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2475 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002476 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00002477 if (qf_add_entry(&prevp,
2478 NULL, /* dir */
2479 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00002480 ml_get_buf(buf,
2481 regmatch.startpos[0].lnum + lnum, FALSE),
2482 regmatch.startpos[0].lnum + lnum,
2483 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00002484 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002485 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002486 0, /* nr */
2487 0, /* type */
2488 TRUE /* valid */
2489 ) == FAIL)
2490 {
2491 got_int = TRUE;
2492 break;
2493 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002494 else
2495 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002496 if ((flags & VGR_GLOBAL) == 0
2497 || regmatch.endpos[0].lnum > 0)
2498 break;
2499 col = regmatch.endpos[0].col
2500 + (col == regmatch.endpos[0].col);
2501 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
2502 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002503 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002504 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00002505 if (got_int)
2506 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002507 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002508
2509 if (using_dummy)
2510 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002511 if (found_match && first_match_buf == NULL)
2512 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00002513 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002514 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002515 /* Never keep a dummy buffer if there is another buffer
2516 * with the same name. */
2517 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002518 buf = NULL;
2519 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002520 else if (!buf_hide(buf))
2521 {
2522 /* When not hiding the buffer and no match was found we
2523 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00002524 * there was a match and it wasn't the first one or we
2525 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002526 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002527 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002528 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002529 buf = NULL;
2530 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002531 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002532 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002533 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002534 buf = NULL;
2535 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002536 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002537
2538#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2539 if (buf != NULL)
2540 {
2541 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaar748bf032005-02-02 23:04:36 +00002542 * need to be done now, in that buffer. And then the
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002543 * modelines need to be done (again). */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002544 aucmd_prepbuf(&aco, buf);
2545 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
2546 buf->b_fname, TRUE, buf);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002547 do_modelines(FALSE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002548 aucmd_restbuf(&aco);
2549 }
2550#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002551 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002552 }
2553 }
2554
2555 FreeWild(fcount, fnames);
2556
2557 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2558 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2559 qf_lists[qf_curlist].qf_index = 1;
2560
2561#ifdef FEAT_WINDOWS
2562 qf_update_buffer();
2563#endif
2564
2565 /* Jump to first match. */
2566 if (qf_lists[qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002567 {
2568 if ((flags & VGR_NOJUMP) == 0)
2569 qf_jump(0, 0, eap->forceit);
2570 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002571 else
2572 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002573
Bram Moolenaar7c626922005-02-07 22:01:03 +00002574#ifdef FEAT_AUTOCMD
2575 if (au_name != NULL)
2576 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2577 curbuf->b_fname, TRUE, curbuf);
2578#endif
2579
Bram Moolenaar86b68352004-12-27 21:59:20 +00002580theend:
2581 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002582}
2583
2584/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00002585 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00002586 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00002587 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
2588 * If "s" is not NULL terminate the pattern with a NUL.
2589 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00002590 */
2591 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00002592skip_vimgrep_pat(p, s, flags)
2593 char_u *p;
2594 char_u **s;
2595 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002596{
2597 int c;
2598
2599 if (vim_isIDc(*p))
2600 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002601 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00002602 if (s != NULL)
2603 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002604 p = skiptowhite(p);
2605 if (s != NULL && *p != NUL)
2606 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002607 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002608 else
2609 {
2610 /* ":vimgrep /pattern/[g][j] fname" */
2611 if (s != NULL)
2612 *s = p + 1;
2613 c = *p;
2614 p = skip_regexp(p + 1, c, TRUE, NULL);
2615 if (*p != c)
2616 return NULL;
2617
2618 /* Truncate the pattern. */
2619 if (s != NULL)
2620 *p = NUL;
2621 ++p;
2622
2623 /* Find the flags */
2624 while (*p == 'g' || *p == 'j')
2625 {
2626 if (flags != NULL)
2627 {
2628 if (*p == 'g')
2629 *flags |= VGR_GLOBAL;
2630 else
2631 *flags |= VGR_NOJUMP;
2632 }
2633 ++p;
2634 }
2635 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00002636 return p;
2637}
2638
2639/*
Bram Moolenaar81695252004-12-29 20:58:21 +00002640 * Load file "fname" into a dummy buffer and return the buffer pointer.
2641 * Returns NULL if it fails.
2642 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
2643 */
2644 static buf_T *
2645load_dummy_buffer(fname)
2646 char_u *fname;
2647{
2648 buf_T *newbuf;
2649 int failed = TRUE;
2650#ifdef FEAT_AUTOCMD
2651 aco_save_T aco;
2652#else
2653 buf_T *old_curbuf = curbuf;
2654#endif
2655
2656 /* Allocate a buffer without putting it in the buffer list. */
2657 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
2658 if (newbuf == NULL)
2659 return NULL;
2660
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002661 /* Init the options. */
2662 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
2663
Bram Moolenaar81695252004-12-29 20:58:21 +00002664#ifdef FEAT_AUTOCMD
2665 /* set curwin/curbuf to buf and save a few things */
2666 aucmd_prepbuf(&aco, newbuf);
2667#else
2668 curbuf = newbuf;
2669 curwin->w_buffer = newbuf;
2670#endif
2671
2672 /* Need to set the filename for autocommands. */
2673 (void)setfname(curbuf, fname, NULL, FALSE);
2674
2675 if (ml_open() == OK)
2676 {
2677 /* Create swap file now to avoid the ATTENTION message. */
2678 check_need_swap(TRUE);
2679
2680 /* Remove the "dummy" flag, otherwise autocommands may not
2681 * work. */
2682 curbuf->b_flags &= ~BF_DUMMY;
2683
2684 if (readfile(fname, NULL,
2685 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
2686 NULL, READ_NEW | READ_DUMMY) == OK
2687 && !(curbuf->b_flags & BF_NEW))
2688 {
2689 failed = FALSE;
2690 if (curbuf != newbuf)
2691 {
2692 /* Bloody autocommands changed the buffer! */
2693 if (buf_valid(newbuf))
2694 wipe_buffer(newbuf, FALSE);
2695 newbuf = curbuf;
2696 }
2697 }
2698 }
2699
2700#ifdef FEAT_AUTOCMD
2701 /* restore curwin/curbuf and a few other things */
2702 aucmd_restbuf(&aco);
2703#else
2704 curbuf = old_curbuf;
2705 curwin->w_buffer = old_curbuf;
2706#endif
2707
2708 if (!buf_valid(newbuf))
2709 return NULL;
2710 if (failed)
2711 {
2712 wipe_dummy_buffer(newbuf);
2713 return NULL;
2714 }
2715 return newbuf;
2716}
2717
2718/*
2719 * Wipe out the dummy buffer that load_dummy_buffer() created.
2720 */
2721 static void
2722wipe_dummy_buffer(buf)
2723 buf_T *buf;
2724{
2725 if (curbuf != buf) /* safety check */
2726 wipe_buffer(buf, FALSE);
2727}
2728
2729/*
2730 * Unload the dummy buffer that load_dummy_buffer() created.
2731 */
2732 static void
2733unload_dummy_buffer(buf)
2734 buf_T *buf;
2735{
2736 if (curbuf != buf) /* safety check */
2737 close_buffer(NULL, buf, DOBUF_UNLOAD);
2738}
2739
Bram Moolenaar05159a02005-02-26 23:04:13 +00002740#if defined(FEAT_EVAL) || defined(PROTO)
2741/*
2742 * Add each quickfix error to list "list" as a dictionary.
2743 */
2744 int
2745get_errorlist(list)
2746 list_T *list;
2747{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002748 dict_T *dict;
2749 char_u buf[2];
2750 qfline_T *qfp;
2751 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002752
2753 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
2754 {
2755 EMSG(_(e_quickfix));
2756 return FAIL;
2757 }
2758
2759 qfp = qf_lists[qf_curlist].qf_start;
2760 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; ++i)
2761 {
2762 if ((dict = dict_alloc()) == NULL)
2763 return FAIL;
2764 if (list_append_dict(list, dict) == FAIL)
2765 return FAIL;
2766
2767 buf[0] = qfp->qf_type;
2768 buf[1] = NUL;
2769 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
2770 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
2771 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
2772 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
2773 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002774 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002775 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
2776 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
2777 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
2778 return FAIL;
2779
2780 qfp = qfp->qf_next;
2781 }
2782 return OK;
2783}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002784
2785/*
2786 * Populate the quickfix list with the items supplied in the list
2787 * of dictionaries.
2788 */
2789 int
2790set_errorlist(list)
2791 list_T *list;
2792{
2793 listitem_T *li;
2794 dict_T *d;
2795 char_u *filename, *pattern, *text, *type;
2796 long lnum;
2797 int col, nr;
2798 int vcol;
2799 qfline_T *prevp = NULL;
2800 int valid, status;
2801 int retval = OK;
2802
2803 /* make place for a new list */
2804 qf_new_list();
2805
2806 for (li = list->lv_first; li != NULL; li = li->li_next)
2807 {
2808 if (li->li_tv.v_type != VAR_DICT)
2809 continue; /* Skip non-dict items */
2810
2811 d = li->li_tv.vval.v_dict;
2812 if (d == NULL)
2813 continue;
2814
2815 filename = get_dict_string(d, (char_u *)"filename");
2816 lnum = get_dict_number(d, (char_u *)"lnum");
2817 col = get_dict_number(d, (char_u *)"col");
2818 vcol = get_dict_number(d, (char_u *)"vcol");
2819 nr = get_dict_number(d, (char_u *)"nr");
2820 type = get_dict_string(d, (char_u *)"type");
2821 pattern = get_dict_string(d, (char_u *)"pattern");
2822 text = get_dict_string(d, (char_u *)"text");
2823 if (text == NULL)
2824 text = vim_strsave((char_u *)"");
2825
2826 valid = TRUE;
2827 if (filename == NULL || (lnum == 0 && pattern == NULL))
2828 valid = FALSE;
2829
2830 status = qf_add_entry(&prevp,
2831 NULL, /* dir */
2832 filename,
2833 text,
2834 lnum,
2835 col,
2836 vcol, /* vis_col */
2837 pattern, /* search pattern */
2838 nr,
2839 type == NULL ? NUL : *type,
2840 valid);
2841
2842 vim_free(filename);
2843 vim_free(pattern);
2844 vim_free(text);
2845 vim_free(type);
2846
2847 if (status == FAIL)
2848 {
2849 retval = FAIL;
2850 break;
2851 }
2852 }
2853
2854 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2855 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2856 qf_lists[qf_curlist].qf_index = 1;
2857
2858#ifdef FEAT_WINDOWS
2859 qf_update_buffer();
2860#endif
2861
2862 return retval;
2863}
Bram Moolenaar05159a02005-02-26 23:04:13 +00002864#endif
2865
Bram Moolenaar81695252004-12-29 20:58:21 +00002866/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002867 * ":[range]cbuffer [bufnr]" command.
2868 */
2869 void
2870ex_cbuffer(eap)
2871 exarg_T *eap;
2872{
2873 buf_T *buf = NULL;
2874
2875 if (*eap->arg == NUL)
2876 buf = curbuf;
2877 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
2878 buf = buflist_findnr(atoi((char *)eap->arg));
2879 if (buf == NULL)
2880 EMSG(_(e_invarg));
2881 else if (buf->b_ml.ml_mfp == NULL)
2882 EMSG(_("E681: Buffer is not loaded"));
2883 else
2884 {
2885 if (eap->addr_count == 0)
2886 {
2887 eap->line1 = 1;
2888 eap->line2 = buf->b_ml.ml_line_count;
2889 }
2890 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
2891 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
2892 EMSG(_(e_invrange));
2893 else
2894 qf_init_ext(NULL, buf, p_efm, TRUE, eap->line1, eap->line2);
2895 }
2896}
2897
2898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 * ":helpgrep {pattern}"
2900 */
2901 void
2902ex_helpgrep(eap)
2903 exarg_T *eap;
2904{
2905 regmatch_T regmatch;
2906 char_u *save_cpo;
2907 char_u *p;
2908 int fcount;
2909 char_u **fnames;
2910 FILE *fd;
2911 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002912 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002914#ifdef FEAT_MULTI_LANG
2915 char_u *lang;
2916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2919 save_cpo = p_cpo;
2920 p_cpo = (char_u *)"";
2921
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002922#ifdef FEAT_MULTI_LANG
2923 /* Check for a specified language */
2924 lang = check_help_lang(eap->arg);
2925#endif
2926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
2928 regmatch.rm_ic = FALSE;
2929 if (regmatch.regprog != NULL)
2930 {
2931 /* create a new quickfix list */
2932 qf_new_list();
2933
2934 /* Go through all directories in 'runtimepath' */
2935 p = p_rtp;
2936 while (*p != NUL && !got_int)
2937 {
2938 copy_option_part(&p, NameBuff, MAXPATHL, ",");
2939
2940 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
2941 add_pathsep(NameBuff);
2942 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
2943 if (gen_expand_wildcards(1, &NameBuff, &fcount,
2944 &fnames, EW_FILE|EW_SILENT) == OK
2945 && fcount > 0)
2946 {
2947 for (fi = 0; fi < fcount && !got_int; ++fi)
2948 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002949#ifdef FEAT_MULTI_LANG
2950 /* Skip files for a different language. */
2951 if (lang != NULL
2952 && STRNICMP(lang, fnames[fi]
2953 + STRLEN(fnames[fi]) - 3, 2) != 0
2954 && !(STRNICMP(lang, "en", 2) == 0
2955 && STRNICMP("txt", fnames[fi]
2956 + STRLEN(fnames[fi]) - 3, 3) == 0))
2957 continue;
2958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 fd = fopen((char *)fnames[fi], "r");
2960 if (fd != NULL)
2961 {
2962 lnum = 1;
2963 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
2964 {
2965 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
2966 {
2967 int l = STRLEN(IObuff);
2968
2969 /* remove trailing CR, LF, spaces, etc. */
2970 while (l > 0 && IObuff[l - 1] <= ' ')
2971 IObuff[--l] = NUL;
2972
2973 if (qf_add_entry(&prevp,
2974 NULL, /* dir */
2975 fnames[fi],
2976 IObuff,
2977 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00002978 (int)(regmatch.startp[0] - IObuff)
2979 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00002980 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002981 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 0, /* nr */
2983 1, /* type */
2984 TRUE /* valid */
2985 ) == FAIL)
2986 {
2987 got_int = TRUE;
2988 break;
2989 }
2990 }
2991 ++lnum;
2992 line_breakcheck();
2993 }
2994 fclose(fd);
2995 }
2996 }
2997 FreeWild(fcount, fnames);
2998 }
2999 }
3000 vim_free(regmatch.regprog);
3001
3002 qf_lists[qf_curlist].qf_nonevalid = FALSE;
3003 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
3004 qf_lists[qf_curlist].qf_index = 1;
3005 }
3006
3007 p_cpo = save_cpo;
3008
3009#ifdef FEAT_WINDOWS
3010 qf_update_buffer();
3011#endif
3012
3013 /* Jump to first match. */
3014 if (qf_lists[qf_curlist].qf_count > 0)
3015 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003016 else
3017 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018}
3019
3020#endif /* FEAT_QUICKFIX */