xref: /vim-8.2.3635/src/quickfix.c (revision dfccaf0f)
1 /* 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 
18 struct dir_stack_T
19 {
20     struct dir_stack_T	*next;
21     char_u		*dirname;
22 };
23 
24 static struct dir_stack_T   *dir_stack = NULL;
25 
26 /*
27  * for each error the next struct is allocated and linked in a list
28  */
29 struct qf_line
30 {
31     struct qf_line  *qf_next;	/* pointer to next error in the list */
32     struct qf_line  *qf_prev;	/* pointer to previous error in the list */
33     linenr_T	     qf_lnum;	/* line number where the error occurred */
34     int		     qf_fnum;	/* file number for the line */
35     int		     qf_col;	/* column where the error occurred */
36     int		     qf_nr;	/* error number */
37     char_u	    *qf_text;	/* description of the error */
38     char_u	     qf_virt_col; /* set to TRUE if qf_col is screen column */
39     char_u	     qf_cleared;/* set to TRUE if line has been deleted */
40     char_u	     qf_type;	/* type of the error (mostly 'E'); 1 for
41 				   :helpgrep */
42     char_u	     qf_valid;	/* valid error message detected */
43 };
44 
45 /*
46  * There is a stack of error lists.
47  */
48 #define LISTCOUNT   10
49 
50 struct qf_list
51 {
52     struct qf_line *qf_start;	/* pointer to the first error */
53     struct qf_line *qf_ptr;	/* pointer to the current error */
54     int  qf_count;		/* number of errors (0 means no error list) */
55     int  qf_index;		/* current index in the error list */
56     int  qf_nonevalid;		/* TRUE if not a single valid entry found */
57 } qf_lists[LISTCOUNT];
58 
59 static int	qf_curlist = 0;	/* current error list */
60 static int	qf_listcount = 0;   /* current number of lists */
61 
62 #define FMT_PATTERNS 9		/* maximum number of % recognized */
63 
64 /*
65  * Structure used to hold the info of one part of 'errorformat'
66  */
67 struct eformat
68 {
69     regprog_T	    *prog;	/* pre-formatted part of 'errorformat' */
70     struct eformat  *next;	/* pointer to next (NULL if last) */
71     char_u	    addr[FMT_PATTERNS]; /* indices of used % patterns */
72     char_u	    prefix;	/* prefix of this format line: */
73 				/*   'D' enter directory */
74 				/*   'X' leave directory */
75 				/*   'A' start of multi-line message */
76 				/*   'E' error message */
77 				/*   'W' warning message */
78 				/*   'I' informational message */
79 				/*   'C' continuation line */
80 				/*   'Z' end of multi-line message */
81 				/*   'G' general, unspecific message */
82 				/*   'P' push file (partial) message */
83 				/*   'Q' pop/quit file (partial) message */
84 				/*   'O' overread (partial) message */
85     char_u	    flags;	/* additional flags given in prefix */
86 				/*   '-' do not include this line */
87 };
88 
89 static int qf_init_ext __ARGS((char_u *efile, buf_T *buf, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast));
90 static void	qf_new_list __ARGS((void));
91 static int	qf_add_entry __ARGS((struct qf_line **prevp, char_u *dir, char_u *fname, char_u *mesg, long lnum, int col, int virt_col, int nr, int type, int valid));
92 static void	qf_msg __ARGS((void));
93 static void	qf_free __ARGS((int idx));
94 static char_u	*qf_types __ARGS((int, int));
95 static int	qf_get_fnum __ARGS((char_u *, char_u *));
96 static char_u	*qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
97 static char_u	*qf_pop_dir __ARGS((struct dir_stack_T **));
98 static char_u	*qf_guess_filepath __ARGS((char_u *));
99 static void	qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
100 static void	qf_clean_dir_stack __ARGS((struct dir_stack_T **));
101 #ifdef FEAT_WINDOWS
102 static int	qf_win_pos_update __ARGS((int old_qf_index));
103 static buf_T	*qf_find_buf __ARGS((void));
104 static void	qf_update_buffer __ARGS((void));
105 static void	qf_fill_buffer __ARGS((void));
106 #endif
107 static char_u	*get_mef_name __ARGS((void));
108 static buf_T	*load_dummy_buffer __ARGS((char_u *fname));
109 static void	wipe_dummy_buffer __ARGS((buf_T *buf));
110 static void	unload_dummy_buffer __ARGS((buf_T *buf));
111 
112 /*
113  * Read the errorfile "efile" into memory, line by line, building the error
114  * list.
115  * Return -1 for error, number of errors for success.
116  */
117     int
118 qf_init(efile, errorformat, newlist)
119     char_u	    *efile;
120     char_u	    *errorformat;
121     int		    newlist;		/* TRUE: start a new error list */
122 {
123     if (efile == NULL)
124 	return FAIL;
125     return qf_init_ext(efile, curbuf, errorformat, newlist,
126 						    (linenr_T)0, (linenr_T)0);
127 }
128 
129 /*
130  * Read the errorfile "efile" into memory, line by line, building the error
131  * list.
132  * Alternative: when "efile" is null read errors from buffer "buf".
133  * Always use 'errorformat' from "buf" if there is a local value.
134  * Then lnumfirst and lnumlast specify the range of lines to use.
135  * Return -1 for error, number of errors for success.
136  */
137     static int
138 qf_init_ext(efile, buf, errorformat, newlist, lnumfirst, lnumlast)
139     char_u	    *efile;
140     buf_T	    *buf;
141     char_u	    *errorformat;
142     int		    newlist;		/* TRUE: start a new error list */
143     linenr_T	    lnumfirst;		/* first line number to use */
144     linenr_T	    lnumlast;		/* last line number to use */
145 {
146     char_u	    *namebuf;
147     char_u	    *errmsg;
148     char_u	    *fmtstr = NULL;
149     int		    col = 0;
150     char_u	    use_virt_col = FALSE;
151     int		    type = 0;
152     int		    valid;
153     linenr_T	    buflnum = lnumfirst;
154     long	    lnum = 0L;
155     int		    enr = 0;
156     FILE	    *fd = NULL;
157     struct qf_line  *qfprev = NULL;	/* init to make SASC shut up */
158     char_u	    *efmp;
159     struct eformat  *fmt_first = NULL;
160     struct eformat  *fmt_last = NULL;
161     struct eformat  *fmt_ptr;
162     char_u	    *efm;
163     char_u	    *ptr;
164     char_u	    *srcptr;
165     int		    len;
166     int		    i;
167     int		    round;
168     int		    idx = 0;
169     int		    multiline = FALSE;
170     int		    multiignore = FALSE;
171     int		    multiscan = FALSE;
172     int		    retval = -1;	/* default: return error flag */
173     char_u	    *directory = NULL;
174     char_u	    *currfile = NULL;
175     char_u	    *tail = NULL;
176     struct dir_stack_T  *file_stack = NULL;
177     regmatch_T	    regmatch;
178     static struct fmtpattern
179     {
180 	char_u	convchar;
181 	char	*pattern;
182     }		    fmt_pat[FMT_PATTERNS] =
183 		    {
184 			{'f', "\\f\\+"},
185 			{'n', "\\d\\+"},
186 			{'l', "\\d\\+"},
187 			{'c', "\\d\\+"},
188 			{'t', "."},
189 			{'m', ".\\+"},
190 			{'r', ".*"},
191 			{'p', "[- .]*"},
192 			{'v', "\\d\\+"}
193 		    };
194 
195     namebuf = alloc(CMDBUFFSIZE + 1);
196     errmsg = alloc(CMDBUFFSIZE + 1);
197     if (namebuf == NULL || errmsg == NULL)
198 	goto qf_init_end;
199 
200     if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
201     {
202 	EMSG2(_(e_openerrf), efile);
203 	goto qf_init_end;
204     }
205 
206     if (newlist || qf_curlist == qf_listcount)
207 	/* make place for a new list */
208 	qf_new_list();
209     else if (qf_lists[qf_curlist].qf_count > 0)
210 	/* Adding to existing list, find last entry. */
211 	for (qfprev = qf_lists[qf_curlist].qf_start;
212 			    qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
213 	    ;
214 
215 /*
216  * Each part of the format string is copied and modified from errorformat to
217  * regex prog.  Only a few % characters are allowed.
218  */
219     /* Use the local value of 'errorformat' if it's set. */
220     if (errorformat == p_efm && *buf->b_p_efm != NUL)
221 	efm = buf->b_p_efm;
222     else
223 	efm = errorformat;
224     /*
225      * Get some space to modify the format string into.
226      */
227     i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
228     for (round = FMT_PATTERNS; round > 0; )
229 	i += (int)STRLEN(fmt_pat[--round].pattern);
230 #ifdef COLON_IN_FILENAME
231     i += 12; /* "%f" can become twelve chars longer */
232 #else
233     i += 2; /* "%f" can become two chars longer */
234 #endif
235     if ((fmtstr = alloc(i)) == NULL)
236 	goto error2;
237 
238     while (efm[0])
239     {
240 	/*
241 	 * Allocate a new eformat structure and put it at the end of the list
242 	 */
243 	fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
244 	if (fmt_ptr == NULL)
245 	    goto error2;
246 	if (fmt_first == NULL)	    /* first one */
247 	    fmt_first = fmt_ptr;
248 	else
249 	    fmt_last->next = fmt_ptr;
250 	fmt_last = fmt_ptr;
251 	fmt_ptr->prefix = NUL;
252 	fmt_ptr->flags = NUL;
253 	fmt_ptr->next = NULL;
254 	fmt_ptr->prog = NULL;
255 	for (round = FMT_PATTERNS; round > 0; )
256 	    fmt_ptr->addr[--round] = NUL;
257 	/* round is 0 now */
258 
259 	/*
260 	 * Isolate one part in the 'errorformat' option
261 	 */
262 	for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
263 	    if (efm[len] == '\\' && efm[len + 1] != NUL)
264 		++len;
265 
266 	/*
267 	 * Build regexp pattern from current 'errorformat' option
268 	 */
269 	ptr = fmtstr;
270 	*ptr++ = '^';
271 	for (efmp = efm; efmp < efm + len; ++efmp)
272 	{
273 	    if (*efmp == '%')
274 	    {
275 		++efmp;
276 		for (idx = 0; idx < FMT_PATTERNS; ++idx)
277 		    if (fmt_pat[idx].convchar == *efmp)
278 			break;
279 		if (idx < FMT_PATTERNS)
280 		{
281 		    if (fmt_ptr->addr[idx])
282 		    {
283 			sprintf((char *)errmsg,
284 				_("E372: Too many %%%c in format string"), *efmp);
285 			EMSG(errmsg);
286 			goto error2;
287 		    }
288 		    if ((idx
289 				&& idx < 6
290 				&& vim_strchr((char_u *)"DXOPQ",
291 						     fmt_ptr->prefix) != NULL)
292 			    || (idx == 6
293 				&& vim_strchr((char_u *)"OPQ",
294 						    fmt_ptr->prefix) == NULL))
295 		    {
296 			sprintf((char *)errmsg,
297 				_("E373: Unexpected %%%c in format string"), *efmp);
298 			EMSG(errmsg);
299 			goto error2;
300 		    }
301 		    fmt_ptr->addr[idx] = (char_u)++round;
302 		    *ptr++ = '\\';
303 		    *ptr++ = '(';
304 #ifdef BACKSLASH_IN_FILENAME
305 		    if (*efmp == 'f')
306 		    {
307 			/* Also match "c:" in the file name, even when
308 			 * checking for a colon next: "%f:".
309 			 * "\%(\a:\)\=" */
310 			STRCPY(ptr, "\\%(\\a:\\)\\=");
311 			ptr += 10;
312 		    }
313 #endif
314 		    if (*efmp == 'f' && efmp[1] != NUL
315 					 && efmp[1] != '\\' && efmp[1] != '%')
316 		    {
317 			/* A file name may contain spaces, but this isn't in
318 			 * "\f".  use "[^x]\+" instead (x is next character) */
319 			*ptr++ = '[';
320 			*ptr++ = '^';
321 			*ptr++ = efmp[1];
322 			*ptr++ = ']';
323 			*ptr++ = '\\';
324 			*ptr++ = '+';
325 		    }
326 		    else
327 		    {
328 			srcptr = (char_u *)fmt_pat[idx].pattern;
329 			while ((*ptr = *srcptr++) != NUL)
330 			    ++ptr;
331 		    }
332 		    *ptr++ = '\\';
333 		    *ptr++ = ')';
334 		}
335 		else if (*efmp == '*')
336 		{
337 		    if (*++efmp == '[' || *efmp == '\\')
338 		    {
339 			if ((*ptr++ = *efmp) == '[')	/* %*[^a-z0-9] etc. */
340 			{
341 			    if (efmp[1] == '^')
342 				*ptr++ = *++efmp;
343 			    if (efmp < efm + len)
344 			    {
345 				*ptr++ = *++efmp;	    /* could be ']' */
346 				while (efmp < efm + len
347 					&& (*ptr++ = *++efmp) != ']')
348 				    /* skip */;
349 				if (efmp == efm + len)
350 				{
351 				    EMSG(_("E374: Missing ] in format string"));
352 				    goto error2;
353 				}
354 			    }
355 			}
356 			else if (efmp < efm + len)	/* %*\D, %*\s etc. */
357 			    *ptr++ = *++efmp;
358 			*ptr++ = '\\';
359 			*ptr++ = '+';
360 		    }
361 		    else
362 		    {
363 			/* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
364 			sprintf((char *)errmsg,
365 				_("E375: Unsupported %%%c in format string"), *efmp);
366 			EMSG(errmsg);
367 			goto error2;
368 		    }
369 		}
370 		else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
371 		    *ptr++ = *efmp;		/* regexp magic characters */
372 		else if (*efmp == '#')
373 		    *ptr++ = '*';
374 		else if (efmp == efm + 1)		/* analyse prefix */
375 		{
376 		    if (vim_strchr((char_u *)"+-", *efmp) != NULL)
377 			fmt_ptr->flags = *efmp++;
378 		    if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
379 			fmt_ptr->prefix = *efmp;
380 		    else
381 		    {
382 			sprintf((char *)errmsg,
383 				_("E376: Invalid %%%c in format string prefix"), *efmp);
384 			EMSG(errmsg);
385 			goto error2;
386 		    }
387 		}
388 		else
389 		{
390 		    sprintf((char *)errmsg,
391 			    _("E377: Invalid %%%c in format string"), *efmp);
392 		    EMSG(errmsg);
393 		    goto error2;
394 		}
395 	    }
396 	    else			/* copy normal character */
397 	    {
398 		if (*efmp == '\\' && efmp + 1 < efm + len)
399 		    ++efmp;
400 		else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
401 		    *ptr++ = '\\';	/* escape regexp atoms */
402 		if (*efmp)
403 		    *ptr++ = *efmp;
404 	    }
405 	}
406 	*ptr++ = '$';
407 	*ptr = NUL;
408 	if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
409 	    goto error2;
410 	/*
411 	 * Advance to next part
412 	 */
413 	efm = skip_to_option_part(efm + len);	/* skip comma and spaces */
414     }
415     if (fmt_first == NULL)	/* nothing found */
416     {
417 	EMSG(_("E378: 'errorformat' contains no pattern"));
418 	goto error2;
419     }
420 
421     /*
422      * got_int is reset here, because it was probably set when killing the
423      * ":make" command, but we still want to read the errorfile then.
424      */
425     got_int = FALSE;
426 
427     /* Always ignore case when looking for a matching error. */
428     regmatch.rm_ic = TRUE;
429 
430     /*
431      * Read the lines in the error file one by one.
432      * Try to recognize one of the error formats in each line.
433      */
434     while (!got_int)
435     {
436 	/* Get the next line. */
437 	if (fd == NULL)
438 	{
439 	    if (buflnum > lnumlast)
440 		break;
441 	    STRNCPY(IObuff, ml_get_buf(buf, buflnum++, FALSE), CMDBUFFSIZE - 2);
442 	}
443 	else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
444 	    break;
445 
446 	IObuff[CMDBUFFSIZE - 2] = NUL;  /* for very long lines */
447 	if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
448 	    *efmp = NUL;
449 #ifdef USE_CRNL
450 	if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
451 	    *efmp = NUL;
452 #endif
453 
454 	/*
455 	 * Try to match each part of 'errorformat' until we find a complete
456 	 * match or no match.
457 	 */
458 	valid = TRUE;
459 restofline:
460 	for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
461 	{
462 	    idx = fmt_ptr->prefix;
463 	    if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
464 		continue;
465 	    namebuf[0] = NUL;
466 	    if (!multiscan)
467 		errmsg[0] = NUL;
468 	    lnum = 0;
469 	    col = 0;
470 	    use_virt_col = FALSE;
471 	    enr = -1;
472 	    type = 0;
473 	    tail = NULL;
474 
475 	    regmatch.regprog = fmt_ptr->prog;
476 	    if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
477 	    {
478 		if ((idx == 'C' || idx == 'Z') && !multiline)
479 		    continue;
480 		if (vim_strchr((char_u *)"EWI", idx) != NULL)
481 		    type = idx;
482 		else
483 		    type = 0;
484 		/*
485 		 * Extract error message data from matched line
486 		 */
487 		if ((i = (int)fmt_ptr->addr[0]) > 0)		/* %f */
488 		{
489 		    len = (int)(regmatch.endp[i] - regmatch.startp[i]);
490 		    STRNCPY(namebuf, regmatch.startp[i], len);
491 		    namebuf[len] = NUL;
492 		    if (vim_strchr((char_u *)"OPQ", idx) != NULL
493 			    && mch_getperm(namebuf) == -1)
494 			continue;
495 		}
496 		if ((i = (int)fmt_ptr->addr[1]) > 0)		/* %n */
497 		    enr = (int)atol((char *)regmatch.startp[i]);
498 		if ((i = (int)fmt_ptr->addr[2]) > 0)		/* %l */
499 		    lnum = atol((char *)regmatch.startp[i]);
500 		if ((i = (int)fmt_ptr->addr[3]) > 0)		/* %c */
501 		    col = (int)atol((char *)regmatch.startp[i]);
502 		if ((i = (int)fmt_ptr->addr[4]) > 0)		/* %t */
503 		    type = *regmatch.startp[i];
504 		if (fmt_ptr->flags ==  '+' && !multiscan)	/* %+ */
505 		    STRCPY(errmsg, IObuff);
506 		else if ((i = (int)fmt_ptr->addr[5]) > 0)	/* %m */
507 		{
508 		    len = (int)(regmatch.endp[i] - regmatch.startp[i]);
509 		    STRNCPY(errmsg, regmatch.startp[i], len);
510 		    errmsg[len] = NUL;
511 		}
512 		if ((i = (int)fmt_ptr->addr[6]) > 0)		/* %r */
513 		    tail = regmatch.startp[i];
514 		if ((i = (int)fmt_ptr->addr[7]) > 0)		/* %p */
515 		{
516 		    col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
517 		    if (*((char_u *)regmatch.startp[i]) != TAB)
518 			use_virt_col = TRUE;
519 		}
520 		if ((i = (int)fmt_ptr->addr[8]) > 0)		/* %v */
521 		{
522 		    col = (int)atol((char *)regmatch.startp[i]);
523 		    use_virt_col = TRUE;
524 		}
525 		break;
526 	    }
527 	}
528 	multiscan = FALSE;
529 	if (!fmt_ptr || idx == 'D' || idx == 'X')
530 	{
531 	    if (fmt_ptr)
532 	    {
533 		if (idx == 'D')				/* enter directory */
534 		{
535 		    if (*namebuf == NUL)
536 		    {
537 			EMSG(_("E379: Missing or empty directory name"));
538 			goto error2;
539 		    }
540 		    if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
541 			goto error2;
542 		}
543 		else if (idx == 'X')			/* leave directory */
544 		    directory = qf_pop_dir(&dir_stack);
545 	    }
546 	    namebuf[0] = NUL;		/* no match found, remove file name */
547 	    lnum = 0;			/* don't jump to this line */
548 	    valid = FALSE;
549 	    STRCPY(errmsg, IObuff);	/* copy whole line to error message */
550 	    if (!fmt_ptr)
551 		multiline = multiignore = FALSE;
552 	}
553 	else if (fmt_ptr)
554 	{
555 	    if (vim_strchr((char_u *)"AEWI", idx) != NULL)
556 		multiline = TRUE;	/* start of a multi-line message */
557 	    else if (vim_strchr((char_u *)"CZ", idx) != NULL)
558 	    {				/* continuation of multi-line msg */
559 		if (qfprev == NULL)
560 		    goto error2;
561 		if (*errmsg && !multiignore)
562 		{
563 		    len = (int)STRLEN(qfprev->qf_text);
564 		    if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
565 								    == NULL)
566 			goto error2;
567 		    STRCPY(ptr, qfprev->qf_text);
568 		    vim_free(qfprev->qf_text);
569 		    qfprev->qf_text = ptr;
570 		    *(ptr += len) = '\n';
571 		    STRCPY(++ptr, errmsg);
572 		}
573 		if (qfprev->qf_nr == -1)
574 		    qfprev->qf_nr = enr;
575 		if (vim_isprintc(type) && !qfprev->qf_type)
576 		    qfprev->qf_type = type;  /* only printable chars allowed */
577 		if (!qfprev->qf_lnum)
578 		    qfprev->qf_lnum = lnum;
579 		if (!qfprev->qf_col)
580 		    qfprev->qf_col = col;
581 		qfprev->qf_virt_col = use_virt_col;
582 		if (!qfprev->qf_fnum)
583 		    qfprev->qf_fnum = qf_get_fnum(directory,
584 					*namebuf || directory ? namebuf
585 					  : currfile && valid ? currfile : 0);
586 		if (idx == 'Z')
587 		    multiline = multiignore = FALSE;
588 		line_breakcheck();
589 		continue;
590 	    }
591 	    else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
592 	    {
593 		/* global file names */
594 		valid = FALSE;
595 		if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
596 		{
597 		    if (*namebuf && idx == 'P')
598 			currfile = qf_push_dir(namebuf, &file_stack);
599 		    else if (idx == 'Q')
600 			currfile = qf_pop_dir(&file_stack);
601 		    *namebuf = NUL;
602 		    if (tail && *tail)
603 		    {
604 			STRCPY(IObuff, skipwhite(tail));
605 			multiscan = TRUE;
606 			goto restofline;
607 		    }
608 		}
609 	    }
610 	    if (fmt_ptr->flags == '-')	/* generally exclude this line */
611 	    {
612 		if (multiline)
613 		    multiignore = TRUE;	/* also exclude continuation lines */
614 		continue;
615 	    }
616 	}
617 
618 	if (qf_add_entry(&qfprev,
619 			directory,
620 			*namebuf || directory
621 			    ? namebuf
622 			    : currfile && valid ? currfile : NULL,
623 			errmsg,
624 			lnum,
625 			col,
626 			use_virt_col,
627 			enr,
628 			type,
629 			valid) == FAIL)
630 	    goto error2;
631 	line_breakcheck();
632     }
633     if (fd == NULL || !ferror(fd))
634     {
635 	if (qf_lists[qf_curlist].qf_index == 0)	/* no valid entry found */
636 	{
637 	    qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
638 	    qf_lists[qf_curlist].qf_index = 1;
639 	    qf_lists[qf_curlist].qf_nonevalid = TRUE;
640 	}
641 	else
642 	{
643 	    qf_lists[qf_curlist].qf_nonevalid = FALSE;
644 	    if (qf_lists[qf_curlist].qf_ptr == NULL)
645 		qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
646 	}
647 	retval = qf_lists[qf_curlist].qf_count;	/* return number of matches */
648 	goto qf_init_ok;
649     }
650     EMSG(_(e_readerrf));
651 error2:
652     qf_free(qf_curlist);
653     qf_listcount--;
654     if (qf_curlist > 0)
655 	--qf_curlist;
656 qf_init_ok:
657     if (fd != NULL)
658 	fclose(fd);
659     for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
660     {
661 	fmt_first = fmt_ptr->next;
662 	vim_free(fmt_ptr->prog);
663 	vim_free(fmt_ptr);
664     }
665     qf_clean_dir_stack(&dir_stack);
666     qf_clean_dir_stack(&file_stack);
667 qf_init_end:
668     vim_free(namebuf);
669     vim_free(errmsg);
670     vim_free(fmtstr);
671 
672 #ifdef FEAT_WINDOWS
673     qf_update_buffer();
674 #endif
675 
676     return retval;
677 }
678 
679 /*
680  * Prepare for adding a new quickfix list.
681  */
682     static void
683 qf_new_list()
684 {
685     int		i;
686 
687     /*
688      * If the current entry is not the last entry, delete entries below
689      * the current entry.  This makes it possible to browse in a tree-like
690      * way with ":grep'.
691      */
692     while (qf_listcount > qf_curlist + 1)
693 	qf_free(--qf_listcount);
694 
695     /*
696      * When the stack is full, remove to oldest entry
697      * Otherwise, add a new entry.
698      */
699     if (qf_listcount == LISTCOUNT)
700     {
701 	qf_free(0);
702 	for (i = 1; i < LISTCOUNT; ++i)
703 	    qf_lists[i - 1] = qf_lists[i];
704 	qf_curlist = LISTCOUNT - 1;
705     }
706     else
707 	qf_curlist = qf_listcount++;
708     qf_lists[qf_curlist].qf_index = 0;
709     qf_lists[qf_curlist].qf_count = 0;
710 }
711 
712 /*
713  * Add an entry to the end of the list of errors.
714  * Returns OK or FAIL.
715  */
716     static int
717 qf_add_entry(prevp, dir, fname, mesg, lnum, col, virt_col, nr, type, valid)
718     struct qf_line **prevp;	/* pointer to previously added entry or NULL */
719     char_u	*dir;		/* optional directory name */
720     char_u	*fname;		/* file name or NULL */
721     char_u	*mesg;		/* message */
722     long	lnum;		/* line number */
723     int		col;		/* column */
724     int		virt_col;	/* using virtual column */
725     int		nr;		/* error number */
726     int		type;		/* type character */
727     int		valid;		/* valid entry */
728 {
729     struct qf_line *qfp;
730 
731     if ((qfp = (struct qf_line *)alloc((unsigned)sizeof(struct qf_line)))
732 								      == NULL)
733 	return FAIL;
734     qfp->qf_fnum = qf_get_fnum(dir, fname);
735     if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
736     {
737 	vim_free(qfp);
738 	return FAIL;
739     }
740     qfp->qf_lnum = lnum;
741     qfp->qf_col = col;
742     qfp->qf_virt_col = virt_col;
743     qfp->qf_nr = nr;
744     if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
745 	type = 0;
746     qfp->qf_type = type;
747     qfp->qf_valid = valid;
748 
749     if (qf_lists[qf_curlist].qf_count == 0)	/* first element in the list */
750     {
751 	qf_lists[qf_curlist].qf_start = qfp;
752 	qfp->qf_prev = qfp;	/* first element points to itself */
753     }
754     else
755     {
756 	qfp->qf_prev = *prevp;
757 	(*prevp)->qf_next = qfp;
758     }
759     qfp->qf_next = qfp;	/* last element points to itself */
760     qfp->qf_cleared = FALSE;
761     *prevp = qfp;
762     ++qf_lists[qf_curlist].qf_count;
763     if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid)
764 					    /* first valid entry */
765     {
766 	qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count;
767 	qf_lists[qf_curlist].qf_ptr = qfp;
768     }
769 
770     return OK;
771 }
772 
773 /*
774  * get buffer number for file "dir.name"
775  */
776     static int
777 qf_get_fnum(directory, fname)
778     char_u   *directory;
779     char_u   *fname;
780 {
781     if (fname == NULL || *fname == NUL)		/* no file name */
782 	return 0;
783     {
784 #ifdef RISCOS
785 	/* Name is reported as `main.c', but file is `c.main' */
786 	return ro_buflist_add(fname);
787 #else
788 	char_u	    *ptr;
789 	int	    fnum;
790 
791 # ifdef VMS
792 	vms_remove_version(fname);
793 # endif
794 # ifdef BACKSLASH_IN_FILENAME
795 	if (directory != NULL)
796 	    slash_adjust(directory);
797 	slash_adjust(fname);
798 # endif
799 	if (directory != NULL && !vim_isAbsName(fname)
800 		&& (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
801 	{
802 	    /*
803 	     * Here we check if the file really exists.
804 	     * This should normally be true, but if make works without
805 	     * "leaving directory"-messages we might have missed a
806 	     * directory change.
807 	     */
808 	    if (mch_getperm(ptr) < 0)
809 	    {
810 		vim_free(ptr);
811 		directory = qf_guess_filepath(fname);
812 		if (directory)
813 		    ptr = concat_fnames(directory, fname, TRUE);
814 		else
815 		    ptr = vim_strsave(fname);
816 	    }
817 	    /* Use concatenated directory name and file name */
818 	    fnum = buflist_add(ptr, 0);
819 	    vim_free(ptr);
820 	    return fnum;
821 	}
822 	return buflist_add(fname, 0);
823 #endif
824     }
825 }
826 
827 /*
828  * push dirbuf onto the directory stack and return pointer to actual dir or
829  * NULL on error
830  */
831     static char_u *
832 qf_push_dir(dirbuf, stackptr)
833     char_u		*dirbuf;
834     struct dir_stack_T	**stackptr;
835 {
836     struct dir_stack_T  *ds_new;
837     struct dir_stack_T  *ds_ptr;
838 
839     /* allocate new stack element and hook it in */
840     ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
841     if (ds_new == NULL)
842 	return NULL;
843 
844     ds_new->next = *stackptr;
845     *stackptr = ds_new;
846 
847     /* store directory on the stack */
848     if (vim_isAbsName(dirbuf)
849 	    || (*stackptr)->next == NULL
850 	    || (*stackptr && dir_stack != *stackptr))
851 	(*stackptr)->dirname = vim_strsave(dirbuf);
852     else
853     {
854 	/* Okay we don't have an absolute path.
855 	 * dirbuf must be a subdir of one of the directories on the stack.
856 	 * Let's search...
857 	 */
858 	ds_new = (*stackptr)->next;
859 	(*stackptr)->dirname = NULL;
860 	while (ds_new)
861 	{
862 	    vim_free((*stackptr)->dirname);
863 	    (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
864 		    TRUE);
865 	    if (mch_isdir((*stackptr)->dirname) == TRUE)
866 		break;
867 
868 	    ds_new = ds_new->next;
869 	}
870 
871 	/* clean up all dirs we already left */
872 	while ((*stackptr)->next != ds_new)
873 	{
874 	    ds_ptr = (*stackptr)->next;
875 	    (*stackptr)->next = (*stackptr)->next->next;
876 	    vim_free(ds_ptr->dirname);
877 	    vim_free(ds_ptr);
878 	}
879 
880 	/* Nothing found -> it must be on top level */
881 	if (ds_new == NULL)
882 	{
883 	    vim_free((*stackptr)->dirname);
884 	    (*stackptr)->dirname = vim_strsave(dirbuf);
885 	}
886     }
887 
888     if ((*stackptr)->dirname != NULL)
889 	return (*stackptr)->dirname;
890     else
891     {
892 	ds_ptr = *stackptr;
893 	*stackptr = (*stackptr)->next;
894 	vim_free(ds_ptr);
895 	return NULL;
896     }
897 }
898 
899 
900 /*
901  * pop dirbuf from the directory stack and return previous directory or NULL if
902  * stack is empty
903  */
904     static char_u *
905 qf_pop_dir(stackptr)
906     struct dir_stack_T	**stackptr;
907 {
908     struct dir_stack_T  *ds_ptr;
909 
910     /* TODO: Should we check if dirbuf is the directory on top of the stack?
911      * What to do if it isn't? */
912 
913     /* pop top element and free it */
914     if (*stackptr != NULL)
915     {
916 	ds_ptr = *stackptr;
917 	*stackptr = (*stackptr)->next;
918 	vim_free(ds_ptr->dirname);
919 	vim_free(ds_ptr);
920     }
921 
922     /* return NEW top element as current dir or NULL if stack is empty*/
923     return *stackptr ? (*stackptr)->dirname : NULL;
924 }
925 
926 /*
927  * clean up directory stack
928  */
929     static void
930 qf_clean_dir_stack(stackptr)
931     struct dir_stack_T	**stackptr;
932 {
933     struct dir_stack_T  *ds_ptr;
934 
935     while ((ds_ptr = *stackptr) != NULL)
936     {
937 	*stackptr = (*stackptr)->next;
938 	vim_free(ds_ptr->dirname);
939 	vim_free(ds_ptr);
940     }
941 }
942 
943 /*
944  * Check in which directory of the directory stack the given file can be
945  * found.
946  * Returns a pointer to the directory name or NULL if not found
947  * Cleans up intermediate directory entries.
948  *
949  * TODO: How to solve the following problem?
950  * If we have the this directory tree:
951  *     ./
952  *     ./aa
953  *     ./aa/bb
954  *     ./bb
955  *     ./bb/x.c
956  * and make says:
957  *     making all in aa
958  *     making all in bb
959  *     x.c:9: Error
960  * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
961  * qf_guess_filepath will return NULL.
962  */
963     static char_u *
964 qf_guess_filepath(filename)
965     char_u *filename;
966 {
967     struct dir_stack_T     *ds_ptr;
968     struct dir_stack_T     *ds_tmp;
969     char_u		   *fullname;
970 
971     /* no dirs on the stack - there's nothing we can do */
972     if (dir_stack == NULL)
973 	return NULL;
974 
975     ds_ptr = dir_stack->next;
976     fullname = NULL;
977     while (ds_ptr)
978     {
979 	vim_free(fullname);
980 	fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
981 
982 	/* If concat_fnames failed, just go on. The worst thing that can happen
983 	 * is that we delete the entire stack.
984 	 */
985 	if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
986 	    break;
987 
988 	ds_ptr = ds_ptr->next;
989     }
990 
991     vim_free(fullname);
992 
993     /* clean up all dirs we already left */
994     while (dir_stack->next != ds_ptr)
995     {
996 	ds_tmp = dir_stack->next;
997 	dir_stack->next = dir_stack->next->next;
998 	vim_free(ds_tmp->dirname);
999 	vim_free(ds_tmp);
1000     }
1001 
1002     return ds_ptr==NULL? NULL: ds_ptr->dirname;
1003 
1004 }
1005 
1006 /*
1007  * jump to a quickfix line
1008  * if dir == FORWARD go "errornr" valid entries forward
1009  * if dir == BACKWARD go "errornr" valid entries backward
1010  * if dir == FORWARD_FILE go "errornr" valid entries files backward
1011  * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1012  * else if "errornr" is zero, redisplay the same line
1013  * else go to entry "errornr"
1014  */
1015     void
1016 qf_jump(dir, errornr, forceit)
1017     int		dir;
1018     int		errornr;
1019     int		forceit;
1020 {
1021     struct qf_line	*qf_ptr;
1022     struct qf_line	*old_qf_ptr;
1023     int			qf_index;
1024     int			old_qf_fnum;
1025     int			old_qf_index;
1026     int			prev_index;
1027     static char_u	*e_no_more_items = (char_u *)N_("E553: No more items");
1028     char_u		*err = e_no_more_items;
1029     linenr_T		i;
1030     buf_T		*old_curbuf;
1031     linenr_T		old_lnum;
1032     char_u		*old_swb = p_swb;
1033     colnr_T		screen_col;
1034     colnr_T		char_col;
1035     char_u		*line;
1036 #ifdef FEAT_WINDOWS
1037     int			opened_window = FALSE;
1038     win_T		*win;
1039     win_T		*altwin;
1040 #endif
1041     int			print_message = TRUE;
1042     int			len;
1043 #ifdef FEAT_FOLDING
1044     int			old_KeyTyped = KeyTyped; /* getting file may reset it */
1045 #endif
1046     int			ok = OK;
1047 
1048     if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1049     {
1050 	EMSG(_(e_quickfix));
1051 	return;
1052     }
1053 
1054     qf_ptr = qf_lists[qf_curlist].qf_ptr;
1055     old_qf_ptr = qf_ptr;
1056     qf_index = qf_lists[qf_curlist].qf_index;
1057     old_qf_index = qf_index;
1058     if (dir == FORWARD || dir == FORWARD_FILE)	    /* next valid entry */
1059     {
1060 	while (errornr--)
1061 	{
1062 	    old_qf_ptr = qf_ptr;
1063 	    prev_index = qf_index;
1064 	    old_qf_fnum = qf_ptr->qf_fnum;
1065 	    do
1066 	    {
1067 		if (qf_index == qf_lists[qf_curlist].qf_count
1068 						   || qf_ptr->qf_next == NULL)
1069 		{
1070 		    qf_ptr = old_qf_ptr;
1071 		    qf_index = prev_index;
1072 		    if (err != NULL)
1073 		    {
1074 			EMSG(_(err));
1075 			goto theend;
1076 		    }
1077 		    errornr = 0;
1078 		    break;
1079 		}
1080 		++qf_index;
1081 		qf_ptr = qf_ptr->qf_next;
1082 	    } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1083 		  || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1084 	    err = NULL;
1085 	}
1086     }
1087     else if (dir == BACKWARD || dir == BACKWARD_FILE)  /* prev. valid entry */
1088     {
1089 	while (errornr--)
1090 	{
1091 	    old_qf_ptr = qf_ptr;
1092 	    prev_index = qf_index;
1093 	    old_qf_fnum = qf_ptr->qf_fnum;
1094 	    do
1095 	    {
1096 		if (qf_index == 1 || qf_ptr->qf_prev == 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_prev;
1110 	    } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1111 		  || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1112 	    err = NULL;
1113 	}
1114     }
1115     else if (errornr != 0)	/* go to specified number */
1116     {
1117 	while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1118 	{
1119 	    --qf_index;
1120 	    qf_ptr = qf_ptr->qf_prev;
1121 	}
1122 	while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count
1123 						   && qf_ptr->qf_next != NULL)
1124 	{
1125 	    ++qf_index;
1126 	    qf_ptr = qf_ptr->qf_next;
1127 	}
1128     }
1129 
1130 #ifdef FEAT_WINDOWS
1131     qf_lists[qf_curlist].qf_index = qf_index;
1132     if (qf_win_pos_update(old_qf_index))
1133 	/* No need to print the error message if it's visible in the error
1134 	 * window */
1135 	print_message = FALSE;
1136 
1137     /*
1138      * For ":helpgrep" find a help window or open one.
1139      */
1140     if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1141     {
1142 	win_T	*wp;
1143 	int	n;
1144 
1145 	for (wp = firstwin; wp != NULL; wp = wp->w_next)
1146 	    if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1147 		break;
1148 	if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1149 	    win_enter(wp, TRUE);
1150 	else
1151 	{
1152 	    /*
1153 	     * Split off help window; put it at far top if no position
1154 	     * specified, the current window is vertically split and narrow.
1155 	     */
1156 	    n = WSP_HELP;
1157 # ifdef FEAT_VERTSPLIT
1158 	    if (cmdmod.split == 0 && curwin->w_width != Columns
1159 						      && curwin->w_width < 80)
1160 		n |= WSP_TOP;
1161 # endif
1162 	    if (win_split(0, n) == FAIL)
1163 		goto theend;
1164 	    opened_window = TRUE;	/* close it when fail */
1165 
1166 	    if (curwin->w_height < p_hh)
1167 		win_setheight((int)p_hh);
1168 	}
1169 
1170 	if (!p_im)
1171 	    restart_edit = 0;	    /* don't want insert mode in help file */
1172     }
1173 
1174     /*
1175      * If currently in the quickfix window, find another window to show the
1176      * file in.
1177      */
1178     if (bt_quickfix(curbuf) && !opened_window)
1179     {
1180 	/*
1181 	 * If there is no file specified, we don't know where to go.
1182 	 * But do advance, otherwise ":cn" gets stuck.
1183 	 */
1184 	if (qf_ptr->qf_fnum == 0)
1185 	    goto theend;
1186 
1187 	/*
1188 	 * If there is only one window, create a new one above the quickfix
1189 	 * window.
1190 	 */
1191 	if (firstwin == lastwin)
1192 	{
1193 	    if (win_split(0, WSP_ABOVE) == FAIL)
1194 		goto failed;		/* not enough room for window */
1195 	    opened_window = TRUE;	/* close it when fail */
1196 	    p_swb = empty_option;	/* don't split again */
1197 # ifdef FEAT_SCROLLBIND
1198 	    curwin->w_p_scb = FALSE;
1199 # endif
1200 	}
1201 	else
1202 	{
1203 	    /*
1204 	     * Try to find a window that shows the right buffer.
1205 	     * Default to the window just above the quickfix buffer.
1206 	     */
1207 	    win = curwin;
1208 	    altwin = NULL;
1209 	    for (;;)
1210 	    {
1211 		if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1212 		    break;
1213 		if (win->w_prev == NULL)
1214 		    win = lastwin;	/* wrap around the top */
1215 		else
1216 		    win = win->w_prev;	/* go to previous window */
1217 
1218 		if (bt_quickfix(win->w_buffer))
1219 		{
1220 		    /* Didn't find it, go to the window before the quickfix
1221 		     * window. */
1222 		    if (altwin != NULL)
1223 			win = altwin;
1224 		    else if (curwin->w_prev != NULL)
1225 			win = curwin->w_prev;
1226 		    else
1227 			win = curwin->w_next;
1228 		    break;
1229 		}
1230 
1231 		/* Remember a usable window. */
1232 		if (altwin == NULL && !win->w_p_pvw
1233 					   && win->w_buffer->b_p_bt[0] == NUL)
1234 		    altwin = win;
1235 	    }
1236 
1237 	    win_goto(win);
1238 	}
1239     }
1240 #endif
1241 
1242     /*
1243      * If there is a file name,
1244      * read the wanted file if needed, and check autowrite etc.
1245      */
1246     old_curbuf = curbuf;
1247     old_lnum = curwin->w_cursor.lnum;
1248 
1249     if (qf_ptr->qf_fnum != 0)
1250     {
1251 	if (qf_ptr->qf_type == 1)
1252 	{
1253 	    /* Open help file (do_ecmd() will set b_help flag, readfile() will
1254 	     * set b_p_ro flag). */
1255 	    if (!can_abandon(curbuf, forceit))
1256 	    {
1257 		EMSG(_(e_nowrtmsg));
1258 		ok = FALSE;
1259 	    }
1260 	    else
1261 		ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1262 						   ECMD_HIDE + ECMD_SET_HELP);
1263 	}
1264 	else
1265 	    ok = buflist_getfile(qf_ptr->qf_fnum,
1266 			    (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1267     }
1268 
1269     if (ok == OK)
1270     {
1271 	/* When not switched to another buffer, still need to set pc mark */
1272 	if (curbuf == old_curbuf)
1273 	    setpcmark();
1274 
1275 	/*
1276 	 * Go to line with error, unless qf_lnum is 0.
1277 	 */
1278 	i = qf_ptr->qf_lnum;
1279 	if (i > 0)
1280 	{
1281 	    if (i > curbuf->b_ml.ml_line_count)
1282 		i = curbuf->b_ml.ml_line_count;
1283 	    curwin->w_cursor.lnum = i;
1284 	}
1285 	if (qf_ptr->qf_col > 0)
1286 	{
1287 	    curwin->w_cursor.col = qf_ptr->qf_col - 1;
1288 	    if (qf_ptr->qf_virt_col == TRUE)
1289 	    {
1290 		/*
1291 		 * Check each character from the beginning of the error
1292 		 * line up to the error column.  For each tab character
1293 		 * found, reduce the error column value by the length of
1294 		 * a tab character.
1295 		 */
1296 		line = ml_get_curline();
1297 		screen_col = 0;
1298 		for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1299 		{
1300 		    if (*line == NUL)
1301 			break;
1302 		    if (*line++ == '\t')
1303 		    {
1304 			curwin->w_cursor.col -= 7 - (screen_col % 8);
1305 			screen_col += 8 - (screen_col % 8);
1306 		    }
1307 		    else
1308 			++screen_col;
1309 		}
1310 	    }
1311 	    check_cursor();
1312 	}
1313 	else
1314 	    beginline(BL_WHITE | BL_FIX);
1315 
1316 #ifdef FEAT_FOLDING
1317 	if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1318 	    foldOpenCursor();
1319 #endif
1320 	if (print_message)
1321 	{
1322 	    /* Update the screen before showing the message */
1323 	    update_topline_redraw();
1324 	    sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1325 		    qf_lists[qf_curlist].qf_count,
1326 		    qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1327 		    (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1328 	    /* Add the message, skipping leading whitespace and newlines. */
1329 	    len = (int)STRLEN(IObuff);
1330 	    qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1331 
1332 	    /* Output the message.  Overwrite to avoid scrolling when the 'O'
1333 	     * flag is present in 'shortmess'; But when not jumping, print the
1334 	     * whole message. */
1335 	    i = msg_scroll;
1336 	    if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1337 		msg_scroll = TRUE;
1338 	    else if (!msg_scrolled && shortmess(SHM_OVERALL))
1339 		msg_scroll = FALSE;
1340 	    msg_attr_keep(IObuff, 0, TRUE);
1341 	    msg_scroll = i;
1342 	}
1343     }
1344     else
1345     {
1346 #ifdef FEAT_WINDOWS
1347 	if (opened_window)
1348 	    win_close(curwin, TRUE);    /* Close opened window */
1349 #endif
1350 	if (qf_ptr->qf_fnum != 0)
1351 	{
1352 	    /*
1353 	     * Couldn't open file, so put index back where it was.  This could
1354 	     * happen if the file was readonly and we changed something.
1355 	     */
1356 #ifdef FEAT_WINDOWS
1357 failed:
1358 #endif
1359 	    qf_ptr = old_qf_ptr;
1360 	    qf_index = old_qf_index;
1361 	}
1362     }
1363 theend:
1364     qf_lists[qf_curlist].qf_ptr = qf_ptr;
1365     qf_lists[qf_curlist].qf_index = qf_index;
1366 #ifdef FEAT_WINDOWS
1367     if (p_swb != old_swb && opened_window)
1368     {
1369 	/* Restore old 'switchbuf' value, but not when an autocommand or
1370 	 * modeline has changed the value. */
1371 	if (p_swb == empty_option)
1372 	    p_swb = old_swb;
1373 	else
1374 	    free_string_option(old_swb);
1375     }
1376 #endif
1377 }
1378 
1379 /*
1380  * ":clist": list all errors
1381  */
1382     void
1383 qf_list(eap)
1384     exarg_T	*eap;
1385 {
1386     buf_T		*buf;
1387     char_u		*fname;
1388     struct qf_line	*qfp;
1389     int			i;
1390     int			idx1 = 1;
1391     int			idx2 = -1;
1392     int			need_return = TRUE;
1393     int			last_printed = 1;
1394     char_u		*arg = eap->arg;
1395     int			all = eap->forceit;	/* if not :cl!, only show
1396 						   recognised errors */
1397 
1398     if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1399     {
1400 	EMSG(_(e_quickfix));
1401 	return;
1402     }
1403     if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1404     {
1405 	EMSG(_(e_trailing));
1406 	return;
1407     }
1408     i = qf_lists[qf_curlist].qf_count;
1409     if (idx1 < 0)
1410 	idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1411     if (idx2 < 0)
1412 	idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1413 
1414     more_back_used = TRUE;
1415     if (qf_lists[qf_curlist].qf_nonevalid)
1416 	all = TRUE;
1417     qfp = qf_lists[qf_curlist].qf_start;
1418     for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
1419     {
1420 	if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1421 	{
1422 	    if (need_return)
1423 	    {
1424 		msg_putchar('\n');
1425 		need_return = FALSE;
1426 	    }
1427 	    if (more_back == 0)
1428 	    {
1429 		fname = NULL;
1430 		if (qfp->qf_fnum != 0
1431 			      && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1432 		{
1433 		    fname = buf->b_fname;
1434 		    if (qfp->qf_type == 1)	/* :helpgrep */
1435 			fname = gettail(fname);
1436 		}
1437 		if (fname == NULL)
1438 		    sprintf((char *)IObuff, "%2d", i);
1439 		else
1440 		    sprintf((char *)IObuff, "%2d %s", i, (char *)fname);
1441 		msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1442 			? hl_attr(HLF_L) : hl_attr(HLF_D));
1443 		if (qfp->qf_lnum == 0)
1444 		    IObuff[0] = NUL;
1445 		else if (qfp->qf_col == 0)
1446 		    sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1447 		else
1448 		    sprintf((char *)IObuff, ":%ld col %d",
1449 						   qfp->qf_lnum, qfp->qf_col);
1450 		sprintf((char *)IObuff + STRLEN(IObuff), "%s: ",
1451 				  (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1452 		msg_puts_attr(IObuff, hl_attr(HLF_N));
1453 		/* Remove newlines and leading whitespace from the text.
1454 		 * For an unrecognized line keep the indent, the compiler may
1455 		 * mark a word with ^^^^. */
1456 		qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1457 				     ? skipwhite(qfp->qf_text) : qfp->qf_text,
1458 							      IObuff, IOSIZE);
1459 		msg_prt_line(IObuff);
1460 		out_flush();		/* show one line at a time */
1461 		need_return = TRUE;
1462 		last_printed = i;
1463 	    }
1464 	}
1465 	if (more_back)
1466 	{
1467 	    /* scrolling backwards from the more-prompt */
1468 	    /* TODO: compute the number of items from the screen lines */
1469 	    more_back = more_back * 2 - 1;
1470 	    while (i > last_printed - more_back && i > idx1)
1471 	    {
1472 		do
1473 		{
1474 		    qfp = qfp->qf_prev;
1475 		    --i;
1476 		}
1477 		while (i > idx1 && !qfp->qf_valid && !all);
1478 	    }
1479 	    more_back = 0;
1480 	}
1481 	else
1482 	{
1483 	    qfp = qfp->qf_next;
1484 	    ++i;
1485 	}
1486 	ui_breakcheck();
1487     }
1488     more_back_used = FALSE;
1489 }
1490 
1491 /*
1492  * Remove newlines and leading whitespace from an error message.
1493  * Put the result in "buf[bufsize]".
1494  */
1495     static void
1496 qf_fmt_text(text, buf, bufsize)
1497     char_u	*text;
1498     char_u	*buf;
1499     int		bufsize;
1500 {
1501     int		i;
1502     char_u	*p = text;
1503 
1504     for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1505     {
1506 	if (*p == '\n')
1507 	{
1508 	    buf[i] = ' ';
1509 	    while (*++p != NUL)
1510 		if (!vim_iswhite(*p) && *p != '\n')
1511 		    break;
1512 	}
1513 	else
1514 	    buf[i] = *p++;
1515     }
1516     buf[i] = NUL;
1517 }
1518 
1519 /*
1520  * ":colder [count]": Up in the quickfix stack.
1521  * ":cnewer [count]": Down in the quickfix stack.
1522  */
1523     void
1524 qf_age(eap)
1525     exarg_T	*eap;
1526 {
1527     int		count;
1528 
1529     if (eap->addr_count != 0)
1530 	count = eap->line2;
1531     else
1532 	count = 1;
1533     while (count--)
1534     {
1535 	if (eap->cmdidx == CMD_colder)
1536 	{
1537 	    if (qf_curlist == 0)
1538 	    {
1539 		EMSG(_("E380: At bottom of quickfix stack"));
1540 		return;
1541 	    }
1542 	    --qf_curlist;
1543 	}
1544 	else
1545 	{
1546 	    if (qf_curlist >= qf_listcount - 1)
1547 	    {
1548 		EMSG(_("E381: At top of quickfix stack"));
1549 		return;
1550 	    }
1551 	    ++qf_curlist;
1552 	}
1553     }
1554     qf_msg();
1555 }
1556 
1557     static void
1558 qf_msg()
1559 {
1560     smsg((char_u *)_("error list %d of %d; %d errors"),
1561 	    qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1562 #ifdef FEAT_WINDOWS
1563     qf_update_buffer();
1564 #endif
1565 }
1566 
1567 /*
1568  * free the error list
1569  */
1570     static void
1571 qf_free(idx)
1572     int		idx;
1573 {
1574     struct qf_line *qfp;
1575 
1576     while (qf_lists[idx].qf_count)
1577     {
1578 	qfp = qf_lists[idx].qf_start->qf_next;
1579 	vim_free(qf_lists[idx].qf_start->qf_text);
1580 	vim_free(qf_lists[idx].qf_start);
1581 	qf_lists[idx].qf_start = qfp;
1582 	--qf_lists[idx].qf_count;
1583     }
1584 }
1585 
1586 /*
1587  * qf_mark_adjust: adjust marks
1588  */
1589    void
1590 qf_mark_adjust(line1, line2, amount, amount_after)
1591     linenr_T	line1;
1592     linenr_T	line2;
1593     long	amount;
1594     long	amount_after;
1595 {
1596     int			i;
1597     struct qf_line	*qfp;
1598     int			idx;
1599 
1600     for (idx = 0; idx < qf_listcount; ++idx)
1601 	if (qf_lists[idx].qf_count)
1602 	    for (i = 0, qfp = qf_lists[idx].qf_start;
1603 		       i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1604 		if (qfp->qf_fnum == curbuf->b_fnum)
1605 		{
1606 		    if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1607 		    {
1608 			if (amount == MAXLNUM)
1609 			    qfp->qf_cleared = TRUE;
1610 			else
1611 			    qfp->qf_lnum += amount;
1612 		    }
1613 		    else if (amount_after && qfp->qf_lnum > line2)
1614 			qfp->qf_lnum += amount_after;
1615 		}
1616 }
1617 
1618 /*
1619  * Make a nice message out of the error character and the error number:
1620  *  char    number	message
1621  *  e or E    0		" error"
1622  *  w or W    0		" warning"
1623  *  i or I    0		" info"
1624  *  0	      0		""
1625  *  other     0		" c"
1626  *  e or E    n		" error n"
1627  *  w or W    n		" warning n"
1628  *  i or I    n		" info n"
1629  *  0	      n		" error n"
1630  *  other     n		" c n"
1631  *  1	      x		""	:helpgrep
1632  */
1633     static char_u *
1634 qf_types(c, nr)
1635     int c, nr;
1636 {
1637     static char_u	buf[20];
1638     static char_u	cc[3];
1639     char_u		*p;
1640 
1641     if (c == 'W' || c == 'w')
1642 	p = (char_u *)" warning";
1643     else if (c == 'I' || c == 'i')
1644 	p = (char_u *)" info";
1645     else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1646 	p = (char_u *)" error";
1647     else if (c == 0 || c == 1)
1648 	p = (char_u *)"";
1649     else
1650     {
1651 	cc[0] = ' ';
1652 	cc[1] = c;
1653 	cc[2] = NUL;
1654 	p = cc;
1655     }
1656 
1657     if (nr <= 0)
1658 	return p;
1659 
1660     sprintf((char *)buf, "%s %3d", (char *)p, nr);
1661     return buf;
1662 }
1663 
1664 #if defined(FEAT_WINDOWS) || defined(PROTO)
1665 /*
1666  * ":cwindow": open the quickfix window if we have errors to display,
1667  *	       close it if not.
1668  */
1669     void
1670 ex_cwindow(eap)
1671     exarg_T	*eap;
1672 {
1673     win_T	*win;
1674 
1675     /*
1676      * Look for an existing quickfix window.
1677      */
1678     for (win = firstwin; win != NULL; win = win->w_next)
1679 	if (bt_quickfix(win->w_buffer))
1680 	    break;
1681 
1682     /*
1683      * If a quickfix window is open but we have no errors to display,
1684      * close the window.  If a quickfix window is not open, then open
1685      * it if we have errors; otherwise, leave it closed.
1686      */
1687     if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1688     {
1689 	if (win != NULL)
1690 	    ex_cclose(eap);
1691     }
1692     else if (win == NULL)
1693 	ex_copen(eap);
1694 }
1695 
1696 /*
1697  * ":cclose": close the window showing the list of errors.
1698  */
1699 /*ARGSUSED*/
1700     void
1701 ex_cclose(eap)
1702     exarg_T	*eap;
1703 {
1704     win_T	*win;
1705 
1706     /*
1707      * Find existing quickfix window and close it.
1708      */
1709     for (win = firstwin; win != NULL; win = win->w_next)
1710 	if (bt_quickfix(win->w_buffer))
1711 	    break;
1712 
1713     if (win != NULL)
1714 	win_close(win, FALSE);
1715 }
1716 
1717 /*
1718  * ":copen": open a window that shows the list of errors.
1719  */
1720     void
1721 ex_copen(eap)
1722     exarg_T	*eap;
1723 {
1724     int		height;
1725     buf_T	*buf;
1726     win_T	*win;
1727 
1728     if (eap->addr_count != 0)
1729 	height = eap->line2;
1730     else
1731 	height = QF_WINHEIGHT;
1732 
1733 #ifdef FEAT_VISUAL
1734     reset_VIsual_and_resel();			/* stop Visual mode */
1735 #endif
1736 #ifdef FEAT_GUI
1737     need_mouse_correct = TRUE;
1738 #endif
1739 
1740     /*
1741      * Find existing quickfix window, or open a new one.
1742      */
1743     for (win = firstwin; win != NULL; win = win->w_next)
1744 	if (bt_quickfix(win->w_buffer))
1745 	    break;
1746     if (win != NULL)
1747 	win_goto(win);
1748     else
1749     {
1750 	/* The current window becomes the previous window afterwards. */
1751 	win = curwin;
1752 
1753 	/* Create the new window at the very bottom. */
1754 	win_goto(lastwin);
1755 	if (win_split(height, WSP_BELOW) == FAIL)
1756 	    return;		/* not enough room for window */
1757 #ifdef FEAT_SCROLLBIND
1758 	curwin->w_p_scb = FALSE;
1759 #endif
1760 
1761 	/*
1762 	 * Find existing quickfix buffer, or create a new one.
1763 	 */
1764 	buf = qf_find_buf();
1765 	if (buf == NULL)
1766 	{
1767 	    (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1768 	    /* switch off 'swapfile' */
1769 	    set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1770 	    set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1771 								   OPT_LOCAL);
1772 	    set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1773 	    set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1774 	}
1775 	else if (buf != curbuf)
1776 	    set_curbuf(buf, DOBUF_GOTO);
1777 
1778 	/* Only set the height when there is no window to the side. */
1779 	if (curwin->w_width == Columns)
1780 	    win_setheight(height);
1781 	curwin->w_p_wfh = TRUE;	    /* set 'winfixheight' */
1782 	if (win_valid(win))
1783 	    prevwin = win;
1784     }
1785 
1786     /*
1787      * Fill the buffer with the quickfix list.
1788      */
1789     qf_fill_buffer();
1790 
1791     curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1792     curwin->w_cursor.col = 0;
1793     check_cursor();
1794     update_topline();		/* scroll to show the line */
1795 }
1796 
1797 /*
1798  * Return the number of the current entry (line number in the quickfix
1799  * window).
1800  */
1801      linenr_T
1802 qf_current_entry()
1803 {
1804     return qf_lists[qf_curlist].qf_index;
1805 }
1806 
1807 /*
1808  * Update the cursor position in the quickfix window to the current error.
1809  * Return TRUE if there is a quickfix window.
1810  */
1811     static int
1812 qf_win_pos_update(old_qf_index)
1813     int		old_qf_index;	/* previous qf_index or zero */
1814 {
1815     win_T	*win;
1816     int		qf_index = qf_lists[qf_curlist].qf_index;
1817 
1818     /*
1819      * Put the cursor on the current error in the quickfix window, so that
1820      * it's viewable.
1821      */
1822     for (win = firstwin; win != NULL; win = win->w_next)
1823 	if (bt_quickfix(win->w_buffer))
1824 	    break;
1825     if (win != NULL
1826 	    && qf_index <= win->w_buffer->b_ml.ml_line_count
1827 	    && old_qf_index != qf_index)
1828     {
1829 	win_T	*old_curwin = curwin;
1830 
1831 	curwin = win;
1832 	curbuf = win->w_buffer;
1833 	if (qf_index > old_qf_index)
1834 	{
1835 	    curwin->w_redraw_top = old_qf_index;
1836 	    curwin->w_redraw_bot = qf_index;
1837 	}
1838 	else
1839 	{
1840 	    curwin->w_redraw_top = qf_index;
1841 	    curwin->w_redraw_bot = old_qf_index;
1842 	}
1843 	curwin->w_cursor.lnum = qf_index;
1844 	curwin->w_cursor.col = 0;
1845 	update_topline();		/* scroll to show the line */
1846 	redraw_later(VALID);
1847 	curwin->w_redr_status = TRUE;	/* update ruler */
1848 	curwin = old_curwin;
1849 	curbuf = curwin->w_buffer;
1850     }
1851     return win != NULL;
1852 }
1853 
1854 /*
1855  * Find quickfix buffer.
1856  */
1857     static buf_T *
1858 qf_find_buf()
1859 {
1860     buf_T	*buf;
1861 
1862     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1863 	if (bt_quickfix(buf))
1864 	    break;
1865     return buf;
1866 }
1867 
1868 /*
1869  * Find the quickfix buffer.  If it exists, update the contents.
1870  */
1871     static void
1872 qf_update_buffer()
1873 {
1874     buf_T	*buf;
1875 #ifdef FEAT_AUTOCMD
1876     aco_save_T	aco;
1877 #else
1878     buf_T	*save_curbuf;
1879 #endif
1880 
1881     /* Check if a buffer for the quickfix list exists.  Update it. */
1882     buf = qf_find_buf();
1883     if (buf != NULL)
1884     {
1885 #ifdef FEAT_AUTOCMD
1886 	/* set curwin/curbuf to buf and save a few things */
1887 	aucmd_prepbuf(&aco, buf);
1888 #else
1889 	save_curbuf = curbuf;
1890 	curbuf = buf;
1891 #endif
1892 
1893 	qf_fill_buffer();
1894 
1895 #ifdef FEAT_AUTOCMD
1896 	/* restore curwin/curbuf and a few other things */
1897 	aucmd_restbuf(&aco);
1898 #else
1899 	curbuf = save_curbuf;
1900 #endif
1901 
1902 	(void)qf_win_pos_update(0);
1903     }
1904 }
1905 
1906 /*
1907  * Fill current buffer with quickfix errors, replacing any previous contents.
1908  * curbuf must be the quickfix buffer!
1909  */
1910     static void
1911 qf_fill_buffer()
1912 {
1913     linenr_T		lnum;
1914     struct qf_line	*qfp;
1915     buf_T		*errbuf;
1916     int			len;
1917     int			old_KeyTyped = KeyTyped;
1918 
1919     /* delete all existing lines */
1920     while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
1921 	(void)ml_delete((linenr_T)1, FALSE);
1922 
1923     /* Check if there is anything to display */
1924     if (qf_curlist < qf_listcount)
1925     {
1926 	/* Add one line for each error */
1927 	qfp = qf_lists[qf_curlist].qf_start;
1928 	for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
1929 	{
1930 	    if (qfp->qf_fnum != 0
1931 		    && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
1932 		    && errbuf->b_fname != NULL)
1933 	    {
1934 		if (qfp->qf_type == 1)	/* :helpgrep */
1935 		    STRCPY(IObuff, gettail(errbuf->b_fname));
1936 		else
1937 		    STRCPY(IObuff, errbuf->b_fname);
1938 		len = (int)STRLEN(IObuff);
1939 	    }
1940 	    else
1941 		len = 0;
1942 	    IObuff[len++] = '|';
1943 
1944 	    if (qfp->qf_lnum > 0)
1945 	    {
1946 		sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
1947 		len += (int)STRLEN(IObuff + len);
1948 
1949 		if (qfp->qf_col > 0)
1950 		{
1951 		    sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
1952 		    len += (int)STRLEN(IObuff + len);
1953 		}
1954 
1955 		sprintf((char *)IObuff + len, "%s",
1956 				  (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1957 		len += (int)STRLEN(IObuff + len);
1958 	    }
1959 	    IObuff[len++] = '|';
1960 	    IObuff[len++] = ' ';
1961 
1962 	    /* Remove newlines and leading whitespace from the text.
1963 	     * For an unrecognized line keep the indent, the compiler may
1964 	     * mark a word with ^^^^. */
1965 	    qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1966 						  IObuff + len, IOSIZE - len);
1967 
1968 	    if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
1969 								      == FAIL)
1970 		break;
1971 	    qfp = qfp->qf_next;
1972 	}
1973 	/* Delete the empty line which is now at the end */
1974 	(void)ml_delete(lnum + 1, FALSE);
1975     }
1976 
1977     /* correct cursor position */
1978     check_lnums(TRUE);
1979 
1980     /* Set the 'filetype' to "qf" each time after filling the buffer.  This
1981      * resembles reading a file into a buffer, it's more logical when using
1982      * autocommands. */
1983     set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
1984     curbuf->b_p_ma = FALSE;
1985 
1986 #ifdef FEAT_AUTOCMD
1987     apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
1988 							       FALSE, curbuf);
1989     apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
1990 							       FALSE, curbuf);
1991 #endif
1992 
1993     /* make sure it will be redrawn */
1994     redraw_curbuf_later(NOT_VALID);
1995 
1996     /* Restore KeyTyped, setting 'filetype' may reset it. */
1997     KeyTyped = old_KeyTyped;
1998 }
1999 
2000 #endif /* FEAT_WINDOWS */
2001 
2002 /*
2003  * Return TRUE if "buf" is the quickfix buffer.
2004  */
2005     int
2006 bt_quickfix(buf)
2007     buf_T	*buf;
2008 {
2009     return (buf->b_p_bt[0] == 'q');
2010 }
2011 
2012 /*
2013  * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2014  * This means the buffer name is not a file name.
2015  */
2016     int
2017 bt_nofile(buf)
2018     buf_T	*buf;
2019 {
2020     return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2021 	    || buf->b_p_bt[0] == 'a';
2022 }
2023 
2024 /*
2025  * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2026  */
2027     int
2028 bt_dontwrite(buf)
2029     buf_T	*buf;
2030 {
2031     return (buf->b_p_bt[0] == 'n');
2032 }
2033 
2034     int
2035 bt_dontwrite_msg(buf)
2036     buf_T	*buf;
2037 {
2038     if (bt_dontwrite(buf))
2039     {
2040 	EMSG(_("E382: Cannot write, 'buftype' option is set"));
2041 	return TRUE;
2042     }
2043     return FALSE;
2044 }
2045 
2046 /*
2047  * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2048  * and 'bufhidden'.
2049  */
2050     int
2051 buf_hide(buf)
2052     buf_T	*buf;
2053 {
2054     /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2055     switch (buf->b_p_bh[0])
2056     {
2057 	case 'u':		    /* "unload" */
2058 	case 'w':		    /* "wipe" */
2059 	case 'd': return FALSE;	    /* "delete" */
2060 	case 'h': return TRUE;	    /* "hide" */
2061     }
2062     return (p_hid || cmdmod.hide);
2063 }
2064 
2065 /*
2066  * Return TRUE when using ":vimgrep" for ":grep".
2067  */
2068     int
2069 grep_internal(cmdidx)
2070     cmdidx_T	cmdidx;
2071 {
2072     return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd)
2073 	    && STRCMP("internal",
2074 			*curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2075 }
2076 
2077 /*
2078  * Used for ":make", ":grep" and ":grepadd".
2079  */
2080     void
2081 ex_make(eap)
2082     exarg_T	*eap;
2083 {
2084     char_u	*name;
2085     char_u	*cmd;
2086     unsigned	len;
2087 
2088     /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2089     if (grep_internal(eap->cmdidx))
2090     {
2091 	ex_vimgrep(eap);
2092 	return;
2093     }
2094 
2095     autowrite_all();
2096     name = get_mef_name();
2097     if (name == NULL)
2098 	return;
2099     mch_remove(name);	    /* in case it's not unique */
2100 
2101     /*
2102      * If 'shellpipe' empty: don't redirect to 'errorfile'.
2103      */
2104     len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2105     if (*p_sp != NUL)
2106 	len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(name) + 3;
2107     cmd = alloc(len);
2108     if (cmd == NULL)
2109 	return;
2110     sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2111 							       (char *)p_shq);
2112     if (*p_sp != NUL)
2113 	append_redir(cmd, p_sp, name);
2114     /*
2115      * Output a newline if there's something else than the :make command that
2116      * was typed (in which case the cursor is in column 0).
2117      */
2118     if (msg_col == 0)
2119 	msg_didout = FALSE;
2120     msg_start();
2121     MSG_PUTS(":!");
2122     msg_outtrans(cmd);		/* show what we are doing */
2123 
2124     /* let the shell know if we are redirecting output or not */
2125     do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2126 
2127 #ifdef AMIGA
2128     out_flush();
2129 		/* read window status report and redraw before message */
2130     (void)char_avail();
2131 #endif
2132 
2133     if (qf_init(name, eap->cmdidx != CMD_make ? p_gefm : p_efm,
2134 					       eap->cmdidx != CMD_grepadd) > 0
2135 	    && !eap->forceit)
2136 	qf_jump(0, 0, FALSE);		/* display first error */
2137 
2138     mch_remove(name);
2139     vim_free(name);
2140     vim_free(cmd);
2141 }
2142 
2143 /*
2144  * Return the name for the errorfile, in allocated memory.
2145  * Find a new unique name when 'makeef' contains "##".
2146  * Returns NULL for error.
2147  */
2148     static char_u *
2149 get_mef_name()
2150 {
2151     char_u	*p;
2152     char_u	*name;
2153     static int	start = -1;
2154     static int	off = 0;
2155 #ifdef HAVE_LSTAT
2156     struct stat	sb;
2157 #endif
2158 
2159     if (*p_mef == NUL)
2160     {
2161 	name = vim_tempname('e');
2162 	if (name == NULL)
2163 	    EMSG(_(e_notmp));
2164 	return name;
2165     }
2166 
2167     for (p = p_mef; *p; ++p)
2168 	if (p[0] == '#' && p[1] == '#')
2169 	    break;
2170 
2171     if (*p == NUL)
2172 	return vim_strsave(p_mef);
2173 
2174     /* Keep trying until the name doesn't exist yet. */
2175     for (;;)
2176     {
2177 	if (start == -1)
2178 	    start = mch_get_pid();
2179 	else
2180 	    off += 19;
2181 
2182 	name = alloc((unsigned)STRLEN(p_mef) + 30);
2183 	if (name == NULL)
2184 	    break;
2185 	STRCPY(name, p_mef);
2186 	sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2187 	STRCAT(name, p + 2);
2188 	if (mch_getperm(name) < 0
2189 #ifdef HAVE_LSTAT
2190 		    /* Don't accept a symbolic link, its a security risk. */
2191 		    && mch_lstat((char *)name, &sb) < 0
2192 #endif
2193 		)
2194 	    break;
2195 	vim_free(name);
2196     }
2197     return name;
2198 }
2199 
2200 /*
2201  * ":cc", ":crewind", ":cfirst" and ":clast".
2202  */
2203     void
2204 ex_cc(eap)
2205     exarg_T	*eap;
2206 {
2207     qf_jump(0,
2208 	    eap->addr_count > 0
2209 	    ? (int)eap->line2
2210 	    : eap->cmdidx == CMD_cc
2211 		? 0
2212 		: (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2213 		    ? 1
2214 		    : 32767,
2215 	    eap->forceit);
2216 }
2217 
2218 /*
2219  * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2220  */
2221     void
2222 ex_cnext(eap)
2223     exarg_T	*eap;
2224 {
2225     qf_jump(eap->cmdidx == CMD_cnext
2226 	    ? FORWARD
2227 	    : eap->cmdidx == CMD_cnfile
2228 		? FORWARD_FILE
2229 		: (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2230 		    ? BACKWARD_FILE
2231 		    : BACKWARD,
2232 	    eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2233 }
2234 
2235 /*
2236  * ":cfile" command.
2237  */
2238     void
2239 ex_cfile(eap)
2240     exarg_T	*eap;
2241 {
2242     if (*eap->arg != NUL)
2243 	set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
2244     if (qf_init(p_ef, p_efm, TRUE) > 0 && eap->cmdidx == CMD_cfile)
2245 	qf_jump(0, 0, eap->forceit);		/* display first error */
2246 }
2247 
2248 /*
2249  * ":vimgrep {pattern} file(s)"
2250  */
2251     void
2252 ex_vimgrep(eap)
2253     exarg_T	*eap;
2254 {
2255     regmmatch_T	regmatch;
2256     char_u	*save_cpo;
2257     int         fcount;
2258     char_u	**fnames;
2259     char_u      *s;
2260     char_u      *p;
2261     int		i;
2262     int         fi;
2263     struct qf_line *prevp = NULL;
2264     long	lnum;
2265     garray_T	ga;
2266     buf_T	*buf;
2267     int		duplicate_name = FALSE;
2268     int		using_dummy;
2269     int		found_match;
2270     int		first_match = TRUE;
2271 
2272     /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2273     save_cpo = p_cpo;
2274     p_cpo = empty_option;
2275 
2276     /* Get the search pattern: either white-separated or enclosed in // */
2277     regmatch.regprog = NULL;
2278     if (vim_isIDc(*eap->arg))
2279     {
2280 	s = eap->arg;
2281 	p = skiptowhite(s);
2282     }
2283     else
2284     {
2285 	s = eap->arg + 1;
2286 	p = skip_regexp(s, *eap->arg, TRUE, NULL);
2287 	if (*p != *eap->arg)
2288 	{
2289 	    EMSG(_("E682: Invalid search pattern or delimiter"));
2290 	    goto theend;
2291 	}
2292     }
2293     if (*p != NUL)
2294 	*p++ = NUL;
2295     regmatch.regprog = vim_regcomp(s, RE_MAGIC);
2296     if (regmatch.regprog == NULL)
2297 	goto theend;
2298     regmatch.rmm_ic = FALSE;
2299 
2300     p = skipwhite(p);
2301     if (*p == NUL)
2302     {
2303 	EMSG(_("E683: File name missing or invalid pattern"));
2304 	goto theend;
2305     }
2306 
2307     if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
2308 						|| qf_curlist == qf_listcount)
2309 	/* make place for a new list */
2310 	qf_new_list();
2311     else if (qf_lists[qf_curlist].qf_count > 0)
2312 	/* Adding to existing list, find last entry. */
2313 	for (prevp = qf_lists[qf_curlist].qf_start;
2314 			    prevp->qf_next != prevp; prevp = prevp->qf_next)
2315 	    ;
2316 
2317     /* parse the list of arguments */
2318     if (get_arglist(&ga, p) == FAIL)
2319 	goto theend;
2320     i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
2321 				       &fcount, &fnames, EW_FILE|EW_NOTFOUND);
2322     ga_clear(&ga);
2323     if (i == FAIL)
2324 	goto theend;
2325     if (fcount == 0)
2326     {
2327 	EMSG(_(e_nomatch));
2328 	goto theend;
2329     }
2330 
2331     for (fi = 0; fi < fcount && !got_int; ++fi)
2332     {
2333 	buf = buflist_findname_exp(fnames[fi]);
2334 	if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2335 	{
2336 	    /* Remember that a buffer with this name already exists. */
2337 	    duplicate_name = (buf != NULL);
2338 
2339 	    /* Load file into a buffer, so that 'fileencoding' is detected,
2340 	     * autocommands applied, etc. */
2341 	    buf = load_dummy_buffer(fnames[fi]);
2342 	    using_dummy = TRUE;
2343 	}
2344 	else
2345 	    /* Use existing, loaded buffer. */
2346 	    using_dummy = FALSE;
2347 	if (buf == NULL)
2348 	    smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2349 	else
2350 	{
2351 	    found_match = FALSE;
2352 	    for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
2353 	    {
2354 		if (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2355 							      (colnr_T)0) > 0)
2356 		{
2357 		    if (qf_add_entry(&prevp,
2358 				NULL,       /* dir */
2359 				fnames[fi],
2360 				ml_get_buf(buf,
2361 				     regmatch.startpos[0].lnum + lnum, FALSE),
2362 				regmatch.startpos[0].lnum + lnum,
2363 				regmatch.startpos[0].col + 1,
2364 				FALSE,      /* virt_col */
2365 				0,          /* nr */
2366 				0,          /* type */
2367 				TRUE        /* valid */
2368 				) == FAIL)
2369 		    {
2370 			got_int = TRUE;
2371 			break;
2372 		    }
2373 		    else
2374 			found_match = TRUE;
2375 		}
2376 		line_breakcheck();
2377 		if (got_int)
2378 		    break;
2379 	    }
2380 
2381 	    if (using_dummy)
2382 	    {
2383 		if (duplicate_name)
2384 		    /* Never keep a dummy buffer if there is another buffer
2385 		     * with the same name. */
2386 		    wipe_dummy_buffer(buf);
2387 		else if (!buf_hide(buf))
2388 		{
2389 		    /* When not hiding the buffer and no match was found we
2390 		     * don't need to remember the buffer, wipe it out.  If
2391 		     * there was a match and it wasn't the first one: only
2392 		     * unload the buffer. */
2393 		    if (!found_match)
2394 			wipe_dummy_buffer(buf);
2395 		    else if (!first_match)
2396 			unload_dummy_buffer(buf);
2397 		}
2398 	    }
2399 	    if (found_match)
2400 		first_match = FALSE;
2401 	}
2402     }
2403 
2404     FreeWild(fcount, fnames);
2405 
2406     qf_lists[qf_curlist].qf_nonevalid = FALSE;
2407     qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2408     qf_lists[qf_curlist].qf_index = 1;
2409 
2410 #ifdef FEAT_WINDOWS
2411     qf_update_buffer();
2412 #endif
2413 
2414     /* Jump to first match. */
2415     if (qf_lists[qf_curlist].qf_count > 0)
2416 	qf_jump(0, 0, FALSE);
2417     else
2418 	EMSG2(_(e_nomatch2), s);
2419 
2420 theend:
2421     vim_free(regmatch.regprog);
2422 
2423     /* Only resture 'cpo' when it wasn't set in the mean time. */
2424     if (p_cpo == empty_option)
2425 	p_cpo = save_cpo;
2426     else
2427 	free_string_option(save_cpo);
2428 }
2429 
2430 /*
2431  * Load file "fname" into a dummy buffer and return the buffer pointer.
2432  * Returns NULL if it fails.
2433  * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
2434  */
2435     static buf_T *
2436 load_dummy_buffer(fname)
2437     char_u	*fname;
2438 {
2439     buf_T	*newbuf;
2440     int		failed = TRUE;
2441 #ifdef FEAT_AUTOCMD
2442     aco_save_T	aco;
2443 #else
2444     buf_T	*old_curbuf = curbuf;
2445 #endif
2446 
2447     /* Allocate a buffer without putting it in the buffer list. */
2448     newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
2449     if (newbuf == NULL)
2450 	return NULL;
2451 
2452 #ifdef FEAT_AUTOCMD
2453     /* set curwin/curbuf to buf and save a few things */
2454     aucmd_prepbuf(&aco, newbuf);
2455 #else
2456     curbuf = newbuf;
2457     curwin->w_buffer = newbuf;
2458 #endif
2459 
2460     /* Need to set the filename for autocommands. */
2461     (void)setfname(curbuf, fname, NULL, FALSE);
2462 
2463     if (ml_open() == OK)
2464     {
2465 	/* Create swap file now to avoid the ATTENTION message. */
2466 	check_need_swap(TRUE);
2467 
2468 	/* Remove the "dummy" flag, otherwise autocommands may not
2469 	 * work. */
2470 	curbuf->b_flags &= ~BF_DUMMY;
2471 
2472 	if (readfile(fname, NULL,
2473 		    (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
2474 		    NULL, READ_NEW | READ_DUMMY) == OK
2475 		&& !(curbuf->b_flags & BF_NEW))
2476 	{
2477 	    failed = FALSE;
2478 	    if (curbuf != newbuf)
2479 	    {
2480 		/* Bloody autocommands changed the buffer! */
2481 		if (buf_valid(newbuf))
2482 		    wipe_buffer(newbuf, FALSE);
2483 		newbuf = curbuf;
2484 	    }
2485 	}
2486     }
2487 
2488 #ifdef FEAT_AUTOCMD
2489     /* restore curwin/curbuf and a few other things */
2490     aucmd_restbuf(&aco);
2491 #else
2492     curbuf = old_curbuf;
2493     curwin->w_buffer = old_curbuf;
2494 #endif
2495 
2496     if (!buf_valid(newbuf))
2497 	return NULL;
2498     if (failed)
2499     {
2500 	wipe_dummy_buffer(newbuf);
2501 	return NULL;
2502     }
2503     return newbuf;
2504 }
2505 
2506 /*
2507  * Wipe out the dummy buffer that load_dummy_buffer() created.
2508  */
2509     static void
2510 wipe_dummy_buffer(buf)
2511     buf_T	*buf;
2512 {
2513     if (curbuf != buf)		/* safety check */
2514 	wipe_buffer(buf, FALSE);
2515 }
2516 
2517 /*
2518  * Unload the dummy buffer that load_dummy_buffer() created.
2519  */
2520     static void
2521 unload_dummy_buffer(buf)
2522     buf_T	*buf;
2523 {
2524     if (curbuf != buf)		/* safety check */
2525 	close_buffer(NULL, buf, DOBUF_UNLOAD);
2526 }
2527 
2528 /*
2529  * ":[range]cbuffer [bufnr]" command.
2530  */
2531     void
2532 ex_cbuffer(eap)
2533     exarg_T   *eap;
2534 {
2535     buf_T	*buf = NULL;
2536 
2537     if (*eap->arg == NUL)
2538 	buf = curbuf;
2539     else if (*skipwhite(skipdigits(eap->arg)) == NUL)
2540 	buf = buflist_findnr(atoi((char *)eap->arg));
2541     if (buf == NULL)
2542 	EMSG(_(e_invarg));
2543     else if (buf->b_ml.ml_mfp == NULL)
2544 	EMSG(_("E681: Buffer is not loaded"));
2545     else
2546     {
2547 	if (eap->addr_count == 0)
2548 	{
2549 	    eap->line1 = 1;
2550 	    eap->line2 = buf->b_ml.ml_line_count;
2551 	}
2552 	if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
2553 		|| eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
2554 	    EMSG(_(e_invrange));
2555 	else
2556 	    qf_init_ext(NULL, buf, p_efm, TRUE, eap->line1, eap->line2);
2557     }
2558 }
2559 
2560 /*
2561  * ":helpgrep {pattern}"
2562  */
2563     void
2564 ex_helpgrep(eap)
2565     exarg_T	*eap;
2566 {
2567     regmatch_T	regmatch;
2568     char_u	*save_cpo;
2569     char_u	*p;
2570     int		fcount;
2571     char_u	**fnames;
2572     FILE	*fd;
2573     int		fi;
2574     struct qf_line *prevp = NULL;
2575     long	lnum;
2576 #ifdef FEAT_MULTI_LANG
2577     char_u	*lang;
2578 #endif
2579 
2580     /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2581     save_cpo = p_cpo;
2582     p_cpo = (char_u *)"";
2583 
2584 #ifdef FEAT_MULTI_LANG
2585     /* Check for a specified language */
2586     lang = check_help_lang(eap->arg);
2587 #endif
2588 
2589     regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
2590     regmatch.rm_ic = FALSE;
2591     if (regmatch.regprog != NULL)
2592     {
2593 	/* create a new quickfix list */
2594 	qf_new_list();
2595 
2596 	/* Go through all directories in 'runtimepath' */
2597 	p = p_rtp;
2598 	while (*p != NUL && !got_int)
2599 	{
2600 	    copy_option_part(&p, NameBuff, MAXPATHL, ",");
2601 
2602 	    /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
2603 	    add_pathsep(NameBuff);
2604 	    STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
2605 	    if (gen_expand_wildcards(1, &NameBuff, &fcount,
2606 					     &fnames, EW_FILE|EW_SILENT) == OK
2607 		    && fcount > 0)
2608 	    {
2609 		for (fi = 0; fi < fcount && !got_int; ++fi)
2610 		{
2611 #ifdef FEAT_MULTI_LANG
2612 		    /* Skip files for a different language. */
2613 		    if (lang != NULL
2614 			    && STRNICMP(lang, fnames[fi]
2615 					    + STRLEN(fnames[fi]) - 3, 2) != 0
2616 			    && !(STRNICMP(lang, "en", 2) == 0
2617 				&& STRNICMP("txt", fnames[fi]
2618 					   + STRLEN(fnames[fi]) - 3, 3) == 0))
2619 			    continue;
2620 #endif
2621 		    fd = fopen((char *)fnames[fi], "r");
2622 		    if (fd != NULL)
2623 		    {
2624 			lnum = 1;
2625 			while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
2626 			{
2627 			    if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
2628 			    {
2629 				int	l = STRLEN(IObuff);
2630 
2631 				/* remove trailing CR, LF, spaces, etc. */
2632 				while (l > 0 && IObuff[l - 1] <= ' ')
2633 				     IObuff[--l] = NUL;
2634 
2635 				if (qf_add_entry(&prevp,
2636 					    NULL,	/* dir */
2637 					    fnames[fi],
2638 					    IObuff,
2639 					    lnum,
2640 					    (int)(regmatch.startp[0] - IObuff)
2641 								+ 1, /* col */
2642 					    FALSE,	/* virt_col */
2643 					    0,		/* nr */
2644 					    1,		/* type */
2645 					    TRUE	/* valid */
2646 					    ) == FAIL)
2647 				{
2648 				    got_int = TRUE;
2649 				    break;
2650 				}
2651 			    }
2652 			    ++lnum;
2653 			    line_breakcheck();
2654 			}
2655 			fclose(fd);
2656 		    }
2657 		}
2658 		FreeWild(fcount, fnames);
2659 	    }
2660 	}
2661 	vim_free(regmatch.regprog);
2662 
2663 	qf_lists[qf_curlist].qf_nonevalid = FALSE;
2664 	qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2665 	qf_lists[qf_curlist].qf_index = 1;
2666     }
2667 
2668     p_cpo = save_cpo;
2669 
2670 #ifdef FEAT_WINDOWS
2671     qf_update_buffer();
2672 #endif
2673 
2674     /* Jump to first match. */
2675     if (qf_lists[qf_curlist].qf_count > 0)
2676 	qf_jump(0, 0, FALSE);
2677     else
2678 	EMSG2(_(e_nomatch2), eap->arg);
2679 }
2680 
2681 #endif /* FEAT_QUICKFIX */
2682