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