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