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