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