xref: /vim-8.2.3635/src/quickfix.c (revision 577fadfc)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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 /*
25  * For each error the next struct is allocated and linked in a list.
26  */
27 typedef struct qfline_S qfline_T;
28 struct qfline_S
29 {
30     qfline_T	*qf_next;	// pointer to next error in the list
31     qfline_T	*qf_prev;	// pointer to previous error in the list
32     linenr_T	qf_lnum;	// line number where the error occurred
33     int		qf_fnum;	// file number for the line
34     int		qf_col;		// column where the error occurred
35     int		qf_nr;		// error number
36     char_u	*qf_module;	// module name for this error
37     char_u	*qf_pattern;	// search pattern for the error
38     char_u	*qf_text;	// description of the error
39     char_u	qf_viscol;	// set to TRUE if qf_col is screen column
40     char_u	qf_cleared;	// set to TRUE if line has been deleted
41     char_u	qf_type;	// type of the error (mostly 'E'); 1 for
42 				// :helpgrep
43     char_u	qf_valid;	// valid error message detected
44 };
45 
46 /*
47  * There is a stack of error lists.
48  */
49 #define LISTCOUNT   10
50 #define INVALID_QFIDX (-1)
51 #define INVALID_QFBUFNR (0)
52 
53 /*
54  * Quickfix list type.
55  */
56 typedef enum
57 {
58     QFLT_QUICKFIX, // Quickfix list - global list
59     QFLT_LOCATION, // Location list - per window list
60     QFLT_INTERNAL  // Internal - Temporary list used by getqflist()/getloclist()
61 } qfltype_T;
62 
63 /*
64  * Quickfix/Location list definition
65  * Contains a list of entries (qfline_T). qf_start points to the first entry
66  * and qf_last points to the last entry. qf_count contains the list size.
67  *
68  * Usually the list contains one or more entries. But an empty list can be
69  * created using setqflist()/setloclist() with a title and/or user context
70  * information and entries can be added later using setqflist()/setloclist().
71  */
72 typedef struct qf_list_S
73 {
74     int_u	qf_id;		// Unique identifier for this list
75     qfltype_T	qfl_type;
76     qfline_T	*qf_start;	// pointer to the first error
77     qfline_T	*qf_last;	// pointer to the last error
78     qfline_T	*qf_ptr;	// pointer to the current error
79     int		qf_count;	// number of errors (0 means empty list)
80     int		qf_index;	// current index in the error list
81     int		qf_nonevalid;	// TRUE if not a single valid entry found
82     char_u	*qf_title;	// title derived from the command that created
83 				// the error list or set by setqflist
84     typval_T	*qf_ctx;	// context set by setqflist/setloclist
85 
86     struct dir_stack_T	*qf_dir_stack;
87     char_u		*qf_directory;
88     struct dir_stack_T	*qf_file_stack;
89     char_u		*qf_currfile;
90     int			qf_multiline;
91     int			qf_multiignore;
92     int			qf_multiscan;
93     long		qf_changedtick;
94 } qf_list_T;
95 
96 /*
97  * Quickfix/Location list stack definition
98  * Contains a list of quickfix/location lists (qf_list_T)
99  */
100 struct qf_info_S
101 {
102     // Count of references to this list. Used only for location lists.
103     // When a location list window reference this list, qf_refcount
104     // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
105     // reaches 0, the list is freed.
106     int		qf_refcount;
107     int		qf_listcount;	    // current number of lists
108     int		qf_curlist;	    // current error list
109     qf_list_T	qf_lists[LISTCOUNT];
110     qfltype_T	qfl_type;	    // type of list
111     int		qf_bufnr;	    // quickfix window buffer number
112 };
113 
114 static qf_info_T ql_info;	// global quickfix list
115 static int_u last_qf_id = 0;	// Last used quickfix list id
116 
117 #define FMT_PATTERNS 11		// maximum number of % recognized
118 
119 /*
120  * Structure used to hold the info of one part of 'errorformat'
121  */
122 typedef struct efm_S efm_T;
123 struct efm_S
124 {
125     regprog_T	    *prog;	// pre-formatted part of 'errorformat'
126     efm_T	    *next;	// pointer to next (NULL if last)
127     char_u	    addr[FMT_PATTERNS]; // indices of used % patterns
128     char_u	    prefix;	// prefix of this format line:
129 				//   'D' enter directory
130 				//   'X' leave directory
131 				//   'A' start of multi-line message
132 				//   'E' error message
133 				//   'W' warning message
134 				//   'I' informational message
135 				//   'C' continuation line
136 				//   'Z' end of multi-line message
137 				//   'G' general, unspecific message
138 				//   'P' push file (partial) message
139 				//   'Q' pop/quit file (partial) message
140 				//   'O' overread (partial) message
141     char_u	    flags;	// additional flags given in prefix
142 				//   '-' do not include this line
143 				//   '+' include whole line in message
144     int		    conthere;	// %> used
145 };
146 
147 // List of location lists to be deleted.
148 // Used to delay the deletion of locations lists by autocmds.
149 typedef struct qf_delq_S
150 {
151     struct qf_delq_S	*next;
152     qf_info_T		*qi;
153 } qf_delq_T;
154 static qf_delq_T *qf_delq_head = NULL;
155 
156 // Counter to prevent autocmds from freeing up location lists when they are
157 // still being used.
158 static int	quickfix_busy = 0;
159 
160 static efm_T	*fmt_start = NULL; // cached across qf_parse_line() calls
161 
162 static void	qf_new_list(qf_info_T *qi, char_u *qf_title);
163 static int	qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
164 static void	qf_free(qf_list_T *qfl);
165 static char_u	*qf_types(int, int);
166 static int	qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
167 static char_u	*qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
168 static char_u	*qf_pop_dir(struct dir_stack_T **);
169 static char_u	*qf_guess_filepath(qf_list_T *qfl, char_u *);
170 static void	qf_fmt_text(char_u *text, char_u *buf, int bufsize);
171 static int	qf_win_pos_update(qf_info_T *qi, int old_qf_index);
172 static win_T	*qf_find_win(qf_info_T *qi);
173 static buf_T	*qf_find_buf(qf_info_T *qi);
174 static void	qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
175 static void	qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last);
176 static buf_T	*load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
177 static void	wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
178 static void	unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
179 static qf_info_T *ll_get_or_alloc_list(win_T *);
180 
181 // Quickfix window check helper macro
182 #define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
183 // Location list window check helper macro
184 #define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
185 
186 // Quickfix and location list stack check helper macros
187 #define IS_QF_STACK(qi)		(qi->qfl_type == QFLT_QUICKFIX)
188 #define IS_LL_STACK(qi)		(qi->qfl_type == QFLT_LOCATION)
189 #define IS_QF_LIST(qfl)		(qfl->qfl_type == QFLT_QUICKFIX)
190 #define IS_LL_LIST(qfl)		(qfl->qfl_type == QFLT_LOCATION)
191 
192 /*
193  * Return location list for window 'wp'
194  * For location list window, return the referenced location list
195  */
196 #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
197 
198 // Macro to loop through all the items in a quickfix list
199 // Quickfix item index starts from 1, so i below starts at 1
200 #define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
201 		    for (i = 1, qfp = qfl->qf_start; \
202 			    !got_int && i <= qfl->qf_count && qfp != NULL; \
203 			    ++i, qfp = qfp->qf_next)
204 
205 /*
206  * Looking up a buffer can be slow if there are many.  Remember the last one
207  * to make this a lot faster if there are multiple matches in the same file.
208  */
209 static char_u   *qf_last_bufname = NULL;
210 static bufref_T  qf_last_bufref = {NULL, 0, 0};
211 
212 static char	*e_loc_list_changed =
213 				 N_("E926: Current location list was changed");
214 
215 /*
216  * Maximum number of bytes allowed per line while reading a errorfile.
217  */
218 #define LINE_MAXLEN 4096
219 
220 static struct fmtpattern
221 {
222     char_u	convchar;
223     char	*pattern;
224 } fmt_pat[FMT_PATTERNS] =
225     {
226 	{'f', ".\\+"},	    // only used when at end
227 	{'n', "\\d\\+"},
228 	{'l', "\\d\\+"},
229 	{'c', "\\d\\+"},
230 	{'t', "."},
231 	{'m', ".\\+"},
232 	{'r', ".*"},
233 	{'p', "[- 	.]*"},
234 	{'v', "\\d\\+"},
235 	{'s', ".\\+"},
236 	{'o', ".\\+"}
237     };
238 
239 /*
240  * Convert an errorformat pattern to a regular expression pattern.
241  * See fmt_pat definition above for the list of supported patterns.  The
242  * pattern specifier is supplied in "efmpat".  The converted pattern is stored
243  * in "regpat".  Returns a pointer to the location after the pattern.
244  */
245     static char_u *
246 efmpat_to_regpat(
247 	char_u	*efmpat,
248 	char_u	*regpat,
249 	efm_T	*efminfo,
250 	int	idx,
251 	int	round)
252 {
253     char_u	*srcptr;
254 
255     if (efminfo->addr[idx])
256     {
257 	// Each errorformat pattern can occur only once
258 	semsg(_("E372: Too many %%%c in format string"), *efmpat);
259 	return NULL;
260     }
261     if ((idx && idx < 6
262 		&& vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
263 	    || (idx == 6
264 		&& vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
265     {
266 	semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
267 	return NULL;
268     }
269     efminfo->addr[idx] = (char_u)++round;
270     *regpat++ = '\\';
271     *regpat++ = '(';
272 #ifdef BACKSLASH_IN_FILENAME
273     if (*efmpat == 'f')
274     {
275 	// Also match "c:" in the file name, even when
276 	// checking for a colon next: "%f:".
277 	// "\%(\a:\)\="
278 	STRCPY(regpat, "\\%(\\a:\\)\\=");
279 	regpat += 10;
280     }
281 #endif
282     if (*efmpat == 'f' && efmpat[1] != NUL)
283     {
284 	if (efmpat[1] != '\\' && efmpat[1] != '%')
285 	{
286 	    // A file name may contain spaces, but this isn't
287 	    // in "\f".  For "%f:%l:%m" there may be a ":" in
288 	    // the file name.  Use ".\{-1,}x" instead (x is
289 	    // the next character), the requirement that :999:
290 	    // follows should work.
291 	    STRCPY(regpat, ".\\{-1,}");
292 	    regpat += 7;
293 	}
294 	else
295 	{
296 	    // File name followed by '\\' or '%': include as
297 	    // many file name chars as possible.
298 	    STRCPY(regpat, "\\f\\+");
299 	    regpat += 4;
300 	}
301     }
302     else
303     {
304 	srcptr = (char_u *)fmt_pat[idx].pattern;
305 	while ((*regpat = *srcptr++) != NUL)
306 	    ++regpat;
307     }
308     *regpat++ = '\\';
309     *regpat++ = ')';
310 
311     return regpat;
312 }
313 
314 /*
315  * Convert a scanf like format in 'errorformat' to a regular expression.
316  * Returns a pointer to the location after the pattern.
317  */
318     static char_u *
319 scanf_fmt_to_regpat(
320 	char_u	**pefmp,
321 	char_u	*efm,
322 	int	len,
323 	char_u	*regpat)
324 {
325     char_u	*efmp = *pefmp;
326 
327     if (*efmp == '[' || *efmp == '\\')
328     {
329 	if ((*regpat++ = *efmp) == '[')	// %*[^a-z0-9] etc.
330 	{
331 	    if (efmp[1] == '^')
332 		*regpat++ = *++efmp;
333 	    if (efmp < efm + len)
334 	    {
335 		*regpat++ = *++efmp;	    // could be ']'
336 		while (efmp < efm + len
337 			&& (*regpat++ = *++efmp) != ']')
338 		    // skip ;
339 		if (efmp == efm + len)
340 		{
341 		    emsg(_("E374: Missing ] in format string"));
342 		    return NULL;
343 		}
344 	    }
345 	}
346 	else if (efmp < efm + len)	// %*\D, %*\s etc.
347 	    *regpat++ = *++efmp;
348 	*regpat++ = '\\';
349 	*regpat++ = '+';
350     }
351     else
352     {
353 	// TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
354 	semsg(_("E375: Unsupported %%%c in format string"), *efmp);
355 	return NULL;
356     }
357 
358     *pefmp = efmp;
359 
360     return regpat;
361 }
362 
363 /*
364  * Analyze/parse an errorformat prefix.
365  */
366     static char_u *
367 efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
368 {
369     if (vim_strchr((char_u *)"+-", *efmp) != NULL)
370 	efminfo->flags = *efmp++;
371     if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
372 	efminfo->prefix = *efmp;
373     else
374     {
375 	semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
376 	return NULL;
377     }
378 
379     return efmp;
380 }
381 
382 /*
383  * Converts a 'errorformat' string part in 'efm' to a regular expression
384  * pattern.  The resulting regex pattern is returned in "regpat". Additional
385  * information about the 'erroformat' pattern is returned in "fmt_ptr".
386  * Returns OK or FAIL.
387  */
388     static int
389 efm_to_regpat(
390 	char_u	*efm,
391 	int	len,
392 	efm_T	*fmt_ptr,
393 	char_u	*regpat)
394 {
395     char_u	*ptr;
396     char_u	*efmp;
397     int		round;
398     int		idx = 0;
399 
400     // Build a regexp pattern for a 'errorformat' option part
401     ptr = regpat;
402     *ptr++ = '^';
403     round = 0;
404     for (efmp = efm; efmp < efm + len; ++efmp)
405     {
406 	if (*efmp == '%')
407 	{
408 	    ++efmp;
409 	    for (idx = 0; idx < FMT_PATTERNS; ++idx)
410 		if (fmt_pat[idx].convchar == *efmp)
411 		    break;
412 	    if (idx < FMT_PATTERNS)
413 	    {
414 		ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
415 		if (ptr == NULL)
416 		    return FAIL;
417 		round++;
418 	    }
419 	    else if (*efmp == '*')
420 	    {
421 		++efmp;
422 		ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
423 		if (ptr == NULL)
424 		    return FAIL;
425 	    }
426 	    else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
427 		*ptr++ = *efmp;		// regexp magic characters
428 	    else if (*efmp == '#')
429 		*ptr++ = '*';
430 	    else if (*efmp == '>')
431 		fmt_ptr->conthere = TRUE;
432 	    else if (efmp == efm + 1)		// analyse prefix
433 	    {
434 		// prefix is allowed only at the beginning of the errorformat
435 		// option part
436 		efmp = efm_analyze_prefix(efmp, fmt_ptr);
437 		if (efmp == NULL)
438 		    return FAIL;
439 	    }
440 	    else
441 	    {
442 		semsg(_("E377: Invalid %%%c in format string"), *efmp);
443 		return FAIL;
444 	    }
445 	}
446 	else			// copy normal character
447 	{
448 	    if (*efmp == '\\' && efmp + 1 < efm + len)
449 		++efmp;
450 	    else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
451 		*ptr++ = '\\';	// escape regexp atoms
452 	    if (*efmp)
453 		*ptr++ = *efmp;
454 	}
455     }
456     *ptr++ = '$';
457     *ptr = NUL;
458 
459     return OK;
460 }
461 
462 /*
463  * Free the 'errorformat' information list
464  */
465     static void
466 free_efm_list(efm_T **efm_first)
467 {
468     efm_T *efm_ptr;
469 
470     for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
471     {
472 	*efm_first = efm_ptr->next;
473 	vim_regfree(efm_ptr->prog);
474 	vim_free(efm_ptr);
475     }
476     fmt_start = NULL;
477 }
478 
479 /*
480  * Compute the size of the buffer used to convert a 'errorformat' pattern into
481  * a regular expression pattern.
482  */
483     static int
484 efm_regpat_bufsz(char_u *efm)
485 {
486     int sz;
487     int i;
488 
489     sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
490     for (i = FMT_PATTERNS; i > 0; )
491 	sz += (int)STRLEN(fmt_pat[--i].pattern);
492 #ifdef BACKSLASH_IN_FILENAME
493     sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
494 #else
495     sz += 2; // "%f" can become two chars longer
496 #endif
497 
498     return sz;
499 }
500 
501 /*
502  * Return the length of a 'errorformat' option part (separated by ",").
503  */
504     static int
505 efm_option_part_len(char_u *efm)
506 {
507     int len;
508 
509     for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
510 	if (efm[len] == '\\' && efm[len + 1] != NUL)
511 	    ++len;
512 
513     return len;
514 }
515 
516 /*
517  * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
518  * are parsed and converted to regular expressions. Returns information about
519  * the parsed 'errorformat' option.
520  */
521     static efm_T *
522 parse_efm_option(char_u *efm)
523 {
524     efm_T	*fmt_ptr = NULL;
525     efm_T	*fmt_first = NULL;
526     efm_T	*fmt_last = NULL;
527     char_u	*fmtstr = NULL;
528     int		len;
529     int		sz;
530 
531     // Each part of the format string is copied and modified from errorformat
532     // to regex prog.  Only a few % characters are allowed.
533 
534     // Get some space to modify the format string into.
535     sz = efm_regpat_bufsz(efm);
536     if ((fmtstr = alloc(sz)) == NULL)
537 	goto parse_efm_error;
538 
539     while (efm[0] != NUL)
540     {
541 	// Allocate a new eformat structure and put it at the end of the list
542 	fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
543 	if (fmt_ptr == NULL)
544 	    goto parse_efm_error;
545 	if (fmt_first == NULL)	    // first one
546 	    fmt_first = fmt_ptr;
547 	else
548 	    fmt_last->next = fmt_ptr;
549 	fmt_last = fmt_ptr;
550 
551 	// Isolate one part in the 'errorformat' option
552 	len = efm_option_part_len(efm);
553 
554 	if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
555 	    goto parse_efm_error;
556 	if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
557 	    goto parse_efm_error;
558 	// Advance to next part
559 	efm = skip_to_option_part(efm + len);	// skip comma and spaces
560     }
561 
562     if (fmt_first == NULL)	// nothing found
563 	emsg(_("E378: 'errorformat' contains no pattern"));
564 
565     goto parse_efm_end;
566 
567 parse_efm_error:
568     free_efm_list(&fmt_first);
569 
570 parse_efm_end:
571     vim_free(fmtstr);
572 
573     return fmt_first;
574 }
575 
576 enum {
577     QF_FAIL = 0,
578     QF_OK = 1,
579     QF_END_OF_INPUT = 2,
580     QF_NOMEM = 3,
581     QF_IGNORE_LINE = 4,
582     QF_MULTISCAN = 5,
583 };
584 
585 /*
586  * State information used to parse lines and add entries to a quickfix/location
587  * list.
588  */
589 typedef struct {
590     char_u	*linebuf;
591     int		linelen;
592     char_u	*growbuf;
593     int		growbufsiz;
594     FILE	*fd;
595     typval_T	*tv;
596     char_u	*p_str;
597     listitem_T	*p_li;
598     buf_T	*buf;
599     linenr_T	buflnum;
600     linenr_T	lnumlast;
601     vimconv_T	vc;
602 } qfstate_T;
603 
604 /*
605  * Allocate more memory for the line buffer used for parsing lines.
606  */
607     static char_u *
608 qf_grow_linebuf(qfstate_T *state, int newsz)
609 {
610     char_u	*p;
611 
612     // If the line exceeds LINE_MAXLEN exclude the last
613     // byte since it's not a NL character.
614     state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
615     if (state->growbuf == NULL)
616     {
617 	state->growbuf = alloc(state->linelen + 1);
618 	if (state->growbuf == NULL)
619 	    return NULL;
620 	state->growbufsiz = state->linelen;
621     }
622     else if (state->linelen > state->growbufsiz)
623     {
624 	if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
625 	    return NULL;
626 	state->growbuf = p;
627 	state->growbufsiz = state->linelen;
628     }
629     return state->growbuf;
630 }
631 
632 /*
633  * Get the next string (separated by newline) from state->p_str.
634  */
635     static int
636 qf_get_next_str_line(qfstate_T *state)
637 {
638     // Get the next line from the supplied string
639     char_u	*p_str = state->p_str;
640     char_u	*p;
641     int		len;
642 
643     if (*p_str == NUL) // Reached the end of the string
644 	return QF_END_OF_INPUT;
645 
646     p = vim_strchr(p_str, '\n');
647     if (p != NULL)
648 	len = (int)(p - p_str) + 1;
649     else
650 	len = (int)STRLEN(p_str);
651 
652     if (len > IOSIZE - 2)
653     {
654 	state->linebuf = qf_grow_linebuf(state, len);
655 	if (state->linebuf == NULL)
656 	    return QF_NOMEM;
657     }
658     else
659     {
660 	state->linebuf = IObuff;
661 	state->linelen = len;
662     }
663     vim_strncpy(state->linebuf, p_str, state->linelen);
664 
665     // Increment using len in order to discard the rest of the
666     // line if it exceeds LINE_MAXLEN.
667     p_str += len;
668     state->p_str = p_str;
669 
670     return QF_OK;
671 }
672 
673 /*
674  * Get the next string from state->p_Li.
675  */
676     static int
677 qf_get_next_list_line(qfstate_T *state)
678 {
679     listitem_T	*p_li = state->p_li;
680     int		len;
681 
682     while (p_li != NULL
683 	    && (p_li->li_tv.v_type != VAR_STRING
684 		|| p_li->li_tv.vval.v_string == NULL))
685 	p_li = p_li->li_next;	// Skip non-string items
686 
687     if (p_li == NULL)		// End of the list
688     {
689 	state->p_li = NULL;
690 	return QF_END_OF_INPUT;
691     }
692 
693     len = (int)STRLEN(p_li->li_tv.vval.v_string);
694     if (len > IOSIZE - 2)
695     {
696 	state->linebuf = qf_grow_linebuf(state, len);
697 	if (state->linebuf == NULL)
698 	    return QF_NOMEM;
699     }
700     else
701     {
702 	state->linebuf = IObuff;
703 	state->linelen = len;
704     }
705 
706     vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
707 
708     state->p_li = p_li->li_next;	// next item
709     return QF_OK;
710 }
711 
712 /*
713  * Get the next string from state->buf.
714  */
715     static int
716 qf_get_next_buf_line(qfstate_T *state)
717 {
718     char_u	*p_buf = NULL;
719     int		len;
720 
721     // Get the next line from the supplied buffer
722     if (state->buflnum > state->lnumlast)
723 	return QF_END_OF_INPUT;
724 
725     p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
726     state->buflnum += 1;
727 
728     len = (int)STRLEN(p_buf);
729     if (len > IOSIZE - 2)
730     {
731 	state->linebuf = qf_grow_linebuf(state, len);
732 	if (state->linebuf == NULL)
733 	    return QF_NOMEM;
734     }
735     else
736     {
737 	state->linebuf = IObuff;
738 	state->linelen = len;
739     }
740     vim_strncpy(state->linebuf, p_buf, state->linelen);
741 
742     return QF_OK;
743 }
744 
745 /*
746  * Get the next string from file state->fd.
747  */
748     static int
749 qf_get_next_file_line(qfstate_T *state)
750 {
751     int	    discard;
752     int	    growbuflen;
753 
754     if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
755 	return QF_END_OF_INPUT;
756 
757     discard = FALSE;
758     state->linelen = (int)STRLEN(IObuff);
759     if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
760     {
761 	// The current line exceeds IObuff, continue reading using
762 	// growbuf until EOL or LINE_MAXLEN bytes is read.
763 	if (state->growbuf == NULL)
764 	{
765 	    state->growbufsiz = 2 * (IOSIZE - 1);
766 	    state->growbuf = alloc(state->growbufsiz);
767 	    if (state->growbuf == NULL)
768 		return QF_NOMEM;
769 	}
770 
771 	// Copy the read part of the line, excluding null-terminator
772 	memcpy(state->growbuf, IObuff, IOSIZE - 1);
773 	growbuflen = state->linelen;
774 
775 	for (;;)
776 	{
777 	    char_u	*p;
778 
779 	    if (fgets((char *)state->growbuf + growbuflen,
780 			state->growbufsiz - growbuflen, state->fd) == NULL)
781 		break;
782 	    state->linelen = (int)STRLEN(state->growbuf + growbuflen);
783 	    growbuflen += state->linelen;
784 	    if ((state->growbuf)[growbuflen - 1] == '\n')
785 		break;
786 	    if (state->growbufsiz == LINE_MAXLEN)
787 	    {
788 		discard = TRUE;
789 		break;
790 	    }
791 
792 	    state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
793 		? 2 * state->growbufsiz : LINE_MAXLEN;
794 	    if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
795 		return QF_NOMEM;
796 	    state->growbuf = p;
797 	}
798 
799 	while (discard)
800 	{
801 	    // The current line is longer than LINE_MAXLEN, continue
802 	    // reading but discard everything until EOL or EOF is
803 	    // reached.
804 	    if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
805 		    || (int)STRLEN(IObuff) < IOSIZE - 1
806 		    || IObuff[IOSIZE - 1] == '\n')
807 		break;
808 	}
809 
810 	state->linebuf = state->growbuf;
811 	state->linelen = growbuflen;
812     }
813     else
814 	state->linebuf = IObuff;
815 
816     // Convert a line if it contains a non-ASCII character.
817     if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
818     {
819 	char_u	*line;
820 
821 	line = string_convert(&state->vc, state->linebuf, &state->linelen);
822 	if (line != NULL)
823 	{
824 	    if (state->linelen < IOSIZE)
825 	    {
826 		STRCPY(state->linebuf, line);
827 		vim_free(line);
828 	    }
829 	    else
830 	    {
831 		vim_free(state->growbuf);
832 		state->linebuf = state->growbuf = line;
833 		state->growbufsiz = state->linelen < LINE_MAXLEN
834 						? state->linelen : LINE_MAXLEN;
835 	    }
836 	}
837     }
838 
839     return QF_OK;
840 }
841 
842 /*
843  * Get the next string from a file/buffer/list/string.
844  */
845     static int
846 qf_get_nextline(qfstate_T *state)
847 {
848     int status = QF_FAIL;
849 
850     if (state->fd == NULL)
851     {
852 	if (state->tv != NULL)
853 	{
854 	    if (state->tv->v_type == VAR_STRING)
855 		// Get the next line from the supplied string
856 		status = qf_get_next_str_line(state);
857 	    else if (state->tv->v_type == VAR_LIST)
858 		// Get the next line from the supplied list
859 		status = qf_get_next_list_line(state);
860 	}
861 	else
862 	    // Get the next line from the supplied buffer
863 	    status = qf_get_next_buf_line(state);
864     }
865     else
866 	// Get the next line from the supplied file
867 	status = qf_get_next_file_line(state);
868 
869     if (status != QF_OK)
870 	return status;
871 
872     // remove newline/CR from the line
873     if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
874     {
875 	state->linebuf[state->linelen - 1] = NUL;
876 #ifdef USE_CRNL
877 	if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
878 	    state->linebuf[state->linelen - 2] = NUL;
879 #endif
880     }
881 
882     remove_bom(state->linebuf);
883 
884     return QF_OK;
885 }
886 
887 typedef struct {
888     char_u	*namebuf;
889     char_u	*module;
890     char_u	*errmsg;
891     int		errmsglen;
892     long	lnum;
893     int		col;
894     char_u	use_viscol;
895     char_u	*pattern;
896     int		enr;
897     int		type;
898     int		valid;
899 } qffields_T;
900 
901 /*
902  * Parse the match for filename ('%f') pattern in regmatch.
903  * Return the matched value in "fields->namebuf".
904  */
905     static int
906 qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
907 {
908     int c;
909 
910     if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
911 	return QF_FAIL;
912 
913     // Expand ~/file and $HOME/file to full path.
914     c = *rmp->endp[midx];
915     *rmp->endp[midx] = NUL;
916     expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
917     *rmp->endp[midx] = c;
918 
919     // For separate filename patterns (%O, %P and %Q), the specified file
920     // should exist.
921     if (vim_strchr((char_u *)"OPQ", prefix) != NULL
922 	    && mch_getperm(fields->namebuf) == -1)
923 	return QF_FAIL;
924 
925     return QF_OK;
926 }
927 
928 /*
929  * Parse the match for error number ('%n') pattern in regmatch.
930  * Return the matched value in "fields->enr".
931  */
932     static int
933 qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
934 {
935     if (rmp->startp[midx] == NULL)
936 	return QF_FAIL;
937     fields->enr = (int)atol((char *)rmp->startp[midx]);
938     return QF_OK;
939 }
940 
941 /*
942  * Parse the match for line number (%l') pattern in regmatch.
943  * Return the matched value in "fields->lnum".
944  */
945     static int
946 qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
947 {
948     if (rmp->startp[midx] == NULL)
949 	return QF_FAIL;
950     fields->lnum = atol((char *)rmp->startp[midx]);
951     return QF_OK;
952 }
953 
954 /*
955  * Parse the match for column number ('%c') pattern in regmatch.
956  * Return the matched value in "fields->col".
957  */
958     static int
959 qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
960 {
961     if (rmp->startp[midx] == NULL)
962 	return QF_FAIL;
963     fields->col = (int)atol((char *)rmp->startp[midx]);
964     return QF_OK;
965 }
966 
967 /*
968  * Parse the match for error type ('%t') pattern in regmatch.
969  * Return the matched value in "fields->type".
970  */
971     static int
972 qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
973 {
974     if (rmp->startp[midx] == NULL)
975 	return QF_FAIL;
976     fields->type = *rmp->startp[midx];
977     return QF_OK;
978 }
979 
980 /*
981  * Parse the match for '%+' format pattern. The whole matching line is included
982  * in the error string.  Return the matched line in "fields->errmsg".
983  */
984     static int
985 qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
986 {
987     char_u	*p;
988 
989     if (linelen >= fields->errmsglen)
990     {
991 	// linelen + null terminator
992 	if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
993 	    return QF_NOMEM;
994 	fields->errmsg = p;
995 	fields->errmsglen = linelen + 1;
996     }
997     vim_strncpy(fields->errmsg, linebuf, linelen);
998     return QF_OK;
999 }
1000 
1001 /*
1002  * Parse the match for error message ('%m') pattern in regmatch.
1003  * Return the matched value in "fields->errmsg".
1004  */
1005     static int
1006 qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1007 {
1008     char_u	*p;
1009     int		len;
1010 
1011     if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1012 	return QF_FAIL;
1013     len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1014     if (len >= fields->errmsglen)
1015     {
1016 	// len + null terminator
1017 	if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1018 	    return QF_NOMEM;
1019 	fields->errmsg = p;
1020 	fields->errmsglen = len + 1;
1021     }
1022     vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1023     return QF_OK;
1024 }
1025 
1026 /*
1027  * Parse the match for rest of a single-line file message ('%r') pattern.
1028  * Return the matched value in "tail".
1029  */
1030     static int
1031 qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1032 {
1033     if (rmp->startp[midx] == NULL)
1034 	return QF_FAIL;
1035     *tail = rmp->startp[midx];
1036     return QF_OK;
1037 }
1038 
1039 /*
1040  * Parse the match for the pointer line ('%p') pattern in regmatch.
1041  * Return the matched value in "fields->col".
1042  */
1043     static int
1044 qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1045 {
1046     char_u	*match_ptr;
1047 
1048     if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1049 	return QF_FAIL;
1050     fields->col = 0;
1051     for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1052 								++match_ptr)
1053     {
1054 	++fields->col;
1055 	if (*match_ptr == TAB)
1056 	{
1057 	    fields->col += 7;
1058 	    fields->col -= fields->col % 8;
1059 	}
1060     }
1061     ++fields->col;
1062     fields->use_viscol = TRUE;
1063     return QF_OK;
1064 }
1065 
1066 /*
1067  * Parse the match for the virtual column number ('%v') pattern in regmatch.
1068  * Return the matched value in "fields->col".
1069  */
1070     static int
1071 qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1072 {
1073     if (rmp->startp[midx] == NULL)
1074 	return QF_FAIL;
1075     fields->col = (int)atol((char *)rmp->startp[midx]);
1076     fields->use_viscol = TRUE;
1077     return QF_OK;
1078 }
1079 
1080 /*
1081  * Parse the match for the search text ('%s') pattern in regmatch.
1082  * Return the matched value in "fields->pattern".
1083  */
1084     static int
1085 qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1086 {
1087     int		len;
1088 
1089     if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1090 	return QF_FAIL;
1091     len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1092     if (len > CMDBUFFSIZE - 5)
1093 	len = CMDBUFFSIZE - 5;
1094     STRCPY(fields->pattern, "^\\V");
1095     STRNCAT(fields->pattern, rmp->startp[midx], len);
1096     fields->pattern[len + 3] = '\\';
1097     fields->pattern[len + 4] = '$';
1098     fields->pattern[len + 5] = NUL;
1099     return QF_OK;
1100 }
1101 
1102 /*
1103  * Parse the match for the module ('%o') pattern in regmatch.
1104  * Return the matched value in "fields->module".
1105  */
1106     static int
1107 qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1108 {
1109     int		len;
1110 
1111     if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1112 	return QF_FAIL;
1113     len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1114     if (len > CMDBUFFSIZE)
1115 	len = CMDBUFFSIZE;
1116     STRNCAT(fields->module, rmp->startp[midx], len);
1117     return QF_OK;
1118 }
1119 
1120 /*
1121  * 'errorformat' format pattern parser functions.
1122  * The '%f' and '%r' formats are parsed differently from other formats.
1123  * See qf_parse_match() for details.
1124  */
1125 static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1126 {
1127     NULL,
1128     qf_parse_fmt_n,
1129     qf_parse_fmt_l,
1130     qf_parse_fmt_c,
1131     qf_parse_fmt_t,
1132     qf_parse_fmt_m,
1133     NULL,
1134     qf_parse_fmt_p,
1135     qf_parse_fmt_v,
1136     qf_parse_fmt_s,
1137     qf_parse_fmt_o
1138 };
1139 
1140 /*
1141  * Parse the error format pattern matches in "regmatch" and set the values in
1142  * "fields".  fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1143  * match.  Returns QF_OK if all the matches are successfully parsed. On
1144  * failure, returns QF_FAIL or QF_NOMEM.
1145  */
1146     static int
1147 qf_parse_match(
1148 	char_u		*linebuf,
1149 	int		linelen,
1150 	efm_T		*fmt_ptr,
1151 	regmatch_T	*regmatch,
1152 	qffields_T	*fields,
1153 	int		qf_multiline,
1154 	int		qf_multiscan,
1155 	char_u		**tail)
1156 {
1157     int		idx = fmt_ptr->prefix;
1158     int		i;
1159     int		midx;
1160     int		status;
1161 
1162     if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1163 	return QF_FAIL;
1164     if (vim_strchr((char_u *)"EWI", idx) != NULL)
1165 	fields->type = idx;
1166     else
1167 	fields->type = 0;
1168 
1169     // Extract error message data from matched line.
1170     // We check for an actual submatch, because "\[" and "\]" in
1171     // the 'errorformat' may cause the wrong submatch to be used.
1172     for (i = 0; i < FMT_PATTERNS; i++)
1173     {
1174 	status = QF_OK;
1175 	midx = (int)fmt_ptr->addr[i];
1176 	if (i == 0 && midx > 0)				// %f
1177 	    status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1178 	else if (i == 5)
1179 	{
1180 	    if (fmt_ptr->flags == '+' && !qf_multiscan)	// %+
1181 		status = qf_parse_fmt_plus(linebuf, linelen, fields);
1182 	    else if (midx > 0)				// %m
1183 		status = qf_parse_fmt_m(regmatch, midx, fields);
1184 	}
1185 	else if (i == 6 && midx > 0)			// %r
1186 	    status = qf_parse_fmt_r(regmatch, midx, tail);
1187 	else if (midx > 0)				// others
1188 	    status = (qf_parse_fmt[i])(regmatch, midx, fields);
1189 
1190 	if (status != QF_OK)
1191 	    return status;
1192     }
1193 
1194     return QF_OK;
1195 }
1196 
1197 /*
1198  * Parse an error line in 'linebuf' using a single error format string in
1199  * 'fmt_ptr->prog' and return the matching values in 'fields'.
1200  * Returns QF_OK if the efm format matches completely and the fields are
1201  * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1202  */
1203     static int
1204 qf_parse_get_fields(
1205 	char_u		*linebuf,
1206 	int		linelen,
1207 	efm_T		*fmt_ptr,
1208 	qffields_T	*fields,
1209 	int		qf_multiline,
1210 	int		qf_multiscan,
1211 	char_u		**tail)
1212 {
1213     regmatch_T	regmatch;
1214     int		status = QF_FAIL;
1215     int		r;
1216 
1217     if (qf_multiscan &&
1218 		vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1219 	return QF_FAIL;
1220 
1221     fields->namebuf[0] = NUL;
1222     fields->module[0] = NUL;
1223     fields->pattern[0] = NUL;
1224     if (!qf_multiscan)
1225 	fields->errmsg[0] = NUL;
1226     fields->lnum = 0;
1227     fields->col = 0;
1228     fields->use_viscol = FALSE;
1229     fields->enr = -1;
1230     fields->type = 0;
1231     *tail = NULL;
1232 
1233     // Always ignore case when looking for a matching error.
1234     regmatch.rm_ic = TRUE;
1235     regmatch.regprog = fmt_ptr->prog;
1236     r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1237     fmt_ptr->prog = regmatch.regprog;
1238     if (r)
1239 	status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1240 		fields, qf_multiline, qf_multiscan, tail);
1241 
1242     return status;
1243 }
1244 
1245 /*
1246  * Parse directory error format prefixes (%D and %X).
1247  * Push and pop directories from the directory stack when scanning directory
1248  * names.
1249  */
1250     static int
1251 qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1252 {
1253     if (idx == 'D')				// enter directory
1254     {
1255 	if (*fields->namebuf == NUL)
1256 	{
1257 	    emsg(_("E379: Missing or empty directory name"));
1258 	    return QF_FAIL;
1259 	}
1260 	qfl->qf_directory =
1261 	    qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1262 	if (qfl->qf_directory == NULL)
1263 	    return QF_FAIL;
1264     }
1265     else if (idx == 'X')			// leave directory
1266 	qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1267 
1268     return QF_OK;
1269 }
1270 
1271 /*
1272  * Parse global file name error format prefixes (%O, %P and %Q).
1273  */
1274     static int
1275 qf_parse_file_pfx(
1276 	int idx,
1277 	qffields_T *fields,
1278 	qf_list_T *qfl,
1279 	char_u *tail)
1280 {
1281     fields->valid = FALSE;
1282     if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1283     {
1284 	if (*fields->namebuf && idx == 'P')
1285 	    qfl->qf_currfile =
1286 		qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1287 	else if (idx == 'Q')
1288 	    qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1289 	*fields->namebuf = NUL;
1290 	if (tail && *tail)
1291 	{
1292 	    STRMOVE(IObuff, skipwhite(tail));
1293 	    qfl->qf_multiscan = TRUE;
1294 	    return QF_MULTISCAN;
1295 	}
1296     }
1297 
1298     return QF_OK;
1299 }
1300 
1301 /*
1302  * Parse a non-error line (a line which doesn't match any of the error
1303  * format in 'efm').
1304  */
1305     static int
1306 qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1307 {
1308     char_u	*p;
1309 
1310     fields->namebuf[0] = NUL;	// no match found, remove file name
1311     fields->lnum = 0;		// don't jump to this line
1312     fields->valid = FALSE;
1313     if (linelen >= fields->errmsglen)
1314     {
1315 	// linelen + null terminator
1316 	if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1317 	    return QF_NOMEM;
1318 	fields->errmsg = p;
1319 	fields->errmsglen = linelen + 1;
1320     }
1321     // copy whole line to error message
1322     vim_strncpy(fields->errmsg, linebuf, linelen);
1323 
1324     return QF_OK;
1325 }
1326 
1327 /*
1328  * Parse multi-line error format prefixes (%C and %Z)
1329  */
1330     static int
1331 qf_parse_multiline_pfx(
1332 	int idx,
1333 	qf_list_T *qfl,
1334 	qffields_T *fields)
1335 {
1336     char_u		*ptr;
1337     int			len;
1338 
1339     if (!qfl->qf_multiignore)
1340     {
1341 	qfline_T *qfprev = qfl->qf_last;
1342 
1343 	if (qfprev == NULL)
1344 	    return QF_FAIL;
1345 	if (*fields->errmsg && !qfl->qf_multiignore)
1346 	{
1347 	    len = (int)STRLEN(qfprev->qf_text);
1348 	    if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1349 		    == NULL)
1350 		return QF_FAIL;
1351 	    STRCPY(ptr, qfprev->qf_text);
1352 	    vim_free(qfprev->qf_text);
1353 	    qfprev->qf_text = ptr;
1354 	    *(ptr += len) = '\n';
1355 	    STRCPY(++ptr, fields->errmsg);
1356 	}
1357 	if (qfprev->qf_nr == -1)
1358 	    qfprev->qf_nr = fields->enr;
1359 	if (vim_isprintc(fields->type) && !qfprev->qf_type)
1360 	    // only printable chars allowed
1361 	    qfprev->qf_type = fields->type;
1362 
1363 	if (!qfprev->qf_lnum)
1364 	    qfprev->qf_lnum = fields->lnum;
1365 	if (!qfprev->qf_col)
1366 	    qfprev->qf_col = fields->col;
1367 	qfprev->qf_viscol = fields->use_viscol;
1368 	if (!qfprev->qf_fnum)
1369 	    qfprev->qf_fnum = qf_get_fnum(qfl,
1370 		    qfl->qf_directory,
1371 		    *fields->namebuf || qfl->qf_directory != NULL
1372 		    ? fields->namebuf
1373 		    : qfl->qf_currfile != NULL && fields->valid
1374 		    ? qfl->qf_currfile : 0);
1375     }
1376     if (idx == 'Z')
1377 	qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1378     line_breakcheck();
1379 
1380     return QF_IGNORE_LINE;
1381 }
1382 
1383 /*
1384  * Parse a line and get the quickfix fields.
1385  * Return the QF_ status.
1386  */
1387     static int
1388 qf_parse_line(
1389 	qf_list_T	*qfl,
1390 	char_u		*linebuf,
1391 	int		linelen,
1392 	efm_T		*fmt_first,
1393 	qffields_T	*fields)
1394 {
1395     efm_T		*fmt_ptr;
1396     int			idx = 0;
1397     char_u		*tail = NULL;
1398     int			status;
1399 
1400 restofline:
1401     // If there was no %> item start at the first pattern
1402     if (fmt_start == NULL)
1403 	fmt_ptr = fmt_first;
1404     else
1405     {
1406 	// Otherwise start from the last used pattern
1407 	fmt_ptr = fmt_start;
1408 	fmt_start = NULL;
1409     }
1410 
1411     // Try to match each part of 'errorformat' until we find a complete
1412     // match or no match.
1413     fields->valid = TRUE;
1414     for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1415     {
1416 	idx = fmt_ptr->prefix;
1417 	status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1418 				qfl->qf_multiline, qfl->qf_multiscan, &tail);
1419 	if (status == QF_NOMEM)
1420 	    return status;
1421 	if (status == QF_OK)
1422 	    break;
1423     }
1424     qfl->qf_multiscan = FALSE;
1425 
1426     if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1427     {
1428 	if (fmt_ptr != NULL)
1429 	{
1430 	    // 'D' and 'X' directory specifiers
1431 	    status = qf_parse_dir_pfx(idx, fields, qfl);
1432 	    if (status != QF_OK)
1433 		return status;
1434 	}
1435 
1436 	status = qf_parse_line_nomatch(linebuf, linelen, fields);
1437 	if (status != QF_OK)
1438 	    return status;
1439 
1440 	if (fmt_ptr == NULL)
1441 	    qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1442     }
1443     else if (fmt_ptr != NULL)
1444     {
1445 	// honor %> item
1446 	if (fmt_ptr->conthere)
1447 	    fmt_start = fmt_ptr;
1448 
1449 	if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1450 	{
1451 	    qfl->qf_multiline = TRUE;	// start of a multi-line message
1452 	    qfl->qf_multiignore = FALSE;// reset continuation
1453 	}
1454 	else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1455 	{				// continuation of multi-line msg
1456 	    status = qf_parse_multiline_pfx(idx, qfl, fields);
1457 	    if (status != QF_OK)
1458 		return status;
1459 	}
1460 	else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1461 	{				// global file names
1462 	    status = qf_parse_file_pfx(idx, fields, qfl, tail);
1463 	    if (status == QF_MULTISCAN)
1464 		goto restofline;
1465 	}
1466 	if (fmt_ptr->flags == '-')	// generally exclude this line
1467 	{
1468 	    if (qfl->qf_multiline)
1469 		// also exclude continuation lines
1470 		qfl->qf_multiignore = TRUE;
1471 	    return QF_IGNORE_LINE;
1472 	}
1473     }
1474 
1475     return QF_OK;
1476 }
1477 
1478 /*
1479  * Returns TRUE if the specified quickfix/location stack is empty
1480  */
1481     static int
1482 qf_stack_empty(qf_info_T *qi)
1483 {
1484     return qi == NULL || qi->qf_listcount <= 0;
1485 }
1486 
1487 /*
1488  * Returns TRUE if the specified quickfix/location list is empty.
1489  */
1490     static int
1491 qf_list_empty(qf_list_T *qfl)
1492 {
1493     return qfl == NULL || qfl->qf_count <= 0;
1494 }
1495 
1496 /*
1497  * Return a pointer to a list in the specified quickfix stack
1498  */
1499     static qf_list_T *
1500 qf_get_list(qf_info_T *qi, int idx)
1501 {
1502     return &qi->qf_lists[idx];
1503 }
1504 
1505 /*
1506  * Allocate the fields used for parsing lines and populating a quickfix list.
1507  */
1508     static int
1509 qf_alloc_fields(qffields_T *pfields)
1510 {
1511     pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1512     pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1513     pfields->errmsglen = CMDBUFFSIZE + 1;
1514     pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1515     pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1516     if (pfields->namebuf == NULL || pfields->errmsg == NULL
1517 		|| pfields->pattern == NULL || pfields->module == NULL)
1518 	return FAIL;
1519 
1520     return OK;
1521 }
1522 
1523 /*
1524  * Free the fields used for parsing lines and populating a quickfix list.
1525  */
1526     static void
1527 qf_free_fields(qffields_T *pfields)
1528 {
1529     vim_free(pfields->namebuf);
1530     vim_free(pfields->module);
1531     vim_free(pfields->errmsg);
1532     vim_free(pfields->pattern);
1533 }
1534 
1535 /*
1536  * Setup the state information used for parsing lines and populating a
1537  * quickfix list.
1538  */
1539     static int
1540 qf_setup_state(
1541 	qfstate_T	*pstate,
1542 	char_u		*enc,
1543 	char_u		*efile,
1544 	typval_T	*tv,
1545 	buf_T		*buf,
1546 	linenr_T	lnumfirst,
1547 	linenr_T	lnumlast)
1548 {
1549     pstate->vc.vc_type = CONV_NONE;
1550     if (enc != NULL && *enc != NUL)
1551 	convert_setup(&pstate->vc, enc, p_enc);
1552 
1553     if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1554     {
1555 	semsg(_(e_openerrf), efile);
1556 	return FAIL;
1557     }
1558 
1559     if (tv != NULL)
1560     {
1561 	if (tv->v_type == VAR_STRING)
1562 	    pstate->p_str = tv->vval.v_string;
1563 	else if (tv->v_type == VAR_LIST)
1564 	    pstate->p_li = tv->vval.v_list->lv_first;
1565 	pstate->tv = tv;
1566     }
1567     pstate->buf = buf;
1568     pstate->buflnum = lnumfirst;
1569     pstate->lnumlast = lnumlast;
1570 
1571     return OK;
1572 }
1573 
1574 /*
1575  * Cleanup the state information used for parsing lines and populating a
1576  * quickfix list.
1577  */
1578     static void
1579 qf_cleanup_state(qfstate_T *pstate)
1580 {
1581     if (pstate->fd != NULL)
1582 	fclose(pstate->fd);
1583 
1584     vim_free(pstate->growbuf);
1585     if (pstate->vc.vc_type != CONV_NONE)
1586 	convert_setup(&pstate->vc, NULL, NULL);
1587 }
1588 
1589 /*
1590  * Process the next line from a file/buffer/list/string and add it
1591  * to the quickfix list 'qfl'.
1592  */
1593     static int
1594 qf_init_process_nextline(
1595 	qf_list_T	*qfl,
1596 	efm_T		*fmt_first,
1597 	qfstate_T	*state,
1598 	qffields_T	*fields)
1599 {
1600     int		    status;
1601 
1602     // Get the next line from a file/buffer/list/string
1603     status = qf_get_nextline(state);
1604     if (status != QF_OK)
1605 	return status;
1606 
1607     status = qf_parse_line(qfl, state->linebuf, state->linelen,
1608 	    fmt_first, fields);
1609     if (status != QF_OK)
1610 	return status;
1611 
1612     return qf_add_entry(qfl,
1613 		qfl->qf_directory,
1614 		(*fields->namebuf || qfl->qf_directory != NULL)
1615 		? fields->namebuf
1616 		: ((qfl->qf_currfile != NULL && fields->valid)
1617 		    ? qfl->qf_currfile : (char_u *)NULL),
1618 		fields->module,
1619 		0,
1620 		fields->errmsg,
1621 		fields->lnum,
1622 		fields->col,
1623 		fields->use_viscol,
1624 		fields->pattern,
1625 		fields->enr,
1626 		fields->type,
1627 		fields->valid);
1628 }
1629 
1630 /*
1631  * Read the errorfile "efile" into memory, line by line, building the error
1632  * list.
1633  * Alternative: when "efile" is NULL read errors from buffer "buf".
1634  * Alternative: when "tv" is not NULL get errors from the string or list.
1635  * Always use 'errorformat' from "buf" if there is a local value.
1636  * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1637  * Set the title of the list to "qf_title".
1638  * Return -1 for error, number of errors for success.
1639  */
1640     static int
1641 qf_init_ext(
1642     qf_info_T	    *qi,
1643     int		    qf_idx,
1644     char_u	    *efile,
1645     buf_T	    *buf,
1646     typval_T	    *tv,
1647     char_u	    *errorformat,
1648     int		    newlist,		// TRUE: start a new error list
1649     linenr_T	    lnumfirst,		// first line number to use
1650     linenr_T	    lnumlast,		// last line number to use
1651     char_u	    *qf_title,
1652     char_u	    *enc)
1653 {
1654     qf_list_T	    *qfl;
1655     qfstate_T	    state;
1656     qffields_T	    fields;
1657     qfline_T	    *old_last = NULL;
1658     int		    adding = FALSE;
1659     static efm_T    *fmt_first = NULL;
1660     char_u	    *efm;
1661     static char_u   *last_efm = NULL;
1662     int		    retval = -1;	// default: return error flag
1663     int		    status;
1664 
1665     // Do not used the cached buffer, it may have been wiped out.
1666     VIM_CLEAR(qf_last_bufname);
1667 
1668     vim_memset(&state, 0, sizeof(state));
1669     vim_memset(&fields, 0, sizeof(fields));
1670     if ((qf_alloc_fields(&fields) == FAIL) ||
1671 		(qf_setup_state(&state, enc, efile, tv, buf,
1672 					lnumfirst, lnumlast) == FAIL))
1673 	goto qf_init_end;
1674 
1675     if (newlist || qf_idx == qi->qf_listcount)
1676     {
1677 	// make place for a new list
1678 	qf_new_list(qi, qf_title);
1679 	qf_idx = qi->qf_curlist;
1680 	qfl = qf_get_list(qi, qf_idx);
1681     }
1682     else
1683     {
1684 	// Adding to existing list, use last entry.
1685 	adding = TRUE;
1686 	qfl = qf_get_list(qi, qf_idx);
1687 	if (!qf_list_empty(qfl))
1688 	    old_last = qfl->qf_last;
1689     }
1690 
1691     // Use the local value of 'errorformat' if it's set.
1692     if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
1693 	efm = buf->b_p_efm;
1694     else
1695 	efm = errorformat;
1696 
1697     // If the errorformat didn't change between calls, then reuse the
1698     // previously parsed values.
1699     if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1700     {
1701 	// free the previously parsed data
1702 	VIM_CLEAR(last_efm);
1703 	free_efm_list(&fmt_first);
1704 
1705 	// parse the current 'efm'
1706 	fmt_first = parse_efm_option(efm);
1707 	if (fmt_first != NULL)
1708 	    last_efm = vim_strsave(efm);
1709     }
1710 
1711     if (fmt_first == NULL)	// nothing found
1712 	goto error2;
1713 
1714     // got_int is reset here, because it was probably set when killing the
1715     // ":make" command, but we still want to read the errorfile then.
1716     got_int = FALSE;
1717 
1718     // Read the lines in the error file one by one.
1719     // Try to recognize one of the error formats in each line.
1720     while (!got_int)
1721     {
1722 	status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
1723 	if (status == QF_NOMEM)		// memory alloc failure
1724 	    goto qf_init_end;
1725 	if (status == QF_END_OF_INPUT)	// end of input
1726 	    break;
1727 	if (status == QF_FAIL)
1728 	    goto error2;
1729 
1730 	line_breakcheck();
1731     }
1732     if (state.fd == NULL || !ferror(state.fd))
1733     {
1734 	if (qfl->qf_index == 0)
1735 	{
1736 	    // no valid entry found
1737 	    qfl->qf_ptr = qfl->qf_start;
1738 	    qfl->qf_index = 1;
1739 	    qfl->qf_nonevalid = TRUE;
1740 	}
1741 	else
1742 	{
1743 	    qfl->qf_nonevalid = FALSE;
1744 	    if (qfl->qf_ptr == NULL)
1745 		qfl->qf_ptr = qfl->qf_start;
1746 	}
1747 	// return number of matches
1748 	retval = qfl->qf_count;
1749 	goto qf_init_end;
1750     }
1751     emsg(_(e_readerrf));
1752 error2:
1753     if (!adding)
1754     {
1755 	// Error when creating a new list. Free the new list
1756 	qf_free(qfl);
1757 	qi->qf_listcount--;
1758 	if (qi->qf_curlist > 0)
1759 	    --qi->qf_curlist;
1760     }
1761 qf_init_end:
1762     if (qf_idx == qi->qf_curlist)
1763 	qf_update_buffer(qi, old_last);
1764     qf_cleanup_state(&state);
1765     qf_free_fields(&fields);
1766 
1767     return retval;
1768 }
1769 
1770 /*
1771  * Read the errorfile "efile" into memory, line by line, building the error
1772  * list. Set the error list's title to qf_title.
1773  * Return -1 for error, number of errors for success.
1774  */
1775     int
1776 qf_init(win_T	    *wp,
1777 	char_u	    *efile,
1778 	char_u	    *errorformat,
1779 	int	    newlist,		// TRUE: start a new error list
1780 	char_u	    *qf_title,
1781 	char_u	    *enc)
1782 {
1783     qf_info_T	    *qi = &ql_info;
1784 
1785     if (wp != NULL)
1786     {
1787 	qi = ll_get_or_alloc_list(wp);
1788 	if (qi == NULL)
1789 	    return FAIL;
1790     }
1791 
1792     return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1793 	    newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1794 }
1795 
1796 /*
1797  * Set the title of the specified quickfix list. Frees the previous title.
1798  * Prepends ':' to the title.
1799  */
1800     static void
1801 qf_store_title(qf_list_T *qfl, char_u *title)
1802 {
1803     VIM_CLEAR(qfl->qf_title);
1804 
1805     if (title != NULL)
1806     {
1807 	char_u *p = alloc((int)STRLEN(title) + 2);
1808 
1809 	qfl->qf_title = p;
1810 	if (p != NULL)
1811 	    STRCPY(p, title);
1812     }
1813 }
1814 
1815 /*
1816  * The title of a quickfix/location list is set, by default, to the command
1817  * that created the quickfix list with the ":" prefix.
1818  * Create a quickfix list title string by prepending ":" to a user command.
1819  * Returns a pointer to a static buffer with the title.
1820  */
1821     static char_u *
1822 qf_cmdtitle(char_u *cmd)
1823 {
1824     static char_u qftitle_str[IOSIZE];
1825 
1826     vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1827     return qftitle_str;
1828 }
1829 
1830 /*
1831  * Return a pointer to the current list in the specified quickfix stack
1832  */
1833     static qf_list_T *
1834 qf_get_curlist(qf_info_T *qi)
1835 {
1836     return qf_get_list(qi, qi->qf_curlist);
1837 }
1838 
1839 /*
1840  * Prepare for adding a new quickfix list. If the current list is in the
1841  * middle of the stack, then all the following lists are freed and then
1842  * the new list is added.
1843  */
1844     static void
1845 qf_new_list(qf_info_T *qi, char_u *qf_title)
1846 {
1847     int		i;
1848     qf_list_T	*qfl;
1849 
1850     // If the current entry is not the last entry, delete entries beyond
1851     // the current entry.  This makes it possible to browse in a tree-like
1852     // way with ":grep'.
1853     while (qi->qf_listcount > qi->qf_curlist + 1)
1854 	qf_free(&qi->qf_lists[--qi->qf_listcount]);
1855 
1856     // When the stack is full, remove to oldest entry
1857     // Otherwise, add a new entry.
1858     if (qi->qf_listcount == LISTCOUNT)
1859     {
1860 	qf_free(&qi->qf_lists[0]);
1861 	for (i = 1; i < LISTCOUNT; ++i)
1862 	    qi->qf_lists[i - 1] = qi->qf_lists[i];
1863 	qi->qf_curlist = LISTCOUNT - 1;
1864     }
1865     else
1866 	qi->qf_curlist = qi->qf_listcount++;
1867     qfl = qf_get_curlist(qi);
1868     vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1869     qf_store_title(qfl, qf_title);
1870     qfl->qfl_type = qi->qfl_type;
1871     qfl->qf_id = ++last_qf_id;
1872 }
1873 
1874 /*
1875  * Queue location list stack delete request.
1876  */
1877     static void
1878 locstack_queue_delreq(qf_info_T *qi)
1879 {
1880     qf_delq_T	*q;
1881 
1882     q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1883     if (q != NULL)
1884     {
1885 	q->qi = qi;
1886 	q->next = qf_delq_head;
1887 	qf_delq_head = q;
1888     }
1889 }
1890 
1891 /*
1892  * Return the global quickfix stack window buffer number.
1893  */
1894     int
1895 qf_stack_get_bufnr(void)
1896 {
1897     return ql_info.qf_bufnr;
1898 }
1899 
1900 /*
1901  * Wipe the quickfix window buffer (if present) for the specified
1902  * quickfix/location list.
1903  */
1904     static void
1905 wipe_qf_buffer(qf_info_T *qi)
1906 {
1907     buf_T	*qfbuf;
1908 
1909     if (qi->qf_bufnr != INVALID_QFBUFNR)
1910     {
1911 	qfbuf = buflist_findnr(qi->qf_bufnr);
1912 	if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1913 	{
1914 	    // If the quickfix buffer is not loaded in any window, then
1915 	    // wipe the buffer.
1916 	    close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE);
1917 	    qi->qf_bufnr = INVALID_QFBUFNR;
1918 	}
1919     }
1920 }
1921 
1922 /*
1923  * Free a location list stack
1924  */
1925     static void
1926 ll_free_all(qf_info_T **pqi)
1927 {
1928     int		i;
1929     qf_info_T	*qi;
1930 
1931     qi = *pqi;
1932     if (qi == NULL)
1933 	return;
1934     *pqi = NULL;	// Remove reference to this list
1935 
1936     // If the location list is still in use, then queue the delete request
1937     // to be processed later.
1938     if (quickfix_busy > 0)
1939     {
1940 	locstack_queue_delreq(qi);
1941 	return;
1942     }
1943 
1944     qi->qf_refcount--;
1945     if (qi->qf_refcount < 1)
1946     {
1947 	// No references to this location list.
1948 	// If the quickfix window buffer is loaded, then wipe it
1949 	wipe_qf_buffer(qi);
1950 
1951 	for (i = 0; i < qi->qf_listcount; ++i)
1952 	    qf_free(qf_get_list(qi, i));
1953 	vim_free(qi);
1954     }
1955 }
1956 
1957 /*
1958  * Free all the quickfix/location lists in the stack.
1959  */
1960     void
1961 qf_free_all(win_T *wp)
1962 {
1963     int		i;
1964     qf_info_T	*qi = &ql_info;
1965 
1966     if (wp != NULL)
1967     {
1968 	// location list
1969 	ll_free_all(&wp->w_llist);
1970 	ll_free_all(&wp->w_llist_ref);
1971     }
1972     else
1973 	// quickfix list
1974 	for (i = 0; i < qi->qf_listcount; ++i)
1975 	    qf_free(qf_get_list(qi, i));
1976 }
1977 
1978 /*
1979  * Delay freeing of location list stacks when the quickfix code is running.
1980  * Used to avoid problems with autocmds freeing location list stacks when the
1981  * quickfix code is still referencing the stack.
1982  * Must always call decr_quickfix_busy() exactly once after this.
1983  */
1984     static void
1985 incr_quickfix_busy(void)
1986 {
1987     quickfix_busy++;
1988 }
1989 
1990 /*
1991  * Safe to free location list stacks. Process any delayed delete requests.
1992  */
1993     static void
1994 decr_quickfix_busy(void)
1995 {
1996     if (--quickfix_busy == 0)
1997     {
1998 	// No longer referencing the location lists. Process all the pending
1999 	// delete requests.
2000 	while (qf_delq_head != NULL)
2001 	{
2002 	    qf_delq_T	*q = qf_delq_head;
2003 
2004 	    qf_delq_head = q->next;
2005 	    ll_free_all(&q->qi);
2006 	    vim_free(q);
2007 	}
2008     }
2009 #ifdef ABORT_ON_INTERNAL_ERROR
2010     if (quickfix_busy < 0)
2011     {
2012 	emsg("quickfix_busy has become negative");
2013 	abort();
2014     }
2015 #endif
2016 }
2017 
2018 #if defined(EXITFREE) || defined(PROTO)
2019     void
2020 check_quickfix_busy(void)
2021 {
2022     if (quickfix_busy != 0)
2023     {
2024 	semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
2025 # ifdef ABORT_ON_INTERNAL_ERROR
2026 	abort();
2027 # endif
2028     }
2029 }
2030 #endif
2031 
2032 /*
2033  * Add an entry to the end of the list of errors.
2034  * Returns QF_OK or QF_FAIL.
2035  */
2036     static int
2037 qf_add_entry(
2038     qf_list_T	*qfl,		// quickfix list entry
2039     char_u	*dir,		// optional directory name
2040     char_u	*fname,		// file name or NULL
2041     char_u	*module,	// module name or NULL
2042     int		bufnum,		// buffer number or zero
2043     char_u	*mesg,		// message
2044     long	lnum,		// line number
2045     int		col,		// column
2046     int		vis_col,	// using visual column
2047     char_u	*pattern,	// search pattern
2048     int		nr,		// error number
2049     int		type,		// type character
2050     int		valid)		// valid entry
2051 {
2052     qfline_T	*qfp;
2053     qfline_T	**lastp;	// pointer to qf_last or NULL
2054 
2055     if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
2056 	return QF_FAIL;
2057     if (bufnum != 0)
2058     {
2059 	buf_T *buf = buflist_findnr(bufnum);
2060 
2061 	qfp->qf_fnum = bufnum;
2062 	if (buf != NULL)
2063 	    buf->b_has_qf_entry |=
2064 		IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
2065     }
2066     else
2067 	qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
2068     if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2069     {
2070 	vim_free(qfp);
2071 	return QF_FAIL;
2072     }
2073     qfp->qf_lnum = lnum;
2074     qfp->qf_col = col;
2075     qfp->qf_viscol = vis_col;
2076     if (pattern == NULL || *pattern == NUL)
2077 	qfp->qf_pattern = NULL;
2078     else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2079     {
2080 	vim_free(qfp->qf_text);
2081 	vim_free(qfp);
2082 	return QF_FAIL;
2083     }
2084     if (module == NULL || *module == NUL)
2085 	qfp->qf_module = NULL;
2086     else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2087     {
2088 	vim_free(qfp->qf_text);
2089 	vim_free(qfp->qf_pattern);
2090 	vim_free(qfp);
2091 	return QF_FAIL;
2092     }
2093     qfp->qf_nr = nr;
2094     if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
2095 	type = 0;
2096     qfp->qf_type = type;
2097     qfp->qf_valid = valid;
2098 
2099     lastp = &qfl->qf_last;
2100     if (qf_list_empty(qfl))		// first element in the list
2101     {
2102 	qfl->qf_start = qfp;
2103 	qfl->qf_ptr = qfp;
2104 	qfl->qf_index = 0;
2105 	qfp->qf_prev = NULL;
2106     }
2107     else
2108     {
2109 	qfp->qf_prev = *lastp;
2110 	(*lastp)->qf_next = qfp;
2111     }
2112     qfp->qf_next = NULL;
2113     qfp->qf_cleared = FALSE;
2114     *lastp = qfp;
2115     ++qfl->qf_count;
2116     if (qfl->qf_index == 0 && qfp->qf_valid)	// first valid entry
2117     {
2118 	qfl->qf_index = qfl->qf_count;
2119 	qfl->qf_ptr = qfp;
2120     }
2121 
2122     return QF_OK;
2123 }
2124 
2125 /*
2126  * Allocate a new quickfix/location list stack
2127  */
2128     static qf_info_T *
2129 qf_alloc_stack(qfltype_T qfltype)
2130 {
2131     qf_info_T *qi;
2132 
2133     qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
2134     if (qi != NULL)
2135     {
2136 	qi->qf_refcount++;
2137 	qi->qfl_type = qfltype;
2138 	qi->qf_bufnr = INVALID_QFBUFNR;
2139     }
2140     return qi;
2141 }
2142 
2143 /*
2144  * Return the location list stack for window 'wp'.
2145  * If not present, allocate a location list stack
2146  */
2147     static qf_info_T *
2148 ll_get_or_alloc_list(win_T *wp)
2149 {
2150     if (IS_LL_WINDOW(wp))
2151 	// For a location list window, use the referenced location list
2152 	return wp->w_llist_ref;
2153 
2154     // For a non-location list window, w_llist_ref should not point to a
2155     // location list.
2156     ll_free_all(&wp->w_llist_ref);
2157 
2158     if (wp->w_llist == NULL)
2159 	wp->w_llist = qf_alloc_stack(QFLT_LOCATION);	// new location list
2160     return wp->w_llist;
2161 }
2162 
2163 /*
2164  * Get the quickfix/location list stack to use for the specified Ex command.
2165  * For a location list command, returns the stack for the current window.  If
2166  * the location list is not found, then returns NULL and prints an error
2167  * message if 'print_emsg' is TRUE.
2168  */
2169     static qf_info_T *
2170 qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2171 {
2172     qf_info_T	*qi = &ql_info;
2173 
2174     if (is_loclist_cmd(eap->cmdidx))
2175     {
2176 	qi = GET_LOC_LIST(curwin);
2177 	if (qi == NULL)
2178 	{
2179 	    if (print_emsg)
2180 		emsg(_(e_loclist));
2181 	    return NULL;
2182 	}
2183     }
2184 
2185     return qi;
2186 }
2187 
2188 /*
2189  * Get the quickfix/location list stack to use for the specified Ex command.
2190  * For a location list command, returns the stack for the current window.
2191  * If the location list is not present, then allocates a new one.
2192  * Returns NULL if the allocation fails.  For a location list command, sets
2193  * 'pwinp' to curwin.
2194  */
2195     static qf_info_T *
2196 qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2197 {
2198     qf_info_T	*qi = &ql_info;
2199 
2200     if (is_loclist_cmd(eap->cmdidx))
2201     {
2202 	qi = ll_get_or_alloc_list(curwin);
2203 	if (qi == NULL)
2204 	    return NULL;
2205 	*pwinp = curwin;
2206     }
2207 
2208     return qi;
2209 }
2210 
2211 /*
2212  * Copy location list entries from 'from_qfl' to 'to_qfl'.
2213  */
2214     static int
2215 copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
2216 {
2217     int		i;
2218     qfline_T    *from_qfp;
2219     qfline_T    *prevp;
2220 
2221     // copy all the location entries in this list
2222     FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
2223     {
2224 	if (qf_add_entry(to_qfl,
2225 		    NULL,
2226 		    NULL,
2227 		    from_qfp->qf_module,
2228 		    0,
2229 		    from_qfp->qf_text,
2230 		    from_qfp->qf_lnum,
2231 		    from_qfp->qf_col,
2232 		    from_qfp->qf_viscol,
2233 		    from_qfp->qf_pattern,
2234 		    from_qfp->qf_nr,
2235 		    0,
2236 		    from_qfp->qf_valid) == QF_FAIL)
2237 	    return FAIL;
2238 
2239 	// qf_add_entry() will not set the qf_num field, as the
2240 	// directory and file names are not supplied. So the qf_fnum
2241 	// field is copied here.
2242 	prevp = to_qfl->qf_last;
2243 	prevp->qf_fnum = from_qfp->qf_fnum;	// file number
2244 	prevp->qf_type = from_qfp->qf_type;	// error type
2245 	if (from_qfl->qf_ptr == from_qfp)
2246 	    to_qfl->qf_ptr = prevp;		// current location
2247     }
2248 
2249     return OK;
2250 }
2251 
2252 /*
2253  * Copy the specified location list 'from_qfl' to 'to_qfl'.
2254  */
2255     static int
2256 copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
2257 {
2258     // Some of the fields are populated by qf_add_entry()
2259     to_qfl->qfl_type = from_qfl->qfl_type;
2260     to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2261     to_qfl->qf_count = 0;
2262     to_qfl->qf_index = 0;
2263     to_qfl->qf_start = NULL;
2264     to_qfl->qf_last = NULL;
2265     to_qfl->qf_ptr = NULL;
2266     if (from_qfl->qf_title != NULL)
2267 	to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2268     else
2269 	to_qfl->qf_title = NULL;
2270     if (from_qfl->qf_ctx != NULL)
2271     {
2272 	to_qfl->qf_ctx = alloc_tv();
2273 	if (to_qfl->qf_ctx != NULL)
2274 	    copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2275     }
2276     else
2277 	to_qfl->qf_ctx = NULL;
2278 
2279     if (from_qfl->qf_count)
2280 	if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
2281 	    return FAIL;
2282 
2283     to_qfl->qf_index = from_qfl->qf_index;	// current index in the list
2284 
2285     // Assign a new ID for the location list
2286     to_qfl->qf_id = ++last_qf_id;
2287     to_qfl->qf_changedtick = 0L;
2288 
2289     // When no valid entries are present in the list, qf_ptr points to
2290     // the first item in the list
2291     if (to_qfl->qf_nonevalid)
2292     {
2293 	to_qfl->qf_ptr = to_qfl->qf_start;
2294 	to_qfl->qf_index = 1;
2295     }
2296 
2297     return OK;
2298 }
2299 
2300 /*
2301  * Copy the location list stack 'from' window to 'to' window.
2302  */
2303     void
2304 copy_loclist_stack(win_T *from, win_T *to)
2305 {
2306     qf_info_T	*qi;
2307     int		idx;
2308 
2309     // When copying from a location list window, copy the referenced
2310     // location list. For other windows, copy the location list for
2311     // that window.
2312     if (IS_LL_WINDOW(from))
2313 	qi = from->w_llist_ref;
2314     else
2315 	qi = from->w_llist;
2316 
2317     if (qi == NULL)		    // no location list to copy
2318 	return;
2319 
2320     // allocate a new location list
2321     if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
2322 	return;
2323 
2324     to->w_llist->qf_listcount = qi->qf_listcount;
2325 
2326     // Copy the location lists one at a time
2327     for (idx = 0; idx < qi->qf_listcount; ++idx)
2328     {
2329 	to->w_llist->qf_curlist = idx;
2330 
2331 	if (copy_loclist(qf_get_list(qi, idx),
2332 			qf_get_list(to->w_llist, idx)) == FAIL)
2333 	{
2334 	    qf_free_all(to);
2335 	    return;
2336 	}
2337     }
2338 
2339     to->w_llist->qf_curlist = qi->qf_curlist;	// current list
2340 }
2341 
2342 /*
2343  * Get buffer number for file "directory/fname".
2344  * Also sets the b_has_qf_entry flag.
2345  */
2346     static int
2347 qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
2348 {
2349     char_u	*ptr = NULL;
2350     buf_T	*buf;
2351     char_u	*bufname;
2352 
2353     if (fname == NULL || *fname == NUL)		// no file name
2354 	return 0;
2355 
2356 #ifdef VMS
2357     vms_remove_version(fname);
2358 #endif
2359 #ifdef BACKSLASH_IN_FILENAME
2360     if (directory != NULL)
2361 	slash_adjust(directory);
2362     slash_adjust(fname);
2363 #endif
2364     if (directory != NULL && !vim_isAbsName(fname)
2365 	    && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2366     {
2367 	// Here we check if the file really exists.
2368 	// This should normally be true, but if make works without
2369 	// "leaving directory"-messages we might have missed a
2370 	// directory change.
2371 	if (mch_getperm(ptr) < 0)
2372 	{
2373 	    vim_free(ptr);
2374 	    directory = qf_guess_filepath(qfl, fname);
2375 	    if (directory)
2376 		ptr = concat_fnames(directory, fname, TRUE);
2377 	    else
2378 		ptr = vim_strsave(fname);
2379 	}
2380 	// Use concatenated directory name and file name
2381 	bufname = ptr;
2382     }
2383     else
2384 	bufname = fname;
2385 
2386     if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
2387 	    && bufref_valid(&qf_last_bufref))
2388     {
2389 	buf = qf_last_bufref.br_buf;
2390 	vim_free(ptr);
2391     }
2392     else
2393     {
2394 	vim_free(qf_last_bufname);
2395 	buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2396 	if (bufname == ptr)
2397 	    qf_last_bufname = bufname;
2398 	else
2399 	    qf_last_bufname = vim_strsave(bufname);
2400 	set_bufref(&qf_last_bufref, buf);
2401     }
2402     if (buf == NULL)
2403 	return 0;
2404 
2405     buf->b_has_qf_entry =
2406 			IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
2407     return buf->b_fnum;
2408 }
2409 
2410 /*
2411  * Push dirbuf onto the directory stack and return pointer to actual dir or
2412  * NULL on error.
2413  */
2414     static char_u *
2415 qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
2416 {
2417     struct dir_stack_T  *ds_new;
2418     struct dir_stack_T  *ds_ptr;
2419 
2420     // allocate new stack element and hook it in
2421     ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2422     if (ds_new == NULL)
2423 	return NULL;
2424 
2425     ds_new->next = *stackptr;
2426     *stackptr = ds_new;
2427 
2428     // store directory on the stack
2429     if (vim_isAbsName(dirbuf)
2430 	    || (*stackptr)->next == NULL
2431 	    || (*stackptr && is_file_stack))
2432 	(*stackptr)->dirname = vim_strsave(dirbuf);
2433     else
2434     {
2435 	// Okay we don't have an absolute path.
2436 	// dirbuf must be a subdir of one of the directories on the stack.
2437 	// Let's search...
2438 	ds_new = (*stackptr)->next;
2439 	(*stackptr)->dirname = NULL;
2440 	while (ds_new)
2441 	{
2442 	    vim_free((*stackptr)->dirname);
2443 	    (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2444 		    TRUE);
2445 	    if (mch_isdir((*stackptr)->dirname) == TRUE)
2446 		break;
2447 
2448 	    ds_new = ds_new->next;
2449 	}
2450 
2451 	// clean up all dirs we already left
2452 	while ((*stackptr)->next != ds_new)
2453 	{
2454 	    ds_ptr = (*stackptr)->next;
2455 	    (*stackptr)->next = (*stackptr)->next->next;
2456 	    vim_free(ds_ptr->dirname);
2457 	    vim_free(ds_ptr);
2458 	}
2459 
2460 	// Nothing found -> it must be on top level
2461 	if (ds_new == NULL)
2462 	{
2463 	    vim_free((*stackptr)->dirname);
2464 	    (*stackptr)->dirname = vim_strsave(dirbuf);
2465 	}
2466     }
2467 
2468     if ((*stackptr)->dirname != NULL)
2469 	return (*stackptr)->dirname;
2470     else
2471     {
2472 	ds_ptr = *stackptr;
2473 	*stackptr = (*stackptr)->next;
2474 	vim_free(ds_ptr);
2475 	return NULL;
2476     }
2477 }
2478 
2479 /*
2480  * pop dirbuf from the directory stack and return previous directory or NULL if
2481  * stack is empty
2482  */
2483     static char_u *
2484 qf_pop_dir(struct dir_stack_T **stackptr)
2485 {
2486     struct dir_stack_T  *ds_ptr;
2487 
2488     // TODO: Should we check if dirbuf is the directory on top of the stack?
2489     // What to do if it isn't?
2490 
2491     // pop top element and free it
2492     if (*stackptr != NULL)
2493     {
2494 	ds_ptr = *stackptr;
2495 	*stackptr = (*stackptr)->next;
2496 	vim_free(ds_ptr->dirname);
2497 	vim_free(ds_ptr);
2498     }
2499 
2500     // return NEW top element as current dir or NULL if stack is empty
2501     return *stackptr ? (*stackptr)->dirname : NULL;
2502 }
2503 
2504 /*
2505  * clean up directory stack
2506  */
2507     static void
2508 qf_clean_dir_stack(struct dir_stack_T **stackptr)
2509 {
2510     struct dir_stack_T  *ds_ptr;
2511 
2512     while ((ds_ptr = *stackptr) != NULL)
2513     {
2514 	*stackptr = (*stackptr)->next;
2515 	vim_free(ds_ptr->dirname);
2516 	vim_free(ds_ptr);
2517     }
2518 }
2519 
2520 /*
2521  * Check in which directory of the directory stack the given file can be
2522  * found.
2523  * Returns a pointer to the directory name or NULL if not found.
2524  * Cleans up intermediate directory entries.
2525  *
2526  * TODO: How to solve the following problem?
2527  * If we have this directory tree:
2528  *     ./
2529  *     ./aa
2530  *     ./aa/bb
2531  *     ./bb
2532  *     ./bb/x.c
2533  * and make says:
2534  *     making all in aa
2535  *     making all in bb
2536  *     x.c:9: Error
2537  * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2538  * qf_guess_filepath will return NULL.
2539  */
2540     static char_u *
2541 qf_guess_filepath(qf_list_T *qfl, char_u *filename)
2542 {
2543     struct dir_stack_T     *ds_ptr;
2544     struct dir_stack_T     *ds_tmp;
2545     char_u		   *fullname;
2546 
2547     // no dirs on the stack - there's nothing we can do
2548     if (qfl->qf_dir_stack == NULL)
2549 	return NULL;
2550 
2551     ds_ptr = qfl->qf_dir_stack->next;
2552     fullname = NULL;
2553     while (ds_ptr)
2554     {
2555 	vim_free(fullname);
2556 	fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2557 
2558 	// If concat_fnames failed, just go on. The worst thing that can happen
2559 	// is that we delete the entire stack.
2560 	if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2561 	    break;
2562 
2563 	ds_ptr = ds_ptr->next;
2564     }
2565 
2566     vim_free(fullname);
2567 
2568     // clean up all dirs we already left
2569     while (qfl->qf_dir_stack->next != ds_ptr)
2570     {
2571 	ds_tmp = qfl->qf_dir_stack->next;
2572 	qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
2573 	vim_free(ds_tmp->dirname);
2574 	vim_free(ds_tmp);
2575     }
2576 
2577     return ds_ptr == NULL ? NULL : ds_ptr->dirname;
2578 }
2579 
2580 /*
2581  * Returns TRUE if a quickfix/location list with the given identifier exists.
2582  */
2583     static int
2584 qflist_valid(win_T *wp, int_u qf_id)
2585 {
2586     qf_info_T	*qi = &ql_info;
2587     int		i;
2588 
2589     if (wp != NULL)
2590     {
2591 	qi = GET_LOC_LIST(wp);	    // Location list
2592 	if (qi == NULL)
2593 	    return FALSE;
2594     }
2595 
2596     for (i = 0; i < qi->qf_listcount; ++i)
2597 	if (qi->qf_lists[i].qf_id == qf_id)
2598 	    return TRUE;
2599 
2600     return FALSE;
2601 }
2602 
2603 /*
2604  * When loading a file from the quickfix, the autocommands may modify it.
2605  * This may invalidate the current quickfix entry.  This function checks
2606  * whether an entry is still present in the quickfix list.
2607  * Similar to location list.
2608  */
2609     static int
2610 is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
2611 {
2612     qfline_T	*qfp;
2613     int		i;
2614 
2615     // Search for the entry in the current list
2616     FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2617 	if (qfp == qf_ptr)
2618 	    break;
2619 
2620     if (i > qfl->qf_count) // Entry is not found
2621 	return FALSE;
2622 
2623     return TRUE;
2624 }
2625 
2626 /*
2627  * Get the next valid entry in the current quickfix/location list. The search
2628  * starts from the current entry.  Returns NULL on failure.
2629  */
2630     static qfline_T *
2631 get_next_valid_entry(
2632 	qf_list_T	*qfl,
2633 	qfline_T	*qf_ptr,
2634 	int		*qf_index,
2635 	int		dir)
2636 {
2637     int			idx;
2638     int			old_qf_fnum;
2639 
2640     idx = *qf_index;
2641     old_qf_fnum = qf_ptr->qf_fnum;
2642 
2643     do
2644     {
2645 	if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
2646 	    return NULL;
2647 	++idx;
2648 	qf_ptr = qf_ptr->qf_next;
2649     } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
2650 	    || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2651 
2652     *qf_index = idx;
2653     return qf_ptr;
2654 }
2655 
2656 /*
2657  * Get the previous valid entry in the current quickfix/location list. The
2658  * search starts from the current entry.  Returns NULL on failure.
2659  */
2660     static qfline_T *
2661 get_prev_valid_entry(
2662 	qf_list_T	*qfl,
2663 	qfline_T	*qf_ptr,
2664 	int		*qf_index,
2665 	int		dir)
2666 {
2667     int			idx;
2668     int			old_qf_fnum;
2669 
2670     idx = *qf_index;
2671     old_qf_fnum = qf_ptr->qf_fnum;
2672 
2673     do
2674     {
2675 	if (idx == 1 || qf_ptr->qf_prev == NULL)
2676 	    return NULL;
2677 	--idx;
2678 	qf_ptr = qf_ptr->qf_prev;
2679     } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
2680 	    || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2681 
2682     *qf_index = idx;
2683     return qf_ptr;
2684 }
2685 
2686 /*
2687  * Get the n'th (errornr) previous/next valid entry from the current entry in
2688  * the quickfix list.
2689  *   dir == FORWARD or FORWARD_FILE: next valid entry
2690  *   dir == BACKWARD or BACKWARD_FILE: previous valid entry
2691  */
2692     static qfline_T *
2693 get_nth_valid_entry(
2694 	qf_list_T	*qfl,
2695 	int		errornr,
2696 	int		dir,
2697 	int		*new_qfidx)
2698 {
2699     qfline_T		*qf_ptr = qfl->qf_ptr;
2700     int			qf_idx = qfl->qf_index;
2701     qfline_T		*prev_qf_ptr;
2702     int			prev_index;
2703     static char_u	*e_no_more_items = (char_u *)N_("E553: No more items");
2704     char_u		*err = e_no_more_items;
2705 
2706     while (errornr--)
2707     {
2708 	prev_qf_ptr = qf_ptr;
2709 	prev_index = qf_idx;
2710 
2711 	if (dir == FORWARD || dir == FORWARD_FILE)
2712 	    qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
2713 	else
2714 	    qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
2715 	if (qf_ptr == NULL)
2716 	{
2717 	    qf_ptr = prev_qf_ptr;
2718 	    qf_idx = prev_index;
2719 	    if (err != NULL)
2720 	    {
2721 		emsg(_(err));
2722 		return NULL;
2723 	    }
2724 	    break;
2725 	}
2726 
2727 	err = NULL;
2728     }
2729 
2730     *new_qfidx = qf_idx;
2731     return qf_ptr;
2732 }
2733 
2734 /*
2735  * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2736  * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
2737  */
2738     static qfline_T *
2739 get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
2740 {
2741     qfline_T	*qf_ptr = qfl->qf_ptr;
2742     int		qf_idx = qfl->qf_index;
2743 
2744     // New error number is less than the current error number
2745     while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2746     {
2747 	--qf_idx;
2748 	qf_ptr = qf_ptr->qf_prev;
2749     }
2750     // New error number is greater than the current error number
2751     while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2752 						qf_ptr->qf_next != NULL)
2753     {
2754 	++qf_idx;
2755 	qf_ptr = qf_ptr->qf_next;
2756     }
2757 
2758     *new_qfidx = qf_idx;
2759     return qf_ptr;
2760 }
2761 
2762 /*
2763  * Get a entry specied by 'errornr' and 'dir' from the current
2764  * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2765  * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2766  * Returns a pointer to the entry and the index of the new entry is stored in
2767  * 'new_qfidx'.
2768  */
2769     static qfline_T *
2770 qf_get_entry(
2771 	qf_list_T	*qfl,
2772 	int		errornr,
2773 	int		dir,
2774 	int		*new_qfidx)
2775 {
2776     qfline_T	*qf_ptr = qfl->qf_ptr;
2777     int		qfidx = qfl->qf_index;
2778 
2779     if (dir != 0)    // next/prev valid entry
2780 	qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2781     else if (errornr != 0)	// go to specified number
2782 	qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2783 
2784     *new_qfidx = qfidx;
2785     return qf_ptr;
2786 }
2787 
2788 /*
2789  * Find a window displaying a Vim help file.
2790  */
2791     static win_T *
2792 qf_find_help_win(void)
2793 {
2794     win_T *wp;
2795 
2796     FOR_ALL_WINDOWS(wp)
2797 	if (bt_help(wp->w_buffer))
2798 	    return wp;
2799 
2800     return NULL;
2801 }
2802 
2803 /*
2804  * Set the location list for the specified window to 'qi'.
2805  */
2806     static void
2807 win_set_loclist(win_T *wp, qf_info_T *qi)
2808 {
2809     wp->w_llist = qi;
2810     qi->qf_refcount++;
2811 }
2812 
2813 /*
2814  * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2815  * window.
2816  */
2817     static int
2818 jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
2819 {
2820     win_T	*wp;
2821     int		flags;
2822 
2823     if (cmdmod.tab != 0 || newwin)
2824 	wp = NULL;
2825     else
2826 	wp = qf_find_help_win();
2827     if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2828 	win_enter(wp, TRUE);
2829     else
2830     {
2831 	// Split off help window; put it at far top if no position
2832 	// specified, the current window is vertically split and narrow.
2833 	flags = WSP_HELP;
2834 	if (cmdmod.split == 0 && curwin->w_width != Columns
2835 		&& curwin->w_width < 80)
2836 	    flags |= WSP_TOP;
2837 	// If the user asks to open a new window, then copy the location list.
2838 	// Otherwise, don't copy the location list.
2839 	if (IS_LL_STACK(qi) && !newwin)
2840 	    flags |= WSP_NEWLOC;
2841 
2842 	if (win_split(0, flags) == FAIL)
2843 	    return FAIL;
2844 
2845 	*opened_window = TRUE;
2846 
2847 	if (curwin->w_height < p_hh)
2848 	    win_setheight((int)p_hh);
2849 
2850 	// When using location list, the new window should use the supplied
2851 	// location list. If the user asks to open a new window, then the new
2852 	// window will get a copy of the location list.
2853 	if (IS_LL_STACK(qi) && !newwin)
2854 	    win_set_loclist(curwin, qi);
2855     }
2856 
2857     if (!p_im)
2858 	restart_edit = 0;	    // don't want insert mode in help file
2859 
2860     return OK;
2861 }
2862 
2863 /*
2864  * Find a non-quickfix window in the current tabpage using the given location
2865  * list stack.
2866  * Returns NULL if a matching window is not found.
2867  */
2868     static win_T *
2869 qf_find_win_with_loclist(qf_info_T *ll)
2870 {
2871     win_T	*wp;
2872 
2873     FOR_ALL_WINDOWS(wp)
2874 	if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2875 	    return wp;
2876 
2877     return NULL;
2878 }
2879 
2880 /*
2881  * Find a window containing a normal buffer
2882  */
2883     static win_T *
2884 qf_find_win_with_normal_buf(void)
2885 {
2886     win_T	*wp;
2887 
2888     FOR_ALL_WINDOWS(wp)
2889 	if (bt_normal(wp->w_buffer))
2890 	    return wp;
2891 
2892     return NULL;
2893 }
2894 
2895 /*
2896  * Go to a window in any tabpage containing the specified file.  Returns TRUE
2897  * if successfully jumped to the window. Otherwise returns FALSE.
2898  */
2899     static int
2900 qf_goto_tabwin_with_file(int fnum)
2901 {
2902     tabpage_T	*tp;
2903     win_T	*wp;
2904 
2905     FOR_ALL_TAB_WINDOWS(tp, wp)
2906 	if (wp->w_buffer->b_fnum == fnum)
2907 	{
2908 	    goto_tabpage_win(tp, wp);
2909 	    return TRUE;
2910 	}
2911 
2912     return FALSE;
2913 }
2914 
2915 /*
2916  * Create a new window to show a file above the quickfix window. Called when
2917  * only the quickfix window is present.
2918  */
2919     static int
2920 qf_open_new_file_win(qf_info_T *ll_ref)
2921 {
2922     int		flags;
2923 
2924     flags = WSP_ABOVE;
2925     if (ll_ref != NULL)
2926 	flags |= WSP_NEWLOC;
2927     if (win_split(0, flags) == FAIL)
2928 	return FAIL;		// not enough room for window
2929     p_swb = empty_option;	// don't split again
2930     swb_flags = 0;
2931     RESET_BINDING(curwin);
2932     if (ll_ref != NULL)
2933 	// The new window should use the location list from the
2934 	// location list window
2935 	win_set_loclist(curwin, ll_ref);
2936     return OK;
2937 }
2938 
2939 /*
2940  * Go to a window that shows the right buffer. If the window is not found, go
2941  * to the window just above the location list window. This is used for opening
2942  * a file from a location window and not from a quickfix window. If some usable
2943  * window is previously found, then it is supplied in 'use_win'.
2944  */
2945     static void
2946 qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2947 {
2948     win_T	*win = use_win;
2949 
2950     if (win == NULL)
2951     {
2952 	// Find the window showing the selected file
2953 	FOR_ALL_WINDOWS(win)
2954 	    if (win->w_buffer->b_fnum == qf_fnum)
2955 		break;
2956 	if (win == NULL)
2957 	{
2958 	    // Find a previous usable window
2959 	    win = curwin;
2960 	    do
2961 	    {
2962 		if (bt_normal(win->w_buffer))
2963 		    break;
2964 		if (win->w_prev == NULL)
2965 		    win = lastwin;	// wrap around the top
2966 		else
2967 		    win = win->w_prev; // go to previous window
2968 	    } while (win != curwin);
2969 	}
2970     }
2971     win_goto(win);
2972 
2973     // If the location list for the window is not set, then set it
2974     // to the location list from the location window
2975     if (win->w_llist == NULL && ll_ref != NULL)
2976 	win_set_loclist(win, ll_ref);
2977 }
2978 
2979 /*
2980  * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2981  * not found, then go to the window just above the quickfix window. This is
2982  * used for opening a file from a quickfix window and not from a location
2983  * window.
2984  */
2985     static void
2986 qf_goto_win_with_qfl_file(int qf_fnum)
2987 {
2988     win_T	*win;
2989     win_T	*altwin;
2990 
2991     win = curwin;
2992     altwin = NULL;
2993     for (;;)
2994     {
2995 	if (win->w_buffer->b_fnum == qf_fnum)
2996 	    break;
2997 	if (win->w_prev == NULL)
2998 	    win = lastwin;	// wrap around the top
2999 	else
3000 	    win = win->w_prev;	// go to previous window
3001 
3002 	if (IS_QF_WINDOW(win))
3003 	{
3004 	    // Didn't find it, go to the window before the quickfix
3005 	    // window.
3006 	    if (altwin != NULL)
3007 		win = altwin;
3008 	    else if (curwin->w_prev != NULL)
3009 		win = curwin->w_prev;
3010 	    else
3011 		win = curwin->w_next;
3012 	    break;
3013 	}
3014 
3015 	// Remember a usable window.
3016 	if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
3017 	    altwin = win;
3018     }
3019 
3020     win_goto(win);
3021 }
3022 
3023 /*
3024  * Find a suitable window for opening a file (qf_fnum) from the
3025  * quickfix/location list and jump to it.  If the file is already opened in a
3026  * window, jump to it. Otherwise open a new window to display the file. If
3027  * 'newwin' is TRUE, then always open a new window. This is called from either
3028  * a quickfix or a location list window.
3029  */
3030     static int
3031 qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
3032 {
3033     win_T	*usable_win_ptr = NULL;
3034     int		usable_win;
3035     qf_info_T	*ll_ref = NULL;
3036     win_T	*win;
3037 
3038     usable_win = 0;
3039 
3040     // If opening a new window, then don't use the location list referred by
3041     // the current window.  Otherwise two windows will refer to the same
3042     // location list.
3043     if (!newwin)
3044 	ll_ref = curwin->w_llist_ref;
3045 
3046     if (ll_ref != NULL)
3047     {
3048 	// Find a non-quickfix window with this location list
3049 	usable_win_ptr = qf_find_win_with_loclist(ll_ref);
3050 	if (usable_win_ptr != NULL)
3051 	    usable_win = 1;
3052     }
3053 
3054     if (!usable_win)
3055     {
3056 	// Locate a window showing a normal buffer
3057 	win = qf_find_win_with_normal_buf();
3058 	if (win != NULL)
3059 	    usable_win = 1;
3060     }
3061 
3062     // If no usable window is found and 'switchbuf' contains "usetab"
3063     // then search in other tabs.
3064     if (!usable_win && (swb_flags & SWB_USETAB))
3065 	usable_win = qf_goto_tabwin_with_file(qf_fnum);
3066 
3067     // If there is only one window and it is the quickfix window, create a
3068     // new one above the quickfix window.
3069     if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
3070     {
3071 	if (qf_open_new_file_win(ll_ref) != OK)
3072 	    return FAIL;
3073 	*opened_window = TRUE;	// close it when fail
3074     }
3075     else
3076     {
3077 	if (curwin->w_llist_ref != NULL)	// In a location window
3078 	    qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
3079 	else					// In a quickfix window
3080 	    qf_goto_win_with_qfl_file(qf_fnum);
3081     }
3082 
3083     return OK;
3084 }
3085 
3086 /*
3087  * Edit the selected file or help file.
3088  * Returns OK if successfully edited the file, FAIL on failing to open the
3089  * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3090  * when opening the buffer.
3091  */
3092     static int
3093 qf_jump_edit_buffer(
3094 	qf_info_T	*qi,
3095 	qfline_T	*qf_ptr,
3096 	int		forceit,
3097 	int		prev_winid,
3098 	int		*opened_window)
3099 {
3100     qf_list_T	*qfl = qf_get_curlist(qi);
3101     qfltype_T	qfl_type = qfl->qfl_type;
3102     int		retval = OK;
3103     int		old_qf_curlist = qi->qf_curlist;
3104     int		save_qfid = qfl->qf_id;
3105 
3106     if (qf_ptr->qf_type == 1)
3107     {
3108 	// Open help file (do_ecmd() will set b_help flag, readfile() will
3109 	// set b_p_ro flag).
3110 	if (!can_abandon(curbuf, forceit))
3111 	{
3112 	    no_write_message();
3113 	    return FAIL;
3114 	}
3115 
3116 	retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3117 		ECMD_HIDE + ECMD_SET_HELP,
3118 		prev_winid == curwin->w_id ? curwin : NULL);
3119     }
3120     else
3121 	retval = buflist_getfile(qf_ptr->qf_fnum,
3122 		(linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
3123 
3124     // If a location list, check whether the associated window is still
3125     // present.
3126     if (qfl_type == QFLT_LOCATION)
3127     {
3128 	win_T	*wp = win_id2wp(prev_winid);
3129 	if (wp == NULL && curwin->w_llist != qi)
3130 	{
3131 	    emsg(_("E924: Current window was closed"));
3132 	    *opened_window = FALSE;
3133 	    return NOTDONE;
3134 	}
3135     }
3136 
3137     if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
3138     {
3139 	emsg(_("E925: Current quickfix was changed"));
3140 	return NOTDONE;
3141     }
3142 
3143     if (old_qf_curlist != qi->qf_curlist
3144 	    || !is_qf_entry_present(qfl, qf_ptr))
3145     {
3146 	if (qfl_type == QFLT_QUICKFIX)
3147 	    emsg(_("E925: Current quickfix was changed"));
3148 	else
3149 	    emsg(_(e_loc_list_changed));
3150 	return NOTDONE;
3151     }
3152 
3153     return retval;
3154 }
3155 
3156 /*
3157  * Go to the error line in the current file using either line/column number or
3158  * a search pattern.
3159  */
3160     static void
3161 qf_jump_goto_line(
3162 	linenr_T	qf_lnum,
3163 	int		qf_col,
3164 	char_u		qf_viscol,
3165 	char_u		*qf_pattern)
3166 {
3167     linenr_T		i;
3168 
3169     if (qf_pattern == NULL)
3170     {
3171 	// Go to line with error, unless qf_lnum is 0.
3172 	i = qf_lnum;
3173 	if (i > 0)
3174 	{
3175 	    if (i > curbuf->b_ml.ml_line_count)
3176 		i = curbuf->b_ml.ml_line_count;
3177 	    curwin->w_cursor.lnum = i;
3178 	}
3179 	if (qf_col > 0)
3180 	{
3181 	    curwin->w_cursor.coladd = 0;
3182 	    if (qf_viscol == TRUE)
3183 		coladvance(qf_col - 1);
3184 	    else
3185 		curwin->w_cursor.col = qf_col - 1;
3186 	    curwin->w_set_curswant = TRUE;
3187 	    check_cursor();
3188 	}
3189 	else
3190 	    beginline(BL_WHITE | BL_FIX);
3191     }
3192     else
3193     {
3194 	pos_T save_cursor;
3195 
3196 	// Move the cursor to the first line in the buffer
3197 	save_cursor = curwin->w_cursor;
3198 	curwin->w_cursor.lnum = 0;
3199 	if (!do_search(NULL, '/', qf_pattern, (long)1,
3200 		    SEARCH_KEEP, NULL, NULL))
3201 	    curwin->w_cursor = save_cursor;
3202     }
3203 }
3204 
3205 /*
3206  * Display quickfix list index and size message
3207  */
3208     static void
3209 qf_jump_print_msg(
3210 	qf_info_T	*qi,
3211 	int		qf_index,
3212 	qfline_T	*qf_ptr,
3213 	buf_T		*old_curbuf,
3214 	linenr_T	old_lnum)
3215 {
3216     linenr_T		i;
3217     int			len;
3218 
3219     // Update the screen before showing the message, unless the screen
3220     // scrolled up.
3221     if (!msg_scrolled)
3222 	update_topline_redraw();
3223     sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3224 	    qf_get_curlist(qi)->qf_count,
3225 	    qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3226 	    (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3227     // Add the message, skipping leading whitespace and newlines.
3228     len = (int)STRLEN(IObuff);
3229     qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3230 
3231     // Output the message.  Overwrite to avoid scrolling when the 'O'
3232     // flag is present in 'shortmess'; But when not jumping, print the
3233     // whole message.
3234     i = msg_scroll;
3235     if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3236 	msg_scroll = TRUE;
3237     else if (!msg_scrolled && shortmess(SHM_OVERALL))
3238 	msg_scroll = FALSE;
3239     msg_attr_keep((char *)IObuff, 0, TRUE);
3240     msg_scroll = i;
3241 }
3242 
3243 /*
3244  * Find a usable window for opening a file from the quickfix/location list. If
3245  * a window is not found then open a new window. If 'newwin' is TRUE, then open
3246  * a new window.
3247  * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3248  * able to jump/open a window.  Returns NOTDONE if a file is not associated
3249  * with the entry.
3250  */
3251     static int
3252 qf_jump_open_window(
3253 	qf_info_T	*qi,
3254 	qfline_T	*qf_ptr,
3255 	int		newwin,
3256 	int		*opened_window)
3257 {
3258     // For ":helpgrep" find a help window or open one.
3259     if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
3260 	if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
3261 	    return FAIL;
3262 
3263     // If currently in the quickfix window, find another window to show the
3264     // file in.
3265     if (bt_quickfix(curbuf) && !*opened_window)
3266     {
3267 	// If there is no file specified, we don't know where to go.
3268 	// But do advance, otherwise ":cn" gets stuck.
3269 	if (qf_ptr->qf_fnum == 0)
3270 	    return NOTDONE;
3271 
3272 	if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3273 						opened_window) == FAIL)
3274 	    return FAIL;
3275     }
3276 
3277     return OK;
3278 }
3279 
3280 /*
3281  * Edit a selected file from the quickfix/location list and jump to a
3282  * particular line/column, adjust the folds and display a message about the
3283  * jump.
3284  * Returns OK on success and FAIL on failing to open the file/buffer.  Returns
3285  * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3286  * the file.
3287  */
3288     static int
3289 qf_jump_to_buffer(
3290 	qf_info_T	*qi,
3291 	int		qf_index,
3292 	qfline_T	*qf_ptr,
3293 	int		forceit,
3294 	int		prev_winid,
3295 	int		*opened_window,
3296 	int		openfold,
3297 	int		print_message)
3298 {
3299     buf_T	*old_curbuf;
3300     linenr_T	old_lnum;
3301     int		retval = OK;
3302 
3303     // If there is a file name, read the wanted file if needed, and check
3304     // autowrite etc.
3305     old_curbuf = curbuf;
3306     old_lnum = curwin->w_cursor.lnum;
3307 
3308     if (qf_ptr->qf_fnum != 0)
3309     {
3310 	retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
3311 						opened_window);
3312 	if (retval != OK)
3313 	    return retval;
3314     }
3315 
3316     // When not switched to another buffer, still need to set pc mark
3317     if (curbuf == old_curbuf)
3318 	setpcmark();
3319 
3320     qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3321 	    qf_ptr->qf_pattern);
3322 
3323 #ifdef FEAT_FOLDING
3324     if ((fdo_flags & FDO_QUICKFIX) && openfold)
3325 	foldOpenCursor();
3326 #endif
3327     if (print_message)
3328 	qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3329 
3330     return retval;
3331 }
3332 
3333 /*
3334  * Jump to a quickfix line and try to use an existing window.
3335  */
3336     void
3337 qf_jump(qf_info_T	*qi,
3338 	int		dir,
3339 	int		errornr,
3340 	int		forceit)
3341 {
3342     qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3343 }
3344 
3345 /*
3346  * Jump to a quickfix line.
3347  * If dir == 0 go to entry "errornr".
3348  * If dir == FORWARD go "errornr" valid entries forward.
3349  * If dir == BACKWARD go "errornr" valid entries backward.
3350  * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3351  * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3352  * else if "errornr" is zero, redisplay the same line
3353  * If 'forceit' is TRUE, then can discard changes to the current buffer.
3354  * If 'newwin' is TRUE, then open the file in a new window.
3355  */
3356     void
3357 qf_jump_newwin(qf_info_T	*qi,
3358 	int		dir,
3359 	int		errornr,
3360 	int		forceit,
3361 	int		newwin)
3362 {
3363     qf_list_T		*qfl;
3364     qfline_T		*qf_ptr;
3365     qfline_T		*old_qf_ptr;
3366     int			qf_index;
3367     int			old_qf_index;
3368     char_u		*old_swb = p_swb;
3369     unsigned		old_swb_flags = swb_flags;
3370     int			prev_winid;
3371     int			opened_window = FALSE;
3372     int			print_message = TRUE;
3373 #ifdef FEAT_FOLDING
3374     int			old_KeyTyped = KeyTyped; // getting file may reset it
3375 #endif
3376     int			retval = OK;
3377 
3378     if (qi == NULL)
3379 	qi = &ql_info;
3380 
3381     if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
3382     {
3383 	emsg(_(e_quickfix));
3384 	return;
3385     }
3386 
3387     incr_quickfix_busy();
3388 
3389     qfl = qf_get_curlist(qi);
3390 
3391     qf_ptr = qfl->qf_ptr;
3392     old_qf_ptr = qf_ptr;
3393     qf_index = qfl->qf_index;
3394     old_qf_index = qf_index;
3395 
3396     qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3397     if (qf_ptr == NULL)
3398     {
3399 	qf_ptr = old_qf_ptr;
3400 	qf_index = old_qf_index;
3401 	goto theend;
3402     }
3403 
3404     qfl->qf_index = qf_index;
3405     if (qf_win_pos_update(qi, old_qf_index))
3406 	// No need to print the error message if it's visible in the error
3407 	// window
3408 	print_message = FALSE;
3409 
3410     prev_winid = curwin->w_id;
3411 
3412     retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
3413     if (retval == FAIL)
3414 	goto failed;
3415     if (retval == NOTDONE)
3416 	goto theend;
3417 
3418     retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
3419 	    &opened_window, old_KeyTyped, print_message);
3420     if (retval == NOTDONE)
3421     {
3422 	// Quickfix/location list is freed by an autocmd
3423 	qi = NULL;
3424 	qf_ptr = NULL;
3425     }
3426 
3427     if (retval != OK)
3428     {
3429 	if (opened_window)
3430 	    win_close(curwin, TRUE);    // Close opened window
3431 	if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
3432 	{
3433 	    // Couldn't open file, so put index back where it was.  This could
3434 	    // happen if the file was readonly and we changed something.
3435 failed:
3436 	    qf_ptr = old_qf_ptr;
3437 	    qf_index = old_qf_index;
3438 	}
3439     }
3440 theend:
3441     if (qi != NULL)
3442     {
3443 	qfl->qf_ptr = qf_ptr;
3444 	qfl->qf_index = qf_index;
3445     }
3446     if (p_swb != old_swb)
3447     {
3448 	// Restore old 'switchbuf' value, but not when an autocommand or
3449 	// modeline has changed the value.
3450 	if (p_swb == empty_option)
3451 	{
3452 	    p_swb = old_swb;
3453 	    swb_flags = old_swb_flags;
3454 	}
3455 	else
3456 	    free_string_option(old_swb);
3457     }
3458     decr_quickfix_busy();
3459 }
3460 
3461 // Highlight attributes used for displaying entries from the quickfix list.
3462 static int	qfFileAttr;
3463 static int	qfSepAttr;
3464 static int	qfLineAttr;
3465 
3466 /*
3467  * Display information about a single entry from the quickfix/location list.
3468  * Used by ":clist/:llist" commands.
3469  * 'cursel' will be set to TRUE for the currently selected entry in the
3470  * quickfix list.
3471  */
3472     static void
3473 qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
3474 {
3475     char_u	*fname;
3476     buf_T	*buf;
3477     int		filter_entry;
3478 
3479     fname = NULL;
3480     if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3481 	vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3482 						(char *)qfp->qf_module);
3483     else {
3484 	if (qfp->qf_fnum != 0
3485 		&& (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3486 	{
3487 	    fname = buf->b_fname;
3488 	    if (qfp->qf_type == 1)	// :helpgrep
3489 		fname = gettail(fname);
3490 	}
3491 	if (fname == NULL)
3492 	    sprintf((char *)IObuff, "%2d", qf_idx);
3493 	else
3494 	    vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3495 		    qf_idx, (char *)fname);
3496     }
3497 
3498     // Support for filtering entries using :filter /pat/ clist
3499     // Match against the module name, file name, search pattern and
3500     // text of the entry.
3501     filter_entry = TRUE;
3502     if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3503 	filter_entry &= message_filtered(qfp->qf_module);
3504     if (filter_entry && fname != NULL)
3505 	filter_entry &= message_filtered(fname);
3506     if (filter_entry && qfp->qf_pattern != NULL)
3507 	filter_entry &= message_filtered(qfp->qf_pattern);
3508     if (filter_entry)
3509 	filter_entry &= message_filtered(qfp->qf_text);
3510     if (filter_entry)
3511 	return;
3512 
3513     msg_putchar('\n');
3514     msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
3515 
3516     if (qfp->qf_lnum != 0)
3517 	msg_puts_attr(":", qfSepAttr);
3518     if (qfp->qf_lnum == 0)
3519 	IObuff[0] = NUL;
3520     else if (qfp->qf_col == 0)
3521 	sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3522     else
3523 	sprintf((char *)IObuff, "%ld col %d",
3524 		qfp->qf_lnum, qfp->qf_col);
3525     sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3526 	    (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3527     msg_puts_attr((char *)IObuff, qfLineAttr);
3528     msg_puts_attr(":", qfSepAttr);
3529     if (qfp->qf_pattern != NULL)
3530     {
3531 	qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3532 	msg_puts((char *)IObuff);
3533 	msg_puts_attr(":", qfSepAttr);
3534     }
3535     msg_puts(" ");
3536 
3537     // Remove newlines and leading whitespace from the text.  For an
3538     // unrecognized line keep the indent, the compiler may mark a word
3539     // with ^^^^.
3540     qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3541 				? skipwhite(qfp->qf_text) : qfp->qf_text,
3542 				IObuff, IOSIZE);
3543     msg_prt_line(IObuff, FALSE);
3544     out_flush();		// show one line at a time
3545 }
3546 
3547 /*
3548  * ":clist": list all errors
3549  * ":llist": list all locations
3550  */
3551     void
3552 qf_list(exarg_T *eap)
3553 {
3554     qf_list_T	*qfl;
3555     qfline_T	*qfp;
3556     int		i;
3557     int		idx1 = 1;
3558     int		idx2 = -1;
3559     char_u	*arg = eap->arg;
3560     int		plus = FALSE;
3561     int		all = eap->forceit;	// if not :cl!, only show
3562 					// recognised errors
3563     qf_info_T	*qi;
3564 
3565     if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3566 	return;
3567 
3568     if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
3569     {
3570 	emsg(_(e_quickfix));
3571 	return;
3572     }
3573     if (*arg == '+')
3574     {
3575 	++arg;
3576 	plus = TRUE;
3577     }
3578     if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3579     {
3580 	emsg(_(e_trailing));
3581 	return;
3582     }
3583     qfl = qf_get_curlist(qi);
3584     if (plus)
3585     {
3586 	i = qfl->qf_index;
3587 	idx2 = i + idx1;
3588 	idx1 = i;
3589     }
3590     else
3591     {
3592 	i = qfl->qf_count;
3593 	if (idx1 < 0)
3594 	    idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3595 	if (idx2 < 0)
3596 	    idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3597     }
3598 
3599     // Shorten all the file names, so that it is easy to read
3600     shorten_fnames(FALSE);
3601 
3602     // Get the attributes for the different quickfix highlight items.  Note
3603     // that this depends on syntax items defined in the qf.vim syntax file
3604     qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3605     if (qfFileAttr == 0)
3606 	qfFileAttr = HL_ATTR(HLF_D);
3607     qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3608     if (qfSepAttr == 0)
3609 	qfSepAttr = HL_ATTR(HLF_D);
3610     qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3611     if (qfLineAttr == 0)
3612 	qfLineAttr = HL_ATTR(HLF_N);
3613 
3614     if (qfl->qf_nonevalid)
3615 	all = TRUE;
3616     FOR_ALL_QFL_ITEMS(qfl, qfp, i)
3617     {
3618 	if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3619 	    qf_list_entry(qfp, i, i == qfl->qf_index);
3620 
3621 	ui_breakcheck();
3622     }
3623 }
3624 
3625 /*
3626  * Remove newlines and leading whitespace from an error message.
3627  * Put the result in "buf[bufsize]".
3628  */
3629     static void
3630 qf_fmt_text(char_u *text, char_u *buf, int bufsize)
3631 {
3632     int		i;
3633     char_u	*p = text;
3634 
3635     for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3636     {
3637 	if (*p == '\n')
3638 	{
3639 	    buf[i] = ' ';
3640 	    while (*++p != NUL)
3641 		if (!VIM_ISWHITE(*p) && *p != '\n')
3642 		    break;
3643 	}
3644 	else
3645 	    buf[i] = *p++;
3646     }
3647     buf[i] = NUL;
3648 }
3649 
3650 /*
3651  * Display information (list number, list size and the title) about a
3652  * quickfix/location list.
3653  */
3654     static void
3655 qf_msg(qf_info_T *qi, int which, char *lead)
3656 {
3657     char   *title = (char *)qi->qf_lists[which].qf_title;
3658     int    count = qi->qf_lists[which].qf_count;
3659     char_u buf[IOSIZE];
3660 
3661     vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3662 	    lead,
3663 	    which + 1,
3664 	    qi->qf_listcount,
3665 	    count);
3666 
3667     if (title != NULL)
3668     {
3669 	size_t	len = STRLEN(buf);
3670 
3671 	if (len < 34)
3672 	{
3673 	    vim_memset(buf + len, ' ', 34 - len);
3674 	    buf[34] = NUL;
3675 	}
3676 	vim_strcat(buf, (char_u *)title, IOSIZE);
3677     }
3678     trunc_string(buf, buf, Columns - 1, IOSIZE);
3679     msg((char *)buf);
3680 }
3681 
3682 /*
3683  * ":colder [count]": Up in the quickfix stack.
3684  * ":cnewer [count]": Down in the quickfix stack.
3685  * ":lolder [count]": Up in the location list stack.
3686  * ":lnewer [count]": Down in the location list stack.
3687  */
3688     void
3689 qf_age(exarg_T *eap)
3690 {
3691     qf_info_T	*qi;
3692     int		count;
3693 
3694     if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3695 	return;
3696 
3697     if (eap->addr_count != 0)
3698 	count = eap->line2;
3699     else
3700 	count = 1;
3701     while (count--)
3702     {
3703 	if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
3704 	{
3705 	    if (qi->qf_curlist == 0)
3706 	    {
3707 		emsg(_("E380: At bottom of quickfix stack"));
3708 		break;
3709 	    }
3710 	    --qi->qf_curlist;
3711 	}
3712 	else
3713 	{
3714 	    if (qi->qf_curlist >= qi->qf_listcount - 1)
3715 	    {
3716 		emsg(_("E381: At top of quickfix stack"));
3717 		break;
3718 	    }
3719 	    ++qi->qf_curlist;
3720 	}
3721     }
3722     qf_msg(qi, qi->qf_curlist, "");
3723     qf_update_buffer(qi, NULL);
3724 }
3725 
3726 /*
3727  * Display the information about all the quickfix/location lists in the stack
3728  */
3729     void
3730 qf_history(exarg_T *eap)
3731 {
3732     qf_info_T	*qi = qf_cmd_get_stack(eap, FALSE);
3733     int		i;
3734 
3735     if (qf_stack_empty(qi))
3736 	msg(_("No entries"));
3737     else
3738 	for (i = 0; i < qi->qf_listcount; ++i)
3739 	    qf_msg(qi, i, i == qi->qf_curlist ? "> " : "  ");
3740 }
3741 
3742 /*
3743  * Free all the entries in the error list "idx". Note that other information
3744  * associated with the list like context and title are not freed.
3745  */
3746     static void
3747 qf_free_items(qf_list_T *qfl)
3748 {
3749     qfline_T	*qfp;
3750     qfline_T	*qfpnext;
3751     int		stop = FALSE;
3752 
3753     while (qfl->qf_count && qfl->qf_start != NULL)
3754     {
3755 	qfp = qfl->qf_start;
3756 	qfpnext = qfp->qf_next;
3757 	if (!stop)
3758 	{
3759 	    vim_free(qfp->qf_module);
3760 	    vim_free(qfp->qf_text);
3761 	    vim_free(qfp->qf_pattern);
3762 	    stop = (qfp == qfpnext);
3763 	    vim_free(qfp);
3764 	    if (stop)
3765 		// Somehow qf_count may have an incorrect value, set it to 1
3766 		// to avoid crashing when it's wrong.
3767 		// TODO: Avoid qf_count being incorrect.
3768 		qfl->qf_count = 1;
3769 	}
3770 	qfl->qf_start = qfpnext;
3771 	--qfl->qf_count;
3772     }
3773 
3774     qfl->qf_index = 0;
3775     qfl->qf_start = NULL;
3776     qfl->qf_last = NULL;
3777     qfl->qf_ptr = NULL;
3778     qfl->qf_nonevalid = TRUE;
3779 
3780     qf_clean_dir_stack(&qfl->qf_dir_stack);
3781     qfl->qf_directory = NULL;
3782     qf_clean_dir_stack(&qfl->qf_file_stack);
3783     qfl->qf_currfile = NULL;
3784     qfl->qf_multiline = FALSE;
3785     qfl->qf_multiignore = FALSE;
3786     qfl->qf_multiscan = FALSE;
3787 }
3788 
3789 /*
3790  * Free error list "idx". Frees all the entries in the quickfix list,
3791  * associated context information and the title.
3792  */
3793     static void
3794 qf_free(qf_list_T *qfl)
3795 {
3796     qf_free_items(qfl);
3797 
3798     VIM_CLEAR(qfl->qf_title);
3799     free_tv(qfl->qf_ctx);
3800     qfl->qf_ctx = NULL;
3801     qfl->qf_id = 0;
3802     qfl->qf_changedtick = 0L;
3803 }
3804 
3805 /*
3806  * qf_mark_adjust: adjust marks
3807  */
3808    void
3809 qf_mark_adjust(
3810 	win_T	*wp,
3811 	linenr_T	line1,
3812 	linenr_T	line2,
3813 	long	amount,
3814 	long	amount_after)
3815 {
3816     int		i;
3817     qfline_T	*qfp;
3818     int		idx;
3819     qf_info_T	*qi = &ql_info;
3820     int		found_one = FALSE;
3821     int		buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
3822 
3823     if (!(curbuf->b_has_qf_entry & buf_has_flag))
3824 	return;
3825     if (wp != NULL)
3826     {
3827 	if (wp->w_llist == NULL)
3828 	    return;
3829 	qi = wp->w_llist;
3830     }
3831 
3832     for (idx = 0; idx < qi->qf_listcount; ++idx)
3833     {
3834 	qf_list_T	*qfl = qf_get_list(qi, idx);
3835 
3836 	if (!qf_list_empty(qfl))
3837 	    FOR_ALL_QFL_ITEMS(qfl, qfp, i)
3838 		if (qfp->qf_fnum == curbuf->b_fnum)
3839 		{
3840 		    found_one = TRUE;
3841 		    if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3842 		    {
3843 			if (amount == MAXLNUM)
3844 			    qfp->qf_cleared = TRUE;
3845 			else
3846 			    qfp->qf_lnum += amount;
3847 		    }
3848 		    else if (amount_after && qfp->qf_lnum > line2)
3849 			qfp->qf_lnum += amount_after;
3850 		}
3851     }
3852 
3853     if (!found_one)
3854 	curbuf->b_has_qf_entry &= ~buf_has_flag;
3855 }
3856 
3857 /*
3858  * Make a nice message out of the error character and the error number:
3859  *  char    number	message
3860  *  e or E    0		" error"
3861  *  w or W    0		" warning"
3862  *  i or I    0		" info"
3863  *  0	      0		""
3864  *  other     0		" c"
3865  *  e or E    n		" error n"
3866  *  w or W    n		" warning n"
3867  *  i or I    n		" info n"
3868  *  0	      n		" error n"
3869  *  other     n		" c n"
3870  *  1	      x		""	:helpgrep
3871  */
3872     static char_u *
3873 qf_types(int c, int nr)
3874 {
3875     static char_u	buf[20];
3876     static char_u	cc[3];
3877     char_u		*p;
3878 
3879     if (c == 'W' || c == 'w')
3880 	p = (char_u *)" warning";
3881     else if (c == 'I' || c == 'i')
3882 	p = (char_u *)" info";
3883     else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3884 	p = (char_u *)" error";
3885     else if (c == 0 || c == 1)
3886 	p = (char_u *)"";
3887     else
3888     {
3889 	cc[0] = ' ';
3890 	cc[1] = c;
3891 	cc[2] = NUL;
3892 	p = cc;
3893     }
3894 
3895     if (nr <= 0)
3896 	return p;
3897 
3898     sprintf((char *)buf, "%s %3d", (char *)p, nr);
3899     return buf;
3900 }
3901 
3902 /*
3903  * When "split" is FALSE: Open the entry/result under the cursor.
3904  * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3905  */
3906     void
3907 qf_view_result(int split)
3908 {
3909     qf_info_T   *qi = &ql_info;
3910 
3911     if (!bt_quickfix(curbuf))
3912 	return;
3913 
3914     if (IS_LL_WINDOW(curwin))
3915 	qi = GET_LOC_LIST(curwin);
3916 
3917     if (qf_list_empty(qf_get_curlist(qi)))
3918     {
3919 	emsg(_(e_quickfix));
3920 	return;
3921     }
3922 
3923     if (split)
3924     {
3925 	// Open the selected entry in a new window
3926 	qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3927 	do_cmdline_cmd((char_u *) "clearjumps");
3928 	return;
3929     }
3930 
3931     do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3932 }
3933 
3934 /*
3935  * ":cwindow": open the quickfix window if we have errors to display,
3936  *	       close it if not.
3937  * ":lwindow": open the location list window if we have locations to display,
3938  *	       close it if not.
3939  */
3940     void
3941 ex_cwindow(exarg_T *eap)
3942 {
3943     qf_info_T	*qi;
3944     qf_list_T	*qfl;
3945     win_T	*win;
3946 
3947     if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3948 	return;
3949 
3950     qfl = qf_get_curlist(qi);
3951 
3952     // Look for an existing quickfix window.
3953     win = qf_find_win(qi);
3954 
3955     // If a quickfix window is open but we have no errors to display,
3956     // close the window.  If a quickfix window is not open, then open
3957     // it if we have errors; otherwise, leave it closed.
3958     if (qf_stack_empty(qi)
3959 	    || qfl->qf_nonevalid
3960 	    || qf_list_empty(qfl))
3961     {
3962 	if (win != NULL)
3963 	    ex_cclose(eap);
3964     }
3965     else if (win == NULL)
3966 	ex_copen(eap);
3967 }
3968 
3969 /*
3970  * ":cclose": close the window showing the list of errors.
3971  * ":lclose": close the window showing the location list
3972  */
3973     void
3974 ex_cclose(exarg_T *eap)
3975 {
3976     win_T	*win = NULL;
3977     qf_info_T	*qi;
3978 
3979     if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
3980 	return;
3981 
3982     // Find existing quickfix window and close it.
3983     win = qf_find_win(qi);
3984     if (win != NULL)
3985 	win_close(win, FALSE);
3986 }
3987 
3988 /*
3989  * Set "w:quickfix_title" if "qi" has a title.
3990  */
3991     static void
3992 qf_set_title_var(qf_list_T *qfl)
3993 {
3994     if (qfl->qf_title != NULL)
3995 	set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3996 }
3997 
3998 /*
3999  * Goto a quickfix or location list window (if present).
4000  * Returns OK if the window is found, FAIL otherwise.
4001  */
4002     static int
4003 qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4004 {
4005     win_T	*win;
4006 
4007     win = qf_find_win(qi);
4008     if (win == NULL)
4009 	return FAIL;
4010 
4011     win_goto(win);
4012     if (resize)
4013     {
4014 	if (vertsplit)
4015 	{
4016 	    if (sz != win->w_width)
4017 		win_setwidth(sz);
4018 	}
4019 	else if (sz != win->w_height)
4020 	    win_setheight(sz);
4021     }
4022 
4023     return OK;
4024 }
4025 
4026 /*
4027  * Set options for the buffer in the quickfix or location list window.
4028  */
4029     static void
4030 qf_set_cwindow_options(void)
4031 {
4032     // switch off 'swapfile'
4033     set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4034     set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4035 	    OPT_LOCAL);
4036     set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4037     RESET_BINDING(curwin);
4038 #ifdef FEAT_DIFF
4039     curwin->w_p_diff = FALSE;
4040 #endif
4041 #ifdef FEAT_FOLDING
4042     set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4043 	    OPT_LOCAL);
4044 #endif
4045 }
4046 
4047 /*
4048  * Open a new quickfix or location list window, load the quickfix buffer and
4049  * set the appropriate options for the window.
4050  * Returns FAIL if the window could not be opened.
4051  */
4052     static int
4053 qf_open_new_cwindow(qf_info_T *qi, int height)
4054 {
4055     buf_T	*qf_buf;
4056     win_T	*oldwin = curwin;
4057     tabpage_T	*prevtab = curtab;
4058     int		flags = 0;
4059     win_T	*win;
4060 
4061     qf_buf = qf_find_buf(qi);
4062 
4063     // The current window becomes the previous window afterwards.
4064     win = curwin;
4065 
4066     if (IS_QF_STACK(qi) && cmdmod.split == 0)
4067 	// Create the new quickfix window at the very bottom, except when
4068 	// :belowright or :aboveleft is used.
4069 	win_goto(lastwin);
4070     // Default is to open the window below the current window
4071     if (cmdmod.split == 0)
4072 	flags = WSP_BELOW;
4073     flags |= WSP_NEWLOC;
4074     if (win_split(height, flags) == FAIL)
4075 	return FAIL;		// not enough room for window
4076     RESET_BINDING(curwin);
4077 
4078     if (IS_LL_STACK(qi))
4079     {
4080 	// For the location list window, create a reference to the
4081 	// location list stack from the window 'win'.
4082 	curwin->w_llist_ref = qi;
4083 	qi->qf_refcount++;
4084     }
4085 
4086     if (oldwin != curwin)
4087 	oldwin = NULL;  // don't store info when in another window
4088     if (qf_buf != NULL)
4089     {
4090 	// Use the existing quickfix buffer
4091 	(void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4092 		ECMD_HIDE + ECMD_OLDBUF, oldwin);
4093     }
4094     else
4095     {
4096 	// Create a new quickfix buffer
4097 	(void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4098 
4099 	// save the number of the new buffer
4100 	qi->qf_bufnr = curbuf->b_fnum;
4101     }
4102 
4103     // Set the options for the quickfix buffer/window (if not already done)
4104     // Do this even if the quickfix buffer was already present, as an autocmd
4105     // might have previously deleted (:bdelete) the quickfix buffer.
4106     if (curbuf->b_p_bt[0] != 'q')
4107 	qf_set_cwindow_options();
4108 
4109     // Only set the height when still in the same tab page and there is no
4110     // window to the side.
4111     if (curtab == prevtab && curwin->w_width == Columns)
4112 	win_setheight(height);
4113     curwin->w_p_wfh = TRUE;	    // set 'winfixheight'
4114     if (win_valid(win))
4115 	prevwin = win;
4116 
4117     return OK;
4118 }
4119 
4120 /*
4121  * ":copen": open a window that shows the list of errors.
4122  * ":lopen": open a window that shows the location list.
4123  */
4124     void
4125 ex_copen(exarg_T *eap)
4126 {
4127     qf_info_T	*qi;
4128     qf_list_T	*qfl;
4129     int		height;
4130     int		status = FAIL;
4131     int		lnum;
4132 
4133     if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4134 	return;
4135 
4136     incr_quickfix_busy();
4137 
4138     if (eap->addr_count != 0)
4139 	height = eap->line2;
4140     else
4141 	height = QF_WINHEIGHT;
4142 
4143     reset_VIsual_and_resel();			// stop Visual mode
4144 #ifdef FEAT_GUI
4145     need_mouse_correct = TRUE;
4146 #endif
4147 
4148     // Find an existing quickfix window, or open a new one.
4149     if (cmdmod.tab == 0)
4150 	status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4151 						cmdmod.split & WSP_VERT);
4152     if (status == FAIL)
4153 	if (qf_open_new_cwindow(qi, height) == FAIL)
4154 	{
4155 	    decr_quickfix_busy();
4156 	    return;
4157 	}
4158 
4159     qfl = qf_get_curlist(qi);
4160     qf_set_title_var(qfl);
4161     // Save the current index here, as updating the quickfix buffer may free
4162     // the quickfix list
4163     lnum = qfl->qf_index;
4164 
4165     // Fill the buffer with the quickfix list.
4166     qf_fill_buffer(qfl, curbuf, NULL);
4167 
4168     decr_quickfix_busy();
4169 
4170     curwin->w_cursor.lnum = lnum;
4171     curwin->w_cursor.col = 0;
4172     check_cursor();
4173     update_topline();		// scroll to show the line
4174 }
4175 
4176 /*
4177  * Move the cursor in the quickfix window to "lnum".
4178  */
4179     static void
4180 qf_win_goto(win_T *win, linenr_T lnum)
4181 {
4182     win_T	*old_curwin = curwin;
4183 
4184     curwin = win;
4185     curbuf = win->w_buffer;
4186     curwin->w_cursor.lnum = lnum;
4187     curwin->w_cursor.col = 0;
4188     curwin->w_cursor.coladd = 0;
4189     curwin->w_curswant = 0;
4190     update_topline();		// scroll to show the line
4191     redraw_later(VALID);
4192     curwin->w_redr_status = TRUE;	// update ruler
4193     curwin = old_curwin;
4194     curbuf = curwin->w_buffer;
4195 }
4196 
4197 /*
4198  * :cbottom/:lbottom commands.
4199  */
4200     void
4201 ex_cbottom(exarg_T *eap)
4202 {
4203     qf_info_T	*qi;
4204     win_T	*win;
4205 
4206     if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4207 	return;
4208 
4209     win = qf_find_win(qi);
4210     if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4211 	qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4212 }
4213 
4214 /*
4215  * Return the number of the current entry (line number in the quickfix
4216  * window).
4217  */
4218      linenr_T
4219 qf_current_entry(win_T *wp)
4220 {
4221     qf_info_T	*qi = &ql_info;
4222 
4223     if (IS_LL_WINDOW(wp))
4224 	// In the location list window, use the referenced location list
4225 	qi = wp->w_llist_ref;
4226 
4227     return qf_get_curlist(qi)->qf_index;
4228 }
4229 
4230 /*
4231  * Update the cursor position in the quickfix window to the current error.
4232  * Return TRUE if there is a quickfix window.
4233  */
4234     static int
4235 qf_win_pos_update(
4236     qf_info_T	*qi,
4237     int		old_qf_index)	// previous qf_index or zero
4238 {
4239     win_T	*win;
4240     int		qf_index = qf_get_curlist(qi)->qf_index;
4241 
4242     // Put the cursor on the current error in the quickfix window, so that
4243     // it's viewable.
4244     win = qf_find_win(qi);
4245     if (win != NULL
4246 	    && qf_index <= win->w_buffer->b_ml.ml_line_count
4247 	    && old_qf_index != qf_index)
4248     {
4249 	if (qf_index > old_qf_index)
4250 	{
4251 	    win->w_redraw_top = old_qf_index;
4252 	    win->w_redraw_bot = qf_index;
4253 	}
4254 	else
4255 	{
4256 	    win->w_redraw_top = qf_index;
4257 	    win->w_redraw_bot = old_qf_index;
4258 	}
4259 	qf_win_goto(win, qf_index);
4260     }
4261     return win != NULL;
4262 }
4263 
4264 /*
4265  * Check whether the given window is displaying the specified quickfix/location
4266  * stack.
4267  */
4268     static int
4269 is_qf_win(win_T *win, qf_info_T *qi)
4270 {
4271     // A window displaying the quickfix buffer will have the w_llist_ref field
4272     // set to NULL.
4273     // A window displaying a location list buffer will have the w_llist_ref
4274     // pointing to the location list.
4275     if (bt_quickfix(win->w_buffer))
4276 	if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4277 		|| (IS_LL_STACK(qi) && win->w_llist_ref == qi))
4278 	    return TRUE;
4279 
4280     return FALSE;
4281 }
4282 
4283 /*
4284  * Find a window displaying the quickfix/location stack 'qi'
4285  * Only searches in the current tabpage.
4286  */
4287     static win_T *
4288 qf_find_win(qf_info_T *qi)
4289 {
4290     win_T	*win;
4291 
4292     FOR_ALL_WINDOWS(win)
4293 	if (is_qf_win(win, qi))
4294 	    return win;
4295     return NULL;
4296 }
4297 
4298 /*
4299  * Find a quickfix buffer.
4300  * Searches in windows opened in all the tabs.
4301  */
4302     static buf_T *
4303 qf_find_buf(qf_info_T *qi)
4304 {
4305     tabpage_T	*tp;
4306     win_T	*win;
4307 
4308     if (qi->qf_bufnr != INVALID_QFBUFNR)
4309     {
4310 	buf_T	*qfbuf;
4311 	qfbuf = buflist_findnr(qi->qf_bufnr);
4312 	if (qfbuf != NULL)
4313 	    return qfbuf;
4314 	// buffer is no longer present
4315 	qi->qf_bufnr = INVALID_QFBUFNR;
4316     }
4317 
4318     FOR_ALL_TAB_WINDOWS(tp, win)
4319 	if (is_qf_win(win, qi))
4320 	    return win->w_buffer;
4321 
4322     return NULL;
4323 }
4324 
4325 /*
4326  * Update the w:quickfix_title variable in the quickfix/location list window
4327  */
4328     static void
4329 qf_update_win_titlevar(qf_info_T *qi)
4330 {
4331     win_T	*win;
4332     win_T	*curwin_save;
4333 
4334     if ((win = qf_find_win(qi)) != NULL)
4335     {
4336 	curwin_save = curwin;
4337 	curwin = win;
4338 	qf_set_title_var(qf_get_curlist(qi));
4339 	curwin = curwin_save;
4340     }
4341 }
4342 
4343 /*
4344  * Find the quickfix buffer.  If it exists, update the contents.
4345  */
4346     static void
4347 qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
4348 {
4349     buf_T	*buf;
4350     win_T	*win;
4351     aco_save_T	aco;
4352 
4353     // Check if a buffer for the quickfix list exists.  Update it.
4354     buf = qf_find_buf(qi);
4355     if (buf != NULL)
4356     {
4357 	linenr_T	old_line_count = buf->b_ml.ml_line_count;
4358 
4359 	if (old_last == NULL)
4360 	    // set curwin/curbuf to buf and save a few things
4361 	    aucmd_prepbuf(&aco, buf);
4362 
4363 	qf_update_win_titlevar(qi);
4364 
4365 	qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
4366 	++CHANGEDTICK(buf);
4367 
4368 	if (old_last == NULL)
4369 	{
4370 	    (void)qf_win_pos_update(qi, 0);
4371 
4372 	    // restore curwin/curbuf and a few other things
4373 	    aucmd_restbuf(&aco);
4374 	}
4375 
4376 	// Only redraw when added lines are visible.  This avoids flickering
4377 	// when the added lines are not visible.
4378 	if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4379 	    redraw_buf_later(buf, NOT_VALID);
4380     }
4381 }
4382 
4383 /*
4384  * Add an error line to the quickfix buffer.
4385  */
4386     static int
4387 qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4388 {
4389     int		len;
4390     buf_T	*errbuf;
4391 
4392     if (qfp->qf_module != NULL)
4393     {
4394 	STRCPY(IObuff, qfp->qf_module);
4395 	len = (int)STRLEN(IObuff);
4396     }
4397     else if (qfp->qf_fnum != 0
4398 	    && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4399 	    && errbuf->b_fname != NULL)
4400     {
4401 	if (qfp->qf_type == 1)	// :helpgrep
4402 	    STRCPY(IObuff, gettail(errbuf->b_fname));
4403 	else
4404 	{
4405 	    // shorten the file name if not done already
4406 	    if (errbuf->b_sfname == NULL
4407 		    || mch_isFullName(errbuf->b_sfname))
4408 	    {
4409 		if (*dirname == NUL)
4410 		    mch_dirname(dirname, MAXPATHL);
4411 		shorten_buf_fname(errbuf, dirname, FALSE);
4412 	    }
4413 	    STRCPY(IObuff, errbuf->b_fname);
4414 	}
4415 	len = (int)STRLEN(IObuff);
4416     }
4417     else
4418 	len = 0;
4419     IObuff[len++] = '|';
4420 
4421     if (qfp->qf_lnum > 0)
4422     {
4423 	sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4424 	len += (int)STRLEN(IObuff + len);
4425 
4426 	if (qfp->qf_col > 0)
4427 	{
4428 	    sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4429 	    len += (int)STRLEN(IObuff + len);
4430 	}
4431 
4432 	sprintf((char *)IObuff + len, "%s",
4433 		(char *)qf_types(qfp->qf_type, qfp->qf_nr));
4434 	len += (int)STRLEN(IObuff + len);
4435     }
4436     else if (qfp->qf_pattern != NULL)
4437     {
4438 	qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4439 	len += (int)STRLEN(IObuff + len);
4440     }
4441     IObuff[len++] = '|';
4442     IObuff[len++] = ' ';
4443 
4444     // Remove newlines and leading whitespace from the text.
4445     // For an unrecognized line keep the indent, the compiler may
4446     // mark a word with ^^^^.
4447     qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4448 	    IObuff + len, IOSIZE - len);
4449 
4450     if (ml_append_buf(buf, lnum, IObuff,
4451 		(colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4452 	return FAIL;
4453 
4454     return OK;
4455 }
4456 
4457 /*
4458  * Fill current buffer with quickfix errors, replacing any previous contents.
4459  * curbuf must be the quickfix buffer!
4460  * If "old_last" is not NULL append the items after this one.
4461  * When "old_last" is NULL then "buf" must equal "curbuf"!  Because
4462  * ml_delete() is used and autocommands will be triggered.
4463  */
4464     static void
4465 qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
4466 {
4467     linenr_T	lnum;
4468     qfline_T	*qfp;
4469     int		old_KeyTyped = KeyTyped;
4470 
4471     if (old_last == NULL)
4472     {
4473 	if (buf != curbuf)
4474 	{
4475 	    internal_error("qf_fill_buffer()");
4476 	    return;
4477 	}
4478 
4479 	// delete all existing lines
4480 	while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4481 	    (void)ml_delete((linenr_T)1, FALSE);
4482     }
4483 
4484     // Check if there is anything to display
4485     if (qfl != NULL)
4486     {
4487 	char_u		dirname[MAXPATHL];
4488 
4489 	*dirname = NUL;
4490 
4491 	// Add one line for each error
4492 	if (old_last == NULL)
4493 	{
4494 	    qfp = qfl->qf_start;
4495 	    lnum = 0;
4496 	}
4497 	else
4498 	{
4499 	    qfp = old_last->qf_next;
4500 	    lnum = buf->b_ml.ml_line_count;
4501 	}
4502 	while (lnum < qfl->qf_count)
4503 	{
4504 	    if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
4505 		break;
4506 
4507 	    ++lnum;
4508 	    qfp = qfp->qf_next;
4509 	    if (qfp == NULL)
4510 		break;
4511 	}
4512 
4513 	if (old_last == NULL)
4514 	    // Delete the empty line which is now at the end
4515 	    (void)ml_delete(lnum + 1, FALSE);
4516     }
4517 
4518     // correct cursor position
4519     check_lnums(TRUE);
4520 
4521     if (old_last == NULL)
4522     {
4523 	// Set the 'filetype' to "qf" each time after filling the buffer.
4524 	// This resembles reading a file into a buffer, it's more logical when
4525 	// using autocommands.
4526 	++curbuf_lock;
4527 	set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4528 	curbuf->b_p_ma = FALSE;
4529 
4530 	keep_filetype = TRUE;		// don't detect 'filetype'
4531 	apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
4532 							       FALSE, curbuf);
4533 	apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
4534 							       FALSE, curbuf);
4535 	keep_filetype = FALSE;
4536 	--curbuf_lock;
4537 
4538 	// make sure it will be redrawn
4539 	redraw_curbuf_later(NOT_VALID);
4540     }
4541 
4542     // Restore KeyTyped, setting 'filetype' may reset it.
4543     KeyTyped = old_KeyTyped;
4544 }
4545 
4546 /*
4547  * For every change made to the quickfix list, update the changed tick.
4548  */
4549     static void
4550 qf_list_changed(qf_list_T *qfl)
4551 {
4552     qfl->qf_changedtick++;
4553 }
4554 
4555 /*
4556  * Return the quickfix/location list number with the given identifier.
4557  * Returns -1 if list is not found.
4558  */
4559     static int
4560 qf_id2nr(qf_info_T *qi, int_u qfid)
4561 {
4562     int		qf_idx;
4563 
4564     for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4565 	if (qi->qf_lists[qf_idx].qf_id == qfid)
4566 	    return qf_idx;
4567     return INVALID_QFIDX;
4568 }
4569 
4570 /*
4571  * If the current list is not "save_qfid" and we can find the list with that ID
4572  * then make it the current list.
4573  * This is used when autocommands may have changed the current list.
4574  * Returns OK if successfully restored the list. Returns FAIL if the list with
4575  * the specified identifier (save_qfid) is not found in the stack.
4576  */
4577     static int
4578 qf_restore_list(qf_info_T *qi, int_u save_qfid)
4579 {
4580     int curlist;
4581 
4582     if (qf_get_curlist(qi)->qf_id != save_qfid)
4583     {
4584 	curlist = qf_id2nr(qi, save_qfid);
4585 	if (curlist < 0)
4586 	    // list is not present
4587 	    return FAIL;
4588 	qi->qf_curlist = curlist;
4589     }
4590     return OK;
4591 }
4592 
4593 /*
4594  * Jump to the first entry if there is one.
4595  */
4596     static void
4597 qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4598 {
4599     if (qf_restore_list(qi, save_qfid) == FAIL)
4600 	return;
4601 
4602     // Autocommands might have cleared the list, check for that.
4603     if (!qf_list_empty(qf_get_curlist(qi)))
4604 	qf_jump(qi, 0, 0, forceit);
4605 }
4606 
4607 /*
4608  * Return TRUE when using ":vimgrep" for ":grep".
4609  */
4610     int
4611 grep_internal(cmdidx_T cmdidx)
4612 {
4613     return ((cmdidx == CMD_grep
4614 		|| cmdidx == CMD_lgrep
4615 		|| cmdidx == CMD_grepadd
4616 		|| cmdidx == CMD_lgrepadd)
4617 	    && STRCMP("internal",
4618 			*curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4619 }
4620 
4621 /*
4622  * Return the make/grep autocmd name.
4623  */
4624     static char_u *
4625 make_get_auname(cmdidx_T cmdidx)
4626 {
4627     switch (cmdidx)
4628     {
4629 	case CMD_make:	    return (char_u *)"make";
4630 	case CMD_lmake:	    return (char_u *)"lmake";
4631 	case CMD_grep:	    return (char_u *)"grep";
4632 	case CMD_lgrep:	    return (char_u *)"lgrep";
4633 	case CMD_grepadd:   return (char_u *)"grepadd";
4634 	case CMD_lgrepadd:  return (char_u *)"lgrepadd";
4635 	default: return NULL;
4636     }
4637 }
4638 
4639 /*
4640  * Return the name for the errorfile, in allocated memory.
4641  * Find a new unique name when 'makeef' contains "##".
4642  * Returns NULL for error.
4643  */
4644     static char_u *
4645 get_mef_name(void)
4646 {
4647     char_u	*p;
4648     char_u	*name;
4649     static int	start = -1;
4650     static int	off = 0;
4651 #ifdef HAVE_LSTAT
4652     stat_T	sb;
4653 #endif
4654 
4655     if (*p_mef == NUL)
4656     {
4657 	name = vim_tempname('e', FALSE);
4658 	if (name == NULL)
4659 	    emsg(_(e_notmp));
4660 	return name;
4661     }
4662 
4663     for (p = p_mef; *p; ++p)
4664 	if (p[0] == '#' && p[1] == '#')
4665 	    break;
4666 
4667     if (*p == NUL)
4668 	return vim_strsave(p_mef);
4669 
4670     // Keep trying until the name doesn't exist yet.
4671     for (;;)
4672     {
4673 	if (start == -1)
4674 	    start = mch_get_pid();
4675 	else
4676 	    off += 19;
4677 
4678 	name = alloc((unsigned)STRLEN(p_mef) + 30);
4679 	if (name == NULL)
4680 	    break;
4681 	STRCPY(name, p_mef);
4682 	sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4683 	STRCAT(name, p + 2);
4684 	if (mch_getperm(name) < 0
4685 #ifdef HAVE_LSTAT
4686 		    // Don't accept a symbolic link, it's a security risk.
4687 		    && mch_lstat((char *)name, &sb) < 0
4688 #endif
4689 		)
4690 	    break;
4691 	vim_free(name);
4692     }
4693     return name;
4694 }
4695 
4696 /*
4697  * Form the complete command line to invoke 'make'/'grep'. Quote the command
4698  * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4699  */
4700     static char_u *
4701 make_get_fullcmd(char_u *makecmd, char_u *fname)
4702 {
4703     char_u	*cmd;
4704     unsigned	len;
4705 
4706     len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4707     if (*p_sp != NUL)
4708 	len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4709     cmd = alloc(len);
4710     if (cmd == NULL)
4711 	return NULL;
4712     sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4713 							       (char *)p_shq);
4714 
4715     // If 'shellpipe' empty: don't redirect to 'errorfile'.
4716     if (*p_sp != NUL)
4717 	append_redir(cmd, len, p_sp, fname);
4718 
4719     // Display the fully formed command.  Output a newline if there's something
4720     // else than the :make command that was typed (in which case the cursor is
4721     // in column 0).
4722     if (msg_col == 0)
4723 	msg_didout = FALSE;
4724     msg_start();
4725     msg_puts(":!");
4726     msg_outtrans(cmd);		// show what we are doing
4727 
4728     return cmd;
4729 }
4730 
4731 /*
4732  * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4733  */
4734     void
4735 ex_make(exarg_T *eap)
4736 {
4737     char_u	*fname;
4738     char_u	*cmd;
4739     char_u	*enc = NULL;
4740     win_T	*wp = NULL;
4741     qf_info_T	*qi = &ql_info;
4742     int		res;
4743     char_u	*au_name = NULL;
4744     int_u	save_qfid;
4745 
4746     // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4747     if (grep_internal(eap->cmdidx))
4748     {
4749 	ex_vimgrep(eap);
4750 	return;
4751     }
4752 
4753     au_name = make_get_auname(eap->cmdidx);
4754     if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4755 					       curbuf->b_fname, TRUE, curbuf))
4756     {
4757 #ifdef FEAT_EVAL
4758 	if (aborting())
4759 	    return;
4760 #endif
4761     }
4762     enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4763 
4764     if (is_loclist_cmd(eap->cmdidx))
4765 	wp = curwin;
4766 
4767     autowrite_all();
4768     fname = get_mef_name();
4769     if (fname == NULL)
4770 	return;
4771     mch_remove(fname);	    // in case it's not unique
4772 
4773     cmd = make_get_fullcmd(eap->arg, fname);
4774     if (cmd == NULL)
4775 	return;
4776 
4777     // let the shell know if we are redirecting output or not
4778     do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4779 
4780 #ifdef AMIGA
4781     out_flush();
4782 		// read window status report and redraw before message
4783     (void)char_avail();
4784 #endif
4785 
4786     incr_quickfix_busy();
4787 
4788     res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4789 			    && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4790 					   (eap->cmdidx != CMD_grepadd
4791 					    && eap->cmdidx != CMD_lgrepadd),
4792 					   qf_cmdtitle(*eap->cmdlinep), enc);
4793     if (wp != NULL)
4794     {
4795 	qi = GET_LOC_LIST(wp);
4796 	if (qi == NULL)
4797 	    goto cleanup;
4798     }
4799     if (res >= 0)
4800 	qf_list_changed(qf_get_curlist(qi));
4801 
4802     // Remember the current quickfix list identifier, so that we can
4803     // check for autocommands changing the current quickfix list.
4804     save_qfid = qf_get_curlist(qi)->qf_id;
4805     if (au_name != NULL)
4806 	apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4807 					       curbuf->b_fname, TRUE, curbuf);
4808     if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4809 	// display the first error
4810 	qf_jump_first(qi, save_qfid, FALSE);
4811 
4812 cleanup:
4813     decr_quickfix_busy();
4814     mch_remove(fname);
4815     vim_free(fname);
4816     vim_free(cmd);
4817 }
4818 
4819 /*
4820  * Returns the number of valid entries in the current quickfix/location list.
4821  */
4822     int
4823 qf_get_size(exarg_T *eap)
4824 {
4825     qf_info_T	*qi;
4826     qf_list_T	*qfl;
4827     qfline_T	*qfp;
4828     int		i, sz = 0;
4829     int		prev_fnum = 0;
4830 
4831     if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4832 	return 0;
4833 
4834     qfl = qf_get_curlist(qi);
4835     FOR_ALL_QFL_ITEMS(qfl, qfp, i)
4836     {
4837 	if (qfp->qf_valid)
4838 	{
4839 	    if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4840 		sz++;	// Count all valid entries
4841 	    else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4842 	    {
4843 		// Count the number of files
4844 		sz++;
4845 		prev_fnum = qfp->qf_fnum;
4846 	    }
4847 	}
4848     }
4849 
4850     return sz;
4851 }
4852 
4853 /*
4854  * Returns the current index of the quickfix/location list.
4855  * Returns 0 if there is an error.
4856  */
4857     int
4858 qf_get_cur_idx(exarg_T *eap)
4859 {
4860     qf_info_T	*qi;
4861 
4862     if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4863 	return 0;
4864 
4865     return qf_get_curlist(qi)->qf_index;
4866 }
4867 
4868 /*
4869  * Returns the current index in the quickfix/location list (counting only valid
4870  * entries). If no valid entries are in the list, then returns 1.
4871  */
4872     int
4873 qf_get_cur_valid_idx(exarg_T *eap)
4874 {
4875     qf_info_T	*qi;
4876     qf_list_T	*qfl;
4877     qfline_T	*qfp;
4878     int		i, eidx = 0;
4879     int		prev_fnum = 0;
4880 
4881     if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4882 	return 1;
4883 
4884     qfl = qf_get_curlist(qi);
4885     qfp = qfl->qf_start;
4886 
4887     // check if the list has valid errors
4888     if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4889 	return 1;
4890 
4891     for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4892     {
4893 	if (qfp->qf_valid)
4894 	{
4895 	    if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4896 	    {
4897 		if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4898 		{
4899 		    // Count the number of files
4900 		    eidx++;
4901 		    prev_fnum = qfp->qf_fnum;
4902 		}
4903 	    }
4904 	    else
4905 		eidx++;
4906 	}
4907     }
4908 
4909     return eidx ? eidx : 1;
4910 }
4911 
4912 /*
4913  * Get the 'n'th valid error entry in the quickfix or location list.
4914  * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4915  * For :cdo and :ldo returns the 'n'th valid error entry.
4916  * For :cfdo and :lfdo returns the 'n'th valid file entry.
4917  */
4918     static int
4919 qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
4920 {
4921     qfline_T	*qfp;
4922     int		i, eidx;
4923     int		prev_fnum = 0;
4924 
4925     // check if the list has valid errors
4926     if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4927 	return 1;
4928 
4929     eidx = 0;
4930     FOR_ALL_QFL_ITEMS(qfl, qfp, i)
4931     {
4932 	if (qfp->qf_valid)
4933 	{
4934 	    if (fdo)
4935 	    {
4936 		if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4937 		{
4938 		    // Count the number of files
4939 		    eidx++;
4940 		    prev_fnum = qfp->qf_fnum;
4941 		}
4942 	    }
4943 	    else
4944 		eidx++;
4945 	}
4946 
4947 	if (eidx == n)
4948 	    break;
4949     }
4950 
4951     if (i <= qfl->qf_count)
4952 	return i;
4953     else
4954 	return 1;
4955 }
4956 
4957 /*
4958  * ":cc", ":crewind", ":cfirst" and ":clast".
4959  * ":ll", ":lrewind", ":lfirst" and ":llast".
4960  * ":cdo", ":ldo", ":cfdo" and ":lfdo"
4961  */
4962     void
4963 ex_cc(exarg_T *eap)
4964 {
4965     qf_info_T	*qi;
4966     int		errornr;
4967 
4968     if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4969 	return;
4970 
4971     if (eap->addr_count > 0)
4972 	errornr = (int)eap->line2;
4973     else
4974     {
4975 	switch (eap->cmdidx)
4976 	{
4977 	    case CMD_cc: case CMD_ll:
4978 		errornr = 0;
4979 		break;
4980 	    case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4981 	    case CMD_lfirst:
4982 		errornr = 1;
4983 		break;
4984 	    default:
4985 		errornr = 32767;
4986 	}
4987     }
4988 
4989     // For cdo and ldo commands, jump to the nth valid error.
4990     // For cfdo and lfdo commands, jump to the nth valid file entry.
4991     if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4992 	    || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4993 	errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
4994 		eap->addr_count > 0 ? (int)eap->line1 : 1,
4995 		eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4996 
4997     qf_jump(qi, 0, errornr, eap->forceit);
4998 }
4999 
5000 /*
5001  * ":cnext", ":cnfile", ":cNext" and ":cprevious".
5002  * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
5003  * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
5004  */
5005     void
5006 ex_cnext(exarg_T *eap)
5007 {
5008     qf_info_T	*qi;
5009     int		errornr;
5010     int		dir;
5011 
5012     if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5013 	return;
5014 
5015     if (eap->addr_count > 0
5016 	    && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5017 		&& eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
5018 	errornr = (int)eap->line2;
5019     else
5020 	errornr = 1;
5021 
5022     // Depending on the command jump to either next or previous entry/file.
5023     switch (eap->cmdidx)
5024     {
5025 	case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5026 	    dir = FORWARD;
5027 	    break;
5028 	case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5029 	case CMD_lNext:
5030 	    dir = BACKWARD;
5031 	    break;
5032 	case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5033 	    dir = FORWARD_FILE;
5034 	    break;
5035 	case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5036 	    dir = BACKWARD_FILE;
5037 	    break;
5038 	default:
5039 	    dir = FORWARD;
5040 	    break;
5041     }
5042 
5043     qf_jump(qi, dir, errornr, eap->forceit);
5044 }
5045 
5046 /*
5047  * Return the autocmd name for the :cfile Ex commands
5048  */
5049     static char_u *
5050 cfile_get_auname(cmdidx_T cmdidx)
5051 {
5052     switch (cmdidx)
5053     {
5054 	case CMD_cfile:	    return (char_u *)"cfile";
5055 	case CMD_cgetfile:  return (char_u *)"cgetfile";
5056 	case CMD_caddfile:  return (char_u *)"caddfile";
5057 	case CMD_lfile:	    return (char_u *)"lfile";
5058 	case CMD_lgetfile:  return (char_u *)"lgetfile";
5059 	case CMD_laddfile:  return (char_u *)"laddfile";
5060 	default:	    return NULL;
5061     }
5062 }
5063 
5064 /*
5065  * ":cfile"/":cgetfile"/":caddfile" commands.
5066  * ":lfile"/":lgetfile"/":laddfile" commands.
5067  */
5068     void
5069 ex_cfile(exarg_T *eap)
5070 {
5071     char_u	*enc = NULL;
5072     win_T	*wp = NULL;
5073     qf_info_T	*qi = &ql_info;
5074     char_u	*au_name = NULL;
5075     int_u	save_qfid = 0;		// init for gcc
5076     int		res;
5077 
5078     au_name = cfile_get_auname(eap->cmdidx);
5079     if (au_name != NULL)
5080 	apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
5081 
5082     enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
5083 #ifdef FEAT_BROWSE
5084     if (cmdmod.browse)
5085     {
5086 	char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
5087 				   NULL, NULL,
5088 				   (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
5089 	if (browse_file == NULL)
5090 	    return;
5091 	set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5092 	vim_free(browse_file);
5093     }
5094     else
5095 #endif
5096     if (*eap->arg != NUL)
5097 	set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
5098 
5099     if (is_loclist_cmd(eap->cmdidx))
5100 	wp = curwin;
5101 
5102     incr_quickfix_busy();
5103 
5104     // This function is used by the :cfile, :cgetfile and :caddfile
5105     // commands.
5106     // :cfile always creates a new quickfix list and jumps to the
5107     // first error.
5108     // :cgetfile creates a new quickfix list but doesn't jump to the
5109     // first error.
5110     // :caddfile adds to an existing quickfix list. If there is no
5111     // quickfix list then a new list is created.
5112     res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
5113 			&& eap->cmdidx != CMD_laddfile),
5114 			qf_cmdtitle(*eap->cmdlinep), enc);
5115     if (wp != NULL)
5116     {
5117 	qi = GET_LOC_LIST(wp);
5118 	if (qi == NULL)
5119 	{
5120 	    decr_quickfix_busy();
5121 	    return;
5122 	}
5123     }
5124     if (res >= 0)
5125 	qf_list_changed(qf_get_curlist(qi));
5126     save_qfid = qf_get_curlist(qi)->qf_id;
5127     if (au_name != NULL)
5128 	apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
5129 
5130     // Jump to the first error for a new list and if autocmds didn't
5131     // free the list.
5132     if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5133 	    && qflist_valid(wp, save_qfid))
5134 	// display the first error
5135 	qf_jump_first(qi, save_qfid, eap->forceit);
5136 
5137     decr_quickfix_busy();
5138 }
5139 
5140 /*
5141  * Return the vimgrep autocmd name.
5142  */
5143     static char_u *
5144 vgr_get_auname(cmdidx_T cmdidx)
5145 {
5146     switch (cmdidx)
5147     {
5148 	case CMD_vimgrep:     return (char_u *)"vimgrep";
5149 	case CMD_lvimgrep:    return (char_u *)"lvimgrep";
5150 	case CMD_vimgrepadd:  return (char_u *)"vimgrepadd";
5151 	case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5152 	case CMD_grep:	      return (char_u *)"grep";
5153 	case CMD_lgrep:	      return (char_u *)"lgrep";
5154 	case CMD_grepadd:     return (char_u *)"grepadd";
5155 	case CMD_lgrepadd:    return (char_u *)"lgrepadd";
5156 	default: return NULL;
5157     }
5158 }
5159 
5160 /*
5161  * Initialize the regmatch used by vimgrep for pattern "s".
5162  */
5163     static void
5164 vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5165 {
5166     // Get the search pattern: either white-separated or enclosed in //
5167     regmatch->regprog = NULL;
5168 
5169     if (s == NULL || *s == NUL)
5170     {
5171 	// Pattern is empty, use last search pattern.
5172 	if (last_search_pat() == NULL)
5173 	{
5174 	    emsg(_(e_noprevre));
5175 	    return;
5176 	}
5177 	regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5178     }
5179     else
5180 	regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5181 
5182     regmatch->rmm_ic = p_ic;
5183     regmatch->rmm_maxcol = 0;
5184 }
5185 
5186 /*
5187  * Display a file name when vimgrep is running.
5188  */
5189     static void
5190 vgr_display_fname(char_u *fname)
5191 {
5192     char_u	*p;
5193 
5194     msg_start();
5195     p = msg_strtrunc(fname, TRUE);
5196     if (p == NULL)
5197 	msg_outtrans(fname);
5198     else
5199     {
5200 	msg_outtrans(p);
5201 	vim_free(p);
5202     }
5203     msg_clr_eos();
5204     msg_didout = FALSE;	    // overwrite this message
5205     msg_nowait = TRUE;	    // don't wait for this message
5206     msg_col = 0;
5207     out_flush();
5208 }
5209 
5210 /*
5211  * Load a dummy buffer to search for a pattern using vimgrep.
5212  */
5213     static buf_T *
5214 vgr_load_dummy_buf(
5215 	char_u *fname,
5216 	char_u *dirname_start,
5217 	char_u *dirname_now)
5218 {
5219     int		save_mls;
5220 #if defined(FEAT_SYN_HL)
5221     char_u	*save_ei = NULL;
5222 #endif
5223     buf_T	*buf;
5224 
5225 #if defined(FEAT_SYN_HL)
5226     // Don't do Filetype autocommands to avoid loading syntax and
5227     // indent scripts, a great speed improvement.
5228     save_ei = au_event_disable(",Filetype");
5229 #endif
5230     // Don't use modelines here, it's useless.
5231     save_mls = p_mls;
5232     p_mls = 0;
5233 
5234     // Load file into a buffer, so that 'fileencoding' is detected,
5235     // autocommands applied, etc.
5236     buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5237 
5238     p_mls = save_mls;
5239 #if defined(FEAT_SYN_HL)
5240     au_event_restore(save_ei);
5241 #endif
5242 
5243     return buf;
5244 }
5245 
5246 /*
5247  * Check whether a quickfix/location list is valid. Autocmds may remove or
5248  * change a quickfix list when vimgrep is running. If the list is not found,
5249  * create a new list.
5250  */
5251     static int
5252 vgr_qflist_valid(
5253 	win_T	    *wp,
5254 	qf_info_T   *qi,
5255 	int_u	    qfid,
5256 	char_u	    *title)
5257 {
5258     // Verify that the quickfix/location list was not freed by an autocmd
5259     if (!qflist_valid(wp, qfid))
5260     {
5261 	if (wp != NULL)
5262 	{
5263 	    // An autocmd has freed the location list.
5264 	    emsg(_(e_loc_list_changed));
5265 	    return FALSE;
5266 	}
5267 	else
5268 	{
5269 	    // Quickfix list is not found, create a new one.
5270 	    qf_new_list(qi, title);
5271 	    return TRUE;
5272 	}
5273     }
5274 
5275     if (qf_restore_list(qi, qfid) == FAIL)
5276 	return FALSE;
5277 
5278     return TRUE;
5279 }
5280 
5281 /*
5282  * Search for a pattern in all the lines in a buffer and add the matching lines
5283  * to a quickfix list.
5284  */
5285     static int
5286 vgr_match_buflines(
5287 	qf_list_T   *qfl,
5288 	char_u	    *fname,
5289 	buf_T	    *buf,
5290 	regmmatch_T *regmatch,
5291 	long	    *tomatch,
5292 	int	    duplicate_name,
5293 	int	    flags)
5294 {
5295     int		found_match = FALSE;
5296     long	lnum;
5297     colnr_T	col;
5298 
5299     for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
5300     {
5301 	col = 0;
5302 	while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5303 		    col, NULL, NULL) > 0)
5304 	{
5305 	    // Pass the buffer number so that it gets used even for a
5306 	    // dummy buffer, unless duplicate_name is set, then the
5307 	    // buffer will be wiped out below.
5308 	    if (qf_add_entry(qfl,
5309 			NULL,       // dir
5310 			fname,
5311 			NULL,
5312 			duplicate_name ? 0 : buf->b_fnum,
5313 			ml_get_buf(buf,
5314 			    regmatch->startpos[0].lnum + lnum, FALSE),
5315 			regmatch->startpos[0].lnum + lnum,
5316 			regmatch->startpos[0].col + 1,
5317 			FALSE,      // vis_col
5318 			NULL,	    // search pattern
5319 			0,	    // nr
5320 			0,	    // type
5321 			TRUE	    // valid
5322 			) == QF_FAIL)
5323 	    {
5324 		got_int = TRUE;
5325 		break;
5326 	    }
5327 	    found_match = TRUE;
5328 	    if (--*tomatch == 0)
5329 		break;
5330 	    if ((flags & VGR_GLOBAL) == 0
5331 		    || regmatch->endpos[0].lnum > 0)
5332 		break;
5333 	    col = regmatch->endpos[0].col
5334 		+ (col == regmatch->endpos[0].col);
5335 	    if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5336 		break;
5337 	}
5338 	line_breakcheck();
5339 	if (got_int)
5340 	    break;
5341     }
5342 
5343     return found_match;
5344 }
5345 
5346 /*
5347  * Jump to the first match and update the directory.
5348  */
5349     static void
5350 vgr_jump_to_match(
5351 	qf_info_T   *qi,
5352 	int	    forceit,
5353 	int	    *redraw_for_dummy,
5354 	buf_T	    *first_match_buf,
5355 	char_u	    *target_dir)
5356 {
5357     buf_T	*buf;
5358 
5359     buf = curbuf;
5360     qf_jump(qi, 0, 0, forceit);
5361     if (buf != curbuf)
5362 	// If we jumped to another buffer redrawing will already be
5363 	// taken care of.
5364 	*redraw_for_dummy = FALSE;
5365 
5366     // Jump to the directory used after loading the buffer.
5367     if (curbuf == first_match_buf && target_dir != NULL)
5368     {
5369 	exarg_T ea;
5370 
5371 	ea.arg = target_dir;
5372 	ea.cmdidx = CMD_lcd;
5373 	ex_cd(&ea);
5374     }
5375 }
5376 
5377 /*
5378  * ":vimgrep {pattern} file(s)"
5379  * ":vimgrepadd {pattern} file(s)"
5380  * ":lvimgrep {pattern} file(s)"
5381  * ":lvimgrepadd {pattern} file(s)"
5382  */
5383     void
5384 ex_vimgrep(exarg_T *eap)
5385 {
5386     regmmatch_T	regmatch;
5387     int		fcount;
5388     char_u	**fnames;
5389     char_u	*fname;
5390     char_u	*title;
5391     char_u	*s;
5392     char_u	*p;
5393     int		fi;
5394     qf_info_T	*qi;
5395     qf_list_T	*qfl;
5396     int_u	save_qfid;
5397     win_T	*wp = NULL;
5398     buf_T	*buf;
5399     int		duplicate_name = FALSE;
5400     int		using_dummy;
5401     int		redraw_for_dummy = FALSE;
5402     int		found_match;
5403     buf_T	*first_match_buf = NULL;
5404     time_t	seconds = 0;
5405     aco_save_T	aco;
5406     int		flags = 0;
5407     long	tomatch;
5408     char_u	*dirname_start = NULL;
5409     char_u	*dirname_now = NULL;
5410     char_u	*target_dir = NULL;
5411     char_u	*au_name =  NULL;
5412 
5413     au_name = vgr_get_auname(eap->cmdidx);
5414     if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5415 					       curbuf->b_fname, TRUE, curbuf))
5416     {
5417 #ifdef FEAT_EVAL
5418 	if (aborting())
5419 	    return;
5420 #endif
5421     }
5422 
5423     qi = qf_cmd_get_or_alloc_stack(eap, &wp);
5424     if (qi == NULL)
5425 	return;
5426 
5427     if (eap->addr_count > 0)
5428 	tomatch = eap->line2;
5429     else
5430 	tomatch = MAXLNUM;
5431 
5432     // Get the search pattern: either white-separated or enclosed in //
5433     regmatch.regprog = NULL;
5434     title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
5435     p = skip_vimgrep_pat(eap->arg, &s, &flags);
5436     if (p == NULL)
5437     {
5438 	emsg(_(e_invalpat));
5439 	goto theend;
5440     }
5441 
5442     vgr_init_regmatch(&regmatch, s);
5443     if (regmatch.regprog == NULL)
5444 	goto theend;
5445 
5446     p = skipwhite(p);
5447     if (*p == NUL)
5448     {
5449 	emsg(_("E683: File name missing or invalid pattern"));
5450 	goto theend;
5451     }
5452 
5453     if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
5454 		&& eap->cmdidx != CMD_vimgrepadd
5455 		&& eap->cmdidx != CMD_lvimgrepadd)
5456 					|| qf_stack_empty(qi))
5457 	// make place for a new list
5458 	qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
5459 
5460     // parse the list of arguments
5461     if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
5462 	goto theend;
5463     if (fcount == 0)
5464     {
5465 	emsg(_(e_nomatch));
5466 	goto theend;
5467     }
5468 
5469     dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5470     dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
5471     if (dirname_start == NULL || dirname_now == NULL)
5472     {
5473 	FreeWild(fcount, fnames);
5474 	goto theend;
5475     }
5476 
5477     // Remember the current directory, because a BufRead autocommand that does
5478     // ":lcd %:p:h" changes the meaning of short path names.
5479     mch_dirname(dirname_start, MAXPATHL);
5480 
5481     incr_quickfix_busy();
5482 
5483     // Remember the current quickfix list identifier, so that we can check for
5484     // autocommands changing the current quickfix list.
5485     save_qfid = qf_get_curlist(qi)->qf_id;
5486 
5487     seconds = (time_t)0;
5488     for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
5489     {
5490 	fname = shorten_fname1(fnames[fi]);
5491 	if (time(NULL) > seconds)
5492 	{
5493 	    // Display the file name every second or so, show the user we are
5494 	    // working on it.
5495 	    seconds = time(NULL);
5496 	    vgr_display_fname(fname);
5497 	}
5498 
5499 	buf = buflist_findname_exp(fnames[fi]);
5500 	if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5501 	{
5502 	    // Remember that a buffer with this name already exists.
5503 	    duplicate_name = (buf != NULL);
5504 	    using_dummy = TRUE;
5505 	    redraw_for_dummy = TRUE;
5506 
5507 	    buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
5508 	}
5509 	else
5510 	    // Use existing, loaded buffer.
5511 	    using_dummy = FALSE;
5512 
5513 	// Check whether the quickfix list is still valid. When loading a
5514 	// buffer above, autocommands might have changed the quickfix list.
5515 	if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
5516 	{
5517 	    FreeWild(fcount, fnames);
5518 	    decr_quickfix_busy();
5519 	    goto theend;
5520 	}
5521 	save_qfid = qf_get_curlist(qi)->qf_id;
5522 
5523 	if (buf == NULL)
5524 	{
5525 	    if (!got_int)
5526 		smsg(_("Cannot open file \"%s\""), fname);
5527 	}
5528 	else
5529 	{
5530 	    // Try for a match in all lines of the buffer.
5531 	    // For ":1vimgrep" look for first match only.
5532 	    found_match = vgr_match_buflines(qf_get_curlist(qi),
5533 		    fname, buf, &regmatch,
5534 		    &tomatch, duplicate_name, flags);
5535 
5536 	    if (using_dummy)
5537 	    {
5538 		if (found_match && first_match_buf == NULL)
5539 		    first_match_buf = buf;
5540 		if (duplicate_name)
5541 		{
5542 		    // Never keep a dummy buffer if there is another buffer
5543 		    // with the same name.
5544 		    wipe_dummy_buffer(buf, dirname_start);
5545 		    buf = NULL;
5546 		}
5547 		else if (!cmdmod.hide
5548 			    || buf->b_p_bh[0] == 'u'	// "unload"
5549 			    || buf->b_p_bh[0] == 'w'	// "wipe"
5550 			    || buf->b_p_bh[0] == 'd')	// "delete"
5551 		{
5552 		    // When no match was found we don't need to remember the
5553 		    // buffer, wipe it out.  If there was a match and it
5554 		    // wasn't the first one or we won't jump there: only
5555 		    // unload the buffer.
5556 		    // Ignore 'hidden' here, because it may lead to having too
5557 		    // many swap files.
5558 		    if (!found_match)
5559 		    {
5560 			wipe_dummy_buffer(buf, dirname_start);
5561 			buf = NULL;
5562 		    }
5563 		    else if (buf != first_match_buf || (flags & VGR_NOJUMP))
5564 		    {
5565 			unload_dummy_buffer(buf, dirname_start);
5566 			// Keeping the buffer, remove the dummy flag.
5567 			buf->b_flags &= ~BF_DUMMY;
5568 			buf = NULL;
5569 		    }
5570 		}
5571 
5572 		if (buf != NULL)
5573 		{
5574 		    // Keeping the buffer, remove the dummy flag.
5575 		    buf->b_flags &= ~BF_DUMMY;
5576 
5577 		    // If the buffer is still loaded we need to use the
5578 		    // directory we jumped to below.
5579 		    if (buf == first_match_buf
5580 			    && target_dir == NULL
5581 			    && STRCMP(dirname_start, dirname_now) != 0)
5582 			target_dir = vim_strsave(dirname_now);
5583 
5584 		    // The buffer is still loaded, the Filetype autocommands
5585 		    // need to be done now, in that buffer.  And the modelines
5586 		    // need to be done (again).  But not the window-local
5587 		    // options!
5588 		    aucmd_prepbuf(&aco, buf);
5589 #if defined(FEAT_SYN_HL)
5590 		    apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5591 						     buf->b_fname, TRUE, buf);
5592 #endif
5593 		    do_modelines(OPT_NOWIN);
5594 		    aucmd_restbuf(&aco);
5595 		}
5596 	    }
5597 	}
5598     }
5599 
5600     FreeWild(fcount, fnames);
5601 
5602     qfl = qf_get_curlist(qi);
5603     qfl->qf_nonevalid = FALSE;
5604     qfl->qf_ptr = qfl->qf_start;
5605     qfl->qf_index = 1;
5606     qf_list_changed(qfl);
5607 
5608     qf_update_buffer(qi, NULL);
5609 
5610     if (au_name != NULL)
5611 	apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5612 					       curbuf->b_fname, TRUE, curbuf);
5613     // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5614     // is still valid.
5615     if (!qflist_valid(wp, save_qfid)
5616 	    || qf_restore_list(qi, save_qfid) == FAIL)
5617     {
5618 	decr_quickfix_busy();
5619 	goto theend;
5620     }
5621 
5622     // Jump to first match.
5623     if (!qf_list_empty(qf_get_curlist(qi)))
5624     {
5625 	if ((flags & VGR_NOJUMP) == 0)
5626 	    vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5627 		    first_match_buf, target_dir);
5628     }
5629     else
5630 	semsg(_(e_nomatch2), s);
5631 
5632     decr_quickfix_busy();
5633 
5634     // If we loaded a dummy buffer into the current window, the autocommands
5635     // may have messed up things, need to redraw and recompute folds.
5636     if (redraw_for_dummy)
5637     {
5638 #ifdef FEAT_FOLDING
5639 	foldUpdateAll(curwin);
5640 #else
5641 	redraw_later(NOT_VALID);
5642 #endif
5643     }
5644 
5645 theend:
5646     vim_free(title);
5647     vim_free(dirname_now);
5648     vim_free(dirname_start);
5649     vim_free(target_dir);
5650     vim_regfree(regmatch.regprog);
5651 }
5652 
5653 /*
5654  * Restore current working directory to "dirname_start" if they differ, taking
5655  * into account whether it is set locally or globally.
5656  */
5657     static void
5658 restore_start_dir(char_u *dirname_start)
5659 {
5660     char_u *dirname_now = alloc(MAXPATHL);
5661 
5662     if (NULL != dirname_now)
5663     {
5664 	mch_dirname(dirname_now, MAXPATHL);
5665 	if (STRCMP(dirname_start, dirname_now) != 0)
5666 	{
5667 	    // If the directory has changed, change it back by building up an
5668 	    // appropriate ex command and executing it.
5669 	    exarg_T ea;
5670 
5671 	    ea.arg = dirname_start;
5672 	    ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5673 	    ex_cd(&ea);
5674 	}
5675 	vim_free(dirname_now);
5676     }
5677 }
5678 
5679 /*
5680  * Load file "fname" into a dummy buffer and return the buffer pointer,
5681  * placing the directory resulting from the buffer load into the
5682  * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5683  * prior to calling this function. Restores directory to "dirname_start" prior
5684  * to returning, if autocmds or the 'autochdir' option have changed it.
5685  *
5686  * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5687  * or wipe_dummy_buffer() later!
5688  *
5689  * Returns NULL if it fails.
5690  */
5691     static buf_T *
5692 load_dummy_buffer(
5693     char_u	*fname,
5694     char_u	*dirname_start,  // in: old directory
5695     char_u	*resulting_dir)  // out: new directory
5696 {
5697     buf_T	*newbuf;
5698     bufref_T	newbufref;
5699     bufref_T	newbuf_to_wipe;
5700     int		failed = TRUE;
5701     aco_save_T	aco;
5702     int		readfile_result;
5703 
5704     // Allocate a buffer without putting it in the buffer list.
5705     newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5706     if (newbuf == NULL)
5707 	return NULL;
5708     set_bufref(&newbufref, newbuf);
5709 
5710     // Init the options.
5711     buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5712 
5713     // need to open the memfile before putting the buffer in a window
5714     if (ml_open(newbuf) == OK)
5715     {
5716 	// Make sure this buffer isn't wiped out by autocommands.
5717 	++newbuf->b_locked;
5718 
5719 	// set curwin/curbuf to buf and save a few things
5720 	aucmd_prepbuf(&aco, newbuf);
5721 
5722 	// Need to set the filename for autocommands.
5723 	(void)setfname(curbuf, fname, NULL, FALSE);
5724 
5725 	// Create swap file now to avoid the ATTENTION message.
5726 	check_need_swap(TRUE);
5727 
5728 	// Remove the "dummy" flag, otherwise autocommands may not
5729 	// work.
5730 	curbuf->b_flags &= ~BF_DUMMY;
5731 
5732 	newbuf_to_wipe.br_buf = NULL;
5733 	readfile_result = readfile(fname, NULL,
5734 		    (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5735 		    NULL, READ_NEW | READ_DUMMY);
5736 	--newbuf->b_locked;
5737 	if (readfile_result == OK
5738 		&& !got_int
5739 		&& !(curbuf->b_flags & BF_NEW))
5740 	{
5741 	    failed = FALSE;
5742 	    if (curbuf != newbuf)
5743 	    {
5744 		// Bloody autocommands changed the buffer!  Can happen when
5745 		// using netrw and editing a remote file.  Use the current
5746 		// buffer instead, delete the dummy one after restoring the
5747 		// window stuff.
5748 		set_bufref(&newbuf_to_wipe, newbuf);
5749 		newbuf = curbuf;
5750 	    }
5751 	}
5752 
5753 	// restore curwin/curbuf and a few other things
5754 	aucmd_restbuf(&aco);
5755 	if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5756 	    wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
5757 
5758 	// Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5759 	// skip it.
5760 	newbuf->b_flags |= BF_DUMMY;
5761     }
5762 
5763     // When autocommands/'autochdir' option changed directory: go back.
5764     // Let the caller know what the resulting dir was first, in case it is
5765     // important.
5766     mch_dirname(resulting_dir, MAXPATHL);
5767     restore_start_dir(dirname_start);
5768 
5769     if (!bufref_valid(&newbufref))
5770 	return NULL;
5771     if (failed)
5772     {
5773 	wipe_dummy_buffer(newbuf, dirname_start);
5774 	return NULL;
5775     }
5776     return newbuf;
5777 }
5778 
5779 /*
5780  * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5781  * directory to "dirname_start" prior to returning, if autocmds or the
5782  * 'autochdir' option have changed it.
5783  */
5784     static void
5785 wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
5786 {
5787     if (curbuf != buf)		// safety check
5788     {
5789 #if defined(FEAT_EVAL)
5790 	cleanup_T   cs;
5791 
5792 	// Reset the error/interrupt/exception state here so that aborting()
5793 	// returns FALSE when wiping out the buffer.  Otherwise it doesn't
5794 	// work when got_int is set.
5795 	enter_cleanup(&cs);
5796 #endif
5797 
5798 	wipe_buffer(buf, FALSE);
5799 
5800 #if defined(FEAT_EVAL)
5801 	// Restore the error/interrupt/exception state if not discarded by a
5802 	// new aborting error, interrupt, or uncaught exception.
5803 	leave_cleanup(&cs);
5804 #endif
5805 	// When autocommands/'autochdir' option changed directory: go back.
5806 	restore_start_dir(dirname_start);
5807     }
5808 }
5809 
5810 /*
5811  * Unload the dummy buffer that load_dummy_buffer() created. Restores
5812  * directory to "dirname_start" prior to returning, if autocmds or the
5813  * 'autochdir' option have changed it.
5814  */
5815     static void
5816 unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
5817 {
5818     if (curbuf != buf)		// safety check
5819     {
5820 	close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
5821 
5822 	// When autocommands/'autochdir' option changed directory: go back.
5823 	restore_start_dir(dirname_start);
5824     }
5825 }
5826 
5827 #if defined(FEAT_EVAL) || defined(PROTO)
5828 /*
5829  * Copy the specified quickfix entry items into a new dict and appened the dict
5830  * to 'list'.  Returns OK on success.
5831  */
5832     static int
5833 get_qfline_items(qfline_T *qfp, list_T *list)
5834 {
5835     int		bufnum;
5836     dict_T	*dict;
5837     char_u	buf[2];
5838 
5839     // Handle entries with a non-existing buffer number.
5840     bufnum = qfp->qf_fnum;
5841     if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5842 	bufnum = 0;
5843 
5844     if ((dict = dict_alloc()) == NULL)
5845 	return FAIL;
5846     if (list_append_dict(list, dict) == FAIL)
5847 	return FAIL;
5848 
5849     buf[0] = qfp->qf_type;
5850     buf[1] = NUL;
5851     if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5852 	    || dict_add_number(dict, "lnum",  (long)qfp->qf_lnum) == FAIL
5853 	    || dict_add_number(dict, "col",   (long)qfp->qf_col) == FAIL
5854 	    || dict_add_number(dict, "vcol",  (long)qfp->qf_viscol) == FAIL
5855 	    || dict_add_number(dict, "nr",    (long)qfp->qf_nr) == FAIL
5856 	    || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5857 	    || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5858 	    || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5859 	    || dict_add_string(dict, "type", buf) == FAIL
5860 	    || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
5861 	return FAIL;
5862 
5863     return OK;
5864 }
5865 
5866 /*
5867  * Add each quickfix error to list "list" as a dictionary.
5868  * If qf_idx is -1, use the current list. Otherwise, use the specified list.
5869  */
5870     int
5871 get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
5872 {
5873     qf_info_T	*qi = qi_arg;
5874     qf_list_T	*qfl;
5875     qfline_T	*qfp;
5876     int		i;
5877 
5878     if (qi == NULL)
5879     {
5880 	qi = &ql_info;
5881 	if (wp != NULL)
5882 	{
5883 	    qi = GET_LOC_LIST(wp);
5884 	    if (qi == NULL)
5885 		return FAIL;
5886 	}
5887     }
5888 
5889     if (qf_idx == INVALID_QFIDX)
5890 	qf_idx = qi->qf_curlist;
5891 
5892     if (qf_idx >= qi->qf_listcount)
5893 	return FAIL;
5894 
5895     qfl = qf_get_list(qi, qf_idx);
5896     if (qf_list_empty(qfl))
5897 	return FAIL;
5898 
5899     FOR_ALL_QFL_ITEMS(qfl, qfp, i)
5900     {
5901 	if (get_qfline_items(qfp, list) == FAIL)
5902 	    return FAIL;
5903     }
5904 
5905     return OK;
5906 }
5907 
5908 // Flags used by getqflist()/getloclist() to determine which fields to return.
5909 enum {
5910     QF_GETLIST_NONE	= 0x0,
5911     QF_GETLIST_TITLE	= 0x1,
5912     QF_GETLIST_ITEMS	= 0x2,
5913     QF_GETLIST_NR	= 0x4,
5914     QF_GETLIST_WINID	= 0x8,
5915     QF_GETLIST_CONTEXT	= 0x10,
5916     QF_GETLIST_ID	= 0x20,
5917     QF_GETLIST_IDX	= 0x40,
5918     QF_GETLIST_SIZE	= 0x80,
5919     QF_GETLIST_TICK	= 0x100,
5920     QF_GETLIST_FILEWINID	= 0x200,
5921     QF_GETLIST_QFBUFNR	= 0x400,
5922     QF_GETLIST_ALL	= 0x7FF,
5923 };
5924 
5925 /*
5926  * Parse text from 'di' and return the quickfix list items.
5927  * Existing quickfix lists are not modified.
5928  */
5929     static int
5930 qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
5931 {
5932     int		status = FAIL;
5933     qf_info_T	*qi;
5934     char_u	*errorformat = p_efm;
5935     dictitem_T	*efm_di;
5936     list_T	*l;
5937 
5938     // Only a List value is supported
5939     if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
5940     {
5941 	// If errorformat is supplied then use it, otherwise use the 'efm'
5942 	// option setting
5943 	if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5944 	{
5945 	    if (efm_di->di_tv.v_type != VAR_STRING ||
5946 		    efm_di->di_tv.vval.v_string == NULL)
5947 		return FAIL;
5948 	    errorformat = efm_di->di_tv.vval.v_string;
5949 	}
5950 
5951 	l = list_alloc();
5952 	if (l == NULL)
5953 	    return FAIL;
5954 
5955 	qi = qf_alloc_stack(QFLT_INTERNAL);
5956 	if (qi != NULL)
5957 	{
5958 	    if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
5959 			TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5960 	    {
5961 		(void)get_errorlist(qi, NULL, 0, l);
5962 		qf_free(&qi->qf_lists[0]);
5963 	    }
5964 	    free(qi);
5965 	}
5966 	dict_add_list(retdict, "items", l);
5967 	status = OK;
5968     }
5969 
5970     return status;
5971 }
5972 
5973 /*
5974  * Return the quickfix/location list window identifier in the current tabpage.
5975  */
5976     static int
5977 qf_winid(qf_info_T *qi)
5978 {
5979     win_T	*win;
5980 
5981     // The quickfix window can be opened even if the quickfix list is not set
5982     // using ":copen". This is not true for location lists.
5983     if (qi == NULL)
5984 	return 0;
5985     win = qf_find_win(qi);
5986     if (win != NULL)
5987 	return win->w_id;
5988     return 0;
5989 }
5990 
5991 /*
5992  * Returns the number of the buffer displayed in the quickfix/location list
5993  * window. If there is no buffer associated with the list, then returns 0.
5994  */
5995     static int
5996 qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
5997 {
5998     return dict_add_number(retdict, "qfbufnr",
5999 					(qi == NULL) ? 0 : qi->qf_bufnr);
6000 }
6001 
6002 /*
6003  * Convert the keys in 'what' to quickfix list property flags.
6004  */
6005     static int
6006 qf_getprop_keys2flags(dict_T *what, int loclist)
6007 {
6008     int		flags = QF_GETLIST_NONE;
6009 
6010     if (dict_find(what, (char_u *)"all", -1) != NULL)
6011     {
6012 	flags |= QF_GETLIST_ALL;
6013 	if (!loclist)
6014 	    // File window ID is applicable only to location list windows
6015 	    flags &= ~ QF_GETLIST_FILEWINID;
6016     }
6017 
6018     if (dict_find(what, (char_u *)"title", -1) != NULL)
6019 	flags |= QF_GETLIST_TITLE;
6020 
6021     if (dict_find(what, (char_u *)"nr", -1) != NULL)
6022 	flags |= QF_GETLIST_NR;
6023 
6024     if (dict_find(what, (char_u *)"winid", -1) != NULL)
6025 	flags |= QF_GETLIST_WINID;
6026 
6027     if (dict_find(what, (char_u *)"context", -1) != NULL)
6028 	flags |= QF_GETLIST_CONTEXT;
6029 
6030     if (dict_find(what, (char_u *)"id", -1) != NULL)
6031 	flags |= QF_GETLIST_ID;
6032 
6033     if (dict_find(what, (char_u *)"items", -1) != NULL)
6034 	flags |= QF_GETLIST_ITEMS;
6035 
6036     if (dict_find(what, (char_u *)"idx", -1) != NULL)
6037 	flags |= QF_GETLIST_IDX;
6038 
6039     if (dict_find(what, (char_u *)"size", -1) != NULL)
6040 	flags |= QF_GETLIST_SIZE;
6041 
6042     if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6043 	flags |= QF_GETLIST_TICK;
6044 
6045     if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6046 	flags |= QF_GETLIST_FILEWINID;
6047 
6048     if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6049 	flags |= QF_GETLIST_QFBUFNR;
6050 
6051     return flags;
6052 }
6053 
6054 /*
6055  * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6056  * If 'nr' and 'id' are not present in 'what' then return the current
6057  * quickfix list index.
6058  * If 'nr' is zero then return the current quickfix list index.
6059  * If 'nr' is '$' then return the last quickfix list index.
6060  * If 'id' is present then return the index of the quickfix list with that id.
6061  * If 'id' is zero then return the quickfix list index specified by 'nr'.
6062  * Return -1, if quickfix list is not present or if the stack is empty.
6063  */
6064     static int
6065 qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6066 {
6067     int		qf_idx;
6068     dictitem_T	*di;
6069 
6070     qf_idx = qi->qf_curlist;	// default is the current list
6071     if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6072     {
6073 	// Use the specified quickfix/location list
6074 	if (di->di_tv.v_type == VAR_NUMBER)
6075 	{
6076 	    // for zero use the current list
6077 	    if (di->di_tv.vval.v_number != 0)
6078 	    {
6079 		qf_idx = di->di_tv.vval.v_number - 1;
6080 		if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
6081 		    qf_idx = INVALID_QFIDX;
6082 	    }
6083 	}
6084 	else if (di->di_tv.v_type == VAR_STRING
6085 		&& di->di_tv.vval.v_string != NULL
6086 		&& STRCMP(di->di_tv.vval.v_string, "$") == 0)
6087 	    // Get the last quickfix list number
6088 	    qf_idx = qi->qf_listcount - 1;
6089 	else
6090 	    qf_idx = INVALID_QFIDX;
6091     }
6092 
6093     if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6094     {
6095 	// Look for a list with the specified id
6096 	if (di->di_tv.v_type == VAR_NUMBER)
6097 	{
6098 	    // For zero, use the current list or the list specified by 'nr'
6099 	    if (di->di_tv.vval.v_number != 0)
6100 		qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6101 	}
6102 	else
6103 	    qf_idx = INVALID_QFIDX;
6104     }
6105 
6106     return qf_idx;
6107 }
6108 
6109 /*
6110  * Return default values for quickfix list properties in retdict.
6111  */
6112     static int
6113 qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
6114 {
6115     int		status = OK;
6116 
6117     if (flags & QF_GETLIST_TITLE)
6118 	status = dict_add_string(retdict, "title", (char_u *)"");
6119     if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6120     {
6121 	list_T	*l = list_alloc();
6122 	if (l != NULL)
6123 	    status = dict_add_list(retdict, "items", l);
6124 	else
6125 	    status = FAIL;
6126     }
6127     if ((status == OK) && (flags & QF_GETLIST_NR))
6128 	status = dict_add_number(retdict, "nr", 0);
6129     if ((status == OK) && (flags & QF_GETLIST_WINID))
6130 	status = dict_add_number(retdict, "winid", qf_winid(qi));
6131     if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
6132 	status = dict_add_string(retdict, "context", (char_u *)"");
6133     if ((status == OK) && (flags & QF_GETLIST_ID))
6134 	status = dict_add_number(retdict, "id", 0);
6135     if ((status == OK) && (flags & QF_GETLIST_IDX))
6136 	status = dict_add_number(retdict, "idx", 0);
6137     if ((status == OK) && (flags & QF_GETLIST_SIZE))
6138 	status = dict_add_number(retdict, "size", 0);
6139     if ((status == OK) && (flags & QF_GETLIST_TICK))
6140 	status = dict_add_number(retdict, "changedtick", 0);
6141     if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
6142 	status = dict_add_number(retdict, "filewinid", 0);
6143     if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6144 	status = qf_getprop_qfbufnr(qi, retdict);
6145 
6146     return status;
6147 }
6148 
6149 /*
6150  * Return the quickfix list title as 'title' in retdict
6151  */
6152     static int
6153 qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
6154 {
6155     return dict_add_string(retdict, "title", qfl->qf_title);
6156 }
6157 
6158 /*
6159  * Returns the identifier of the window used to display files from a location
6160  * list.  If there is no associated window, then returns 0. Useful only when
6161  * called from a location list window.
6162  */
6163     static int
6164 qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6165 {
6166     int winid = 0;
6167 
6168     if (wp != NULL && IS_LL_WINDOW(wp))
6169     {
6170 	win_T	*ll_wp = qf_find_win_with_loclist(qi);
6171 	if (ll_wp != NULL)
6172 	    winid = ll_wp->w_id;
6173     }
6174 
6175     return dict_add_number(retdict, "filewinid", winid);
6176 }
6177 
6178 /*
6179  * Return the quickfix list items/entries as 'items' in retdict
6180  */
6181     static int
6182 qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6183 {
6184     int		status = OK;
6185     list_T	*l = list_alloc();
6186     if (l != NULL)
6187     {
6188 	(void)get_errorlist(qi, NULL, qf_idx, l);
6189 	dict_add_list(retdict, "items", l);
6190     }
6191     else
6192 	status = FAIL;
6193 
6194     return status;
6195 }
6196 
6197 /*
6198  * Return the quickfix list context (if any) as 'context' in retdict.
6199  */
6200     static int
6201 qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
6202 {
6203     int		status;
6204     dictitem_T	*di;
6205 
6206     if (qfl->qf_ctx != NULL)
6207     {
6208 	di = dictitem_alloc((char_u *)"context");
6209 	if (di != NULL)
6210 	{
6211 	    copy_tv(qfl->qf_ctx, &di->di_tv);
6212 	    status = dict_add(retdict, di);
6213 	    if (status == FAIL)
6214 		dictitem_free(di);
6215 	}
6216 	else
6217 	    status = FAIL;
6218     }
6219     else
6220 	status = dict_add_string(retdict, "context", (char_u *)"");
6221 
6222     return status;
6223 }
6224 
6225 /*
6226  * Return the current quickfix list index as 'idx' in retdict
6227  */
6228     static int
6229 qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
6230 {
6231     int curidx = qfl->qf_index;
6232     if (qf_list_empty(qfl))
6233 	// For empty lists, current index is set to 0
6234 	curidx = 0;
6235     return dict_add_number(retdict, "idx", curidx);
6236 }
6237 
6238 /*
6239  * Return quickfix/location list details (title) as a
6240  * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6241  * then current list is used. Otherwise the specified list is used.
6242  */
6243     int
6244 qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6245 {
6246     qf_info_T	*qi = &ql_info;
6247     qf_list_T	*qfl;
6248     int		status = OK;
6249     int		qf_idx = INVALID_QFIDX;
6250     dictitem_T	*di;
6251     int		flags = QF_GETLIST_NONE;
6252 
6253     if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6254 	return qf_get_list_from_lines(what, di, retdict);
6255 
6256     if (wp != NULL)
6257 	qi = GET_LOC_LIST(wp);
6258 
6259     flags = qf_getprop_keys2flags(what, (wp != NULL));
6260 
6261     if (!qf_stack_empty(qi))
6262 	qf_idx = qf_getprop_qfidx(qi, what);
6263 
6264     // List is not present or is empty
6265     if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
6266 	return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
6267 
6268     qfl = qf_get_list(qi, qf_idx);
6269 
6270     if (flags & QF_GETLIST_TITLE)
6271 	status = qf_getprop_title(qfl, retdict);
6272     if ((status == OK) && (flags & QF_GETLIST_NR))
6273 	status = dict_add_number(retdict, "nr", qf_idx + 1);
6274     if ((status == OK) && (flags & QF_GETLIST_WINID))
6275 	status = dict_add_number(retdict, "winid", qf_winid(qi));
6276     if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6277 	status = qf_getprop_items(qi, qf_idx, retdict);
6278     if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
6279 	status = qf_getprop_ctx(qfl, retdict);
6280     if ((status == OK) && (flags & QF_GETLIST_ID))
6281 	status = dict_add_number(retdict, "id", qfl->qf_id);
6282     if ((status == OK) && (flags & QF_GETLIST_IDX))
6283 	status = qf_getprop_idx(qfl, retdict);
6284     if ((status == OK) && (flags & QF_GETLIST_SIZE))
6285 	status = dict_add_number(retdict, "size", qfl->qf_count);
6286     if ((status == OK) && (flags & QF_GETLIST_TICK))
6287 	status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
6288     if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6289 	status = qf_getprop_filewinid(wp, qi, retdict);
6290     if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6291 	status = qf_getprop_qfbufnr(qi, retdict);
6292 
6293     return status;
6294 }
6295 
6296 /*
6297  * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6298  * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6299  * to TRUE.
6300  */
6301     static int
6302 qf_add_entry_from_dict(
6303 	qf_list_T	*qfl,
6304 	dict_T		*d,
6305 	int		first_entry,
6306 	int		*valid_entry)
6307 {
6308     static int	did_bufnr_emsg;
6309     char_u	*filename, *module, *pattern, *text, *type;
6310     int		bufnum, valid, status, col, vcol, nr;
6311     long	lnum;
6312 
6313     if (first_entry)
6314 	did_bufnr_emsg = FALSE;
6315 
6316     filename = dict_get_string(d, (char_u *)"filename", TRUE);
6317     module = dict_get_string(d, (char_u *)"module", TRUE);
6318     bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6319     lnum = (int)dict_get_number(d, (char_u *)"lnum");
6320     col = (int)dict_get_number(d, (char_u *)"col");
6321     vcol = (int)dict_get_number(d, (char_u *)"vcol");
6322     nr = (int)dict_get_number(d, (char_u *)"nr");
6323     type = dict_get_string(d, (char_u *)"type", TRUE);
6324     pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6325     text = dict_get_string(d, (char_u *)"text", TRUE);
6326     if (text == NULL)
6327 	text = vim_strsave((char_u *)"");
6328 
6329     valid = TRUE;
6330     if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6331 	valid = FALSE;
6332 
6333     // Mark entries with non-existing buffer number as not valid. Give the
6334     // error message only once.
6335     if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6336     {
6337 	if (!did_bufnr_emsg)
6338 	{
6339 	    did_bufnr_emsg = TRUE;
6340 	    semsg(_("E92: Buffer %d not found"), bufnum);
6341 	}
6342 	valid = FALSE;
6343 	bufnum = 0;
6344     }
6345 
6346     // If the 'valid' field is present it overrules the detected value.
6347     if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6348 	valid = (int)dict_get_number(d, (char_u *)"valid");
6349 
6350     status =  qf_add_entry(qfl,
6351 			NULL,		// dir
6352 			filename,
6353 			module,
6354 			bufnum,
6355 			text,
6356 			lnum,
6357 			col,
6358 			vcol,		// vis_col
6359 			pattern,	// search pattern
6360 			nr,
6361 			type == NULL ? NUL : *type,
6362 			valid);
6363 
6364     vim_free(filename);
6365     vim_free(module);
6366     vim_free(pattern);
6367     vim_free(text);
6368     vim_free(type);
6369 
6370     if (valid)
6371 	*valid_entry = TRUE;
6372 
6373     return status;
6374 }
6375 
6376 /*
6377  * Add list of entries to quickfix/location list. Each list entry is
6378  * a dictionary with item information.
6379  */
6380     static int
6381 qf_add_entries(
6382 	qf_info_T	*qi,
6383 	int		qf_idx,
6384 	list_T		*list,
6385 	char_u		*title,
6386 	int		action)
6387 {
6388     qf_list_T	*qfl = qf_get_list(qi, qf_idx);
6389     listitem_T	*li;
6390     dict_T	*d;
6391     qfline_T	*old_last = NULL;
6392     int		retval = OK;
6393     int		valid_entry = FALSE;
6394 
6395     if (action == ' ' || qf_idx == qi->qf_listcount)
6396     {
6397 	// make place for a new list
6398 	qf_new_list(qi, title);
6399 	qf_idx = qi->qf_curlist;
6400 	qfl = qf_get_list(qi, qf_idx);
6401     }
6402     else if (action == 'a' && !qf_list_empty(qfl))
6403 	// Adding to existing list, use last entry.
6404 	old_last = qfl->qf_last;
6405     else if (action == 'r')
6406     {
6407 	qf_free_items(qfl);
6408 	qf_store_title(qfl, title);
6409     }
6410 
6411     for (li = list->lv_first; li != NULL; li = li->li_next)
6412     {
6413 	if (li->li_tv.v_type != VAR_DICT)
6414 	    continue; // Skip non-dict items
6415 
6416 	d = li->li_tv.vval.v_dict;
6417 	if (d == NULL)
6418 	    continue;
6419 
6420 	retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
6421 								&valid_entry);
6422 	if (retval == QF_FAIL)
6423 	    break;
6424     }
6425 
6426     // Check if any valid error entries are added to the list.
6427     if (valid_entry)
6428 	qfl->qf_nonevalid = FALSE;
6429     else if (qfl->qf_index == 0)
6430 	// no valid entry
6431 	qfl->qf_nonevalid = TRUE;
6432 
6433     // If not appending to the list, set the current error to the first entry
6434     if (action != 'a')
6435 	qfl->qf_ptr = qfl->qf_start;
6436 
6437     // Update the current error index if not appending to the list or if the
6438     // list was empty before and it is not empty now.
6439     if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
6440 	qfl->qf_index = 1;
6441 
6442     // Don't update the cursor in quickfix window when appending entries
6443     qf_update_buffer(qi, old_last);
6444 
6445     return retval;
6446 }
6447 
6448 /*
6449  * Get the quickfix list index from 'nr' or 'id'
6450  */
6451     static int
6452 qf_setprop_get_qfidx(
6453 	qf_info_T	*qi,
6454 	dict_T		*what,
6455 	int		action,
6456 	int		*newlist)
6457 {
6458     dictitem_T	*di;
6459     int		qf_idx = qi->qf_curlist;    // default is the current list
6460 
6461     if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6462     {
6463 	// Use the specified quickfix/location list
6464 	if (di->di_tv.v_type == VAR_NUMBER)
6465 	{
6466 	    // for zero use the current list
6467 	    if (di->di_tv.vval.v_number != 0)
6468 		qf_idx = di->di_tv.vval.v_number - 1;
6469 
6470 	    if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6471 	    {
6472 		// When creating a new list, accept qf_idx pointing to the next
6473 		// non-available list and add the new list at the end of the
6474 		// stack.
6475 		*newlist = TRUE;
6476 		qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
6477 	    }
6478 	    else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
6479 		return INVALID_QFIDX;
6480 	    else if (action != ' ')
6481 		*newlist = FALSE;	// use the specified list
6482 	}
6483 	else if (di->di_tv.v_type == VAR_STRING
6484 		&& di->di_tv.vval.v_string != NULL
6485 		&& STRCMP(di->di_tv.vval.v_string, "$") == 0)
6486 	{
6487 	    if (!qf_stack_empty(qi))
6488 		qf_idx = qi->qf_listcount - 1;
6489 	    else if (*newlist)
6490 		qf_idx = 0;
6491 	    else
6492 		return INVALID_QFIDX;
6493 	}
6494 	else
6495 	    return INVALID_QFIDX;
6496     }
6497 
6498     if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
6499     {
6500 	// Use the quickfix/location list with the specified id
6501 	if (di->di_tv.v_type != VAR_NUMBER)
6502 	    return INVALID_QFIDX;
6503 
6504 	return qf_id2nr(qi, di->di_tv.vval.v_number);
6505     }
6506 
6507     return qf_idx;
6508 }
6509 
6510 /*
6511  * Set the quickfix list title.
6512  */
6513     static int
6514 qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6515 {
6516     qf_list_T	*qfl = qf_get_list(qi, qf_idx);
6517 
6518     if (di->di_tv.v_type != VAR_STRING)
6519 	return FAIL;
6520 
6521     vim_free(qfl->qf_title);
6522     qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
6523     if (qf_idx == qi->qf_curlist)
6524 	qf_update_win_titlevar(qi);
6525 
6526     return OK;
6527 }
6528 
6529 /*
6530  * Set quickfix list items/entries.
6531  */
6532     static int
6533 qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6534 {
6535     int		retval = FAIL;
6536     char_u	*title_save;
6537 
6538     if (di->di_tv.v_type != VAR_LIST)
6539 	return FAIL;
6540 
6541     title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6542     retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6543 	    title_save, action == ' ' ? 'a' : action);
6544     vim_free(title_save);
6545 
6546     return retval;
6547 }
6548 
6549 /*
6550  * Set quickfix list items/entries from a list of lines.
6551  */
6552     static int
6553 qf_setprop_items_from_lines(
6554 	qf_info_T	*qi,
6555 	int		qf_idx,
6556 	dict_T		*what,
6557 	dictitem_T	*di,
6558 	int		action)
6559 {
6560     char_u	*errorformat = p_efm;
6561     dictitem_T	*efm_di;
6562     int		retval = FAIL;
6563 
6564     // Use the user supplied errorformat settings (if present)
6565     if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6566     {
6567 	if (efm_di->di_tv.v_type != VAR_STRING ||
6568 		efm_di->di_tv.vval.v_string == NULL)
6569 	    return FAIL;
6570 	errorformat = efm_di->di_tv.vval.v_string;
6571     }
6572 
6573     // Only a List value is supported
6574     if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6575 	return FAIL;
6576 
6577     if (action == 'r')
6578 	qf_free_items(&qi->qf_lists[qf_idx]);
6579     if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6580 		FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6581 	retval = OK;
6582 
6583     return retval;
6584 }
6585 
6586 /*
6587  * Set quickfix list context.
6588  */
6589     static int
6590 qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
6591 {
6592     typval_T	*ctx;
6593 
6594     free_tv(qfl->qf_ctx);
6595     ctx =  alloc_tv();
6596     if (ctx != NULL)
6597 	copy_tv(&di->di_tv, ctx);
6598     qfl->qf_ctx = ctx;
6599 
6600     return OK;
6601 }
6602 
6603 /*
6604  * Set the current index in the specified quickfix list
6605  */
6606     static int
6607 qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6608 {
6609     int		denote = FALSE;
6610     int		newidx;
6611     int		old_qfidx;
6612     qfline_T	*qf_ptr;
6613 
6614     // If the specified index is '$', then use the last entry
6615     if (di->di_tv.v_type == VAR_STRING
6616 	    && di->di_tv.vval.v_string != NULL
6617 	    && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6618 	newidx = qfl->qf_count;
6619     else
6620     {
6621 	// Otherwise use the specified index
6622 	newidx = tv_get_number_chk(&di->di_tv, &denote);
6623 	if (denote)
6624 	    return FAIL;
6625     }
6626 
6627     if (newidx < 1)		// sanity check
6628 	return FAIL;
6629     if (newidx > qfl->qf_count)
6630 	newidx = qfl->qf_count;
6631 
6632     old_qfidx = qfl->qf_index;
6633     qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6634     if (qf_ptr == NULL)
6635 	return FAIL;
6636     qfl->qf_ptr = qf_ptr;
6637     qfl->qf_index = newidx;
6638 
6639     // If the current list is modified and it is displayed in the quickfix
6640     // window, then Update it.
6641     if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
6642 	qf_win_pos_update(qi, old_qfidx);
6643 
6644     return OK;
6645 }
6646 
6647 /*
6648  * Set quickfix/location list properties (title, items, context).
6649  * Also used to add items from parsing a list of lines.
6650  * Used by the setqflist() and setloclist() Vim script functions.
6651  */
6652     static int
6653 qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6654 {
6655     dictitem_T	*di;
6656     int		retval = FAIL;
6657     int		qf_idx;
6658     int		newlist = FALSE;
6659     qf_list_T	*qfl;
6660 
6661     if (action == ' ' || qf_stack_empty(qi))
6662 	newlist = TRUE;
6663 
6664     qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6665     if (qf_idx == INVALID_QFIDX)	// List not found
6666 	return FAIL;
6667 
6668     if (newlist)
6669     {
6670 	qi->qf_curlist = qf_idx;
6671 	qf_new_list(qi, title);
6672 	qf_idx = qi->qf_curlist;
6673     }
6674 
6675     qfl = qf_get_list(qi, qf_idx);
6676     if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
6677 	retval = qf_setprop_title(qi, qf_idx, what, di);
6678     if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
6679 	retval = qf_setprop_items(qi, qf_idx, di, action);
6680     if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6681 	retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
6682     if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
6683 	retval = qf_setprop_context(qfl, di);
6684     if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6685 	retval = qf_setprop_curidx(qi, qfl, di);
6686 
6687     if (retval == OK)
6688 	qf_list_changed(qfl);
6689 
6690     return retval;
6691 }
6692 
6693 /*
6694  * Free the entire quickfix/location list stack.
6695  * If the quickfix/location list window is open, then clear it.
6696  */
6697     static void
6698 qf_free_stack(win_T *wp, qf_info_T *qi)
6699 {
6700     win_T	*qfwin = qf_find_win(qi);
6701     win_T	*llwin = NULL;
6702 
6703     if (qfwin != NULL)
6704     {
6705 	// If the quickfix/location list window is open, then clear it
6706 	if (qi->qf_curlist < qi->qf_listcount)
6707 	    qf_free(qf_get_curlist(qi));
6708 	qf_update_buffer(qi, NULL);
6709     }
6710 
6711     if (wp != NULL && IS_LL_WINDOW(wp))
6712     {
6713 	// If in the location list window, then use the non-location list
6714 	// window with this location list (if present)
6715 	llwin = qf_find_win_with_loclist(qi);
6716 	if (llwin != NULL)
6717 	    wp = llwin;
6718     }
6719 
6720     qf_free_all(wp);
6721     if (wp == NULL)
6722     {
6723 	// quickfix list
6724 	qi->qf_curlist = 0;
6725 	qi->qf_listcount = 0;
6726     }
6727     else if (qfwin != NULL)
6728     {
6729 	// If the location list window is open, then create a new empty
6730 	// location list
6731 	qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
6732 
6733 	if (new_ll != NULL)
6734 	{
6735 	    new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
6736 
6737 	    // first free the list reference in the location list window
6738 	    ll_free_all(&qfwin->w_llist_ref);
6739 
6740 	    qfwin->w_llist_ref = new_ll;
6741 	    if (wp != qfwin)
6742 		win_set_loclist(wp, new_ll);
6743 	}
6744     }
6745 }
6746 
6747 /*
6748  * Populate the quickfix list with the items supplied in the list
6749  * of dictionaries. "title" will be copied to w:quickfix_title.
6750  * "action" is 'a' for add, 'r' for replace.  Otherwise create a new list.
6751  */
6752     int
6753 set_errorlist(
6754 	win_T	*wp,
6755 	list_T	*list,
6756 	int	action,
6757 	char_u	*title,
6758 	dict_T	*what)
6759 {
6760     qf_info_T	*qi = &ql_info;
6761     int		retval = OK;
6762 
6763     if (wp != NULL)
6764     {
6765 	qi = ll_get_or_alloc_list(wp);
6766 	if (qi == NULL)
6767 	    return FAIL;
6768     }
6769 
6770     if (action == 'f')
6771     {
6772 	// Free the entire quickfix or location list stack
6773 	qf_free_stack(wp, qi);
6774 	return OK;
6775     }
6776 
6777     incr_quickfix_busy();
6778 
6779     if (what != NULL)
6780 	retval = qf_set_properties(qi, what, action, title);
6781     else
6782     {
6783 	retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
6784 	if (retval == OK)
6785 	    qf_list_changed(qf_get_curlist(qi));
6786     }
6787 
6788     decr_quickfix_busy();
6789 
6790     return retval;
6791 }
6792 
6793 /*
6794  * Mark the context as in use for all the lists in a quickfix stack.
6795  */
6796     static int
6797 mark_quickfix_ctx(qf_info_T *qi, int copyID)
6798 {
6799     int		i;
6800     int		abort = FALSE;
6801     typval_T	*ctx;
6802 
6803     for (i = 0; i < LISTCOUNT && !abort; ++i)
6804     {
6805 	ctx = qi->qf_lists[i].qf_ctx;
6806 	if (ctx != NULL && ctx->v_type != VAR_NUMBER
6807 		&& ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
6808 	    abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6809     }
6810 
6811     return abort;
6812 }
6813 
6814 /*
6815  * Mark the context of the quickfix list and the location lists (if present) as
6816  * "in use". So that garbage collection doesn't free the context.
6817  */
6818     int
6819 set_ref_in_quickfix(int copyID)
6820 {
6821     int		abort = FALSE;
6822     tabpage_T	*tp;
6823     win_T	*win;
6824 
6825     abort = mark_quickfix_ctx(&ql_info, copyID);
6826     if (abort)
6827 	return abort;
6828 
6829     FOR_ALL_TAB_WINDOWS(tp, win)
6830     {
6831 	if (win->w_llist != NULL)
6832 	{
6833 	    abort = mark_quickfix_ctx(win->w_llist, copyID);
6834 	    if (abort)
6835 		return abort;
6836 	}
6837 	if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6838 	{
6839 	    // In a location list window and none of the other windows is
6840 	    // referring to this location list. Mark the location list
6841 	    // context as still in use.
6842 	    abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6843 	    if (abort)
6844 		return abort;
6845 	}
6846     }
6847 
6848     return abort;
6849 }
6850 #endif
6851 
6852 /*
6853  * Return the autocmd name for the :cbuffer Ex commands
6854  */
6855     static char_u *
6856 cbuffer_get_auname(cmdidx_T cmdidx)
6857 {
6858     switch (cmdidx)
6859     {
6860 	case CMD_cbuffer:	return (char_u *)"cbuffer";
6861 	case CMD_cgetbuffer:	return (char_u *)"cgetbuffer";
6862 	case CMD_caddbuffer:	return (char_u *)"caddbuffer";
6863 	case CMD_lbuffer:	return (char_u *)"lbuffer";
6864 	case CMD_lgetbuffer:	return (char_u *)"lgetbuffer";
6865 	case CMD_laddbuffer:	return (char_u *)"laddbuffer";
6866 	default:		return NULL;
6867     }
6868 }
6869 
6870 /*
6871  * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
6872  * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
6873  */
6874     static int
6875 cbuffer_process_args(
6876 	exarg_T		*eap,
6877 	buf_T		**bufp,
6878 	linenr_T	*line1,
6879 	linenr_T	*line2)
6880 {
6881     buf_T	*buf = NULL;
6882 
6883     if (*eap->arg == NUL)
6884 	buf = curbuf;
6885     else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6886 	buf = buflist_findnr(atoi((char *)eap->arg));
6887 
6888     if (buf == NULL)
6889     {
6890 	emsg(_(e_invarg));
6891 	return FAIL;
6892     }
6893 
6894     if (buf->b_ml.ml_mfp == NULL)
6895     {
6896 	emsg(_("E681: Buffer is not loaded"));
6897 	return FAIL;
6898     }
6899 
6900     if (eap->addr_count == 0)
6901     {
6902 	eap->line1 = 1;
6903 	eap->line2 = buf->b_ml.ml_line_count;
6904     }
6905 
6906     if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6907 	    || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6908     {
6909 	emsg(_(e_invrange));
6910 	return FAIL;
6911     }
6912 
6913     *line1 = eap->line1;
6914     *line2 = eap->line2;
6915     *bufp = buf;
6916 
6917     return OK;
6918 }
6919 
6920 /*
6921  * ":[range]cbuffer [bufnr]" command.
6922  * ":[range]caddbuffer [bufnr]" command.
6923  * ":[range]cgetbuffer [bufnr]" command.
6924  * ":[range]lbuffer [bufnr]" command.
6925  * ":[range]laddbuffer [bufnr]" command.
6926  * ":[range]lgetbuffer [bufnr]" command.
6927  */
6928     void
6929 ex_cbuffer(exarg_T *eap)
6930 {
6931     buf_T	*buf = NULL;
6932     qf_info_T	*qi;
6933     char_u	*au_name = NULL;
6934     int		res;
6935     int_u	save_qfid;
6936     win_T	*wp = NULL;
6937     char_u	*qf_title;
6938     linenr_T	line1;
6939     linenr_T	line2;
6940 
6941     au_name = cbuffer_get_auname(eap->cmdidx);
6942     if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6943 					curbuf->b_fname, TRUE, curbuf))
6944     {
6945 #ifdef FEAT_EVAL
6946 	if (aborting())
6947 	    return;
6948 #endif
6949     }
6950 
6951     // Must come after autocommands.
6952     qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6953     if (qi == NULL)
6954 	return;
6955 
6956     if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
6957 	return;
6958 
6959     qf_title = qf_cmdtitle(*eap->cmdlinep);
6960 
6961     if (buf->b_sfname)
6962     {
6963 	vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6964 		(char *)qf_title, (char *)buf->b_sfname);
6965 	qf_title = IObuff;
6966     }
6967 
6968     incr_quickfix_busy();
6969 
6970     res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
6971 	    (eap->cmdidx != CMD_caddbuffer
6972 	     && eap->cmdidx != CMD_laddbuffer),
6973 	    line1, line2,
6974 	    qf_title, NULL);
6975     if (qf_stack_empty(qi))
6976     {
6977 	decr_quickfix_busy();
6978 	return;
6979     }
6980     if (res >= 0)
6981 	qf_list_changed(qf_get_curlist(qi));
6982 
6983     // Remember the current quickfix list identifier, so that we can
6984     // check for autocommands changing the current quickfix list.
6985     save_qfid = qf_get_curlist(qi)->qf_id;
6986     if (au_name != NULL)
6987     {
6988 	buf_T *curbuf_old = curbuf;
6989 
6990 	apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
6991 								TRUE, curbuf);
6992 	if (curbuf != curbuf_old)
6993 	    // Autocommands changed buffer, don't jump now, "qi" may
6994 	    // be invalid.
6995 	    res = 0;
6996     }
6997     // Jump to the first error for a new list and if autocmds didn't
6998     // free the list.
6999     if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7000 		eap->cmdidx == CMD_lbuffer)
7001 	    && qflist_valid(wp, save_qfid))
7002 	// display the first error
7003 	qf_jump_first(qi, save_qfid, eap->forceit);
7004 
7005     decr_quickfix_busy();
7006 }
7007 
7008 #if defined(FEAT_EVAL) || defined(PROTO)
7009 /*
7010  * Return the autocmd name for the :cexpr Ex commands.
7011  */
7012     static char_u *
7013 cexpr_get_auname(cmdidx_T cmdidx)
7014 {
7015     switch (cmdidx)
7016     {
7017 	case CMD_cexpr:	    return (char_u *)"cexpr";
7018 	case CMD_cgetexpr:  return (char_u *)"cgetexpr";
7019 	case CMD_caddexpr:  return (char_u *)"caddexpr";
7020 	case CMD_lexpr:	    return (char_u *)"lexpr";
7021 	case CMD_lgetexpr:  return (char_u *)"lgetexpr";
7022 	case CMD_laddexpr:  return (char_u *)"laddexpr";
7023 	default:	    return NULL;
7024     }
7025 }
7026 
7027 /*
7028  * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7029  * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
7030  */
7031     void
7032 ex_cexpr(exarg_T *eap)
7033 {
7034     typval_T	*tv;
7035     qf_info_T	*qi;
7036     char_u	*au_name = NULL;
7037     int		res;
7038     int_u	save_qfid;
7039     win_T	*wp = NULL;
7040 
7041     au_name = cexpr_get_auname(eap->cmdidx);
7042     if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7043 					       curbuf->b_fname, TRUE, curbuf))
7044     {
7045 #ifdef FEAT_EVAL
7046 	if (aborting())
7047 	    return;
7048 #endif
7049     }
7050 
7051     qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7052     if (qi == NULL)
7053 	return;
7054 
7055     // Evaluate the expression.  When the result is a string or a list we can
7056     // use it to fill the errorlist.
7057     tv = eval_expr(eap->arg, NULL);
7058     if (tv != NULL)
7059     {
7060 	if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7061 		|| (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7062 	{
7063 	    incr_quickfix_busy();
7064 	    res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
7065 			    (eap->cmdidx != CMD_caddexpr
7066 			     && eap->cmdidx != CMD_laddexpr),
7067 				 (linenr_T)0, (linenr_T)0,
7068 				 qf_cmdtitle(*eap->cmdlinep), NULL);
7069 	    if (qf_stack_empty(qi))
7070 	    {
7071 		decr_quickfix_busy();
7072 		goto cleanup;
7073 	    }
7074 	    if (res >= 0)
7075 		qf_list_changed(qf_get_curlist(qi));
7076 
7077 	    // Remember the current quickfix list identifier, so that we can
7078 	    // check for autocommands changing the current quickfix list.
7079 	    save_qfid = qf_get_curlist(qi)->qf_id;
7080 	    if (au_name != NULL)
7081 		apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7082 						curbuf->b_fname, TRUE, curbuf);
7083 
7084 	    // Jump to the first error for a new list and if autocmds didn't
7085 	    // free the list.
7086 	    if (res > 0 && (eap->cmdidx == CMD_cexpr
7087 						   || eap->cmdidx == CMD_lexpr)
7088 		    && qflist_valid(wp, save_qfid))
7089 		// display the first error
7090 		qf_jump_first(qi, save_qfid, eap->forceit);
7091 	    decr_quickfix_busy();
7092 	}
7093 	else
7094 	    emsg(_("E777: String or List expected"));
7095 cleanup:
7096 	free_tv(tv);
7097     }
7098 }
7099 #endif
7100 
7101 /*
7102  * Get the location list for ":lhelpgrep"
7103  */
7104     static qf_info_T *
7105 hgr_get_ll(int *new_ll)
7106 {
7107     win_T	*wp;
7108     qf_info_T	*qi;
7109 
7110     // If the current window is a help window, then use it
7111     if (bt_help(curwin->w_buffer))
7112 	wp = curwin;
7113     else
7114 	// Find an existing help window
7115 	wp = qf_find_help_win();
7116 
7117     if (wp == NULL)	    // Help window not found
7118 	qi = NULL;
7119     else
7120 	qi = wp->w_llist;
7121 
7122     if (qi == NULL)
7123     {
7124 	// Allocate a new location list for help text matches
7125 	if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
7126 	    return NULL;
7127 	*new_ll = TRUE;
7128     }
7129 
7130     return qi;
7131 }
7132 
7133 /*
7134  * Search for a pattern in a help file.
7135  */
7136     static void
7137 hgr_search_file(
7138 	qf_list_T *qfl,
7139 	char_u *fname,
7140 	vimconv_T *p_vc,
7141 	regmatch_T *p_regmatch)
7142 {
7143     FILE	*fd;
7144     long	lnum;
7145 
7146     fd = mch_fopen((char *)fname, "r");
7147     if (fd == NULL)
7148 	return;
7149 
7150     lnum = 1;
7151     while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7152     {
7153 	char_u    *line = IObuff;
7154 
7155 	// Convert a line if 'encoding' is not utf-8 and
7156 	// the line contains a non-ASCII character.
7157 	if (p_vc->vc_type != CONV_NONE
7158 		&& has_non_ascii(IObuff))
7159 	{
7160 	    line = string_convert(p_vc, IObuff, NULL);
7161 	    if (line == NULL)
7162 		line = IObuff;
7163 	}
7164 
7165 	if (vim_regexec(p_regmatch, line, (colnr_T)0))
7166 	{
7167 	    int	l = (int)STRLEN(line);
7168 
7169 	    // remove trailing CR, LF, spaces, etc.
7170 	    while (l > 0 && line[l - 1] <= ' ')
7171 		line[--l] = NUL;
7172 
7173 	    if (qf_add_entry(qfl,
7174 			NULL,	// dir
7175 			fname,
7176 			NULL,
7177 			0,
7178 			line,
7179 			lnum,
7180 			(int)(p_regmatch->startp[0] - line)
7181 			+ 1,	// col
7182 			FALSE,	// vis_col
7183 			NULL,	// search pattern
7184 			0,	// nr
7185 			1,	// type
7186 			TRUE	// valid
7187 			) == QF_FAIL)
7188 	    {
7189 		got_int = TRUE;
7190 		if (line != IObuff)
7191 		    vim_free(line);
7192 		break;
7193 	    }
7194 	}
7195 	if (line != IObuff)
7196 	    vim_free(line);
7197 	++lnum;
7198 	line_breakcheck();
7199     }
7200     fclose(fd);
7201 }
7202 
7203 /*
7204  * Search for a pattern in all the help files in the doc directory under
7205  * the given directory.
7206  */
7207     static void
7208 hgr_search_files_in_dir(
7209 	qf_list_T *qfl,
7210 	char_u *dirname,
7211 	regmatch_T *p_regmatch,
7212 	vimconv_T *p_vc
7213 #ifdef FEAT_MULTI_LANG
7214 	, char_u *lang
7215 #endif
7216 	)
7217 {
7218     int		fcount;
7219     char_u	**fnames;
7220     int		fi;
7221 
7222     // Find all "*.txt" and "*.??x" files in the "doc" directory.
7223     add_pathsep(dirname);
7224     STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7225     if (gen_expand_wildcards(1, &dirname, &fcount,
7226 		&fnames, EW_FILE|EW_SILENT) == OK
7227 	    && fcount > 0)
7228     {
7229 	for (fi = 0; fi < fcount && !got_int; ++fi)
7230 	{
7231 #ifdef FEAT_MULTI_LANG
7232 	    // Skip files for a different language.
7233 	    if (lang != NULL
7234 		    && STRNICMP(lang, fnames[fi]
7235 				    + STRLEN(fnames[fi]) - 3, 2) != 0
7236 		    && !(STRNICMP(lang, "en", 2) == 0
7237 			&& STRNICMP("txt", fnames[fi]
7238 			    + STRLEN(fnames[fi]) - 3, 3) == 0))
7239 		continue;
7240 #endif
7241 
7242 	    hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
7243 	}
7244 	FreeWild(fcount, fnames);
7245     }
7246 }
7247 
7248 /*
7249  * Search for a pattern in all the help files in the 'runtimepath'
7250  * and add the matches to a quickfix list.
7251  * 'lang' is the language specifier.  If supplied, then only matches in the
7252  * specified language are found.
7253  */
7254     static void
7255 hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
7256 {
7257     char_u	*p;
7258 
7259     vimconv_T	vc;
7260 
7261     // Help files are in utf-8 or latin1, convert lines when 'encoding'
7262     // differs.
7263     vc.vc_type = CONV_NONE;
7264     if (!enc_utf8)
7265 	convert_setup(&vc, (char_u *)"utf-8", p_enc);
7266 
7267     // Go through all the directories in 'runtimepath'
7268     p = p_rtp;
7269     while (*p != NUL && !got_int)
7270     {
7271 	copy_option_part(&p, NameBuff, MAXPATHL, ",");
7272 
7273 	hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
7274 #ifdef FEAT_MULTI_LANG
7275 		, lang
7276 #endif
7277 		);
7278     }
7279 
7280     if (vc.vc_type != CONV_NONE)
7281 	convert_setup(&vc, NULL, NULL);
7282 }
7283 
7284 /*
7285  * ":helpgrep {pattern}"
7286  */
7287     void
7288 ex_helpgrep(exarg_T *eap)
7289 {
7290     regmatch_T	regmatch;
7291     char_u	*save_cpo;
7292     qf_info_T	*qi = &ql_info;
7293     int		new_qi = FALSE;
7294     char_u	*au_name =  NULL;
7295     char_u	*lang = NULL;
7296 
7297     switch (eap->cmdidx)
7298     {
7299 	case CMD_helpgrep:  au_name = (char_u *)"helpgrep"; break;
7300 	case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7301 	default: break;
7302     }
7303     if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7304 					       curbuf->b_fname, TRUE, curbuf))
7305     {
7306 #ifdef FEAT_EVAL
7307 	if (aborting())
7308 	    return;
7309 #endif
7310     }
7311 
7312     if (is_loclist_cmd(eap->cmdidx))
7313     {
7314 	qi = hgr_get_ll(&new_qi);
7315 	if (qi == NULL)
7316 	    return;
7317     }
7318 
7319     // Make 'cpoptions' empty, the 'l' flag should not be used here.
7320     save_cpo = p_cpo;
7321     p_cpo = empty_option;
7322 
7323     incr_quickfix_busy();
7324 
7325 #ifdef FEAT_MULTI_LANG
7326     // Check for a specified language
7327     lang = check_help_lang(eap->arg);
7328 #endif
7329     regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7330     regmatch.rm_ic = FALSE;
7331     if (regmatch.regprog != NULL)
7332     {
7333 	qf_list_T	*qfl;
7334 
7335 	// create a new quickfix list
7336 	qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
7337 	qfl = qf_get_curlist(qi);
7338 
7339 	hgr_search_in_rtp(qfl, &regmatch, lang);
7340 
7341 	vim_regfree(regmatch.regprog);
7342 
7343 	qfl->qf_nonevalid = FALSE;
7344 	qfl->qf_ptr = qfl->qf_start;
7345 	qfl->qf_index = 1;
7346 	qf_list_changed(qfl);
7347 	qf_update_buffer(qi, NULL);
7348     }
7349 
7350     if (p_cpo == empty_option)
7351 	p_cpo = save_cpo;
7352     else
7353 	// Darn, some plugin changed the value.
7354 	free_string_option(save_cpo);
7355 
7356     if (au_name != NULL)
7357     {
7358 	apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7359 					       curbuf->b_fname, TRUE, curbuf);
7360 	if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
7361 	{
7362 	    // autocommands made "qi" invalid
7363 	    decr_quickfix_busy();
7364 	    return;
7365 	}
7366     }
7367 
7368     // Jump to first match.
7369     if (!qf_list_empty(qf_get_curlist(qi)))
7370 	qf_jump(qi, 0, 0, FALSE);
7371     else
7372 	semsg(_(e_nomatch2), eap->arg);
7373 
7374     decr_quickfix_busy();
7375 
7376     if (eap->cmdidx == CMD_lhelpgrep)
7377     {
7378 	// If the help window is not opened or if it already points to the
7379 	// correct location list, then free the new location list.
7380 	if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
7381 	{
7382 	    if (new_qi)
7383 		ll_free_all(&qi);
7384 	}
7385 	else if (curwin->w_llist == NULL)
7386 	    curwin->w_llist = qi;
7387     }
7388 }
7389 
7390 #endif /* FEAT_QUICKFIX */
7391