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