blob: a481b0f316dabb44a7819a109f46da42094a2ebc [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
Bram Moolenaar051b7822005-05-19 21:00:46 +00001481 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1482 i, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1484 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1485 if (qfp->qf_lnum == 0)
1486 IObuff[0] = NUL;
1487 else if (qfp->qf_col == 0)
1488 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1489 else
1490 sprintf((char *)IObuff, ":%ld col %d",
1491 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001492 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1494 msg_puts_attr(IObuff, hl_attr(HLF_N));
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001495 if (qfp->qf_pattern != NULL)
1496 {
1497 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1498 STRCAT(IObuff, ":");
1499 msg_puts(IObuff);
1500 }
1501 msg_puts((char_u *)" ");
1502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 /* Remove newlines and leading whitespace from the text.
1504 * For an unrecognized line keep the indent, the compiler may
1505 * mark a word with ^^^^. */
1506 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1507 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1508 IObuff, IOSIZE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001509 msg_prt_line(IObuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 out_flush(); /* show one line at a time */
1511 need_return = TRUE;
1512 last_printed = i;
1513 }
1514 }
1515 if (more_back)
1516 {
1517 /* scrolling backwards from the more-prompt */
1518 /* TODO: compute the number of items from the screen lines */
1519 more_back = more_back * 2 - 1;
1520 while (i > last_printed - more_back && i > idx1)
1521 {
1522 do
1523 {
1524 qfp = qfp->qf_prev;
1525 --i;
1526 }
1527 while (i > idx1 && !qfp->qf_valid && !all);
1528 }
1529 more_back = 0;
1530 }
1531 else
1532 {
1533 qfp = qfp->qf_next;
1534 ++i;
1535 }
1536 ui_breakcheck();
1537 }
1538 more_back_used = FALSE;
1539}
1540
1541/*
1542 * Remove newlines and leading whitespace from an error message.
1543 * Put the result in "buf[bufsize]".
1544 */
1545 static void
1546qf_fmt_text(text, buf, bufsize)
1547 char_u *text;
1548 char_u *buf;
1549 int bufsize;
1550{
1551 int i;
1552 char_u *p = text;
1553
1554 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1555 {
1556 if (*p == '\n')
1557 {
1558 buf[i] = ' ';
1559 while (*++p != NUL)
1560 if (!vim_iswhite(*p) && *p != '\n')
1561 break;
1562 }
1563 else
1564 buf[i] = *p++;
1565 }
1566 buf[i] = NUL;
1567}
1568
1569/*
1570 * ":colder [count]": Up in the quickfix stack.
1571 * ":cnewer [count]": Down in the quickfix stack.
1572 */
1573 void
1574qf_age(eap)
1575 exarg_T *eap;
1576{
1577 int count;
1578
1579 if (eap->addr_count != 0)
1580 count = eap->line2;
1581 else
1582 count = 1;
1583 while (count--)
1584 {
1585 if (eap->cmdidx == CMD_colder)
1586 {
1587 if (qf_curlist == 0)
1588 {
1589 EMSG(_("E380: At bottom of quickfix stack"));
1590 return;
1591 }
1592 --qf_curlist;
1593 }
1594 else
1595 {
1596 if (qf_curlist >= qf_listcount - 1)
1597 {
1598 EMSG(_("E381: At top of quickfix stack"));
1599 return;
1600 }
1601 ++qf_curlist;
1602 }
1603 }
1604 qf_msg();
1605}
1606
1607 static void
1608qf_msg()
1609{
1610 smsg((char_u *)_("error list %d of %d; %d errors"),
1611 qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1612#ifdef FEAT_WINDOWS
1613 qf_update_buffer();
1614#endif
1615}
1616
1617/*
1618 * free the error list
1619 */
1620 static void
1621qf_free(idx)
1622 int idx;
1623{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001624 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 while (qf_lists[idx].qf_count)
1627 {
1628 qfp = qf_lists[idx].qf_start->qf_next;
1629 vim_free(qf_lists[idx].qf_start->qf_text);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001630 vim_free(qf_lists[idx].qf_start->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 vim_free(qf_lists[idx].qf_start);
1632 qf_lists[idx].qf_start = qfp;
1633 --qf_lists[idx].qf_count;
1634 }
1635}
1636
1637/*
1638 * qf_mark_adjust: adjust marks
1639 */
1640 void
1641qf_mark_adjust(line1, line2, amount, amount_after)
1642 linenr_T line1;
1643 linenr_T line2;
1644 long amount;
1645 long amount_after;
1646{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001647 int i;
1648 qfline_T *qfp;
1649 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
1651 for (idx = 0; idx < qf_listcount; ++idx)
1652 if (qf_lists[idx].qf_count)
1653 for (i = 0, qfp = qf_lists[idx].qf_start;
1654 i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1655 if (qfp->qf_fnum == curbuf->b_fnum)
1656 {
1657 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1658 {
1659 if (amount == MAXLNUM)
1660 qfp->qf_cleared = TRUE;
1661 else
1662 qfp->qf_lnum += amount;
1663 }
1664 else if (amount_after && qfp->qf_lnum > line2)
1665 qfp->qf_lnum += amount_after;
1666 }
1667}
1668
1669/*
1670 * Make a nice message out of the error character and the error number:
1671 * char number message
1672 * e or E 0 " error"
1673 * w or W 0 " warning"
1674 * i or I 0 " info"
1675 * 0 0 ""
1676 * other 0 " c"
1677 * e or E n " error n"
1678 * w or W n " warning n"
1679 * i or I n " info n"
1680 * 0 n " error n"
1681 * other n " c n"
1682 * 1 x "" :helpgrep
1683 */
1684 static char_u *
1685qf_types(c, nr)
1686 int c, nr;
1687{
1688 static char_u buf[20];
1689 static char_u cc[3];
1690 char_u *p;
1691
1692 if (c == 'W' || c == 'w')
1693 p = (char_u *)" warning";
1694 else if (c == 'I' || c == 'i')
1695 p = (char_u *)" info";
1696 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1697 p = (char_u *)" error";
1698 else if (c == 0 || c == 1)
1699 p = (char_u *)"";
1700 else
1701 {
1702 cc[0] = ' ';
1703 cc[1] = c;
1704 cc[2] = NUL;
1705 p = cc;
1706 }
1707
1708 if (nr <= 0)
1709 return p;
1710
1711 sprintf((char *)buf, "%s %3d", (char *)p, nr);
1712 return buf;
1713}
1714
1715#if defined(FEAT_WINDOWS) || defined(PROTO)
1716/*
1717 * ":cwindow": open the quickfix window if we have errors to display,
1718 * close it if not.
1719 */
1720 void
1721ex_cwindow(eap)
1722 exarg_T *eap;
1723{
1724 win_T *win;
1725
1726 /*
1727 * Look for an existing quickfix window.
1728 */
1729 for (win = firstwin; win != NULL; win = win->w_next)
1730 if (bt_quickfix(win->w_buffer))
1731 break;
1732
1733 /*
1734 * If a quickfix window is open but we have no errors to display,
1735 * close the window. If a quickfix window is not open, then open
1736 * it if we have errors; otherwise, leave it closed.
1737 */
1738 if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1739 {
1740 if (win != NULL)
1741 ex_cclose(eap);
1742 }
1743 else if (win == NULL)
1744 ex_copen(eap);
1745}
1746
1747/*
1748 * ":cclose": close the window showing the list of errors.
1749 */
1750/*ARGSUSED*/
1751 void
1752ex_cclose(eap)
1753 exarg_T *eap;
1754{
1755 win_T *win;
1756
1757 /*
1758 * Find existing quickfix window and close it.
1759 */
1760 for (win = firstwin; win != NULL; win = win->w_next)
1761 if (bt_quickfix(win->w_buffer))
1762 break;
1763
1764 if (win != NULL)
1765 win_close(win, FALSE);
1766}
1767
1768/*
1769 * ":copen": open a window that shows the list of errors.
1770 */
1771 void
1772ex_copen(eap)
1773 exarg_T *eap;
1774{
1775 int height;
1776 buf_T *buf;
1777 win_T *win;
1778
1779 if (eap->addr_count != 0)
1780 height = eap->line2;
1781 else
1782 height = QF_WINHEIGHT;
1783
1784#ifdef FEAT_VISUAL
1785 reset_VIsual_and_resel(); /* stop Visual mode */
1786#endif
1787#ifdef FEAT_GUI
1788 need_mouse_correct = TRUE;
1789#endif
1790
1791 /*
1792 * Find existing quickfix window, or open a new one.
1793 */
1794 for (win = firstwin; win != NULL; win = win->w_next)
1795 if (bt_quickfix(win->w_buffer))
1796 break;
1797 if (win != NULL)
1798 win_goto(win);
1799 else
1800 {
1801 /* The current window becomes the previous window afterwards. */
1802 win = curwin;
1803
1804 /* Create the new window at the very bottom. */
1805 win_goto(lastwin);
1806 if (win_split(height, WSP_BELOW) == FAIL)
1807 return; /* not enough room for window */
1808#ifdef FEAT_SCROLLBIND
1809 curwin->w_p_scb = FALSE;
1810#endif
1811
1812 /*
1813 * Find existing quickfix buffer, or create a new one.
1814 */
1815 buf = qf_find_buf();
1816 if (buf == NULL)
1817 {
1818 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1819 /* switch off 'swapfile' */
1820 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1821 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1822 OPT_LOCAL);
1823 set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1824 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1825 }
1826 else if (buf != curbuf)
1827 set_curbuf(buf, DOBUF_GOTO);
1828
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001829#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 /* Only set the height when there is no window to the side. */
1831 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 win_setheight(height);
1834 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
1835 if (win_valid(win))
1836 prevwin = win;
1837 }
1838
1839 /*
1840 * Fill the buffer with the quickfix list.
1841 */
1842 qf_fill_buffer();
1843
1844 curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1845 curwin->w_cursor.col = 0;
1846 check_cursor();
1847 update_topline(); /* scroll to show the line */
1848}
1849
1850/*
1851 * Return the number of the current entry (line number in the quickfix
1852 * window).
1853 */
1854 linenr_T
1855qf_current_entry()
1856{
1857 return qf_lists[qf_curlist].qf_index;
1858}
1859
1860/*
1861 * Update the cursor position in the quickfix window to the current error.
1862 * Return TRUE if there is a quickfix window.
1863 */
1864 static int
1865qf_win_pos_update(old_qf_index)
1866 int old_qf_index; /* previous qf_index or zero */
1867{
1868 win_T *win;
1869 int qf_index = qf_lists[qf_curlist].qf_index;
1870
1871 /*
1872 * Put the cursor on the current error in the quickfix window, so that
1873 * it's viewable.
1874 */
1875 for (win = firstwin; win != NULL; win = win->w_next)
1876 if (bt_quickfix(win->w_buffer))
1877 break;
1878 if (win != NULL
1879 && qf_index <= win->w_buffer->b_ml.ml_line_count
1880 && old_qf_index != qf_index)
1881 {
1882 win_T *old_curwin = curwin;
1883
1884 curwin = win;
1885 curbuf = win->w_buffer;
1886 if (qf_index > old_qf_index)
1887 {
1888 curwin->w_redraw_top = old_qf_index;
1889 curwin->w_redraw_bot = qf_index;
1890 }
1891 else
1892 {
1893 curwin->w_redraw_top = qf_index;
1894 curwin->w_redraw_bot = old_qf_index;
1895 }
1896 curwin->w_cursor.lnum = qf_index;
1897 curwin->w_cursor.col = 0;
1898 update_topline(); /* scroll to show the line */
1899 redraw_later(VALID);
1900 curwin->w_redr_status = TRUE; /* update ruler */
1901 curwin = old_curwin;
1902 curbuf = curwin->w_buffer;
1903 }
1904 return win != NULL;
1905}
1906
1907/*
1908 * Find quickfix buffer.
1909 */
1910 static buf_T *
1911qf_find_buf()
1912{
1913 buf_T *buf;
1914
1915 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1916 if (bt_quickfix(buf))
1917 break;
1918 return buf;
1919}
1920
1921/*
1922 * Find the quickfix buffer. If it exists, update the contents.
1923 */
1924 static void
1925qf_update_buffer()
1926{
1927 buf_T *buf;
1928#ifdef FEAT_AUTOCMD
1929 aco_save_T aco;
1930#else
1931 buf_T *save_curbuf;
1932#endif
1933
1934 /* Check if a buffer for the quickfix list exists. Update it. */
1935 buf = qf_find_buf();
1936 if (buf != NULL)
1937 {
1938#ifdef FEAT_AUTOCMD
1939 /* set curwin/curbuf to buf and save a few things */
1940 aucmd_prepbuf(&aco, buf);
1941#else
1942 save_curbuf = curbuf;
1943 curbuf = buf;
1944#endif
1945
1946 qf_fill_buffer();
1947
1948#ifdef FEAT_AUTOCMD
1949 /* restore curwin/curbuf and a few other things */
1950 aucmd_restbuf(&aco);
1951#else
1952 curbuf = save_curbuf;
1953#endif
1954
1955 (void)qf_win_pos_update(0);
1956 }
1957}
1958
1959/*
1960 * Fill current buffer with quickfix errors, replacing any previous contents.
1961 * curbuf must be the quickfix buffer!
1962 */
1963 static void
1964qf_fill_buffer()
1965{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001966 linenr_T lnum;
1967 qfline_T *qfp;
1968 buf_T *errbuf;
1969 int len;
1970 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972 /* delete all existing lines */
1973 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
1974 (void)ml_delete((linenr_T)1, FALSE);
1975
1976 /* Check if there is anything to display */
1977 if (qf_curlist < qf_listcount)
1978 {
1979 /* Add one line for each error */
1980 qfp = qf_lists[qf_curlist].qf_start;
1981 for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
1982 {
1983 if (qfp->qf_fnum != 0
1984 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
1985 && errbuf->b_fname != NULL)
1986 {
1987 if (qfp->qf_type == 1) /* :helpgrep */
1988 STRCPY(IObuff, gettail(errbuf->b_fname));
1989 else
1990 STRCPY(IObuff, errbuf->b_fname);
1991 len = (int)STRLEN(IObuff);
1992 }
1993 else
1994 len = 0;
1995 IObuff[len++] = '|';
1996
1997 if (qfp->qf_lnum > 0)
1998 {
1999 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2000 len += (int)STRLEN(IObuff + len);
2001
2002 if (qfp->qf_col > 0)
2003 {
2004 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2005 len += (int)STRLEN(IObuff + len);
2006 }
2007
2008 sprintf((char *)IObuff + len, "%s",
2009 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2010 len += (int)STRLEN(IObuff + len);
2011 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002012 else if (qfp->qf_pattern != NULL)
2013 {
2014 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2015 len += (int)STRLEN(IObuff + len);
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 IObuff[len++] = '|';
2018 IObuff[len++] = ' ';
2019
2020 /* Remove newlines and leading whitespace from the text.
2021 * For an unrecognized line keep the indent, the compiler may
2022 * mark a word with ^^^^. */
2023 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2024 IObuff + len, IOSIZE - len);
2025
2026 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2027 == FAIL)
2028 break;
2029 qfp = qfp->qf_next;
2030 }
2031 /* Delete the empty line which is now at the end */
2032 (void)ml_delete(lnum + 1, FALSE);
2033 }
2034
2035 /* correct cursor position */
2036 check_lnums(TRUE);
2037
2038 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2039 * resembles reading a file into a buffer, it's more logical when using
2040 * autocommands. */
2041 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2042 curbuf->b_p_ma = FALSE;
2043
2044#ifdef FEAT_AUTOCMD
2045 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2046 FALSE, curbuf);
2047 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2048 FALSE, curbuf);
2049#endif
2050
2051 /* make sure it will be redrawn */
2052 redraw_curbuf_later(NOT_VALID);
2053
2054 /* Restore KeyTyped, setting 'filetype' may reset it. */
2055 KeyTyped = old_KeyTyped;
2056}
2057
2058#endif /* FEAT_WINDOWS */
2059
2060/*
2061 * Return TRUE if "buf" is the quickfix buffer.
2062 */
2063 int
2064bt_quickfix(buf)
2065 buf_T *buf;
2066{
2067 return (buf->b_p_bt[0] == 'q');
2068}
2069
2070/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002071 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2072 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 */
2074 int
2075bt_nofile(buf)
2076 buf_T *buf;
2077{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002078 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2079 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080}
2081
2082/*
2083 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2084 */
2085 int
2086bt_dontwrite(buf)
2087 buf_T *buf;
2088{
2089 return (buf->b_p_bt[0] == 'n');
2090}
2091
2092 int
2093bt_dontwrite_msg(buf)
2094 buf_T *buf;
2095{
2096 if (bt_dontwrite(buf))
2097 {
2098 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2099 return TRUE;
2100 }
2101 return FALSE;
2102}
2103
2104/*
2105 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2106 * and 'bufhidden'.
2107 */
2108 int
2109buf_hide(buf)
2110 buf_T *buf;
2111{
2112 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2113 switch (buf->b_p_bh[0])
2114 {
2115 case 'u': /* "unload" */
2116 case 'w': /* "wipe" */
2117 case 'd': return FALSE; /* "delete" */
2118 case 'h': return TRUE; /* "hide" */
2119 }
2120 return (p_hid || cmdmod.hide);
2121}
2122
2123/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002124 * Return TRUE when using ":vimgrep" for ":grep".
2125 */
2126 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002127grep_internal(cmdidx)
2128 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002129{
Bram Moolenaar81695252004-12-29 20:58:21 +00002130 return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002131 && STRCMP("internal",
2132 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2133}
2134
2135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 * Used for ":make", ":grep" and ":grepadd".
2137 */
2138 void
2139ex_make(eap)
2140 exarg_T *eap;
2141{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002142 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 char_u *cmd;
2144 unsigned len;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002145#ifdef FEAT_AUTOCMD
2146 char_u *au_name = NULL;
2147
2148 switch (eap->cmdidx)
2149 {
2150 case CMD_make: au_name = (char_u *)"make"; break;
2151 case CMD_grep: au_name = (char_u *)"grep"; break;
2152 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2153 default: break;
2154 }
2155 if (au_name != NULL)
2156 {
2157 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2158 curbuf->b_fname, TRUE, curbuf);
2159 if (did_throw || force_abort)
2160 return;
2161 }
2162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Bram Moolenaar86b68352004-12-27 21:59:20 +00002164 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002165 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002166 {
2167 ex_vimgrep(eap);
2168 return;
2169 }
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002172 fname = get_mef_name();
2173 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002175 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 /*
2178 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2179 */
2180 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2181 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002182 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 cmd = alloc(len);
2184 if (cmd == NULL)
2185 return;
2186 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2187 (char *)p_shq);
2188 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002189 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 /*
2191 * Output a newline if there's something else than the :make command that
2192 * was typed (in which case the cursor is in column 0).
2193 */
2194 if (msg_col == 0)
2195 msg_didout = FALSE;
2196 msg_start();
2197 MSG_PUTS(":!");
2198 msg_outtrans(cmd); /* show what we are doing */
2199
2200 /* let the shell know if we are redirecting output or not */
2201 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2202
2203#ifdef AMIGA
2204 out_flush();
2205 /* read window status report and redraw before message */
2206 (void)char_avail();
2207#endif
2208
Bram Moolenaar7c626922005-02-07 22:01:03 +00002209 if (qf_init(fname, eap->cmdidx != CMD_make ? p_gefm : p_efm,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002210 eap->cmdidx != CMD_grepadd) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 && !eap->forceit)
2212 qf_jump(0, 0, FALSE); /* display first error */
2213
Bram Moolenaar7c626922005-02-07 22:01:03 +00002214 mch_remove(fname);
2215 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002217
2218#ifdef FEAT_AUTOCMD
2219 if (au_name != NULL)
2220 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2221 curbuf->b_fname, TRUE, curbuf);
2222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223}
2224
2225/*
2226 * Return the name for the errorfile, in allocated memory.
2227 * Find a new unique name when 'makeef' contains "##".
2228 * Returns NULL for error.
2229 */
2230 static char_u *
2231get_mef_name()
2232{
2233 char_u *p;
2234 char_u *name;
2235 static int start = -1;
2236 static int off = 0;
2237#ifdef HAVE_LSTAT
2238 struct stat sb;
2239#endif
2240
2241 if (*p_mef == NUL)
2242 {
2243 name = vim_tempname('e');
2244 if (name == NULL)
2245 EMSG(_(e_notmp));
2246 return name;
2247 }
2248
2249 for (p = p_mef; *p; ++p)
2250 if (p[0] == '#' && p[1] == '#')
2251 break;
2252
2253 if (*p == NUL)
2254 return vim_strsave(p_mef);
2255
2256 /* Keep trying until the name doesn't exist yet. */
2257 for (;;)
2258 {
2259 if (start == -1)
2260 start = mch_get_pid();
2261 else
2262 off += 19;
2263
2264 name = alloc((unsigned)STRLEN(p_mef) + 30);
2265 if (name == NULL)
2266 break;
2267 STRCPY(name, p_mef);
2268 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2269 STRCAT(name, p + 2);
2270 if (mch_getperm(name) < 0
2271#ifdef HAVE_LSTAT
2272 /* Don't accept a symbolic link, its a security risk. */
2273 && mch_lstat((char *)name, &sb) < 0
2274#endif
2275 )
2276 break;
2277 vim_free(name);
2278 }
2279 return name;
2280}
2281
2282/*
2283 * ":cc", ":crewind", ":cfirst" and ":clast".
2284 */
2285 void
2286ex_cc(eap)
2287 exarg_T *eap;
2288{
2289 qf_jump(0,
2290 eap->addr_count > 0
2291 ? (int)eap->line2
2292 : eap->cmdidx == CMD_cc
2293 ? 0
2294 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2295 ? 1
2296 : 32767,
2297 eap->forceit);
2298}
2299
2300/*
2301 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2302 */
2303 void
2304ex_cnext(eap)
2305 exarg_T *eap;
2306{
2307 qf_jump(eap->cmdidx == CMD_cnext
2308 ? FORWARD
2309 : eap->cmdidx == CMD_cnfile
2310 ? FORWARD_FILE
2311 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2312 ? BACKWARD_FILE
2313 : BACKWARD,
2314 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2315}
2316
2317/*
2318 * ":cfile" command.
2319 */
2320 void
2321ex_cfile(eap)
2322 exarg_T *eap;
2323{
2324 if (*eap->arg != NUL)
2325 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
2326 if (qf_init(p_ef, p_efm, TRUE) > 0 && eap->cmdidx == CMD_cfile)
2327 qf_jump(0, 0, eap->forceit); /* display first error */
2328}
2329
2330/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002331 * ":vimgrep {pattern} file(s)"
2332 */
2333 void
2334ex_vimgrep(eap)
2335 exarg_T *eap;
2336{
Bram Moolenaar81695252004-12-29 20:58:21 +00002337 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002338 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002339 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002340 char_u *s;
2341 char_u *p;
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;
Bram Moolenaar81695252004-12-29 20:58:21 +00002345 buf_T *buf;
2346 int duplicate_name = FALSE;
2347 int using_dummy;
2348 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002349 buf_T *first_match_buf = NULL;
2350 time_t seconds = 0;
2351#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2352 char_u *save_ei = NULL;
2353 aco_save_T aco;
2354#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002355#ifdef FEAT_AUTOCMD
2356 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002357 int flags = 0;
2358 colnr_T col;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002359
2360 switch (eap->cmdidx)
2361 {
2362 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
2363 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
2364 default: break;
2365 }
2366 if (au_name != NULL)
2367 {
2368 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2369 curbuf->b_fname, TRUE, curbuf);
2370 if (did_throw || force_abort)
2371 return;
2372 }
2373#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002374
Bram Moolenaar81695252004-12-29 20:58:21 +00002375 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002376 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002377 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002378 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002379 {
Bram Moolenaar748bf032005-02-02 23:04:36 +00002380 EMSG(_("E682: Invalid search pattern or delimiter"));
2381 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002382 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002383 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002384 if (regmatch.regprog == NULL)
2385 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002386 regmatch.rmm_ic = p_ic;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002387
2388 p = skipwhite(p);
2389 if (*p == NUL)
2390 {
2391 EMSG(_("E683: File name missing or invalid pattern"));
2392 goto theend;
2393 }
2394
2395 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
2396 || qf_curlist == qf_listcount)
2397 /* make place for a new list */
2398 qf_new_list();
2399 else if (qf_lists[qf_curlist].qf_count > 0)
2400 /* Adding to existing list, find last entry. */
2401 for (prevp = qf_lists[qf_curlist].qf_start;
2402 prevp->qf_next != prevp; prevp = prevp->qf_next)
2403 ;
2404
2405 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002406 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002407 goto theend;
2408 if (fcount == 0)
2409 {
2410 EMSG(_(e_nomatch));
2411 goto theend;
2412 }
2413
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002414 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002415 for (fi = 0; fi < fcount && !got_int; ++fi)
2416 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002417 if (time(NULL) > seconds)
2418 {
2419 /* Display the file name every second or so. */
2420 seconds = time(NULL);
2421 msg_start();
2422 p = msg_strtrunc(fnames[fi]);
2423 if (p == NULL)
2424 msg_outtrans(fnames[fi]);
2425 else
2426 {
2427 msg_outtrans(p);
2428 vim_free(p);
2429 }
2430 msg_clr_eos();
2431 msg_didout = FALSE; /* overwrite this message */
2432 msg_nowait = TRUE; /* don't wait for this message */
2433 msg_col = 0;
2434 out_flush();
2435 }
2436
Bram Moolenaar81695252004-12-29 20:58:21 +00002437 buf = buflist_findname_exp(fnames[fi]);
2438 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2439 {
2440 /* Remember that a buffer with this name already exists. */
2441 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002442 using_dummy = TRUE;
2443
2444#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2445 /* Don't do Filetype autocommands to avoid loading syntax and
2446 * indent scripts, a great speed improvement. */
2447 save_ei = au_event_disable(",Filetype");
2448#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002449
2450 /* Load file into a buffer, so that 'fileencoding' is detected,
2451 * autocommands applied, etc. */
2452 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002453
2454#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2455 au_event_restore(save_ei);
2456#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002457 }
2458 else
2459 /* Use existing, loaded buffer. */
2460 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002461
Bram Moolenaar81695252004-12-29 20:58:21 +00002462 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002463 {
2464 if (!got_int)
2465 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2466 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002467 else
2468 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002469 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002470 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002471 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002472 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002473 /* For ":1vimgrep" look for multiple matches. */
2474 col = 0;
2475 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2476 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002477 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00002478 if (qf_add_entry(&prevp,
2479 NULL, /* dir */
2480 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00002481 ml_get_buf(buf,
2482 regmatch.startpos[0].lnum + lnum, FALSE),
2483 regmatch.startpos[0].lnum + lnum,
2484 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00002485 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002486 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002487 0, /* nr */
2488 0, /* type */
2489 TRUE /* valid */
2490 ) == FAIL)
2491 {
2492 got_int = TRUE;
2493 break;
2494 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002495 else
2496 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002497 if ((flags & VGR_GLOBAL) == 0
2498 || regmatch.endpos[0].lnum > 0)
2499 break;
2500 col = regmatch.endpos[0].col
2501 + (col == regmatch.endpos[0].col);
2502 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
2503 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002504 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002505 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00002506 if (got_int)
2507 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002508 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002509
2510 if (using_dummy)
2511 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002512 if (found_match && first_match_buf == NULL)
2513 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00002514 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002515 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002516 /* Never keep a dummy buffer if there is another buffer
2517 * with the same name. */
2518 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002519 buf = NULL;
2520 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002521 else if (!buf_hide(buf))
2522 {
2523 /* When not hiding the buffer and no match was found we
2524 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00002525 * there was a match and it wasn't the first one or we
2526 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002527 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002528 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002529 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002530 buf = NULL;
2531 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002532 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002533 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002534 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002535 buf = NULL;
2536 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002537 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002538
2539#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2540 if (buf != NULL)
2541 {
2542 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaar748bf032005-02-02 23:04:36 +00002543 * need to be done now, in that buffer. And then the
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002544 * modelines need to be done (again). */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002545 aucmd_prepbuf(&aco, buf);
2546 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
2547 buf->b_fname, TRUE, buf);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002548 do_modelines(FALSE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002549 aucmd_restbuf(&aco);
2550 }
2551#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002552 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002553 }
2554 }
2555
2556 FreeWild(fcount, fnames);
2557
2558 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2559 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2560 qf_lists[qf_curlist].qf_index = 1;
2561
2562#ifdef FEAT_WINDOWS
2563 qf_update_buffer();
2564#endif
2565
2566 /* Jump to first match. */
2567 if (qf_lists[qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002568 {
2569 if ((flags & VGR_NOJUMP) == 0)
2570 qf_jump(0, 0, eap->forceit);
2571 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002572 else
2573 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002574
Bram Moolenaar7c626922005-02-07 22:01:03 +00002575#ifdef FEAT_AUTOCMD
2576 if (au_name != NULL)
2577 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2578 curbuf->b_fname, TRUE, curbuf);
2579#endif
2580
Bram Moolenaar86b68352004-12-27 21:59:20 +00002581theend:
2582 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002583}
2584
2585/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00002586 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00002587 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00002588 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
2589 * If "s" is not NULL terminate the pattern with a NUL.
2590 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00002591 */
2592 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00002593skip_vimgrep_pat(p, s, flags)
2594 char_u *p;
2595 char_u **s;
2596 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002597{
2598 int c;
2599
2600 if (vim_isIDc(*p))
2601 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002602 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00002603 if (s != NULL)
2604 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002605 p = skiptowhite(p);
2606 if (s != NULL && *p != NUL)
2607 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002608 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002609 else
2610 {
2611 /* ":vimgrep /pattern/[g][j] fname" */
2612 if (s != NULL)
2613 *s = p + 1;
2614 c = *p;
2615 p = skip_regexp(p + 1, c, TRUE, NULL);
2616 if (*p != c)
2617 return NULL;
2618
2619 /* Truncate the pattern. */
2620 if (s != NULL)
2621 *p = NUL;
2622 ++p;
2623
2624 /* Find the flags */
2625 while (*p == 'g' || *p == 'j')
2626 {
2627 if (flags != NULL)
2628 {
2629 if (*p == 'g')
2630 *flags |= VGR_GLOBAL;
2631 else
2632 *flags |= VGR_NOJUMP;
2633 }
2634 ++p;
2635 }
2636 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00002637 return p;
2638}
2639
2640/*
Bram Moolenaar81695252004-12-29 20:58:21 +00002641 * Load file "fname" into a dummy buffer and return the buffer pointer.
2642 * Returns NULL if it fails.
2643 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
2644 */
2645 static buf_T *
2646load_dummy_buffer(fname)
2647 char_u *fname;
2648{
2649 buf_T *newbuf;
2650 int failed = TRUE;
2651#ifdef FEAT_AUTOCMD
2652 aco_save_T aco;
2653#else
2654 buf_T *old_curbuf = curbuf;
2655#endif
2656
2657 /* Allocate a buffer without putting it in the buffer list. */
2658 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
2659 if (newbuf == NULL)
2660 return NULL;
2661
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002662 /* Init the options. */
2663 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
2664
Bram Moolenaar81695252004-12-29 20:58:21 +00002665#ifdef FEAT_AUTOCMD
2666 /* set curwin/curbuf to buf and save a few things */
2667 aucmd_prepbuf(&aco, newbuf);
2668#else
2669 curbuf = newbuf;
2670 curwin->w_buffer = newbuf;
2671#endif
2672
2673 /* Need to set the filename for autocommands. */
2674 (void)setfname(curbuf, fname, NULL, FALSE);
2675
2676 if (ml_open() == OK)
2677 {
2678 /* Create swap file now to avoid the ATTENTION message. */
2679 check_need_swap(TRUE);
2680
2681 /* Remove the "dummy" flag, otherwise autocommands may not
2682 * work. */
2683 curbuf->b_flags &= ~BF_DUMMY;
2684
2685 if (readfile(fname, NULL,
2686 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
2687 NULL, READ_NEW | READ_DUMMY) == OK
2688 && !(curbuf->b_flags & BF_NEW))
2689 {
2690 failed = FALSE;
2691 if (curbuf != newbuf)
2692 {
2693 /* Bloody autocommands changed the buffer! */
2694 if (buf_valid(newbuf))
2695 wipe_buffer(newbuf, FALSE);
2696 newbuf = curbuf;
2697 }
2698 }
2699 }
2700
2701#ifdef FEAT_AUTOCMD
2702 /* restore curwin/curbuf and a few other things */
2703 aucmd_restbuf(&aco);
2704#else
2705 curbuf = old_curbuf;
2706 curwin->w_buffer = old_curbuf;
2707#endif
2708
2709 if (!buf_valid(newbuf))
2710 return NULL;
2711 if (failed)
2712 {
2713 wipe_dummy_buffer(newbuf);
2714 return NULL;
2715 }
2716 return newbuf;
2717}
2718
2719/*
2720 * Wipe out the dummy buffer that load_dummy_buffer() created.
2721 */
2722 static void
2723wipe_dummy_buffer(buf)
2724 buf_T *buf;
2725{
2726 if (curbuf != buf) /* safety check */
2727 wipe_buffer(buf, FALSE);
2728}
2729
2730/*
2731 * Unload the dummy buffer that load_dummy_buffer() created.
2732 */
2733 static void
2734unload_dummy_buffer(buf)
2735 buf_T *buf;
2736{
2737 if (curbuf != buf) /* safety check */
2738 close_buffer(NULL, buf, DOBUF_UNLOAD);
2739}
2740
Bram Moolenaar05159a02005-02-26 23:04:13 +00002741#if defined(FEAT_EVAL) || defined(PROTO)
2742/*
2743 * Add each quickfix error to list "list" as a dictionary.
2744 */
2745 int
2746get_errorlist(list)
2747 list_T *list;
2748{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002749 dict_T *dict;
2750 char_u buf[2];
2751 qfline_T *qfp;
2752 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002753
2754 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
2755 {
2756 EMSG(_(e_quickfix));
2757 return FAIL;
2758 }
2759
2760 qfp = qf_lists[qf_curlist].qf_start;
2761 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; ++i)
2762 {
2763 if ((dict = dict_alloc()) == NULL)
2764 return FAIL;
2765 if (list_append_dict(list, dict) == FAIL)
2766 return FAIL;
2767
2768 buf[0] = qfp->qf_type;
2769 buf[1] = NUL;
2770 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
2771 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
2772 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
2773 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
2774 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002775 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002776 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
2777 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
2778 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
2779 return FAIL;
2780
2781 qfp = qfp->qf_next;
2782 }
2783 return OK;
2784}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002785
2786/*
2787 * Populate the quickfix list with the items supplied in the list
2788 * of dictionaries.
2789 */
2790 int
2791set_errorlist(list)
2792 list_T *list;
2793{
2794 listitem_T *li;
2795 dict_T *d;
2796 char_u *filename, *pattern, *text, *type;
2797 long lnum;
2798 int col, nr;
2799 int vcol;
2800 qfline_T *prevp = NULL;
2801 int valid, status;
2802 int retval = OK;
2803
2804 /* make place for a new list */
2805 qf_new_list();
2806
2807 for (li = list->lv_first; li != NULL; li = li->li_next)
2808 {
2809 if (li->li_tv.v_type != VAR_DICT)
2810 continue; /* Skip non-dict items */
2811
2812 d = li->li_tv.vval.v_dict;
2813 if (d == NULL)
2814 continue;
2815
2816 filename = get_dict_string(d, (char_u *)"filename");
2817 lnum = get_dict_number(d, (char_u *)"lnum");
2818 col = get_dict_number(d, (char_u *)"col");
2819 vcol = get_dict_number(d, (char_u *)"vcol");
2820 nr = get_dict_number(d, (char_u *)"nr");
2821 type = get_dict_string(d, (char_u *)"type");
2822 pattern = get_dict_string(d, (char_u *)"pattern");
2823 text = get_dict_string(d, (char_u *)"text");
2824 if (text == NULL)
2825 text = vim_strsave((char_u *)"");
2826
2827 valid = TRUE;
2828 if (filename == NULL || (lnum == 0 && pattern == NULL))
2829 valid = FALSE;
2830
2831 status = qf_add_entry(&prevp,
2832 NULL, /* dir */
2833 filename,
2834 text,
2835 lnum,
2836 col,
2837 vcol, /* vis_col */
2838 pattern, /* search pattern */
2839 nr,
2840 type == NULL ? NUL : *type,
2841 valid);
2842
2843 vim_free(filename);
2844 vim_free(pattern);
2845 vim_free(text);
2846 vim_free(type);
2847
2848 if (status == FAIL)
2849 {
2850 retval = FAIL;
2851 break;
2852 }
2853 }
2854
2855 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2856 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2857 qf_lists[qf_curlist].qf_index = 1;
2858
2859#ifdef FEAT_WINDOWS
2860 qf_update_buffer();
2861#endif
2862
2863 return retval;
2864}
Bram Moolenaar05159a02005-02-26 23:04:13 +00002865#endif
2866
Bram Moolenaar81695252004-12-29 20:58:21 +00002867/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002868 * ":[range]cbuffer [bufnr]" command.
2869 */
2870 void
2871ex_cbuffer(eap)
2872 exarg_T *eap;
2873{
2874 buf_T *buf = NULL;
2875
2876 if (*eap->arg == NUL)
2877 buf = curbuf;
2878 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
2879 buf = buflist_findnr(atoi((char *)eap->arg));
2880 if (buf == NULL)
2881 EMSG(_(e_invarg));
2882 else if (buf->b_ml.ml_mfp == NULL)
2883 EMSG(_("E681: Buffer is not loaded"));
2884 else
2885 {
2886 if (eap->addr_count == 0)
2887 {
2888 eap->line1 = 1;
2889 eap->line2 = buf->b_ml.ml_line_count;
2890 }
2891 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
2892 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
2893 EMSG(_(e_invrange));
2894 else
2895 qf_init_ext(NULL, buf, p_efm, TRUE, eap->line1, eap->line2);
2896 }
2897}
2898
2899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 * ":helpgrep {pattern}"
2901 */
2902 void
2903ex_helpgrep(eap)
2904 exarg_T *eap;
2905{
2906 regmatch_T regmatch;
2907 char_u *save_cpo;
2908 char_u *p;
2909 int fcount;
2910 char_u **fnames;
2911 FILE *fd;
2912 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002913 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002915#ifdef FEAT_MULTI_LANG
2916 char_u *lang;
2917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
2919 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2920 save_cpo = p_cpo;
2921 p_cpo = (char_u *)"";
2922
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002923#ifdef FEAT_MULTI_LANG
2924 /* Check for a specified language */
2925 lang = check_help_lang(eap->arg);
2926#endif
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
2929 regmatch.rm_ic = FALSE;
2930 if (regmatch.regprog != NULL)
2931 {
2932 /* create a new quickfix list */
2933 qf_new_list();
2934
2935 /* Go through all directories in 'runtimepath' */
2936 p = p_rtp;
2937 while (*p != NUL && !got_int)
2938 {
2939 copy_option_part(&p, NameBuff, MAXPATHL, ",");
2940
2941 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
2942 add_pathsep(NameBuff);
2943 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
2944 if (gen_expand_wildcards(1, &NameBuff, &fcount,
2945 &fnames, EW_FILE|EW_SILENT) == OK
2946 && fcount > 0)
2947 {
2948 for (fi = 0; fi < fcount && !got_int; ++fi)
2949 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002950#ifdef FEAT_MULTI_LANG
2951 /* Skip files for a different language. */
2952 if (lang != NULL
2953 && STRNICMP(lang, fnames[fi]
2954 + STRLEN(fnames[fi]) - 3, 2) != 0
2955 && !(STRNICMP(lang, "en", 2) == 0
2956 && STRNICMP("txt", fnames[fi]
2957 + STRLEN(fnames[fi]) - 3, 3) == 0))
2958 continue;
2959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 fd = fopen((char *)fnames[fi], "r");
2961 if (fd != NULL)
2962 {
2963 lnum = 1;
2964 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
2965 {
2966 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
2967 {
2968 int l = STRLEN(IObuff);
2969
2970 /* remove trailing CR, LF, spaces, etc. */
2971 while (l > 0 && IObuff[l - 1] <= ' ')
2972 IObuff[--l] = NUL;
2973
2974 if (qf_add_entry(&prevp,
2975 NULL, /* dir */
2976 fnames[fi],
2977 IObuff,
2978 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00002979 (int)(regmatch.startp[0] - IObuff)
2980 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00002981 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002982 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 0, /* nr */
2984 1, /* type */
2985 TRUE /* valid */
2986 ) == FAIL)
2987 {
2988 got_int = TRUE;
2989 break;
2990 }
2991 }
2992 ++lnum;
2993 line_breakcheck();
2994 }
2995 fclose(fd);
2996 }
2997 }
2998 FreeWild(fcount, fnames);
2999 }
3000 }
3001 vim_free(regmatch.regprog);
3002
3003 qf_lists[qf_curlist].qf_nonevalid = FALSE;
3004 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
3005 qf_lists[qf_curlist].qf_index = 1;
3006 }
3007
3008 p_cpo = save_cpo;
3009
3010#ifdef FEAT_WINDOWS
3011 qf_update_buffer();
3012#endif
3013
3014 /* Jump to first match. */
3015 if (qf_lists[qf_curlist].qf_count > 0)
3016 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003017 else
3018 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020
3021#endif /* FEAT_QUICKFIX */