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