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