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