blob: 7b07f9358a96a18c0d665e63c8ea7aa3f7d806d5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaar8f77c5a2017-04-30 14:21:00 +020060 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020075
76 int qf_dir_curlist; /* error list for qf_dir_stack */
77 struct dir_stack_T *qf_dir_stack;
78 char_u *qf_directory;
79 struct dir_stack_T *qf_file_stack;
80 char_u *qf_currfile;
81 int qf_multiline;
82 int qf_multiignore;
83 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000084};
85
86static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
Bram Moolenaar68b76a62005-03-25 21:53:48 +000088#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
90/*
91 * Structure used to hold the info of one part of 'errorformat'
92 */
Bram Moolenaar01265852006-03-20 21:50:15 +000093typedef struct efm_S efm_T;
94struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000095{
96 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000097 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
99 char_u prefix; /* prefix of this format line: */
100 /* 'D' enter directory */
101 /* 'X' leave directory */
102 /* 'A' start of multi-line message */
103 /* 'E' error message */
104 /* 'W' warning message */
105 /* 'I' informational message */
106 /* 'C' continuation line */
107 /* 'Z' end of multi-line message */
108 /* 'G' general, unspecific message */
109 /* 'P' push file (partial) message */
110 /* 'Q' pop/quit file (partial) message */
111 /* 'O' overread (partial) message */
112 char_u flags; /* additional flags given in prefix */
113 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000114 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000115 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116};
117
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100118static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
119
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100120static 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, char_u *enc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100121static void qf_store_title(qf_info_T *qi, char_u *title);
122static void qf_new_list(qf_info_T *qi, char_u *qf_title);
123static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200124static 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 +0100125static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static void qf_free(qf_info_T *qi, int idx);
127static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200128static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
129static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200131static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
133static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
136static int is_qf_win(win_T *win, qf_info_T *qi);
137static win_T *qf_find_win(qf_info_T *qi);
138static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200139static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200141static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *get_mef_name(void);
144static void restore_start_dir(char_u *dirname_start);
145static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
146static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
147static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
148static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000150/* Quickfix window check helper macro */
151#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
152/* Location list window check helper macro */
153#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
154/*
155 * Return location list for window 'wp'
156 * For location list window, return the referenced location list
157 */
158#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000161 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200162 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 * Return -1 for error, number of errors for success.
164 */
165 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100166qf_init(
167 win_T *wp,
168 char_u *efile,
169 char_u *errorformat,
170 int newlist, /* TRUE: start a new error list */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100171 char_u *qf_title,
172 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000174 qf_info_T *qi = &ql_info;
175
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000176 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000177 {
178 qi = ll_get_or_alloc_list(wp);
179 if (qi == NULL)
180 return FAIL;
181 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000182
183 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200184 (linenr_T)0, (linenr_T)0,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100185 qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000186}
187
188/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200189 * Maximum number of bytes allowed per line while reading a errorfile.
190 */
191#define LINE_MAXLEN 4096
192
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200193static struct fmtpattern
194{
195 char_u convchar;
196 char *pattern;
197} fmt_pat[FMT_PATTERNS] =
198 {
199 {'f', ".\\+"}, /* only used when at end */
200 {'n', "\\d\\+"},
201 {'l', "\\d\\+"},
202 {'c', "\\d\\+"},
203 {'t', "."},
204 {'m', ".\\+"},
205 {'r', ".*"},
206 {'p', "[- .]*"},
207 {'v', "\\d\\+"},
208 {'s', ".\\+"}
209 };
210
211/*
212 * Converts a 'errorformat' string to regular expression pattern
213 */
214 static int
215efm_to_regpat(
216 char_u *efm,
217 int len,
218 efm_T *fmt_ptr,
219 char_u *regpat,
220 char_u *errmsg)
221{
222 char_u *ptr;
223 char_u *efmp;
224 char_u *srcptr;
225 int round;
226 int idx = 0;
227
228 /*
229 * Build regexp pattern from current 'errorformat' option
230 */
231 ptr = regpat;
232 *ptr++ = '^';
233 round = 0;
234 for (efmp = efm; efmp < efm + len; ++efmp)
235 {
236 if (*efmp == '%')
237 {
238 ++efmp;
239 for (idx = 0; idx < FMT_PATTERNS; ++idx)
240 if (fmt_pat[idx].convchar == *efmp)
241 break;
242 if (idx < FMT_PATTERNS)
243 {
244 if (fmt_ptr->addr[idx])
245 {
246 sprintf((char *)errmsg,
247 _("E372: Too many %%%c in format string"), *efmp);
248 EMSG(errmsg);
249 return -1;
250 }
251 if ((idx
252 && idx < 6
253 && vim_strchr((char_u *)"DXOPQ",
254 fmt_ptr->prefix) != NULL)
255 || (idx == 6
256 && vim_strchr((char_u *)"OPQ",
257 fmt_ptr->prefix) == NULL))
258 {
259 sprintf((char *)errmsg,
260 _("E373: Unexpected %%%c in format string"), *efmp);
261 EMSG(errmsg);
262 return -1;
263 }
264 fmt_ptr->addr[idx] = (char_u)++round;
265 *ptr++ = '\\';
266 *ptr++ = '(';
267#ifdef BACKSLASH_IN_FILENAME
268 if (*efmp == 'f')
269 {
270 /* Also match "c:" in the file name, even when
271 * checking for a colon next: "%f:".
272 * "\%(\a:\)\=" */
273 STRCPY(ptr, "\\%(\\a:\\)\\=");
274 ptr += 10;
275 }
276#endif
277 if (*efmp == 'f' && efmp[1] != NUL)
278 {
279 if (efmp[1] != '\\' && efmp[1] != '%')
280 {
281 /* A file name may contain spaces, but this isn't
282 * in "\f". For "%f:%l:%m" there may be a ":" in
283 * the file name. Use ".\{-1,}x" instead (x is
284 * the next character), the requirement that :999:
285 * follows should work. */
286 STRCPY(ptr, ".\\{-1,}");
287 ptr += 7;
288 }
289 else
290 {
291 /* File name followed by '\\' or '%': include as
292 * many file name chars as possible. */
293 STRCPY(ptr, "\\f\\+");
294 ptr += 4;
295 }
296 }
297 else
298 {
299 srcptr = (char_u *)fmt_pat[idx].pattern;
300 while ((*ptr = *srcptr++) != NUL)
301 ++ptr;
302 }
303 *ptr++ = '\\';
304 *ptr++ = ')';
305 }
306 else if (*efmp == '*')
307 {
308 if (*++efmp == '[' || *efmp == '\\')
309 {
310 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
311 {
312 if (efmp[1] == '^')
313 *ptr++ = *++efmp;
314 if (efmp < efm + len)
315 {
316 *ptr++ = *++efmp; /* could be ']' */
317 while (efmp < efm + len
318 && (*ptr++ = *++efmp) != ']')
319 /* skip */;
320 if (efmp == efm + len)
321 {
322 EMSG(_("E374: Missing ] in format string"));
323 return -1;
324 }
325 }
326 }
327 else if (efmp < efm + len) /* %*\D, %*\s etc. */
328 *ptr++ = *++efmp;
329 *ptr++ = '\\';
330 *ptr++ = '+';
331 }
332 else
333 {
334 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
335 sprintf((char *)errmsg,
336 _("E375: Unsupported %%%c in format string"), *efmp);
337 EMSG(errmsg);
338 return -1;
339 }
340 }
341 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
342 *ptr++ = *efmp; /* regexp magic characters */
343 else if (*efmp == '#')
344 *ptr++ = '*';
345 else if (*efmp == '>')
346 fmt_ptr->conthere = TRUE;
347 else if (efmp == efm + 1) /* analyse prefix */
348 {
349 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
350 fmt_ptr->flags = *efmp++;
351 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
352 fmt_ptr->prefix = *efmp;
353 else
354 {
355 sprintf((char *)errmsg,
356 _("E376: Invalid %%%c in format string prefix"), *efmp);
357 EMSG(errmsg);
358 return -1;
359 }
360 }
361 else
362 {
363 sprintf((char *)errmsg,
364 _("E377: Invalid %%%c in format string"), *efmp);
365 EMSG(errmsg);
366 return -1;
367 }
368 }
369 else /* copy normal character */
370 {
371 if (*efmp == '\\' && efmp + 1 < efm + len)
372 ++efmp;
373 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
374 *ptr++ = '\\'; /* escape regexp atoms */
375 if (*efmp)
376 *ptr++ = *efmp;
377 }
378 }
379 *ptr++ = '$';
380 *ptr = NUL;
381
382 return 0;
383}
384
385 static void
386free_efm_list(efm_T **efm_first)
387{
388 efm_T *efm_ptr;
389
390 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
391 {
392 *efm_first = efm_ptr->next;
393 vim_regfree(efm_ptr->prog);
394 vim_free(efm_ptr);
395 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100396 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200397}
398
399/* Parse 'errorformat' option */
400 static efm_T *
401parse_efm_option(char_u *efm)
402{
403 char_u *errmsg = NULL;
404 int errmsglen;
405 efm_T *fmt_ptr = NULL;
406 efm_T *fmt_first = NULL;
407 efm_T *fmt_last = NULL;
408 char_u *fmtstr = NULL;
409 int len;
410 int i;
411 int round;
412
413 errmsglen = CMDBUFFSIZE + 1;
414 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
415 if (errmsg == NULL)
416 goto parse_efm_end;
417
418 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200419 * Each part of the format string is copied and modified from errorformat
420 * to regex prog. Only a few % characters are allowed.
421 */
422
423 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200424 * Get some space to modify the format string into.
425 */
426 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
427 for (round = FMT_PATTERNS; round > 0; )
428 i += (int)STRLEN(fmt_pat[--round].pattern);
429#ifdef COLON_IN_FILENAME
430 i += 12; /* "%f" can become twelve chars longer */
431#else
432 i += 2; /* "%f" can become two chars longer */
433#endif
434 if ((fmtstr = alloc(i)) == NULL)
435 goto parse_efm_error;
436
437 while (efm[0] != NUL)
438 {
439 /*
440 * Allocate a new eformat structure and put it at the end of the list
441 */
442 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
443 if (fmt_ptr == NULL)
444 goto parse_efm_error;
445 if (fmt_first == NULL) /* first one */
446 fmt_first = fmt_ptr;
447 else
448 fmt_last->next = fmt_ptr;
449 fmt_last = fmt_ptr;
450
451 /*
452 * Isolate one part in the 'errorformat' option
453 */
454 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
455 if (efm[len] == '\\' && efm[len + 1] != NUL)
456 ++len;
457
458 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
459 goto parse_efm_error;
460 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
461 goto parse_efm_error;
462 /*
463 * Advance to next part
464 */
465 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
466 }
467
468 if (fmt_first == NULL) /* nothing found */
469 EMSG(_("E378: 'errorformat' contains no pattern"));
470
471 goto parse_efm_end;
472
473parse_efm_error:
474 free_efm_list(&fmt_first);
475
476parse_efm_end:
477 vim_free(fmtstr);
478 vim_free(errmsg);
479
480 return fmt_first;
481}
482
Bram Moolenaare0d37972016-07-15 22:36:01 +0200483enum {
484 QF_FAIL = 0,
485 QF_OK = 1,
486 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200487 QF_NOMEM = 3,
488 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200489};
490
491typedef struct {
492 char_u *linebuf;
493 int linelen;
494 char_u *growbuf;
495 int growbufsiz;
496 FILE *fd;
497 typval_T *tv;
498 char_u *p_str;
499 listitem_T *p_li;
500 buf_T *buf;
501 linenr_T buflnum;
502 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100503 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200504} qfstate_T;
505
506 static char_u *
507qf_grow_linebuf(qfstate_T *state, int newsz)
508{
509 /*
510 * If the line exceeds LINE_MAXLEN exclude the last
511 * byte since it's not a NL character.
512 */
513 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
514 if (state->growbuf == NULL)
515 {
516 state->growbuf = alloc(state->linelen + 1);
517 if (state->growbuf == NULL)
518 return NULL;
519 state->growbufsiz = state->linelen;
520 }
521 else if (state->linelen > state->growbufsiz)
522 {
523 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
524 if (state->growbuf == NULL)
525 return NULL;
526 state->growbufsiz = state->linelen;
527 }
528 return state->growbuf;
529}
530
531/*
532 * Get the next string (separated by newline) from state->p_str.
533 */
534 static int
535qf_get_next_str_line(qfstate_T *state)
536{
537 /* Get the next line from the supplied string */
538 char_u *p_str = state->p_str;
539 char_u *p;
540 int len;
541
542 if (*p_str == NUL) /* Reached the end of the string */
543 return QF_END_OF_INPUT;
544
545 p = vim_strchr(p_str, '\n');
546 if (p != NULL)
547 len = (int)(p - p_str) + 1;
548 else
549 len = (int)STRLEN(p_str);
550
551 if (len > IOSIZE - 2)
552 {
553 state->linebuf = qf_grow_linebuf(state, len);
554 if (state->linebuf == NULL)
555 return QF_NOMEM;
556 }
557 else
558 {
559 state->linebuf = IObuff;
560 state->linelen = len;
561 }
562 vim_strncpy(state->linebuf, p_str, state->linelen);
563
564 /*
565 * Increment using len in order to discard the rest of the
566 * line if it exceeds LINE_MAXLEN.
567 */
568 p_str += len;
569 state->p_str = p_str;
570
571 return QF_OK;
572}
573
574/*
575 * Get the next string from state->p_Li.
576 */
577 static int
578qf_get_next_list_line(qfstate_T *state)
579{
580 listitem_T *p_li = state->p_li;
581 int len;
582
583 while (p_li != NULL
584 && (p_li->li_tv.v_type != VAR_STRING
585 || p_li->li_tv.vval.v_string == NULL))
586 p_li = p_li->li_next; /* Skip non-string items */
587
588 if (p_li == NULL) /* End of the list */
589 {
590 state->p_li = NULL;
591 return QF_END_OF_INPUT;
592 }
593
594 len = (int)STRLEN(p_li->li_tv.vval.v_string);
595 if (len > IOSIZE - 2)
596 {
597 state->linebuf = qf_grow_linebuf(state, len);
598 if (state->linebuf == NULL)
599 return QF_NOMEM;
600 }
601 else
602 {
603 state->linebuf = IObuff;
604 state->linelen = len;
605 }
606
607 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
608
609 state->p_li = p_li->li_next; /* next item */
610 return QF_OK;
611}
612
613/*
614 * Get the next string from state->buf.
615 */
616 static int
617qf_get_next_buf_line(qfstate_T *state)
618{
619 char_u *p_buf = NULL;
620 int len;
621
622 /* Get the next line from the supplied buffer */
623 if (state->buflnum > state->lnumlast)
624 return QF_END_OF_INPUT;
625
626 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
627 state->buflnum += 1;
628
629 len = (int)STRLEN(p_buf);
630 if (len > IOSIZE - 2)
631 {
632 state->linebuf = qf_grow_linebuf(state, len);
633 if (state->linebuf == NULL)
634 return QF_NOMEM;
635 }
636 else
637 {
638 state->linebuf = IObuff;
639 state->linelen = len;
640 }
641 vim_strncpy(state->linebuf, p_buf, state->linelen);
642
643 return QF_OK;
644}
645
646/*
647 * Get the next string from file state->fd.
648 */
649 static int
650qf_get_next_file_line(qfstate_T *state)
651{
652 int discard;
653 int growbuflen;
654
655 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
656 return QF_END_OF_INPUT;
657
658 discard = FALSE;
659 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200660 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200661 {
662 /*
663 * The current line exceeds IObuff, continue reading using
664 * growbuf until EOL or LINE_MAXLEN bytes is read.
665 */
666 if (state->growbuf == NULL)
667 {
668 state->growbufsiz = 2 * (IOSIZE - 1);
669 state->growbuf = alloc(state->growbufsiz);
670 if (state->growbuf == NULL)
671 return QF_NOMEM;
672 }
673
674 /* Copy the read part of the line, excluding null-terminator */
675 memcpy(state->growbuf, IObuff, IOSIZE - 1);
676 growbuflen = state->linelen;
677
678 for (;;)
679 {
680 if (fgets((char *)state->growbuf + growbuflen,
681 state->growbufsiz - growbuflen, state->fd) == NULL)
682 break;
683 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
684 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200685 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200686 break;
687 if (state->growbufsiz == LINE_MAXLEN)
688 {
689 discard = TRUE;
690 break;
691 }
692
693 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
694 ? 2 * state->growbufsiz : LINE_MAXLEN;
695 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
696 if (state->growbuf == NULL)
697 return QF_NOMEM;
698 }
699
700 while (discard)
701 {
702 /*
703 * The current line is longer than LINE_MAXLEN, continue
704 * reading but discard everything until EOL or EOF is
705 * reached.
706 */
707 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
708 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200709 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200710 break;
711 }
712
713 state->linebuf = state->growbuf;
714 state->linelen = growbuflen;
715 }
716 else
717 state->linebuf = IObuff;
718
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100719#ifdef FEAT_MBYTE
720 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200721 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
722 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100723 char_u *line;
724
725 line = string_convert(&state->vc, state->linebuf, &state->linelen);
726 if (line != NULL)
727 {
728 if (state->linelen < IOSIZE)
729 {
730 STRCPY(state->linebuf, line);
731 vim_free(line);
732 }
733 else
734 {
735 vim_free(state->growbuf);
736 state->linebuf = state->growbuf = line;
737 state->growbufsiz = state->linelen < LINE_MAXLEN
738 ? state->linelen : LINE_MAXLEN;
739 }
740 }
741 }
742#endif
743
Bram Moolenaare0d37972016-07-15 22:36:01 +0200744 return QF_OK;
745}
746
747/*
748 * Get the next string from a file/buffer/list/string.
749 */
750 static int
751qf_get_nextline(qfstate_T *state)
752{
753 int status = QF_FAIL;
754
755 if (state->fd == NULL)
756 {
757 if (state->tv != NULL)
758 {
759 if (state->tv->v_type == VAR_STRING)
760 /* Get the next line from the supplied string */
761 status = qf_get_next_str_line(state);
762 else if (state->tv->v_type == VAR_LIST)
763 /* Get the next line from the supplied list */
764 status = qf_get_next_list_line(state);
765 }
766 else
767 /* Get the next line from the supplied buffer */
768 status = qf_get_next_buf_line(state);
769 }
770 else
771 /* Get the next line from the supplied file */
772 status = qf_get_next_file_line(state);
773
774 if (status != QF_OK)
775 return status;
776
777 /* remove newline/CR from the line */
778 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200779 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200780 state->linebuf[state->linelen - 1] = NUL;
781#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200782 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
783 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200784#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200785 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786
787#ifdef FEAT_MBYTE
788 remove_bom(state->linebuf);
789#endif
790
791 return QF_OK;
792}
793
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200794typedef struct {
795 char_u *namebuf;
796 char_u *errmsg;
797 int errmsglen;
798 long lnum;
799 int col;
800 char_u use_viscol;
801 char_u *pattern;
802 int enr;
803 int type;
804 int valid;
805} qffields_T;
806
807/*
808 * Parse a line and get the quickfix fields.
809 * Return the QF_ status.
810 */
811 static int
812qf_parse_line(
813 qf_info_T *qi,
814 char_u *linebuf,
815 int linelen,
816 efm_T *fmt_first,
817 qffields_T *fields)
818{
819 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200820 char_u *ptr;
821 int len;
822 int i;
823 int idx = 0;
824 char_u *tail = NULL;
825 regmatch_T regmatch;
826
827 /* Always ignore case when looking for a matching error. */
828 regmatch.rm_ic = TRUE;
829
830 /* If there was no %> item start at the first pattern */
831 if (fmt_start == NULL)
832 fmt_ptr = fmt_first;
833 else
834 {
835 fmt_ptr = fmt_start;
836 fmt_start = NULL;
837 }
838
839 /*
840 * Try to match each part of 'errorformat' until we find a complete
841 * match or no match.
842 */
843 fields->valid = TRUE;
844restofline:
845 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
846 {
847 int r;
848
849 idx = fmt_ptr->prefix;
850 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
851 continue;
852 fields->namebuf[0] = NUL;
853 fields->pattern[0] = NUL;
854 if (!qi->qf_multiscan)
855 fields->errmsg[0] = NUL;
856 fields->lnum = 0;
857 fields->col = 0;
858 fields->use_viscol = FALSE;
859 fields->enr = -1;
860 fields->type = 0;
861 tail = NULL;
862
863 regmatch.regprog = fmt_ptr->prog;
864 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
865 fmt_ptr->prog = regmatch.regprog;
866 if (r)
867 {
868 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
869 continue;
870 if (vim_strchr((char_u *)"EWI", idx) != NULL)
871 fields->type = idx;
872 else
873 fields->type = 0;
874 /*
875 * Extract error message data from matched line.
876 * We check for an actual submatch, because "\[" and "\]" in
877 * the 'errorformat' may cause the wrong submatch to be used.
878 */
879 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
880 {
881 int c;
882
883 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
884 continue;
885
886 /* Expand ~/file and $HOME/file to full path. */
887 c = *regmatch.endp[i];
888 *regmatch.endp[i] = NUL;
889 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
890 *regmatch.endp[i] = c;
891
892 if (vim_strchr((char_u *)"OPQ", idx) != NULL
893 && mch_getperm(fields->namebuf) == -1)
894 continue;
895 }
896 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
897 {
898 if (regmatch.startp[i] == NULL)
899 continue;
900 fields->enr = (int)atol((char *)regmatch.startp[i]);
901 }
902 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
903 {
904 if (regmatch.startp[i] == NULL)
905 continue;
906 fields->lnum = atol((char *)regmatch.startp[i]);
907 }
908 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
909 {
910 if (regmatch.startp[i] == NULL)
911 continue;
912 fields->col = (int)atol((char *)regmatch.startp[i]);
913 }
914 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
915 {
916 if (regmatch.startp[i] == NULL)
917 continue;
918 fields->type = *regmatch.startp[i];
919 }
920 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
921 {
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200922 if (linelen > fields->errmsglen)
923 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200924 /* linelen + null terminator */
925 if ((fields->errmsg = vim_realloc(fields->errmsg,
926 linelen + 1)) == NULL)
927 return QF_NOMEM;
928 fields->errmsglen = linelen + 1;
929 }
930 vim_strncpy(fields->errmsg, linebuf, linelen);
931 }
932 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
933 {
934 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
935 continue;
936 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200937 if (len > fields->errmsglen)
938 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200939 /* len + null terminator */
940 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
941 == NULL)
942 return QF_NOMEM;
943 fields->errmsglen = len + 1;
944 }
945 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
946 }
947 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
948 {
949 if (regmatch.startp[i] == NULL)
950 continue;
951 tail = regmatch.startp[i];
952 }
953 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
954 {
955 char_u *match_ptr;
956
957 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
958 continue;
959 fields->col = 0;
960 for (match_ptr = regmatch.startp[i];
961 match_ptr != regmatch.endp[i]; ++match_ptr)
962 {
963 ++fields->col;
964 if (*match_ptr == TAB)
965 {
966 fields->col += 7;
967 fields->col -= fields->col % 8;
968 }
969 }
970 ++fields->col;
971 fields->use_viscol = TRUE;
972 }
973 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
974 {
975 if (regmatch.startp[i] == NULL)
976 continue;
977 fields->col = (int)atol((char *)regmatch.startp[i]);
978 fields->use_viscol = TRUE;
979 }
980 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
981 {
982 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
983 continue;
984 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
985 if (len > CMDBUFFSIZE - 5)
986 len = CMDBUFFSIZE - 5;
987 STRCPY(fields->pattern, "^\\V");
988 STRNCAT(fields->pattern, regmatch.startp[i], len);
989 fields->pattern[len + 3] = '\\';
990 fields->pattern[len + 4] = '$';
991 fields->pattern[len + 5] = NUL;
992 }
993 break;
994 }
995 }
996 qi->qf_multiscan = FALSE;
997
998 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
999 {
1000 if (fmt_ptr != NULL)
1001 {
1002 if (idx == 'D') /* enter directory */
1003 {
1004 if (*fields->namebuf == NUL)
1005 {
1006 EMSG(_("E379: Missing or empty directory name"));
1007 return QF_FAIL;
1008 }
1009 qi->qf_directory =
1010 qf_push_dir(fields->namebuf, &qi->qf_dir_stack, FALSE);
1011 if (qi->qf_directory == NULL)
1012 return QF_FAIL;
1013 }
1014 else if (idx == 'X') /* leave directory */
1015 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
1016 }
1017 fields->namebuf[0] = NUL; /* no match found, remove file name */
1018 fields->lnum = 0; /* don't jump to this line */
1019 fields->valid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001020 if (linelen > fields->errmsglen)
1021 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001022 /* linelen + null terminator */
1023 if ((fields->errmsg = vim_realloc(fields->errmsg,
1024 linelen + 1)) == NULL)
1025 return QF_NOMEM;
1026 fields->errmsglen = linelen + 1;
1027 }
1028 /* copy whole line to error message */
1029 vim_strncpy(fields->errmsg, linebuf, linelen);
1030 if (fmt_ptr == NULL)
1031 qi->qf_multiline = qi->qf_multiignore = FALSE;
1032 }
1033 else if (fmt_ptr != NULL)
1034 {
1035 /* honor %> item */
1036 if (fmt_ptr->conthere)
1037 fmt_start = fmt_ptr;
1038
1039 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1040 {
1041 qi->qf_multiline = TRUE; /* start of a multi-line message */
1042 qi->qf_multiignore = FALSE; /* reset continuation */
1043 }
1044 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1045 { /* continuation of multi-line msg */
Bram Moolenaar9b457942016-10-09 16:10:05 +02001046 if (!qi->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001047 {
Bram Moolenaar9b457942016-10-09 16:10:05 +02001048 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001049
Bram Moolenaar9b457942016-10-09 16:10:05 +02001050 if (qfprev == NULL)
1051 return QF_FAIL;
1052 if (*fields->errmsg && !qi->qf_multiignore)
1053 {
1054 len = (int)STRLEN(qfprev->qf_text);
1055 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1056 == NULL)
1057 return QF_FAIL;
1058 STRCPY(ptr, qfprev->qf_text);
1059 vim_free(qfprev->qf_text);
1060 qfprev->qf_text = ptr;
1061 *(ptr += len) = '\n';
1062 STRCPY(++ptr, fields->errmsg);
1063 }
1064 if (qfprev->qf_nr == -1)
1065 qfprev->qf_nr = fields->enr;
1066 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1067 /* only printable chars allowed */
1068 qfprev->qf_type = fields->type;
1069
1070 if (!qfprev->qf_lnum)
1071 qfprev->qf_lnum = fields->lnum;
1072 if (!qfprev->qf_col)
1073 qfprev->qf_col = fields->col;
1074 qfprev->qf_viscol = fields->use_viscol;
1075 if (!qfprev->qf_fnum)
1076 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1077 *fields->namebuf || qi->qf_directory != NULL
1078 ? fields->namebuf
1079 : qi->qf_currfile != NULL && fields->valid
1080 ? qi->qf_currfile : 0);
1081 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001082 if (idx == 'Z')
1083 qi->qf_multiline = qi->qf_multiignore = FALSE;
1084 line_breakcheck();
1085 return QF_IGNORE_LINE;
1086 }
1087 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1088 {
1089 /* global file names */
1090 fields->valid = FALSE;
1091 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1092 {
1093 if (*fields->namebuf && idx == 'P')
1094 qi->qf_currfile =
1095 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1096 else if (idx == 'Q')
1097 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1098 *fields->namebuf = NUL;
1099 if (tail && *tail)
1100 {
1101 STRMOVE(IObuff, skipwhite(tail));
1102 qi->qf_multiscan = TRUE;
1103 goto restofline;
1104 }
1105 }
1106 }
1107 if (fmt_ptr->flags == '-') /* generally exclude this line */
1108 {
1109 if (qi->qf_multiline)
1110 /* also exclude continuation lines */
1111 qi->qf_multiignore = TRUE;
1112 return QF_IGNORE_LINE;
1113 }
1114 }
1115
1116 return QF_OK;
1117}
1118
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001119/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001120 * Read the errorfile "efile" into memory, line by line, building the error
1121 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001122 * Alternative: when "efile" is NULL read errors from buffer "buf".
1123 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001124 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001125 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1126 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001127 * Return -1 for error, number of errors for success.
1128 */
1129 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001130qf_init_ext(
1131 qf_info_T *qi,
1132 char_u *efile,
1133 buf_T *buf,
1134 typval_T *tv,
1135 char_u *errorformat,
1136 int newlist, /* TRUE: start a new error list */
1137 linenr_T lnumfirst, /* first line number to use */
1138 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001139 char_u *qf_title,
1140 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001141{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001142 qfstate_T state;
1143 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001144#ifdef FEAT_WINDOWS
1145 qfline_T *old_last = NULL;
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001146 int adding = FALSE;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001147#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001148 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001150 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001152 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001154 vim_memset(&state, 0, sizeof(state));
1155 vim_memset(&fields, 0, sizeof(fields));
1156#ifdef FEAT_MBYTE
1157 state.vc.vc_type = CONV_NONE;
1158 if (enc != NULL && *enc != NUL)
1159 convert_setup(&state.vc, enc, p_enc);
1160#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001161 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1162 fields.errmsglen = CMDBUFFSIZE + 1;
1163 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1164 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1165 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1166 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 goto qf_init_end;
1168
Bram Moolenaare0d37972016-07-15 22:36:01 +02001169 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
1171 EMSG2(_(e_openerrf), efile);
1172 goto qf_init_end;
1173 }
1174
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001175 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001177 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001178#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001179 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001180 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001181 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001182 adding = TRUE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001183 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001184 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001188 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001189 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 else
1191 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001193 /*
1194 * If we are not adding or adding to another list: clear the state.
1195 */
1196 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1197 {
1198 qi->qf_dir_curlist = qi->qf_curlist;
1199 qf_clean_dir_stack(&qi->qf_dir_stack);
1200 qi->qf_directory = NULL;
1201 qf_clean_dir_stack(&qi->qf_file_stack);
1202 qi->qf_currfile = NULL;
1203 qi->qf_multiline = FALSE;
1204 qi->qf_multiignore = FALSE;
1205 qi->qf_multiscan = FALSE;
1206 }
1207
1208 /*
1209 * If the errorformat didn't change between calls, then reuse the
1210 * previously parsed values.
1211 */
1212 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1213 {
1214 /* free the previously parsed data */
1215 vim_free(last_efm);
1216 last_efm = NULL;
1217 free_efm_list(&fmt_first);
1218
1219 /* parse the current 'efm' */
1220 fmt_first = parse_efm_option(efm);
1221 if (fmt_first != NULL)
1222 last_efm = vim_strsave(efm);
1223 }
1224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
1228 /*
1229 * got_int is reset here, because it was probably set when killing the
1230 * ":make" command, but we still want to read the errorfile then.
1231 */
1232 got_int = FALSE;
1233
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001234 if (tv != NULL)
1235 {
1236 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001237 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001238 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001239 state.p_li = tv->vval.v_list->lv_first;
1240 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001241 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001242 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001243 state.buflnum = lnumfirst;
1244 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 /*
1247 * Read the lines in the error file one by one.
1248 * Try to recognize one of the error formats in each line.
1249 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001250 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001252 /* Get the next line from a file/buffer/list/string */
1253 status = qf_get_nextline(&state);
1254 if (status == QF_NOMEM) /* memory alloc failure */
1255 goto qf_init_end;
1256 if (status == QF_END_OF_INPUT) /* end of input */
1257 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001259 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1260 &fields);
1261 if (status == QF_FAIL)
1262 goto error2;
1263 if (status == QF_NOMEM)
1264 goto qf_init_end;
1265 if (status == QF_IGNORE_LINE)
1266 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001268 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001269 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001270 (*fields.namebuf || qi->qf_directory != NULL)
1271 ? fields.namebuf
1272 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001273 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001274 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001275 fields.errmsg,
1276 fields.lnum,
1277 fields.col,
1278 fields.use_viscol,
1279 fields.pattern,
1280 fields.enr,
1281 fields.type,
1282 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 goto error2;
1284 line_breakcheck();
1285 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001286 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001288 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001290 /* no valid entry found */
1291 qi->qf_lists[qi->qf_curlist].qf_ptr =
1292 qi->qf_lists[qi->qf_curlist].qf_start;
1293 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1294 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296 else
1297 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001298 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1299 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1300 qi->qf_lists[qi->qf_curlist].qf_ptr =
1301 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001303 /* return number of matches */
1304 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001305 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 EMSG(_(e_readerrf));
1308error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001309 if (!adding)
1310 {
1311 qf_free(qi, qi->qf_curlist);
1312 qi->qf_listcount--;
1313 if (qi->qf_curlist > 0)
1314 --qi->qf_curlist;
1315 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001316qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001317 if (state.fd != NULL)
1318 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001319 vim_free(fields.namebuf);
1320 vim_free(fields.errmsg);
1321 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001322 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001325 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001327#ifdef FEAT_MBYTE
1328 if (state.vc.vc_type != CONV_NONE)
1329 convert_setup(&state.vc, NULL, NULL);
1330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
1332 return retval;
1333}
1334
Bram Moolenaarfb604092014-07-23 15:55:00 +02001335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001336qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001337{
1338 if (title != NULL)
1339 {
1340 char_u *p = alloc((int)STRLEN(title) + 2);
1341
1342 qi->qf_lists[qi->qf_curlist].qf_title = p;
1343 if (p != NULL)
1344 sprintf((char *)p, ":%s", (char *)title);
1345 }
1346}
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348/*
1349 * Prepare for adding a new quickfix list.
1350 */
1351 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001352qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 int i;
1355
1356 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001357 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 * the current entry. This makes it possible to browse in a tree-like
1359 * way with ":grep'.
1360 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001361 while (qi->qf_listcount > qi->qf_curlist + 1)
1362 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
1364 /*
1365 * When the stack is full, remove to oldest entry
1366 * Otherwise, add a new entry.
1367 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001368 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001370 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001372 qi->qf_lists[i - 1] = qi->qf_lists[i];
1373 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001376 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001377 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001378 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379}
1380
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001381/*
1382 * Free a location list
1383 */
1384 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001385ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001386{
1387 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001389
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001390 qi = *pqi;
1391 if (qi == NULL)
1392 return;
1393 *pqi = NULL; /* Remove reference to this list */
1394
1395 qi->qf_refcount--;
1396 if (qi->qf_refcount < 1)
1397 {
1398 /* No references to this location list */
1399 for (i = 0; i < qi->qf_listcount; ++i)
1400 qf_free(qi, i);
1401 vim_free(qi);
1402 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001403}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001404
1405 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001406qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407{
1408 int i;
1409 qf_info_T *qi = &ql_info;
1410
1411 if (wp != NULL)
1412 {
1413 /* location list */
1414 ll_free_all(&wp->w_llist);
1415 ll_free_all(&wp->w_llist_ref);
1416 }
1417 else
1418 /* quickfix list */
1419 for (i = 0; i < qi->qf_listcount; ++i)
1420 qf_free(qi, i);
1421}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423/*
1424 * Add an entry to the end of the list of errors.
1425 * Returns OK or FAIL.
1426 */
1427 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001428qf_add_entry(
1429 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001430 char_u *dir, /* optional directory name */
1431 char_u *fname, /* file name or NULL */
1432 int bufnum, /* buffer number or zero */
1433 char_u *mesg, /* message */
1434 long lnum, /* line number */
1435 int col, /* column */
1436 int vis_col, /* using visual column */
1437 char_u *pattern, /* search pattern */
1438 int nr, /* error number */
1439 int type, /* type character */
1440 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001442 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001443 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001445 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001447 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001448 {
1449 buf_T *buf = buflist_findnr(bufnum);
1450
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001451 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001452 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001453 buf->b_has_qf_entry |=
1454 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001455 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001456 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001457 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1459 {
1460 vim_free(qfp);
1461 return FAIL;
1462 }
1463 qfp->qf_lnum = lnum;
1464 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001465 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001466 if (pattern == NULL || *pattern == NUL)
1467 qfp->qf_pattern = NULL;
1468 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1469 {
1470 vim_free(qfp->qf_text);
1471 vim_free(qfp);
1472 return FAIL;
1473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 qfp->qf_nr = nr;
1475 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1476 type = 0;
1477 qfp->qf_type = type;
1478 qfp->qf_valid = valid;
1479
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001480 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001481 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1482 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001484 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001485 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1486 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001487 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
1489 else
1490 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001491 qfp->qf_prev = *lastp;
1492 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001494 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001496 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001497 ++qi->qf_lists[qi->qf_curlist].qf_count;
1498 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1499 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001501 qi->qf_lists[qi->qf_curlist].qf_index =
1502 qi->qf_lists[qi->qf_curlist].qf_count;
1503 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505
1506 return OK;
1507}
1508
1509/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001510 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001511 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001512 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001513ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001514{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001515 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001516
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001517 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1518 if (qi != NULL)
1519 {
1520 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1521 qi->qf_refcount++;
1522 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001523
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001524 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001525}
1526
1527/*
1528 * Return the location list for window 'wp'.
1529 * If not present, allocate a location list
1530 */
1531 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001532ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001533{
1534 if (IS_LL_WINDOW(wp))
1535 /* For a location list window, use the referenced location list */
1536 return wp->w_llist_ref;
1537
1538 /*
1539 * For a non-location list window, w_llist_ref should not point to a
1540 * location list.
1541 */
1542 ll_free_all(&wp->w_llist_ref);
1543
1544 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001545 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546 return wp->w_llist;
1547}
1548
1549/*
1550 * Copy the location list from window "from" to window "to".
1551 */
1552 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001553copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001554{
1555 qf_info_T *qi;
1556 int idx;
1557 int i;
1558
1559 /*
1560 * When copying from a location list window, copy the referenced
1561 * location list. For other windows, copy the location list for
1562 * that window.
1563 */
1564 if (IS_LL_WINDOW(from))
1565 qi = from->w_llist_ref;
1566 else
1567 qi = from->w_llist;
1568
1569 if (qi == NULL) /* no location list to copy */
1570 return;
1571
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001572 /* allocate a new location list */
1573 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001574 return;
1575
1576 to->w_llist->qf_listcount = qi->qf_listcount;
1577
1578 /* Copy the location lists one at a time */
1579 for (idx = 0; idx < qi->qf_listcount; idx++)
1580 {
1581 qf_list_T *from_qfl;
1582 qf_list_T *to_qfl;
1583
1584 to->w_llist->qf_curlist = idx;
1585
1586 from_qfl = &qi->qf_lists[idx];
1587 to_qfl = &to->w_llist->qf_lists[idx];
1588
1589 /* Some of the fields are populated by qf_add_entry() */
1590 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1591 to_qfl->qf_count = 0;
1592 to_qfl->qf_index = 0;
1593 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001594 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001595 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001596 if (from_qfl->qf_title != NULL)
1597 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1598 else
1599 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001600 if (from_qfl->qf_ctx != NULL)
1601 {
1602 to_qfl->qf_ctx = alloc_tv();
1603 if (to_qfl->qf_ctx != NULL)
1604 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1605 }
1606 else
1607 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001608
1609 if (from_qfl->qf_count)
1610 {
1611 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001612 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001613
1614 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001615 for (i = 0, from_qfp = from_qfl->qf_start;
1616 i < from_qfl->qf_count && from_qfp != NULL;
1617 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001618 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001619 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001620 NULL,
1621 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001622 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001623 from_qfp->qf_text,
1624 from_qfp->qf_lnum,
1625 from_qfp->qf_col,
1626 from_qfp->qf_viscol,
1627 from_qfp->qf_pattern,
1628 from_qfp->qf_nr,
1629 0,
1630 from_qfp->qf_valid) == FAIL)
1631 {
1632 qf_free_all(to);
1633 return;
1634 }
1635 /*
1636 * qf_add_entry() will not set the qf_num field, as the
1637 * directory and file names are not supplied. So the qf_fnum
1638 * field is copied here.
1639 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001640 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001641 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1642 prevp->qf_type = from_qfp->qf_type; /* error type */
1643 if (from_qfl->qf_ptr == from_qfp)
1644 to_qfl->qf_ptr = prevp; /* current location */
1645 }
1646 }
1647
1648 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1649
1650 /* When no valid entries are present in the list, qf_ptr points to
1651 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001652 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001653 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001654 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001655 to_qfl->qf_index = 1;
1656 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001657 }
1658
1659 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1660}
1661
1662/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001663 * Looking up a buffer can be slow if there are many. Remember the last one
1664 * to make this a lot faster if there are multiple matches in the same file.
1665 */
1666static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001667static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001668
1669/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001670 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001671 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 */
1673 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001674qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
Bram Moolenaar82404332016-07-10 17:00:38 +02001676 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001677 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001678 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001679
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (fname == NULL || *fname == NUL) /* no file name */
1681 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaare60acc12011-05-10 16:41:25 +02001683#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001684 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001685#endif
1686#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001687 if (directory != NULL)
1688 slash_adjust(directory);
1689 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001690#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001691 if (directory != NULL && !vim_isAbsName(fname)
1692 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1693 {
1694 /*
1695 * Here we check if the file really exists.
1696 * This should normally be true, but if make works without
1697 * "leaving directory"-messages we might have missed a
1698 * directory change.
1699 */
1700 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001703 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001704 if (directory)
1705 ptr = concat_fnames(directory, fname, TRUE);
1706 else
1707 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001709 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001710 bufname = ptr;
1711 }
1712 else
1713 bufname = fname;
1714
1715 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001716 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001717 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001718 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001719 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001721 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001722 {
1723 vim_free(qf_last_bufname);
1724 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1725 if (bufname == ptr)
1726 qf_last_bufname = bufname;
1727 else
1728 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001729 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001730 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001731 if (buf == NULL)
1732 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001733
Bram Moolenaarc1542742016-07-20 21:44:37 +02001734 buf->b_has_qf_entry =
1735 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001736 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737}
1738
1739/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001740 * Push dirbuf onto the directory stack and return pointer to actual dir or
1741 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 */
1743 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001744qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 struct dir_stack_T *ds_new;
1747 struct dir_stack_T *ds_ptr;
1748
1749 /* allocate new stack element and hook it in */
1750 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1751 if (ds_new == NULL)
1752 return NULL;
1753
1754 ds_new->next = *stackptr;
1755 *stackptr = ds_new;
1756
1757 /* store directory on the stack */
1758 if (vim_isAbsName(dirbuf)
1759 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001760 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 (*stackptr)->dirname = vim_strsave(dirbuf);
1762 else
1763 {
1764 /* Okay we don't have an absolute path.
1765 * dirbuf must be a subdir of one of the directories on the stack.
1766 * Let's search...
1767 */
1768 ds_new = (*stackptr)->next;
1769 (*stackptr)->dirname = NULL;
1770 while (ds_new)
1771 {
1772 vim_free((*stackptr)->dirname);
1773 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1774 TRUE);
1775 if (mch_isdir((*stackptr)->dirname) == TRUE)
1776 break;
1777
1778 ds_new = ds_new->next;
1779 }
1780
1781 /* clean up all dirs we already left */
1782 while ((*stackptr)->next != ds_new)
1783 {
1784 ds_ptr = (*stackptr)->next;
1785 (*stackptr)->next = (*stackptr)->next->next;
1786 vim_free(ds_ptr->dirname);
1787 vim_free(ds_ptr);
1788 }
1789
1790 /* Nothing found -> it must be on top level */
1791 if (ds_new == NULL)
1792 {
1793 vim_free((*stackptr)->dirname);
1794 (*stackptr)->dirname = vim_strsave(dirbuf);
1795 }
1796 }
1797
1798 if ((*stackptr)->dirname != NULL)
1799 return (*stackptr)->dirname;
1800 else
1801 {
1802 ds_ptr = *stackptr;
1803 *stackptr = (*stackptr)->next;
1804 vim_free(ds_ptr);
1805 return NULL;
1806 }
1807}
1808
1809
1810/*
1811 * pop dirbuf from the directory stack and return previous directory or NULL if
1812 * stack is empty
1813 */
1814 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001815qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 struct dir_stack_T *ds_ptr;
1818
1819 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1820 * What to do if it isn't? */
1821
1822 /* pop top element and free it */
1823 if (*stackptr != NULL)
1824 {
1825 ds_ptr = *stackptr;
1826 *stackptr = (*stackptr)->next;
1827 vim_free(ds_ptr->dirname);
1828 vim_free(ds_ptr);
1829 }
1830
1831 /* return NEW top element as current dir or NULL if stack is empty*/
1832 return *stackptr ? (*stackptr)->dirname : NULL;
1833}
1834
1835/*
1836 * clean up directory stack
1837 */
1838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001839qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 struct dir_stack_T *ds_ptr;
1842
1843 while ((ds_ptr = *stackptr) != NULL)
1844 {
1845 *stackptr = (*stackptr)->next;
1846 vim_free(ds_ptr->dirname);
1847 vim_free(ds_ptr);
1848 }
1849}
1850
1851/*
1852 * Check in which directory of the directory stack the given file can be
1853 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001854 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 * Cleans up intermediate directory entries.
1856 *
1857 * TODO: How to solve the following problem?
1858 * If we have the this directory tree:
1859 * ./
1860 * ./aa
1861 * ./aa/bb
1862 * ./bb
1863 * ./bb/x.c
1864 * and make says:
1865 * making all in aa
1866 * making all in bb
1867 * x.c:9: Error
1868 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1869 * qf_guess_filepath will return NULL.
1870 */
1871 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001872qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873{
1874 struct dir_stack_T *ds_ptr;
1875 struct dir_stack_T *ds_tmp;
1876 char_u *fullname;
1877
1878 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001879 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 return NULL;
1881
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001882 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 fullname = NULL;
1884 while (ds_ptr)
1885 {
1886 vim_free(fullname);
1887 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1888
1889 /* If concat_fnames failed, just go on. The worst thing that can happen
1890 * is that we delete the entire stack.
1891 */
1892 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1893 break;
1894
1895 ds_ptr = ds_ptr->next;
1896 }
1897
1898 vim_free(fullname);
1899
1900 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001901 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001903 ds_tmp = qi->qf_dir_stack->next;
1904 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 vim_free(ds_tmp->dirname);
1906 vim_free(ds_tmp);
1907 }
1908
1909 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910}
1911
1912/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001913 * When loading a file from the quickfix, the auto commands may modify it.
1914 * This may invalidate the current quickfix entry. This function checks
1915 * whether a entry is still present in the quickfix.
1916 * Similar to location list.
1917 */
1918 static int
1919is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1920{
1921 qf_list_T *qfl;
1922 qfline_T *qfp;
1923 int i;
1924
1925 qfl = &qi->qf_lists[qi->qf_curlist];
1926
1927 /* Search for the entry in the current list */
1928 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1929 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001930 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001931 break;
1932
1933 if (i == qfl->qf_count) /* Entry is not found */
1934 return FALSE;
1935
1936 return TRUE;
1937}
1938
1939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 * jump to a quickfix line
1941 * if dir == FORWARD go "errornr" valid entries forward
1942 * if dir == BACKWARD go "errornr" valid entries backward
1943 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1944 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1945 * else if "errornr" is zero, redisplay the same line
1946 * else go to entry "errornr"
1947 */
1948 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001949qf_jump(
1950 qf_info_T *qi,
1951 int dir,
1952 int errornr,
1953 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001956 qfline_T *qf_ptr;
1957 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 int qf_index;
1959 int old_qf_fnum;
1960 int old_qf_index;
1961 int prev_index;
1962 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1963 char_u *err = e_no_more_items;
1964 linenr_T i;
1965 buf_T *old_curbuf;
1966 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 colnr_T screen_col;
1968 colnr_T char_col;
1969 char_u *line;
1970#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001971 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001972 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 int opened_window = FALSE;
1974 win_T *win;
1975 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001976 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001978 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 int print_message = TRUE;
1980 int len;
1981#ifdef FEAT_FOLDING
1982 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1983#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001984 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001985 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001987 if (qi == NULL)
1988 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001989
1990 if (qi->qf_curlist >= qi->qf_listcount
1991 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 EMSG(_(e_quickfix));
1994 return;
1995 }
1996
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001997 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001999 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 old_qf_index = qf_index;
2001 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
2002 {
2003 while (errornr--)
2004 {
2005 old_qf_ptr = qf_ptr;
2006 prev_index = qf_index;
2007 old_qf_fnum = qf_ptr->qf_fnum;
2008 do
2009 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002010 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 || qf_ptr->qf_next == NULL)
2012 {
2013 qf_ptr = old_qf_ptr;
2014 qf_index = prev_index;
2015 if (err != NULL)
2016 {
2017 EMSG(_(err));
2018 goto theend;
2019 }
2020 errornr = 0;
2021 break;
2022 }
2023 ++qf_index;
2024 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2026 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2028 err = NULL;
2029 }
2030 }
2031 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
2032 {
2033 while (errornr--)
2034 {
2035 old_qf_ptr = qf_ptr;
2036 prev_index = qf_index;
2037 old_qf_fnum = qf_ptr->qf_fnum;
2038 do
2039 {
2040 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
2041 {
2042 qf_ptr = old_qf_ptr;
2043 qf_index = prev_index;
2044 if (err != NULL)
2045 {
2046 EMSG(_(err));
2047 goto theend;
2048 }
2049 errornr = 0;
2050 break;
2051 }
2052 --qf_index;
2053 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002054 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2055 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2057 err = NULL;
2058 }
2059 }
2060 else if (errornr != 0) /* go to specified number */
2061 {
2062 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2063 {
2064 --qf_index;
2065 qf_ptr = qf_ptr->qf_prev;
2066 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002067 while (errornr > qf_index && qf_index <
2068 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 && qf_ptr->qf_next != NULL)
2070 {
2071 ++qf_index;
2072 qf_ptr = qf_ptr->qf_next;
2073 }
2074 }
2075
2076#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2078 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 /* No need to print the error message if it's visible in the error
2080 * window */
2081 print_message = FALSE;
2082
2083 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002084 * For ":helpgrep" find a help window or open one.
2085 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002086 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002087 {
2088 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002089
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002090 if (cmdmod.tab != 0)
2091 wp = NULL;
2092 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002093 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002094 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2095 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002096 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2097 win_enter(wp, TRUE);
2098 else
2099 {
2100 /*
2101 * Split off help window; put it at far top if no position
2102 * specified, the current window is vertically split and narrow.
2103 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002104 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002105 if (cmdmod.split == 0 && curwin->w_width != Columns
2106 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002107 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002108 if (qi != &ql_info)
2109 flags |= WSP_NEWLOC; /* don't copy the location list */
2110
2111 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002112 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002113 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002114
2115 if (curwin->w_height < p_hh)
2116 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002117
2118 if (qi != &ql_info) /* not a quickfix list */
2119 {
2120 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002121 curwin->w_llist = qi;
2122 qi->qf_refcount++;
2123 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002124 }
2125
2126 if (!p_im)
2127 restart_edit = 0; /* don't want insert mode in help file */
2128 }
2129
2130 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 * If currently in the quickfix window, find another window to show the
2132 * file in.
2133 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002134 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002136 win_T *usable_win_ptr = NULL;
2137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 /*
2139 * If there is no file specified, we don't know where to go.
2140 * But do advance, otherwise ":cn" gets stuck.
2141 */
2142 if (qf_ptr->qf_fnum == 0)
2143 goto theend;
2144
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002145 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002146
2147 ll_ref = curwin->w_llist_ref;
2148 if (ll_ref != NULL)
2149 {
2150 /* Find a window using the same location list that is not a
2151 * quickfix window. */
2152 FOR_ALL_WINDOWS(usable_win_ptr)
2153 if (usable_win_ptr->w_llist == ll_ref
2154 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002155 {
2156 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002157 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002158 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002159 }
2160
2161 if (!usable_win)
2162 {
2163 /* Locate a window showing a normal buffer */
2164 FOR_ALL_WINDOWS(win)
2165 if (win->w_buffer->b_p_bt[0] == NUL)
2166 {
2167 usable_win = 1;
2168 break;
2169 }
2170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002173 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002174 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002176 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002177 {
2178 tabpage_T *tp;
2179 win_T *wp;
2180
2181 FOR_ALL_TAB_WINDOWS(tp, wp)
2182 {
2183 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2184 {
2185 goto_tabpage_win(tp, wp);
2186 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002187 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002188 }
2189 }
2190 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002191win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002192
2193 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002194 * If there is only one window and it is the quickfix window, create a
2195 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002196 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002197 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002199 flags = WSP_ABOVE;
2200 if (ll_ref != NULL)
2201 flags |= WSP_NEWLOC;
2202 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 goto failed; /* not enough room for window */
2204 opened_window = TRUE; /* close it when fail */
2205 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002206 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002207 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002208 if (ll_ref != NULL)
2209 {
2210 /* The new window should use the location list from the
2211 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002212 curwin->w_llist = ll_ref;
2213 ll_ref->qf_refcount++;
2214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
2216 else
2217 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002218 if (curwin->w_llist_ref != NULL)
2219 {
2220 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002221 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002222 if (win == NULL)
2223 {
2224 /* Find the window showing the selected file */
2225 FOR_ALL_WINDOWS(win)
2226 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2227 break;
2228 if (win == NULL)
2229 {
2230 /* Find a previous usable window */
2231 win = curwin;
2232 do
2233 {
2234 if (win->w_buffer->b_p_bt[0] == NUL)
2235 break;
2236 if (win->w_prev == NULL)
2237 win = lastwin; /* wrap around the top */
2238 else
2239 win = win->w_prev; /* go to previous window */
2240 } while (win != curwin);
2241 }
2242 }
2243 win_goto(win);
2244
2245 /* If the location list for the window is not set, then set it
2246 * to the location list from the location window */
2247 if (win->w_llist == NULL)
2248 {
2249 win->w_llist = ll_ref;
2250 ll_ref->qf_refcount++;
2251 }
2252 }
2253 else
2254 {
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 /*
2257 * Try to find a window that shows the right buffer.
2258 * Default to the window just above the quickfix buffer.
2259 */
2260 win = curwin;
2261 altwin = NULL;
2262 for (;;)
2263 {
2264 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2265 break;
2266 if (win->w_prev == NULL)
2267 win = lastwin; /* wrap around the top */
2268 else
2269 win = win->w_prev; /* go to previous window */
2270
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002271 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 /* Didn't find it, go to the window before the quickfix
2274 * window. */
2275 if (altwin != NULL)
2276 win = altwin;
2277 else if (curwin->w_prev != NULL)
2278 win = curwin->w_prev;
2279 else
2280 win = curwin->w_next;
2281 break;
2282 }
2283
2284 /* Remember a usable window. */
2285 if (altwin == NULL && !win->w_p_pvw
2286 && win->w_buffer->b_p_bt[0] == NUL)
2287 altwin = win;
2288 }
2289
2290 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293 }
2294#endif
2295
2296 /*
2297 * If there is a file name,
2298 * read the wanted file if needed, and check autowrite etc.
2299 */
2300 old_curbuf = curbuf;
2301 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002302
2303 if (qf_ptr->qf_fnum != 0)
2304 {
2305 if (qf_ptr->qf_type == 1)
2306 {
2307 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2308 * set b_p_ro flag). */
2309 if (!can_abandon(curbuf, forceit))
2310 {
2311 EMSG(_(e_nowrtmsg));
2312 ok = FALSE;
2313 }
2314 else
2315 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002316 ECMD_HIDE + ECMD_SET_HELP,
2317 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002318 }
2319 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002320 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002321 int old_qf_curlist = qi->qf_curlist;
2322 int is_abort = FALSE;
2323
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002324 ok = buflist_getfile(qf_ptr->qf_fnum,
2325 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002326 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002327 {
2328 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002329 is_abort = TRUE;
2330 opened_window = FALSE;
2331 }
2332 else if (old_qf_curlist != qi->qf_curlist
2333 || !is_qf_entry_present(qi, qf_ptr))
2334 {
2335 if (qi == &ql_info)
2336 EMSG(_("E925: Current quickfix was changed"));
2337 else
2338 EMSG(_("E926: Current location list was changed"));
2339 is_abort = TRUE;
2340 }
2341
2342 if (is_abort)
2343 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002344 ok = FALSE;
2345 qi = NULL;
2346 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002347 }
2348 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002349 }
2350
2351 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 /* When not switched to another buffer, still need to set pc mark */
2354 if (curbuf == old_curbuf)
2355 setpcmark();
2356
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002357 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002359 /*
2360 * Go to line with error, unless qf_lnum is 0.
2361 */
2362 i = qf_ptr->qf_lnum;
2363 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002365 if (i > curbuf->b_ml.ml_line_count)
2366 i = curbuf->b_ml.ml_line_count;
2367 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002369 if (qf_ptr->qf_col > 0)
2370 {
2371 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002372#ifdef FEAT_VIRTUALEDIT
2373 curwin->w_cursor.coladd = 0;
2374#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002375 if (qf_ptr->qf_viscol == TRUE)
2376 {
2377 /*
2378 * Check each character from the beginning of the error
2379 * line up to the error column. For each tab character
2380 * found, reduce the error column value by the length of
2381 * a tab character.
2382 */
2383 line = ml_get_curline();
2384 screen_col = 0;
2385 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2386 {
2387 if (*line == NUL)
2388 break;
2389 if (*line++ == '\t')
2390 {
2391 curwin->w_cursor.col -= 7 - (screen_col % 8);
2392 screen_col += 8 - (screen_col % 8);
2393 }
2394 else
2395 ++screen_col;
2396 }
2397 }
2398 check_cursor();
2399 }
2400 else
2401 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002404 {
2405 pos_T save_cursor;
2406
2407 /* Move the cursor to the first line in the buffer */
2408 save_cursor = curwin->w_cursor;
2409 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002410 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2411 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002412 curwin->w_cursor = save_cursor;
2413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415#ifdef FEAT_FOLDING
2416 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2417 foldOpenCursor();
2418#endif
2419 if (print_message)
2420 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002421 /* Update the screen before showing the message, unless the screen
2422 * scrolled up. */
2423 if (!msg_scrolled)
2424 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2428 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2429 /* Add the message, skipping leading whitespace and newlines. */
2430 len = (int)STRLEN(IObuff);
2431 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2432
2433 /* Output the message. Overwrite to avoid scrolling when the 'O'
2434 * flag is present in 'shortmess'; But when not jumping, print the
2435 * whole message. */
2436 i = msg_scroll;
2437 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2438 msg_scroll = TRUE;
2439 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2440 msg_scroll = FALSE;
2441 msg_attr_keep(IObuff, 0, TRUE);
2442 msg_scroll = i;
2443 }
2444 }
2445 else
2446 {
2447#ifdef FEAT_WINDOWS
2448 if (opened_window)
2449 win_close(curwin, TRUE); /* Close opened window */
2450#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002451 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
2453 /*
2454 * Couldn't open file, so put index back where it was. This could
2455 * happen if the file was readonly and we changed something.
2456 */
2457#ifdef FEAT_WINDOWS
2458failed:
2459#endif
2460 qf_ptr = old_qf_ptr;
2461 qf_index = old_qf_index;
2462 }
2463 }
2464theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002465 if (qi != NULL)
2466 {
2467 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2468 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470#ifdef FEAT_WINDOWS
2471 if (p_swb != old_swb && opened_window)
2472 {
2473 /* Restore old 'switchbuf' value, but not when an autocommand or
2474 * modeline has changed the value. */
2475 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002478 swb_flags = old_swb_flags;
2479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 else
2481 free_string_option(old_swb);
2482 }
2483#endif
2484}
2485
2486/*
2487 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002488 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
2490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002491qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002493 buf_T *buf;
2494 char_u *fname;
2495 qfline_T *qfp;
2496 int i;
2497 int idx1 = 1;
2498 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002499 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002500 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002501 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002503 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002505 if (eap->cmdidx == CMD_llist)
2506 {
2507 qi = GET_LOC_LIST(curwin);
2508 if (qi == NULL)
2509 {
2510 EMSG(_(e_loclist));
2511 return;
2512 }
2513 }
2514
2515 if (qi->qf_curlist >= qi->qf_listcount
2516 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 EMSG(_(e_quickfix));
2519 return;
2520 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002521 if (*arg == '+')
2522 {
2523 ++arg;
2524 plus = TRUE;
2525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2527 {
2528 EMSG(_(e_trailing));
2529 return;
2530 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002531 if (plus)
2532 {
2533 i = qi->qf_lists[qi->qf_curlist].qf_index;
2534 idx2 = i + idx1;
2535 idx1 = i;
2536 }
2537 else
2538 {
2539 i = qi->qf_lists[qi->qf_curlist].qf_count;
2540 if (idx1 < 0)
2541 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2542 if (idx2 < 0)
2543 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002546 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002548 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2549 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
2551 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2552 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002553 msg_putchar('\n');
2554 if (got_int)
2555 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002556
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002557 fname = NULL;
2558 if (qfp->qf_fnum != 0
2559 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2560 {
2561 fname = buf->b_fname;
2562 if (qfp->qf_type == 1) /* :helpgrep */
2563 fname = gettail(fname);
2564 }
2565 if (fname == NULL)
2566 sprintf((char *)IObuff, "%2d", i);
2567 else
2568 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2569 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002570 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar8820b482017-03-16 17:23:31 +01002571 ? HL_ATTR(HLF_L) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002572 if (qfp->qf_lnum == 0)
2573 IObuff[0] = NUL;
2574 else if (qfp->qf_col == 0)
2575 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2576 else
2577 sprintf((char *)IObuff, ":%ld col %d",
2578 qfp->qf_lnum, qfp->qf_col);
2579 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2580 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002581 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002582 if (qfp->qf_pattern != NULL)
2583 {
2584 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2585 STRCAT(IObuff, ":");
2586 msg_puts(IObuff);
2587 }
2588 msg_puts((char_u *)" ");
2589
2590 /* Remove newlines and leading whitespace from the text. For an
2591 * unrecognized line keep the indent, the compiler may mark a word
2592 * with ^^^^. */
2593 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2595 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002596 msg_prt_line(IObuff, FALSE);
2597 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002599
2600 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002601 if (qfp == NULL)
2602 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002603 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 ui_breakcheck();
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606}
2607
2608/*
2609 * Remove newlines and leading whitespace from an error message.
2610 * Put the result in "buf[bufsize]".
2611 */
2612 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002613qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 int i;
2616 char_u *p = text;
2617
2618 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2619 {
2620 if (*p == '\n')
2621 {
2622 buf[i] = ' ';
2623 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002624 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 break;
2626 }
2627 else
2628 buf[i] = *p++;
2629 }
2630 buf[i] = NUL;
2631}
2632
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002633 static void
2634qf_msg(qf_info_T *qi, int which, char *lead)
2635{
2636 char *title = (char *)qi->qf_lists[which].qf_title;
2637 int count = qi->qf_lists[which].qf_count;
2638 char_u buf[IOSIZE];
2639
2640 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2641 lead,
2642 which + 1,
2643 qi->qf_listcount,
2644 count);
2645
2646 if (title != NULL)
2647 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002648 size_t len = STRLEN(buf);
2649
2650 if (len < 34)
2651 {
2652 vim_memset(buf + len, ' ', 34 - len);
2653 buf[34] = NUL;
2654 }
2655 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002656 }
2657 trunc_string(buf, buf, Columns - 1, IOSIZE);
2658 msg(buf);
2659}
2660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661/*
2662 * ":colder [count]": Up in the quickfix stack.
2663 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002664 * ":lolder [count]": Up in the location list stack.
2665 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 */
2667 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002668qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002670 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 int count;
2672
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002673 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2674 {
2675 qi = GET_LOC_LIST(curwin);
2676 if (qi == NULL)
2677 {
2678 EMSG(_(e_loclist));
2679 return;
2680 }
2681 }
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (eap->addr_count != 0)
2684 count = eap->line2;
2685 else
2686 count = 1;
2687 while (count--)
2688 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002689 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002691 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002694 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002696 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698 else
2699 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002700 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
2702 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002703 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002705 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
2707 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002708 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002710 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711#endif
2712}
2713
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002714 void
2715qf_history(exarg_T *eap)
2716{
2717 qf_info_T *qi = &ql_info;
2718 int i;
2719
2720 if (eap->cmdidx == CMD_lhistory)
2721 qi = GET_LOC_LIST(curwin);
2722 if (qi == NULL || (qi->qf_listcount == 0
2723 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2724 MSG(_("No entries"));
2725 else
2726 for (i = 0; i < qi->qf_listcount; ++i)
2727 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2728}
2729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002731 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 */
2733 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002734qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002736 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002737 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002738 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002740 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002742 qfp = qi->qf_lists[idx].qf_start;
2743 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002744 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002745 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002746 vim_free(qfp->qf_text);
2747 stop = (qfp == qfpnext);
2748 vim_free(qfp->qf_pattern);
2749 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002750 if (stop)
2751 /* Somehow qf_count may have an incorrect value, set it to 1
2752 * to avoid crashing when it's wrong.
2753 * TODO: Avoid qf_count being incorrect. */
2754 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002755 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002756 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002757 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002759 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002760 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002761 free_tv(qi->qf_lists[idx].qf_ctx);
2762 qi->qf_lists[idx].qf_ctx = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002763 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002764 qi->qf_lists[idx].qf_start = NULL;
Bram Moolenaar31bdd132017-04-15 15:22:52 +02002765 qi->qf_lists[idx].qf_last = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002766 qi->qf_lists[idx].qf_ptr = NULL;
2767 qi->qf_lists[idx].qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002768
2769 qf_clean_dir_stack(&qi->qf_dir_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002770 qi->qf_directory = NULL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002771 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002772 qi->qf_currfile = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002773 qi->qf_multiline = FALSE;
2774 qi->qf_multiignore = FALSE;
2775 qi->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776}
2777
2778/*
2779 * qf_mark_adjust: adjust marks
2780 */
2781 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002782qf_mark_adjust(
2783 win_T *wp,
2784 linenr_T line1,
2785 linenr_T line2,
2786 long amount,
2787 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002789 int i;
2790 qfline_T *qfp;
2791 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002792 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002793 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002794 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
Bram Moolenaarc1542742016-07-20 21:44:37 +02002796 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002797 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002798 if (wp != NULL)
2799 {
2800 if (wp->w_llist == NULL)
2801 return;
2802 qi = wp->w_llist;
2803 }
2804
2805 for (idx = 0; idx < qi->qf_listcount; ++idx)
2806 if (qi->qf_lists[idx].qf_count)
2807 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002808 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2809 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (qfp->qf_fnum == curbuf->b_fnum)
2811 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002812 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2814 {
2815 if (amount == MAXLNUM)
2816 qfp->qf_cleared = TRUE;
2817 else
2818 qfp->qf_lnum += amount;
2819 }
2820 else if (amount_after && qfp->qf_lnum > line2)
2821 qfp->qf_lnum += amount_after;
2822 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002823
2824 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002825 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828/*
2829 * Make a nice message out of the error character and the error number:
2830 * char number message
2831 * e or E 0 " error"
2832 * w or W 0 " warning"
2833 * i or I 0 " info"
2834 * 0 0 ""
2835 * other 0 " c"
2836 * e or E n " error n"
2837 * w or W n " warning n"
2838 * i or I n " info n"
2839 * 0 n " error n"
2840 * other n " c n"
2841 * 1 x "" :helpgrep
2842 */
2843 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002844qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
2846 static char_u buf[20];
2847 static char_u cc[3];
2848 char_u *p;
2849
2850 if (c == 'W' || c == 'w')
2851 p = (char_u *)" warning";
2852 else if (c == 'I' || c == 'i')
2853 p = (char_u *)" info";
2854 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2855 p = (char_u *)" error";
2856 else if (c == 0 || c == 1)
2857 p = (char_u *)"";
2858 else
2859 {
2860 cc[0] = ' ';
2861 cc[1] = c;
2862 cc[2] = NUL;
2863 p = cc;
2864 }
2865
2866 if (nr <= 0)
2867 return p;
2868
2869 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2870 return buf;
2871}
2872
2873#if defined(FEAT_WINDOWS) || defined(PROTO)
2874/*
2875 * ":cwindow": open the quickfix window if we have errors to display,
2876 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002877 * ":lwindow": open the location list window if we have locations to display,
2878 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 */
2880 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002881ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002883 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 win_T *win;
2885
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002886 if (eap->cmdidx == CMD_lwindow)
2887 {
2888 qi = GET_LOC_LIST(curwin);
2889 if (qi == NULL)
2890 return;
2891 }
2892
2893 /* Look for an existing quickfix window. */
2894 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
2896 /*
2897 * If a quickfix window is open but we have no errors to display,
2898 * close the window. If a quickfix window is not open, then open
2899 * it if we have errors; otherwise, leave it closed.
2900 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002901 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002902 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002903 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 {
2905 if (win != NULL)
2906 ex_cclose(eap);
2907 }
2908 else if (win == NULL)
2909 ex_copen(eap);
2910}
2911
2912/*
2913 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002914 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002917ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 win_T *win = NULL;
2920 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002922 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2923 {
2924 qi = GET_LOC_LIST(curwin);
2925 if (qi == NULL)
2926 return;
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002929 /* Find existing quickfix window and close it. */
2930 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 if (win != NULL)
2932 win_close(win, FALSE);
2933}
2934
2935/*
2936 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002937 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 */
2939 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002940ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002945 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002946 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002947 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002949 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2950 {
2951 qi = GET_LOC_LIST(curwin);
2952 if (qi == NULL)
2953 {
2954 EMSG(_(e_loclist));
2955 return;
2956 }
2957 }
2958
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (eap->addr_count != 0)
2960 height = eap->line2;
2961 else
2962 height = QF_WINHEIGHT;
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965#ifdef FEAT_GUI
2966 need_mouse_correct = TRUE;
2967#endif
2968
2969 /*
2970 * Find existing quickfix window, or open a new one.
2971 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002972 win = qf_find_win(qi);
2973
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002974 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002977 if (eap->addr_count != 0)
2978 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002979 if (cmdmod.split & WSP_VERT)
2980 {
2981 if (height != W_WIDTH(win))
2982 win_setwidth(height);
2983 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002984 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002985 win_setheight(height);
2986 }
2987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 else
2989 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002990 qf_buf = qf_find_buf(qi);
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 /* The current window becomes the previous window afterwards. */
2993 win = curwin;
2994
Bram Moolenaar77642c02012-11-20 17:55:10 +01002995 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2996 && cmdmod.split == 0)
2997 /* Create the new window at the very bottom, except when
2998 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002999 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003000 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003002 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003004 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003006 /*
3007 * For the location list window, create a reference to the
3008 * location list from the window 'win'.
3009 */
3010 curwin->w_llist_ref = win->w_llist;
3011 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003013
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003014 if (oldwin != curwin)
3015 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003016 if (qf_buf != NULL)
3017 /* Use the existing quickfix buffer */
3018 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003019 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003020 else
3021 {
3022 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003023 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003024 /* switch off 'swapfile' */
3025 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3026 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003027 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003028 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003029 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003030#ifdef FEAT_DIFF
3031 curwin->w_p_diff = FALSE;
3032#endif
3033#ifdef FEAT_FOLDING
3034 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3035 OPT_LOCAL);
3036#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003039 /* Only set the height when still in the same tab page and there is no
3040 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003041 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 win_setheight(height);
3043 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3044 if (win_valid(win))
3045 prevwin = win;
3046 }
3047
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003048 qf_set_title_var(qi);
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 /*
3051 * Fill the buffer with the quickfix list.
3052 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003053 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003055 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 curwin->w_cursor.col = 0;
3057 check_cursor();
3058 update_topline(); /* scroll to show the line */
3059}
3060
3061/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003062 * Move the cursor in the quickfix window to "lnum".
3063 */
3064 static void
3065qf_win_goto(win_T *win, linenr_T lnum)
3066{
3067 win_T *old_curwin = curwin;
3068
3069 curwin = win;
3070 curbuf = win->w_buffer;
3071 curwin->w_cursor.lnum = lnum;
3072 curwin->w_cursor.col = 0;
3073#ifdef FEAT_VIRTUALEDIT
3074 curwin->w_cursor.coladd = 0;
3075#endif
3076 curwin->w_curswant = 0;
3077 update_topline(); /* scroll to show the line */
3078 redraw_later(VALID);
3079 curwin->w_redr_status = TRUE; /* update ruler */
3080 curwin = old_curwin;
3081 curbuf = curwin->w_buffer;
3082}
3083
3084/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003085 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003086 */
3087 void
3088ex_cbottom(exarg_T *eap UNUSED)
3089{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003090 qf_info_T *qi = &ql_info;
3091 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003092
Bram Moolenaar537ef082016-07-09 17:56:19 +02003093 if (eap->cmdidx == CMD_lbottom)
3094 {
3095 qi = GET_LOC_LIST(curwin);
3096 if (qi == NULL)
3097 {
3098 EMSG(_(e_loclist));
3099 return;
3100 }
3101 }
3102
3103 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003104 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3105 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3106}
3107
3108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * Return the number of the current entry (line number in the quickfix
3110 * window).
3111 */
3112 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003113qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003115 qf_info_T *qi = &ql_info;
3116
3117 if (IS_LL_WINDOW(wp))
3118 /* In the location list window, use the referenced location list */
3119 qi = wp->w_llist_ref;
3120
3121 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122}
3123
3124/*
3125 * Update the cursor position in the quickfix window to the current error.
3126 * Return TRUE if there is a quickfix window.
3127 */
3128 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003129qf_win_pos_update(
3130 qf_info_T *qi,
3131 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003134 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 /*
3137 * Put the cursor on the current error in the quickfix window, so that
3138 * it's viewable.
3139 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003140 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (win != NULL
3142 && qf_index <= win->w_buffer->b_ml.ml_line_count
3143 && old_qf_index != qf_index)
3144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (qf_index > old_qf_index)
3146 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003147 win->w_redraw_top = old_qf_index;
3148 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
3150 else
3151 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003152 win->w_redraw_top = qf_index;
3153 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003155 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157 return win != NULL;
3158}
3159
3160/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003161 * Check whether the given window is displaying the specified quickfix/location
3162 * list buffer
3163 */
3164 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003165is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003166{
3167 /*
3168 * A window displaying the quickfix buffer will have the w_llist_ref field
3169 * set to NULL.
3170 * A window displaying a location list buffer will have the w_llist_ref
3171 * pointing to the location list.
3172 */
3173 if (bt_quickfix(win->w_buffer))
3174 if ((qi == &ql_info && win->w_llist_ref == NULL)
3175 || (qi != &ql_info && win->w_llist_ref == qi))
3176 return TRUE;
3177
3178 return FALSE;
3179}
3180
3181/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003182 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003183 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003184 */
3185 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003186qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003187{
3188 win_T *win;
3189
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003190 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003191 if (is_qf_win(win, qi))
3192 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003193
3194 return win;
3195}
3196
3197/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003198 * Find a quickfix buffer.
3199 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 */
3201 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003202qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003204 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003205 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar9c102382006-05-03 21:26:49 +00003207 FOR_ALL_TAB_WINDOWS(tp, win)
3208 if (is_qf_win(win, qi))
3209 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003210
Bram Moolenaar9c102382006-05-03 21:26:49 +00003211 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212}
3213
3214/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003215 * Update the w:quickfix_title variable in the quickfix/location list window
3216 */
3217 static void
3218qf_update_win_titlevar(qf_info_T *qi)
3219{
3220 win_T *win;
3221 win_T *curwin_save;
3222
3223 if ((win = qf_find_win(qi)) != NULL)
3224 {
3225 curwin_save = curwin;
3226 curwin = win;
3227 qf_set_title_var(qi);
3228 curwin = curwin_save;
3229 }
3230}
3231
3232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 * Find the quickfix buffer. If it exists, update the contents.
3234 */
3235 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003236qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003239 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003243 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (buf != NULL)
3245 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003246 linenr_T old_line_count = buf->b_ml.ml_line_count;
3247
3248 if (old_last == NULL)
3249 /* set curwin/curbuf to buf and save a few things */
3250 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaard823fa92016-08-12 16:29:27 +02003252 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003253
Bram Moolenaar864293a2016-06-02 13:40:04 +02003254 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003255
Bram Moolenaar864293a2016-06-02 13:40:04 +02003256 if (old_last == NULL)
3257 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003258 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003259
3260 /* restore curwin/curbuf and a few other things */
3261 aucmd_restbuf(&aco);
3262 }
3263
3264 /* Only redraw when added lines are visible. This avoids flickering
3265 * when the added lines are not visible. */
3266 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3267 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269}
3270
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003271/*
3272 * Set "w:quickfix_title" if "qi" has a title.
3273 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003274 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003275qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003276{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003277 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3278 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003279 qi->qf_lists[qi->qf_curlist].qf_title);
3280}
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282/*
3283 * Fill current buffer with quickfix errors, replacing any previous contents.
3284 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003285 * If "old_last" is not NULL append the items after this one.
3286 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3287 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 */
3289 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003290qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003292 linenr_T lnum;
3293 qfline_T *qfp;
3294 buf_T *errbuf;
3295 int len;
3296 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
Bram Moolenaar864293a2016-06-02 13:40:04 +02003298 if (old_last == NULL)
3299 {
3300 if (buf != curbuf)
3301 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003302 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003303 return;
3304 }
3305
3306 /* delete all existing lines */
3307 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3308 (void)ml_delete((linenr_T)1, FALSE);
3309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
3311 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003312 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003315 if (old_last == NULL)
3316 {
3317 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3318 lnum = 0;
3319 }
3320 else
3321 {
3322 qfp = old_last->qf_next;
3323 lnum = buf->b_ml.ml_line_count;
3324 }
3325 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
3327 if (qfp->qf_fnum != 0
3328 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3329 && errbuf->b_fname != NULL)
3330 {
3331 if (qfp->qf_type == 1) /* :helpgrep */
3332 STRCPY(IObuff, gettail(errbuf->b_fname));
3333 else
3334 STRCPY(IObuff, errbuf->b_fname);
3335 len = (int)STRLEN(IObuff);
3336 }
3337 else
3338 len = 0;
3339 IObuff[len++] = '|';
3340
3341 if (qfp->qf_lnum > 0)
3342 {
3343 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3344 len += (int)STRLEN(IObuff + len);
3345
3346 if (qfp->qf_col > 0)
3347 {
3348 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3349 len += (int)STRLEN(IObuff + len);
3350 }
3351
3352 sprintf((char *)IObuff + len, "%s",
3353 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3354 len += (int)STRLEN(IObuff + len);
3355 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003356 else if (qfp->qf_pattern != NULL)
3357 {
3358 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3359 len += (int)STRLEN(IObuff + len);
3360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 IObuff[len++] = '|';
3362 IObuff[len++] = ' ';
3363
3364 /* Remove newlines and leading whitespace from the text.
3365 * For an unrecognized line keep the indent, the compiler may
3366 * mark a word with ^^^^. */
3367 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3368 IObuff + len, IOSIZE - len);
3369
Bram Moolenaar864293a2016-06-02 13:40:04 +02003370 if (ml_append_buf(buf, lnum, IObuff,
3371 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003373 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003375 if (qfp == NULL)
3376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003378
3379 if (old_last == NULL)
3380 /* Delete the empty line which is now at the end */
3381 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383
3384 /* correct cursor position */
3385 check_lnums(TRUE);
3386
Bram Moolenaar864293a2016-06-02 13:40:04 +02003387 if (old_last == NULL)
3388 {
3389 /* Set the 'filetype' to "qf" each time after filling the buffer.
3390 * This resembles reading a file into a buffer, it's more logical when
3391 * using autocommands. */
3392 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3393 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003396 keep_filetype = TRUE; /* don't detect 'filetype' */
3397 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003399 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003401 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003403 /* make sure it will be redrawn */
3404 redraw_curbuf_later(NOT_VALID);
3405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
3407 /* Restore KeyTyped, setting 'filetype' may reset it. */
3408 KeyTyped = old_KeyTyped;
3409}
3410
3411#endif /* FEAT_WINDOWS */
3412
3413/*
3414 * Return TRUE if "buf" is the quickfix buffer.
3415 */
3416 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003417bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003419 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420}
3421
3422/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003423 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3424 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 */
3426 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003427bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003429 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3430 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431}
3432
3433/*
3434 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3435 */
3436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003437bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003439 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440}
3441
3442 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003443bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 if (bt_dontwrite(buf))
3446 {
3447 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3448 return TRUE;
3449 }
3450 return FALSE;
3451}
3452
3453/*
3454 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3455 * and 'bufhidden'.
3456 */
3457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003458buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3461 switch (buf->b_p_bh[0])
3462 {
3463 case 'u': /* "unload" */
3464 case 'w': /* "wipe" */
3465 case 'd': return FALSE; /* "delete" */
3466 case 'h': return TRUE; /* "hide" */
3467 }
3468 return (p_hid || cmdmod.hide);
3469}
3470
3471/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003472 * Return TRUE when using ":vimgrep" for ":grep".
3473 */
3474 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003475grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003476{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003477 return ((cmdidx == CMD_grep
3478 || cmdidx == CMD_lgrep
3479 || cmdidx == CMD_grepadd
3480 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003481 && STRCMP("internal",
3482 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3483}
3484
3485/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003486 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 */
3488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003489ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003491 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003493 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003495 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003496 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003497 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003498#ifdef FEAT_AUTOCMD
3499 char_u *au_name = NULL;
3500
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003501 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3502 if (grep_internal(eap->cmdidx))
3503 {
3504 ex_vimgrep(eap);
3505 return;
3506 }
3507
Bram Moolenaar7c626922005-02-07 22:01:03 +00003508 switch (eap->cmdidx)
3509 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003510 case CMD_make: au_name = (char_u *)"make"; break;
3511 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3512 case CMD_grep: au_name = (char_u *)"grep"; break;
3513 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3514 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3515 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003516 default: break;
3517 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003518 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3519 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003520 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003521# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003522 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003523 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003524# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003525 }
3526#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003527#ifdef FEAT_MBYTE
3528 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaara6557602006-02-04 22:43:20 +00003531 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3532 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003533 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003536 fname = get_mef_name();
3537 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003539 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541 /*
3542 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3543 */
3544 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3545 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003546 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 cmd = alloc(len);
3548 if (cmd == NULL)
3549 return;
3550 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3551 (char *)p_shq);
3552 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003553 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 /*
3555 * Output a newline if there's something else than the :make command that
3556 * was typed (in which case the cursor is in column 0).
3557 */
3558 if (msg_col == 0)
3559 msg_didout = FALSE;
3560 msg_start();
3561 MSG_PUTS(":!");
3562 msg_outtrans(cmd); /* show what we are doing */
3563
3564 /* let the shell know if we are redirecting output or not */
3565 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3566
3567#ifdef AMIGA
3568 out_flush();
3569 /* read window status report and redraw before message */
3570 (void)char_avail();
3571#endif
3572
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003573 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003574 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3575 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003576 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003577 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003578 if (wp != NULL)
3579 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003580#ifdef FEAT_AUTOCMD
3581 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003582 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003583 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3584 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003585 if (qi->qf_curlist < qi->qf_listcount)
3586 res = qi->qf_lists[qi->qf_curlist].qf_count;
3587 else
3588 res = 0;
3589 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003590#endif
3591 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003592 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
Bram Moolenaar7c626922005-02-07 22:01:03 +00003594 mch_remove(fname);
3595 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 vim_free(cmd);
3597}
3598
3599/*
3600 * Return the name for the errorfile, in allocated memory.
3601 * Find a new unique name when 'makeef' contains "##".
3602 * Returns NULL for error.
3603 */
3604 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003605get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606{
3607 char_u *p;
3608 char_u *name;
3609 static int start = -1;
3610 static int off = 0;
3611#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003612 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613#endif
3614
3615 if (*p_mef == NUL)
3616 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003617 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (name == NULL)
3619 EMSG(_(e_notmp));
3620 return name;
3621 }
3622
3623 for (p = p_mef; *p; ++p)
3624 if (p[0] == '#' && p[1] == '#')
3625 break;
3626
3627 if (*p == NUL)
3628 return vim_strsave(p_mef);
3629
3630 /* Keep trying until the name doesn't exist yet. */
3631 for (;;)
3632 {
3633 if (start == -1)
3634 start = mch_get_pid();
3635 else
3636 off += 19;
3637
3638 name = alloc((unsigned)STRLEN(p_mef) + 30);
3639 if (name == NULL)
3640 break;
3641 STRCPY(name, p_mef);
3642 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3643 STRCAT(name, p + 2);
3644 if (mch_getperm(name) < 0
3645#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003646 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 && mch_lstat((char *)name, &sb) < 0
3648#endif
3649 )
3650 break;
3651 vim_free(name);
3652 }
3653 return name;
3654}
3655
3656/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003657 * Returns the number of valid entries in the current quickfix/location list.
3658 */
3659 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003660qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003661{
3662 qf_info_T *qi = &ql_info;
3663 qfline_T *qfp;
3664 int i, sz = 0;
3665 int prev_fnum = 0;
3666
3667 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3668 {
3669 /* Location list */
3670 qi = GET_LOC_LIST(curwin);
3671 if (qi == NULL)
3672 return 0;
3673 }
3674
3675 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003676 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003677 ++i, qfp = qfp->qf_next)
3678 {
3679 if (qfp->qf_valid)
3680 {
3681 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3682 sz++; /* Count all valid entries */
3683 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3684 {
3685 /* Count the number of files */
3686 sz++;
3687 prev_fnum = qfp->qf_fnum;
3688 }
3689 }
3690 }
3691
3692 return sz;
3693}
3694
3695/*
3696 * Returns the current index of the quickfix/location list.
3697 * Returns 0 if there is an error.
3698 */
3699 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003700qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003701{
3702 qf_info_T *qi = &ql_info;
3703
3704 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3705 {
3706 /* Location list */
3707 qi = GET_LOC_LIST(curwin);
3708 if (qi == NULL)
3709 return 0;
3710 }
3711
3712 return qi->qf_lists[qi->qf_curlist].qf_index;
3713}
3714
3715/*
3716 * Returns the current index in the quickfix/location list (counting only valid
3717 * entries). If no valid entries are in the list, then returns 1.
3718 */
3719 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003720qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003721{
3722 qf_info_T *qi = &ql_info;
3723 qf_list_T *qfl;
3724 qfline_T *qfp;
3725 int i, eidx = 0;
3726 int prev_fnum = 0;
3727
3728 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3729 {
3730 /* Location list */
3731 qi = GET_LOC_LIST(curwin);
3732 if (qi == NULL)
3733 return 1;
3734 }
3735
3736 qfl = &qi->qf_lists[qi->qf_curlist];
3737 qfp = qfl->qf_start;
3738
3739 /* check if the list has valid errors */
3740 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3741 return 1;
3742
3743 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3744 {
3745 if (qfp->qf_valid)
3746 {
3747 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3748 {
3749 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3750 {
3751 /* Count the number of files */
3752 eidx++;
3753 prev_fnum = qfp->qf_fnum;
3754 }
3755 }
3756 else
3757 eidx++;
3758 }
3759 }
3760
3761 return eidx ? eidx : 1;
3762}
3763
3764/*
3765 * Get the 'n'th valid error entry in the quickfix or location list.
3766 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3767 * For :cdo and :ldo returns the 'n'th valid error entry.
3768 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3769 */
3770 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003771qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003772{
3773 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3774 qfline_T *qfp = qfl->qf_start;
3775 int i, eidx;
3776 int prev_fnum = 0;
3777
3778 /* check if the list has valid errors */
3779 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3780 return 1;
3781
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003782 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003783 i++, qfp = qfp->qf_next)
3784 {
3785 if (qfp->qf_valid)
3786 {
3787 if (fdo)
3788 {
3789 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3790 {
3791 /* Count the number of files */
3792 eidx++;
3793 prev_fnum = qfp->qf_fnum;
3794 }
3795 }
3796 else
3797 eidx++;
3798 }
3799
3800 if (eidx == n)
3801 break;
3802 }
3803
3804 if (i <= qfl->qf_count)
3805 return i;
3806 else
3807 return 1;
3808}
3809
3810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003812 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003813 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 */
3815 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003816ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003818 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003819 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003820
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003821 if (eap->cmdidx == CMD_ll
3822 || eap->cmdidx == CMD_lrewind
3823 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003824 || eap->cmdidx == CMD_llast
3825 || eap->cmdidx == CMD_ldo
3826 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003827 {
3828 qi = GET_LOC_LIST(curwin);
3829 if (qi == NULL)
3830 {
3831 EMSG(_(e_loclist));
3832 return;
3833 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003834 }
3835
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003836 if (eap->addr_count > 0)
3837 errornr = (int)eap->line2;
3838 else
3839 {
3840 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3841 errornr = 0;
3842 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3843 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3844 errornr = 1;
3845 else
3846 errornr = 32767;
3847 }
3848
3849 /* For cdo and ldo commands, jump to the nth valid error.
3850 * For cfdo and lfdo commands, jump to the nth valid file entry.
3851 */
3852 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3853 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3854 errornr = qf_get_nth_valid_entry(qi,
3855 eap->addr_count > 0 ? (int)eap->line1 : 1,
3856 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3857
3858 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859}
3860
3861/*
3862 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003863 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003864 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 */
3866 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003867ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003869 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003870 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003871
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003872 if (eap->cmdidx == CMD_lnext
3873 || eap->cmdidx == CMD_lNext
3874 || eap->cmdidx == CMD_lprevious
3875 || eap->cmdidx == CMD_lnfile
3876 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003877 || eap->cmdidx == CMD_lpfile
3878 || eap->cmdidx == CMD_ldo
3879 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003880 {
3881 qi = GET_LOC_LIST(curwin);
3882 if (qi == NULL)
3883 {
3884 EMSG(_(e_loclist));
3885 return;
3886 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003887 }
3888
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003889 if (eap->addr_count > 0 &&
3890 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3891 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3892 errornr = (int)eap->line2;
3893 else
3894 errornr = 1;
3895
3896 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3897 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003899 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3900 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003902 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3903 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 ? BACKWARD_FILE
3905 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003906 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907}
3908
3909/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003910 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003911 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 */
3913 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003914ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003916 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003917 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003918 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003919#ifdef FEAT_AUTOCMD
3920 char_u *au_name = NULL;
3921#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003922
3923 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003924 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003925 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003926
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003927#ifdef FEAT_AUTOCMD
3928 switch (eap->cmdidx)
3929 {
3930 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3931 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3932 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3933 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3934 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3935 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3936 default: break;
3937 }
3938 if (au_name != NULL)
3939 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3940#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003941#ifdef FEAT_MBYTE
3942 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3943#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003944#ifdef FEAT_BROWSE
3945 if (cmdmod.browse)
3946 {
3947 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3948 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3949 if (browse_file == NULL)
3950 return;
3951 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3952 vim_free(browse_file);
3953 }
3954 else
3955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003957 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003958
3959 /*
3960 * This function is used by the :cfile, :cgetfile and :caddfile
3961 * commands.
3962 * :cfile always creates a new quickfix list and jumps to the
3963 * first error.
3964 * :cgetfile creates a new quickfix list but doesn't jump to the
3965 * first error.
3966 * :caddfile adds to an existing quickfix list. If there is no
3967 * quickfix list then a new list is created.
3968 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003969 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003970 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003971 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003972 && (eap->cmdidx == CMD_cfile
3973 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003974 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003975#ifdef FEAT_AUTOCMD
3976 if (au_name != NULL)
3977 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3978#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003979 if (wp != NULL)
3980 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003981 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003982 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003983
3984 else
3985 {
3986#ifdef FEAT_AUTOCMD
3987 if (au_name != NULL)
3988 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3989#endif
3990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991}
3992
3993/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003994 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003995 * ":vimgrepadd {pattern} file(s)"
3996 * ":lvimgrep {pattern} file(s)"
3997 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003998 */
3999 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004000ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004001{
Bram Moolenaar81695252004-12-29 20:58:21 +00004002 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004003 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004004 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004005 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004006 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004007 char_u *s;
4008 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004009 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004010 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004011#ifdef FEAT_AUTOCMD
4012 qfline_T *cur_qf_start;
4013#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004014 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004015 buf_T *buf;
4016 int duplicate_name = FALSE;
4017 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004018 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004019 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004020 buf_T *first_match_buf = NULL;
4021 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004022 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004023#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4024 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004025#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004026 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004027 int flags = 0;
4028 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004029 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004030 char_u *dirname_start = NULL;
4031 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004032 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004033#ifdef FEAT_AUTOCMD
4034 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004035
4036 switch (eap->cmdidx)
4037 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004038 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4039 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4040 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004041 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004042 case CMD_grep: au_name = (char_u *)"grep"; break;
4043 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4044 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4045 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004046 default: break;
4047 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004048 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4049 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004050 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004051# ifdef FEAT_EVAL
4052 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004053 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004054# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004055 }
4056#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004057
Bram Moolenaar754b5602006-02-09 23:53:20 +00004058 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004059 || eap->cmdidx == CMD_lvimgrep
4060 || eap->cmdidx == CMD_lgrepadd
4061 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004062 {
4063 qi = ll_get_or_alloc_list(curwin);
4064 if (qi == NULL)
4065 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004066 }
4067
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004068 if (eap->addr_count > 0)
4069 tomatch = eap->line2;
4070 else
4071 tomatch = MAXLNUM;
4072
Bram Moolenaar81695252004-12-29 20:58:21 +00004073 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004074 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004075 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004076 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004077 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004078 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004079 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004080 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004081 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004082
4083 if (s != NULL && *s == NUL)
4084 {
4085 /* Pattern is empty, use last search pattern. */
4086 if (last_search_pat() == NULL)
4087 {
4088 EMSG(_(e_noprevre));
4089 goto theend;
4090 }
4091 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4092 }
4093 else
4094 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4095
Bram Moolenaar86b68352004-12-27 21:59:20 +00004096 if (regmatch.regprog == NULL)
4097 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004098 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004099 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004100
4101 p = skipwhite(p);
4102 if (*p == NUL)
4103 {
4104 EMSG(_("E683: File name missing or invalid pattern"));
4105 goto theend;
4106 }
4107
Bram Moolenaar754b5602006-02-09 23:53:20 +00004108 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004109 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004110 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004111 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004112 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004113
4114 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004115 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004116 goto theend;
4117 if (fcount == 0)
4118 {
4119 EMSG(_(e_nomatch));
4120 goto theend;
4121 }
4122
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004123 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4124 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004125 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004126 {
4127 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004128 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004129 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004130
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004131 /* Remember the current directory, because a BufRead autocommand that does
4132 * ":lcd %:p:h" changes the meaning of short path names. */
4133 mch_dirname(dirname_start, MAXPATHL);
4134
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004135#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004136 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004137 * changing the current quickfix list. */
4138 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4139#endif
4140
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004141 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004142 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004143 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004144 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004145 if (time(NULL) > seconds)
4146 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004147 /* Display the file name every second or so, show the user we are
4148 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004149 seconds = time(NULL);
4150 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004151 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004152 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004153 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004154 else
4155 {
4156 msg_outtrans(p);
4157 vim_free(p);
4158 }
4159 msg_clr_eos();
4160 msg_didout = FALSE; /* overwrite this message */
4161 msg_nowait = TRUE; /* don't wait for this message */
4162 msg_col = 0;
4163 out_flush();
4164 }
4165
Bram Moolenaar81695252004-12-29 20:58:21 +00004166 buf = buflist_findname_exp(fnames[fi]);
4167 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4168 {
4169 /* Remember that a buffer with this name already exists. */
4170 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004171 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004172 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004173
4174#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4175 /* Don't do Filetype autocommands to avoid loading syntax and
4176 * indent scripts, a great speed improvement. */
4177 save_ei = au_event_disable(",Filetype");
4178#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004179 /* Don't use modelines here, it's useless. */
4180 save_mls = p_mls;
4181 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004182
4183 /* Load file into a buffer, so that 'fileencoding' is detected,
4184 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004185 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004186
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004187 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004188#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4189 au_event_restore(save_ei);
4190#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004191 }
4192 else
4193 /* Use existing, loaded buffer. */
4194 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004195
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004196#ifdef FEAT_AUTOCMD
4197 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4198 {
4199 int idx;
4200
4201 /* Autocommands changed the quickfix list. Find the one we were
4202 * using and restore it. */
4203 for (idx = 0; idx < LISTCOUNT; ++idx)
4204 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4205 {
4206 qi->qf_curlist = idx;
4207 break;
4208 }
4209 if (idx == LISTCOUNT)
4210 {
4211 /* List cannot be found, create a new one. */
4212 qf_new_list(qi, *eap->cmdlinep);
4213 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4214 }
4215 }
4216#endif
4217
Bram Moolenaar81695252004-12-29 20:58:21 +00004218 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004219 {
4220 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004221 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004222 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004223 else
4224 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004225 /* Try for a match in all lines of the buffer.
4226 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004227 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004228 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4229 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004230 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004231 col = 0;
4232 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004233 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004234 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004235 /* Pass the buffer number so that it gets used even for a
4236 * dummy buffer, unless duplicate_name is set, then the
4237 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004238 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004239 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004240 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004241 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004242 ml_get_buf(buf,
4243 regmatch.startpos[0].lnum + lnum, FALSE),
4244 regmatch.startpos[0].lnum + lnum,
4245 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004246 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004247 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004248 0, /* nr */
4249 0, /* type */
4250 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004251 ) == FAIL)
4252 {
4253 got_int = TRUE;
4254 break;
4255 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004256 found_match = TRUE;
4257 if (--tomatch == 0)
4258 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004259 if ((flags & VGR_GLOBAL) == 0
4260 || regmatch.endpos[0].lnum > 0)
4261 break;
4262 col = regmatch.endpos[0].col
4263 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004264 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004265 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004266 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004267 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004268 if (got_int)
4269 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004270 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004271#ifdef FEAT_AUTOCMD
4272 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4273#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004274
4275 if (using_dummy)
4276 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004277 if (found_match && first_match_buf == NULL)
4278 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004279 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004280 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004281 /* Never keep a dummy buffer if there is another buffer
4282 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004283 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004284 buf = NULL;
4285 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004286 else if (!cmdmod.hide
4287 || buf->b_p_bh[0] == 'u' /* "unload" */
4288 || buf->b_p_bh[0] == 'w' /* "wipe" */
4289 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004290 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004291 /* When no match was found we don't need to remember the
4292 * buffer, wipe it out. If there was a match and it
4293 * wasn't the first one or we won't jump there: only
4294 * unload the buffer.
4295 * Ignore 'hidden' here, because it may lead to having too
4296 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004297 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004298 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004299 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004300 buf = NULL;
4301 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004302 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004303 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004304 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004305 /* Keeping the buffer, remove the dummy flag. */
4306 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004307 buf = NULL;
4308 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004309 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004310
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004311 if (buf != NULL)
4312 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004313 /* Keeping the buffer, remove the dummy flag. */
4314 buf->b_flags &= ~BF_DUMMY;
4315
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004316 /* If the buffer is still loaded we need to use the
4317 * directory we jumped to below. */
4318 if (buf == first_match_buf
4319 && target_dir == NULL
4320 && STRCMP(dirname_start, dirname_now) != 0)
4321 target_dir = vim_strsave(dirname_now);
4322
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004323 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004324 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004325 * need to be done (again). But not the window-local
4326 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004327 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004328#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004329 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4330 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004331#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004332 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004333 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004334 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004335 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004336 }
4337 }
4338
4339 FreeWild(fcount, fnames);
4340
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004341 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4342 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4343 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004344
4345#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004346 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004347#endif
4348
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004349#ifdef FEAT_AUTOCMD
4350 if (au_name != NULL)
4351 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4352 curbuf->b_fname, TRUE, curbuf);
4353#endif
4354
Bram Moolenaar86b68352004-12-27 21:59:20 +00004355 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004356 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004357 {
4358 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004359 {
4360 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004361 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004362 if (buf != curbuf)
4363 /* If we jumped to another buffer redrawing will already be
4364 * taken care of. */
4365 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004366
4367 /* Jump to the directory used after loading the buffer. */
4368 if (curbuf == first_match_buf && target_dir != NULL)
4369 {
4370 exarg_T ea;
4371
4372 ea.arg = target_dir;
4373 ea.cmdidx = CMD_lcd;
4374 ex_cd(&ea);
4375 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004376 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004377 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004378 else
4379 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004380
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004381 /* If we loaded a dummy buffer into the current window, the autocommands
4382 * may have messed up things, need to redraw and recompute folds. */
4383 if (redraw_for_dummy)
4384 {
4385#ifdef FEAT_FOLDING
4386 foldUpdateAll(curwin);
4387#else
4388 redraw_later(NOT_VALID);
4389#endif
4390 }
4391
Bram Moolenaar86b68352004-12-27 21:59:20 +00004392theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004393 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004394 vim_free(dirname_now);
4395 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004396 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004397 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004398}
4399
4400/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004401 * Restore current working directory to "dirname_start" if they differ, taking
4402 * into account whether it is set locally or globally.
4403 */
4404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004405restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004406{
4407 char_u *dirname_now = alloc(MAXPATHL);
4408
4409 if (NULL != dirname_now)
4410 {
4411 mch_dirname(dirname_now, MAXPATHL);
4412 if (STRCMP(dirname_start, dirname_now) != 0)
4413 {
4414 /* If the directory has changed, change it back by building up an
4415 * appropriate ex command and executing it. */
4416 exarg_T ea;
4417
4418 ea.arg = dirname_start;
4419 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4420 ex_cd(&ea);
4421 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004422 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004423 }
4424}
4425
4426/*
4427 * Load file "fname" into a dummy buffer and return the buffer pointer,
4428 * placing the directory resulting from the buffer load into the
4429 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4430 * prior to calling this function. Restores directory to "dirname_start" prior
4431 * to returning, if autocmds or the 'autochdir' option have changed it.
4432 *
4433 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4434 * or wipe_dummy_buffer() later!
4435 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004436 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004437 */
4438 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004439load_dummy_buffer(
4440 char_u *fname,
4441 char_u *dirname_start, /* in: old directory */
4442 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004443{
4444 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004445 bufref_T newbufref;
4446 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004447 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004448 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004449
4450 /* Allocate a buffer without putting it in the buffer list. */
4451 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4452 if (newbuf == NULL)
4453 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004454 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004455
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004456 /* Init the options. */
4457 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4458
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004459 /* need to open the memfile before putting the buffer in a window */
4460 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004461 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004462 /* set curwin/curbuf to buf and save a few things */
4463 aucmd_prepbuf(&aco, newbuf);
4464
4465 /* Need to set the filename for autocommands. */
4466 (void)setfname(curbuf, fname, NULL, FALSE);
4467
Bram Moolenaar81695252004-12-29 20:58:21 +00004468 /* Create swap file now to avoid the ATTENTION message. */
4469 check_need_swap(TRUE);
4470
4471 /* Remove the "dummy" flag, otherwise autocommands may not
4472 * work. */
4473 curbuf->b_flags &= ~BF_DUMMY;
4474
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004475 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004476 if (readfile(fname, NULL,
4477 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4478 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004479 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004480 && !(curbuf->b_flags & BF_NEW))
4481 {
4482 failed = FALSE;
4483 if (curbuf != newbuf)
4484 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004485 /* Bloody autocommands changed the buffer! Can happen when
4486 * using netrw and editing a remote file. Use the current
4487 * buffer instead, delete the dummy one after restoring the
4488 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004489 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004490 newbuf = curbuf;
4491 }
4492 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004493
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004494 /* restore curwin/curbuf and a few other things */
4495 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004496 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4497 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004498
4499 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4500 * skip it. */
4501 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004502 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004503
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004504 /*
4505 * When autocommands/'autochdir' option changed directory: go back.
4506 * Let the caller know what the resulting dir was first, in case it is
4507 * important.
4508 */
4509 mch_dirname(resulting_dir, MAXPATHL);
4510 restore_start_dir(dirname_start);
4511
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004512 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004513 return NULL;
4514 if (failed)
4515 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004516 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004517 return NULL;
4518 }
4519 return newbuf;
4520}
4521
4522/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004523 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4524 * directory to "dirname_start" prior to returning, if autocmds or the
4525 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004526 */
4527 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004528wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004529{
4530 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004531 {
4532#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4533 cleanup_T cs;
4534
4535 /* Reset the error/interrupt/exception state here so that aborting()
4536 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4537 * work when got_int is set. */
4538 enter_cleanup(&cs);
4539#endif
4540
Bram Moolenaar81695252004-12-29 20:58:21 +00004541 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004542
4543#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4544 /* Restore the error/interrupt/exception state if not discarded by a
4545 * new aborting error, interrupt, or uncaught exception. */
4546 leave_cleanup(&cs);
4547#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004548 /* When autocommands/'autochdir' option changed directory: go back. */
4549 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004550 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004551}
4552
4553/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004554 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4555 * directory to "dirname_start" prior to returning, if autocmds or the
4556 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004557 */
4558 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004559unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004560{
4561 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004562 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004563 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004564
4565 /* When autocommands/'autochdir' option changed directory: go back. */
4566 restore_start_dir(dirname_start);
4567 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004568}
4569
Bram Moolenaar05159a02005-02-26 23:04:13 +00004570#if defined(FEAT_EVAL) || defined(PROTO)
4571/*
4572 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004573 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004574 */
4575 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004576get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004577{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004578 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004579 dict_T *dict;
4580 char_u buf[2];
4581 qfline_T *qfp;
4582 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004583 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004584
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004585 if (wp != NULL)
4586 {
4587 qi = GET_LOC_LIST(wp);
4588 if (qi == NULL)
4589 return FAIL;
4590 }
4591
Bram Moolenaard823fa92016-08-12 16:29:27 +02004592 if (qf_idx == -1)
4593 qf_idx = qi->qf_curlist;
4594
4595 if (qf_idx >= qi->qf_listcount
4596 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004597 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004598
Bram Moolenaard823fa92016-08-12 16:29:27 +02004599 qfp = qi->qf_lists[qf_idx].qf_start;
4600 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004601 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004602 /* Handle entries with a non-existing buffer number. */
4603 bufnum = qfp->qf_fnum;
4604 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4605 bufnum = 0;
4606
Bram Moolenaar05159a02005-02-26 23:04:13 +00004607 if ((dict = dict_alloc()) == NULL)
4608 return FAIL;
4609 if (list_append_dict(list, dict) == FAIL)
4610 return FAIL;
4611
4612 buf[0] = qfp->qf_type;
4613 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004614 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004615 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4616 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4617 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4618 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004619 || dict_add_nr_str(dict, "pattern", 0L,
4620 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4621 || dict_add_nr_str(dict, "text", 0L,
4622 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004623 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4624 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4625 return FAIL;
4626
4627 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004628 if (qfp == NULL)
4629 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004630 }
4631 return OK;
4632}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004633
4634/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004635 * Flags used by getqflist()/getloclist() to determine which fields to return.
4636 */
4637enum {
4638 QF_GETLIST_NONE = 0x0,
4639 QF_GETLIST_TITLE = 0x1,
4640 QF_GETLIST_ITEMS = 0x2,
4641 QF_GETLIST_NR = 0x4,
4642 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004643 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004644 QF_GETLIST_ALL = 0xFF
4645};
4646
4647/*
4648 * Return quickfix/location list details (title) as a
4649 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4650 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004651 */
4652 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004653get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4654{
4655 qf_info_T *qi = &ql_info;
4656 int status = OK;
4657 int qf_idx;
4658 dictitem_T *di;
4659 int flags = QF_GETLIST_NONE;
4660
4661 if (wp != NULL)
4662 {
4663 qi = GET_LOC_LIST(wp);
4664 if (qi == NULL)
4665 return FAIL;
4666 }
4667
4668 qf_idx = qi->qf_curlist; /* default is the current list */
4669 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4670 {
4671 /* Use the specified quickfix/location list */
4672 if (di->di_tv.v_type == VAR_NUMBER)
4673 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004674 /* for zero use the current list */
4675 if (di->di_tv.vval.v_number != 0)
4676 {
4677 qf_idx = di->di_tv.vval.v_number - 1;
4678 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4679 return FAIL;
4680 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004681 flags |= QF_GETLIST_NR;
4682 }
4683 else
4684 return FAIL;
4685 }
4686
4687 if (dict_find(what, (char_u *)"all", -1) != NULL)
4688 flags |= QF_GETLIST_ALL;
4689
4690 if (dict_find(what, (char_u *)"title", -1) != NULL)
4691 flags |= QF_GETLIST_TITLE;
4692
4693 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4694 flags |= QF_GETLIST_WINID;
4695
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004696 if (dict_find(what, (char_u *)"context", -1) != NULL)
4697 flags |= QF_GETLIST_CONTEXT;
4698
Bram Moolenaard823fa92016-08-12 16:29:27 +02004699 if (flags & QF_GETLIST_TITLE)
4700 {
4701 char_u *t;
4702 t = qi->qf_lists[qf_idx].qf_title;
4703 if (t == NULL)
4704 t = (char_u *)"";
4705 status = dict_add_nr_str(retdict, "title", 0L, t);
4706 }
4707 if ((status == OK) && (flags & QF_GETLIST_NR))
4708 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4709 if ((status == OK) && (flags & QF_GETLIST_WINID))
4710 {
4711 win_T *win;
4712 win = qf_find_win(qi);
4713 if (win != NULL)
4714 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4715 }
4716
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004717 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4718 {
4719 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4720 {
4721 di = dictitem_alloc((char_u *)"context");
4722 if (di != NULL)
4723 {
4724 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004725 if (dict_add(retdict, di) == FAIL)
4726 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004727 }
4728 }
4729 else
4730 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4731 }
4732
Bram Moolenaard823fa92016-08-12 16:29:27 +02004733 return status;
4734}
4735
4736/*
4737 * Add list of entries to quickfix/location list. Each list entry is
4738 * a dictionary with item information.
4739 */
4740 static int
4741qf_add_entries(
4742 qf_info_T *qi,
4743 list_T *list,
4744 char_u *title,
4745 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004746{
4747 listitem_T *li;
4748 dict_T *d;
4749 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004750 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004751 long lnum;
4752 int col, nr;
4753 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004754#ifdef FEAT_WINDOWS
4755 qfline_T *old_last = NULL;
4756#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004757 int valid, status;
4758 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004759 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004760
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004761 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004762 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004763 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004764#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004765 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4766 /* Adding to existing list, use last entry. */
4767 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004768#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004769 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004770 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004771 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004772 qf_store_title(qi, title);
4773 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004774
4775 for (li = list->lv_first; li != NULL; li = li->li_next)
4776 {
4777 if (li->li_tv.v_type != VAR_DICT)
4778 continue; /* Skip non-dict items */
4779
4780 d = li->li_tv.vval.v_dict;
4781 if (d == NULL)
4782 continue;
4783
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004784 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004785 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4786 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4787 col = (int)get_dict_number(d, (char_u *)"col");
4788 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4789 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004790 type = get_dict_string(d, (char_u *)"type", TRUE);
4791 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4792 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004793 if (text == NULL)
4794 text = vim_strsave((char_u *)"");
4795
4796 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004797 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004798 valid = FALSE;
4799
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004800 /* Mark entries with non-existing buffer number as not valid. Give the
4801 * error message only once. */
4802 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4803 {
4804 if (!did_bufnr_emsg)
4805 {
4806 did_bufnr_emsg = TRUE;
4807 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4808 }
4809 valid = FALSE;
4810 bufnum = 0;
4811 }
4812
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02004813 /* If the 'valid' field is present it overrules the detected value. */
4814 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
4815 valid = (int)get_dict_number(d, (char_u *)"valid");
4816
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004817 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004818 NULL, /* dir */
4819 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004820 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004821 text,
4822 lnum,
4823 col,
4824 vcol, /* vis_col */
4825 pattern, /* search pattern */
4826 nr,
4827 type == NULL ? NUL : *type,
4828 valid);
4829
4830 vim_free(filename);
4831 vim_free(pattern);
4832 vim_free(text);
4833 vim_free(type);
4834
4835 if (status == FAIL)
4836 {
4837 retval = FAIL;
4838 break;
4839 }
4840 }
4841
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004842 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004843 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004844 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4845 else
4846 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004847 if (action != 'a')
4848 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004849 qi->qf_lists[qi->qf_curlist].qf_ptr =
4850 qi->qf_lists[qi->qf_curlist].qf_start;
4851 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4852 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4853 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004854
4855#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004856 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004857 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004858#endif
4859
4860 return retval;
4861}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004862
4863 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004864qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004865{
4866 dictitem_T *di;
4867 int retval = FAIL;
4868 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004869 int newlist = FALSE;
4870
4871 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4872 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004873
4874 qf_idx = qi->qf_curlist; /* default is the current list */
4875 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4876 {
4877 /* Use the specified quickfix/location list */
4878 if (di->di_tv.v_type == VAR_NUMBER)
4879 {
4880 qf_idx = di->di_tv.vval.v_number - 1;
4881 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4882 return FAIL;
4883 }
4884 else
4885 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004886 newlist = FALSE; /* use the specified list */
4887 }
4888
4889 if (newlist)
4890 {
4891 qf_new_list(qi, NULL);
4892 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004893 }
4894
4895 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4896 {
4897 if (di->di_tv.v_type == VAR_STRING)
4898 {
4899 vim_free(qi->qf_lists[qf_idx].qf_title);
4900 qi->qf_lists[qf_idx].qf_title =
4901 get_dict_string(what, (char_u *)"title", TRUE);
4902 if (qf_idx == qi->qf_curlist)
4903 qf_update_win_titlevar(qi);
4904 retval = OK;
4905 }
4906 }
4907
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004908 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
4909 {
4910 typval_T *ctx;
4911 free_tv(qi->qf_lists[qi->qf_curlist].qf_ctx);
4912 ctx = alloc_tv();
4913 if (ctx != NULL)
4914 copy_tv(&di->di_tv, ctx);
4915 qi->qf_lists[qi->qf_curlist].qf_ctx = ctx;
4916 }
4917
Bram Moolenaard823fa92016-08-12 16:29:27 +02004918 return retval;
4919}
4920
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004921/*
4922 * Find the non-location list window with the specified location list.
4923 */
4924 static win_T *
4925find_win_with_ll(qf_info_T *qi)
4926{
4927 win_T *wp = NULL;
4928
4929 FOR_ALL_WINDOWS(wp)
4930 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
4931 return wp;
4932
4933 return NULL;
4934}
4935
4936/*
4937 * Free the entire quickfix/location list stack.
4938 * If the quickfix/location list window is open, then clear it.
4939 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004940 static void
4941qf_free_stack(win_T *wp, qf_info_T *qi)
4942{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004943 win_T *qfwin = qf_find_win(qi);
4944 win_T *llwin = NULL;
4945 win_T *orig_wp = wp;
4946
4947 if (qfwin != NULL)
4948 {
4949 /* If the quickfix/location list window is open, then clear it */
4950 if (qi->qf_curlist < qi->qf_listcount)
4951 qf_free(qi, qi->qf_curlist);
4952 qf_update_buffer(qi, NULL);
4953 }
4954
4955 if (wp != NULL && IS_LL_WINDOW(wp))
4956 {
4957 /* If in the location list window, then use the non-location list
4958 * window with this location list (if present)
4959 */
4960 llwin = find_win_with_ll(qi);
4961 if (llwin != NULL)
4962 wp = llwin;
4963 }
4964
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004965 qf_free_all(wp);
4966 if (wp == NULL)
4967 {
4968 /* quickfix list */
4969 qi->qf_curlist = 0;
4970 qi->qf_listcount = 0;
4971 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004972 else if (IS_LL_WINDOW(orig_wp))
4973 {
4974 /* If the location list window is open, then create a new empty
4975 * location list */
4976 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02004977
Bram Moolenaard788f6f2017-04-23 17:19:43 +02004978 /* first free the list reference in the location list window */
4979 ll_free_all(&orig_wp->w_llist_ref);
4980
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004981 orig_wp->w_llist_ref = new_ll;
4982 if (llwin != NULL)
4983 {
4984 llwin->w_llist = new_ll;
4985 new_ll->qf_refcount++;
4986 }
4987 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004988}
4989
Bram Moolenaard823fa92016-08-12 16:29:27 +02004990/*
4991 * Populate the quickfix list with the items supplied in the list
4992 * of dictionaries. "title" will be copied to w:quickfix_title.
4993 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
4994 */
4995 int
4996set_errorlist(
4997 win_T *wp,
4998 list_T *list,
4999 int action,
5000 char_u *title,
5001 dict_T *what)
5002{
5003 qf_info_T *qi = &ql_info;
5004 int retval = OK;
5005
5006 if (wp != NULL)
5007 {
5008 qi = ll_get_or_alloc_list(wp);
5009 if (qi == NULL)
5010 return FAIL;
5011 }
5012
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005013 if (action == 'f')
5014 {
5015 /* Free the entire quickfix or location list stack */
5016 qf_free_stack(wp, qi);
5017 }
5018 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005019 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005020 else
5021 retval = qf_add_entries(qi, list, title, action);
5022
5023 return retval;
5024}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005025
5026 static int
5027mark_quickfix_ctx(qf_info_T *qi, int copyID)
5028{
5029 int i;
5030 int abort = FALSE;
5031 typval_T *ctx;
5032
5033 for (i = 0; i < LISTCOUNT && !abort; ++i)
5034 {
5035 ctx = qi->qf_lists[i].qf_ctx;
5036 if (ctx != NULL && ctx->v_type != VAR_NUMBER &&
5037 ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
5038 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5039 }
5040
5041 return abort;
5042}
5043
5044/*
5045 * Mark the context of the quickfix list and the location lists (if present) as
5046 * "in use". So that garabage collection doesn't free the context.
5047 */
5048 int
5049set_ref_in_quickfix(int copyID)
5050{
5051 int abort = FALSE;
5052 tabpage_T *tp;
5053 win_T *win;
5054
5055 abort = mark_quickfix_ctx(&ql_info, copyID);
5056 if (abort)
5057 return abort;
5058
5059 FOR_ALL_TAB_WINDOWS(tp, win)
5060 {
5061 if (win->w_llist != NULL)
5062 {
5063 abort = mark_quickfix_ctx(win->w_llist, copyID);
5064 if (abort)
5065 return abort;
5066 }
5067 }
5068
5069 return abort;
5070}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005071#endif
5072
Bram Moolenaar81695252004-12-29 20:58:21 +00005073/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005074 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005075 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005076 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005077 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005078 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005079 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005080 */
5081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005082ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005083{
5084 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005085 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005086#ifdef FEAT_AUTOCMD
5087 char_u *au_name = NULL;
5088#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005089
Bram Moolenaardb552d602006-03-23 22:59:57 +00005090 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5091 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005092 {
5093 qi = ll_get_or_alloc_list(curwin);
5094 if (qi == NULL)
5095 return;
5096 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005097
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005098#ifdef FEAT_AUTOCMD
5099 switch (eap->cmdidx)
5100 {
5101 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5102 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5103 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5104 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5105 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5106 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5107 default: break;
5108 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005109 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5110 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005111 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005112# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005113 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005114 return;
5115# endif
5116 }
5117#endif
5118
Bram Moolenaar86b68352004-12-27 21:59:20 +00005119 if (*eap->arg == NUL)
5120 buf = curbuf;
5121 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5122 buf = buflist_findnr(atoi((char *)eap->arg));
5123 if (buf == NULL)
5124 EMSG(_(e_invarg));
5125 else if (buf->b_ml.ml_mfp == NULL)
5126 EMSG(_("E681: Buffer is not loaded"));
5127 else
5128 {
5129 if (eap->addr_count == 0)
5130 {
5131 eap->line1 = 1;
5132 eap->line2 = buf->b_ml.ml_line_count;
5133 }
5134 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5135 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5136 EMSG(_(e_invrange));
5137 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005138 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005139 char_u *qf_title = *eap->cmdlinep;
5140
5141 if (buf->b_sfname)
5142 {
5143 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5144 (char *)qf_title, (char *)buf->b_sfname);
5145 qf_title = IObuff;
5146 }
5147
Bram Moolenaardb552d602006-03-23 22:59:57 +00005148 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
5149 (eap->cmdidx != CMD_caddbuffer
5150 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005151 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005152 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005153 {
5154#ifdef FEAT_AUTOCMD
5155 if (au_name != NULL)
5156 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5157 curbuf->b_fname, TRUE, curbuf);
5158#endif
5159 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5160 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5161 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005162 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005163 }
5164}
5165
Bram Moolenaar1e015462005-09-25 22:16:38 +00005166#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005167/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005168 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5169 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005170 */
5171 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005172ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005173{
5174 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005175 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005176#ifdef FEAT_AUTOCMD
5177 char_u *au_name = NULL;
5178#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005179
Bram Moolenaardb552d602006-03-23 22:59:57 +00005180 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5181 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005182 {
5183 qi = ll_get_or_alloc_list(curwin);
5184 if (qi == NULL)
5185 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005186 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005187
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005188#ifdef FEAT_AUTOCMD
5189 switch (eap->cmdidx)
5190 {
5191 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5192 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5193 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5194 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5195 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5196 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5197 default: break;
5198 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005199 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5200 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005201 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005202# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005203 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005204 return;
5205# endif
5206 }
5207#endif
5208
Bram Moolenaar4770d092006-01-12 23:22:24 +00005209 /* Evaluate the expression. When the result is a string or a list we can
5210 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005211 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005212 if (tv != NULL)
5213 {
5214 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5215 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5216 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01005217 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005218 (eap->cmdidx != CMD_caddexpr
5219 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005220 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5221 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005222 {
5223#ifdef FEAT_AUTOCMD
5224 if (au_name != NULL)
5225 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5226 curbuf->b_fname, TRUE, curbuf);
5227#endif
5228 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5229 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5230 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005231 }
5232 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005233 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005234 free_tv(tv);
5235 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005236}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005237#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005238
5239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 * ":helpgrep {pattern}"
5241 */
5242 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005243ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244{
5245 regmatch_T regmatch;
5246 char_u *save_cpo;
5247 char_u *p;
5248 int fcount;
5249 char_u **fnames;
5250 FILE *fd;
5251 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005253#ifdef FEAT_MULTI_LANG
5254 char_u *lang;
5255#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005256 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005257 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005258 int new_qi = FALSE;
5259 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005260#ifdef FEAT_AUTOCMD
5261 char_u *au_name = NULL;
5262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005264#ifdef FEAT_MULTI_LANG
5265 /* Check for a specified language */
5266 lang = check_help_lang(eap->arg);
5267#endif
5268
Bram Moolenaar73633f82012-01-20 13:39:07 +01005269#ifdef FEAT_AUTOCMD
5270 switch (eap->cmdidx)
5271 {
5272 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5273 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5274 default: break;
5275 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005276 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5277 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005278 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005279# ifdef FEAT_EVAL
5280 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005281 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005282# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005283 }
5284#endif
5285
5286 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5287 save_cpo = p_cpo;
5288 p_cpo = empty_option;
5289
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005290 if (eap->cmdidx == CMD_lhelpgrep)
5291 {
5292 /* Find an existing help window */
5293 FOR_ALL_WINDOWS(wp)
5294 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5295 break;
5296
5297 if (wp == NULL) /* Help window not found */
5298 qi = NULL;
5299 else
5300 qi = wp->w_llist;
5301
5302 if (qi == NULL)
5303 {
5304 /* Allocate a new location list for help text matches */
5305 if ((qi = ll_new_list()) == NULL)
5306 return;
5307 new_qi = TRUE;
5308 }
5309 }
5310
Bram Moolenaaree85df32017-03-19 14:19:50 +01005311 /* Autocommands may change the list. Save it for later comparison */
5312 save_qi = qi;
5313
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5315 regmatch.rm_ic = FALSE;
5316 if (regmatch.regprog != NULL)
5317 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005318#ifdef FEAT_MBYTE
5319 vimconv_T vc;
5320
5321 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5322 * differs. */
5323 vc.vc_type = CONV_NONE;
5324 if (!enc_utf8)
5325 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5326#endif
5327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005329 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331 /* Go through all directories in 'runtimepath' */
5332 p = p_rtp;
5333 while (*p != NUL && !got_int)
5334 {
5335 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5336
5337 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5338 add_pathsep(NameBuff);
5339 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5340 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5341 &fnames, EW_FILE|EW_SILENT) == OK
5342 && fcount > 0)
5343 {
5344 for (fi = 0; fi < fcount && !got_int; ++fi)
5345 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005346#ifdef FEAT_MULTI_LANG
5347 /* Skip files for a different language. */
5348 if (lang != NULL
5349 && STRNICMP(lang, fnames[fi]
5350 + STRLEN(fnames[fi]) - 3, 2) != 0
5351 && !(STRNICMP(lang, "en", 2) == 0
5352 && STRNICMP("txt", fnames[fi]
5353 + STRLEN(fnames[fi]) - 3, 3) == 0))
5354 continue;
5355#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005356 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (fd != NULL)
5358 {
5359 lnum = 1;
5360 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5361 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005362 char_u *line = IObuff;
5363#ifdef FEAT_MBYTE
5364 /* Convert a line if 'encoding' is not utf-8 and
5365 * the line contains a non-ASCII character. */
5366 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005367 && has_non_ascii(IObuff))
5368 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005369 line = string_convert(&vc, IObuff, NULL);
5370 if (line == NULL)
5371 line = IObuff;
5372 }
5373#endif
5374
5375 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005377 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
5379 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005380 while (l > 0 && line[l - 1] <= ' ')
5381 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005383 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 NULL, /* dir */
5385 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005386 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005387 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005389 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005390 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005391 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005392 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 0, /* nr */
5394 1, /* type */
5395 TRUE /* valid */
5396 ) == FAIL)
5397 {
5398 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005399#ifdef FEAT_MBYTE
5400 if (line != IObuff)
5401 vim_free(line);
5402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 break;
5404 }
5405 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005406#ifdef FEAT_MBYTE
5407 if (line != IObuff)
5408 vim_free(line);
5409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 ++lnum;
5411 line_breakcheck();
5412 }
5413 fclose(fd);
5414 }
5415 }
5416 FreeWild(fcount, fnames);
5417 }
5418 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005419
Bram Moolenaar473de612013-06-08 18:19:48 +02005420 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005421#ifdef FEAT_MBYTE
5422 if (vc.vc_type != CONV_NONE)
5423 convert_setup(&vc, NULL, NULL);
5424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005426 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5427 qi->qf_lists[qi->qf_curlist].qf_ptr =
5428 qi->qf_lists[qi->qf_curlist].qf_start;
5429 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005432 if (p_cpo == empty_option)
5433 p_cpo = save_cpo;
5434 else
5435 /* Darn, some plugin changed the value. */
5436 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
5438#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005439 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440#endif
5441
Bram Moolenaar73633f82012-01-20 13:39:07 +01005442#ifdef FEAT_AUTOCMD
5443 if (au_name != NULL)
5444 {
5445 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5446 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005447 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005448 /* autocommands made "qi" invalid */
5449 return;
5450 }
5451#endif
5452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005454 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005455 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005456 else
5457 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005458
5459 if (eap->cmdidx == CMD_lhelpgrep)
5460 {
5461 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005462 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005463 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5464 {
5465 if (new_qi)
5466 ll_free_all(&qi);
5467 }
5468 else if (curwin->w_llist == NULL)
5469 curwin->w_llist = qi;
5470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471}
5472
5473#endif /* FEAT_QUICKFIX */