blob: a4c016e09a8f1e55a53ba734bcdf0f5fad2f087f [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 Moolenaar86b68352004-12-27 21:59:20 +00002341 int i;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002342 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002343 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002344 long lnum;
2345 garray_T ga;
Bram Moolenaar81695252004-12-29 20:58:21 +00002346 buf_T *buf;
2347 int duplicate_name = FALSE;
2348 int using_dummy;
2349 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002350 buf_T *first_match_buf = NULL;
2351 time_t seconds = 0;
2352#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2353 char_u *save_ei = NULL;
2354 aco_save_T aco;
2355#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002356#ifdef FEAT_AUTOCMD
2357 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002358 int flags = 0;
2359 colnr_T col;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002360
2361 switch (eap->cmdidx)
2362 {
2363 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
2364 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
2365 default: break;
2366 }
2367 if (au_name != NULL)
2368 {
2369 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2370 curbuf->b_fname, TRUE, curbuf);
2371 if (did_throw || force_abort)
2372 return;
2373 }
2374#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002375
Bram Moolenaar81695252004-12-29 20:58:21 +00002376 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002377 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002378 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002379 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002380 {
Bram Moolenaar748bf032005-02-02 23:04:36 +00002381 EMSG(_("E682: Invalid search pattern or delimiter"));
2382 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002383 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002384 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002385 if (regmatch.regprog == NULL)
2386 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002387 regmatch.rmm_ic = p_ic;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002388
2389 p = skipwhite(p);
2390 if (*p == NUL)
2391 {
2392 EMSG(_("E683: File name missing or invalid pattern"));
2393 goto theend;
2394 }
2395
2396 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
2397 || qf_curlist == qf_listcount)
2398 /* make place for a new list */
2399 qf_new_list();
2400 else if (qf_lists[qf_curlist].qf_count > 0)
2401 /* Adding to existing list, find last entry. */
2402 for (prevp = qf_lists[qf_curlist].qf_start;
2403 prevp->qf_next != prevp; prevp = prevp->qf_next)
2404 ;
2405
2406 /* parse the list of arguments */
2407 if (get_arglist(&ga, p) == FAIL)
2408 goto theend;
2409 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
2410 &fcount, &fnames, EW_FILE|EW_NOTFOUND);
2411 ga_clear(&ga);
2412 if (i == FAIL)
2413 goto theend;
2414 if (fcount == 0)
2415 {
2416 EMSG(_(e_nomatch));
2417 goto theend;
2418 }
2419
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002420 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002421 for (fi = 0; fi < fcount && !got_int; ++fi)
2422 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002423 if (time(NULL) > seconds)
2424 {
2425 /* Display the file name every second or so. */
2426 seconds = time(NULL);
2427 msg_start();
2428 p = msg_strtrunc(fnames[fi]);
2429 if (p == NULL)
2430 msg_outtrans(fnames[fi]);
2431 else
2432 {
2433 msg_outtrans(p);
2434 vim_free(p);
2435 }
2436 msg_clr_eos();
2437 msg_didout = FALSE; /* overwrite this message */
2438 msg_nowait = TRUE; /* don't wait for this message */
2439 msg_col = 0;
2440 out_flush();
2441 }
2442
Bram Moolenaar81695252004-12-29 20:58:21 +00002443 buf = buflist_findname_exp(fnames[fi]);
2444 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2445 {
2446 /* Remember that a buffer with this name already exists. */
2447 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002448 using_dummy = TRUE;
2449
2450#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2451 /* Don't do Filetype autocommands to avoid loading syntax and
2452 * indent scripts, a great speed improvement. */
2453 save_ei = au_event_disable(",Filetype");
2454#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002455
2456 /* Load file into a buffer, so that 'fileencoding' is detected,
2457 * autocommands applied, etc. */
2458 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002459
2460#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2461 au_event_restore(save_ei);
2462#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002463 }
2464 else
2465 /* Use existing, loaded buffer. */
2466 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002467
Bram Moolenaar81695252004-12-29 20:58:21 +00002468 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002469 {
2470 if (!got_int)
2471 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2472 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002473 else
2474 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002475 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002476 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002477 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002478 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002479 /* For ":1vimgrep" look for multiple matches. */
2480 col = 0;
2481 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2482 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002483 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00002484 if (qf_add_entry(&prevp,
2485 NULL, /* dir */
2486 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00002487 ml_get_buf(buf,
2488 regmatch.startpos[0].lnum + lnum, FALSE),
2489 regmatch.startpos[0].lnum + lnum,
2490 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00002491 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002492 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002493 0, /* nr */
2494 0, /* type */
2495 TRUE /* valid */
2496 ) == FAIL)
2497 {
2498 got_int = TRUE;
2499 break;
2500 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002501 else
2502 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002503 if ((flags & VGR_GLOBAL) == 0
2504 || regmatch.endpos[0].lnum > 0)
2505 break;
2506 col = regmatch.endpos[0].col
2507 + (col == regmatch.endpos[0].col);
2508 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
2509 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002510 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002511 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00002512 if (got_int)
2513 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002514 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002515
2516 if (using_dummy)
2517 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002518 if (found_match && first_match_buf == NULL)
2519 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00002520 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002521 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002522 /* Never keep a dummy buffer if there is another buffer
2523 * with the same name. */
2524 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002525 buf = NULL;
2526 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002527 else if (!buf_hide(buf))
2528 {
2529 /* When not hiding the buffer and no match was found we
2530 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00002531 * there was a match and it wasn't the first one or we
2532 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002533 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002534 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002535 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002536 buf = NULL;
2537 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002538 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002539 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002540 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002541 buf = NULL;
2542 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002543 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002544
2545#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2546 if (buf != NULL)
2547 {
2548 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaar748bf032005-02-02 23:04:36 +00002549 * need to be done now, in that buffer. And then the
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002550 * modelines need to be done (again). */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002551 aucmd_prepbuf(&aco, buf);
2552 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
2553 buf->b_fname, TRUE, buf);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002554 do_modelines(FALSE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002555 aucmd_restbuf(&aco);
2556 }
2557#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002558 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002559 }
2560 }
2561
2562 FreeWild(fcount, fnames);
2563
2564 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2565 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2566 qf_lists[qf_curlist].qf_index = 1;
2567
2568#ifdef FEAT_WINDOWS
2569 qf_update_buffer();
2570#endif
2571
2572 /* Jump to first match. */
2573 if (qf_lists[qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002574 {
2575 if ((flags & VGR_NOJUMP) == 0)
2576 qf_jump(0, 0, eap->forceit);
2577 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002578 else
2579 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002580
Bram Moolenaar7c626922005-02-07 22:01:03 +00002581#ifdef FEAT_AUTOCMD
2582 if (au_name != NULL)
2583 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2584 curbuf->b_fname, TRUE, curbuf);
2585#endif
2586
Bram Moolenaar86b68352004-12-27 21:59:20 +00002587theend:
2588 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002589}
2590
2591/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00002592 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00002593 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00002594 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
2595 * If "s" is not NULL terminate the pattern with a NUL.
2596 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00002597 */
2598 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00002599skip_vimgrep_pat(p, s, flags)
2600 char_u *p;
2601 char_u **s;
2602 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002603{
2604 int c;
2605
2606 if (vim_isIDc(*p))
2607 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002608 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00002609 if (s != NULL)
2610 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002611 p = skiptowhite(p);
2612 if (s != NULL && *p != NUL)
2613 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002614 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002615 else
2616 {
2617 /* ":vimgrep /pattern/[g][j] fname" */
2618 if (s != NULL)
2619 *s = p + 1;
2620 c = *p;
2621 p = skip_regexp(p + 1, c, TRUE, NULL);
2622 if (*p != c)
2623 return NULL;
2624
2625 /* Truncate the pattern. */
2626 if (s != NULL)
2627 *p = NUL;
2628 ++p;
2629
2630 /* Find the flags */
2631 while (*p == 'g' || *p == 'j')
2632 {
2633 if (flags != NULL)
2634 {
2635 if (*p == 'g')
2636 *flags |= VGR_GLOBAL;
2637 else
2638 *flags |= VGR_NOJUMP;
2639 }
2640 ++p;
2641 }
2642 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00002643 return p;
2644}
2645
2646/*
Bram Moolenaar81695252004-12-29 20:58:21 +00002647 * Load file "fname" into a dummy buffer and return the buffer pointer.
2648 * Returns NULL if it fails.
2649 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
2650 */
2651 static buf_T *
2652load_dummy_buffer(fname)
2653 char_u *fname;
2654{
2655 buf_T *newbuf;
2656 int failed = TRUE;
2657#ifdef FEAT_AUTOCMD
2658 aco_save_T aco;
2659#else
2660 buf_T *old_curbuf = curbuf;
2661#endif
2662
2663 /* Allocate a buffer without putting it in the buffer list. */
2664 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
2665 if (newbuf == NULL)
2666 return NULL;
2667
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002668 /* Init the options. */
2669 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
2670
Bram Moolenaar81695252004-12-29 20:58:21 +00002671#ifdef FEAT_AUTOCMD
2672 /* set curwin/curbuf to buf and save a few things */
2673 aucmd_prepbuf(&aco, newbuf);
2674#else
2675 curbuf = newbuf;
2676 curwin->w_buffer = newbuf;
2677#endif
2678
2679 /* Need to set the filename for autocommands. */
2680 (void)setfname(curbuf, fname, NULL, FALSE);
2681
2682 if (ml_open() == OK)
2683 {
2684 /* Create swap file now to avoid the ATTENTION message. */
2685 check_need_swap(TRUE);
2686
2687 /* Remove the "dummy" flag, otherwise autocommands may not
2688 * work. */
2689 curbuf->b_flags &= ~BF_DUMMY;
2690
2691 if (readfile(fname, NULL,
2692 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
2693 NULL, READ_NEW | READ_DUMMY) == OK
2694 && !(curbuf->b_flags & BF_NEW))
2695 {
2696 failed = FALSE;
2697 if (curbuf != newbuf)
2698 {
2699 /* Bloody autocommands changed the buffer! */
2700 if (buf_valid(newbuf))
2701 wipe_buffer(newbuf, FALSE);
2702 newbuf = curbuf;
2703 }
2704 }
2705 }
2706
2707#ifdef FEAT_AUTOCMD
2708 /* restore curwin/curbuf and a few other things */
2709 aucmd_restbuf(&aco);
2710#else
2711 curbuf = old_curbuf;
2712 curwin->w_buffer = old_curbuf;
2713#endif
2714
2715 if (!buf_valid(newbuf))
2716 return NULL;
2717 if (failed)
2718 {
2719 wipe_dummy_buffer(newbuf);
2720 return NULL;
2721 }
2722 return newbuf;
2723}
2724
2725/*
2726 * Wipe out the dummy buffer that load_dummy_buffer() created.
2727 */
2728 static void
2729wipe_dummy_buffer(buf)
2730 buf_T *buf;
2731{
2732 if (curbuf != buf) /* safety check */
2733 wipe_buffer(buf, FALSE);
2734}
2735
2736/*
2737 * Unload the dummy buffer that load_dummy_buffer() created.
2738 */
2739 static void
2740unload_dummy_buffer(buf)
2741 buf_T *buf;
2742{
2743 if (curbuf != buf) /* safety check */
2744 close_buffer(NULL, buf, DOBUF_UNLOAD);
2745}
2746
Bram Moolenaar05159a02005-02-26 23:04:13 +00002747#if defined(FEAT_EVAL) || defined(PROTO)
2748/*
2749 * Add each quickfix error to list "list" as a dictionary.
2750 */
2751 int
2752get_errorlist(list)
2753 list_T *list;
2754{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002755 dict_T *dict;
2756 char_u buf[2];
2757 qfline_T *qfp;
2758 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002759
2760 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
2761 {
2762 EMSG(_(e_quickfix));
2763 return FAIL;
2764 }
2765
2766 qfp = qf_lists[qf_curlist].qf_start;
2767 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; ++i)
2768 {
2769 if ((dict = dict_alloc()) == NULL)
2770 return FAIL;
2771 if (list_append_dict(list, dict) == FAIL)
2772 return FAIL;
2773
2774 buf[0] = qfp->qf_type;
2775 buf[1] = NUL;
2776 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
2777 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
2778 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
2779 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
2780 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002781 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002782 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
2783 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
2784 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
2785 return FAIL;
2786
2787 qfp = qfp->qf_next;
2788 }
2789 return OK;
2790}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002791
2792/*
2793 * Populate the quickfix list with the items supplied in the list
2794 * of dictionaries.
2795 */
2796 int
2797set_errorlist(list)
2798 list_T *list;
2799{
2800 listitem_T *li;
2801 dict_T *d;
2802 char_u *filename, *pattern, *text, *type;
2803 long lnum;
2804 int col, nr;
2805 int vcol;
2806 qfline_T *prevp = NULL;
2807 int valid, status;
2808 int retval = OK;
2809
2810 /* make place for a new list */
2811 qf_new_list();
2812
2813 for (li = list->lv_first; li != NULL; li = li->li_next)
2814 {
2815 if (li->li_tv.v_type != VAR_DICT)
2816 continue; /* Skip non-dict items */
2817
2818 d = li->li_tv.vval.v_dict;
2819 if (d == NULL)
2820 continue;
2821
2822 filename = get_dict_string(d, (char_u *)"filename");
2823 lnum = get_dict_number(d, (char_u *)"lnum");
2824 col = get_dict_number(d, (char_u *)"col");
2825 vcol = get_dict_number(d, (char_u *)"vcol");
2826 nr = get_dict_number(d, (char_u *)"nr");
2827 type = get_dict_string(d, (char_u *)"type");
2828 pattern = get_dict_string(d, (char_u *)"pattern");
2829 text = get_dict_string(d, (char_u *)"text");
2830 if (text == NULL)
2831 text = vim_strsave((char_u *)"");
2832
2833 valid = TRUE;
2834 if (filename == NULL || (lnum == 0 && pattern == NULL))
2835 valid = FALSE;
2836
2837 status = qf_add_entry(&prevp,
2838 NULL, /* dir */
2839 filename,
2840 text,
2841 lnum,
2842 col,
2843 vcol, /* vis_col */
2844 pattern, /* search pattern */
2845 nr,
2846 type == NULL ? NUL : *type,
2847 valid);
2848
2849 vim_free(filename);
2850 vim_free(pattern);
2851 vim_free(text);
2852 vim_free(type);
2853
2854 if (status == FAIL)
2855 {
2856 retval = FAIL;
2857 break;
2858 }
2859 }
2860
2861 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2862 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2863 qf_lists[qf_curlist].qf_index = 1;
2864
2865#ifdef FEAT_WINDOWS
2866 qf_update_buffer();
2867#endif
2868
2869 return retval;
2870}
Bram Moolenaar05159a02005-02-26 23:04:13 +00002871#endif
2872
Bram Moolenaar81695252004-12-29 20:58:21 +00002873/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002874 * ":[range]cbuffer [bufnr]" command.
2875 */
2876 void
2877ex_cbuffer(eap)
2878 exarg_T *eap;
2879{
2880 buf_T *buf = NULL;
2881
2882 if (*eap->arg == NUL)
2883 buf = curbuf;
2884 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
2885 buf = buflist_findnr(atoi((char *)eap->arg));
2886 if (buf == NULL)
2887 EMSG(_(e_invarg));
2888 else if (buf->b_ml.ml_mfp == NULL)
2889 EMSG(_("E681: Buffer is not loaded"));
2890 else
2891 {
2892 if (eap->addr_count == 0)
2893 {
2894 eap->line1 = 1;
2895 eap->line2 = buf->b_ml.ml_line_count;
2896 }
2897 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
2898 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
2899 EMSG(_(e_invrange));
2900 else
2901 qf_init_ext(NULL, buf, p_efm, TRUE, eap->line1, eap->line2);
2902 }
2903}
2904
2905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 * ":helpgrep {pattern}"
2907 */
2908 void
2909ex_helpgrep(eap)
2910 exarg_T *eap;
2911{
2912 regmatch_T regmatch;
2913 char_u *save_cpo;
2914 char_u *p;
2915 int fcount;
2916 char_u **fnames;
2917 FILE *fd;
2918 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002919 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002921#ifdef FEAT_MULTI_LANG
2922 char_u *lang;
2923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2926 save_cpo = p_cpo;
2927 p_cpo = (char_u *)"";
2928
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002929#ifdef FEAT_MULTI_LANG
2930 /* Check for a specified language */
2931 lang = check_help_lang(eap->arg);
2932#endif
2933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
2935 regmatch.rm_ic = FALSE;
2936 if (regmatch.regprog != NULL)
2937 {
2938 /* create a new quickfix list */
2939 qf_new_list();
2940
2941 /* Go through all directories in 'runtimepath' */
2942 p = p_rtp;
2943 while (*p != NUL && !got_int)
2944 {
2945 copy_option_part(&p, NameBuff, MAXPATHL, ",");
2946
2947 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
2948 add_pathsep(NameBuff);
2949 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
2950 if (gen_expand_wildcards(1, &NameBuff, &fcount,
2951 &fnames, EW_FILE|EW_SILENT) == OK
2952 && fcount > 0)
2953 {
2954 for (fi = 0; fi < fcount && !got_int; ++fi)
2955 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002956#ifdef FEAT_MULTI_LANG
2957 /* Skip files for a different language. */
2958 if (lang != NULL
2959 && STRNICMP(lang, fnames[fi]
2960 + STRLEN(fnames[fi]) - 3, 2) != 0
2961 && !(STRNICMP(lang, "en", 2) == 0
2962 && STRNICMP("txt", fnames[fi]
2963 + STRLEN(fnames[fi]) - 3, 3) == 0))
2964 continue;
2965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 fd = fopen((char *)fnames[fi], "r");
2967 if (fd != NULL)
2968 {
2969 lnum = 1;
2970 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
2971 {
2972 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
2973 {
2974 int l = STRLEN(IObuff);
2975
2976 /* remove trailing CR, LF, spaces, etc. */
2977 while (l > 0 && IObuff[l - 1] <= ' ')
2978 IObuff[--l] = NUL;
2979
2980 if (qf_add_entry(&prevp,
2981 NULL, /* dir */
2982 fnames[fi],
2983 IObuff,
2984 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00002985 (int)(regmatch.startp[0] - IObuff)
2986 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00002987 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002988 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 0, /* nr */
2990 1, /* type */
2991 TRUE /* valid */
2992 ) == FAIL)
2993 {
2994 got_int = TRUE;
2995 break;
2996 }
2997 }
2998 ++lnum;
2999 line_breakcheck();
3000 }
3001 fclose(fd);
3002 }
3003 }
3004 FreeWild(fcount, fnames);
3005 }
3006 }
3007 vim_free(regmatch.regprog);
3008
3009 qf_lists[qf_curlist].qf_nonevalid = FALSE;
3010 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
3011 qf_lists[qf_curlist].qf_index = 1;
3012 }
3013
3014 p_cpo = save_cpo;
3015
3016#ifdef FEAT_WINDOWS
3017 qf_update_buffer();
3018#endif
3019
3020 /* Jump to first match. */
3021 if (qf_lists[qf_curlist].qf_count > 0)
3022 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003023 else
3024 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026
3027#endif /* FEAT_QUICKFIX */