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