blob: 4be1d91cbd06745e66219c9ad9e9a67ae53d284b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 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_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaard12f5c12006-01-25 22:10:52 +000050typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000052 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020053 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_ptr; /* pointer to the current error */
55 int qf_count; /* number of errors (0 means no error list) */
56 int qf_index; /* current index in the error list */
57 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058 char_u *qf_title; /* title derived from the command that created
59 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000060} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062struct qf_info_S
63{
64 /*
65 * Count of references to this list. Used only for location lists.
66 * When a location list window reference this list, qf_refcount
67 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
68 * reaches 0, the list is freed.
69 */
70 int qf_refcount;
71 int qf_listcount; /* current number of lists */
72 int qf_curlist; /* current error list */
73 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020074
75 int qf_dir_curlist; /* error list for qf_dir_stack */
76 struct dir_stack_T *qf_dir_stack;
77 char_u *qf_directory;
78 struct dir_stack_T *qf_file_stack;
79 char_u *qf_currfile;
80 int qf_multiline;
81 int qf_multiignore;
82 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000083};
84
85static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar68b76a62005-03-25 21:53:48 +000087#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89/*
90 * Structure used to hold the info of one part of 'errorformat'
91 */
Bram Moolenaar01265852006-03-20 21:50:15 +000092typedef struct efm_S efm_T;
93struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000094{
95 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000096 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
98 char_u prefix; /* prefix of this format line: */
99 /* 'D' enter directory */
100 /* 'X' leave directory */
101 /* 'A' start of multi-line message */
102 /* 'E' error message */
103 /* 'W' warning message */
104 /* 'I' informational message */
105 /* 'C' continuation line */
106 /* 'Z' end of multi-line message */
107 /* 'G' general, unspecific message */
108 /* 'P' push file (partial) message */
109 /* 'Q' pop/quit file (partial) message */
110 /* 'O' overread (partial) message */
111 char_u flags; /* additional flags given in prefix */
112 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000113 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115};
116
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
118static void qf_store_title(qf_info_T *qi, char_u *title);
119static void qf_new_list(qf_info_T *qi, char_u *qf_title);
120static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200121static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static qf_info_T *ll_new_list(void);
123static void qf_msg(qf_info_T *qi);
124static void qf_free(qf_info_T *qi, int idx);
125static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200126static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
127static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200129static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
131static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
134static int is_qf_win(win_T *win, qf_info_T *qi);
135static win_T *qf_find_win(qf_info_T *qi);
136static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200137static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200139static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static char_u *get_mef_name(void);
142static void restore_start_dir(char_u *dirname_start);
143static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
144static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
145static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
146static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000148/* Quickfix window check helper macro */
149#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
150/* Location list window check helper macro */
151#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
152/*
153 * Return location list for window 'wp'
154 * For location list window, return the referenced location list
155 */
156#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000159 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200160 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 * Return -1 for error, number of errors for success.
162 */
163 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100164qf_init(
165 win_T *wp,
166 char_u *efile,
167 char_u *errorformat,
168 int newlist, /* TRUE: start a new error list */
169 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171 qf_info_T *qi = &ql_info;
172
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000173 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000174 {
175 qi = ll_get_or_alloc_list(wp);
176 if (qi == NULL)
177 return FAIL;
178 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000179
180 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200181 (linenr_T)0, (linenr_T)0,
182 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000183}
184
185/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200186 * Maximum number of bytes allowed per line while reading a errorfile.
187 */
188#define LINE_MAXLEN 4096
189
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200190static struct fmtpattern
191{
192 char_u convchar;
193 char *pattern;
194} fmt_pat[FMT_PATTERNS] =
195 {
196 {'f', ".\\+"}, /* only used when at end */
197 {'n', "\\d\\+"},
198 {'l', "\\d\\+"},
199 {'c', "\\d\\+"},
200 {'t', "."},
201 {'m', ".\\+"},
202 {'r', ".*"},
203 {'p', "[- .]*"},
204 {'v', "\\d\\+"},
205 {'s', ".\\+"}
206 };
207
208/*
209 * Converts a 'errorformat' string to regular expression pattern
210 */
211 static int
212efm_to_regpat(
213 char_u *efm,
214 int len,
215 efm_T *fmt_ptr,
216 char_u *regpat,
217 char_u *errmsg)
218{
219 char_u *ptr;
220 char_u *efmp;
221 char_u *srcptr;
222 int round;
223 int idx = 0;
224
225 /*
226 * Build regexp pattern from current 'errorformat' option
227 */
228 ptr = regpat;
229 *ptr++ = '^';
230 round = 0;
231 for (efmp = efm; efmp < efm + len; ++efmp)
232 {
233 if (*efmp == '%')
234 {
235 ++efmp;
236 for (idx = 0; idx < FMT_PATTERNS; ++idx)
237 if (fmt_pat[idx].convchar == *efmp)
238 break;
239 if (idx < FMT_PATTERNS)
240 {
241 if (fmt_ptr->addr[idx])
242 {
243 sprintf((char *)errmsg,
244 _("E372: Too many %%%c in format string"), *efmp);
245 EMSG(errmsg);
246 return -1;
247 }
248 if ((idx
249 && idx < 6
250 && vim_strchr((char_u *)"DXOPQ",
251 fmt_ptr->prefix) != NULL)
252 || (idx == 6
253 && vim_strchr((char_u *)"OPQ",
254 fmt_ptr->prefix) == NULL))
255 {
256 sprintf((char *)errmsg,
257 _("E373: Unexpected %%%c in format string"), *efmp);
258 EMSG(errmsg);
259 return -1;
260 }
261 fmt_ptr->addr[idx] = (char_u)++round;
262 *ptr++ = '\\';
263 *ptr++ = '(';
264#ifdef BACKSLASH_IN_FILENAME
265 if (*efmp == 'f')
266 {
267 /* Also match "c:" in the file name, even when
268 * checking for a colon next: "%f:".
269 * "\%(\a:\)\=" */
270 STRCPY(ptr, "\\%(\\a:\\)\\=");
271 ptr += 10;
272 }
273#endif
274 if (*efmp == 'f' && efmp[1] != NUL)
275 {
276 if (efmp[1] != '\\' && efmp[1] != '%')
277 {
278 /* A file name may contain spaces, but this isn't
279 * in "\f". For "%f:%l:%m" there may be a ":" in
280 * the file name. Use ".\{-1,}x" instead (x is
281 * the next character), the requirement that :999:
282 * follows should work. */
283 STRCPY(ptr, ".\\{-1,}");
284 ptr += 7;
285 }
286 else
287 {
288 /* File name followed by '\\' or '%': include as
289 * many file name chars as possible. */
290 STRCPY(ptr, "\\f\\+");
291 ptr += 4;
292 }
293 }
294 else
295 {
296 srcptr = (char_u *)fmt_pat[idx].pattern;
297 while ((*ptr = *srcptr++) != NUL)
298 ++ptr;
299 }
300 *ptr++ = '\\';
301 *ptr++ = ')';
302 }
303 else if (*efmp == '*')
304 {
305 if (*++efmp == '[' || *efmp == '\\')
306 {
307 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
308 {
309 if (efmp[1] == '^')
310 *ptr++ = *++efmp;
311 if (efmp < efm + len)
312 {
313 *ptr++ = *++efmp; /* could be ']' */
314 while (efmp < efm + len
315 && (*ptr++ = *++efmp) != ']')
316 /* skip */;
317 if (efmp == efm + len)
318 {
319 EMSG(_("E374: Missing ] in format string"));
320 return -1;
321 }
322 }
323 }
324 else if (efmp < efm + len) /* %*\D, %*\s etc. */
325 *ptr++ = *++efmp;
326 *ptr++ = '\\';
327 *ptr++ = '+';
328 }
329 else
330 {
331 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
332 sprintf((char *)errmsg,
333 _("E375: Unsupported %%%c in format string"), *efmp);
334 EMSG(errmsg);
335 return -1;
336 }
337 }
338 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
339 *ptr++ = *efmp; /* regexp magic characters */
340 else if (*efmp == '#')
341 *ptr++ = '*';
342 else if (*efmp == '>')
343 fmt_ptr->conthere = TRUE;
344 else if (efmp == efm + 1) /* analyse prefix */
345 {
346 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
347 fmt_ptr->flags = *efmp++;
348 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
349 fmt_ptr->prefix = *efmp;
350 else
351 {
352 sprintf((char *)errmsg,
353 _("E376: Invalid %%%c in format string prefix"), *efmp);
354 EMSG(errmsg);
355 return -1;
356 }
357 }
358 else
359 {
360 sprintf((char *)errmsg,
361 _("E377: Invalid %%%c in format string"), *efmp);
362 EMSG(errmsg);
363 return -1;
364 }
365 }
366 else /* copy normal character */
367 {
368 if (*efmp == '\\' && efmp + 1 < efm + len)
369 ++efmp;
370 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
371 *ptr++ = '\\'; /* escape regexp atoms */
372 if (*efmp)
373 *ptr++ = *efmp;
374 }
375 }
376 *ptr++ = '$';
377 *ptr = NUL;
378
379 return 0;
380}
381
382 static void
383free_efm_list(efm_T **efm_first)
384{
385 efm_T *efm_ptr;
386
387 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
388 {
389 *efm_first = efm_ptr->next;
390 vim_regfree(efm_ptr->prog);
391 vim_free(efm_ptr);
392 }
393}
394
395/* Parse 'errorformat' option */
396 static efm_T *
397parse_efm_option(char_u *efm)
398{
399 char_u *errmsg = NULL;
400 int errmsglen;
401 efm_T *fmt_ptr = NULL;
402 efm_T *fmt_first = NULL;
403 efm_T *fmt_last = NULL;
404 char_u *fmtstr = NULL;
405 int len;
406 int i;
407 int round;
408
409 errmsglen = CMDBUFFSIZE + 1;
410 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
411 if (errmsg == NULL)
412 goto parse_efm_end;
413
414 /*
415 * Get some space to modify the format string into.
416 */
417 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
418 for (round = FMT_PATTERNS; round > 0; )
419 i += (int)STRLEN(fmt_pat[--round].pattern);
420#ifdef COLON_IN_FILENAME
421 i += 12; /* "%f" can become twelve chars longer */
422#else
423 i += 2; /* "%f" can become two chars longer */
424#endif
425 if ((fmtstr = alloc(i)) == NULL)
426 goto parse_efm_error;
427
428 while (efm[0] != NUL)
429 {
430 /*
431 * Allocate a new eformat structure and put it at the end of the list
432 */
433 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
434 if (fmt_ptr == NULL)
435 goto parse_efm_error;
436 if (fmt_first == NULL) /* first one */
437 fmt_first = fmt_ptr;
438 else
439 fmt_last->next = fmt_ptr;
440 fmt_last = fmt_ptr;
441
442 /*
443 * Isolate one part in the 'errorformat' option
444 */
445 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
446 if (efm[len] == '\\' && efm[len + 1] != NUL)
447 ++len;
448
449 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
450 goto parse_efm_error;
451 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
452 goto parse_efm_error;
453 /*
454 * Advance to next part
455 */
456 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
457 }
458
459 if (fmt_first == NULL) /* nothing found */
460 EMSG(_("E378: 'errorformat' contains no pattern"));
461
462 goto parse_efm_end;
463
464parse_efm_error:
465 free_efm_list(&fmt_first);
466
467parse_efm_end:
468 vim_free(fmtstr);
469 vim_free(errmsg);
470
471 return fmt_first;
472}
473
Bram Moolenaare0d37972016-07-15 22:36:01 +0200474enum {
475 QF_FAIL = 0,
476 QF_OK = 1,
477 QF_END_OF_INPUT = 2,
478 QF_NOMEM = 3
479};
480
481typedef struct {
482 char_u *linebuf;
483 int linelen;
484 char_u *growbuf;
485 int growbufsiz;
486 FILE *fd;
487 typval_T *tv;
488 char_u *p_str;
489 listitem_T *p_li;
490 buf_T *buf;
491 linenr_T buflnum;
492 linenr_T lnumlast;
493} qfstate_T;
494
495 static char_u *
496qf_grow_linebuf(qfstate_T *state, int newsz)
497{
498 /*
499 * If the line exceeds LINE_MAXLEN exclude the last
500 * byte since it's not a NL character.
501 */
502 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
503 if (state->growbuf == NULL)
504 {
505 state->growbuf = alloc(state->linelen + 1);
506 if (state->growbuf == NULL)
507 return NULL;
508 state->growbufsiz = state->linelen;
509 }
510 else if (state->linelen > state->growbufsiz)
511 {
512 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
513 if (state->growbuf == NULL)
514 return NULL;
515 state->growbufsiz = state->linelen;
516 }
517 return state->growbuf;
518}
519
520/*
521 * Get the next string (separated by newline) from state->p_str.
522 */
523 static int
524qf_get_next_str_line(qfstate_T *state)
525{
526 /* Get the next line from the supplied string */
527 char_u *p_str = state->p_str;
528 char_u *p;
529 int len;
530
531 if (*p_str == NUL) /* Reached the end of the string */
532 return QF_END_OF_INPUT;
533
534 p = vim_strchr(p_str, '\n');
535 if (p != NULL)
536 len = (int)(p - p_str) + 1;
537 else
538 len = (int)STRLEN(p_str);
539
540 if (len > IOSIZE - 2)
541 {
542 state->linebuf = qf_grow_linebuf(state, len);
543 if (state->linebuf == NULL)
544 return QF_NOMEM;
545 }
546 else
547 {
548 state->linebuf = IObuff;
549 state->linelen = len;
550 }
551 vim_strncpy(state->linebuf, p_str, state->linelen);
552
553 /*
554 * Increment using len in order to discard the rest of the
555 * line if it exceeds LINE_MAXLEN.
556 */
557 p_str += len;
558 state->p_str = p_str;
559
560 return QF_OK;
561}
562
563/*
564 * Get the next string from state->p_Li.
565 */
566 static int
567qf_get_next_list_line(qfstate_T *state)
568{
569 listitem_T *p_li = state->p_li;
570 int len;
571
572 while (p_li != NULL
573 && (p_li->li_tv.v_type != VAR_STRING
574 || p_li->li_tv.vval.v_string == NULL))
575 p_li = p_li->li_next; /* Skip non-string items */
576
577 if (p_li == NULL) /* End of the list */
578 {
579 state->p_li = NULL;
580 return QF_END_OF_INPUT;
581 }
582
583 len = (int)STRLEN(p_li->li_tv.vval.v_string);
584 if (len > IOSIZE - 2)
585 {
586 state->linebuf = qf_grow_linebuf(state, len);
587 if (state->linebuf == NULL)
588 return QF_NOMEM;
589 }
590 else
591 {
592 state->linebuf = IObuff;
593 state->linelen = len;
594 }
595
596 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
597
598 state->p_li = p_li->li_next; /* next item */
599 return QF_OK;
600}
601
602/*
603 * Get the next string from state->buf.
604 */
605 static int
606qf_get_next_buf_line(qfstate_T *state)
607{
608 char_u *p_buf = NULL;
609 int len;
610
611 /* Get the next line from the supplied buffer */
612 if (state->buflnum > state->lnumlast)
613 return QF_END_OF_INPUT;
614
615 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
616 state->buflnum += 1;
617
618 len = (int)STRLEN(p_buf);
619 if (len > IOSIZE - 2)
620 {
621 state->linebuf = qf_grow_linebuf(state, len);
622 if (state->linebuf == NULL)
623 return QF_NOMEM;
624 }
625 else
626 {
627 state->linebuf = IObuff;
628 state->linelen = len;
629 }
630 vim_strncpy(state->linebuf, p_buf, state->linelen);
631
632 return QF_OK;
633}
634
635/*
636 * Get the next string from file state->fd.
637 */
638 static int
639qf_get_next_file_line(qfstate_T *state)
640{
641 int discard;
642 int growbuflen;
643
644 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
645 return QF_END_OF_INPUT;
646
647 discard = FALSE;
648 state->linelen = (int)STRLEN(IObuff);
649 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'
650#ifdef USE_CRNL
651 || IObuff[state->linelen - 1] == '\r'
652#endif
653 ))
654 {
655 /*
656 * The current line exceeds IObuff, continue reading using
657 * growbuf until EOL or LINE_MAXLEN bytes is read.
658 */
659 if (state->growbuf == NULL)
660 {
661 state->growbufsiz = 2 * (IOSIZE - 1);
662 state->growbuf = alloc(state->growbufsiz);
663 if (state->growbuf == NULL)
664 return QF_NOMEM;
665 }
666
667 /* Copy the read part of the line, excluding null-terminator */
668 memcpy(state->growbuf, IObuff, IOSIZE - 1);
669 growbuflen = state->linelen;
670
671 for (;;)
672 {
673 if (fgets((char *)state->growbuf + growbuflen,
674 state->growbufsiz - growbuflen, state->fd) == NULL)
675 break;
676 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
677 growbuflen += state->linelen;
678 if ((state->growbuf)[growbuflen - 1] == '\n'
679#ifdef USE_CRNL
680 || (state->growbuf)[growbuflen - 1] == '\r'
681#endif
682 )
683 break;
684 if (state->growbufsiz == LINE_MAXLEN)
685 {
686 discard = TRUE;
687 break;
688 }
689
690 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
691 ? 2 * state->growbufsiz : LINE_MAXLEN;
692 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
693 if (state->growbuf == NULL)
694 return QF_NOMEM;
695 }
696
697 while (discard)
698 {
699 /*
700 * The current line is longer than LINE_MAXLEN, continue
701 * reading but discard everything until EOL or EOF is
702 * reached.
703 */
704 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
705 || (int)STRLEN(IObuff) < IOSIZE - 1
706 || IObuff[IOSIZE - 1] == '\n'
707#ifdef USE_CRNL
708 || IObuff[IOSIZE - 1] == '\r'
709#endif
710 )
711 break;
712 }
713
714 state->linebuf = state->growbuf;
715 state->linelen = growbuflen;
716 }
717 else
718 state->linebuf = IObuff;
719
720 return QF_OK;
721}
722
723/*
724 * Get the next string from a file/buffer/list/string.
725 */
726 static int
727qf_get_nextline(qfstate_T *state)
728{
729 int status = QF_FAIL;
730
731 if (state->fd == NULL)
732 {
733 if (state->tv != NULL)
734 {
735 if (state->tv->v_type == VAR_STRING)
736 /* Get the next line from the supplied string */
737 status = qf_get_next_str_line(state);
738 else if (state->tv->v_type == VAR_LIST)
739 /* Get the next line from the supplied list */
740 status = qf_get_next_list_line(state);
741 }
742 else
743 /* Get the next line from the supplied buffer */
744 status = qf_get_next_buf_line(state);
745 }
746 else
747 /* Get the next line from the supplied file */
748 status = qf_get_next_file_line(state);
749
750 if (status != QF_OK)
751 return status;
752
753 /* remove newline/CR from the line */
754 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
755 state->linebuf[state->linelen - 1] = NUL;
756#ifdef USE_CRNL
757 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\r')
758 state->linebuf[state->linelen - 1] = NUL;
759#endif
760
761#ifdef FEAT_MBYTE
762 remove_bom(state->linebuf);
763#endif
764
765 return QF_OK;
766}
767
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200768/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000769 * Read the errorfile "efile" into memory, line by line, building the error
770 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +0200771 * Alternative: when "efile" is NULL read errors from buffer "buf".
772 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000773 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200774 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
775 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000776 * Return -1 for error, number of errors for success.
777 */
778 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100779qf_init_ext(
780 qf_info_T *qi,
781 char_u *efile,
782 buf_T *buf,
783 typval_T *tv,
784 char_u *errorformat,
785 int newlist, /* TRUE: start a new error list */
786 linenr_T lnumfirst, /* first line number to use */
787 linenr_T lnumlast, /* last line number to use */
788 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000789{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 char_u *namebuf;
791 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200792 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000793 char_u *pattern;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
795 NULL, lnumfirst, lnumlast};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 int type = 0;
799 int valid;
800 long lnum = 0L;
801 int enr = 0;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200802#ifdef FEAT_WINDOWS
803 qfline_T *old_last = NULL;
804#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200805 static efm_T *fmt_first = NULL;
Bram Moolenaar01265852006-03-20 21:50:15 +0000806 efm_T *fmt_ptr;
807 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200809 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 int len;
812 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200815 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 char_u *tail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100819 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200820 errmsglen = CMDBUFFSIZE + 1;
821 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100822 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000823 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 goto qf_init_end;
825
Bram Moolenaare0d37972016-07-15 22:36:01 +0200826 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 EMSG2(_(e_openerrf), efile);
829 goto qf_init_end;
830 }
831
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000832 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100834 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200835#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000836 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +0200837 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200838 /* Adding to existing list, use last entry. */
839 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200840 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843/*
844 * Each part of the format string is copied and modified from errorformat to
845 * regex prog. Only a few % characters are allowed.
846 */
847 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000848 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000849 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 else
851 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200853 /*
854 * If we are not adding or adding to another list: clear the state.
855 */
856 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
857 {
858 qi->qf_dir_curlist = qi->qf_curlist;
859 qf_clean_dir_stack(&qi->qf_dir_stack);
860 qi->qf_directory = NULL;
861 qf_clean_dir_stack(&qi->qf_file_stack);
862 qi->qf_currfile = NULL;
863 qi->qf_multiline = FALSE;
864 qi->qf_multiignore = FALSE;
865 qi->qf_multiscan = FALSE;
866 }
867
868 /*
869 * If the errorformat didn't change between calls, then reuse the
870 * previously parsed values.
871 */
872 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
873 {
874 /* free the previously parsed data */
875 vim_free(last_efm);
876 last_efm = NULL;
877 free_efm_list(&fmt_first);
878
879 /* parse the current 'efm' */
880 fmt_first = parse_efm_option(efm);
881 if (fmt_first != NULL)
882 last_efm = vim_strsave(efm);
883 }
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 /*
889 * got_int is reset here, because it was probably set when killing the
890 * ":make" command, but we still want to read the errorfile then.
891 */
892 got_int = FALSE;
893
894 /* Always ignore case when looking for a matching error. */
895 regmatch.rm_ic = TRUE;
896
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000897 if (tv != NULL)
898 {
899 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200900 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000901 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200902 state.p_li = tv->vval.v_list->lv_first;
903 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000904 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200905 state.buf = buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 /*
908 * Read the lines in the error file one by one.
909 * Try to recognize one of the error formats in each line.
910 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000911 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200913 /* Get the next line from a file/buffer/list/string */
914 status = qf_get_nextline(&state);
915 if (status == QF_NOMEM) /* memory alloc failure */
916 goto qf_init_end;
917 if (status == QF_END_OF_INPUT) /* end of input */
918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaar01265852006-03-20 21:50:15 +0000920 /* If there was no %> item start at the first pattern */
921 if (fmt_start == NULL)
922 fmt_ptr = fmt_first;
923 else
924 {
925 fmt_ptr = fmt_start;
926 fmt_start = NULL;
927 }
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 /*
930 * Try to match each part of 'errorformat' until we find a complete
931 * match or no match.
932 */
933 valid = TRUE;
934restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000935 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100937 int r;
938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 idx = fmt_ptr->prefix;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200940 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 continue;
942 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000943 pattern[0] = NUL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200944 if (!qi->qf_multiscan)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 errmsg[0] = NUL;
946 lnum = 0;
947 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000948 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 enr = -1;
950 type = 0;
951 tail = NULL;
952
953 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200954 r = vim_regexec(&regmatch, state.linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100955 fmt_ptr->prog = regmatch.regprog;
956 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200958 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 continue;
960 if (vim_strchr((char_u *)"EWI", idx) != NULL)
961 type = idx;
962 else
963 type = 0;
964 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000965 * Extract error message data from matched line.
966 * We check for an actual submatch, because "\[" and "\]" in
967 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 */
969 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
970 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000971 int c;
972
973 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
974 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000975
976 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000977 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000978 *regmatch.endp[i] = NUL;
979 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
980 *regmatch.endp[i] = c;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000983 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 continue;
985 }
986 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000987 {
988 if (regmatch.startp[i] == NULL)
989 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000993 {
994 if (regmatch.startp[i] == NULL)
995 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000999 {
1000 if (regmatch.startp[i] == NULL)
1001 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +00001003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +00001005 {
1006 if (regmatch.startp[i] == NULL)
1007 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +00001009 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001010 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001011 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001012 if (state.linelen > errmsglen) {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001013 /* linelen + null terminator */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001014 if ((errmsg = vim_realloc(errmsg,
1015 state.linelen + 1)) == NULL)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001016 goto qf_init_end;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001017 errmsglen = state.linelen + 1;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001018 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001019 vim_strncpy(errmsg, state.linebuf, state.linelen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
1022 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001023 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1024 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001026 if (len > errmsglen) {
1027 /* len + null terminator */
1028 if ((errmsg = vim_realloc(errmsg, len + 1))
1029 == NULL)
1030 goto qf_init_end;
1031 errmsglen = len + 1;
1032 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001033 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +00001036 {
1037 if (regmatch.startp[i] == NULL)
1038 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
1042 {
Bram Moolenaarf13de072012-06-01 18:34:41 +02001043 char_u *match_ptr;
1044
Bram Moolenaar4169da72006-06-20 18:49:32 +00001045 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1046 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +02001047 col = 0;
1048 for (match_ptr = regmatch.startp[i];
1049 match_ptr != regmatch.endp[i]; ++match_ptr)
1050 {
1051 ++col;
1052 if (*match_ptr == TAB)
1053 {
1054 col += 7;
1055 col -= col % 8;
1056 }
1057 }
1058 ++col;
1059 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
1061 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
1062 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001063 if (regmatch.startp[i] == NULL)
1064 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001066 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001068 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1069 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001070 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1071 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001072 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1073 if (len > CMDBUFFSIZE - 5)
1074 len = CMDBUFFSIZE - 5;
1075 STRCPY(pattern, "^\\V");
1076 STRNCAT(pattern, regmatch.startp[i], len);
1077 pattern[len + 3] = '\\';
1078 pattern[len + 4] = '$';
1079 pattern[len + 5] = NUL;
1080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 break;
1082 }
1083 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001084 qi->qf_multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +00001085
Bram Moolenaar4770d092006-01-12 23:22:24 +00001086 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001088 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
1090 if (idx == 'D') /* enter directory */
1091 {
1092 if (*namebuf == NUL)
1093 {
1094 EMSG(_("E379: Missing or empty directory name"));
1095 goto error2;
1096 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001097 qi->qf_directory =
1098 qf_push_dir(namebuf, &qi->qf_dir_stack, FALSE);
1099 if (qi->qf_directory == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 goto error2;
1101 }
1102 else if (idx == 'X') /* leave directory */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001103 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105 namebuf[0] = NUL; /* no match found, remove file name */
1106 lnum = 0; /* don't jump to this line */
1107 valid = FALSE;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001108 if (state.linelen > errmsglen) {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001109 /* linelen + null terminator */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001110 if ((errmsg = vim_realloc(errmsg, state.linelen + 1)) == NULL)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001111 goto qf_init_end;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001112 errmsglen = state.linelen + 1;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001113 }
1114 /* copy whole line to error message */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001115 vim_strncpy(errmsg, state.linebuf, state.linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001116 if (fmt_ptr == NULL)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001117 qi->qf_multiline = qi->qf_multiignore = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001119 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
Bram Moolenaar01265852006-03-20 21:50:15 +00001121 /* honor %> item */
1122 if (fmt_ptr->conthere)
1123 fmt_start = fmt_ptr;
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +01001126 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001127 qi->qf_multiline = TRUE; /* start of a multi-line message */
1128 qi->qf_multiignore = FALSE; /* reset continuation */
Bram Moolenaar8eded092014-03-12 19:41:55 +01001129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1131 { /* continuation of multi-line msg */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001132 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (qfprev == NULL)
1135 goto error2;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001136 if (*errmsg && !qi->qf_multiignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
1138 len = (int)STRLEN(qfprev->qf_text);
1139 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
1140 == NULL)
1141 goto error2;
1142 STRCPY(ptr, qfprev->qf_text);
1143 vim_free(qfprev->qf_text);
1144 qfprev->qf_text = ptr;
1145 *(ptr += len) = '\n';
1146 STRCPY(++ptr, errmsg);
1147 }
1148 if (qfprev->qf_nr == -1)
1149 qfprev->qf_nr = enr;
1150 if (vim_isprintc(type) && !qfprev->qf_type)
1151 qfprev->qf_type = type; /* only printable chars allowed */
1152 if (!qfprev->qf_lnum)
1153 qfprev->qf_lnum = lnum;
1154 if (!qfprev->qf_col)
1155 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001156 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (!qfprev->qf_fnum)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001158 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1159 *namebuf || qi->qf_directory != NULL
1160 ? namebuf
1161 : qi->qf_currfile != NULL && valid
1162 ? qi->qf_currfile : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (idx == 'Z')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001164 qi->qf_multiline = qi->qf_multiignore = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 line_breakcheck();
1166 continue;
1167 }
1168 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1169 {
1170 /* global file names */
1171 valid = FALSE;
1172 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
1173 {
1174 if (*namebuf && idx == 'P')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001175 qi->qf_currfile =
1176 qf_push_dir(namebuf, &qi->qf_file_stack, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 else if (idx == 'Q')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001178 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 *namebuf = NUL;
1180 if (tail && *tail)
1181 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00001182 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001183 qi->qf_multiscan = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 goto restofline;
1185 }
1186 }
1187 }
1188 if (fmt_ptr->flags == '-') /* generally exclude this line */
1189 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001190 if (qi->qf_multiline)
1191 /* also exclude continuation lines */
1192 qi->qf_multiignore = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 continue;
1194 }
1195 }
1196
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001197 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001198 qi->qf_directory,
1199 (*namebuf || qi->qf_directory != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 ? namebuf
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001201 : ((qi->qf_currfile != NULL && valid)
1202 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001203 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 errmsg,
1205 lnum,
1206 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001207 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001208 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 enr,
1210 type,
1211 valid) == FAIL)
1212 goto error2;
1213 line_breakcheck();
1214 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001215 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001217 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001219 /* no valid entry found */
1220 qi->qf_lists[qi->qf_curlist].qf_ptr =
1221 qi->qf_lists[qi->qf_curlist].qf_start;
1222 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1223 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225 else
1226 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001227 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1228 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1229 qi->qf_lists[qi->qf_curlist].qf_ptr =
1230 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001232 /* return number of matches */
1233 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001234 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236 EMSG(_(e_readerrf));
1237error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001238 qf_free(qi, qi->qf_curlist);
1239 qi->qf_listcount--;
1240 if (qi->qf_curlist > 0)
1241 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001242qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001243 if (state.fd != NULL)
1244 fclose(state.fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 vim_free(namebuf);
1246 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001247 vim_free(pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001248 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249
1250#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001251 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252#endif
1253
1254 return retval;
1255}
1256
Bram Moolenaarfb604092014-07-23 15:55:00 +02001257 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001258qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001259{
1260 if (title != NULL)
1261 {
1262 char_u *p = alloc((int)STRLEN(title) + 2);
1263
1264 qi->qf_lists[qi->qf_curlist].qf_title = p;
1265 if (p != NULL)
1266 sprintf((char *)p, ":%s", (char *)title);
1267 }
1268}
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270/*
1271 * Prepare for adding a new quickfix list.
1272 */
1273 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001274qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 int i;
1277
1278 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001279 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 * the current entry. This makes it possible to browse in a tree-like
1281 * way with ":grep'.
1282 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001283 while (qi->qf_listcount > qi->qf_curlist + 1)
1284 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
1286 /*
1287 * When the stack is full, remove to oldest entry
1288 * Otherwise, add a new entry.
1289 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001290 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001292 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001294 qi->qf_lists[i - 1] = qi->qf_lists[i];
1295 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 }
1297 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001298 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001299 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001300 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301}
1302
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001303/*
1304 * Free a location list
1305 */
1306 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001307ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001308{
1309 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001310 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001311
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001312 qi = *pqi;
1313 if (qi == NULL)
1314 return;
1315 *pqi = NULL; /* Remove reference to this list */
1316
1317 qi->qf_refcount--;
1318 if (qi->qf_refcount < 1)
1319 {
1320 /* No references to this location list */
1321 for (i = 0; i < qi->qf_listcount; ++i)
1322 qf_free(qi, i);
1323 vim_free(qi);
1324 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001325}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001326
1327 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001328qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001329{
1330 int i;
1331 qf_info_T *qi = &ql_info;
1332
1333 if (wp != NULL)
1334 {
1335 /* location list */
1336 ll_free_all(&wp->w_llist);
1337 ll_free_all(&wp->w_llist_ref);
1338 }
1339 else
1340 /* quickfix list */
1341 for (i = 0; i < qi->qf_listcount; ++i)
1342 qf_free(qi, i);
1343}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345/*
1346 * Add an entry to the end of the list of errors.
1347 * Returns OK or FAIL.
1348 */
1349 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001350qf_add_entry(
1351 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001352 char_u *dir, /* optional directory name */
1353 char_u *fname, /* file name or NULL */
1354 int bufnum, /* buffer number or zero */
1355 char_u *mesg, /* message */
1356 long lnum, /* line number */
1357 int col, /* column */
1358 int vis_col, /* using visual column */
1359 char_u *pattern, /* search pattern */
1360 int nr, /* error number */
1361 int type, /* type character */
1362 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001364 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001365 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001367 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001369 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001370 {
1371 buf_T *buf = buflist_findnr(bufnum);
1372
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001373 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001374 if (buf != NULL)
1375 buf->b_has_qf_entry = TRUE;
1376 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001377 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001378 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1380 {
1381 vim_free(qfp);
1382 return FAIL;
1383 }
1384 qfp->qf_lnum = lnum;
1385 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001386 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001387 if (pattern == NULL || *pattern == NUL)
1388 qfp->qf_pattern = NULL;
1389 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1390 {
1391 vim_free(qfp->qf_text);
1392 vim_free(qfp);
1393 return FAIL;
1394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 qfp->qf_nr = nr;
1396 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1397 type = 0;
1398 qfp->qf_type = type;
1399 qfp->qf_valid = valid;
1400
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001401 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001402 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1403 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001405 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001406 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1407 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001408 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
1410 else
1411 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001412 qfp->qf_prev = *lastp;
1413 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001415 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001417 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001418 ++qi->qf_lists[qi->qf_curlist].qf_count;
1419 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1420 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001422 qi->qf_lists[qi->qf_curlist].qf_index =
1423 qi->qf_lists[qi->qf_curlist].qf_count;
1424 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426
1427 return OK;
1428}
1429
1430/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001431 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001432 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001433 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001434ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001435{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001436 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001437
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001438 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1439 if (qi != NULL)
1440 {
1441 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1442 qi->qf_refcount++;
1443 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001444
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001445 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001446}
1447
1448/*
1449 * Return the location list for window 'wp'.
1450 * If not present, allocate a location list
1451 */
1452 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001453ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001454{
1455 if (IS_LL_WINDOW(wp))
1456 /* For a location list window, use the referenced location list */
1457 return wp->w_llist_ref;
1458
1459 /*
1460 * For a non-location list window, w_llist_ref should not point to a
1461 * location list.
1462 */
1463 ll_free_all(&wp->w_llist_ref);
1464
1465 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001466 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001467 return wp->w_llist;
1468}
1469
1470/*
1471 * Copy the location list from window "from" to window "to".
1472 */
1473 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001474copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001475{
1476 qf_info_T *qi;
1477 int idx;
1478 int i;
1479
1480 /*
1481 * When copying from a location list window, copy the referenced
1482 * location list. For other windows, copy the location list for
1483 * that window.
1484 */
1485 if (IS_LL_WINDOW(from))
1486 qi = from->w_llist_ref;
1487 else
1488 qi = from->w_llist;
1489
1490 if (qi == NULL) /* no location list to copy */
1491 return;
1492
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001493 /* allocate a new location list */
1494 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001495 return;
1496
1497 to->w_llist->qf_listcount = qi->qf_listcount;
1498
1499 /* Copy the location lists one at a time */
1500 for (idx = 0; idx < qi->qf_listcount; idx++)
1501 {
1502 qf_list_T *from_qfl;
1503 qf_list_T *to_qfl;
1504
1505 to->w_llist->qf_curlist = idx;
1506
1507 from_qfl = &qi->qf_lists[idx];
1508 to_qfl = &to->w_llist->qf_lists[idx];
1509
1510 /* Some of the fields are populated by qf_add_entry() */
1511 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1512 to_qfl->qf_count = 0;
1513 to_qfl->qf_index = 0;
1514 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001515 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001516 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001517 if (from_qfl->qf_title != NULL)
1518 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1519 else
1520 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001521
1522 if (from_qfl->qf_count)
1523 {
1524 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001525 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001526
1527 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001528 for (i = 0, from_qfp = from_qfl->qf_start;
1529 i < from_qfl->qf_count && from_qfp != NULL;
1530 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001532 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001533 NULL,
1534 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001535 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001536 from_qfp->qf_text,
1537 from_qfp->qf_lnum,
1538 from_qfp->qf_col,
1539 from_qfp->qf_viscol,
1540 from_qfp->qf_pattern,
1541 from_qfp->qf_nr,
1542 0,
1543 from_qfp->qf_valid) == FAIL)
1544 {
1545 qf_free_all(to);
1546 return;
1547 }
1548 /*
1549 * qf_add_entry() will not set the qf_num field, as the
1550 * directory and file names are not supplied. So the qf_fnum
1551 * field is copied here.
1552 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001553 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001554 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1555 prevp->qf_type = from_qfp->qf_type; /* error type */
1556 if (from_qfl->qf_ptr == from_qfp)
1557 to_qfl->qf_ptr = prevp; /* current location */
1558 }
1559 }
1560
1561 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1562
1563 /* When no valid entries are present in the list, qf_ptr points to
1564 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001565 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001566 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001567 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001568 to_qfl->qf_index = 1;
1569 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001570 }
1571
1572 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1573}
1574
1575/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001576 * Looking up a buffer can be slow if there are many. Remember the last one
1577 * to make this a lot faster if there are multiple matches in the same file.
1578 */
1579static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001580static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001581
1582/*
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001583 * Get buffer number for file "dir.name".
1584 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 */
1586 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001587qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar82404332016-07-10 17:00:38 +02001589 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001590 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001591 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 if (fname == NULL || *fname == NUL) /* no file name */
1594 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaare60acc12011-05-10 16:41:25 +02001596#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001597 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001598#endif
1599#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001600 if (directory != NULL)
1601 slash_adjust(directory);
1602 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001603#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001604 if (directory != NULL && !vim_isAbsName(fname)
1605 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1606 {
1607 /*
1608 * Here we check if the file really exists.
1609 * This should normally be true, but if make works without
1610 * "leaving directory"-messages we might have missed a
1611 * directory change.
1612 */
1613 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001616 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001617 if (directory)
1618 ptr = concat_fnames(directory, fname, TRUE);
1619 else
1620 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001622 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001623 bufname = ptr;
1624 }
1625 else
1626 bufname = fname;
1627
1628 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001629 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001630 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001631 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001632 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001634 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001635 {
1636 vim_free(qf_last_bufname);
1637 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1638 if (bufname == ptr)
1639 qf_last_bufname = bufname;
1640 else
1641 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001642 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001643 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001644 if (buf == NULL)
1645 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001646
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001647 buf->b_has_qf_entry = TRUE;
1648 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649}
1650
1651/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001652 * Push dirbuf onto the directory stack and return pointer to actual dir or
1653 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 */
1655 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001656qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
1658 struct dir_stack_T *ds_new;
1659 struct dir_stack_T *ds_ptr;
1660
1661 /* allocate new stack element and hook it in */
1662 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1663 if (ds_new == NULL)
1664 return NULL;
1665
1666 ds_new->next = *stackptr;
1667 *stackptr = ds_new;
1668
1669 /* store directory on the stack */
1670 if (vim_isAbsName(dirbuf)
1671 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001672 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 (*stackptr)->dirname = vim_strsave(dirbuf);
1674 else
1675 {
1676 /* Okay we don't have an absolute path.
1677 * dirbuf must be a subdir of one of the directories on the stack.
1678 * Let's search...
1679 */
1680 ds_new = (*stackptr)->next;
1681 (*stackptr)->dirname = NULL;
1682 while (ds_new)
1683 {
1684 vim_free((*stackptr)->dirname);
1685 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1686 TRUE);
1687 if (mch_isdir((*stackptr)->dirname) == TRUE)
1688 break;
1689
1690 ds_new = ds_new->next;
1691 }
1692
1693 /* clean up all dirs we already left */
1694 while ((*stackptr)->next != ds_new)
1695 {
1696 ds_ptr = (*stackptr)->next;
1697 (*stackptr)->next = (*stackptr)->next->next;
1698 vim_free(ds_ptr->dirname);
1699 vim_free(ds_ptr);
1700 }
1701
1702 /* Nothing found -> it must be on top level */
1703 if (ds_new == NULL)
1704 {
1705 vim_free((*stackptr)->dirname);
1706 (*stackptr)->dirname = vim_strsave(dirbuf);
1707 }
1708 }
1709
1710 if ((*stackptr)->dirname != NULL)
1711 return (*stackptr)->dirname;
1712 else
1713 {
1714 ds_ptr = *stackptr;
1715 *stackptr = (*stackptr)->next;
1716 vim_free(ds_ptr);
1717 return NULL;
1718 }
1719}
1720
1721
1722/*
1723 * pop dirbuf from the directory stack and return previous directory or NULL if
1724 * stack is empty
1725 */
1726 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001727qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 struct dir_stack_T *ds_ptr;
1730
1731 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1732 * What to do if it isn't? */
1733
1734 /* pop top element and free it */
1735 if (*stackptr != NULL)
1736 {
1737 ds_ptr = *stackptr;
1738 *stackptr = (*stackptr)->next;
1739 vim_free(ds_ptr->dirname);
1740 vim_free(ds_ptr);
1741 }
1742
1743 /* return NEW top element as current dir or NULL if stack is empty*/
1744 return *stackptr ? (*stackptr)->dirname : NULL;
1745}
1746
1747/*
1748 * clean up directory stack
1749 */
1750 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001751qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
1753 struct dir_stack_T *ds_ptr;
1754
1755 while ((ds_ptr = *stackptr) != NULL)
1756 {
1757 *stackptr = (*stackptr)->next;
1758 vim_free(ds_ptr->dirname);
1759 vim_free(ds_ptr);
1760 }
1761}
1762
1763/*
1764 * Check in which directory of the directory stack the given file can be
1765 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001766 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 * Cleans up intermediate directory entries.
1768 *
1769 * TODO: How to solve the following problem?
1770 * If we have the this directory tree:
1771 * ./
1772 * ./aa
1773 * ./aa/bb
1774 * ./bb
1775 * ./bb/x.c
1776 * and make says:
1777 * making all in aa
1778 * making all in bb
1779 * x.c:9: Error
1780 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1781 * qf_guess_filepath will return NULL.
1782 */
1783 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001784qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 struct dir_stack_T *ds_ptr;
1787 struct dir_stack_T *ds_tmp;
1788 char_u *fullname;
1789
1790 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001791 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 return NULL;
1793
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001794 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 fullname = NULL;
1796 while (ds_ptr)
1797 {
1798 vim_free(fullname);
1799 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1800
1801 /* If concat_fnames failed, just go on. The worst thing that can happen
1802 * is that we delete the entire stack.
1803 */
1804 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1805 break;
1806
1807 ds_ptr = ds_ptr->next;
1808 }
1809
1810 vim_free(fullname);
1811
1812 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001813 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001815 ds_tmp = qi->qf_dir_stack->next;
1816 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 vim_free(ds_tmp->dirname);
1818 vim_free(ds_tmp);
1819 }
1820
1821 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822}
1823
1824/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001825 * When loading a file from the quickfix, the auto commands may modify it.
1826 * This may invalidate the current quickfix entry. This function checks
1827 * whether a entry is still present in the quickfix.
1828 * Similar to location list.
1829 */
1830 static int
1831is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1832{
1833 qf_list_T *qfl;
1834 qfline_T *qfp;
1835 int i;
1836
1837 qfl = &qi->qf_lists[qi->qf_curlist];
1838
1839 /* Search for the entry in the current list */
1840 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1841 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001842 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001843 break;
1844
1845 if (i == qfl->qf_count) /* Entry is not found */
1846 return FALSE;
1847
1848 return TRUE;
1849}
1850
1851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 * jump to a quickfix line
1853 * if dir == FORWARD go "errornr" valid entries forward
1854 * if dir == BACKWARD go "errornr" valid entries backward
1855 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1856 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1857 * else if "errornr" is zero, redisplay the same line
1858 * else go to entry "errornr"
1859 */
1860 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001861qf_jump(
1862 qf_info_T *qi,
1863 int dir,
1864 int errornr,
1865 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001867 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001868 qfline_T *qf_ptr;
1869 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 int qf_index;
1871 int old_qf_fnum;
1872 int old_qf_index;
1873 int prev_index;
1874 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1875 char_u *err = e_no_more_items;
1876 linenr_T i;
1877 buf_T *old_curbuf;
1878 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 colnr_T screen_col;
1880 colnr_T char_col;
1881 char_u *line;
1882#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001883 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001884 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 int opened_window = FALSE;
1886 win_T *win;
1887 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001888 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001890 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 int print_message = TRUE;
1892 int len;
1893#ifdef FEAT_FOLDING
1894 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1895#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001896 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001897 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001899 if (qi == NULL)
1900 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001901
1902 if (qi->qf_curlist >= qi->qf_listcount
1903 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
1905 EMSG(_(e_quickfix));
1906 return;
1907 }
1908
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001909 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001911 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 old_qf_index = qf_index;
1913 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1914 {
1915 while (errornr--)
1916 {
1917 old_qf_ptr = qf_ptr;
1918 prev_index = qf_index;
1919 old_qf_fnum = qf_ptr->qf_fnum;
1920 do
1921 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001922 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 || qf_ptr->qf_next == NULL)
1924 {
1925 qf_ptr = old_qf_ptr;
1926 qf_index = prev_index;
1927 if (err != NULL)
1928 {
1929 EMSG(_(err));
1930 goto theend;
1931 }
1932 errornr = 0;
1933 break;
1934 }
1935 ++qf_index;
1936 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001937 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1938 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1940 err = NULL;
1941 }
1942 }
1943 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1944 {
1945 while (errornr--)
1946 {
1947 old_qf_ptr = qf_ptr;
1948 prev_index = qf_index;
1949 old_qf_fnum = qf_ptr->qf_fnum;
1950 do
1951 {
1952 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1953 {
1954 qf_ptr = old_qf_ptr;
1955 qf_index = prev_index;
1956 if (err != NULL)
1957 {
1958 EMSG(_(err));
1959 goto theend;
1960 }
1961 errornr = 0;
1962 break;
1963 }
1964 --qf_index;
1965 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001966 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1967 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1969 err = NULL;
1970 }
1971 }
1972 else if (errornr != 0) /* go to specified number */
1973 {
1974 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1975 {
1976 --qf_index;
1977 qf_ptr = qf_ptr->qf_prev;
1978 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001979 while (errornr > qf_index && qf_index <
1980 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 && qf_ptr->qf_next != NULL)
1982 {
1983 ++qf_index;
1984 qf_ptr = qf_ptr->qf_next;
1985 }
1986 }
1987
1988#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001989 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1990 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 /* No need to print the error message if it's visible in the error
1992 * window */
1993 print_message = FALSE;
1994
1995 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001996 * For ":helpgrep" find a help window or open one.
1997 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001998 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001999 {
2000 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002001
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002002 if (cmdmod.tab != 0)
2003 wp = NULL;
2004 else
2005 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2006 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2007 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002008 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2009 win_enter(wp, TRUE);
2010 else
2011 {
2012 /*
2013 * Split off help window; put it at far top if no position
2014 * specified, the current window is vertically split and narrow.
2015 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002016 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002017 if (cmdmod.split == 0 && curwin->w_width != Columns
2018 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002019 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002020 if (qi != &ql_info)
2021 flags |= WSP_NEWLOC; /* don't copy the location list */
2022
2023 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002024 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002025 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002026
2027 if (curwin->w_height < p_hh)
2028 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002029
2030 if (qi != &ql_info) /* not a quickfix list */
2031 {
2032 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002033 curwin->w_llist = qi;
2034 qi->qf_refcount++;
2035 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002036 }
2037
2038 if (!p_im)
2039 restart_edit = 0; /* don't want insert mode in help file */
2040 }
2041
2042 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 * If currently in the quickfix window, find another window to show the
2044 * file in.
2045 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002046 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002048 win_T *usable_win_ptr = NULL;
2049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 /*
2051 * If there is no file specified, we don't know where to go.
2052 * But do advance, otherwise ":cn" gets stuck.
2053 */
2054 if (qf_ptr->qf_fnum == 0)
2055 goto theend;
2056
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002057 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002058
2059 ll_ref = curwin->w_llist_ref;
2060 if (ll_ref != NULL)
2061 {
2062 /* Find a window using the same location list that is not a
2063 * quickfix window. */
2064 FOR_ALL_WINDOWS(usable_win_ptr)
2065 if (usable_win_ptr->w_llist == ll_ref
2066 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002067 {
2068 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002069 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002070 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002071 }
2072
2073 if (!usable_win)
2074 {
2075 /* Locate a window showing a normal buffer */
2076 FOR_ALL_WINDOWS(win)
2077 if (win->w_buffer->b_p_bt[0] == NUL)
2078 {
2079 usable_win = 1;
2080 break;
2081 }
2082 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002083
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002085 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002086 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002088 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002089 {
2090 tabpage_T *tp;
2091 win_T *wp;
2092
2093 FOR_ALL_TAB_WINDOWS(tp, wp)
2094 {
2095 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2096 {
2097 goto_tabpage_win(tp, wp);
2098 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002099 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002100 }
2101 }
2102 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002103win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002104
2105 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002106 * If there is only one window and it is the quickfix window, create a
2107 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002108 */
2109 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002111 flags = WSP_ABOVE;
2112 if (ll_ref != NULL)
2113 flags |= WSP_NEWLOC;
2114 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 goto failed; /* not enough room for window */
2116 opened_window = TRUE; /* close it when fail */
2117 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002118 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002119 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002120 if (ll_ref != NULL)
2121 {
2122 /* The new window should use the location list from the
2123 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002124 curwin->w_llist = ll_ref;
2125 ll_ref->qf_refcount++;
2126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
2128 else
2129 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002130 if (curwin->w_llist_ref != NULL)
2131 {
2132 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002133 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002134 if (win == NULL)
2135 {
2136 /* Find the window showing the selected file */
2137 FOR_ALL_WINDOWS(win)
2138 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2139 break;
2140 if (win == NULL)
2141 {
2142 /* Find a previous usable window */
2143 win = curwin;
2144 do
2145 {
2146 if (win->w_buffer->b_p_bt[0] == NUL)
2147 break;
2148 if (win->w_prev == NULL)
2149 win = lastwin; /* wrap around the top */
2150 else
2151 win = win->w_prev; /* go to previous window */
2152 } while (win != curwin);
2153 }
2154 }
2155 win_goto(win);
2156
2157 /* If the location list for the window is not set, then set it
2158 * to the location list from the location window */
2159 if (win->w_llist == NULL)
2160 {
2161 win->w_llist = ll_ref;
2162 ll_ref->qf_refcount++;
2163 }
2164 }
2165 else
2166 {
2167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 /*
2169 * Try to find a window that shows the right buffer.
2170 * Default to the window just above the quickfix buffer.
2171 */
2172 win = curwin;
2173 altwin = NULL;
2174 for (;;)
2175 {
2176 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2177 break;
2178 if (win->w_prev == NULL)
2179 win = lastwin; /* wrap around the top */
2180 else
2181 win = win->w_prev; /* go to previous window */
2182
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002183 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
2185 /* Didn't find it, go to the window before the quickfix
2186 * window. */
2187 if (altwin != NULL)
2188 win = altwin;
2189 else if (curwin->w_prev != NULL)
2190 win = curwin->w_prev;
2191 else
2192 win = curwin->w_next;
2193 break;
2194 }
2195
2196 /* Remember a usable window. */
2197 if (altwin == NULL && !win->w_p_pvw
2198 && win->w_buffer->b_p_bt[0] == NUL)
2199 altwin = win;
2200 }
2201
2202 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 }
2206#endif
2207
2208 /*
2209 * If there is a file name,
2210 * read the wanted file if needed, and check autowrite etc.
2211 */
2212 old_curbuf = curbuf;
2213 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002214
2215 if (qf_ptr->qf_fnum != 0)
2216 {
2217 if (qf_ptr->qf_type == 1)
2218 {
2219 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2220 * set b_p_ro flag). */
2221 if (!can_abandon(curbuf, forceit))
2222 {
2223 EMSG(_(e_nowrtmsg));
2224 ok = FALSE;
2225 }
2226 else
2227 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002228 ECMD_HIDE + ECMD_SET_HELP,
2229 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002230 }
2231 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002232 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002233 int old_qf_curlist = qi->qf_curlist;
2234 int is_abort = FALSE;
2235
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002236 ok = buflist_getfile(qf_ptr->qf_fnum,
2237 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002238 if (qi != &ql_info && !win_valid(oldwin))
2239 {
2240 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002241 is_abort = TRUE;
2242 opened_window = FALSE;
2243 }
2244 else if (old_qf_curlist != qi->qf_curlist
2245 || !is_qf_entry_present(qi, qf_ptr))
2246 {
2247 if (qi == &ql_info)
2248 EMSG(_("E925: Current quickfix was changed"));
2249 else
2250 EMSG(_("E926: Current location list was changed"));
2251 is_abort = TRUE;
2252 }
2253
2254 if (is_abort)
2255 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002256 ok = FALSE;
2257 qi = NULL;
2258 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002259 }
2260 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002261 }
2262
2263 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 /* When not switched to another buffer, still need to set pc mark */
2266 if (curbuf == old_curbuf)
2267 setpcmark();
2268
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002269 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002271 /*
2272 * Go to line with error, unless qf_lnum is 0.
2273 */
2274 i = qf_ptr->qf_lnum;
2275 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002277 if (i > curbuf->b_ml.ml_line_count)
2278 i = curbuf->b_ml.ml_line_count;
2279 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002281 if (qf_ptr->qf_col > 0)
2282 {
2283 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002284#ifdef FEAT_VIRTUALEDIT
2285 curwin->w_cursor.coladd = 0;
2286#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002287 if (qf_ptr->qf_viscol == TRUE)
2288 {
2289 /*
2290 * Check each character from the beginning of the error
2291 * line up to the error column. For each tab character
2292 * found, reduce the error column value by the length of
2293 * a tab character.
2294 */
2295 line = ml_get_curline();
2296 screen_col = 0;
2297 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2298 {
2299 if (*line == NUL)
2300 break;
2301 if (*line++ == '\t')
2302 {
2303 curwin->w_cursor.col -= 7 - (screen_col % 8);
2304 screen_col += 8 - (screen_col % 8);
2305 }
2306 else
2307 ++screen_col;
2308 }
2309 }
2310 check_cursor();
2311 }
2312 else
2313 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002316 {
2317 pos_T save_cursor;
2318
2319 /* Move the cursor to the first line in the buffer */
2320 save_cursor = curwin->w_cursor;
2321 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002322 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2323 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002324 curwin->w_cursor = save_cursor;
2325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
2327#ifdef FEAT_FOLDING
2328 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2329 foldOpenCursor();
2330#endif
2331 if (print_message)
2332 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002333 /* Update the screen before showing the message, unless the screen
2334 * scrolled up. */
2335 if (!msg_scrolled)
2336 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002338 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2340 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2341 /* Add the message, skipping leading whitespace and newlines. */
2342 len = (int)STRLEN(IObuff);
2343 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2344
2345 /* Output the message. Overwrite to avoid scrolling when the 'O'
2346 * flag is present in 'shortmess'; But when not jumping, print the
2347 * whole message. */
2348 i = msg_scroll;
2349 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2350 msg_scroll = TRUE;
2351 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2352 msg_scroll = FALSE;
2353 msg_attr_keep(IObuff, 0, TRUE);
2354 msg_scroll = i;
2355 }
2356 }
2357 else
2358 {
2359#ifdef FEAT_WINDOWS
2360 if (opened_window)
2361 win_close(curwin, TRUE); /* Close opened window */
2362#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002363 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
2365 /*
2366 * Couldn't open file, so put index back where it was. This could
2367 * happen if the file was readonly and we changed something.
2368 */
2369#ifdef FEAT_WINDOWS
2370failed:
2371#endif
2372 qf_ptr = old_qf_ptr;
2373 qf_index = old_qf_index;
2374 }
2375 }
2376theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002377 if (qi != NULL)
2378 {
2379 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2380 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#ifdef FEAT_WINDOWS
2383 if (p_swb != old_swb && opened_window)
2384 {
2385 /* Restore old 'switchbuf' value, but not when an autocommand or
2386 * modeline has changed the value. */
2387 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002390 swb_flags = old_swb_flags;
2391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 else
2393 free_string_option(old_swb);
2394 }
2395#endif
2396}
2397
2398/*
2399 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002400 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 */
2402 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002403qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002405 buf_T *buf;
2406 char_u *fname;
2407 qfline_T *qfp;
2408 int i;
2409 int idx1 = 1;
2410 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002411 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002412 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002413 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002415 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002417 if (eap->cmdidx == CMD_llist)
2418 {
2419 qi = GET_LOC_LIST(curwin);
2420 if (qi == NULL)
2421 {
2422 EMSG(_(e_loclist));
2423 return;
2424 }
2425 }
2426
2427 if (qi->qf_curlist >= qi->qf_listcount
2428 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 EMSG(_(e_quickfix));
2431 return;
2432 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002433 if (*arg == '+')
2434 {
2435 ++arg;
2436 plus = TRUE;
2437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2439 {
2440 EMSG(_(e_trailing));
2441 return;
2442 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002443 if (plus)
2444 {
2445 i = qi->qf_lists[qi->qf_curlist].qf_index;
2446 idx2 = i + idx1;
2447 idx1 = i;
2448 }
2449 else
2450 {
2451 i = qi->qf_lists[qi->qf_curlist].qf_count;
2452 if (idx1 < 0)
2453 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2454 if (idx2 < 0)
2455 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002458 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002460 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2461 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
2463 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2464 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002465 msg_putchar('\n');
2466 if (got_int)
2467 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002468
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002469 fname = NULL;
2470 if (qfp->qf_fnum != 0
2471 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2472 {
2473 fname = buf->b_fname;
2474 if (qfp->qf_type == 1) /* :helpgrep */
2475 fname = gettail(fname);
2476 }
2477 if (fname == NULL)
2478 sprintf((char *)IObuff, "%2d", i);
2479 else
2480 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2481 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002482 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002483 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2484 if (qfp->qf_lnum == 0)
2485 IObuff[0] = NUL;
2486 else if (qfp->qf_col == 0)
2487 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2488 else
2489 sprintf((char *)IObuff, ":%ld col %d",
2490 qfp->qf_lnum, qfp->qf_col);
2491 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2492 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2493 msg_puts_attr(IObuff, hl_attr(HLF_N));
2494 if (qfp->qf_pattern != NULL)
2495 {
2496 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2497 STRCAT(IObuff, ":");
2498 msg_puts(IObuff);
2499 }
2500 msg_puts((char_u *)" ");
2501
2502 /* Remove newlines and leading whitespace from the text. For an
2503 * unrecognized line keep the indent, the compiler may mark a word
2504 * with ^^^^. */
2505 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2507 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002508 msg_prt_line(IObuff, FALSE);
2509 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002511
2512 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002513 if (qfp == NULL)
2514 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002515 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 ui_breakcheck();
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518}
2519
2520/*
2521 * Remove newlines and leading whitespace from an error message.
2522 * Put the result in "buf[bufsize]".
2523 */
2524 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002525qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
2527 int i;
2528 char_u *p = text;
2529
2530 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2531 {
2532 if (*p == '\n')
2533 {
2534 buf[i] = ' ';
2535 while (*++p != NUL)
2536 if (!vim_iswhite(*p) && *p != '\n')
2537 break;
2538 }
2539 else
2540 buf[i] = *p++;
2541 }
2542 buf[i] = NUL;
2543}
2544
2545/*
2546 * ":colder [count]": Up in the quickfix stack.
2547 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002548 * ":lolder [count]": Up in the location list stack.
2549 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 */
2551 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002552qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002554 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 int count;
2556
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002557 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2558 {
2559 qi = GET_LOC_LIST(curwin);
2560 if (qi == NULL)
2561 {
2562 EMSG(_(e_loclist));
2563 return;
2564 }
2565 }
2566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (eap->addr_count != 0)
2568 count = eap->line2;
2569 else
2570 count = 1;
2571 while (count--)
2572 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002573 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002575 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002578 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002580 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582 else
2583 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002584 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002589 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002592 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593}
2594
2595 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002596qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002599 qi->qf_curlist + 1, qi->qf_listcount,
2600 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002602 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603#endif
2604}
2605
2606/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002607 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
2609 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002610qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002612 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002613 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002614 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002616 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002618 qfp = qi->qf_lists[idx].qf_start;
2619 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002620 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002621 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002622 vim_free(qfp->qf_text);
2623 stop = (qfp == qfpnext);
2624 vim_free(qfp->qf_pattern);
2625 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002626 if (stop)
2627 /* Somehow qf_count may have an incorrect value, set it to 1
2628 * to avoid crashing when it's wrong.
2629 * TODO: Avoid qf_count being incorrect. */
2630 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002631 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002632 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002633 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002635 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002636 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002637 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002638
2639 qf_clean_dir_stack(&qi->qf_dir_stack);
2640 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641}
2642
2643/*
2644 * qf_mark_adjust: adjust marks
2645 */
2646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002647qf_mark_adjust(
2648 win_T *wp,
2649 linenr_T line1,
2650 linenr_T line2,
2651 long amount,
2652 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002654 int i;
2655 qfline_T *qfp;
2656 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002657 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002658 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002660 if (!curbuf->b_has_qf_entry)
2661 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002662 if (wp != NULL)
2663 {
2664 if (wp->w_llist == NULL)
2665 return;
2666 qi = wp->w_llist;
2667 }
2668
2669 for (idx = 0; idx < qi->qf_listcount; ++idx)
2670 if (qi->qf_lists[idx].qf_count)
2671 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002672 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2673 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (qfp->qf_fnum == curbuf->b_fnum)
2675 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002676 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2678 {
2679 if (amount == MAXLNUM)
2680 qfp->qf_cleared = TRUE;
2681 else
2682 qfp->qf_lnum += amount;
2683 }
2684 else if (amount_after && qfp->qf_lnum > line2)
2685 qfp->qf_lnum += amount_after;
2686 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002687
2688 if (!found_one)
2689 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690}
2691
2692/*
2693 * Make a nice message out of the error character and the error number:
2694 * char number message
2695 * e or E 0 " error"
2696 * w or W 0 " warning"
2697 * i or I 0 " info"
2698 * 0 0 ""
2699 * other 0 " c"
2700 * e or E n " error n"
2701 * w or W n " warning n"
2702 * i or I n " info n"
2703 * 0 n " error n"
2704 * other n " c n"
2705 * 1 x "" :helpgrep
2706 */
2707 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002708qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 static char_u buf[20];
2711 static char_u cc[3];
2712 char_u *p;
2713
2714 if (c == 'W' || c == 'w')
2715 p = (char_u *)" warning";
2716 else if (c == 'I' || c == 'i')
2717 p = (char_u *)" info";
2718 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2719 p = (char_u *)" error";
2720 else if (c == 0 || c == 1)
2721 p = (char_u *)"";
2722 else
2723 {
2724 cc[0] = ' ';
2725 cc[1] = c;
2726 cc[2] = NUL;
2727 p = cc;
2728 }
2729
2730 if (nr <= 0)
2731 return p;
2732
2733 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2734 return buf;
2735}
2736
2737#if defined(FEAT_WINDOWS) || defined(PROTO)
2738/*
2739 * ":cwindow": open the quickfix window if we have errors to display,
2740 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002741 * ":lwindow": open the location list window if we have locations to display,
2742 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 */
2744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002745ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002747 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 win_T *win;
2749
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002750 if (eap->cmdidx == CMD_lwindow)
2751 {
2752 qi = GET_LOC_LIST(curwin);
2753 if (qi == NULL)
2754 return;
2755 }
2756
2757 /* Look for an existing quickfix window. */
2758 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
2760 /*
2761 * If a quickfix window is open but we have no errors to display,
2762 * close the window. If a quickfix window is not open, then open
2763 * it if we have errors; otherwise, leave it closed.
2764 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002765 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002766 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002767 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
2769 if (win != NULL)
2770 ex_cclose(eap);
2771 }
2772 else if (win == NULL)
2773 ex_copen(eap);
2774}
2775
2776/*
2777 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002778 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002781ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783 win_T *win = NULL;
2784 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2787 {
2788 qi = GET_LOC_LIST(curwin);
2789 if (qi == NULL)
2790 return;
2791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002793 /* Find existing quickfix window and close it. */
2794 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (win != NULL)
2796 win_close(win, FALSE);
2797}
2798
2799/*
2800 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002801 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 */
2803 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002804ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002806 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002809 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002810 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002811 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002813 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2814 {
2815 qi = GET_LOC_LIST(curwin);
2816 if (qi == NULL)
2817 {
2818 EMSG(_(e_loclist));
2819 return;
2820 }
2821 }
2822
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 if (eap->addr_count != 0)
2824 height = eap->line2;
2825 else
2826 height = QF_WINHEIGHT;
2827
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#ifdef FEAT_GUI
2830 need_mouse_correct = TRUE;
2831#endif
2832
2833 /*
2834 * Find existing quickfix window, or open a new one.
2835 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002836 win = qf_find_win(qi);
2837
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002838 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002841 if (eap->addr_count != 0)
2842 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002843 if (cmdmod.split & WSP_VERT)
2844 {
2845 if (height != W_WIDTH(win))
2846 win_setwidth(height);
2847 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002848 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002849 win_setheight(height);
2850 }
2851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 else
2853 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002854 qf_buf = qf_find_buf(qi);
2855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 /* The current window becomes the previous window afterwards. */
2857 win = curwin;
2858
Bram Moolenaar77642c02012-11-20 17:55:10 +01002859 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2860 && cmdmod.split == 0)
2861 /* Create the new window at the very bottom, except when
2862 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002863 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002864 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002866 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002868 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002870 /*
2871 * For the location list window, create a reference to the
2872 * location list from the window 'win'.
2873 */
2874 curwin->w_llist_ref = win->w_llist;
2875 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002877
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002878 if (oldwin != curwin)
2879 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002880 if (qf_buf != NULL)
2881 /* Use the existing quickfix buffer */
2882 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002883 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002884 else
2885 {
2886 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002887 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002888 /* switch off 'swapfile' */
2889 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2890 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002891 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002892 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002893 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002894#ifdef FEAT_DIFF
2895 curwin->w_p_diff = FALSE;
2896#endif
2897#ifdef FEAT_FOLDING
2898 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2899 OPT_LOCAL);
2900#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002903 /* Only set the height when still in the same tab page and there is no
2904 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002905 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 win_setheight(height);
2907 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2908 if (win_valid(win))
2909 prevwin = win;
2910 }
2911
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002912 qf_set_title_var(qi);
2913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 /*
2915 * Fill the buffer with the quickfix list.
2916 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002917 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 curwin->w_cursor.col = 0;
2921 check_cursor();
2922 update_topline(); /* scroll to show the line */
2923}
2924
2925/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002926 * Move the cursor in the quickfix window to "lnum".
2927 */
2928 static void
2929qf_win_goto(win_T *win, linenr_T lnum)
2930{
2931 win_T *old_curwin = curwin;
2932
2933 curwin = win;
2934 curbuf = win->w_buffer;
2935 curwin->w_cursor.lnum = lnum;
2936 curwin->w_cursor.col = 0;
2937#ifdef FEAT_VIRTUALEDIT
2938 curwin->w_cursor.coladd = 0;
2939#endif
2940 curwin->w_curswant = 0;
2941 update_topline(); /* scroll to show the line */
2942 redraw_later(VALID);
2943 curwin->w_redr_status = TRUE; /* update ruler */
2944 curwin = old_curwin;
2945 curbuf = curwin->w_buffer;
2946}
2947
2948/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02002949 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02002950 */
2951 void
2952ex_cbottom(exarg_T *eap UNUSED)
2953{
Bram Moolenaar537ef082016-07-09 17:56:19 +02002954 qf_info_T *qi = &ql_info;
2955 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02002956
Bram Moolenaar537ef082016-07-09 17:56:19 +02002957 if (eap->cmdidx == CMD_lbottom)
2958 {
2959 qi = GET_LOC_LIST(curwin);
2960 if (qi == NULL)
2961 {
2962 EMSG(_(e_loclist));
2963 return;
2964 }
2965 }
2966
2967 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02002968 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
2969 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
2970}
2971
2972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 * Return the number of the current entry (line number in the quickfix
2974 * window).
2975 */
2976 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002977qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979 qf_info_T *qi = &ql_info;
2980
2981 if (IS_LL_WINDOW(wp))
2982 /* In the location list window, use the referenced location list */
2983 qi = wp->w_llist_ref;
2984
2985 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986}
2987
2988/*
2989 * Update the cursor position in the quickfix window to the current error.
2990 * Return TRUE if there is a quickfix window.
2991 */
2992 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002993qf_win_pos_update(
2994 qf_info_T *qi,
2995 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002998 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000 /*
3001 * Put the cursor on the current error in the quickfix window, so that
3002 * it's viewable.
3003 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003004 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (win != NULL
3006 && qf_index <= win->w_buffer->b_ml.ml_line_count
3007 && old_qf_index != qf_index)
3008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 if (qf_index > old_qf_index)
3010 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003011 win->w_redraw_top = old_qf_index;
3012 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014 else
3015 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003016 win->w_redraw_top = qf_index;
3017 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003019 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021 return win != NULL;
3022}
3023
3024/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003025 * Check whether the given window is displaying the specified quickfix/location
3026 * list buffer
3027 */
3028 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003029is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003030{
3031 /*
3032 * A window displaying the quickfix buffer will have the w_llist_ref field
3033 * set to NULL.
3034 * A window displaying a location list buffer will have the w_llist_ref
3035 * pointing to the location list.
3036 */
3037 if (bt_quickfix(win->w_buffer))
3038 if ((qi == &ql_info && win->w_llist_ref == NULL)
3039 || (qi != &ql_info && win->w_llist_ref == qi))
3040 return TRUE;
3041
3042 return FALSE;
3043}
3044
3045/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003046 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003047 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003048 */
3049 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003050qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003051{
3052 win_T *win;
3053
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003054 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003055 if (is_qf_win(win, qi))
3056 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003057
3058 return win;
3059}
3060
3061/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003062 * Find a quickfix buffer.
3063 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 */
3065 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003066qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003068 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003069 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
Bram Moolenaar9c102382006-05-03 21:26:49 +00003071 FOR_ALL_TAB_WINDOWS(tp, win)
3072 if (is_qf_win(win, qi))
3073 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003074
Bram Moolenaar9c102382006-05-03 21:26:49 +00003075 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077
3078/*
3079 * Find the quickfix buffer. If it exists, update the contents.
3080 */
3081 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003082qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003085 win_T *win;
3086 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003090 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (buf != NULL)
3092 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003093 linenr_T old_line_count = buf->b_ml.ml_line_count;
3094
3095 if (old_last == NULL)
3096 /* set curwin/curbuf to buf and save a few things */
3097 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003099 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003100 {
3101 curwin_save = curwin;
3102 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02003103 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003104 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003105 }
3106
Bram Moolenaar864293a2016-06-02 13:40:04 +02003107 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003108
Bram Moolenaar864293a2016-06-02 13:40:04 +02003109 if (old_last == NULL)
3110 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003111 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003112
3113 /* restore curwin/curbuf and a few other things */
3114 aucmd_restbuf(&aco);
3115 }
3116
3117 /* Only redraw when added lines are visible. This avoids flickering
3118 * when the added lines are not visible. */
3119 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3120 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122}
3123
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003124/*
3125 * Set "w:quickfix_title" if "qi" has a title.
3126 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003127 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003128qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003129{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003130 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3131 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003132 qi->qf_lists[qi->qf_curlist].qf_title);
3133}
3134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135/*
3136 * Fill current buffer with quickfix errors, replacing any previous contents.
3137 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003138 * If "old_last" is not NULL append the items after this one.
3139 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3140 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 */
3142 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003143qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003145 linenr_T lnum;
3146 qfline_T *qfp;
3147 buf_T *errbuf;
3148 int len;
3149 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
Bram Moolenaar864293a2016-06-02 13:40:04 +02003151 if (old_last == NULL)
3152 {
3153 if (buf != curbuf)
3154 {
3155 EMSG2(_(e_intern2), "qf_fill_buffer()");
3156 return;
3157 }
3158
3159 /* delete all existing lines */
3160 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3161 (void)ml_delete((linenr_T)1, FALSE);
3162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003165 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003168 if (old_last == NULL)
3169 {
3170 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3171 lnum = 0;
3172 }
3173 else
3174 {
3175 qfp = old_last->qf_next;
3176 lnum = buf->b_ml.ml_line_count;
3177 }
3178 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
3180 if (qfp->qf_fnum != 0
3181 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3182 && errbuf->b_fname != NULL)
3183 {
3184 if (qfp->qf_type == 1) /* :helpgrep */
3185 STRCPY(IObuff, gettail(errbuf->b_fname));
3186 else
3187 STRCPY(IObuff, errbuf->b_fname);
3188 len = (int)STRLEN(IObuff);
3189 }
3190 else
3191 len = 0;
3192 IObuff[len++] = '|';
3193
3194 if (qfp->qf_lnum > 0)
3195 {
3196 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3197 len += (int)STRLEN(IObuff + len);
3198
3199 if (qfp->qf_col > 0)
3200 {
3201 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3202 len += (int)STRLEN(IObuff + len);
3203 }
3204
3205 sprintf((char *)IObuff + len, "%s",
3206 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3207 len += (int)STRLEN(IObuff + len);
3208 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003209 else if (qfp->qf_pattern != NULL)
3210 {
3211 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3212 len += (int)STRLEN(IObuff + len);
3213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 IObuff[len++] = '|';
3215 IObuff[len++] = ' ';
3216
3217 /* Remove newlines and leading whitespace from the text.
3218 * For an unrecognized line keep the indent, the compiler may
3219 * mark a word with ^^^^. */
3220 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3221 IObuff + len, IOSIZE - len);
3222
Bram Moolenaar864293a2016-06-02 13:40:04 +02003223 if (ml_append_buf(buf, lnum, IObuff,
3224 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003226 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003228 if (qfp == NULL)
3229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003231
3232 if (old_last == NULL)
3233 /* Delete the empty line which is now at the end */
3234 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
3236
3237 /* correct cursor position */
3238 check_lnums(TRUE);
3239
Bram Moolenaar864293a2016-06-02 13:40:04 +02003240 if (old_last == NULL)
3241 {
3242 /* Set the 'filetype' to "qf" each time after filling the buffer.
3243 * This resembles reading a file into a buffer, it's more logical when
3244 * using autocommands. */
3245 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3246 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
3248#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003249 keep_filetype = TRUE; /* don't detect 'filetype' */
3250 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003252 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003254 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003256 /* make sure it will be redrawn */
3257 redraw_curbuf_later(NOT_VALID);
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260 /* Restore KeyTyped, setting 'filetype' may reset it. */
3261 KeyTyped = old_KeyTyped;
3262}
3263
3264#endif /* FEAT_WINDOWS */
3265
3266/*
3267 * Return TRUE if "buf" is the quickfix buffer.
3268 */
3269 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003270bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003272 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273}
3274
3275/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003276 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3277 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 */
3279 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003280bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003282 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3283 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284}
3285
3286/*
3287 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3288 */
3289 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003290bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003292 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293}
3294
3295 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003296bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 if (bt_dontwrite(buf))
3299 {
3300 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3301 return TRUE;
3302 }
3303 return FALSE;
3304}
3305
3306/*
3307 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3308 * and 'bufhidden'.
3309 */
3310 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003311buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3314 switch (buf->b_p_bh[0])
3315 {
3316 case 'u': /* "unload" */
3317 case 'w': /* "wipe" */
3318 case 'd': return FALSE; /* "delete" */
3319 case 'h': return TRUE; /* "hide" */
3320 }
3321 return (p_hid || cmdmod.hide);
3322}
3323
3324/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003325 * Return TRUE when using ":vimgrep" for ":grep".
3326 */
3327 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003328grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003329{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003330 return ((cmdidx == CMD_grep
3331 || cmdidx == CMD_lgrep
3332 || cmdidx == CMD_grepadd
3333 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003334 && STRCMP("internal",
3335 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3336}
3337
3338/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003339 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 */
3341 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003342ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003344 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 char_u *cmd;
3346 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003347 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003348 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003349 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003350#ifdef FEAT_AUTOCMD
3351 char_u *au_name = NULL;
3352
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003353 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3354 if (grep_internal(eap->cmdidx))
3355 {
3356 ex_vimgrep(eap);
3357 return;
3358 }
3359
Bram Moolenaar7c626922005-02-07 22:01:03 +00003360 switch (eap->cmdidx)
3361 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003362 case CMD_make: au_name = (char_u *)"make"; break;
3363 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3364 case CMD_grep: au_name = (char_u *)"grep"; break;
3365 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3366 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3367 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003368 default: break;
3369 }
3370 if (au_name != NULL)
3371 {
3372 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3373 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003374# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003375 if (did_throw || force_abort)
3376 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003377# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003378 }
3379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaara6557602006-02-04 22:43:20 +00003381 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3382 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003383 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003386 fname = get_mef_name();
3387 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003389 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391 /*
3392 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3393 */
3394 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3395 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003396 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 cmd = alloc(len);
3398 if (cmd == NULL)
3399 return;
3400 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3401 (char *)p_shq);
3402 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003403 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 /*
3405 * Output a newline if there's something else than the :make command that
3406 * was typed (in which case the cursor is in column 0).
3407 */
3408 if (msg_col == 0)
3409 msg_didout = FALSE;
3410 msg_start();
3411 MSG_PUTS(":!");
3412 msg_outtrans(cmd); /* show what we are doing */
3413
3414 /* let the shell know if we are redirecting output or not */
3415 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3416
3417#ifdef AMIGA
3418 out_flush();
3419 /* read window status report and redraw before message */
3420 (void)char_avail();
3421#endif
3422
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003423 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003424 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3425 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003426 && eap->cmdidx != CMD_lgrepadd),
3427 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003428 if (wp != NULL)
3429 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003430#ifdef FEAT_AUTOCMD
3431 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003432 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003433 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3434 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003435 if (qi->qf_curlist < qi->qf_listcount)
3436 res = qi->qf_lists[qi->qf_curlist].qf_count;
3437 else
3438 res = 0;
3439 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003440#endif
3441 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003442 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar7c626922005-02-07 22:01:03 +00003444 mch_remove(fname);
3445 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 vim_free(cmd);
3447}
3448
3449/*
3450 * Return the name for the errorfile, in allocated memory.
3451 * Find a new unique name when 'makeef' contains "##".
3452 * Returns NULL for error.
3453 */
3454 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003455get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 char_u *p;
3458 char_u *name;
3459 static int start = -1;
3460 static int off = 0;
3461#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003462 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463#endif
3464
3465 if (*p_mef == NUL)
3466 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003467 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (name == NULL)
3469 EMSG(_(e_notmp));
3470 return name;
3471 }
3472
3473 for (p = p_mef; *p; ++p)
3474 if (p[0] == '#' && p[1] == '#')
3475 break;
3476
3477 if (*p == NUL)
3478 return vim_strsave(p_mef);
3479
3480 /* Keep trying until the name doesn't exist yet. */
3481 for (;;)
3482 {
3483 if (start == -1)
3484 start = mch_get_pid();
3485 else
3486 off += 19;
3487
3488 name = alloc((unsigned)STRLEN(p_mef) + 30);
3489 if (name == NULL)
3490 break;
3491 STRCPY(name, p_mef);
3492 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3493 STRCAT(name, p + 2);
3494 if (mch_getperm(name) < 0
3495#ifdef HAVE_LSTAT
3496 /* Don't accept a symbolic link, its a security risk. */
3497 && mch_lstat((char *)name, &sb) < 0
3498#endif
3499 )
3500 break;
3501 vim_free(name);
3502 }
3503 return name;
3504}
3505
3506/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003507 * Returns the number of valid entries in the current quickfix/location list.
3508 */
3509 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003510qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003511{
3512 qf_info_T *qi = &ql_info;
3513 qfline_T *qfp;
3514 int i, sz = 0;
3515 int prev_fnum = 0;
3516
3517 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3518 {
3519 /* Location list */
3520 qi = GET_LOC_LIST(curwin);
3521 if (qi == NULL)
3522 return 0;
3523 }
3524
3525 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003526 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003527 ++i, qfp = qfp->qf_next)
3528 {
3529 if (qfp->qf_valid)
3530 {
3531 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3532 sz++; /* Count all valid entries */
3533 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3534 {
3535 /* Count the number of files */
3536 sz++;
3537 prev_fnum = qfp->qf_fnum;
3538 }
3539 }
3540 }
3541
3542 return sz;
3543}
3544
3545/*
3546 * Returns the current index of the quickfix/location list.
3547 * Returns 0 if there is an error.
3548 */
3549 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003550qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003551{
3552 qf_info_T *qi = &ql_info;
3553
3554 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3555 {
3556 /* Location list */
3557 qi = GET_LOC_LIST(curwin);
3558 if (qi == NULL)
3559 return 0;
3560 }
3561
3562 return qi->qf_lists[qi->qf_curlist].qf_index;
3563}
3564
3565/*
3566 * Returns the current index in the quickfix/location list (counting only valid
3567 * entries). If no valid entries are in the list, then returns 1.
3568 */
3569 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003570qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003571{
3572 qf_info_T *qi = &ql_info;
3573 qf_list_T *qfl;
3574 qfline_T *qfp;
3575 int i, eidx = 0;
3576 int prev_fnum = 0;
3577
3578 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3579 {
3580 /* Location list */
3581 qi = GET_LOC_LIST(curwin);
3582 if (qi == NULL)
3583 return 1;
3584 }
3585
3586 qfl = &qi->qf_lists[qi->qf_curlist];
3587 qfp = qfl->qf_start;
3588
3589 /* check if the list has valid errors */
3590 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3591 return 1;
3592
3593 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3594 {
3595 if (qfp->qf_valid)
3596 {
3597 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3598 {
3599 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3600 {
3601 /* Count the number of files */
3602 eidx++;
3603 prev_fnum = qfp->qf_fnum;
3604 }
3605 }
3606 else
3607 eidx++;
3608 }
3609 }
3610
3611 return eidx ? eidx : 1;
3612}
3613
3614/*
3615 * Get the 'n'th valid error entry in the quickfix or location list.
3616 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3617 * For :cdo and :ldo returns the 'n'th valid error entry.
3618 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3619 */
3620 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003621qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003622{
3623 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3624 qfline_T *qfp = qfl->qf_start;
3625 int i, eidx;
3626 int prev_fnum = 0;
3627
3628 /* check if the list has valid errors */
3629 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3630 return 1;
3631
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003632 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003633 i++, qfp = qfp->qf_next)
3634 {
3635 if (qfp->qf_valid)
3636 {
3637 if (fdo)
3638 {
3639 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3640 {
3641 /* Count the number of files */
3642 eidx++;
3643 prev_fnum = qfp->qf_fnum;
3644 }
3645 }
3646 else
3647 eidx++;
3648 }
3649
3650 if (eidx == n)
3651 break;
3652 }
3653
3654 if (i <= qfl->qf_count)
3655 return i;
3656 else
3657 return 1;
3658}
3659
3660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003662 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003663 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
3665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003666ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003668 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003669 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003670
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003671 if (eap->cmdidx == CMD_ll
3672 || eap->cmdidx == CMD_lrewind
3673 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003674 || eap->cmdidx == CMD_llast
3675 || eap->cmdidx == CMD_ldo
3676 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003677 {
3678 qi = GET_LOC_LIST(curwin);
3679 if (qi == NULL)
3680 {
3681 EMSG(_(e_loclist));
3682 return;
3683 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003684 }
3685
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003686 if (eap->addr_count > 0)
3687 errornr = (int)eap->line2;
3688 else
3689 {
3690 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3691 errornr = 0;
3692 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3693 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3694 errornr = 1;
3695 else
3696 errornr = 32767;
3697 }
3698
3699 /* For cdo and ldo commands, jump to the nth valid error.
3700 * For cfdo and lfdo commands, jump to the nth valid file entry.
3701 */
3702 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3703 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3704 errornr = qf_get_nth_valid_entry(qi,
3705 eap->addr_count > 0 ? (int)eap->line1 : 1,
3706 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3707
3708 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709}
3710
3711/*
3712 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003713 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003714 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 */
3716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003717ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003719 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003720 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003721
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003722 if (eap->cmdidx == CMD_lnext
3723 || eap->cmdidx == CMD_lNext
3724 || eap->cmdidx == CMD_lprevious
3725 || eap->cmdidx == CMD_lnfile
3726 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003727 || eap->cmdidx == CMD_lpfile
3728 || eap->cmdidx == CMD_ldo
3729 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003730 {
3731 qi = GET_LOC_LIST(curwin);
3732 if (qi == NULL)
3733 {
3734 EMSG(_(e_loclist));
3735 return;
3736 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003737 }
3738
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003739 if (eap->addr_count > 0 &&
3740 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3741 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3742 errornr = (int)eap->line2;
3743 else
3744 errornr = 1;
3745
3746 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3747 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003749 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3750 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003752 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3753 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 ? BACKWARD_FILE
3755 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003756 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757}
3758
3759/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003760 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003761 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 */
3763 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003764ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003766 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003767 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003768#ifdef FEAT_AUTOCMD
3769 char_u *au_name = NULL;
3770#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003771
3772 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003773 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003774 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003775
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003776#ifdef FEAT_AUTOCMD
3777 switch (eap->cmdidx)
3778 {
3779 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3780 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3781 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3782 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3783 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3784 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3785 default: break;
3786 }
3787 if (au_name != NULL)
3788 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3789#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003790#ifdef FEAT_BROWSE
3791 if (cmdmod.browse)
3792 {
3793 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3794 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3795 if (browse_file == NULL)
3796 return;
3797 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3798 vim_free(browse_file);
3799 }
3800 else
3801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003803 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003804
3805 /*
3806 * This function is used by the :cfile, :cgetfile and :caddfile
3807 * commands.
3808 * :cfile always creates a new quickfix list and jumps to the
3809 * first error.
3810 * :cgetfile creates a new quickfix list but doesn't jump to the
3811 * first error.
3812 * :caddfile adds to an existing quickfix list. If there is no
3813 * quickfix list then a new list is created.
3814 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003816 && eap->cmdidx != CMD_laddfile),
3817 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003818 && (eap->cmdidx == CMD_cfile
3819 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003820 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003821#ifdef FEAT_AUTOCMD
3822 if (au_name != NULL)
3823 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3824#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003825 if (wp != NULL)
3826 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003827 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003828 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003829
3830 else
3831 {
3832#ifdef FEAT_AUTOCMD
3833 if (au_name != NULL)
3834 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3835#endif
3836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837}
3838
3839/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003840 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003841 * ":vimgrepadd {pattern} file(s)"
3842 * ":lvimgrep {pattern} file(s)"
3843 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003844 */
3845 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003846ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003847{
Bram Moolenaar81695252004-12-29 20:58:21 +00003848 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003849 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003850 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003851 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003852 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003853 char_u *s;
3854 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003855 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003856 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003857#ifdef FEAT_AUTOCMD
3858 qfline_T *cur_qf_start;
3859#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003860 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003861 buf_T *buf;
3862 int duplicate_name = FALSE;
3863 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003864 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003865 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003866 buf_T *first_match_buf = NULL;
3867 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003868 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003869#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3870 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003871#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003872 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003873 int flags = 0;
3874 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003875 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003876 char_u *dirname_start = NULL;
3877 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003878 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003879#ifdef FEAT_AUTOCMD
3880 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003881
3882 switch (eap->cmdidx)
3883 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003884 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3885 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3886 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003887 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003888 case CMD_grep: au_name = (char_u *)"grep"; break;
3889 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3890 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3891 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003892 default: break;
3893 }
3894 if (au_name != NULL)
3895 {
3896 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3897 curbuf->b_fname, TRUE, curbuf);
3898 if (did_throw || force_abort)
3899 return;
3900 }
3901#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003902
Bram Moolenaar754b5602006-02-09 23:53:20 +00003903 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003904 || eap->cmdidx == CMD_lvimgrep
3905 || eap->cmdidx == CMD_lgrepadd
3906 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003907 {
3908 qi = ll_get_or_alloc_list(curwin);
3909 if (qi == NULL)
3910 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003911 }
3912
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003913 if (eap->addr_count > 0)
3914 tomatch = eap->line2;
3915 else
3916 tomatch = MAXLNUM;
3917
Bram Moolenaar81695252004-12-29 20:58:21 +00003918 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003919 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003920 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003921 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003922 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003923 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003924 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003925 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003926 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003927
3928 if (s != NULL && *s == NUL)
3929 {
3930 /* Pattern is empty, use last search pattern. */
3931 if (last_search_pat() == NULL)
3932 {
3933 EMSG(_(e_noprevre));
3934 goto theend;
3935 }
3936 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3937 }
3938 else
3939 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3940
Bram Moolenaar86b68352004-12-27 21:59:20 +00003941 if (regmatch.regprog == NULL)
3942 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003943 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003944 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003945
3946 p = skipwhite(p);
3947 if (*p == NUL)
3948 {
3949 EMSG(_("E683: File name missing or invalid pattern"));
3950 goto theend;
3951 }
3952
Bram Moolenaar754b5602006-02-09 23:53:20 +00003953 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003954 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003955 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003956 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003957 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003958
3959 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003960 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003961 goto theend;
3962 if (fcount == 0)
3963 {
3964 EMSG(_(e_nomatch));
3965 goto theend;
3966 }
3967
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003968 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3969 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003970 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003971 {
3972 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003973 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003974 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003975
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003976 /* Remember the current directory, because a BufRead autocommand that does
3977 * ":lcd %:p:h" changes the meaning of short path names. */
3978 mch_dirname(dirname_start, MAXPATHL);
3979
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003980#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003981 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003982 * changing the current quickfix list. */
3983 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3984#endif
3985
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003986 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003987 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003988 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003989 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003990 if (time(NULL) > seconds)
3991 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003992 /* Display the file name every second or so, show the user we are
3993 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003994 seconds = time(NULL);
3995 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003996 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003997 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003998 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003999 else
4000 {
4001 msg_outtrans(p);
4002 vim_free(p);
4003 }
4004 msg_clr_eos();
4005 msg_didout = FALSE; /* overwrite this message */
4006 msg_nowait = TRUE; /* don't wait for this message */
4007 msg_col = 0;
4008 out_flush();
4009 }
4010
Bram Moolenaar81695252004-12-29 20:58:21 +00004011 buf = buflist_findname_exp(fnames[fi]);
4012 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4013 {
4014 /* Remember that a buffer with this name already exists. */
4015 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004016 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004017 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004018
4019#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4020 /* Don't do Filetype autocommands to avoid loading syntax and
4021 * indent scripts, a great speed improvement. */
4022 save_ei = au_event_disable(",Filetype");
4023#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004024 /* Don't use modelines here, it's useless. */
4025 save_mls = p_mls;
4026 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004027
4028 /* Load file into a buffer, so that 'fileencoding' is detected,
4029 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004030 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004031
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004032 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004033#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4034 au_event_restore(save_ei);
4035#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004036 }
4037 else
4038 /* Use existing, loaded buffer. */
4039 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004040
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004041#ifdef FEAT_AUTOCMD
4042 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4043 {
4044 int idx;
4045
4046 /* Autocommands changed the quickfix list. Find the one we were
4047 * using and restore it. */
4048 for (idx = 0; idx < LISTCOUNT; ++idx)
4049 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4050 {
4051 qi->qf_curlist = idx;
4052 break;
4053 }
4054 if (idx == LISTCOUNT)
4055 {
4056 /* List cannot be found, create a new one. */
4057 qf_new_list(qi, *eap->cmdlinep);
4058 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4059 }
4060 }
4061#endif
4062
Bram Moolenaar81695252004-12-29 20:58:21 +00004063 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004064 {
4065 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004066 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004067 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004068 else
4069 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004070 /* Try for a match in all lines of the buffer.
4071 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004072 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004073 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4074 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004075 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004076 col = 0;
4077 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004078 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004079 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004080 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004081 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004082 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004083 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004084 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00004085 ml_get_buf(buf,
4086 regmatch.startpos[0].lnum + lnum, FALSE),
4087 regmatch.startpos[0].lnum + lnum,
4088 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004089 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004090 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004091 0, /* nr */
4092 0, /* type */
4093 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004094 ) == FAIL)
4095 {
4096 got_int = TRUE;
4097 break;
4098 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004099 found_match = TRUE;
4100 if (--tomatch == 0)
4101 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004102 if ((flags & VGR_GLOBAL) == 0
4103 || regmatch.endpos[0].lnum > 0)
4104 break;
4105 col = regmatch.endpos[0].col
4106 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004107 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004108 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004109 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004110 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004111 if (got_int)
4112 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004113 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004114#ifdef FEAT_AUTOCMD
4115 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4116#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004117
4118 if (using_dummy)
4119 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004120 if (found_match && first_match_buf == NULL)
4121 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004122 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004123 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004124 /* Never keep a dummy buffer if there is another buffer
4125 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004126 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004127 buf = NULL;
4128 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004129 else if (!cmdmod.hide
4130 || buf->b_p_bh[0] == 'u' /* "unload" */
4131 || buf->b_p_bh[0] == 'w' /* "wipe" */
4132 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004133 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004134 /* When no match was found we don't need to remember the
4135 * buffer, wipe it out. If there was a match and it
4136 * wasn't the first one or we won't jump there: only
4137 * unload the buffer.
4138 * Ignore 'hidden' here, because it may lead to having too
4139 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004140 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004141 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004142 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004143 buf = NULL;
4144 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004145 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004146 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004147 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004148 buf = NULL;
4149 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004150 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004151
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004152 if (buf != NULL)
4153 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004154 /* If the buffer is still loaded we need to use the
4155 * directory we jumped to below. */
4156 if (buf == first_match_buf
4157 && target_dir == NULL
4158 && STRCMP(dirname_start, dirname_now) != 0)
4159 target_dir = vim_strsave(dirname_now);
4160
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004161 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004162 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004163 * need to be done (again). But not the window-local
4164 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004165 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004166#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004167 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4168 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004169#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004170 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004171 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004172 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004173 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004174 }
4175 }
4176
4177 FreeWild(fcount, fnames);
4178
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004179 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4180 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4181 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004182
4183#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004184 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004185#endif
4186
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004187#ifdef FEAT_AUTOCMD
4188 if (au_name != NULL)
4189 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4190 curbuf->b_fname, TRUE, curbuf);
4191#endif
4192
Bram Moolenaar86b68352004-12-27 21:59:20 +00004193 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004194 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004195 {
4196 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004197 {
4198 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004199 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004200 if (buf != curbuf)
4201 /* If we jumped to another buffer redrawing will already be
4202 * taken care of. */
4203 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004204
4205 /* Jump to the directory used after loading the buffer. */
4206 if (curbuf == first_match_buf && target_dir != NULL)
4207 {
4208 exarg_T ea;
4209
4210 ea.arg = target_dir;
4211 ea.cmdidx = CMD_lcd;
4212 ex_cd(&ea);
4213 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004214 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004216 else
4217 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004218
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004219 /* If we loaded a dummy buffer into the current window, the autocommands
4220 * may have messed up things, need to redraw and recompute folds. */
4221 if (redraw_for_dummy)
4222 {
4223#ifdef FEAT_FOLDING
4224 foldUpdateAll(curwin);
4225#else
4226 redraw_later(NOT_VALID);
4227#endif
4228 }
4229
Bram Moolenaar86b68352004-12-27 21:59:20 +00004230theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004231 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004232 vim_free(dirname_now);
4233 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004234 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004235 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004236}
4237
4238/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004239 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004240 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004241 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4242 * If "s" is not NULL terminate the pattern with a NUL.
4243 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004244 */
4245 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004246skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004247{
4248 int c;
4249
4250 if (vim_isIDc(*p))
4251 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004252 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004253 if (s != NULL)
4254 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004255 p = skiptowhite(p);
4256 if (s != NULL && *p != NUL)
4257 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004258 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004259 else
4260 {
4261 /* ":vimgrep /pattern/[g][j] fname" */
4262 if (s != NULL)
4263 *s = p + 1;
4264 c = *p;
4265 p = skip_regexp(p + 1, c, TRUE, NULL);
4266 if (*p != c)
4267 return NULL;
4268
4269 /* Truncate the pattern. */
4270 if (s != NULL)
4271 *p = NUL;
4272 ++p;
4273
4274 /* Find the flags */
4275 while (*p == 'g' || *p == 'j')
4276 {
4277 if (flags != NULL)
4278 {
4279 if (*p == 'g')
4280 *flags |= VGR_GLOBAL;
4281 else
4282 *flags |= VGR_NOJUMP;
4283 }
4284 ++p;
4285 }
4286 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004287 return p;
4288}
4289
4290/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004291 * Restore current working directory to "dirname_start" if they differ, taking
4292 * into account whether it is set locally or globally.
4293 */
4294 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004295restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004296{
4297 char_u *dirname_now = alloc(MAXPATHL);
4298
4299 if (NULL != dirname_now)
4300 {
4301 mch_dirname(dirname_now, MAXPATHL);
4302 if (STRCMP(dirname_start, dirname_now) != 0)
4303 {
4304 /* If the directory has changed, change it back by building up an
4305 * appropriate ex command and executing it. */
4306 exarg_T ea;
4307
4308 ea.arg = dirname_start;
4309 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4310 ex_cd(&ea);
4311 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004312 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004313 }
4314}
4315
4316/*
4317 * Load file "fname" into a dummy buffer and return the buffer pointer,
4318 * placing the directory resulting from the buffer load into the
4319 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4320 * prior to calling this function. Restores directory to "dirname_start" prior
4321 * to returning, if autocmds or the 'autochdir' option have changed it.
4322 *
4323 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4324 * or wipe_dummy_buffer() later!
4325 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004326 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004327 */
4328 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004329load_dummy_buffer(
4330 char_u *fname,
4331 char_u *dirname_start, /* in: old directory */
4332 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004333{
4334 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004335 bufref_T newbufref;
4336 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004337 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004338 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004339
4340 /* Allocate a buffer without putting it in the buffer list. */
4341 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4342 if (newbuf == NULL)
4343 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004344 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004345
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004346 /* Init the options. */
4347 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4348
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004349 /* need to open the memfile before putting the buffer in a window */
4350 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004351 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004352 /* set curwin/curbuf to buf and save a few things */
4353 aucmd_prepbuf(&aco, newbuf);
4354
4355 /* Need to set the filename for autocommands. */
4356 (void)setfname(curbuf, fname, NULL, FALSE);
4357
Bram Moolenaar81695252004-12-29 20:58:21 +00004358 /* Create swap file now to avoid the ATTENTION message. */
4359 check_need_swap(TRUE);
4360
4361 /* Remove the "dummy" flag, otherwise autocommands may not
4362 * work. */
4363 curbuf->b_flags &= ~BF_DUMMY;
4364
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004365 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004366 if (readfile(fname, NULL,
4367 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4368 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004369 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004370 && !(curbuf->b_flags & BF_NEW))
4371 {
4372 failed = FALSE;
4373 if (curbuf != newbuf)
4374 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004375 /* Bloody autocommands changed the buffer! Can happen when
4376 * using netrw and editing a remote file. Use the current
4377 * buffer instead, delete the dummy one after restoring the
4378 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004379 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004380 newbuf = curbuf;
4381 }
4382 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004383
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004384 /* restore curwin/curbuf and a few other things */
4385 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004386 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4387 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004388
4389 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4390 * skip it. */
4391 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004392 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004393
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004394 /*
4395 * When autocommands/'autochdir' option changed directory: go back.
4396 * Let the caller know what the resulting dir was first, in case it is
4397 * important.
4398 */
4399 mch_dirname(resulting_dir, MAXPATHL);
4400 restore_start_dir(dirname_start);
4401
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004402 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004403 return NULL;
4404 if (failed)
4405 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004406 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004407 return NULL;
4408 }
4409 return newbuf;
4410}
4411
4412/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004413 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4414 * directory to "dirname_start" prior to returning, if autocmds or the
4415 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004416 */
4417 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004418wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004419{
4420 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004421 {
4422#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4423 cleanup_T cs;
4424
4425 /* Reset the error/interrupt/exception state here so that aborting()
4426 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4427 * work when got_int is set. */
4428 enter_cleanup(&cs);
4429#endif
4430
Bram Moolenaar81695252004-12-29 20:58:21 +00004431 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004432
4433#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4434 /* Restore the error/interrupt/exception state if not discarded by a
4435 * new aborting error, interrupt, or uncaught exception. */
4436 leave_cleanup(&cs);
4437#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004438 /* When autocommands/'autochdir' option changed directory: go back. */
4439 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004440 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004441}
4442
4443/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004444 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4445 * directory to "dirname_start" prior to returning, if autocmds or the
4446 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004447 */
4448 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004449unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004450{
4451 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004452 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004453 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004454
4455 /* When autocommands/'autochdir' option changed directory: go back. */
4456 restore_start_dir(dirname_start);
4457 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004458}
4459
Bram Moolenaar05159a02005-02-26 23:04:13 +00004460#if defined(FEAT_EVAL) || defined(PROTO)
4461/*
4462 * Add each quickfix error to list "list" as a dictionary.
4463 */
4464 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004465get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004466{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004467 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004468 dict_T *dict;
4469 char_u buf[2];
4470 qfline_T *qfp;
4471 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004472 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004473
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004474 if (wp != NULL)
4475 {
4476 qi = GET_LOC_LIST(wp);
4477 if (qi == NULL)
4478 return FAIL;
4479 }
4480
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004481 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004482 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004483 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004484
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004485 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4486 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004487 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004488 /* Handle entries with a non-existing buffer number. */
4489 bufnum = qfp->qf_fnum;
4490 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4491 bufnum = 0;
4492
Bram Moolenaar05159a02005-02-26 23:04:13 +00004493 if ((dict = dict_alloc()) == NULL)
4494 return FAIL;
4495 if (list_append_dict(list, dict) == FAIL)
4496 return FAIL;
4497
4498 buf[0] = qfp->qf_type;
4499 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004500 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004501 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4502 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4503 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4504 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004505 || dict_add_nr_str(dict, "pattern", 0L,
4506 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4507 || dict_add_nr_str(dict, "text", 0L,
4508 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004509 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4510 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4511 return FAIL;
4512
4513 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004514 if (qfp == NULL)
4515 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004516 }
4517 return OK;
4518}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004519
4520/*
4521 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004522 * of dictionaries. "title" will be copied to w:quickfix_title.
4523 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004524 */
4525 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004526set_errorlist(
4527 win_T *wp,
4528 list_T *list,
4529 int action,
4530 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004531{
4532 listitem_T *li;
4533 dict_T *d;
4534 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004535 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004536 long lnum;
4537 int col, nr;
4538 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004539#ifdef FEAT_WINDOWS
4540 qfline_T *old_last = NULL;
4541#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004542 int valid, status;
4543 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004544 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004545 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004546
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004547 if (wp != NULL)
4548 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004549 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004550 if (qi == NULL)
4551 return FAIL;
4552 }
4553
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004554 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004555 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004556 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004557#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004558 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4559 /* Adding to existing list, use last entry. */
4560 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004561#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004562 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004563 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004564 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004565 qf_store_title(qi, title);
4566 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004567
4568 for (li = list->lv_first; li != NULL; li = li->li_next)
4569 {
4570 if (li->li_tv.v_type != VAR_DICT)
4571 continue; /* Skip non-dict items */
4572
4573 d = li->li_tv.vval.v_dict;
4574 if (d == NULL)
4575 continue;
4576
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004577 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004578 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4579 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4580 col = (int)get_dict_number(d, (char_u *)"col");
4581 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4582 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004583 type = get_dict_string(d, (char_u *)"type", TRUE);
4584 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4585 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004586 if (text == NULL)
4587 text = vim_strsave((char_u *)"");
4588
4589 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004590 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004591 valid = FALSE;
4592
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004593 /* Mark entries with non-existing buffer number as not valid. Give the
4594 * error message only once. */
4595 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4596 {
4597 if (!did_bufnr_emsg)
4598 {
4599 did_bufnr_emsg = TRUE;
4600 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4601 }
4602 valid = FALSE;
4603 bufnum = 0;
4604 }
4605
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004606 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004607 NULL, /* dir */
4608 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004609 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004610 text,
4611 lnum,
4612 col,
4613 vcol, /* vis_col */
4614 pattern, /* search pattern */
4615 nr,
4616 type == NULL ? NUL : *type,
4617 valid);
4618
4619 vim_free(filename);
4620 vim_free(pattern);
4621 vim_free(text);
4622 vim_free(type);
4623
4624 if (status == FAIL)
4625 {
4626 retval = FAIL;
4627 break;
4628 }
4629 }
4630
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004631 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004632 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004633 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4634 else
4635 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004636 if (action != 'a') {
4637 qi->qf_lists[qi->qf_curlist].qf_ptr =
4638 qi->qf_lists[qi->qf_curlist].qf_start;
4639 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4640 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4641 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004642
4643#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004644 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004645 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004646#endif
4647
4648 return retval;
4649}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004650#endif
4651
Bram Moolenaar81695252004-12-29 20:58:21 +00004652/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004653 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004654 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004655 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004656 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004657 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004658 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004659 */
4660 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004661ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004662{
4663 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004664 qf_info_T *qi = &ql_info;
4665
Bram Moolenaardb552d602006-03-23 22:59:57 +00004666 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4667 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004668 {
4669 qi = ll_get_or_alloc_list(curwin);
4670 if (qi == NULL)
4671 return;
4672 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004673
4674 if (*eap->arg == NUL)
4675 buf = curbuf;
4676 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4677 buf = buflist_findnr(atoi((char *)eap->arg));
4678 if (buf == NULL)
4679 EMSG(_(e_invarg));
4680 else if (buf->b_ml.ml_mfp == NULL)
4681 EMSG(_("E681: Buffer is not loaded"));
4682 else
4683 {
4684 if (eap->addr_count == 0)
4685 {
4686 eap->line1 = 1;
4687 eap->line2 = buf->b_ml.ml_line_count;
4688 }
4689 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4690 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4691 EMSG(_(e_invrange));
4692 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004693 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004694 char_u *qf_title = *eap->cmdlinep;
4695
4696 if (buf->b_sfname)
4697 {
4698 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4699 (char *)qf_title, (char *)buf->b_sfname);
4700 qf_title = IObuff;
4701 }
4702
Bram Moolenaardb552d602006-03-23 22:59:57 +00004703 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4704 (eap->cmdidx != CMD_caddbuffer
4705 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004706 eap->line1, eap->line2,
4707 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004708 && (eap->cmdidx == CMD_cbuffer
4709 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004710 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4711 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004712 }
4713}
4714
Bram Moolenaar1e015462005-09-25 22:16:38 +00004715#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004716/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004717 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4718 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004719 */
4720 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004721ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004722{
4723 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004724 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004725
Bram Moolenaardb552d602006-03-23 22:59:57 +00004726 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4727 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004728 {
4729 qi = ll_get_or_alloc_list(curwin);
4730 if (qi == NULL)
4731 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004732 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004733
Bram Moolenaar4770d092006-01-12 23:22:24 +00004734 /* Evaluate the expression. When the result is a string or a list we can
4735 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004736 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004737 if (tv != NULL)
4738 {
4739 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4740 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4741 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004742 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004743 (eap->cmdidx != CMD_caddexpr
4744 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004745 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004746 && (eap->cmdidx == CMD_cexpr
4747 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004748 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004749 }
4750 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004751 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004752 free_tv(tv);
4753 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004754}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004755#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004756
4757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 * ":helpgrep {pattern}"
4759 */
4760 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004761ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 regmatch_T regmatch;
4764 char_u *save_cpo;
4765 char_u *p;
4766 int fcount;
4767 char_u **fnames;
4768 FILE *fd;
4769 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004771#ifdef FEAT_MULTI_LANG
4772 char_u *lang;
4773#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004774 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004775 int new_qi = FALSE;
4776 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004777#ifdef FEAT_AUTOCMD
4778 char_u *au_name = NULL;
4779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004781#ifdef FEAT_MULTI_LANG
4782 /* Check for a specified language */
4783 lang = check_help_lang(eap->arg);
4784#endif
4785
Bram Moolenaar73633f82012-01-20 13:39:07 +01004786#ifdef FEAT_AUTOCMD
4787 switch (eap->cmdidx)
4788 {
4789 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4790 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4791 default: break;
4792 }
4793 if (au_name != NULL)
4794 {
4795 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4796 curbuf->b_fname, TRUE, curbuf);
4797 if (did_throw || force_abort)
4798 return;
4799 }
4800#endif
4801
4802 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4803 save_cpo = p_cpo;
4804 p_cpo = empty_option;
4805
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004806 if (eap->cmdidx == CMD_lhelpgrep)
4807 {
4808 /* Find an existing help window */
4809 FOR_ALL_WINDOWS(wp)
4810 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4811 break;
4812
4813 if (wp == NULL) /* Help window not found */
4814 qi = NULL;
4815 else
4816 qi = wp->w_llist;
4817
4818 if (qi == NULL)
4819 {
4820 /* Allocate a new location list for help text matches */
4821 if ((qi = ll_new_list()) == NULL)
4822 return;
4823 new_qi = TRUE;
4824 }
4825 }
4826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4828 regmatch.rm_ic = FALSE;
4829 if (regmatch.regprog != NULL)
4830 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004831#ifdef FEAT_MBYTE
4832 vimconv_T vc;
4833
4834 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4835 * differs. */
4836 vc.vc_type = CONV_NONE;
4837 if (!enc_utf8)
4838 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4839#endif
4840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004842 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 /* Go through all directories in 'runtimepath' */
4845 p = p_rtp;
4846 while (*p != NUL && !got_int)
4847 {
4848 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4849
4850 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4851 add_pathsep(NameBuff);
4852 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4853 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4854 &fnames, EW_FILE|EW_SILENT) == OK
4855 && fcount > 0)
4856 {
4857 for (fi = 0; fi < fcount && !got_int; ++fi)
4858 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004859#ifdef FEAT_MULTI_LANG
4860 /* Skip files for a different language. */
4861 if (lang != NULL
4862 && STRNICMP(lang, fnames[fi]
4863 + STRLEN(fnames[fi]) - 3, 2) != 0
4864 && !(STRNICMP(lang, "en", 2) == 0
4865 && STRNICMP("txt", fnames[fi]
4866 + STRLEN(fnames[fi]) - 3, 3) == 0))
4867 continue;
4868#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004869 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (fd != NULL)
4871 {
4872 lnum = 1;
4873 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4874 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004875 char_u *line = IObuff;
4876#ifdef FEAT_MBYTE
4877 /* Convert a line if 'encoding' is not utf-8 and
4878 * the line contains a non-ASCII character. */
4879 if (vc.vc_type != CONV_NONE
4880 && has_non_ascii(IObuff)) {
4881 line = string_convert(&vc, IObuff, NULL);
4882 if (line == NULL)
4883 line = IObuff;
4884 }
4885#endif
4886
4887 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004889 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
4891 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004892 while (l > 0 && line[l - 1] <= ' ')
4893 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004895 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 NULL, /* dir */
4897 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004898 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004899 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004901 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004902 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004903 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004904 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 0, /* nr */
4906 1, /* type */
4907 TRUE /* valid */
4908 ) == FAIL)
4909 {
4910 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004911#ifdef FEAT_MBYTE
4912 if (line != IObuff)
4913 vim_free(line);
4914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 break;
4916 }
4917 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004918#ifdef FEAT_MBYTE
4919 if (line != IObuff)
4920 vim_free(line);
4921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 ++lnum;
4923 line_breakcheck();
4924 }
4925 fclose(fd);
4926 }
4927 }
4928 FreeWild(fcount, fnames);
4929 }
4930 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004931
Bram Moolenaar473de612013-06-08 18:19:48 +02004932 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004933#ifdef FEAT_MBYTE
4934 if (vc.vc_type != CONV_NONE)
4935 convert_setup(&vc, NULL, NULL);
4936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004938 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4939 qi->qf_lists[qi->qf_curlist].qf_ptr =
4940 qi->qf_lists[qi->qf_curlist].qf_start;
4941 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004944 if (p_cpo == empty_option)
4945 p_cpo = save_cpo;
4946 else
4947 /* Darn, some plugin changed the value. */
4948 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004951 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952#endif
4953
Bram Moolenaar73633f82012-01-20 13:39:07 +01004954#ifdef FEAT_AUTOCMD
4955 if (au_name != NULL)
4956 {
4957 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4958 curbuf->b_fname, TRUE, curbuf);
4959 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4960 /* autocommands made "qi" invalid */
4961 return;
4962 }
4963#endif
4964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004966 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004967 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004968 else
4969 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004970
4971 if (eap->cmdidx == CMD_lhelpgrep)
4972 {
4973 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004974 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004975 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4976 {
4977 if (new_qi)
4978 ll_free_all(&qi);
4979 }
4980 else if (curwin->w_llist == NULL)
4981 curwin->w_llist = qi;
4982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983}
4984
4985#endif /* FEAT_QUICKFIX */